from tensorflow.keras.applications import vgg16
# resnet, inception ... 등 다양한 모델 적용해볼 예정
from tensorflow.keras.applications.imagenet_utils import preprocess_input
from tensorflow.keras.preprocessing.image import load_img,img_to_array
from tensorflow.keras.models import Model
import os
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn.metrics.pairwise import cosine_similarity
vgg_model = vgg16.VGG16(weights='imagenet')
#imagenet 가중치 가져오기
model = Model(inputs=vgg_model.input, outputs=vgg_model.get_layer("fc2").output)
# output 부분 fully-connected를 추가 !
# 특징을 추출해서 유사한 이미지를 찾는 것이 목적 .
# label 예측 XXXXXXXX
model.summary()
img_path = 'style/'
img_width, img_height = 224, 224
most_similar = 5
# 유사한 아이템 몇개를 뽑을것인지
files = [img_path + x for x in os.listdir(img_path) if 'png' in x]
image_1 = load_img(files[0], target_size=(img_width, img_height, 3))
# (img_width, img_height, 3) 3채널이 아닌 4채널로 나오기에 3채널로 변경
plt.imshow(image_1)
plt.show()
img_array = img_to_array(image_1)
# 이미지 array 읽어오기
img_batch = np.expand_dims(img_array, axis=0)
#(batch_size, img_width, img_height, channel)으로 만들기 위함
# batch_size = None이면 모델이 작동하지 않음
print('image_batch_size=',img_batch.shape)
# 4_channel -> 3_channel로 변경
processed_image = preprocess_input(img_batch)
# tf.kears.applications.[model] import preprocess_input
# 불러온 모델에 가장 잘 맞게 이미지를 맞추기위함.
processed_image
img_feature = model.predict(processed_image)
print('number of image features: ',img_feature.size)
img_feature
image_list = []
for f in files:
file_name = f
image = load_img(file_name, target_size=(224, 224, 3))
img_array = img_to_array(image)
img_batch = np.expand_dims(img_array, axis=0)
image_list.append(img_batch)
images = np.vstack(image_list)
# vstack -> 세로로 합치기
# hstack -> 가로로 합치기
processed_imgs = preprocess_input(images)
img_feature = model.predict(processed_imgs)
cosine = cosine_similarity(img_feature)
#예측 값으로 유사도 구하기
cos_df = pd.DataFrame(cosine, columns=files, index=files)
#pd.DataFrame(value, columns, index)
#files = img_name
# index, column -> 이미지이름으로 하고 예측했던 값들로 value
cos_df
def similarity_image(img):
print('-'*30)
print('selected product:')
image = load_img(img, target_size=(img_width, img_height, 3))
plt.imshow(image)
plt.show()
print('-'*30)
print('similar products:')
cosine_img = cos_df[img].sort_values(ascending=False)[1:most_similar+1].index
cosine_img_score = cos_df[img].sort_values(ascending=False)[1:most_similar+1]
for i in range(0, len(cosine_img)):
image_cosine = load_img(cosine_img[i], target_size=(img_width, img_height, 3))
plt.imshow(image_cosine)
plt.show()
similarity_image(files[5])
# 코드
from tensorflow.keras.applications import vgg16
from tensorflow.keras.applications.imagenet_utils import preprocess_input
from tensorflow.keras.preprocessing.image import load_img,img_to_array
from tensorflow.keras.models import Model
import os
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn.metrics.pairwise import cosine_similarity
vgg_model = vgg16.VGG16(weights='imagenet')
model = Model(inputs=vgg_model.input, outputs=vgg_model.get_layer("fc2").output)
model.summary()
img_path = 'style/'
img_width, img_height = 224, 224
most_similar = 5
files = [img_path + x for x in os.listdir(img_path) if 'png' in x]
image_list = []
for f in files:
file_name = f
image = load_img(file_name, target_size=(224, 224, 3))
img_array = img_to_array(image)
img_batch = np.expand_dims(img_array, axis=0)
image_list.append(img_batch)
images = np.vstack(image_list)
processed_imgs = preprocess_input(images)
img_feature = model.predict(processed_imgs)
cosine = cosine_similarity(img_feature)
cos_df = pd.DataFrame(cosine, columns=files, index=files)
def similarity_image(img):
print('-'*30)
print('selected product:')
image = load_img(img, target_size=(img_width, img_height, 3))
plt.imshow(image)
plt.show()
print('-'*30)
print('similar products:')
cosine_img = cos_df[img].sort_values(ascending=False)[1:most_similar+1].index
cosine_img_score = cos_df[img].sort_values(ascending=False)[1:most_similar+1]
for i in range(0, len(cosine_img)):
image_cosine = load_img(cosine_img[i], target_size=(img_width, img_height, 3))
plt.imshow(image_cosine)
plt.show()
similarity_image(files[5])
used_Data :::::: https://www.kaggle.com/olgabelitskaya/style-color-images