forked from ultralytics/yolov5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVGG16_similarity_Xing.py
90 lines (79 loc) · 3.87 KB
/
VGG16_similarity_Xing.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
# -*- coding: utf-8 -*-
"""
25th March, 2020
Image Similarity using VGG16
"""
import os
import numpy as np
from keras.layers import Input
from keras.models import Model
from vgg16 import VGG16
from keras.utils import image_utils as image
from keras.applications.imagenet_utils import preprocess_input
from sklearn.metrics.pairwise import cosine_similarity
#from scipy.spatial import distance
'''
def get_feature_vector(img):
img1 = cv2.resize(img, (224, 224))
feature_vector = feature_model.predict(img1.reshape(1, 224, 224, 3))
return feature_vector
'''
# fc2(Dense)output shape: (None, 4096)
def get_feature_vector_fromPIL(img):
feature_vector = feature_model.predict(img)
assert(feature_vector.shape == (1,4096))
return feature_vector
def calculate_similarity_cosine(vector1, vector2):
#return 1- distance.cosine(vector1, vector2)
return cosine_similarity(vector1, vector2)
# This distance can be in range of [0,∞]. And this distance is converted to a [0,1]
def calculate_similarity_euclidean(vector1, vector2):
#return distance.euclidean(vector1, vector2) #distance.euclidean is slower
return 1/(1+np.linalg.norm(vector1 - vector2)) #np.linalg.norm is faster
def SIM(a,b):
image_input = Input(shape=(224, 224, 3))
model = VGG16(input_tensor=image_input, include_top=True,weights='imagenet')
layer_name = 'fc2'
feature_model = Model(inputs=model.input,outputs=model.get_layer(layer_name).output)
image_similarity_cosine = calculate_similarity_cosine(get_feature_vector_fromPIL(a),get_feature_vector_fromPIL(b))
image_similarity_euclidean = calculate_similarity_euclidean(get_feature_vector_fromPIL(a),get_feature_vector_fromPIL(b))
print('VGG16 image similarity_euclidean:',image_similarity_euclidean)
print("VGG16 image similarity_cosine: {:.2f}%".format(image_similarity_cosine[0][0]*100))
# 首先定义一个类,要有__init__
class jisuanxiangsidu:
def __init__(self):
image_input = Input(shape=(224, 224, 3))
model = VGG16(input_tensor=image_input, include_top=True,weights='imagenet')
layer_name = 'fc2'
feature_model = Model(inputs=model.input,outputs=model.get_layer(layer_name).output)
def begin(a,b):
image_similarity_cosine = calculate_similarity_cosine(get_feature_vector_fromPIL(a),get_feature_vector_fromPIL(b))
image_similarity_euclidean = calculate_similarity_euclidean(get_feature_vector_fromPIL(a),get_feature_vector_fromPIL(b))
print('VGG16 image similarity_euclidean:',image_similarity_euclidean)
print("VGG16 image similarity_cosine: {:.2f}%".format(image_similarity_cosine[0][0]*100))
# Use VGG16 model as an image feature extractor
image_input = Input(shape=(224, 224, 3))
model = VGG16(input_tensor=image_input, include_top=True,weights='imagenet')
layer_name = 'fc2'
feature_model = Model(inputs=model.input,outputs=model.get_layer(layer_name).output)
# Load images in the images folder into array
cwd_path = os.getcwd()
data_path =cwd_path + '/images'
data_dir_list = os.listdir(data_path)
img_data_list=[]
for dataset in data_dir_list:
img_path = data_path + '/'+ dataset
print(img_path)
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
img_data_list.append(x)
#vector_VGG16 =get_feature_vector_fromPIL(img_data_list[6])
# Caculate cosine similarity: [-1,1], that is, [completedly different,same]
image_similarity_cosine = calculate_similarity_cosine(get_feature_vector_fromPIL(img_data_list[1]), get_feature_vector_fromPIL(img_data_list[2]))
# Cacluate euclidean similarity: range from [0, 1], that is, [completedly different, same]
image_similarity_euclidean = calculate_similarity_euclidean(get_feature_vector_fromPIL(img_data_list[1]), get_feature_vector_fromPIL(img_data_list[2]))
print('VGG16 image similarity_euclidean:',image_similarity_euclidean)
print("VGG16 image similarity_cosine: {:.2f}%".format(image_similarity_cosine[0][0]*100))