"bbox_mode": BoxMode.XYXY_ABS,
                "segmentation": [poly],
                #"category_id": classes.index(anno['label']),
                "category_id": 0,
                "iscrowd": 0
            }
            objs.append(obj)
        record["annotations"] = objs
        dataset_dicts.append(record)
    return dataset_dicts

from detectron2.data import DatasetCatalog, MetadataCatalog
for d in ["train", "test"]:
    DatasetCatalog.register("prostate_" + d, lambda d=d: get_prostate_dicts('Prostate_cancer_data/' + d))
    #MetadataCatalog.get("prostate_" + d).set(thing_classes=['3', '4', '5'])
    MetadataCatalog.get("prostate_" + d).set(thing_classes=['5'])
prostate_metadata = MetadataCatalog.get("prostate_train")


# 데이터 등록여부 확인
import random
dataset_dicts = get_prostate_dicts("Prostate_cancer_data/train")


# In[6]:


for d in random.sample(dataset_dicts, 1):
    img = cv2.imread(d["file_name"])
    print(d["file_name"])
    v = Visualizer(img[:, :, ::-1], metadata=prostate_metadata, scale=1.0)
Пример #2
0
def getModelMetadataObjNames():
    metadata = MetadataCatalog.get("coco_2017_val")
    names = metadata.get("thing_classes", None)
    return names
Пример #3
0
    TensorboardXWriter,
)
from detectron2.data.datasets import register_coco_instances
from detectron2.data import MetadataCatalog, DatasetCatalog

logger = logging.getLogger("detectron2")

register_coco_instances(
    "plans_train", {},
    "./Flo2PlanAll-Seg/annotations/flo2plan_instances_final.json",
    "./Flo2PlanAll-Seg/images")
register_coco_instances(
    "plans_test", {},
    "./Flo2PlanAll-Seg/annotations/flo2plan_instances_final.json",
    "./Flo2PlanAll-Seg/images")
plans_metadata = MetadataCatalog.get("plans_train")
dataset_dicts = DatasetCatalog.get("plans_train")
#dataset_dicts_test = DatasetCatalog.get("plans_test")


def get_evaluator(cfg, dataset_name, output_folder=None):
    """
    Create evaluator(s) for a given dataset.
    This uses the special metadata "evaluator_type" associated with each builtin dataset.
    For your own dataset, you can simply create an evaluator manually in your
    script and do not have to worry about the hacky if-else logic here.
    """
    if output_folder is None:
        output_folder = os.path.join(cfg.OUTPUT_DIR, "inference")
    evaluator_list = []
    evaluator_type = MetadataCatalog.get(dataset_name).evaluator_type
                         np.max(py)],
                "bbox_mode": BoxMode.XYXY_ABS,
                "segmentation": [poly],
                "category_id": 0,
                "iscrowd": 0
            }
            objs.append(obj)
        record["annotations"] = objs
        dataset_dicts.append(record)
    return dataset_dicts


from detectron2.data import DatasetCatalog, MetadataCatalog
for d in ["train"]:
    DatasetCatalog.register("petbottle_" + d,
                            lambda d=d: get_balloon_dicts("petbottle_" + d))
    MetadataCatalog.get("petbottle_" + d).set(thing_classes=["petbottle"])
petbottle_metadata = MetadataCatalog.get("petbottle")

#path od dataset
dataset_dicts = get_balloon_dicts(path_of_raw_img)
for d in random.sample(dataset_dicts, 3):
    img = cv2.imread(d["file_name"])
    visualizer = Visualizer(img[:, :, ::-1],
                            metadata=petbottle_metadata,
                            scale=0.5)
    vis = visualizer.draw_dataset_dict(d)
    cv2.imshow("Frame", vis.get_image()[:, :, ::-1])
    cv2.waitKey(1000)

cv2.destroyAllWindows
Пример #5
0
                "WARNING: It looks like you have a CUDA device, but aren't " + "using CUDA.\nRun with --cuda for optimal training speed.")
            torch.set_default_tensor_type('torch.FloatTensor')
    else:
        torch.set_default_tensor_type('torch.FloatTensor')

    if not args.testSiamese:
        if not args.multi_cpu_eval:
            register_ILSVRC()
            cfg = get_cfg()
            cfg.merge_from_file("../../../configs/COCO-Detection/faster_rcnn_R_101_FPN_3x.yaml")
            cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5  # set threshold for this model
            # Find a model from detectron2's model zoo. You can either use the https://dl.fbaipublicfiles.... url, or use the following shorthand
            # cfg.MODEL.WEIGHTS ="../datasets/tem/train_output/model_0449999.pth"
            cfg.MODEL.WEIGHTS = args.weight_detector
            cfg.MODEL.ROI_HEADS.NUM_CLASSES = 30
            metalog = MetadataCatalog.get("ILSVRC_VID_val")

            predictor = DefaultPredictor(cfg)
            class_names = metalog.get("thing_classes", None)

    # assert 0 < args.pos_samples_ratio <= 1, "the pos_samples_ratio valid range is (0, 1]"

    # set opts based on the args.. especially the number of samples etc.
    # opts['nPos_init'] = int(args.initial_samples * args.pos_samples_ratio)
    # opts['nNeg_init'] = int(args.initial_samples - opts['nPos_init'])
    # opts['nPos_online'] = int(args.online_samples * args.pos_samples_ratio)
    # opts['nNeg_online'] = int(args.online_samples - opts['nPos_online'])

    # just to make sure if one of nNeg is zero, the other nNeg is zero (kinda small hack...)
    # if opts['nNeg_init'] == 0:
    #     opts['nNeg_online'] = 0
Пример #6
0
def register_pascal_voc(name, dirname, split, year):
    DatasetCatalog.register(name, lambda: load_voc_instances(dirname, split))
    MetadataCatalog.get(name).set(
        thing_classes=CLASS_NAMES, dirname=dirname, year=year, split=split
    )
Пример #7
0
        region_name='us-east-1',
        verify=False)
   print("Downloading Datasets ... ",end="")
   download_s3_folder('digitalhub','clearml-data/balloon','balloon')
   print("Done")

   print("Downloading pretrained models ... ",end="")
   s3.Bucket('digitalhub').download_file('clearml-models/detectron2/coco-instancesegmentation/mask_rcnn_R_50_FPN_3x/mask_rcnn_R_50_FPN_3x.yaml','/home/appuser/detectron2_repo/detectron2/model_zoo/configs/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml')
   s3.Bucket('digitalhub').download_file('clearml-models/detectron2/coco-instancesegmentation/mask_rcnn_R_50_FPN_3x/model_final_f10217.pkl','/home/appuser/model_final_f10217.pkl')
   print("Done")
 
