Upload 2 files
Browse files- app.py +2 -2
- image_feature.py +24 -6
app.py
CHANGED
@@ -3,8 +3,8 @@ import image_feature as func
|
|
3 |
|
4 |
|
5 |
def work11(image1, image2):
|
6 |
-
|
7 |
-
return func.
|
8 |
|
9 |
|
10 |
# with gr.Blocks() as demo:
|
|
|
3 |
|
4 |
|
5 |
def work11(image1, image2):
|
6 |
+
return func.infer1(image1, image2)
|
7 |
+
# return func.infer4(image1, image2)
|
8 |
|
9 |
|
10 |
# with gr.Blocks() as demo:
|
image_feature.py
CHANGED
@@ -4,7 +4,7 @@ import torch
|
|
4 |
from PIL import Image
|
5 |
from torch.nn.functional import cosine_similarity
|
6 |
from transformers import AutoImageProcessor, AutoModel
|
7 |
-
|
8 |
from transformers import pipeline
|
9 |
|
10 |
# import transformers
|
@@ -51,11 +51,14 @@ 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 |
-
processor = AutoImageProcessor.from_pretrained("google/vit-base-patch16-224-in21k")
|
55 |
-
model = AutoModel.from_pretrained("google/vit-base-patch16-224-in21k").to(DEVICE)
|
56 |
# processor = AutoImageProcessor.from_pretrained("chanhua/autotrain-izefx-v3qh0")
|
57 |
# model = AutoModel.from_pretrained("chanhua/autotrain-izefx-v3qh0").to(DEVICE)
|
58 |
|
|
|
|
|
|
|
59 |
|
60 |
# tensor([0.6061], device='cuda:0', grad_fn=<SumBackward1>)
|
61 |
|
@@ -66,7 +69,7 @@ pipe = pipeline(task="image-feature-extraction", model_name="google/vit-base-pat
|
|
66 |
|
67 |
|
68 |
# 推理
|
69 |
-
def
|
70 |
try:
|
71 |
print("进入推理")
|
72 |
print("打开图片1")
|
@@ -100,7 +103,7 @@ def infer3(url1, url2):
|
|
100 |
print("这是finally块")
|
101 |
|
102 |
# 推理
|
103 |
-
def
|
104 |
# image_real = Image.open(requests.get(img_urls[0], stream=True).raw).convert("RGB")
|
105 |
# image = Image.open(requests.get(url, stream=True).raw).convert("RGB")
|
106 |
image = Image.open(url).convert('RGB')
|
@@ -109,8 +112,21 @@ def infer2(url):
|
|
109 |
outputs = model(**inputs)
|
110 |
return outputs.pooler_output
|
111 |
|
112 |
-
|
113 |
# 推理
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
def infer1(image1, image2):
|
115 |
try:
|
116 |
embed_real = infer2(image1)
|
@@ -129,3 +145,5 @@ def infer1(image1, image2):
|
|
129 |
finally:
|
130 |
# 无论是否发生异常,都会执行此代码块
|
131 |
print("这是finally块")
|
|
|
|
|
|
4 |
from PIL import Image
|
5 |
from torch.nn.functional import cosine_similarity
|
6 |
from transformers import AutoImageProcessor, AutoModel
|
7 |
+
from transformers import ViTImageProcessor, ViTModel
|
8 |
from transformers import pipeline
|
9 |
|
10 |
# import transformers
|
|
|
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("google/vit-base-patch16-224-in21k")
|
55 |
+
# model = AutoModel.from_pretrained("google/vit-base-patch16-224-in21k").to(DEVICE)
|
56 |
# processor = AutoImageProcessor.from_pretrained("chanhua/autotrain-izefx-v3qh0")
|
57 |
# model = AutoModel.from_pretrained("chanhua/autotrain-izefx-v3qh0").to(DEVICE)
|
58 |
|
59 |
+
processor = ViTImageProcessor.from_pretrained('google/vit-base-patch16-224-in21k')
|
60 |
+
model = ViTModel.from_pretrained('google/vit-base-patch16-224-in21k')
|
61 |
+
|
62 |
|
63 |
# tensor([0.6061], device='cuda:0', grad_fn=<SumBackward1>)
|
64 |
|
|
|
69 |
|
70 |
|
71 |
# 推理
|
72 |
+
def infer4(url1, url2):
|
73 |
try:
|
74 |
print("进入推理")
|
75 |
print("打开图片1")
|
|
|
103 |
print("这是finally块")
|
104 |
|
105 |
# 推理
|
106 |
+
def infer3(url):
|
107 |
# image_real = Image.open(requests.get(img_urls[0], stream=True).raw).convert("RGB")
|
108 |
# image = Image.open(requests.get(url, stream=True).raw).convert("RGB")
|
109 |
image = Image.open(url).convert('RGB')
|
|
|
112 |
outputs = model(**inputs)
|
113 |
return outputs.pooler_output
|
114 |
|
|
|
115 |
# 推理
|
116 |
+
def infer2(url):
|
117 |
+
# image_real = Image.open(requests.get(img_urls[0], stream=True).raw).convert("RGB")
|
118 |
+
# image = Image.open(requests.get(url, stream=True).raw).convert("RGB")
|
119 |
+
image = Image.open(url).convert('RGB')
|
120 |
+
|
121 |
+
inputs = processor(images=image, return_tensors="pt").to(DEVICE)
|
122 |
+
|
123 |
+
outputs = model(**inputs)
|
124 |
+
# last_hidden_states = outputs.last_hidden_state
|
125 |
+
|
126 |
+
return outputs.pooler_output
|
127 |
+
|
128 |
+
|
129 |
+
# 计算相似度
|
130 |
def infer1(image1, image2):
|
131 |
try:
|
132 |
embed_real = infer2(image1)
|
|
|
145 |
finally:
|
146 |
# 无论是否发生异常,都会执行此代码块
|
147 |
print("这是finally块")
|
148 |
+
|
149 |
+
|