forked from benlawson/expert-broccoli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
representation.py
57 lines (45 loc) · 1.62 KB
/
representation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#standard libraries
import os
import glob
from collections import defaultdict
#external libraries
import numpy as np
import joblib
from keras.applications.inception_v3 import InceptionV3, preprocess_input
from keras.preprocessing.image import load_img, img_to_array
# import theano
# theano.config.openmp = True
def preprocess(imgs):
# shape: (# samples, flattened 1 x 299 x 299 x 3 dimensions)
preprocessed = []
for img in imgs:
img = np.expand_dims(img, axis=0)
img = preprocess_input(img)
preprocessed.append(img.flatten())
return preprocessed
model = InceptionV3(include_top=False, pooling='avg')
cat_dict = defaultdict(list)
# filenames = sorted(glob.glob("/backupdrive/datasets/BUSampleDataSet/*.jpg"))
for folder in sorted(glob.glob("../WinEarthPhotosByKeyword/*")):
print(folder)
filenames = sorted(glob.glob(os.path.join(folder, "*.jpg")))
for filename in filenames:
image = img_to_array(load_img(filename, target_size=(299,299)))
cat_dict[folder].append(image)
#print("loaded all images")
# joblib.dump(cat_dict, "images.joblib")
# cat_dict = joblib.load("images.joblib")
# images = joblib.load("images.joblib")
imgs = []
reps = []
labels = []
for cat in sorted(cat_dict.keys()):
print("representing {}".format(cat))
representations = model.predict(np.array(cat_dict[cat]))
reps.extend(representations)
preprocessed = preprocess(cat_dict[cat])
imgs.extend(preprocessed)
for _ in range(len(cat_dict[cat])):
labels.append(cat)
joblib.dump((reps, labels), "representations.joblib")
joblib.dump((imgs, labels), "preprocessed_imgs.joblib")