Closed charlescho64 closed 4 years ago
Hello, I upload the file I have been testing in colab.
If possible, I hope you give me opinion to this code .
@charlescho64 This problem arises because the versions of detectron2 are not consistent and the APIs are kind of different. In our version, the mask head is initialized by calling: https://github.com/hustvl/BMaskR-CNN/blob/ed164ae6a9f47219a4ac06f5ae626889eb4687be/detectron2/modeling/roi_heads/roi_heads.py#L597 in BMask R-CNN: https://github.com/hustvl/BMaskR-CNN/blob/ed164ae6a9f47219a4ac06f5ae626889eb4687be/projects/BMaskR-CNN/bmaskrcnn/roi_heads.py#L26 While in the v0.1 version of detectron2 (old version):
The interface is different here.
After updating the version of detectron2, you can run your notebook.
Hi @charlescho64 Were you able to train BMaskRCNN on colab using single GPU? i am able to do inference but I am skeptical about training on single GPU and the time required for that.
Hi @c0gn0scente
it is possible to train BMaskRCNN with single GPU.
I hope you refer to below code. if you run step by step, you can do. ....................................... !pip install -U 'git+https://github.com/facebookresearch/fvcore.git' !pip install pyyaml==5.1 import torch, torchvision print(torch.version, torch.cuda.is_available()) !gcc --version assert torch.version.startswith("1.7") torch.version
!git clone --branch v0.1.3 https://github.com/facebookresearch/detectron2.git detectron2_repo !pip install -e detectron2_repo
!git clone https://github.com/hustvl/BMaskR-CNN bmask_repo
import detectron2 from detectron2.utils.logger import setup_logger setup_logger()
import matplotlib.pyplot as plt import numpy as np import cv2 from google.colab.patches import cv2_imshow
from detectron2.engine import DefaultPredictor from detectron2.config import get_cfg from detectron2.utils.visualizer import Visualizer from detectron2.data import MetadataCatalog, DatasetCatalog
import sys; sys.path.insert(0, "bmask_repo/projects/BMaskR-CNN") from bmaskrcnn import add_boundary_preserving_config
!wget https://github.com/matterport/Mask_RCNN/releases/download/v2.1/balloon_dataset.zip !unzip balloon_dataset.zip > /dev/null
from detectron2.structures import BoxMode
def get_balloon_dicts(img_dir): json_file = os.path.join(img_dir, "via_region_data.json") with open(json_file) as f: imgs_anns = json.load(f)
dataset_dicts = []
for idx, v in enumerate(imgs_anns.values()):
record = {}
filename = os.path.join(img_dir, v["filename"])
height, width = cv2.imread(filename).shape[:2]
record["file_name"] = filename
record["image_id"] = idx
record["height"] = height
record["width"] = width
annos = v["regions"]
objs = []
for _, anno in annos.items():
assert not anno["region_attributes"]
anno = anno["shape_attributes"]
px = anno["all_points_x"]
py = anno["all_points_y"]
poly = [(x + 0.5, y + 0.5) for x, y in zip(px, py)]
poly = [p for x in poly for p in x]
obj = {
"bbox": [np.min(px), np.min(py), np.max(px), np.max(py)],
"bbox_mode": BoxMode.XYXY_ABS,
"segmentation": [poly],
"category_id": 0,
}
objs.append(obj)
record["annotations"] = objs
dataset_dicts.append(record)
return dataset_dicts
for d in ["train", "val"]: DatasetCatalog.register("balloon_" + d, lambda d=d: get_balloondicts("balloon/" + d)) MetadataCatalog.get("balloon" + d).set(thing_classes=["balloon"]) balloon_metadata = MetadataCatalog.get("balloon_train")
import random, os, json dataset_dicts = get_balloon_dicts("balloon/train") for d in random.sample(dataset_dicts, 3): img = cv2.imread(d["file_name"]) visualizer = Visualizer(img[:, :, ::-1], metadata=balloon_metadata, scale=0.5) out = visualizer.draw_dataset_dict(d) cv2_imshow(out.get_image()[:, :, ::-1])
from detectron2.engine import DefaultTrainer from detectron2.config import get_cfg import os
cfg = get_cfg()
add_boundary_preserving_config(cfg)
cfg.merge_from_file("bmask_repo/projects/BMaskR-CNN/configs/cascade_bmask_rcnn_R_101_FPN_1x.yaml")
cfg.DATASETS.TRAIN = ("balloon_train",) cfg.DATASETS.TEST = () # no metrics implemented for this dataset cfg.DATALOADER.NUM_WORKERS = 2
cfg.SOLVER.IMS_PER_BATCH = 2 cfg.SOLVER.BASE_LR = 0.02 cfg.SOLVER.MAX_ITER = 300 # 300 iterations seems good enough, but you can certainly train longer
os.makedirs(cfg.OUTPUT_DIR, exist_ok=True) trainer = DefaultTrainer(cfg) trainer.resume_or_load(resume=False) trainer.train()
cfg.MODEL.WEIGHTS = os.path.join(cfg.OUTPUT_DIR, "model_final.pth") cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5 # set the testing threshold for this model cfg.DATASETS.TEST = ("balloon_train", ) predictor = DefaultPredictor(cfg)
from detectron2.utils.visualizer import ColorMode
dataset_dicts = get_balloon_dicts("balloon/val")
for d in random.sample(dataset_dicts, 3):
im = cv2.imread(d["file_name"])
outputs = predictor(im) # format is documented at https://detectron2.readthedocs.io/tutorials/models.html#model-output-format
v = Visualizer(im[:, :, ::-1],
metadata=balloon_metadata,
scale=0.5,
instance_mode=ColorMode.IMAGE_BW # remove the colors of unsegmented pixels. This option is only available for segmentation models
)
out = v.draw_instance_predictions(outputs["instances"].to("cpu"))
cv2_imshow(out.get_image()[:, :, ::-1])
Hi @c0gn0scente
it is possible to train BMaskRCNN with single GPU.
I hope you refer to below code. if you run step by step, you can do. ....................................... !pip install -U 'git+https://github.com/facebookresearch/fvcore.git' !pip install pyyaml==5.1 import torch, torchvision print(torch.version, torch.cuda.is_available()) !gcc --version assert torch.version.startswith("1.7") torch.version
!git clone --branch v0.1.3 https://github.com/facebookresearch/detectron2.git detectron2_repo !pip install -e detectron2_repo
!git clone https://github.com/hustvl/BMaskR-CNN bmask_repo
import detectron2 from detectron2.utils.logger import setup_logger setup_logger()
import matplotlib.pyplot as plt import numpy as np import cv2 from google.colab.patches import cv2_imshow
from detectron2.engine import DefaultPredictor from detectron2.config import get_cfg from detectron2.utils.visualizer import Visualizer from detectron2.data import MetadataCatalog, DatasetCatalog
import sys; sys.path.insert(0, "bmask_repo/projects/BMaskR-CNN") from bmaskrcnn import add_boundary_preserving_config
!wget https://github.com/matterport/Mask_RCNN/releases/download/v2.1/balloon_dataset.zip !unzip balloon_dataset.zip > /dev/null
from detectron2.structures import BoxMode
def get_balloon_dicts(img_dir): json_file = os.path.join(img_dir, "via_region_data.json") with open(json_file) as f: imgs_anns = json.load(f)
dataset_dicts = [] for idx, v in enumerate(imgs_anns.values()): record = {} filename = os.path.join(img_dir, v["filename"]) height, width = cv2.imread(filename).shape[:2] record["file_name"] = filename record["image_id"] = idx record["height"] = height record["width"] = width annos = v["regions"] objs = [] for _, anno in annos.items(): assert not anno["region_attributes"] anno = anno["shape_attributes"] px = anno["all_points_x"] py = anno["all_points_y"] poly = [(x + 0.5, y + 0.5) for x, y in zip(px, py)] poly = [p for x in poly for p in x] obj = { "bbox": [np.min(px), np.min(py), np.max(px), np.max(py)], "bbox_mode": BoxMode.XYXY_ABS, "segmentation": [poly], "category_id": 0, } objs.append(obj) record["annotations"] = objs dataset_dicts.append(record) return dataset_dicts
for d in ["train", "val"]: DatasetCatalog.register("balloon_" + d, lambda d=d: get_balloondicts("balloon/" + d)) MetadataCatalog.get("balloon" + d).set(thing_classes=["balloon"]) balloon_metadata = MetadataCatalog.get("balloon_train")
import random, os, json dataset_dicts = get_balloon_dicts("balloon/train") for d in random.sample(dataset_dicts, 3): img = cv2.imread(d["file_name"]) visualizer = Visualizer(img[:, :, ::-1], metadata=balloon_metadata, scale=0.5) out = visualizer.draw_dataset_dict(d) cv2_imshow(out.get_image()[:, :, ::-1])
from detectron2.engine import DefaultTrainer from detectron2.config import get_cfg import os
cfg = get_cfg()
add_boundary_preserving_config(cfg)
Load a config from file
cfg.merge_from_file("bmask_repo/projects/BMaskR-CNN/configs/cascade_bmask_rcnn_R_101_FPN_1x.yaml")
cfg.DATASETS.TRAIN = ("balloon_train",) cfg.DATASETS.TEST = () # no metrics implemented for this dataset cfg.DATALOADER.NUM_WORKERS = 2
cfg.SOLVER.IMS_PER_BATCH = 2 cfg.SOLVER.BASE_LR = 0.02 cfg.SOLVER.MAX_ITER = 300 # 300 iterations seems good enough, but you can certainly train longer
os.makedirs(cfg.OUTPUT_DIR, exist_ok=True) trainer = DefaultTrainer(cfg) trainer.resume_or_load(resume=False) trainer.train()
cfg.MODEL.WEIGHTS = os.path.join(cfg.OUTPUT_DIR, "model_final.pth") cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5 # set the testing threshold for this model cfg.DATASETS.TEST = ("balloon_train", ) predictor = DefaultPredictor(cfg)
from detectron2.utils.visualizer import ColorMode
dataset_dicts = get_balloon_dicts("balloon/val") for d in random.sample(dataset_dicts, 3): im = cv2.imread(d["file_name"]) outputs = predictor(im) # format is documented at https://detectron2.readthedocs.io/tutorials/models.html#model-output-format v = Visualizer(im[:, :, ::-1], metadata=balloon_metadata, scale=0.5, instance_mode=ColorMode.IMAGE_BW # remove the colors of unsegmented pixels. This option is only available for segmentation models ) out = v.draw_instance_predictions(outputs["instances"].to("cpu")) cv2_imshow(out.get_image()[:, :, ::-1])
Thanks, @charlescho64 for the confirmation and helper code. Best,
Hi @c0gn0scente
If you have problem, our service site is more easier than it. first of all, I hope you refer to my youtube showcase. https://www.youtube.com/watch?v=-VD8abN0Sc4
Hello,
I usually use detectron2 and predict image detection and classfication. I tried to test you solution with almost same method used in detection2. but I got test code and error like below
---------------------------------------TEST Code------------------------------------------------------ cfg = get_cfg() bmaskrcnn.add_boundary_preserving_config(cfg) // Load a config from file cfg.merge_from_file("bmask_repo/projects/BMaskR-CNN/configs/bmask_rcnn_R_50_FPN_1x.yaml") cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5
cfg.MODEL.WEIGHTS = "/content/gdrive/My Drive/bmask/bmask_rcnn_r50_1x.pth" bmask_predictor = DefaultPredictor(cfg) outputs = bmask_predictor(im)
--------------------------------------ERROR MESSAGE------------------------------------------------------- TypeError Traceback (most recent call last)