-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
50 lines (42 loc) · 1.8 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from detectron2.data import DatasetCatalog, MetadataCatalog
from detectron2.utils.visualizer import Visualizer
from detectron2.config import get_cfg
from detectron2 import model_zoo
from detectron2.utils.visualizer import ColorMode
import random
import cv2
import matplotlib.pyplot as plt
def plot_samples(dataset_name,n=1):
dataset_custom = DatasetCatalog.get(dataset_name)
dataset_custom_metadata = MetadataCatalog.get(dataset_name)
for s in random.sample(dataset_custom, n):
img = cv2.imread(s["file_name"])
v = Visualizer(img[:, :, ::-1], metadata=dataset_custom_metadata, scale=0.5)
v = v.draw_dataset_dict(s)
plt.figure(figsize = (15,20))
plt.imshow(v.get_image())
plt.show()
def get_train_cfg(config_file_path,checkpoint_url,train_dataset_name,test_dataset_name,num_classes,device,output_dir):
cfg = get_cfg()
cfg.merge_from_file(model_zoo.get_config_file(config_file_path))
cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url(checkpoint_url)
cfg.DATASETS.TRAIN = (train_dataset_name,)
cfg.DATASETS.TEST = (test_dataset_name,)
cfg.DATALOADER.NUM_WORKERS = 2
cfg.SOLVER.IMS_PER_BATCH = 2
cfg.SOLVER.BASE_LR = 0.0005
cfg.SOLVER.MAX_ITER = 20000
cfg.SOLVER.STEPS = []
cfg.MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE = 100
cfg.MODEL.ROI_HEADS.NUM_CLASSES = num_classes
cfg.MODEL.DEVICE = device
cfg.OUTPUT_DIR = output_dir
return cfg
def on_image(image_path,predictor):
im = cv2.imread(image_path)
outputs = predictor(im)
v = Visualizer(im[:, :, ::-1],metadata={},scale=0.5,instance_mode=ColorMode.SEGMENTATION)
v = v.draw_instance_predictions(outputs["instances"].to("cpu"))
plt.figure(figsize = (14,10))
plt.imshow(v.get_image())
plt.show()