-
Notifications
You must be signed in to change notification settings - Fork 1
/
convphashaec.py
187 lines (166 loc) · 7.68 KB
/
convphashaec.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import os
import numpy as np
import tensorflow as tf
class ConvPhashAutoEnc:
""" Convolutional Perceptual Hash
"""
def __init__(self, model_file):
""" init method
args
model_file -- file for inception/autoenc model
"""
with tf.gfile.GFile(model_file, 'rb') as f:
self.classif_graph_def = tf.GraphDef()
self.classif_graph_def.ParseFromString(f.read())
self.graph = tf.Graph()
with self.graph.as_default():
tf.import_graph_def(self.classif_graph_def, name="aec")
self.file_in = self.graph.get_tensor_by_name('aec/preprocess/input:0')
self.img_out = self.graph.get_tensor_by_name('aec/preprocess/output:0')
self.in_tensor = self.graph.get_tensor_by_name('aec/classifier/input:0')
self.feature_tensor = self.graph.get_tensor_by_name('aec/classifier/feature_vector:0')
self.out = self.graph.get_tensor_by_name('aec/aec_encoder/output:0')
# self.out1024 = self.graph.get_tensor_by_name('aec/aec_encoder/output1024:0')
# self.out512 = self.graph.get_tensor_by_name('aec/aec_encoder/output512:0')
# self.out256 = self.graph.get_tensor_by_name('aec/aec_encoder/output256:0')
# self.out128 = self.graph.get_tensor_by_name('aec/aec_encoder/output128:0')
# self.out64 = self.graph.get_tensor_by_name('aec/aec_encoder/output64:0')
# self.out32 = self.graph.get_tensor_by_name('aec/aec_encoder/output32:0')
self.sess = tf.Session(graph=self.graph)
def process_image_files(self, img_dir, limit=50):
""" process images from files in a directory
args
img_dir -- directory of jpeg images
return generator of ndarray's (shape: limit x h x w x 3)
"""
arrlist = []
count = 0
for entry in os.scandir(img_dir):
if entry.is_file() and entry.name.endswith('.jpg'):
img_data = self.sess.run(self.img_out,
feed_dict={self.file_in: entry.path})
arrlist.append(img_data)
count = count + 1
if count >= limit:
count = 0
yield np.concatenate(arrlist)
arrlist.clear()
if count > 0:
yield np.concatenate(arrlist)
def image_features(self, img_dir, limit=50):
""" Get feature vectors for images from module
args
images -- ndarray of images obtained from process_image_files()
size: [no_images x 299 x 299 x 3]
return
features -- ndarray (size no_images x feature_length)
"""
features_list = []
images_gen = self.process_image_files(img_dir, limit=limit)
with self.graph.as_default():
for np_images in images_gen:
features = self.sess.run(self.feature_tensor,
feed_dict={self.in_tensor: np_images})
features_list.append(features)
concatfeatures = np.concatenate(features_list, axis=0)
return concatfeatures
def image_condensed(self, img_dir, limit=50):
""" Compute condensed image perceptual hashes from features.
args
features -- ndarray of features (size: [no_images x 1 x 1 x 2048])
return -- ndarray of final hashes (size: no_images x 256)
"""
features_list = []
images_gen = self.process_image_files(img_dir, limit=limit)
with self.graph.as_default():
for np_images in images_gen:
features = self.sess.run(self.out,
feed_dict={self.in_tensor: np_images})
features_list.append(features)
concatfeatures = np.concatenate(features_list, axis=0)
return concatfeatures
'''
def image_condensed512(self, img_dir, limit=50):
""" Compute condensed image perceptual hashes from features.
args
features -- ndarray of features (size: [no_images x 1 x 1 x 2048])
return -- ndarray of final hashes (size: no_images x 256)
"""
features_list = []
images_gen = self.process_image_files(img_dir, limit=limit)
with self.graph.as_default():
for np_images in images_gen:
features = self.sess.run(self.out512,
feed_dict={self.in_tensor: np_images})
features_list.append(features)
concatfeatures = np.concatenate(features_list, axis=0)
return concatfeatures
'''
def image_condensed256(self, img_dir, limit=50):
""" Compute condensed image perceptual hashes from features.
args
features -- ndarray of features (size: [no_images x 1 x 1 x 2048])
return -- ndarray of final hashes (size: no_images x 256)
"""
features_list = []
images_gen = self.process_image_files(img_dir, limit=limit)
with self.graph.as_default():
for np_images in images_gen:
features = self.sess.run(self.out256,
feed_dict={self.in_tensor: np_images})
features_list.append(features)
concatfeatures = np.concatenate(features_list, axis=0)
return concatfeatures
'''
def image_condensed128(self, img_dir, limit=50):
""" Compute condensed image perceptual hashes from features.
args
features -- ndarray of features (size: [no_images x 1 x 1 x 2048])
return -- ndarray of final hashes (size: no_images x 256)
"""
features_list = []
images_gen = self.process_image_files(img_dir, limit=limit)
with self.graph.as_default():
for np_images in images_gen:
features = self.sess.run(self.out128,
feed_dict={self.in_tensor: np_images})
features_list.append(features)
concatfeatures = np.concatenate(features_list, axis=0)
return concatfeatures
def image_condensed64(self, img_dir, limit=50):
""" Compute condensed image perceptual hashes from features.
args
features -- ndarray of features (size: [no_images x 1 x 1 x 2048])
return -- ndarray of final hashes (size: no_images x 256)
"""
features_list = []
images_gen = self.process_image_files(img_dir, limit=limit)
with self.graph.as_default():
for np_images in images_gen:
features = self.sess.run(self.out64, feed_dict={self.in_tensor: np_images})
features_list.append(features)
concatfeatures = np.concatenate(features_list, axis=0)
return concatfeatures
def image_condensed32(self, img_dir, limit=50):
""" Compute condensed image perceptual hashes from features.
args
features -- ndarray of features (size: [no_images x 1 x 1 x 2048])
return -- ndarray of final hashes (size: no_images x 256)
"""
features_list = []
images_gen = self.process_image_files(img_dir, limit=limit)
with self.graph.as_default():
for np_images in images_gen:
features = self.sess.run(self.out32, feed_dict={self.in_tensor: np_images})
features_list.append(features)
concatfeatures = np.concatenate(features_list, axis=0)
return concatfeatures
'''
def l1_distance(self, x, y, axis=1):
diff = np.sum(np.abs((x.astype(float) - y.astype(float))),
axis=axis, keepdims=False)
return diff
def l2_distance(self, x, y, axis=1):
diff = np.sqrt(np.sum(np.square(x.astype(float)-y.astype(float)),
axis=axis, keepdims=False))
return diff