### Registering dataset as per required by Detectron2 ###  
   print("Registering Datasets into Detectron ... ",end="")
   for d in ["train", "val"]:
      DatasetCatalog.register("balloon_" + d, lambda d=d: get_balloon_dicts("balloon/" + d))
      MetadataCatalog.get("balloon_" + d).set(thing_classes=["balloon"])
   balloon_metadata = MetadataCatalog.get("balloon_train")
   print("Done")   
   

### Randomly sampling and showing dataset ###
   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)
      imout=out.get_image()[:, :, ::-1]
      cv2.imwrite("Sample_"+d["file_name"], imout)
      
### CONFIGURING THE MODEL ###
   cfg = get_cfg()
Пример #8
0
def main():
    """ Mask RCNN Object Detection with Detectron2 """
    rospy.init_node("mask_rcnn", anonymous=True)
    bridge = CvBridge()
    start_time = time.time()
    image_counter = 0
    
    register_coco_instances("train_set", {}, "/home/labuser/ros_ws/src/odhe_ros/arm_camera_dataset/train/annotations.json", "/home/labuser/ros_ws/src/odhe_ros/arm_camera_dataset/train")
    register_coco_instances("test_set", {}, "/home/labuser/ros_ws/src/odhe_ros/arm_camera_dataset/test/annotations.json", "/home/labuser/ros_ws/src/odhe_ros/arm_camera_dataset/test")
    
    train_metadata = MetadataCatalog.get("train_set")
    print(train_metadata)
    dataset_dicts_train = DatasetCatalog.get("train_set")

    test_metadata = MetadataCatalog.get("test_set")
    print(test_metadata)
    dataset_dicts_test = DatasetCatalog.get("test_set")

    cfg = get_cfg()
    cfg.merge_from_file(model_zoo.get_config_file("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml"))
    cfg.DATASETS.TRAIN = ("train_set")
    cfg.DATASETS.TEST = ()  # no metrics implemented for this dataset
    cfg.DATALOADER.NUM_WORKERS = 4
    cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml")  # initialize from model zoo
    cfg.SOLVER.IMS_PER_BATCH = 4
    cfg.SOLVER.BASE_LR = 0.01
    cfg.SOLVER.MAX_ITER = 1000 # 300 iterations seems good enough, but you can certainly train longer
    cfg.MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE = (
        128
    )  # faster, and good enough for this toy dataset
    cfg.MODEL.ROI_HEADS.NUM_CLASSES = 5  # 5 classes (Plate, Carrot, Celery, Pretzel, Gripper)

    # Temporary Solution. If I train again I think I can use the dynamically set path again
    cfg.MODEL.WEIGHTS = os.path.join(cfg.OUTPUT_DIR, "/home/labuser/ros_ws/src/odhe_ros/arm_camera_dataset/output/model_final.pth")
    # cfg.MODEL.WEIGHTS = os.path.join(cfg.OUTPUT_DIR, "model_final.pth")
    cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.4   # set the testing threshold for this model
    cfg.DATASETS.TEST = ("test_set")
    predictor = DefaultPredictor(cfg)

    class_names = MetadataCatalog.get("train_set").thing_classes

    # Set up custom cv2 visualization parameters
    # Classes: [name, id]
    #               -
    #          [Plate,   0]
    #          [Carrot,  1]
    #          [Celery,  2]
    #          [Pretzel, 3]
    #          [Gripper, 4]

    # Colors = [blue, green, red]
    color_plate = [0, 255, 0]       # green
    color_carrot = [255, 200, 0]    # blue
    color_celery = [0, 0, 255]      # red
    color_pretzel = [0, 220, 255]   # yellow
    color_gripper = [204, 0, 150]   # purple
    colors = list([color_plate, color_carrot, color_celery, color_pretzel, color_gripper])

    alpha = .4

    run = maskRCNN()
    while not rospy.is_shutdown():
        # Get images
        img = run.get_img()

        if img is not None:
            outputs = predictor(img)
            predictions = outputs["instances"].to("cpu")

            # Get results
            result = run.getResult(predictions, class_names)

            # Visualize using custom cv2 code
            if result is not None:
                result_cls = result.class_names
                result_clsId = result.class_ids
                result_scores = result.scores
                result_masks = result.masks

                # Create copies of the original image
                im = img.copy()
                output = img.copy()

                # Initialize lists
                masks = []
                masks_indices = []
                for i in range(len(result_clsId)):
                    # Obtain current object mask as a numpy array (black and white mask of single object)
                    current_mask = bridge.imgmsg_to_cv2(result_masks[i])

                    # Find current mask indices
                    mask_indices = np.where(current_mask==255)

                    # Add to mask indices list
                    if len(masks_indices) > len(result_clsId):
                        masks_indices = []
                    else:
                        masks_indices.append(mask_indices)

                    # Add to mask list
                    if len(masks) > len(result_clsId):
                        masks = []
                    else:
                        masks.append(current_mask)

                if len(masks) > 0:
                    # Create composite mask
                    composite_mask = sum(masks)

                    # Clip composite mask between 0 and 255   
                    composite_mask = composite_mask.clip(0, 255)

                # # Apply mask to image
                # masked_img = cv2.bitwise_and(im, im, mask=current_mask)

                # Find indices of object in mask
                # composite_mask_indices = np.where(composite_mask==255)

                for i in range(len(result_clsId)):
                    # Select correct object color
                    color = colors[result_clsId[i]]

                    # Change the color of the current mask object
                    im[masks_indices[i][0], masks_indices[i][1], :] = color

                # Apply alpha scaling to image to adjust opacity
                cv2.addWeighted(im, alpha, output, 1 - alpha, 0, output)

                for i in range(len(result_clsId)):
                    # Draw Bounding boxes
                    start_point = (result.boxes[i].x_offset, result.boxes[i].y_offset)
                    end_point = (result.boxes[i].x_offset + result.boxes[i].width, result.boxes[i].y_offset + result.boxes[i].height)
                    start_point2 = (result.boxes[i].x_offset + 2, result.boxes[i].y_offset + 2)
                    end_point2 = (result.boxes[i].x_offset + result.boxes[i].width - 2, result.boxes[i].y_offset + 12)
                    color = colors[result_clsId[i]]
                    box_thickness =  1

                    name = result_cls[i]
                    score = result_scores[i]
                    conf = round(score.item() * 100, 1)
                    string = str(name) + ":" + str(conf) + "%"
                    font = cv2.FONT_HERSHEY_SIMPLEX
                    org = (result.boxes[i].x_offset + 2, result.boxes[i].y_offset + 10)
                    fontScale = .3
                    text_thickness = 1
                    output = cv2.rectangle(output, start_point, end_point, color, box_thickness)
                    output = cv2.rectangle(output, start_point2, end_point2, color, -1)     # Text box
                    output = cv2.putText(output, string, org, font, fontScale, [0, 0, 0], text_thickness, cv2.LINE_AA, False)

                im_rgb = cv2.cvtColor(output, cv2.COLOR_BGR2RGB)
                im_msg = bridge.cv2_to_imgmsg(im_rgb, encoding="rgb8")


            # Display Image Counter
            image_counter = image_counter + 1
            # if (image_counter % 11) == 10:
            #     rospy.loginfo("Images detected per second=%.2f", float(image_counter) / (time.time() - start_time))

            run.publish(im_msg, result)    
        

    return 0
    return dataset_dicts


