This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
virtualenv venv -p python3.7 | |
source venv/bin/activate | |
pip3 install torch torchvision | |
pip3 install opencv-python | |
pip3 install 'git+https://github.com/facebookresearch/fvcore' | |
pip3 install cython; pip install 'git+https://github.com/cocodataset/cocoapi.git#subdirectory=PythonAPI' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
v = Visualizer(im[:, :, ::-1], | |
metadata=balloon_metadata, | |
scale=0.8, | |
instance_mode=ColorMode.IMAGE_BW # делаем фон черно-белым | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cfg.MODEL.WEIGHTS = os.path.join(cfg.OUTPUT_DIR, "model_final.pth") | |
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5 | |
cfg.DATASETS.TEST = ("balloon/val", ) | |
predictor = DefaultPredictor(cfg) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from detectron2.engine import DefaultTrainer | |
from detectron2.config import get_cfg | |
cfg = get_cfg() | |
cfg.merge_from_file("./detectron2_repo/configs/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml") | |
cfg.DATASETS.TRAIN = ("balloon/train",) | |
cfg.DATASETS.TEST = () # на данном этапе производить оценку качества не будем | |
cfg.DATALOADER.NUM_WORKERS = 2 | |
cfg.MODEL.WEIGHTS = "detectron2://COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x/137849600/model_final_f10217.pkl" | |
cfg.SOLVER.IMS_PER_BATCH = 2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
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) | |
vis = visualizer.draw_dataset_dict(d) | |
cv2_imshow(vis.get_image()[:, :, ::-1]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import numpy as np | |
import json | |
from detectron2.structures import BoxMode | |
import itertools | |
# функция загрузки изображений в стандартный для Detectron2 формат | |
def get_balloon_dicts(img_dir): | |
# COCO json аннотации | |
json_file = os.path.join(img_dir, "via_region_data.json") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# скачивание и распаковывание датасета | |
!wget https://github.com/matterport/Mask_RCNN/releases/download/v2.1/balloon_dataset.zip | |
!unzip balloon_dataset.zip > /dev/null |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
KEYPOINT_CONFIG = "./detectron2_repo/configs/COCO-Keypoints/keypoint_rcnn_R_101_FPN_3x.yaml" | |
KEYPOINT_CONFIG_WEIGHTS = "detectron2://COCO-Keypoints/keypoint_rcnn_R_101_FPN_3x/138363331/model_final_997cc7.pkl" | |
predictor = get_predictor(KEYPOINT_CONFIG, KEYPOINT_CONFIG_WEIGHTS) | |
outputs = predictor(im) | |
visualise(outputs) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DEFAULT_CONFIG = "./detectron2_repo/configs/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml" | |
DEFAULT_CONFIG_WEIGHTS = "detectron2://COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x/137849600/model_final_f10217.pkl" | |
cfg = get_cfg() | |
# Метод конфигурации модели с заданными параметрами и весами | |
def get_predictor(config_path=DEFAULT_CONFIG, config_weights=DEFAULT_CONFIG_WEIGHTS): | |
cfg.merge_from_file(config_path) | |
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5 # Порог для заданной модели | |
cfg.MODEL.WEIGHTS = config_weights |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
predictor = get_predictor() | |
outputs = predictor(im) | |
visualise(outputs) |
NewerOlder