Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -53,65 +53,6 @@ def Retrieval(image, candidate_labels):
|
|
| 53 |
return results
|
| 54 |
|
| 55 |
|
| 56 |
-
def Get_Densefeature(image, candidate_labels):
|
| 57 |
-
"""
|
| 58 |
-
Takes an image and a comma-separated string of candidate labels,
|
| 59 |
-
and returns the classification scores.
|
| 60 |
-
"""
|
| 61 |
-
candidate_labels = [label.lstrip(" ") for label in candidate_labels.split(",") if label !=""]
|
| 62 |
-
# print(candidate_labels)
|
| 63 |
-
|
| 64 |
-
image_size=224
|
| 65 |
-
image = image.convert("RGB")
|
| 66 |
-
image = image.resize((image_size,image_size))
|
| 67 |
-
image_input = image_processor.preprocess(image, return_tensors='pt')['pixel_values'].to(device)
|
| 68 |
-
|
| 69 |
-
with torch.no_grad():
|
| 70 |
-
dense_image_feature = model.get_image_dense_features(image_input)
|
| 71 |
-
captions = [candidate_labels[0]]
|
| 72 |
-
caption_input = torch.tensor(tokenizer(captions, max_length=77, padding="max_length", truncation=True).input_ids, dtype=torch.long, device=device)
|
| 73 |
-
text_feature = model.get_text_features(caption_input,walk_short_pos=True)
|
| 74 |
-
text_feature = text_feature / text_feature.norm(p=2, dim=-1, keepdim=True)
|
| 75 |
-
dense_image_feature = dense_image_feature / dense_image_feature.norm(p=2, dim=-1, keepdim=True)
|
| 76 |
-
|
| 77 |
-
similarity = dense_image_feature.squeeze() @ text_feature.squeeze().T
|
| 78 |
-
similarity = similarity.cpu().numpy()
|
| 79 |
-
patch_size = int(math.sqrt(similarity.shape[0]))
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
original_shape = (patch_size, patch_size)
|
| 83 |
-
show_image = similarity.reshape(original_shape)
|
| 84 |
-
# normalized = (show_image - show_image.min()) / (show_image.max() - show_image.min())
|
| 85 |
-
|
| 86 |
-
# def viridis_colormap(x):
|
| 87 |
-
|
| 88 |
-
# r = np.clip(1.1746 * x - 0.1776, 0, 1)
|
| 89 |
-
# g = np.clip(2.0 * x - 0.7, 0, 1)
|
| 90 |
-
# b = np.clip(-2.0 * x + 1.7, 0, 1)
|
| 91 |
-
# return np.stack([r, g, b], axis=-1)
|
| 92 |
-
|
| 93 |
-
# color_mapped = viridis_colormap(normalized)
|
| 94 |
-
|
| 95 |
-
# color_mapped_uint8 = (color_mapped * 255).astype(np.uint8)
|
| 96 |
-
|
| 97 |
-
# pil_img = Image.fromarray(color_mapped_uint8)
|
| 98 |
-
# pil_img = pil_img.resize((512,512))
|
| 99 |
-
|
| 100 |
-
fig = plt.figure(figsize=(6, 6))
|
| 101 |
-
plt.imshow(show_image)
|
| 102 |
-
plt.title('similarity Visualization')
|
| 103 |
-
plt.axis('off')
|
| 104 |
-
|
| 105 |
-
buf = io.BytesIO()
|
| 106 |
-
plt.savefig(buf, format='png')
|
| 107 |
-
buf.seek(0)
|
| 108 |
-
plt.close(fig)
|
| 109 |
-
|
| 110 |
-
pil_img = Image.open(buf)
|
| 111 |
-
# buf.close()
|
| 112 |
-
return pil_img
|
| 113 |
-
|
| 114 |
-
|
| 115 |
|
| 116 |
|
| 117 |
def infer(image, candidate_labels):
|
|
@@ -126,33 +67,28 @@ with gr.Blocks() as demo:
|
|
| 126 |
|
| 127 |
"This app uses the FG-CLIP model (qihoo360/fg-clip-base) for retrieval on CPU :"
|
| 128 |
)
|
| 129 |
-
|
| 130 |
-
"(Run Densefeature) only support one class!"
|
| 131 |
-
)
|
| 132 |
-
|
| 133 |
with gr.Row():
|
| 134 |
with gr.Column():
|
| 135 |
image_input = gr.Image(type="pil")
|
| 136 |
text_input = gr.Textbox(label="Input a list of labels (comma seperated)")
|
| 137 |
run_button = gr.Button("Run Retrieval", visible=True)
|
| 138 |
-
dfs_button = gr.Button("Run Densefeature", visible=True)
|
| 139 |
with gr.Column():
|
| 140 |
fg_output = gr.Label(label="FG-CLIP Output", num_top_classes=11)
|
| 141 |
-
|
| 142 |
|
| 143 |
examples = [
|
| 144 |
-
|
| 145 |
-
# ["./dog.jpg", "A light brown wood stool, A bucket with a body made of dark brown plastic, A black velvet back cover for a cellular telephone, A green ball with a perforated pattern, A light blue plastic helmet made of plastic, A grey slipper made of wool, A newspaper with white and black perforated printed on a paper texture, A blue dog with a white colored head, A yellow sponge with a dark green rough surface, A book with white, dark orange and brown pages made of paper, A black ceramic scarf with a body made of fabric."],
|
| 146 |
["./Landscape.jpg", "red grass, yellow grass, green grass"],
|
| 147 |
["./cat.jpg", "two sleeping cats, two cats playing, three cats laying down"],
|
| 148 |
-
|
| 149 |
]
|
| 150 |
gr.Examples(
|
| 151 |
examples=examples,
|
| 152 |
inputs=[image_input, text_input],
|
| 153 |
-
|
| 154 |
-
|
| 155 |
)
|
| 156 |
run_button.click(fn=infer, inputs=[image_input, text_input], outputs=fg_output)
|
| 157 |
-
|
| 158 |
demo.launch()
|
|
|
|
| 53 |
return results
|
| 54 |
|
| 55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
|
| 58 |
def infer(image, candidate_labels):
|
|
|
|
| 67 |
|
| 68 |
"This app uses the FG-CLIP model (qihoo360/fg-clip-base) for retrieval on CPU :"
|
| 69 |
)
|
| 70 |
+
|
|
|
|
|
|
|
|
|
|
| 71 |
with gr.Row():
|
| 72 |
with gr.Column():
|
| 73 |
image_input = gr.Image(type="pil")
|
| 74 |
text_input = gr.Textbox(label="Input a list of labels (comma seperated)")
|
| 75 |
run_button = gr.Button("Run Retrieval", visible=True)
|
|
|
|
| 76 |
with gr.Column():
|
| 77 |
fg_output = gr.Label(label="FG-CLIP Output", num_top_classes=11)
|
| 78 |
+
|
| 79 |
|
| 80 |
examples = [
|
| 81 |
+
|
|
|
|
| 82 |
["./Landscape.jpg", "red grass, yellow grass, green grass"],
|
| 83 |
["./cat.jpg", "two sleeping cats, two cats playing, three cats laying down"],
|
| 84 |
+
|
| 85 |
]
|
| 86 |
gr.Examples(
|
| 87 |
examples=examples,
|
| 88 |
inputs=[image_input, text_input],
|
| 89 |
+
outputs=fg_output,
|
| 90 |
+
fn=infer,
|
| 91 |
)
|
| 92 |
run_button.click(fn=infer, inputs=[image_input, text_input], outputs=fg_output)
|
| 93 |
+
|
| 94 |
demo.launch()
|