Upload 2 files
Browse files- app.py +2 -2
- image_feature.py +34 -26
app.py
CHANGED
@@ -3,8 +3,8 @@ import image_feature as func
|
|
3 |
|
4 |
|
5 |
def work11(image1, image2):
|
6 |
-
|
7 |
-
return func.infer3(image1, image2)
|
8 |
|
9 |
|
10 |
# with gr.Blocks() as demo:
|
|
|
3 |
|
4 |
|
5 |
def work11(image1, image2):
|
6 |
+
return func.infer1(image1, image2)
|
7 |
+
# return func.infer3(image1, image2)
|
8 |
|
9 |
|
10 |
# with gr.Blocks() as demo:
|
image_feature.py
CHANGED
@@ -51,6 +51,8 @@ DEVICE = torch.device('cpu')
|
|
51 |
# 第二种方式推理图片相似度
|
52 |
# processor = AutoImageProcessor.from_pretrained("google/vit-base-patch16-224")
|
53 |
# model = AutoModel.from_pretrained("google/vit-base-patch16-224").to(DEVICE)
|
|
|
|
|
54 |
|
55 |
|
56 |
# tensor([0.6061], device='cuda:0', grad_fn=<SumBackward1>)
|
@@ -58,14 +60,18 @@ DEVICE = torch.device('cpu')
|
|
58 |
# pipe = pipeline(task="image-feature-extraction", model_name="google/vit-base-patch16-384", device=DEVICE, pool=True)
|
59 |
pipe = pipeline(task="image-feature-extraction", model_name="chanhua/autotrain-izefx-v3qh0", device=DEVICE, pool=True)
|
60 |
|
|
|
61 |
# 推理
|
62 |
def infer3(url1, url2):
|
63 |
try:
|
64 |
print("进入推理")
|
65 |
print("打开图片1")
|
66 |
-
image_real = Image.open(requests.get(url1, stream=True).raw).convert("RGB")
|
|
|
|
|
67 |
print("打开图片2")
|
68 |
-
image_gen = Image.open(requests.get(url2, stream=True).raw).convert("RGB")
|
|
|
69 |
|
70 |
print("利用模型获取图片特征向量")
|
71 |
outputs = pipe([image_real, image_gen])
|
@@ -89,31 +95,33 @@ def infer3(url1, url2):
|
|
89 |
# 无论是否发生异常,都会执行此代码块
|
90 |
print("这是finally块")
|
91 |
|
92 |
-
|
93 |
# 推理
|
94 |
-
|
95 |
-
#
|
96 |
-
#
|
97 |
-
|
98 |
-
|
99 |
-
|
|
|
|
|
100 |
|
101 |
|
102 |
# 推理
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
#
|
110 |
-
|
111 |
-
|
112 |
-
#
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
#
|
|
|
|
51 |
# 第二种方式推理图片相似度
|
52 |
# processor = AutoImageProcessor.from_pretrained("google/vit-base-patch16-224")
|
53 |
# model = AutoModel.from_pretrained("google/vit-base-patch16-224").to(DEVICE)
|
54 |
+
processor = AutoImageProcessor.from_pretrained("chanhua/autotrain-izefx-v3qh0")
|
55 |
+
model = AutoModel.from_pretrained("chanhua/autotrain-izefx-v3qh0").to(DEVICE)
|
56 |
|
57 |
|
58 |
# tensor([0.6061], device='cuda:0', grad_fn=<SumBackward1>)
|
|
|
60 |
# pipe = pipeline(task="image-feature-extraction", model_name="google/vit-base-patch16-384", device=DEVICE, pool=True)
|
61 |
pipe = pipeline(task="image-feature-extraction", model_name="chanhua/autotrain-izefx-v3qh0", device=DEVICE, pool=True)
|
62 |
|
63 |
+
|
64 |
# 推理
|
65 |
def infer3(url1, url2):
|
66 |
try:
|
67 |
print("进入推理")
|
68 |
print("打开图片1")
|
69 |
+
# image_real = Image.open(requests.get(url1, stream=True).raw).convert("RGB")
|
70 |
+
image_real = Image.open(url1).convert('RGB')
|
71 |
+
|
72 |
print("打开图片2")
|
73 |
+
# image_gen = Image.open(requests.get(url2, stream=True).raw).convert("RGB")
|
74 |
+
image_gen = Image.open(url2).convert('RGB')
|
75 |
|
76 |
print("利用模型获取图片特征向量")
|
77 |
outputs = pipe([image_real, image_gen])
|
|
|
95 |
# 无论是否发生异常,都会执行此代码块
|
96 |
print("这是finally块")
|
97 |
|
|
|
98 |
# 推理
|
99 |
+
def infer2(url):
|
100 |
+
# image_real = Image.open(requests.get(img_urls[0], stream=True).raw).convert("RGB")
|
101 |
+
# image = Image.open(requests.get(url, stream=True).raw).convert("RGB")
|
102 |
+
image = Image.open(url).convert('RGB')
|
103 |
+
|
104 |
+
inputs = processor(image, return_tensors="pt").to(DEVICE)
|
105 |
+
outputs = model(**inputs)
|
106 |
+
return outputs.pooler_output
|
107 |
|
108 |
|
109 |
# 推理
|
110 |
+
def infer1(image1, image2):
|
111 |
+
try:
|
112 |
+
embed_real = infer2(image1)
|
113 |
+
embed_gen = infer2(image2)
|
114 |
+
similarity_score = cosine_similarity(embed_real, embed_gen, dim=1)
|
115 |
+
print(similarity_score)
|
116 |
+
# 如果你想在CPU上操作这个值,你需要先将tensor移动到CPU
|
117 |
+
t_cpu = similarity_score.cpu()
|
118 |
+
|
119 |
+
# 然后提取这个值
|
120 |
+
return t_cpu.item()
|
121 |
+
|
122 |
+
except Exception as e:
|
123 |
+
print(f"发生了一个错误: {e}")
|
124 |
+
return 0.0
|
125 |
+
finally:
|
126 |
+
# 无论是否发生异常,都会执行此代码块
|
127 |
+
print("这是finally块")
|