chanhua commited on
Commit
d336c8b
1 Parent(s): ef3d2c0
Files changed (5) hide show
  1. app.py +12 -26
  2. app_1.py +39 -0
  3. image_feature.py +75 -0
  4. requirements.txt +1 -3
  5. requirements_bak.txt +6 -0
app.py CHANGED
@@ -1,20 +1,9 @@
1
  import gradio as gr
2
- import testMch as func
3
-
4
-
5
- def work(imgurl):
6
- # output = testMch.describeImage2(img)
7
- # # output = testMch.agent(img)
8
- # # output = testMch.agent(img)
9
- # return output
10
- # imgurl = "r" + imgurl
11
- # imgurl = imgurl.replace("\\\\\\", "\\")
12
- # input = "Describe the following image:\n" + imgurl
13
- out = func.agent(f"Please describe the following image:\n{imgurl}")
14
- anws = func.toChinese(out['output'])
15
- return anws
16
 
17
 
 
 
18
 
19
 
20
  # with gr.Blocks() as demo:
@@ -25,15 +14,12 @@ def work(imgurl):
25
  # submit.click(work, inputs=[image_url, input], outputs=output)
26
  # demo.launch()
27
 
28
- demo = gr.Interface(title="识别图片",
29
- css="",
30
- fn=work,
31
- inputs=[gr.Image(type="filepath", label="请上传图片")],
32
- outputs=[gr.Textbox(lines=3, label="识别结果")])
33
- #
34
- # # demo = gr.Interface(fn=work, inputs="image,text", outputs="text")
35
- #
36
- demo.launch()
37
-
38
- # # interface = gr.Interface(fn=agent, inputs="image", outputs="text")
39
- # # interface.launch()
 
1
  import gradio as gr
2
+ import image_feature as func
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
 
5
+ def work(image1, image2):
6
+ return func.infer1(image1, image2)
7
 
8
 
9
  # with gr.Blocks() as demo:
 
14
  # submit.click(work, inputs=[image_url, input], outputs=output)
15
  # demo.launch()
16
 
17
+ # 定义你的界面
18
+ with gr.Interface(fn=work,
19
+ inputs=[gr.Textbox(label='图片1', lines=1), gr.Textbox(label='图片2', lines=1)], # 两个文本输入框
20
+ outputs=[gr.Textbox(lines=3, label="推理结果")], # 输出为文本
21
+ title="图片相似度推理", # 界面标题
22
+ description="输入两张图片链接进行相似度推理", # 界面描述
23
+ examples=[["https://example.com", "https://google.com"], # 示例输入
24
+ ["https://github.com", "https://twitter.com"]]) as demo: # 更多示例输入
25
+ demo.launch() # 启动界面
 
 
 
app_1.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import testMch as func
3
+
4
+
5
+ def work(imgurl):
6
+ # output = testMch.describeImage2(img)
7
+ # # output = testMch.agent(img)
8
+ # # output = testMch.agent(img)
9
+ # return output
10
+ # imgurl = "r" + imgurl
11
+ # imgurl = imgurl.replace("\\\\\\", "\\")
12
+ # input = "Describe the following image:\n" + imgurl
13
+ out = func.agent(f"Please describe the following image:\n{imgurl}")
14
+ anws = func.toChinese(out['output'])
15
+ return anws
16
+
17
+
18
+
19
+
20
+ # with gr.Blocks() as demo:
21
+ # image_url = gr.Image(type="filepath", label="请选择一张图片")
22
+ # input = gr.Textbox(label='请描述您的问题', placeholder="", lines=1)
23
+ # output = gr.Textbox(label='答案', placeholder="", lines=2, interactive=False)
24
+ # submit = gr.Button('提问', variant="primary")
25
+ # submit.click(work, inputs=[image_url, input], outputs=output)
26
+ # demo.launch()
27
+
28
+ demo = gr.Interface(title="识别图片",
29
+ css="",
30
+ fn=work,
31
+ inputs=[gr.Image(type="filepath", label="请上传图片")],
32
+ outputs=[gr.Textbox(lines=3, label="识别结果")])
33
+ #
34
+ # # demo = gr.Interface(fn=work, inputs="image,text", outputs="text")
35
+ #
36
+ demo.launch()
37
+
38
+ # # interface = gr.Interface(fn=agent, inputs="image", outputs="text")
39
+ # # interface.launch()
image_feature.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # import numpy as np
2
+ # import requests
3
+ 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
11
+ #
12
+ # print(transformers.__version__)
13
+ #
14
+ # img_urls = ["https://img0.baidu.com/it/u=3704428154,2884159591&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500",
15
+ # "https://img0.baidu.com/it/u=3704428154,2884159591&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500"]
16
+ #
17
+ # image_real = Image.open(requests.get(img_urls[0], stream=True).raw).convert("RGB")
18
+ # image_gen = Image.open(requests.get(img_urls[1], stream=True).raw).convert("RGB")
19
+ #
20
+ # # DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
21
+ DEVICE = torch.device('cpu')
22
+ # pipe = pipeline(task="image-feature-extraction", model_name="google/vit-base-patch16-384", device=DEVICE, pool=True)
23
+ #
24
+ # # 1提取图片特征向量
25
+ # outputs = pipe([image_real, image_gen])
26
+ #
27
+ # # get the length of a single output
28
+ # print(len(outputs[0][0]))
29
+ # # show outputs
30
+ # print(outputs)
31
+ #
32
+ # # 768
33
+ # # [[[-0.03909236937761307, 0.43381670117378235, -0.06913255900144577,
34
+ #
35
+ # # 2计算图片相似度
36
+ # similarity_score = cosine_similarity(torch.Tensor(outputs[0]),
37
+ # torch.Tensor(outputs[1]), dim=1)
38
+ #
39
+ # print(similarity_score)
40
+
41
+ # tensor([0.6043])
42
+
43
+ # pipe = pipeline(task="image-feature-extraction", model_name="google/vit-base-patch16-224", device=DEVICE)
44
+ # output = pipe(image_real)
45
+ #
46
+ # # 其中第一个维度是批量大小,最后两个维度是嵌入形状。
47
+ # print(np.array(outputs).shape)
48
+ # # (1, 197, 768)
49
+
50
+
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
+ # 推理
57
+ def infer(image):
58
+ inputs = processor(image, return_tensors="pt").to(DEVICE)
59
+ outputs = model(**inputs)
60
+ return outputs.pooler_output
61
+
62
+
63
+ # 推理
64
+ def infer1(image1, image2):
65
+ embed_real = infer(image1)
66
+ embed_gen = infer(image2)
67
+ similarity_score = cosine_similarity(embed_real, embed_gen, dim=1)
68
+ print(similarity_score)
69
+ # 如果你想在CPU上操作这个值,你需要先将tensor移动到CPU
70
+ t_cpu = similarity_score.cpu()
71
+
72
+ # 然后提取这个值
73
+ return t_cpu.item()
74
+
75
+ # tensor([0.6061], device='cuda:0', grad_fn=<SumBackward1>)
requirements.txt CHANGED
@@ -1,6 +1,4 @@
1
- transformers
2
  torch
3
- langchain
4
- openai
5
  gradio
6
  pillow
 
1
+ transformers==4.39.1
2
  torch
 
 
3
  gradio
4
  pillow
requirements_bak.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ transformers
2
+ torch
3
+ langchain
4
+ openai
5
+ gradio
6
+ pillow