# Dataset registration
for d in ["train_kitti", "val_kitti"]:
    print("d=", d)
    # /home/group00/mcv/datasets/KITTI/training/label_2
    path = os.path.join("/home/group00/mcv/datasets/KITTI", d + ".txt")
    with open(path, "r") as f:
        lines = f.read().split('\n')

    #DatasetCatalog.register("balloon_" + d, lambda d=d: get_balloon_dicts("balloon/" + d))
    DatasetCatalog.register(d, lambda d=d: get_kitti_dataset(lines))
    MetadataCatalog.get(d).set(thing_classes=[
        'Car', 'Van', 'Truck', 'Pedestrian', 'Person_sitting', 'Cyclist',
        'Tram', 'Misc', 'DontCare'
    ])

kitti_train_metadata = MetadataCatalog.get("train_kitti")

path = os.path.join("/home/group00/mcv/datasets/KITTI", "train_kitti.txt")
with open(path, "r") as f:
    lines = f.read().split('\n')

dataset_dicts = get_kitti_dataset(lines)
for d in random.sample(dataset_dicts, 3):
    img = cv2.imread(d["file_name"])
    visualizer = Visualizer(img[:, :, ::-1],
                            metadata=kitti_train_metadata,
                            scale=0.5)
    out = visualizer.draw_dataset_dict(d)
Пример #10
0
def main(config):

    root = expanduser(config["base"]["root"])
    imgs_root = expanduser(config["base"]["imgs_root"])
    jsons_dir = join(root, "jsons")
    model_dir = join(root, "outputs")

    model_pth = expanduser(config["analyze_vids"]["model_pth"])
    vid_ending = '*' + config["analyze_vids"]["vid_ending"]
    expected_obj_nums = config["analyze_vids"]["expected_obj_nums"]
    score_cutoff = float(config["analyze_vids"]["score_cutoff"])
    vids_root = expanduser(config["analyze_vids"]["vids_root"])
    framerate = int(config["analyze_vids"]["framerate"])

    if not model_pth.endswith(".pth"):
        raise ValueError(f"{basename(model_pth)} must be a '.pth' file.")
    if not vid_ending.endswith(".mp4"):
        raise ValueError(f"{vid_ending} must end in '.mp4'")
    # if model_pth not in model_dir:
    #     raise IOError(f"The selected model, {basename(model_pth)}, is not in "
    #                   f"{basename(model_dir)}. Please pick a model that resides")
    if not 0 < score_cutoff < 1:
        raise ValueError(
            f"The testing threshold, {score_cutoff}, must be between 0 and 1.")

    vids = [str(path.absolute()) for path in Path(vids_root).rglob(vid_ending)]
    # from pprint import pprint; pprint(vids)
    # import ipdb;ipdb.set_trace()
    if len(vids) == 0:
        print(f"No .mp4 videos were found in {vids_root} ...")

    register_data(jsons_dir, imgs_root)

    # Need the `datasets =` line, in order for metadata to have the
    # .thing_classes attrib. I don't really use these two lines, I
    # only call them so I can get the .thing_classes attrib off
    # `metadata`. So, it doesn't matter if I use "training_data" as
    # my arg or some other registered dataset, for these two calls:
    datasets = DatasetCatalog.get("training_data")
    metadata = MetadataCatalog.get("training_data")

    # Read the cfg back in:
    with open(join(model_dir, "cfg.txt"), "r") as f:
        cfg = f.read()
    # Turn into CfgNode obj:
    cfg = CfgNode.load_cfg(cfg)

    # Use the weights from our chosen model:
    cfg.MODEL.WEIGHTS = model_pth
    # # Pick a confidence cutoff # TODO: based on PR curve
    # # TODO: don't have this as a user parameter
    cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = score_cutoff

    print("Generating predictor ...")
    predictor = DefaultPredictor(cfg)

    bgr_palette = [(165, 194, 102), (98, 141, 252), (203, 160, 141),
                   (195, 138, 231), (84, 216, 166), (47, 217, 255),
                   (148, 196, 229), (179, 179, 179)]

    for vid in vids:

        print(f"Analyzing {vid} ...")

        cap = cv2.VideoCapture(vid)
        frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
        pbar = trange(frame_count)
        output_vid = f"{splitext(vid)[0]}_detected.mp4"

        output_csv = f"{splitext(vid)[0]}_detected.csv"
        csv_file_handle = open(output_csv, "w", newline="")
        atexit.register(csv_file_handle.close)
        col_names = [
            "frame", "x1", "y1", "x2", "y2", "score", "thing", "dummy_id"
        ]
        csv_writer = csv.DictWriter(csv_file_handle, fieldnames=col_names)
        csv_writer.writeheader()

        # Define the codec and create VideoWriter object
        fourcc = cv2.VideoWriter_fourcc(*"mp4v")
        out = cv2.VideoWriter(filename=output_vid,
                              apiPreference=0,
                              fourcc=fourcc,
                              fps=int(framerate),
                              frameSize=(int(cap.get(3)), int(cap.get(4))),
                              params=None)

        # Use Detectron2 model on each frame in vid:
        all_detection_info = []
        for f, _ in enumerate(pbar):

            ret, frame = cap.read()

            if ret:

                detected = predictor(frame)

                # # Visualize:
                # visualizer = Visualizer(frame[:, :, ::-1],
                #                         metadata=metadata,
                #                         scale=1.0,
                #                         instance_mode=ColorMode)
                # visualizer = visualizer.draw_instance_predictions(detected["instances"].to("cpu"))
                # detected_img = visualizer.get_image()[:, :, ::-1]

                labelled_frame = frame

                # Save the predicted box coords and scores to a dictionary:
                preds = detected['instances'].to('cpu')
                boxes = preds.pred_boxes.tensor.numpy()
                thing_ids = preds.pred_classes.tolist()
                scores = preds.scores.numpy()

                # Get the idxs of each unique thing_id:
                idxs_of_each_thing = {}
                for thing_id in set(thing_ids):
                    idxs_of_each_thing[thing_id] = []
                for i, thing_id in enumerate(thing_ids):
                    idxs_of_each_thing[thing_id].append(i)

                # Split up the data according to thing_id:
                for i, (thing_id,
                        idxs) in enumerate(idxs_of_each_thing.items()):

                    thing_class = metadata.thing_classes[thing_id]

                    if thing_class in expected_obj_nums:

                        expected_obj_num = expected_obj_nums[thing_class]
                        num_boxes = scores.size

                        assert expected_obj_num <= num_boxes, \
                            f"You expected {expected_obj_num} {thing_class} in \
                            every frame of the video, according to `expected_obj_nums`, \
                            but only {num_boxes} were found in frame {f}."

                        thing_scores = scores[idxs]
                        thing_boxes = boxes[idxs]

                        # Here, I grab the top n animals according to their score
                        # because by default, `preds` is sorted by their descending scores:
                        for j in range(0, expected_obj_num):

                            coords = thing_boxes[j]
                            x1 = int(coords[0])
                            y1 = int(coords[1])
                            x2 = int(coords[2])
                            y2 = int(coords[3])
                            score = float(thing_scores[j])

                            labelled_frame = cv2.rectangle(
                                labelled_frame, (x1, y1), (x2, y2),
                                bgr_palette[thing_id], 2)
                            labelled_frame = cv2.putText(
                                labelled_frame, thing_class,
                                (x2 - 10, y2 - 40), cv2.FONT_HERSHEY_SIMPLEX,
                                1, bgr_palette[thing_id], 2)

                            # TODO: Write some sort of simple filtering thing that skips
                            # big jumps ... maybe just store the last bbox coords and compare.
                            # Define big jumps as a proportion of the frame width or average of
                            # last few frame deltas
                            csv_writer.writerow({
                                col_names[0]: int(f),  # frame
                                col_names[1]: x1,  # x1
                                col_names[2]: y1,  # y1
                                col_names[3]: x2,  # x2
                                col_names[4]: y2,  # y2
                                col_names[5]: score,  # score
                                col_names[6]: thing_class,  # thing
                                col_names[7]: i
                            })  # dummy id

                # Save frame to vid:
                out.write(labelled_frame)

                pbar.set_description(f"Detecting in frame {f+1}/{frame_count}")

    # Clear GPU memory
    torch.cuda.empty_cache()
