Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,11 @@
|
|
|
|
1 |
import spaces
|
2 |
import gradio as gr
|
3 |
from transformers import AutoProcessor, AutoModelForCausalLM
|
4 |
# import peft
|
5 |
-
|
6 |
import requests
|
7 |
import copy
|
8 |
-
import os
|
9 |
|
10 |
from PIL import Image, ImageDraw, ImageFont
|
11 |
import io
|
@@ -15,6 +15,16 @@ import matplotlib.patches as patches
|
|
15 |
import random
|
16 |
import numpy as np
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
import subprocess
|
19 |
subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
|
20 |
|
@@ -130,11 +140,7 @@ single_task_list =[
|
|
130 |
'Object Detection'
|
131 |
]
|
132 |
|
133 |
-
#
|
134 |
-
example_image_dir = 'examples/bccd-test/'
|
135 |
-
annotations_file_path = os.path.join(example_image_dir, '_annotations.coco.json')
|
136 |
-
example_images = [f for f in os.listdir(example_image_dir) if f.endswith('.jpg')]
|
137 |
-
|
138 |
cfg = get_cfg()
|
139 |
cfg.merge_from_file(model_zoo.get_config_file("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml"))
|
140 |
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5
|
@@ -142,18 +148,13 @@ cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url("COCO-InstanceSegmentation/mask
|
|
142 |
cfg.MODEL.DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
143 |
predictor = DefaultPredictor(cfg)
|
144 |
|
145 |
-
def
|
146 |
image_path = os.path.join(example_image_dir, image_name)
|
147 |
image = cv2.imread(image_path)
|
148 |
outputs = predictor(image)
|
149 |
v = Visualizer(image[:, :, ::-1], MetadataCatalog.get(cfg.DATASETS.TRAIN[0]), scale=1.2)
|
150 |
out = v.draw_instance_predictions(outputs["instances"].to("cpu"))
|
151 |
-
|
152 |
-
plt.imshow(out.get_image()[:, :, ::-1])
|
153 |
-
plt.axis('off')
|
154 |
-
plt.show()
|
155 |
-
return out.get_image()[:, :, ::-1]
|
156 |
-
|
157 |
|
158 |
with gr.Blocks(theme="sudeepshouche/minimalist") as demo:
|
159 |
gr.Markdown("## 🧬OmniScience - building teams of fine tuned VLM models for diagnosis and detection 🔧")
|
@@ -163,6 +164,7 @@ with gr.Blocks(theme="sudeepshouche/minimalist") as demo:
|
|
163 |
gr.Markdown("BCCD Datasets on Hugging Face:")
|
164 |
gr.Markdown("- [🌺 Florence 2](https://huggingface.co/datasets/dwb2023/roboflow100-bccd-florence2/viewer/default/test?q=BloodImage_00038_jpg.rf.1b0ce1635e11b3b49302de527c86bb02.jpg), [💎 PaliGemma](https://huggingface.co/datasets/dwb2023/roboflow-bccd-paligemma/viewer/default/test?q=BloodImage_00038_jpg.rf.1b0ce1635e11b3b49302de527c86bb02.jpg)")
|
165 |
|
|
|
166 |
with gr.Tab(label="Florence-2 Object Detection"):
|
167 |
with gr.Row():
|
168 |
with gr.Column():
|
@@ -204,12 +206,7 @@ with gr.Blocks(theme="sudeepshouche/minimalist") as demo:
|
|
204 |
with gr.Column():
|
205 |
output_img = gr.Image(label="Output Image")
|
206 |
|
207 |
-
|
208 |
-
image_path = os.path.join(example_image_dir, image_name)
|
209 |
-
result_image = process_segmentation(image_path, annotations_file_path)
|
210 |
-
return result_image
|
211 |
-
|
212 |
-
submit_btn.click(process_segmentation_tab, input_img, output_img)
|
213 |
|
214 |
gr.Markdown("## 🚀Other Cool Stuff:")
|
215 |
gr.Markdown("- [Florence 2 Whitepaper](https://arxiv.org/pdf/2311.06242) - how I found out about the Roboflow 100 and the BCCD dataset. Turns out this nugget was from the original [Florence whitepaper](https://arxiv.org/pdf/2111.11432) but useful all the same!")
|
@@ -218,4 +215,4 @@ with gr.Blocks(theme="sudeepshouche/minimalist") as demo:
|
|
218 |
gr.Markdown("- [OmniScience fork of Landing AI repo](https://huggingface.co/spaces/dwb2023/omniscience) - I had a lot of fun with this one... some great 🔍reverse engineering enabled by W&B's Weave📊.")
|
219 |
gr.Markdown("- [Scooby Snacks🐕 - microservice based function calling with style](https://huggingface.co/spaces/dwb2023/blackbird-app) - Leveraging 🤖Claude Sonnet 3.5 to orchestrate Microservice-Based Function Calling.")
|
220 |
|
221 |
-
demo.launch(debug=True)
|
|
|
1 |
+
import os
|
2 |
import spaces
|
3 |
import gradio as gr
|
4 |
from transformers import AutoProcessor, AutoModelForCausalLM
|
5 |
# import peft
|
6 |
+
|
7 |
import requests
|
8 |
import copy
|
|
|
9 |
|
10 |
from PIL import Image, ImageDraw, ImageFont
|
11 |
import io
|
|
|
15 |
import random
|
16 |
import numpy as np
|
17 |
|
18 |
+
from transformers import AutoProcessor, AutoModelForCausalLM
|
19 |
+
from detectron2.config import get_cfg
|
20 |
+
from detectron2.engine import DefaultPredictor
|
21 |
+
from detectron2 import model_zoo
|
22 |
+
from detectron2.utils.visualizer import Visualizer, ColorMode
|
23 |
+
from detectron2.data import MetadataCatalog
|
24 |
+
import cv2
|
25 |
+
import numpy as np
|
26 |
+
import matplotlib.pyplot as plt
|
27 |
+
|
28 |
import subprocess
|
29 |
subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
|
30 |
|
|
|
140 |
'Object Detection'
|
141 |
]
|
142 |
|
143 |
+
# Detectron2 configuration
|
|
|
|
|
|
|
|
|
144 |
cfg = get_cfg()
|
145 |
cfg.merge_from_file(model_zoo.get_config_file("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml"))
|
146 |
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5
|
|
|
148 |
cfg.MODEL.DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
149 |
predictor = DefaultPredictor(cfg)
|
150 |
|
151 |
+
def process_image_with_detectron2(image_name):
|
152 |
image_path = os.path.join(example_image_dir, image_name)
|
153 |
image = cv2.imread(image_path)
|
154 |
outputs = predictor(image)
|
155 |
v = Visualizer(image[:, :, ::-1], MetadataCatalog.get(cfg.DATASETS.TRAIN[0]), scale=1.2)
|
156 |
out = v.draw_instance_predictions(outputs["instances"].to("cpu"))
|
157 |
+
return Image.fromarray(out.get_image()[:, :, ::-1])
|
|
|
|
|
|
|
|
|
|
|
158 |
|
159 |
with gr.Blocks(theme="sudeepshouche/minimalist") as demo:
|
160 |
gr.Markdown("## 🧬OmniScience - building teams of fine tuned VLM models for diagnosis and detection 🔧")
|
|
|
164 |
gr.Markdown("BCCD Datasets on Hugging Face:")
|
165 |
gr.Markdown("- [🌺 Florence 2](https://huggingface.co/datasets/dwb2023/roboflow100-bccd-florence2/viewer/default/test?q=BloodImage_00038_jpg.rf.1b0ce1635e11b3b49302de527c86bb02.jpg), [💎 PaliGemma](https://huggingface.co/datasets/dwb2023/roboflow-bccd-paligemma/viewer/default/test?q=BloodImage_00038_jpg.rf.1b0ce1635e11b3b49302de527c86bb02.jpg)")
|
166 |
|
167 |
+
|
168 |
with gr.Tab(label="Florence-2 Object Detection"):
|
169 |
with gr.Row():
|
170 |
with gr.Column():
|
|
|
206 |
with gr.Column():
|
207 |
output_img = gr.Image(label="Output Image")
|
208 |
|
209 |
+
submit_btn.click(process_segmentation, inputs=[input_img], outputs=[output_img])
|
|
|
|
|
|
|
|
|
|
|
210 |
|
211 |
gr.Markdown("## 🚀Other Cool Stuff:")
|
212 |
gr.Markdown("- [Florence 2 Whitepaper](https://arxiv.org/pdf/2311.06242) - how I found out about the Roboflow 100 and the BCCD dataset. Turns out this nugget was from the original [Florence whitepaper](https://arxiv.org/pdf/2111.11432) but useful all the same!")
|
|
|
215 |
gr.Markdown("- [OmniScience fork of Landing AI repo](https://huggingface.co/spaces/dwb2023/omniscience) - I had a lot of fun with this one... some great 🔍reverse engineering enabled by W&B's Weave📊.")
|
216 |
gr.Markdown("- [Scooby Snacks🐕 - microservice based function calling with style](https://huggingface.co/spaces/dwb2023/blackbird-app) - Leveraging 🤖Claude Sonnet 3.5 to orchestrate Microservice-Based Function Calling.")
|
217 |
|
218 |
+
demo.launch(debug=True)
|