2014104094

K-means 클러스터링

...@@ -75,7 +75,7 @@ def mobile_net(input_shape): ...@@ -75,7 +75,7 @@ def mobile_net(input_shape):
75 75
76 x = mobile_net_block(x, 1024, 2) 76 x = mobile_net_block(x, 1024, 2)
77 x = mobile_net_block(x, 1024) 77 x = mobile_net_block(x, 1024)
78 - x = cbam_block(x) 78 + #x = cbam_block(x)
79 79
80 x = GlobalAvgPool2D()(x) 80 x = GlobalAvgPool2D()(x)
81 81
...@@ -153,4 +153,4 @@ if __name__ == "__main__": ...@@ -153,4 +153,4 @@ if __name__ == "__main__":
153 # predict sample 153 # predict sample
154 predict(model, X_to_predict, y_to_predict) 154 predict(model, X_to_predict, y_to_predict)
155 155
156 - model.save('C:/Users/nokh9/Desktop/mobile_net2.h5')
...\ No newline at end of file ...\ No newline at end of file
156 + model.save('C:/Users/nokh9/Desktop/수업/캡스톤디자인2/소스코드/mobile_net3.h5')
...\ No newline at end of file ...\ No newline at end of file
......
1 +"""
2 + VGG16 모델과 K-Means Clustering을 이용하여 소리에 대한 데이터를 군집화 후 분
3 +"""
4 +from keras.preprocessing import image
5 +from keras.applications.vgg16 import VGG16
6 +from keras.applications.vgg16 import preprocess_input
7 +import numpy as np
8 +from sklearn.cluster import KMeans
9 +import os, shutil, glob, os.path
10 +from PIL import Image as pil_image
11 +
12 +image.LOAD_TRUNCATED_IMAGES = True
13 +model = VGG16(weights='imagenet', include_top=False)
14 +
15 +imdir = 'PATH WHERE YOUR FILE IS'
16 +targetdir = 'PATH WHERE YOUT FILE SAVE'
17 +number_clusters = 2
18 +
19 +# Loop over files and get features
20 +filelist = glob.glob(os.path.join(imdir, '*.png'))
21 +#filelist.sort()
22 +featurelist = []
23 +
24 +for i, imagepath in enumerate(filelist):
25 + print(" Status: %s / %s" %(i, len(filelist)), end="\r")
26 + img = image.load_img(imagepath, target_size=(370, 470))
27 + img_data = image.img_to_array(img)
28 + img_data = np.expand_dims(img_data, axis=0)
29 + img_data = preprocess_input(img_data)
30 + features = np.array(model.predict(img_data))
31 + featurelist.append(features.flatten())
32 +
33 + if i % 10:
34 + print(i)
35 +
36 +# K-Means 클러스터링
37 +kmeans = KMeans(n_clusters=number_clusters, random_state=0).fit(np.array(featurelist))
38 +
39 +# Copy with cluster name
40 +print("\n")
41 +for i, m in enumerate(kmeans.labels_):
42 + print(" Copy: %s / %s" %(i, len(kmeans.labels_)), end="\r")
43 + shutil.copy(filelist[i], targetdir + str(m) + "_" + str(i) + ".jpg")
44 +
45 + if(i % 100) == 0:
46 + print(str(i)+'is finished')
...\ No newline at end of file ...\ No newline at end of file