Пример #11
0
    json.dump(dict_list_train, fout)
  
with open('seg_val.json', 'w') as fout:
    json.dump(dict_list_val, fout)
  
with open('seg_test.json', 'w') as fout:
    json.dump(dict_list_test, fout)

#register custom dataset
mode_list = ["train", "val", "test"]
path1 = "/content/seg_"


for mode in mode_list:
  DatasetCatalog.register("ds9_" + mode, lambda mode=mode: json_to_ds(path1 + mode + ".json"))
  MetadataCatalog.get("ds9_" + mode).set(thing_classes=["cell"])

cell_meta = MetadataCatalog.get("ds9_train")

#hyperparams
cfg = get_cfg()
cfg.merge_from_file(model_zoo.get_config_file("COCO-Detection/retinanet_R_50_FPN_1x.yaml")) #TBD, this arch for now
cfg.DATASETS.TRAIN = ("ds9_train",) # 
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5 #have this for now
cfg.DATASETS.TEST = () #

cfg.DATALOADER.NUM_WORKERS = 2 #if memory doesnt get too loaded can increase
cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url("COCO-Detection/retinanet_R_50_FPN_1x.yaml")  #TBD, these weights for now
cfg.SOLVER.IMS_PER_BATCH = 1 #number of images per batch per one core
cfg.SOLVER.BASE_LR = 0.001 #
cfg.SOLVER.MOMENTUM = 0.9 #
Пример #12
0
                        default=0.5,
                        type=float,
                        help="confidence threshold")
    args = parser.parse_args()

    logger = setup_logger()

    with PathManager.open(args.input, "r") as f:
        predictions = json.load(f)

    pred_by_image = defaultdict(list)
    for p in predictions:
        pred_by_image[p["image_id"]].append(p)

    dicts = list(DatasetCatalog.get(args.dataset))
    metadata = MetadataCatalog.get(args.dataset)
    if hasattr(metadata, "thing_dataset_id_to_contiguous_id"):

        def dataset_id_map(ds_id):
            return metadata.thing_dataset_id_to_contiguous_id[ds_id]

    elif "lvis" in args.dataset:
        # LVIS results are in the same format as COCO results, but have a different
        # mapping from dataset category id to contiguous category id in [0, #categories - 1]
        def dataset_id_map(ds_id):
            return ds_id - 1

    else:
        raise ValueError("Unsupported dataset: {}".format(args.dataset))

    os.makedirs(args.output, exist_ok=True)
Пример #13
0
import os
from detectron2 import model_zoo
from detectron2.engine import DefaultTrainer
from detectron2.config import get_cfg
from detectron2.data import MetadataCatalog, DatasetCatalog
from detectron2.data.datasets import register_coco_instances

train_json_path = "station_mask/annotation/mask_train.json"
val_json_path = "station_mask/annotation/mask_val.json"
train_image_path = "station_mask/images/train"
val_image_path = "station_mask/images/val"
register_coco_instances("station_mask_train", {}, train_json_path,
                        train_image_path)
register_coco_instances("station_mask_val", {}, val_json_path, val_image_path)

station_train_metadata = MetadataCatalog.get("station_mask_train")
station_val_metadata = MetadataCatalog.get("station_mask_val")

train_dataset_dicts = DatasetCatalog.get("station_mask_train")
val_dataset_dicts = DatasetCatalog.get("station_mask_val")

cfg = get_cfg()
cfg.merge_from_file(
    model_zoo.get_config_file("COCO-Detection/faster_rcnn_R_101_FPN_3x.yaml"))
cfg.DATASETS.TRAIN = ("station_mask_train", )
cfg.DATASETS.TEST = ()
cfg.DATALOADER.NUM_WORKERS = 2
cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url(
    "COCO-Detection/faster_rcnn_R_101_FPN_3x.yaml")
cfg.SOLVER.IMS_PER_BATCH = 2
cfg.SOLVER.BASE_LR = 0.00025  # pick a good LR
def get_caltech_dicts(split):
    json_file = split + '_annos.json'
    with open(json_file) as f:
        imgs_anns = json.load(f)

    for i in range(len(imgs_anns)):
        for j in range(len(imgs_anns[i]['annotations'])):
            imgs_anns[i]['annotations'][j]['bbox_mode'] = BoxMode.XYWH_ABS
        imgs_anns[i]['proposal_bbox_mode'] = BoxMode.XYXY_ABS

    return imgs_anns


for d in ["train", "test"]:
    DatasetCatalog.register("caltech_" + d, lambda d=d: get_caltech_dicts(d))
    MetadataCatalog.get("caltech_" + d).set(thing_classes=["person"])
caltech_metadata = MetadataCatalog.get("caltech_train")

cfg = get_cfg()
cfg.merge_from_file("./configs/frcn_tb.yaml")
os.makedirs(cfg.OUTPUT_DIR, exist_ok=True)
logger = logging.getLogger("frcn")
if not logger.isEnabledFor(logging.INFO):  # setup_logger is not called for d2
    setup_logger()

