main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gd
|
| 2 |
+
import torch
|
| 3 |
+
from torchvision import
|
| 4 |
+
|
| 5 |
+
# -- get torch and cuda version
|
| 6 |
+
TORCH_VERSION = ".".join(torch.__version__.split(".")[:2])
|
| 7 |
+
CUDA_VERSION = torch.__version__.split("+")[-1]
|
| 8 |
+
|
| 9 |
+
# -- install pre-build detectron2
|
| 10 |
+
!pip install detectron2 -f https://dl.fbaipublicfiles.com/detectron2/wheels/{CUDA_VERSION}/{TORCH_VERSION}/index.html
|
| 11 |
+
|
| 12 |
+
import detectron2
|
| 13 |
+
from detectron2.utils.logger import setup_logger # ????
|
| 14 |
+
|
| 15 |
+
from detectron2 import model_zoo
|
| 16 |
+
from detectron2.engine import DefaultPredictor
|
| 17 |
+
from detectron2.config import get_cfg
|
| 18 |
+
|
| 19 |
+
# ????
|
| 20 |
+
setup_logger()
|
| 21 |
+
|
| 22 |
+
# -- load rcnn model
|
| 23 |
+
cfg = get_cfg()
|
| 24 |
+
# add project-specific config (e.g., TensorMask) here if you're not running a model in detectron2's core library
|
| 25 |
+
cfg.merge_from_file(model_zoo.get_config_file("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml"))
|
| 26 |
+
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5 # set threshold for this model
|
| 27 |
+
# Find a model from detectron2's model zoo. You can use the https://dl.fbaipublicfiles... url as well
|
| 28 |
+
cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml")
|
| 29 |
+
predictor = DefaultPredictor(cfg)
|
| 30 |
+
|
| 31 |
+
!wget http://images.cocodataset.org/val2017/000000439715.jpg -q -O input.jpg
|
| 32 |
+
im = cv2.imread("./input.jpg")
|
| 33 |
+
cv2_imshow(im)
|
| 34 |
+
|
| 35 |
+
outputs = predictor(im)
|
| 36 |
+
|
| 37 |
+
print(outputs["instances"].pred_classes)
|
| 38 |
+
print(outputs["instances"].pred_boxes)
|
| 39 |
+
|
| 40 |
+
# -- load Mask R-CNN model for segmentation
|
| 41 |
+
MaskRCNN = torch.load("MaskRCNN.pt")
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
|