-
Notifications
You must be signed in to change notification settings - Fork 3
/
clustering_script.py
172 lines (130 loc) · 4.39 KB
/
clustering_script.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
import numpy as np
import matplotlib.pyplot as plt
from keras.layers import Input, Dense, Conv2D, MaxPooling2D, UpSampling2D, Flatten
from keras.models import Model
from keras import backend as K
import tensorflow.keras.layers
import keras
import os
import tensorflow as tf
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
encoder = tf.keras.models.load_model('encoder', compile=False)
import zipfile
zip_ref = zipfile.ZipFile("Zipped_final.zip", 'r')
zip_ref.extractall("/tmp")
zip_ref.close()
from skimage.transform import resize
from skimage.io import imread_collection
def get_amazon():
col_dir = '/tmp/Zipped/Amazon Images/*.jpg'
col_amazon = imread_collection(col_dir)
col_amazon = list(col_amazon)
for i in range(len(col_amazon)):
col_amazon[i] = resize(col_amazon[i], (320, 192, 3))
return col_amazon
def get_flipkart():
col_dir = '/tmp/Zipped/Flipkart Images/*.jpg'
col_fk = imread_collection(col_dir)
col_fk = list(col_fk)
for i in range(len(col_fk)):
col_fk[i] = resize(col_fk[i], (320, 192, 3))
return col_fk
def get_myntra():
col_dir = '/tmp/Zipped/Myntra Images/*.jpg'
col_myntra = imread_collection(col_dir)
col_myntra = list(col_myntra)
for i in range(len(col_myntra)):
col_myntra[i] = resize(col_myntra[i], (320, 192, 3))
return col_myntra
def get_pinmen():
col_dir = '/tmp/Zipped/Pinterest Men Images/*.jpg'
col_pinmen = imread_collection(col_dir)
col_pinmen = list(col_pinmen)
for i in range(len(col_pinmen)):
col_pinmen[i] = resize(col_pinmen[i], (320, 192, 3))
return col_pinmen
def get_pinwomen():
col_dir = '/tmp/Zipped/Pinterest Women Images/*.jpg'
col_pinwom = imread_collection(col_dir)
col_pinwom = list(col_pinwom)
for i in range(len(col_pinwom)):
col_pinwom[i] = resize(col_pinwom[i], (320, 192, 3))
return col_pinwom
def get_vogue():
col_dir = '/tmp/Zipped/Vogue Images/*.jpg'
col_vog = imread_collection(col_dir)
col_vog = list(col_vog)
for i in range(len(col_vog)):
col_vog[i] = resize(col_vog[i], (320, 192, 3))
return col_vog
print("You can choose out of - ")
print("1. amazon (Amamzon)")
print("2. flipkart (Flipkart)")
print("3. myntra (Myntra)")
print("4. pinmen (Pinterest Men)")
print("5. pinwom (Pinterest Women)")
print("6. vogue (Vogue)")
user_input = input("Please enter your choices(separated by a comma): ")
user_input = user_input.split(",")
col_cluster = []
for cat in user_input:
if "pinmen" == cat:
col_cluster += get_pinmen()
elif "pinwom" == cat:
col_cluster += get_pinwomen()
elif "vogue" == cat:
col_cluster += get_vogue()
elif "myntra" == cat:
col_cluster += get_myntra()
elif "amazon" == cat:
col_cluster += get_amazon()
elif "flipkart" == cat:
col_cluster += get_flipkart()
print("Data collected successfully")
col_cluster = np.array(col_cluster)
encodings = encoder.predict(col_cluster)
flattened_encodings = []
for en in encodings:
en = en.flatten()
flattened_encodings.append(en)
print("Encodings calculated")
from sklearn.cluster import KMeans
clusters = int(len(flattened_encodings)/10)
kmeans = KMeans(n_clusters=clusters, init='k-means++', max_iter=500, n_init=10, random_state=0)
pred_y = kmeans.fit_predict(flattened_encodings)
print("Clustering complete")
from collections import Counter
list_freq= (Counter(pred_y))
list_freq = sorted(list_freq.items(), key=lambda x: x[1], reverse=True)
trend = list_freq[0][0]
lagging = []
for i in range(clusters-clusters//10, clusters):
lagging.append(list_freq[i][0])
frequent_imgs = []
lagging_imgs = []
for i in range(len(pred_y)):
if pred_y[i] == trend:
frequent_imgs.append(i)
if pred_y[i] in lagging:
lagging_imgs.append(i)
import os
directory = "cluster_trends"
img_path = os.path.join(directory)
if(os.path.isdir(img_path)==False):
os.mkdir(img_path)
print("Directory '% s' created" % directory)
directory = "cluster_lags"
img_path = os.path.join(directory)
if(os.path.isdir(img_path)==False):
os.mkdir(img_path)
print("Directory '% s' created" % directory)
from skimage.io import imsave
for j in range(len(frequent_imgs)):
img = (col_cluster[frequent_imgs[j]]*255).astype(np.uint8)
path = 'cluster_trends/'+str(j)+'.jpg'
imsave(path, img)
for j in range(len(lagging_imgs)):
img = (col_cluster[lagging_imgs[j]]*255).astype(np.uint8)
path = 'cluster_lags/'+str(j)+'.jpg'
imsave(path, img)
print("Done! Most popular images are saved on your device!")