model = build_model(cfg)
logger.info("Model:\n{}".format(model))
model.train()
optimizer = build_optimizer(cfg, model)
scheduler = build_lr_scheduler(cfg, optimizer)
Пример #15
0
    def kitti_mots_test():
        return get_kiti_mots_dicts(ims_path_kitti,
                                   annots_path_kitti,
                                   is_train=False,
                                   train_percentage=train_percent_kitti_mots,
                                   image_extension='png')

    def mots_challenge_train():
        return get_kiti_mots_dicts(ims_path,
                                   annots_path,
                                   is_train=True,
                                   train_percentage=1.,
                                   image_extension='jpg')

    DatasetCatalog.register("kitti_mots_train", kitti_mots_train)
    MetadataCatalog.get("kitti_mots_train").set(
        thing_classes=[k for k, v in kitti_correspondences.items()])

    DatasetCatalog.register("kitti_mots_test", kitti_mots_test)
    MetadataCatalog.get("kitti_mots_test").set(
        thing_classes=[k for k, v in kitti_correspondences.items()])

    DatasetCatalog.register("mots_challenge_train", mots_challenge_train)
    MetadataCatalog.get("mots_challenge_train").set(
        thing_classes=[k for k, v in mots_correspondences.items()])

    #        | COCO | KITTI | MOTSChallenge
    # person | 0    | 1     | 1
    # Car    | 2    | 0     | -

    # dict that maps category_id from coco dataset to the corresponding ones in kitti
    coco_to_kitti_dict = {
Пример #16
0
    cfg.MODEL.ROI_HEADS.NUM_CLASSES = 1  # only has one class (ballon)
    cfg.OUTPUT_DIR = OUTPUT_DIR
    cfg.SOLVER.BASE_LR = 0.0001  # pick a good LR

    # INPUT settings are very important !!
    cfg.INPUT.CROP.ENABLED = True
    cfg.INPUT.CROP.TYPE = "relative"
    cfg.INPUT.CROP.SIZE = [0.4, 0.4]
    cfg.INPUT.MIN_SIZE_TRAIN = (960, )  # 2400 * 0.4
    cfg.INPUT.MAX_SIZE_TRAIN = 1440
    cfg.INPUT.MIN_SIZE_TEST = 2400
    cfg.INPUT.MAX_SIZE_TEST = 4500

    print(cfg)

    metadata = MetadataCatalog.get("val")
    dataset_dicts = DatasetCatalog.get("val")

    trainer = DefaultTrainer(cfg)
    trainer.resume_or_load(resume=False)

    evaluator = COCOEvaluator("val",
                              cfg,
                              False,
                              output_dir=os.path.join(cfg.OUTPUT_DIR,
                                                      "inference"))
    val_loader = build_detection_test_loader(cfg, "val")
    inference_on_dataset(trainer.model, val_loader, evaluator)
    """
    predictor = DefaultPredictor(cfg)
    for d in random.sample(dataset_dicts, 5):
Пример #17
0
if __name__ == "__main__":

    dataset_name = "Dataset_name"
    class_list = ["box"]
    models_pretrained_type_list = [
        "COCO-Detection/faster_rcnn_R_50_FPN_3x.yaml",
        "COCO-InstanceSegmentation/mask_rcnn_R_50_C4_3x.yaml",
        "COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml"
    ]
    model_type_title = models_pretrained_type_list[2]

    for d in ["train", "val"]:
        DatasetCatalog.register(dataset_name + "_" + d,
                                lambda d=d: get_dicts(dataset_name + "/" + d,
                                                      "via_region_data.json"))
        MetadataCatalog.get(dataset_name + "_" +
                            d).set(thing_classes=class_list)
    train_metadata = MetadataCatalog.get(dataset_name + "_train")

    # Visualize connecting with dataset
    dataset_dicts = get_dicts(dataset_name + "/train", "via_region_data.json")
    for d in random.sample(dataset_dicts, 3):
        img = cv2.imread(d["file_name"])
        visualizer = Visualizer(img[:, :, ::-1],
                                metadata=train_metadata,
                                scale=0.5)
        vis = visualizer.draw_dataset_dict(d)
        print(vis.get_image().shape)
        cv2.imshow("image", vis.get_image()[:, :, ::-1])
        cv2.waitKey(2000)
        cv2.destroyAllWindows()
def generate_config(args):
    cfg, _ = load_model(config_only=True)
    classes = MetadataCatalog.get(cfg.DATASETS.TRAIN[0]).thing_classes

    if args.video_id == "eKKdRy20HXI":
        return TubeletAlgorithmConfig(
            class_index_to_detect=classes.index("car"),
            score_threshold=0.7,
            tubelet_iou_threshold=0.3,
            nms_iou_threshold=0.4,
            extend_class_only=False,
            num_skippable_frames=10,
            max_dimension_change_ratio=0.4,
            max_dimension_change_abs=20,
            perform_projection=True,
            min_extension_probability=0.01,
            min_extension_probability_after_skipped_frames=0.01,
        )
    elif args.video_id == "fgHjVvqLXV8":
        return TubeletAlgorithmConfig(
            class_index_to_detect=classes.index("airplane"),
            score_threshold=0.7,
            tubelet_iou_threshold=0.1,
            nms_iou_threshold=0.4,
            extend_class_only=False,
            num_skippable_frames=20,
            max_dimension_change_ratio=0.3,
            max_dimension_change_abs=15,
            perform_projection=True,
            min_extension_probability=0.01,
            min_extension_probability_after_skipped_frames=0.01,
        )
    elif args.video_id == "3h6aLq2kjxg":
        return TubeletAlgorithmConfig(
            class_index_to_detect=classes.index("traffic light"),
            score_threshold=0.7,
            tubelet_iou_threshold=0.1,
            nms_iou_threshold=0.4,
            extend_class_only=False,
            num_skippable_frames=5,
            max_dimension_change_ratio=0.3,
            max_dimension_change_abs=15,
            perform_projection=True,
            min_extension_probability=0.01,
            min_extension_probability_after_skipped_frames=0.01,
        )
    elif args.video_id == "AUyOZAfnehM":
        return TubeletAlgorithmConfig(
            class_index_to_detect=classes.index("sports ball"),
            score_threshold=0.7,
            tubelet_iou_threshold=0.1,
            nms_iou_threshold=0.4,
            extend_class_only=False,
            num_skippable_frames=20,
            max_dimension_change_ratio=0.3,
            max_dimension_change_abs=15,
            perform_projection=True,
            min_extension_probability=0.01,
            min_extension_probability_after_skipped_frames=0.01,
        )
    else:
        raise ValueError(f"Unknown video: {args.video_id}")
Пример #19
0
    ("coco/train2014", "coco/annotations/densepose_train2014.json"),
    "densepose_coco_2014_minival":
    ("coco/val2014", "coco/annotations/densepose_minival2014.json"),
    "densepose_coco_2014_minival_100": (
        "coco/val2014",
        "coco/annotations/densepose_minival2014_100.json",
    ),
    "densepose_coco_2014_valminusminival": (
        "coco/val2014",
        "coco/annotations/densepose_valminusminival2014.json",
    ),
}

DENSEPOSE_KEYS = ["dp_x", "dp_y", "dp_I", "dp_U", "dp_V", "dp_masks"]

for key, (image_root, json_file) in SPLITS.items():
    # Assume pre-defined datasets live in `./datasets`.
    json_file = os.path.join("datasets", json_file)
    image_root = os.path.join("datasets", image_root)

    DatasetCatalog.register(
        key,
        lambda key=key, json_file=json_file, image_root=image_root:
        load_coco_json(
            json_file, image_root, key, extra_annotation_keys=DENSEPOSE_KEYS),
    )

    MetadataCatalog.get(key).set(json_file=json_file,
                                 image_root=image_root,
                                 **get_densepose_metadata())
                             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", "test"]:
        DatasetCatalog.register(
            "balloon_" + d,
            lambda d=d: get_balloon_dicts(input_data_source + "/" + d))
        MetadataCatalog.get("balloon_" + d).set(thing_classes=["balloon"])
    balloon_metadata = MetadataCatalog.get("balloon_train")
    """To verify the data loading is correct, let's visualize the annotations of randomly selected samples in the training set:"""

    #     dataset_dicts = get_balloon_dicts(train_directory)
    #     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("Verify_data",out.get_image()[:, :, ::-1])
    #         cv2.waitKey(1000)
    #         cv2.destroyAllWindows()
    """## Train!

    Now, let's fine-tune a COCO-pretrained R50-FPN Mask R-CNN model on the balloon dataset. It takes ~6 minutes to train 300 iterations on Colab's K80 GPU, or ~2 minutes on a P100 GPU.
    """
Пример #21
0
# import some common libraries
import matplotlib.pyplot as plt
import numpy as np
import cv2
from google.colab.patches import cv2_imshow

# import some common detectron2 utilities
from detectron2.engine import DefaultPredictor
from detectron2.config import get_cfg
from detectron2.utils.visualizer import Visualizer
from detectron2.data import MetadataCatalog, DatasetCatalog

from detectron2.data.datasets import register_coco_instances
#register_coco_instances("fruits_nuts", {}, "/home/sxm/cocodataset/data/trainval.json", "/home/sxm/cocodataset/data/images")

fruits_nuts_metadata = MetadataCatalog.get("fruits_nuts")
dataset_dicts = DatasetCatalog.get("fruits_nuts")

import random

for d in random.sample(dataset_dicts, 3):
    img = cv2.imread(d["file_name"])
    visualizer = Visualizer(img[:, :, ::-1],
                            metadata=fruits_nuts_metadata,
                            scale=0.5)
    vis = visualizer.draw_dataset_dict(d)
    #cv2_imshow(vis.get_image()[:, :, ::-1])
    plt.figure()
    plt.imshow(vis.get_image()[:, :, ::-1])
    plt.show()
def main():
    """ Faster RCNN Object Detection with Detectron2 """
    rospy.init_node("faster_rcnn", anonymous=True)
    bridge = CvBridge()
    start_time = time.time()
    image_counter = 0

    register_coco_instances(
        "train_set", {},
        "/home/labuser/ros_ws/src/odhe_ros/scene_camera_dataset/train/annotations.json",
        "/home/labuser/ros_ws/src/odhe_ros/scene_camera_dataset/train")
    train_metadata = MetadataCatalog.get("train_set")
    dataset_dicts = DatasetCatalog.get("train_set")

    cfg = get_cfg()
    cfg.merge_from_file(
        model_zoo.get_config_file(
            "COCO-Detection/faster_rcnn_R_50_FPN_3x.yaml"))
    cfg.DATASETS.TRAIN = ("train_set")
    cfg.DATASETS.TEST = ()  # no metrics implemented for this dataset
    cfg.DATALOADER.NUM_WORKERS = 2
    cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url(
        "COCO-Detection/faster_rcnn_R_50_FPN_3x.yaml"
    )  # initialize from model zoo
    cfg.SOLVER.IMS_PER_BATCH = 2
    cfg.SOLVER.BASE_LR = 0.02
    cfg.SOLVER.MAX_ITER = 1000  # 300 iterations seems good enough, but you can certainly train longer
    cfg.MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE = (
        128)  # faster, and good enough for this toy dataset
    cfg.MODEL.ROI_HEADS.NUM_CLASSES = 5  # 5 classes (plate, cup, bowl, fork, spoon)

    # Temporary Solution. If I train again I think I can use the dynamically set path again
    cfg.MODEL.WEIGHTS = os.path.join(
        cfg.OUTPUT_DIR,
        "/home/labuser/ros_ws/src/odhe_ros/scene_camera_dataset/output/model_final.pth"
    )
    # cfg.MODEL.WEIGHTS = os.path.join(cfg.OUTPUT_DIR, "model_final.pth")
    cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.9  # set the testing threshold for this model
    cfg.DATASETS.TEST = ("train_set", )
    predictor = DefaultPredictor(cfg)

    class_names = MetadataCatalog.get("train_set").thing_classes

    # Set up custom cv2 visualization parameters
    # Classes: [name, id]
    #               -
    #          [fork,  0]
    #          [spoon, 1]
    #          [plate, 2]
    #          [bowl,  3]
    #          [cup,   4]

    # Colors = [blue, green, red]
    color_fork = [255, 200, 0]  # blue
    color_spoon = [0, 255, 0]  # green
    color_plate = [0, 0, 255]  # red
    color_bowl = [204, 0, 150]  # purple
    color_cup = [0, 220, 255]  # yellow
    colors = list(
        [color_fork, color_spoon, color_plate, color_bowl, color_cup])

    run = fasterRCNN()
    while not rospy.is_shutdown():
        # Get images
        img = run.get_img()

        if img is not None:
            outputs = predictor(img)
            predictions = outputs["instances"].to("cpu")

            # Get results
            result = run.getResult(predictions, class_names)

            # # Visualize using detectron2 built in visualizer
            # v = Visualizer(img[:, :, ::-1],
            #             metadata=train_metadata,
            #             scale=1.0
            #             # instance_mode=ColorMode.IMAGE_BW   # remove the colors of unsegmented pixels
            # )
            # v = v.draw_instance_predictions(outputs["instances"].to("cpu"))
            # img = v.get_image()[:, :, ::-1]
            # im_msg = bridge.cv2_to_imgmsg(img, encoding="bgr8")

            # Visualize using custom cv2 code
            if result is not None:
                result_cls = result.class_names
                result_clsId = result.class_ids
                result_scores = result.scores

                for i in range(len(result_clsId)):
                    start_point = (result.boxes[i].x_offset,
                                   result.boxes[i].y_offset)
                    end_point = (result.boxes[i].x_offset +
                                 result.boxes[i].width,
                                 result.boxes[i].y_offset +
                                 result.boxes[i].height)
                    start_point2 = (result.boxes[i].x_offset + 2,
                                    result.boxes[i].y_offset + 2)
                    end_point2 = (result.boxes[i].x_offset +
                                  result.boxes[i].width - 2,
                                  result.boxes[i].y_offset + 12)
                    color = colors[result_clsId[i]]
                    box_thickness = 2

                    name = result_cls[i]
                    score = result_scores[i]
                    conf = round(score.item() * 100, 1)
                    string = str(name) + ":" + str(conf) + "%"
                    font = cv2.FONT_HERSHEY_SIMPLEX
                    org = (result.boxes[i].x_offset + 2,
                           result.boxes[i].y_offset + 10)
                    fontScale = .3
                    text_thickness = 1
                    img = cv2.rectangle(img, start_point, end_point, color,
                                        box_thickness)
                    img = cv2.rectangle(img, start_point2, end_point2, color,
                                        -1)  # White text box
                    img = cv2.putText(img, string, org, font, fontScale,
                                      [0, 0, 0], text_thickness, cv2.LINE_AA,
                                      False)

                im_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
                im_msg = bridge.cv2_to_imgmsg(im_rgb, encoding="rgb8")

            # Display Image Counter
            image_counter = image_counter + 1
            if (image_counter % 11) == 10:
                rospy.loginfo(
                    "Images detected per second=%.2f",
                    float(image_counter) / (time.time() - start_time))

            run.publish(im_msg, result)

    return 0
Пример #23
0
              "vehicle.emergency.ambulance",
              "vehicle.emergency.police",
              "vehicle.motorcycle",
              "vehicle.trailer",
              "vehicle.truck"]
