2014104094

K-means 클러스터링

......@@ -75,7 +75,7 @@ def mobile_net(input_shape):
x = mobile_net_block(x, 1024, 2)
x = mobile_net_block(x, 1024)
x = cbam_block(x)
#x = cbam_block(x)
x = GlobalAvgPool2D()(x)
......@@ -153,4 +153,4 @@ if __name__ == "__main__":
# predict sample
predict(model, X_to_predict, y_to_predict)
model.save('C:/Users/nokh9/Desktop/mobile_net2.h5')
\ No newline at end of file
model.save('C:/Users/nokh9/Desktop/수업/캡스톤디자인2/소스코드/mobile_net3.h5')
\ No newline at end of file
......
"""
VGG16 모델과 K-Means Clustering을 이용하여 소리에 대한 데이터를 군집화 후 분
"""
from keras.preprocessing import image
from keras.applications.vgg16 import VGG16
from keras.applications.vgg16 import preprocess_input
import numpy as np
from sklearn.cluster import KMeans
import os, shutil, glob, os.path
from PIL import Image as pil_image
image.LOAD_TRUNCATED_IMAGES = True
model = VGG16(weights='imagenet', include_top=False)
imdir = 'PATH WHERE YOUR FILE IS'
targetdir = 'PATH WHERE YOUT FILE SAVE'
number_clusters = 2
# Loop over files and get features
filelist = glob.glob(os.path.join(imdir, '*.png'))
#filelist.sort()
featurelist = []
for i, imagepath in enumerate(filelist):
print(" Status: %s / %s" %(i, len(filelist)), end="\r")
img = image.load_img(imagepath, target_size=(370, 470))
img_data = image.img_to_array(img)
img_data = np.expand_dims(img_data, axis=0)
img_data = preprocess_input(img_data)
features = np.array(model.predict(img_data))
featurelist.append(features.flatten())
if i % 10:
print(i)
# K-Means 클러스터링
kmeans = KMeans(n_clusters=number_clusters, random_state=0).fit(np.array(featurelist))
# Copy with cluster name
print("\n")
for i, m in enumerate(kmeans.labels_):
print(" Copy: %s / %s" %(i, len(kmeans.labels_)), end="\r")
shutil.copy(filelist[i], targetdir + str(m) + "_" + str(i) + ".jpg")
if(i % 100) == 0:
print(str(i)+'is finished')
\ No newline at end of file