Spaces:
Sleeping
Sleeping
Update cal_compatibility.py
Browse files- cal_compatibility.py +41 -43
cal_compatibility.py
CHANGED
@@ -1,43 +1,41 @@
|
|
1 |
-
import vgg16
|
2 |
-
import numpy as np
|
3 |
-
|
4 |
-
|
5 |
-
def cosine_similarity(vector1, vector2):
|
6 |
-
# # 计算向量的点积
|
7 |
-
# dot_product = np.dot(vector1, vector2)
|
8 |
-
# # 计算向量的模长
|
9 |
-
# magnitude_vector1 = np.linalg.norm(vector1)
|
10 |
-
# magnitude_vector2 = np.linalg.norm(vector2)
|
11 |
-
# # 计算余弦相似度
|
12 |
-
# return dot_product / (magnitude_vector1 * magnitude_vector2)
|
13 |
-
flat_vector1 = vector1.reshape(-1)
|
14 |
-
flat_vector2 = vector2.reshape(-1)
|
15 |
-
dot_product = np.dot(flat_vector1, flat_vector2)
|
16 |
-
norm_vector1 = np.linalg.norm(flat_vector1)
|
17 |
-
norm_vector2 = np.linalg.norm(flat_vector2)
|
18 |
-
return dot_product / (norm_vector1 * norm_vector2)
|
19 |
-
|
20 |
-
def cal_compatibility():
|
21 |
-
n = 4096
|
22 |
-
access_feature = []
|
23 |
-
cloth_feature = []
|
24 |
-
for item_id in range(1,
|
25 |
-
access_feature.append(vgg16.extract_features('downloads/access_' + '%s.jpg' % item_id)[0])
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
# cal_compatibility()
|
|
|
1 |
+
import vgg16
|
2 |
+
import numpy as np
|
3 |
+
|
4 |
+
|
5 |
+
def cosine_similarity(vector1, vector2):
|
6 |
+
# # 计算向量的点积
|
7 |
+
# dot_product = np.dot(vector1, vector2)
|
8 |
+
# # 计算向量的模长
|
9 |
+
# magnitude_vector1 = np.linalg.norm(vector1)
|
10 |
+
# magnitude_vector2 = np.linalg.norm(vector2)
|
11 |
+
# # 计算余弦相似度
|
12 |
+
# return dot_product / (magnitude_vector1 * magnitude_vector2)
|
13 |
+
flat_vector1 = vector1.reshape(-1)
|
14 |
+
flat_vector2 = vector2.reshape(-1)
|
15 |
+
dot_product = np.dot(flat_vector1, flat_vector2)
|
16 |
+
norm_vector1 = np.linalg.norm(flat_vector1)
|
17 |
+
norm_vector2 = np.linalg.norm(flat_vector2)
|
18 |
+
return dot_product / (norm_vector1 * norm_vector2)
|
19 |
+
|
20 |
+
def cal_compatibility():
|
21 |
+
n = 4096
|
22 |
+
access_feature = []
|
23 |
+
cloth_feature = []
|
24 |
+
for item_id in range(1, 9):
|
25 |
+
access_feature.append(vgg16.extract_features('downloads/access_' + '%s.jpg' % item_id)[0])
|
26 |
+
for item_id in range(1, 7):
|
27 |
+
cloth_feature.append(vgg16.extract_features('downloads/gen_cloth_' + '%s.jpeg' % item_id)[0])
|
28 |
+
|
29 |
+
best_score = float('-inf')
|
30 |
+
best_cloth = 0
|
31 |
+
best_access = 0
|
32 |
+
for i in range(1, 9):
|
33 |
+
for j in range(1, 7):
|
34 |
+
score = cosine_similarity(access_feature[i - 1], cloth_feature[j - 1])
|
35 |
+
if score > best_score:
|
36 |
+
best_score = score
|
37 |
+
best_cloth = j
|
38 |
+
best_access = i
|
39 |
+
print(best_cloth, best_access)
|
40 |
+
picture = [f"downloads/gen_cloth_{best_cloth}.jpeg", f"downloads/access_{best_access}.jpg"]
|
41 |
+
return picture
|
|
|
|