categories = ['human.pedestrian',
              'vehicle.car',
              'vehicle.bus',
              'vehicle.truck',
              'vehicle.cycle',
              'vehicle.cycle.with_rider']
dataset = 'nuimages_mini'
version = 'v1.0-mini'
get_dicts = lambda p=root_path, c=categories: load_nuimages_dicts(path=p, version=version, categories=c)
DatasetCatalog.register(dataset, get_dicts)
MetadataCatalog.get(dataset).thing_classes = categories
MetadataCatalog.get(dataset).evaluator_type = "coco"

dataset_dicts = load_nuimages_dicts(root_path, version, categories)

def transform_instance_to_dict(instances):
    scores = instances.scores.numpy()
    pred_classes = instances.pred_classes.numpy()
    pred_boxes = instances.pred_boxes.tensor.numpy()
    pred_masks = instances.pred_masks.numpy()
    pred_masks_encode = []
    for objid in range(len(scores)):
        mask_encode = cocomask.encode(np.asfortranarray(pred_masks[objid]))
        pred_masks_encode.append(mask_encode)
    #pred_classes = convert_anno_to_nuimages(pred_classes)
    # dict= {
Пример #24
0
val_inds = np.random.choice(N, N // 3)
train_inds = np.setdiff1d(np.arange(N), val_inds)
train_val_split = {
    'train': [csv_files[i] for i in train_inds],
    'val': [csv_files[i] for i in val_inds]
}

m2cai_data_dir = '/home/mona/share/data/m2cai16-tool-locations'

for d in ["val"]:
    dataset_name = "tool_" + d
    DatasetCatalog.register(
        dataset_name,
        lambda d=d: create_dataset(surgical_tool_path, train_val_split[d],
                                   m2cai_data_dir, f'{d}.txt'))
    MetadataCatalog.get(dataset_name).set(thing_classes=["tool"])

cfg = get_cfg()
# add project-specific config (e.g., TensorMask) here if you're not running a model in detectron2's core library
cfg.merge_from_file(
    model_zoo.get_config_file("COCO-Detection/faster_rcnn_R_50_C4_3x.yaml"))
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5  # set threshold for this model
# Find a model from detectron2's model zoo. You can use the https://dl.fbaipublicfiles... url as well
# cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url("COCO-Detection/faster_rcnn_R_50_C4_3x.yaml")
cfg.DATASETS.TEST = ('tool_val', )
cfg.DATALOADER.NUM_WORKERS = 2
# cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml")  # Let training initialize from model zoo
cfg.SOLVER.IMS_PER_BATCH = 2
cfg.SOLVER.MAX_ITER = 50  # 300 iterations seems good enough for this toy dataset; you will need to train longer for a practical dataset
cfg.SOLVER.STEPS = []  # do not decay learning rate
cfg.MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE = 128  # faster, and good enough for this toy dataset (default: 512)
Пример #25
0
def process_adnet_test(videos_infos,dataset_start_id, v_start_id,v_end_id,train_videos,save_root,
                        spend_times_share,vid_preds, opts,args, lock):
    siamesenet=''
    if args.useSiamese:
        siamesenet = SiameseNetwork().cuda()
        resume = args.weight_siamese
        # resume = False
        if resume:
            siamesenet.load_weights(resume)

    # print('Loading {}...'.format(args.weight_file))
            
    net, domain_nets = adnet(opts, trained_file=args.weight_file, random_initialize_domain_specific=False)
    net.eval()
    if args.cuda:
        net = nn.DataParallel(net)
        cudnn.benchmark = True
    if args.cuda:
        net = net.cuda()
    if args.cuda:
        net.module.set_phase('test')
    else:
        net.set_phase('test')

    register_ILSVRC()
    cfg = get_cfg()
    cfg.merge_from_file("../../../configs/COCO-Detection/faster_rcnn_R_101_FPN_3x.yaml")
    cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5  # set threshold for this model
    # Find a model from detectron2's model zoo. You can either use the https://dl.fbaipublicfiles.... url, or use the following shorthand
    # cfg.MODEL.WEIGHTS ="../datasets/tem/train_output/model_0449999.pth"
    cfg.MODEL.WEIGHTS = args.weight_detector
    cfg.MODEL.ROI_HEADS.NUM_CLASSES = 30
    metalog = MetadataCatalog.get("ILSVRC_VID_val")

    predictor = DefaultPredictor(cfg)
    class_names = metalog.get("thing_classes", None)
    for vidx in range(v_start_id, v_end_id):
        # for vidx in range(20):
        vid_folder = videos_infos[vidx]

        if args.save_result_images_bool:
            args.save_result_images = os.path.join(save_root, train_videos['video_names'][vidx])
            if not os.path.exists(args.save_result_images):
                os.makedirs(args.save_result_images)

        vid_pred,spend_time = adnet_test(net,predictor,siamesenet,metalog,class_names, vidx,vid_folder['img_files'], opts, args)
        try:
            lock.acquire()
            spend_times=spend_times_share[0].copy()
            spend_times['predict']+=spend_time['predict']
            spend_times['n_predict_frames'] += spend_time['n_predict_frames']
            spend_times['track'] += spend_time['track']
            spend_times['n_track_frames'] += spend_time['n_track_frames']
            spend_times['readframe'] += spend_time['readframe']
            spend_times['n_readframe'] += spend_time['n_readframe']
            spend_times['append'] += spend_time['append']
            spend_times['n_append'] += spend_time['n_append']
            spend_times['transform'] += spend_time['transform']
            spend_times['n_transform'] += spend_time['n_transform']
            spend_times['argmax_after_forward'] += spend_time['argmax_after_forward']
            spend_times['n_argmax_after_forward'] += spend_time['n_argmax_after_forward']
            spend_times['do_action'] += spend_time['do_action']
            spend_times['n_do_action'] += spend_time['n_do_action']
            spend_times_share[0]=spend_times
            vid_preds[vidx-dataset_start_id]=vid_pred
        except Exception as err:
            raise err
        finally:
            lock.release()
Пример #26
0
    
    path_1 = '../../../Datasets/FLIR/' + data_set + '/resized_RGB/'
    path_2 = '../../../Datasets/FLIR/' + data_set + '/thermal_8_bit/'
    out_folder = 'out/box_comparison/'
    #train_json_path = '../../../Datasets/'+dataset+'/train/thermal_annotations_4_channel_no_dogs.json'
    
    val_json_path = '../../../Datasets/'+dataset+'/val/' + val_file_name
    val_folder = '../../../Datasets/FLIR/val/thermal_8_bit'

    if not os.path.exists(out_folder):
        os.mkdir(out_folder)

    # Register dataset
    dataset = 'FLIR_val'
    register_coco_instances(dataset, {}, val_json_path, val_folder)
    FLIR_metadata = MetadataCatalog.get(dataset)
    dataset_dicts = DatasetCatalog.get(dataset)

    # Create config
    cfg = get_cfg()
    cfg.DATALOADER.NUM_WORKERS = 2
    cfg.OUTPUT_DIR = out_folder
    cfg.merge_from_file("./configs/COCO-Detection/faster_rcnn_R_101_FPN_3x.yaml")
    cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5  # set threshold for this model
    cfg.MODEL.WEIGHTS = "detectron2://COCO-Detection/faster_rcnn_R_101_FPN_3x/137851257/model_final_f6e8b1.pkl"
    #cfg.MODEL.WEIGHTS = os.path.join(cfg.OUTPUT_DIR, "good_model/out_model_iter_32000.pth")
    cfg.MODEL.ROI_HEADS.NUM_CLASSES = 80
    cfg.DATASETS.TEST = (dataset, )
    cfg.INPUT.FORMAT = 'BGR'
    cfg.INPUT.NUM_IN_CHANNELS = 3
    cfg.MODEL.PIXEL_MEAN = [103.530, 116.280, 123.675]
Пример #27
0
def getModelMetadataSegNames():
    metadata = MetadataCatalog.get("coco_2017_val_panoptic_separated")
    names = metadata.get("stuff_classes", None)
    return names
Пример #28
0
                "category_id": MappingLabels(b[9]),
                "iscrowd": 0
            }
            objs.append(obj)

        record["annotations"] = objs
        dataset_dicts.append(record)
    return dataset_dicts


