-
Notifications
You must be signed in to change notification settings - Fork 5
/
extractor.py
53 lines (42 loc) · 1.92 KB
/
extractor.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
import tensorflow as tf
import numpy as np
class Extractor():
def __init__(self, weights=None):
"""Either load pretrained from imagenet, or load our saved
weights from our own training."""
self.weights = weights # so we can check elsewhere which model
if weights is None:
# Get model with pretrained weights.
base_model = tf.contrib.keras.applications.inception_v3.InceptionV3(
weights='imagenet',
include_top=True
)
# We'll extract features at the final pool layer.
self.model = tf.contrib.keras.models.Model(
inputs=base_model.input,
outputs=base_model.get_layer('avg_pool').output
)
else:
# Load the model first.
self.model = tf.contrib.keras.models.load_model(weights)
# Then remove the top so we get features not predictions.
# From: https://github.com/fchollet/keras/issues/2371
self.model.layers.pop()
self.model.layers.pop() # two pops to get to pool layer
self.model.outputs = [self.model.layers[-1].output]
self.model.output_layers = [self.model.layers[-1]]
self.model.layers[-1].outbound_nodes = []
def extract(self, image_path):
img = tf.contrib.keras.preprocessing.image.load_img(image_path, target_size=(299, 299))
x = tf.contrib.keras.preprocessing.image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = tf.contrib.keras.applications.inception_v3.preprocess_input(x)
# Get the prediction.
features = self.model.predict(x)
if self.weights is None:
# For imagenet/default network:
features = features[0]
else:
# For loaded network:
features = features[0]
return features