Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,40 +10,50 @@ os.system("python setup.py build install")
|
|
| 10 |
os.chdir("/home/user/app/Mask2Former/")
|
| 11 |
import gradio as gr
|
| 12 |
# check pytorch installation:
|
| 13 |
-
import torch, torchvision
|
| 14 |
-
print(torch.__version__, torch.cuda.is_available())
|
| 15 |
-
assert torch.__version__.startswith("1.9") # please manually install torch 1.9 if Colab changes its default version
|
| 16 |
-
# Some basic setup:
|
| 17 |
-
# Setup detectron2 logger
|
| 18 |
import detectron2
|
| 19 |
from detectron2.utils.logger import setup_logger
|
|
|
|
|
|
|
| 20 |
|
| 21 |
# import some common libraries
|
| 22 |
import numpy as np
|
| 23 |
-
import
|
|
|
|
|
|
|
| 24 |
|
| 25 |
# import some common detectron2 utilities
|
| 26 |
from detectron2 import model_zoo
|
| 27 |
from detectron2.engine import DefaultPredictor
|
| 28 |
from detectron2.config import get_cfg
|
| 29 |
-
from detectron2.utils.visualizer import Visualizer
|
| 30 |
-
from detectron2.data import MetadataCatalog
|
| 31 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
cfg = get_cfg()
|
| 34 |
cfg.MODEL.DEVICE='cpu'
|
| 35 |
-
|
| 36 |
-
cfg
|
| 37 |
-
cfg.
|
| 38 |
-
|
| 39 |
-
cfg.MODEL.
|
|
|
|
|
|
|
| 40 |
predictor = DefaultPredictor(cfg)
|
|
|
|
|
|
|
| 41 |
def inference(img):
|
| 42 |
-
im = cv2.imread(img
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
|
| 49 |
title = "Detectron 2"
|
|
@@ -52,7 +62,7 @@ article = "<p style='text-align: center'><a href='https://ai.facebook.com/blog/-
|
|
| 52 |
|
| 53 |
examples = [['airplane.png']]
|
| 54 |
|
| 55 |
-
gr.Interface(inference, inputs=gr.inputs.Image(type="
|
| 56 |
description=description,
|
| 57 |
article=article,
|
| 58 |
examples=examples).launch()
|
|
|
|
| 10 |
os.chdir("/home/user/app/Mask2Former/")
|
| 11 |
import gradio as gr
|
| 12 |
# check pytorch installation:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
import detectron2
|
| 14 |
from detectron2.utils.logger import setup_logger
|
| 15 |
+
setup_logger()
|
| 16 |
+
setup_logger(name="mask2former")
|
| 17 |
|
| 18 |
# import some common libraries
|
| 19 |
import numpy as np
|
| 20 |
+
import cv2
|
| 21 |
+
import torch
|
| 22 |
+
from google.colab.patches import cv2_imshow
|
| 23 |
|
| 24 |
# import some common detectron2 utilities
|
| 25 |
from detectron2 import model_zoo
|
| 26 |
from detectron2.engine import DefaultPredictor
|
| 27 |
from detectron2.config import get_cfg
|
| 28 |
+
from detectron2.utils.visualizer import Visualizer, ColorMode
|
| 29 |
+
from detectron2.data import MetadataCatalog
|
| 30 |
+
from detectron2.projects.deeplab import add_deeplab_config
|
| 31 |
+
coco_metadata = MetadataCatalog.get("coco_2017_val_panoptic")
|
| 32 |
+
|
| 33 |
+
# import Mask2Former project
|
| 34 |
+
from mask2former import add_maskformer2_config
|
| 35 |
|
| 36 |
cfg = get_cfg()
|
| 37 |
cfg.MODEL.DEVICE='cpu'
|
| 38 |
+
add_deeplab_config(cfg)
|
| 39 |
+
add_maskformer2_config(cfg)
|
| 40 |
+
cfg.merge_from_file("configs/coco/panoptic-segmentation/swin/maskformer2_swin_large_IN21k_384_bs16_100ep.yaml")
|
| 41 |
+
cfg.MODEL.WEIGHTS = 'https://dl.fbaipublicfiles.com/maskformer/mask2former/coco/panoptic/maskformer2_swin_large_IN21k_384_bs16_100ep/model_final_f07440.pkl'
|
| 42 |
+
cfg.MODEL.MASK_FORMER.TEST.SEMANTIC_ON = True
|
| 43 |
+
cfg.MODEL.MASK_FORMER.TEST.INSTANCE_ON = True
|
| 44 |
+
cfg.MODEL.MASK_FORMER.TEST.PANOPTIC_ON = True
|
| 45 |
predictor = DefaultPredictor(cfg)
|
| 46 |
+
outputs = predictor(im)
|
| 47 |
+
|
| 48 |
def inference(img):
|
| 49 |
+
im = cv2.imread(img)
|
| 50 |
+
v = Visualizer(im[:, :, ::-1], coco_metadata, scale=1.2, instance_mode=ColorMode.IMAGE_BW)
|
| 51 |
+
panoptic_result = v.draw_panoptic_seg(outputs["panoptic_seg"][0].to("cpu"), outputs["panoptic_seg"][1]).get_image()
|
| 52 |
+
v = Visualizer(im[:, :, ::-1], coco_metadata, scale=1.2, instance_mode=ColorMode.IMAGE_BW)
|
| 53 |
+
instance_result = v.draw_instance_predictions(outputs["instances"].to("cpu")).get_image()
|
| 54 |
+
v = Visualizer(im[:, :, ::-1], coco_metadata, scale=1.2, instance_mode=ColorMode.IMAGE_BW)
|
| 55 |
+
semantic_result = v.draw_sem_seg(outputs["sem_seg"].argmax(0).to("cpu")).get_image()
|
| 56 |
+
return Image.fromarray(np.uint8(panoptic_result)).convert('RGB'),Image.fromarray(np.uint8(instance_result)).convert('RGB'),Image.fromarray(np.uint8(semantic_result)).convert('RGB')
|
| 57 |
|
| 58 |
|
| 59 |
title = "Detectron 2"
|
|
|
|
| 62 |
|
| 63 |
examples = [['airplane.png']]
|
| 64 |
|
| 65 |
+
gr.Interface(inference, inputs=gr.inputs.Image(type="filepath"), outputs=[gr.outputs.Image(label="Panoptic segmentation",type="pil"),gr.outputs.Image(label="instance segmentation",type="pil"),gr.outputs.Image(label="semantic segmentation",type="pil")],enable_queue=True, title=title,
|
| 66 |
description=description,
|
| 67 |
article=article,
|
| 68 |
examples=examples).launch()
|