img_width = 1914
img_height = 1052
for d in ["trainval", "test"]:
    DatasetCatalog.register(
        d, lambda d=d: DataConvert("../" + d, img_height, img_width))
    MetadataCatalog.get(d).set(
        thing_classes=["unkown", "car", "truck", "person"])
vehicle_metadata = MetadataCatalog.get("trainval")

dataset_dicts = DataConvert("../trainval", img_height, img_width)
print(len(dataset_dicts))

count = 0
for d in random.sample(dataset_dicts, 3):
    img = cv2.imread(d["file_name"])
    height, width, channels = img.shape
    print(height, width)
    visualizer = Visualizer(img[:, :, ::-1],
                            metadata=vehicle_metadata,
                            scale=0.5)
    vis = visualizer.draw_dataset_dict(d)
    # print(d)
Пример #29
0
import matplotlib.pyplot as plt

im = cv2.imread("./input.jpg")
plt.imshow(im)
cfg = get_cfg()
# add project-specific config (e.g., TensorMask) here if you're not running a model in detectron2's core library
cfg.merge_from_file(
    "./detectron2/detectron2/model_zoo/configs/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml"
)

cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5
cfg.MODEL.WEIGHTS = "detectron2://COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x/137849600/model_final_f10217.pkl"
predictor = DefaultPredictor(cfg)
outputs = predictor(im)
print(outputs["instances"].pred_classes)
print(outputs["instances"].pred_boxes)
v = Visualizer(im[:, :, ::-1], MetadataCatalog.get(cfg.DATASETS.TRAIN[0]), scale=1.2)
v = v.draw_instance_predictions(outputs["instances"].to("cpu"))
plt.imshow(v.get_image()[:, :, ::-1])
# save image
plt.savefig("output.jpg")

