-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprocess_images.py
More file actions
317 lines (245 loc) · 9.63 KB
/
process_images.py
File metadata and controls
317 lines (245 loc) · 9.63 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import argparse
import glob
import json
import os
from enum import Enum
import cv2
import numpy as np
from keras.applications.vgg16 import VGG16
from keras.applications.vgg16 import preprocess_input
from scipy.spatial.distance import cdist
from sklearn import preprocessing # to normalise
from sklearn.cluster import KMeans
from sklearn.decomposition import PCA
from PIL import Image
import pathlib
from shutil import copyfile
"""
Cluster images using CNN feature maps and PCA.
"""
IMAGE_SIZE = 320
class Mode(Enum):
CLUSTER = 'cluster'
CLASSIFY = 'classify'
RESIZE = 'resize'
DEDUPE = 'dedupe'
def __str__(self):
return self.value
def __repr__(self):
return str(self)
@staticmethod
def argparse(s):
try:
return Mode[s.upper()]
except KeyError:
return s
def glob_files(folder, file_type='*'):
search_string = os.path.join(folder, file_type)
files = glob.glob(search_string)
print('Searching ', search_string)
paths = []
for f in files:
if os.path.isdir(f):
sub_paths = glob_files(f + '/')
paths += sub_paths
else:
paths.append(f)
# We sort the images in alphabetical order to match them
# to the annotation files
paths.sort()
return paths
def glob_folders(folder, file_type='*'):
search_string = os.path.join(folder, file_type)
files = glob.glob(search_string)
print('Searching ', search_string, files)
paths = []
for f in files:
if os.path.isdir(f):
paths.append(f)
# We sort the images in alphabetical order to match them
# to the annotation files
paths.sort()
return paths
def to_feature_maps(path, file_type="*.png"):
def _to_feature_maps(X):
#Convert to VGG input format
X = preprocess_input(X)
#include_top=False == not getting VGG16 last 3 layers
model = VGG16(weights="imagenet", include_top=False)
#Get features
features = model.predict(X)
print(features.shape)
return features
files = glob_files(path, file_type)
files_processed = []
feature_maps = []
for file in files:
print(file)
image = cv2.imread(file)
if image is not None:
image = cv2.resize(image, (IMAGE_SIZE, IMAGE_SIZE))
# doing it one at a time to reduce the memory foot print
image = image / 255
fm = _to_feature_maps(np.array([image]))
feature_maps.append(fm)
files_processed.append(file)
else:
print(file, ' is not an image file')
return np.array(feature_maps), files_processed
def to_pca_reduced(x_features, dimensions=2):
"""
reduces dimensions of the input.
This is mainly for plotting purposes.
:param x_features: feature maps
:param dimensions: target number of dimensions
:return: reduced features
"""
X_features_flatten = x_features.reshape(x_features.shape[0], -1)
print("Flattened shape: ", X_features_flatten.shape)
pca = PCA(dimensions)
X_features_pca_reduced = pca.fit_transform(X_features_flatten)
return X_features_pca_reduced, pca
def to_clusters(x_reduced, K):
kmeans = KMeans(n_clusters=K, random_state=0)
X_clusters = kmeans.fit(x_reduced)
return X_clusters, kmeans
def to_cluster_idx(cluster_labels, bins):
"""
param bins: range of K
param labels: cluster labels
returns: dictionary of cluster IDs
"""
cluster_dict = dict()
for cluster_id in bins:
cluster_dict[cluster_id] = np.where(cluster_labels == cluster_id)[0]
return cluster_dict
def cluster_images(folder, file_type="*"):
X_fm, filenames = to_feature_maps(folder, file_type=file_type)
print("####", X_fm.shape)
# normalize to use cosine similarity
X_fm_normalized = preprocessing.normalize(X_fm.reshape(len(X_fm), -1))
print(X_fm_normalized.shape)
# number of clusters
K = 2
# # Dimensionality reduction through PCA.
# # This is optional.
# # We are using it mainly for plotting purposes.
# X_reduced, pca = to_pca_reduced(X_fm_normalized, dimensions=K)
# cluster using feature maps or PCA reduced features
# X_clusters, kmeans = to_clusters(X_reduced, K)
X_clusters, kmeans = to_clusters(X_fm_normalized, K)
# get the image ids of each cluster
cluster_idx = to_cluster_idx(X_clusters.labels_, range(K))
# keep the cluster centers
print(kmeans.cluster_centers_)
print(cluster_idx)
centroids_filename = 'centroids.json'
to_json(centroids_filename, kmeans.cluster_centers_.tolist())
print('centroid values are saved as {}'.format(centroids_filename))
for key, idx in cluster_idx.items():
print("Cluster {}".format(key))
for id in idx:
print("\t{}".format(filenames[id]))
def to_json(path, data):
"""
save json data to path
"""
with open(path, 'w', encoding='utf-8') as file:
json.dump(data, file, ensure_ascii=False, indent=4)
def from_json(path):
"""
save json data to path
"""
file = open(path, 'r', encoding='utf-8')
return json.load(file)
def classify(folder, centroids_file, file_type='*', threshold=0.7):
centroids = from_json(centroids_file)
X_fm, filenames = to_feature_maps(folder, file_type=file_type)
print("####", X_fm.shape)
# normalize to use cosine similarity
X_fm = preprocessing.normalize(X_fm.reshape(len(X_fm), -1))
# use cosine to calculate similarities
dist = cdist(X_fm, centroids, metric='cosine')
print(dist)
for id, d, filename in zip(range(len(filenames)), dist, filenames):
cluster_id = np.argmin(d)
dist_min = np.min(d)
print("{}: {} is {}".format(id, os.path.basename(filename), cluster_id))
if dist_min > threshold:
print("\t{} might not belong to any cluster. {}".format(os.path.basename(filename), d))
print("\tTime to create a new cluster")
def resize(path_in, file_type='*'):
files = glob_files(path_in, file_type)
path_out = os.path.join(path_in, 'resized')
# create the folder if it doesn't exist
pathlib.Path(path_out).mkdir(parents=True, exist_ok=True)
for file in files:
image = Image.open(file)
# I downsize the image with an ANTIALIAS filter (gives the highest quality)
if image.size[0] > 1000:
image = image.resize((int(image.size[0]/5), int(image.size[1]/5)))
print(image.size[0], image.size[1])
filename_out = os.path.join(path_out, os.path.basename(file))
image.save(filename_out)
def find_duplicates(X_train_pca, threshold=0.001):
# Calculate distances of all points
distances = cdist(X_train_pca, X_train_pca)
# Find duplicates (very similar images)
# dupes = np.array([np.where(distances[id] < 1) for id in range(distances.shape[0])]).reshape(-1)
dupes = [np.array(np.where(distances[id] < threshold)).reshape(-1).tolist() \
for id in range(distances.shape[0])]
to_remove = set()
for d in dupes:
if len(d) > 1:
for id in range(1, len(d)):
to_remove.add(d[id])
print("Found {} duplicates {}".format(len(to_remove), to_remove))
return to_remove
def dedupe(path_in, path_out, threshold=0.1, file_type='*'):
subfolders_in = glob_folders(path_in, file_type=file_type)
if path_out is None:
path_out = os.path.join(path_in, 'deduped')
# create the folder if it doesn't exist
pathlib.Path(path_out).mkdir(parents=True, exist_ok=True)
for i, subfolder_in in zip(range(len(subfolders_in)), subfolders_in):
if i < 1:
print("Skipping " + subfolder_in)
continue
X_fm, filenames = to_feature_maps(subfolder_in, file_type=file_type)
print("####", X_fm.shape)
# normalize to use cosine similarity
X_fm_normalized = preprocessing.normalize(X_fm.reshape(len(X_fm), -1))
# number of clusters
K = 2
# # Dimensionality reduction through PCA.
# # This is optional.
# # We are using it mainly for plotting purposes.
X_reduced, pca = to_pca_reduced(X_fm_normalized, dimensions=K)
to_remove_idx = find_duplicates(X_reduced, threshold=threshold)
subfolder_out = os.path.join(path_out, os.path.basename(subfolder_in))
# create the folder if it doesn't exist
pathlib.Path(subfolder_out).mkdir(parents=True, exist_ok=True)
for id, filename_from in zip(range(len(filenames)), filenames):
if id not in to_remove_idx:
filename_out = os.path.join(subfolder_out, os.path.basename(filename_from))
copyfile(filename_from, filename_out)
print("Copied {}".format(filename_out))
print("Done!")
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("-mode", action="store", type=Mode.argparse, choices=list(Mode), dest="mode")
parser.add_argument("-path", action="store", dest="path", type=str)
parser.add_argument("-path_out", action="store", dest="path_out", type=str)
parser.add_argument("-centroids_json", action="store", dest="centroids_json", type=str)
parser.add_argument("-threshold", action="store", dest="threshold", type=float, default=80)
args = parser.parse_args()
if args.mode == Mode.CLUSTER:
cluster_images(args.path)
elif args.mode == Mode.CLASSIFY:
classify(args.path, args.centroids_json)
elif args.mode == Mode.RESIZE:
resize(args.path)
elif args.mode == Mode.DEDUPE:
dedupe(args.path, args.path_out, threshold=args.threshold)
else:
raise ValueError("Specify [-cluster | -classify | -resize | -dedupe ] option")