Spaces:
Running
Running
Delete modules/florence2.py
Browse files- modules/florence2.py +0 -91
modules/florence2.py
DELETED
|
@@ -1,91 +0,0 @@
|
|
| 1 |
-
import os,io,copy,subprocess,spaces,matplotlib.pyplot as plt,matplotlib.patches as patches,random,numpy as np
|
| 2 |
-
from PIL import Image,ImageDraw,ImageFont
|
| 3 |
-
from unittest.mock import patch
|
| 4 |
-
from transformers.dynamic_module_utils import get_imports
|
| 5 |
-
from transformers import AutoProcessor,AutoModelForCausalLM
|
| 6 |
-
import gradio as gr
|
| 7 |
-
|
| 8 |
-
def fixed_get_imports(filename:str|os.PathLike)->list[str]:
|
| 9 |
-
if not str(filename).endswith('/modeling_florence2.py'):return get_imports(filename)
|
| 10 |
-
imports=get_imports(filename)
|
| 11 |
-
if'flash_attn'in imports:imports.remove('flash_attn')
|
| 12 |
-
return imports
|
| 13 |
-
@spaces.GPU
|
| 14 |
-
def get_device_type():
|
| 15 |
-
import torch
|
| 16 |
-
if torch.cuda.is_available():return'cuda'
|
| 17 |
-
elif torch.backends.mps.is_available()and torch.backends.mps.is_built():return'mps'
|
| 18 |
-
else:return'cpu'
|
| 19 |
-
device = get_device_type()
|
| 20 |
-
if (device == "cuda"):
|
| 21 |
-
subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
|
| 22 |
-
model = AutoModelForCausalLM.from_pretrained("MiaoshouAI/Florence-2-base-PromptGen-v2.0", trust_remote_code=True)
|
| 23 |
-
processor = AutoProcessor.from_pretrained("MiaoshouAI/Florence-2-base-PromptGen-v2.0", trust_remote_code=True)
|
| 24 |
-
model.to(device)
|
| 25 |
-
else:
|
| 26 |
-
with patch("transformers.dynamic_module_utils.get_imports", fixed_get_imports):
|
| 27 |
-
model = AutoModelForCausalLM.from_pretrained("MiaoshouAI/Florence-2-base-PromptGen-v2.0", trust_remote_code=True)
|
| 28 |
-
processor = AutoProcessor.from_pretrained("MiaoshouAI/Florence-2-base-PromptGen-v2.0", trust_remote_code=True)
|
| 29 |
-
model.to(device)
|
| 30 |
-
|
| 31 |
-
colormap=['blue','orange','green','purple','brown','pink','gray','olive','cyan','red','lime','indigo','violet','aqua','magenta','coral','gold','tan','skyblue']
|
| 32 |
-
|
| 33 |
-
def fig_to_pil(fig):buf=io.BytesIO();fig.savefig(buf,format='png');buf.seek(0);return Image.open(buf)
|
| 34 |
-
@spaces.GPU
|
| 35 |
-
def run_example(task_prompt,image,text_input=None):
|
| 36 |
-
if text_input is None:prompt=task_prompt
|
| 37 |
-
else:prompt=task_prompt+text_input
|
| 38 |
-
inputs=processor(text=prompt,images=image,return_tensors='pt').to(device);generated_ids=model.generate(input_ids=inputs['input_ids'],pixel_values=inputs['pixel_values'],max_new_tokens=1024,early_stopping=False,do_sample=False,num_beams=3);generated_text=processor.batch_decode(generated_ids,skip_special_tokens=False)[0];parsed_answer=processor.post_process_generation(generated_text,task=task_prompt,image_size=(image.width,image.height));return parsed_answer
|
| 39 |
-
def plot_bbox(image,data):
|
| 40 |
-
fig,ax=plt.subplots();ax.imshow(image)
|
| 41 |
-
for(bbox,label)in zip(data['bboxes'],data['labels']):x1,y1,x2,y2=bbox;rect=patches.Rectangle((x1,y1),x2-x1,y2-y1,linewidth=1,edgecolor='r',facecolor='none');ax.add_patch(rect);plt.text(x1,y1,label,color='white',fontsize=8,bbox=dict(facecolor='red',alpha=.5))
|
| 42 |
-
ax.axis('off');return fig
|
| 43 |
-
def draw_polygons(image,prediction,fill_mask=False):
|
| 44 |
-
draw=ImageDraw.Draw(image);scale=1
|
| 45 |
-
for(polygons,label)in zip(prediction['polygons'],prediction['labels']):
|
| 46 |
-
color=random.choice(colormap);fill_color=random.choice(colormap)if fill_mask else None
|
| 47 |
-
for _polygon in polygons:
|
| 48 |
-
_polygon=np.array(_polygon).reshape(-1,2)
|
| 49 |
-
if len(_polygon)<3:print('Invalid polygon:',_polygon);continue
|
| 50 |
-
_polygon=(_polygon*scale).reshape(-1).tolist()
|
| 51 |
-
if fill_mask:draw.polygon(_polygon,outline=color,fill=fill_color)
|
| 52 |
-
else:draw.polygon(_polygon,outline=color)
|
| 53 |
-
draw.text((_polygon[0]+8,_polygon[1]+2),label,fill=color)
|
| 54 |
-
return image
|
| 55 |
-
|
| 56 |
-
def draw_ocr_bboxes(image,prediction):
|
| 57 |
-
scale=1;draw=ImageDraw.Draw(image);bboxes,labels=prediction['quad_boxes'],prediction['labels']
|
| 58 |
-
for(box,label)in zip(bboxes,labels):color=random.choice(colormap);new_box=(np.array(box)*scale).tolist();draw.polygon(new_box,width=3,outline=color);draw.text((new_box[0]+8,new_box[1]+2),'{}'.format(label),align='right',fill=color)
|
| 59 |
-
return image
|
| 60 |
-
def convert_to_od_format(data):bboxes=data.get('bboxes',[]);labels=data.get('bboxes_labels',[]);od_results={'bboxes':bboxes,'labels':labels};return od_results
|
| 61 |
-
|
| 62 |
-
def process_image(image,task_prompt,text_input=None):
|
| 63 |
-
if isinstance(image,str):image=Image.open(image)
|
| 64 |
-
else:image=Image.fromarray(image)
|
| 65 |
-
if task_prompt=='Caption':task_prompt='<CAPTION>';results=run_example(task_prompt,image);return results[task_prompt],None
|
| 66 |
-
elif task_prompt=='Detailed Caption':task_prompt='<DETAILED_CAPTION>';results=run_example(task_prompt,image);return results[task_prompt],None
|
| 67 |
-
elif task_prompt=='More Detailed Caption':task_prompt='<MORE_DETAILED_CAPTION>';results=run_example(task_prompt,image);return results,None
|
| 68 |
-
elif task_prompt=='Caption + Grounding':task_prompt='<CAPTION>';results=run_example(task_prompt,image);text_input=results[task_prompt];task_prompt='<CAPTION_TO_PHRASE_GROUNDING>';results=run_example(task_prompt,image,text_input);results['<CAPTION>']=text_input;fig=plot_bbox(image,results['<CAPTION_TO_PHRASE_GROUNDING>']);return results,fig_to_pil(fig)
|
| 69 |
-
elif task_prompt=='Detailed Caption + Grounding':task_prompt='<DETAILED_CAPTION>';results=run_example(task_prompt,image);text_input=results[task_prompt];task_prompt='<CAPTION_TO_PHRASE_GROUNDING>';results=run_example(task_prompt,image,text_input);results['<DETAILED_CAPTION>']=text_input;fig=plot_bbox(image,results['<CAPTION_TO_PHRASE_GROUNDING>']);return results,fig_to_pil(fig)
|
| 70 |
-
elif task_prompt=='More Detailed Caption + Grounding':task_prompt='<MORE_DETAILED_CAPTION>';results=run_example(task_prompt,image);text_input=results[task_prompt];task_prompt='<CAPTION_TO_PHRASE_GROUNDING>';results=run_example(task_prompt,image,text_input);results['<MORE_DETAILED_CAPTION>']=text_input;fig=plot_bbox(image,results['<CAPTION_TO_PHRASE_GROUNDING>']);return results,fig_to_pil(fig)
|
| 71 |
-
elif task_prompt=='Object Detection':task_prompt='<OD>';results=run_example(task_prompt,image);fig=plot_bbox(image,results['<OD>']);return results,fig_to_pil(fig)
|
| 72 |
-
elif task_prompt=='Dense Region Caption':task_prompt='<DENSE_REGION_CAPTION>';results=run_example(task_prompt,image);fig=plot_bbox(image,results['<DENSE_REGION_CAPTION>']);return results,fig_to_pil(fig)
|
| 73 |
-
elif task_prompt=='Region Proposal':task_prompt='<REGION_PROPOSAL>';results=run_example(task_prompt,image);fig=plot_bbox(image,results['<REGION_PROPOSAL>']);return results,fig_to_pil(fig)
|
| 74 |
-
elif task_prompt=='Caption to Phrase Grounding':task_prompt='<CAPTION_TO_PHRASE_GROUNDING>';results=run_example(task_prompt,image,text_input);fig=plot_bbox(image,results['<CAPTION_TO_PHRASE_GROUNDING>']);return results,fig_to_pil(fig)
|
| 75 |
-
elif task_prompt=='Referring Expression Segmentation':task_prompt='<REFERRING_EXPRESSION_SEGMENTATION>';results=run_example(task_prompt,image,text_input);output_image=copy.deepcopy(image);output_image=draw_polygons(output_image,results['<REFERRING_EXPRESSION_SEGMENTATION>'],fill_mask=True);return results,output_image
|
| 76 |
-
elif task_prompt=='Region to Segmentation':task_prompt='<REGION_TO_SEGMENTATION>';results=run_example(task_prompt,image,text_input);output_image=copy.deepcopy(image);output_image=draw_polygons(output_image,results['<REGION_TO_SEGMENTATION>'],fill_mask=True);return results,output_image
|
| 77 |
-
elif task_prompt=='Open Vocabulary Detection':task_prompt='<OPEN_VOCABULARY_DETECTION>';results=run_example(task_prompt,image,text_input);bbox_results=convert_to_od_format(results['<OPEN_VOCABULARY_DETECTION>']);fig=plot_bbox(image,bbox_results);return results,fig_to_pil(fig)
|
| 78 |
-
elif task_prompt=='Region to Category':task_prompt='<REGION_TO_CATEGORY>';results=run_example(task_prompt,image,text_input);return results,None
|
| 79 |
-
elif task_prompt=='Region to Description':task_prompt='<REGION_TO_DESCRIPTION>';results=run_example(task_prompt,image,text_input);return results,None
|
| 80 |
-
elif task_prompt=='OCR':task_prompt='<OCR>';results=run_example(task_prompt,image);return results,None
|
| 81 |
-
elif task_prompt=='OCR with Region':task_prompt='<OCR_WITH_REGION>';results=run_example(task_prompt,image);output_image=copy.deepcopy(image);output_image=draw_ocr_bboxes(output_image,results['<OCR_WITH_REGION>']);return results,output_image
|
| 82 |
-
else:return'',None # Return empty string and None for unknown task prompts
|
| 83 |
-
|
| 84 |
-
single_task_list=['Caption','Detailed Caption','More Detailed Caption','Object Detection','Dense Region Caption','Region Proposal','Caption to Phrase Grounding','Referring Expression Segmentation','Region to Segmentation','Open Vocabulary Detection','Region to Category','Region to Description','OCR','OCR with Region']
|
| 85 |
-
cascaded_task_list=['Caption + Grounding','Detailed Caption + Grounding','More Detailed Caption + Grounding']
|
| 86 |
-
|
| 87 |
-
def update_task_dropdown(choice):
|
| 88 |
-
if choice == 'Cascaded task':
|
| 89 |
-
return gr.Dropdown(choices=cascaded_task_list, value='Caption + Grounding')
|
| 90 |
-
else:
|
| 91 |
-
return gr.Dropdown(choices=single_task_list, value='Caption')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|