-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
67 lines (44 loc) · 1.58 KB
/
utils.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
57
58
59
60
61
62
63
64
65
66
import pickle
import collections
import os
import json
import numpy as np
from keras.preprocessing import image as Image
from keras.applications.vgg19 import VGG19
from keras.models import Model
def save_pickle(data, path):
with open(path, 'wb') as f:
pickle.dump(data, f, pickle.HIGHEST_PROTOCOL)
print('Saved %s..' % path)
def load_pickle(path):
with open(path, 'rb') as f:
data = pickle.load(f)
return data
def save_json(data, path):
with open(path, 'w') as f:
json.dump(data, f)
print('Saved %s..' % path)
def _dict2namedtuple(dictionary):
return collections.namedtuple('GerericDic', dictionary.keys())(**dictionary)
def load_coco(data_dir, mode):
if mode == 'train':
data = _dict2namedtuple(load_pickle(os.path.join(data_dir, 'train_data.pkl')))
elif mode == 'val':
data = _dict2namedtuple(load_pickle(os.path.join(data_dir, 'val_data.pkl')))
else:
raise ValueError('mode is not provided')
return data
def feature_extractor(model_type=None):
if model_type == 'VGG19':
base_model = VGG19(weights='imagenet')
model = Model(inputs=base_model.inputs, outputs=base_model.get_layer('fc2').output)
else:
raise ValueError('model type is not provided')
return model
def feature_extraction(model, image_path=None):
assert isinstance(image_path, str), 'Image is not provided'
img = Image.load_img(image_path, target_size=(224, 224))
img = Image.img_to_array(img)
img = np.expand_dims(img, axis=0)
features = model.predict(img)
return features