Delete app.py.v1
Browse files
app.py.v1
DELETED
|
@@ -1,100 +0,0 @@
|
|
| 1 |
-
import spaces
|
| 2 |
-
from pip._internal import main
|
| 3 |
-
|
| 4 |
-
main(['install', 'timm==1.0.8'])
|
| 5 |
-
import timm
|
| 6 |
-
|
| 7 |
-
print("installed", timm.__version__)
|
| 8 |
-
import gradio as gr
|
| 9 |
-
from inference import sam_preprocess, beit3_preprocess
|
| 10 |
-
from model.evf_sam import EvfSamModel
|
| 11 |
-
from transformers import AutoTokenizer
|
| 12 |
-
import torch
|
| 13 |
-
import numpy as np
|
| 14 |
-
import sys
|
| 15 |
-
import os
|
| 16 |
-
|
| 17 |
-
version = "YxZhang/evf-sam"
|
| 18 |
-
model_type = "ori"
|
| 19 |
-
|
| 20 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
| 21 |
-
version,
|
| 22 |
-
padding_side="right",
|
| 23 |
-
use_fast=False,
|
| 24 |
-
)
|
| 25 |
-
|
| 26 |
-
kwargs = {
|
| 27 |
-
"torch_dtype": torch.half,
|
| 28 |
-
}
|
| 29 |
-
model = EvfSamModel.from_pretrained(version, low_cpu_mem_usage=True,
|
| 30 |
-
**kwargs).eval()
|
| 31 |
-
model.to('cuda')
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
@spaces.GPU
|
| 35 |
-
@torch.no_grad()
|
| 36 |
-
def pred(image_np, prompt):
|
| 37 |
-
original_size_list = [image_np.shape[:2]]
|
| 38 |
-
|
| 39 |
-
image_beit = beit3_preprocess(image_np, 224).to(dtype=model.dtype,
|
| 40 |
-
device=model.device)
|
| 41 |
-
|
| 42 |
-
image_sam, resize_shape = sam_preprocess(image_np, model_type=model_type)
|
| 43 |
-
image_sam = image_sam.to(dtype=model.dtype, device=model.device)
|
| 44 |
-
|
| 45 |
-
input_ids = tokenizer(
|
| 46 |
-
prompt, return_tensors="pt")["input_ids"].to(device=model.device)
|
| 47 |
-
|
| 48 |
-
# infer
|
| 49 |
-
pred_mask = model.inference(
|
| 50 |
-
image_sam.unsqueeze(0),
|
| 51 |
-
image_beit.unsqueeze(0),
|
| 52 |
-
input_ids,
|
| 53 |
-
resize_list=[resize_shape],
|
| 54 |
-
original_size_list=original_size_list,
|
| 55 |
-
)
|
| 56 |
-
pred_mask = pred_mask.detach().cpu().numpy()[0]
|
| 57 |
-
pred_mask = pred_mask > 0
|
| 58 |
-
|
| 59 |
-
visualization = image_np.copy()
|
| 60 |
-
visualization[pred_mask] = (image_np * 0.5 +
|
| 61 |
-
pred_mask[:, :, None].astype(np.uint8) *
|
| 62 |
-
np.array([50, 120, 220]) * 0.5)[pred_mask]
|
| 63 |
-
|
| 64 |
-
return visualization / 255.0, pred_mask.astype(np.float16)
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
desc = """
|
| 68 |
-
<div><h3>EVF-SAM: Early Vision-Language Fusion for Text-Prompted Segment Anything Model</h3>
|
| 69 |
-
<p>EVF-SAM extends SAM's capabilities with text-prompted segmentation, achieving high accuracy in Referring Expression Segmentation.</p></div>
|
| 70 |
-
<div style='display:flex; gap: 0.25rem; align-items: center'><a href="https://arxiv.org/abs/2406.20076"><img src="https://img.shields.io/badge/arXiv-Paper-red"></a><a href="https://github.com/hustvl/EVF-SAM"><img src="https://img.shields.io/badge/GitHub-Code-blue"></a></div>
|
| 71 |
-
"""
|
| 72 |
-
|
| 73 |
-
# desc_title_str = '<div align ="center"><img src="assets/logo.jpg" width="20%"><h3> Early Vision-Language Fusion for Text-Prompted Segment Anything Model</h3></div>'
|
| 74 |
-
# desc_link_str = '[](https://arxiv.org/abs/2406.20076)'
|
| 75 |
-
|
| 76 |
-
demo = gr.Interface(
|
| 77 |
-
fn=pred,
|
| 78 |
-
inputs=[
|
| 79 |
-
gr.components.Image(type="numpy", label="Image", image_mode="RGB"),
|
| 80 |
-
gr.components.Textbox(
|
| 81 |
-
label="Prompt",
|
| 82 |
-
info=
|
| 83 |
-
"Use a phrase or sentence to describe the object you want to segment. Currently we only support English"
|
| 84 |
-
)
|
| 85 |
-
],
|
| 86 |
-
outputs=[
|
| 87 |
-
gr.components.Image(type="numpy", label="visulization"),
|
| 88 |
-
gr.components.Image(type="numpy", label="mask")
|
| 89 |
-
],
|
| 90 |
-
examples=[["assets/zebra.jpg", "zebra top left"],
|
| 91 |
-
["assets/bus.jpg", "bus going to south common"],
|
| 92 |
-
[
|
| 93 |
-
"assets/carrots.jpg",
|
| 94 |
-
"3carrots in center with ice and greenn leaves"
|
| 95 |
-
]],
|
| 96 |
-
title="📷 EVF-SAM: Referring Expression Segmentation",
|
| 97 |
-
description=desc,
|
| 98 |
-
allow_flagging="never")
|
| 99 |
-
# demo.launch()
|
| 100 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|