# DATA SHOULD BE SUPER EASY TO ADD
#from detectron2.data.datasets import register_coco_instances
#register_coco_instances("my_dataset", {}, "json_annotation.json", "path/to/image/dir")
#https://detectron2.readthedocs.io/tutorials/datasets.html#register-a-dataset

# test in CMD 
# cd detectron2
# python demo/demo.py --config-file configs/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml --webcam --opts MODEL.WEIGHTS detectron2://COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x/137849600/model_final_f10217.pkl
Пример #30
0
    # 	{},
    # 	args.train_gt,
    # 	args.train_dir
    # )

    # Register coco valid
    # register_coco_instances(
    # 	f'{args.dataset_name}_val',
    # 	{},
    # 	args.val_gt,
    # 	args.val_dir
    # )

    # Test train and valid annotations
    if DEBUG:
        coco_train_metadata = MetadataCatalog.get('coco_2017_train')
        coco_train_dicts = DatasetCatalog.get('coco_2017_train')
        random_meta_check(coco_train_dicts, coco_train_metadata, 'Train')
        coco_val_metadata = MetadataCatalog.get('coco_2017_val')
        coco_val_dicts = DatasetCatalog.get('coco_2017_val')
        random_meta_check(coco_val_dicts, coco_val_metadata, 'Valid')

    # Launches main
    launch(
        main,
        args.num_gpus,
        num_machines=args.num_machines,
        machine_rank=args.machine_rank,
        dist_url=args.dist_url,
        args=(args, ),
    )