Esempio n. 1
0
def do_test(cfg, args, myargs):

    eval_ckpt_dir = cfg.start.eval_ckpt_dir
    eval_epoch = cfg.start.eval_epoch
    dataset_name = cfg.start.dataset_name
    IMS_PER_BATCH = cfg.start.IMS_PER_BATCH

    cfg.defrost()

    cfg.freeze()

    # build dataset
    DatasetCatalog.get(dataset_name)
    metadata = MetadataCatalog.get(dataset_name)
    num_images = metadata.get('num_images')
    iter_every_epoch = num_images // IMS_PER_BATCH

    model = build_trainer(cfg,
                          myargs=myargs,
                          iter_every_epoch=1,
                          img_size=cfg.dataset.img_size,
                          train_bs=32)

    logger.info("Model:\n{}".format(model))

    eval_iter = (eval_epoch) * iter_every_epoch - 1
    eval_ckpt = os.path.join(eval_ckpt_dir, f'model_{eval_iter:07}.pth')
    model.eval_func(eval_ckpt=eval_ckpt)
Esempio n. 2
0
def get_classification_dataset_dicts(dataset_names):
    """
    Load and prepare dataset dicts for instance detection/segmentation and semantic segmentation.

    Args:
        dataset_names (list[str]): a list of dataset names
    """
    assert len(dataset_names)
    dataset_dicts = []
    for dataset_name in dataset_names:
        assert dataset_name in _GRUUL_DATASETS, f"Unknown train dataset {dataset_name}"

    # dataset = ImageFolder(_GRUUL_DATASETS[cfg.DATASETS.TRAIN[0]])

    dataset_dicts = [
        DatasetCatalog.get(dataset_name) for dataset_name in dataset_names
    ]
    for dataset_name, dicts in zip(dataset_names, dataset_dicts):
        assert len(dicts), "Dataset '{}' is empty!".format(dataset_name)

    dataset_dicts = list(itertools.chain.from_iterable(dataset_dicts))

    try:
        class_names = MetadataCatalog.get(dataset_names[0]).classes
        print_instances_class_histogram(dataset_dicts, class_names)
    except AttributeError:  # class names are not available for this dataset
        pass
    return dataset_dicts
Esempio n. 3
0
def setup_dataset(dataset_name):
    logger.info("Loading dataset {}".format(dataset_name))
    start = timer()
    dataset = DatasetCatalog.get(dataset_name)
    stop = timer()
    logger.info("Loaded dataset {} in {:.3f}s".format(dataset_name, stop - start))
    return dataset
Esempio n. 4
0
    def visualize_samples(self, number):
        from detectron2.utils.visualizer import Visualizer
        from detectron2.data import MetadataCatalog
        from detectron2.data.catalog import DatasetCatalog
        from detectron2.data.datasets import register_coco_instances

        register_coco_instances(
            'sugar_beet_{}'.format(self.split), {},
            join(self.root, 'instances_{}2016.json'.format(self.split)),
            join(self.root, self.split, 'img'))
        # register_coco_instances('sugar_beet_{}'.format(self.split), {},
        #                         join(self.root, 'instances_{}2016.json'.format(self.split)),
        #                         join(self.root, self.split, 'img'))
        # register_coco_instances("sugar_beet_test", {},
        #                         "/home/robot/datasets/structured_cwc/instances_test2016.json",
        #                         "/home/robot/datasets/structured_cwc/test/img/")

        # visualize training data
        my_dataset_train_metadata = MetadataCatalog.get('sugar_beet_{}'.format(
            self.split))
        dataset_dicts = DatasetCatalog.get('sugar_beet_{}'.format(self.split))

        for d in random.sample(dataset_dicts, number):
            img = cv2.imread(d["file_name"])
            visualizer = Visualizer(img[:, :, ::-1],
                                    metadata=my_dataset_train_metadata,
                                    scale=0.5)
            vis = visualizer.draw_dataset_dict(d)
            # cv2.imshow(vis.get_image()[:, :, ::-1])
            cv2.imshow('image', vis.get_image())
            cv2.waitKey()
Esempio n. 5
0
def get_detection_dataset_dicts_support(dataset_name,
                                        filter_empty=True,
                                        min_keypoints=0,
                                        proposal_file=None):

    dataset_dict = DatasetCatalog.get(dataset_name)
    dataset_dict_flattened = []
    for id_class, annotations_class in dataset_dict.items():
        dataset_dict_flattened.extend(annotations_class)

    # pre-extracted proposal: need to think about this later
    """
    if proposal_files is not None:
        assert len(dataset_names) == len(proposal_files)
        # load precomputed proposals from proposal files
        dataset_dicts = [
            load_proposals_into_dataset(dataset_i_dicts, proposal_file)
            for dataset_i_dicts, proposal_file in zip(dataset_dicts, proposal_files)
        ]
    """
    # data distribution
    class_names = MetadataCatalog.get(dataset_name).thing_classes
    check_metadata_consistency("thing_classes", [dataset_name])
    print_instances_class_histogram(dataset_dict_flattened, class_names)

    return dataset_dict
Esempio n. 6
0
def get_hoi_dataset_dicts(dataset_names, filter_empty=True):
    """
    Load and prepare dataset dicts for HOI detection.

    Args:
        dataset_names (list[str]): a list of dataset names
        filter_empty (bool): whether to filter out images without instance annotations
        min_keypoints (int): filter out images with fewer keypoints than
            `min_keypoints`. Set to 0 to do nothing.
        proposal_files (list[str]): if given, a list of object proposal files
            that match each dataset in `dataset_names`.
    """
    assert len(dataset_names)
    dataset_dicts = [DatasetCatalog.get(dataset_name) for dataset_name in dataset_names]
    for dataset_name, dicts in zip(dataset_names, dataset_dicts):
        assert len(dicts), "Dataset '{}' is empty!".format(dataset_name)

    dataset_dicts = list(itertools.chain.from_iterable(dataset_dicts))

    has_instances = "annotations" in dataset_dicts[0]

    if filter_empty and has_instances:
        dataset_dicts = filter_images_with_only_crowd_annotations(dataset_dicts)

    if filter_empty and has_instances and "actions" in dataset_dicts[0]["annotations"][0]:
        dataset_dicts = filter_images_without_any_hois(dataset_dicts)

    if has_instances:
        try:
            class_names = MetadataCatalog.get(dataset_names[0]).thing_classes
            check_metadata_consistency("thing_classes", dataset_names)
            print_instances_class_histogram(dataset_dicts, class_names)
        except AttributeError:  # class names are not available for this dataset
            pass
    return dataset_dicts
Esempio n. 7
0
def convert_ss_box():
    dataset_name = sys.argv[1]
    file_in = sys.argv[2]
    file_out = sys.argv[3]

    dataset_dicts = DatasetCatalog.get(dataset_name)
    raw_data = sio.loadmat(file_in)["boxes"].ravel()
    assert raw_data.shape[0] == len(dataset_dicts)

    boxes = []
    scores = []
    ids = []
    for i in range(len(dataset_dicts)):
        if i % 1000 == 0:
            print("{}/{}".format(i + 1, len(dataset_dicts)))

        if "flickr" in dataset_name:
            index = os.path.basename(dataset_dicts[i]["file_name"])[:-4]
        elif "coco" in dataset_name:
            index = os.path.basename(dataset_dicts[i]["file_name"])[:-4]
        else:
            index = dataset_dicts[i]["image_id"]
        # selective search boxes are 1-indexed and (y1, x1, y2, x2)
        i_boxes = raw_data[i][:, (1, 0, 3, 2)] - 1
        # i_scores = np.zeros((i_boxes.shape[0]), dtype=np.float32)
        i_scores = np.ones((i_boxes.shape[0]), dtype=np.float32)

        boxes.append(i_boxes.astype(np.int16))
        scores.append(np.squeeze(i_scores.astype(np.float32)))
        index = dataset_dicts[i]["image_id"]
        ids.append(index)

    with open(file_out, "wb") as f:
        pickle.dump(dict(boxes=boxes, scores=scores, indexes=ids), f,
                    pickle.HIGHEST_PROTOCOL)
Esempio n. 8
0
def combine_detection_dataset_dicts(
    dataset_names: Collection[str],
    keep_instance_predicate: Optional[InstancePredicate] = None,
    proposal_files: Optional[Collection[str]] = None,
) -> List[Instance]:
    """
    Load and prepare dataset dicts for training / testing

    Args:
        dataset_names (Collection[str]): a list of dataset names
        keep_instance_predicate (Callable: Dict[str, Any] -> bool): predicate
            applied to instance dicts which defines whether to keep the instance
        proposal_files (Collection[str]): if given, a list of object proposal files
            that match each dataset in `dataset_names`.
    """
    assert len(dataset_names)
    if proposal_files is None:
        proposal_files = [None] * len(dataset_names)
    assert len(dataset_names) == len(proposal_files)
    # load datasets and metadata
    dataset_name_to_dicts = {}
    for dataset_name in dataset_names:
        dataset_name_to_dicts[dataset_name] = DatasetCatalog.get(dataset_name)
        assert len(
            dataset_name_to_dicts), f"Dataset '{dataset_name}' is empty!"
    # merge categories, requires category metadata to be loaded
    # cat_id -> [(orig_cat_id, cat_name, dataset_name)]
    merged_categories = _merge_categories(dataset_names)
    _warn_if_merged_different_categories(merged_categories)
    merged_category_names = [
        merged_categories[cat_id][0].mapped_name
        for cat_id in sorted(merged_categories)
    ]
    # map to contiguous category IDs
    _add_category_id_to_contiguous_id_maps_to_metadata(merged_categories)
    # load annotations and dataset metadata
    for dataset_name, proposal_file in zip(dataset_names, proposal_files):
        dataset_dicts = dataset_name_to_dicts[dataset_name]
        assert len(dataset_dicts), f"Dataset '{dataset_name}' is empty!"
        if proposal_file is not None:
            dataset_dicts = load_proposals_into_dataset(
                dataset_dicts, proposal_file)
        dataset_dicts = _maybe_filter_and_map_categories(
            dataset_name, dataset_dicts)
        print_instances_class_histogram(dataset_dicts, merged_category_names)
        dataset_name_to_dicts[dataset_name] = dataset_dicts

    if keep_instance_predicate is not None:
        all_datasets_dicts_plain = [
            d for d in itertools.chain.from_iterable(
                dataset_name_to_dicts.values()) if keep_instance_predicate(d)
        ]
    else:
        all_datasets_dicts_plain = list(
            itertools.chain.from_iterable(dataset_name_to_dicts.values()))
    return all_datasets_dicts_plain
def get_detection_dataset_dicts(dataset_names,
                                filter_empty=True,
                                min_keypoints=0,
                                proposal_files=None):
    """
    Load and prepare dataset dicts for instance detection/segmentation and semantic segmentation.

    Args:
        dataset_names (list[str]): a list of dataset names
        filter_empty (bool): whether to filter out images without instance annotations
        min_keypoints (int): filter out images with fewer keypoints than
            `min_keypoints`. Set to 0 to do nothing.
        proposal_files (list[str]): if given, a list of object proposal files
            that match each dataset in `dataset_names`.
    """
    assert len(dataset_names)
    print(dataset_names)
    dataset_dicts = [
        DatasetCatalog.get(dataset_name) for dataset_name in dataset_names
    ]
    for dataset_name, dicts in zip(dataset_names, dataset_dicts):
        assert len(dicts), "Dataset '{}' is empty!".format(dataset_name)

    if proposal_files is not None:
        assert len(dataset_names) == len(proposal_files)
        # load precomputed proposals from proposal files
        dataset_dicts = [
            load_proposals_into_dataset(dataset_i_dicts, proposal_file)
            for dataset_i_dicts, proposal_file in zip(dataset_dicts,
                                                      proposal_files)
        ]

    dataset_dicts = list(itertools.chain.from_iterable(dataset_dicts))

    has_instances = "annotations" in dataset_dicts[0]
    # Keep images without instance-level GT if the dataset has semantic labels.
    if filter_empty and has_instances and "sem_seg_file_name" not in dataset_dicts[
            0]:
        dataset_dicts = filter_images_with_only_crowd_annotations(
            dataset_dicts)

    if min_keypoints > 0 and has_instances:
        dataset_dicts = filter_images_with_few_keypoints(
            dataset_dicts, min_keypoints)

    if has_instances:
        try:
            class_names = MetadataCatalog.get(dataset_names[0]).thing_classes
            check_metadata_consistency("thing_classes", dataset_names)
            print_instances_class_histogram(dataset_dicts, class_names)
            plot_instances_class_histogram(dataset_dicts, class_names,
                                           dataset_name)
        except AttributeError:  # class names are not available for this dataset
            pass
    return dataset_dicts
Esempio n. 10
0
def combine_detection_dataset_dicts(
    dataset_names: Collection[str],
    keep_instance_predicate: Optional[InstancePredicate] = None,
    proposal_files: Optional[Collection[str]] = None,
) -> List[Instance]:
    """
    Load and prepare dataset dicts for training / testing

    Args:
        dataset_names (Collection[str]): a list of dataset names
        keep_instance_predicate (Callable: Dict[str, Any] -> bool): predicate
            applied to instance dicts which defines whether to keep the instance
        proposal_files (Collection[str]): if given, a list of object proposal files
            that match each dataset in `dataset_names`.
    """
    assert len(dataset_names)
    if proposal_files is None:
        proposal_files = [None] * len(dataset_names)
    assert len(dataset_names) == len(proposal_files)
    # load annotations and dataset metadata
    dataset_map = {}
    for dataset_name in dataset_names:
        dataset_dicts = DatasetCatalog.get(dataset_name)
        dataset_map[dataset_name] = dataset_dicts
    # initialize category maps
    _add_category_id_to_contiguous_id_maps_to_metadata(dataset_names)
    # apply category maps
    all_datasets_dicts = []
    for dataset_name, proposal_file in zip(dataset_names, proposal_files):
        dataset_dicts = dataset_map[dataset_name]
        assert len(dataset_dicts), f"Dataset '{dataset_name}' is empty!"
        if proposal_file is not None:
            dataset_dicts = load_proposals_into_dataset(
                dataset_dicts, proposal_file)
        dataset_dicts = _maybe_filter_and_map_categories(
            dataset_name, dataset_dicts)
        _map_category_id_to_contiguous_id(dataset_name, dataset_dicts)
        print_instances_class_histogram(
            dataset_dicts,
            MetadataCatalog.get(dataset_name).thing_classes)
        all_datasets_dicts.append(dataset_dicts)

    if keep_instance_predicate is not None:
        all_datasets_dicts_plain = [
            d for d in itertools.chain.from_iterable(all_datasets_dicts)
            if keep_instance_predicate(d)
        ]
    else:
        all_datasets_dicts_plain = list(
            itertools.chain.from_iterable(all_datasets_dicts))
    return all_datasets_dicts_plain
Esempio n. 11
0
def get_classification_dataset_dicts(dataset_names):
    """
    Load and prepare dataset dicts for instance detection/segmentation and semantic segmentation.

    Args:
        dataset_names (list[str]): a list of dataset names
    """
    assert len(dataset_names)
    dataset_dicts = [DatasetCatalog.get(dataset_name)
                     for dataset_name in dataset_names]
    for dataset_name, dicts in zip(dataset_names, dataset_dicts):
        assert len(dicts), "Dataset '{}' is empty!".format(dataset_name)

    dataset_dicts = list(itertools.chain.from_iterable(dataset_dicts))

    return dataset_dicts
Esempio n. 12
0
def convert_mcg_seg():
    dataset_name = sys.argv[1]
    dir_in = sys.argv[2]
    dir_out = sys.argv[3]

    if not os.path.isdir(dir_out):
        os.makedirs(dir_out)

    dataset_dicts = DatasetCatalog.get(dataset_name)

    process_pool = Pool(processes=32)

    arg_process = []
    for i in range(len(dataset_dicts)):
        arg_process.append((dataset_dicts[i], dataset_name, dir_in, dir_out))

    results = process_pool.starmap_async(convert_mcg_seg_i, arg_process)
    results = results.get()
Esempio n. 13
0
def convert_mcg_box():
    dataset_name = sys.argv[1]
    dir_in = sys.argv[2]
    file_out = sys.argv[3]

    dataset_dicts = DatasetCatalog.get(dataset_name)

    boxes = []
    scores = []
    ids = []
    for i in range(len(dataset_dicts)):
        if i % 1000 == 0:
            print("{}/{}".format(i + 1, len(dataset_dicts)))

        if "flickr" in dataset_name:
            index = os.path.basename(dataset_dicts[i]["file_name"])[:-4]
        elif "coco" in dataset_name:
            index = os.path.basename(dataset_dicts[i]["file_name"])[:-4]
        else:
            index = dataset_dicts[i]["image_id"]
        box_file = os.path.join(dir_in, "{}.mat".format(index))
        mat_data = sio.loadmat(box_file)
        if i == 0:
            print(mat_data.keys())
        if "flickr" in dataset_name:
            boxes_data = mat_data["bboxes"]
            scores_data = mat_data["bboxes_scores"]
        else:
            boxes_data = mat_data["boxes"]
            scores_data = mat_data["scores"]
        # selective search boxes are 1-indexed and (y1, x1, y2, x2)
        # Boxes from the MCG website are in (y1, x1, y2, x2) order
        boxes_data = boxes_data[:, (1, 0, 3, 2)] - 1
        # boxes_data_ = boxes_data.astype(np.uint16) - 1
        # boxes_data = boxes_data_[:, (1, 0, 3, 2)]

        boxes.append(boxes_data.astype(np.int16))
        scores.append(np.squeeze(scores_data.astype(np.float32)))
        index = dataset_dicts[i]["image_id"]
        ids.append(index)

    with open(file_out, "wb") as f:
        pickle.dump(dict(boxes=boxes, scores=scores, indexes=ids), f,
                    pickle.HIGHEST_PROTOCOL)
Esempio n. 14
0
def get_detection_dataset_dicts_with_source(dataset_names,
                                            filter_empty=True,
                                            min_keypoints=0,
                                            proposal_files=None):
    assert len(dataset_names)
    dataset_dicts = [
        DatasetCatalog.get(dataset_name) for dataset_name in dataset_names
    ]
    for dataset_name, dicts in zip(dataset_names, dataset_dicts):
        assert len(dicts), "Dataset '{}' is empty!".format(dataset_name)

    for source_id, (dataset_name, dicts) in \
        enumerate(zip(dataset_names, dataset_dicts)):
        assert len(dicts), "Dataset '{}' is empty!".format(dataset_name)
        for d in dicts:
            d['dataset_source'] = source_id

        if "annotations" in dicts[0]:
            try:
                class_names = MetadataCatalog.get(dataset_name).thing_classes
                check_metadata_consistency("thing_classes", dataset_name)
                print_instances_class_histogram(dicts, class_names)
            except AttributeError:  # class names are not available for this dataset
                pass

    assert proposal_files is None

    dataset_dicts = list(itertools.chain.from_iterable(dataset_dicts))

    has_instances = "annotations" in dataset_dicts[0]
    if filter_empty and has_instances:
        dataset_dicts = filter_images_with_only_crowd_annotations(
            dataset_dicts)
    if min_keypoints > 0 and has_instances:
        dataset_dicts = filter_images_with_few_keypoints(
            dataset_dicts, min_keypoints)

    return dataset_dicts
Esempio n. 15
0
def register_dataset():

    if regist_train_name in DatasetCatalog._REGISTERED:
        log = 'regist_data exists before: %s , and try to del.... ' % regist_train_name
        print(log)
        print(log, file=open(log_file, "a"))
        DatasetCatalog._REGISTERED.pop(regist_train_name)
        DatasetCatalog._REGISTERED.pop(regist_val_name)

    else:
        log = 'regist_data : %s .... ' % regist_train_name
        print(log)
        print(log, file=open(log_file, "a"))
        register_coco_instances(regist_train_name, {}, train_json_path,
                                train_images_dir)
        register_coco_instances(regist_val_name, {}, val_json_path,
                                val_images_dir)

    train_metadata = MetadataCatalog.get(regist_train_name)
    val_metadata = MetadataCatalog.get(regist_val_name)
    trainset_dicts = DatasetCatalog.get(regist_train_name)

    return train_metadata, val_metadata
Esempio n. 16
0
    def __init__(self, cfg):
        super().__init__()

        # fmt: off
        # backbone - DLA
        head_conv = cfg.MODEL.CENTERNET.HEAD_CONV
        final_kernel = cfg.MODEL.CENTERNET.FINAL_KERNEL
        backbone_type = cfg.MODEL.BACKBONE.NAME
        # heads
        self.heads = cfg.MODEL.CENTERNET.TASK
        # loss
        self.hm_weight = cfg.MODEL.CENTERNET.HM_WEIGHT
        self.wh_weight = cfg.MODEL.CENTERNET.WH_WEIGHT
        self.off_weight = cfg.MODEL.CENTERNET.OFF_WEIGHT
        self.focal_loss_alpha = cfg.MODEL.CENTERNET.FOCAL_LOSS_ALPHA
        # inference
        self.score_threshold = cfg.MODEL.CENTERNET.SCORE_THRESH_TEST
        self.topk_candidates = cfg.MODEL.CENTERNET.TOPK_CANDIDATES_TEST
        self.max_detections_per_image = cfg.TEST.DETECTIONS_PER_IMAGE
        # fmt: on

        # other
        given_dataset = cfg.DATASETS.TRAIN[0]
        DatasetCatalog.get(given_dataset)
        self.meta = MetadataCatalog.get(given_dataset)
        self.num_classes = len(
            self.meta.thing_classes
        )  # modify num_classes by meta data of given dataset
        self.heads["HM"] = self.num_classes
        self.register_buffer("pixel_mean",
                             torch.Tensor(cfg.MODEL.PIXEL_MEAN).view(-1, 1, 1))
        self.register_buffer("pixel_std",
                             torch.Tensor(cfg.MODEL.PIXEL_STD).view(-1, 1, 1))

        # self modules
        self.backbone_type = backbone_type.split('_')[1]
        self.backbone = build_backbone(cfg)
        if self.backbone_type == 'resnet' or self.backbone_type == 'vovnet':
            self.backbone.down_ratio = 4
            self.size_divisibility = 16
            self.deconv_layers = self._make_deconv_layer(
                self.backbone._out_feature_channels['res4'] if \
                self.backbone_type == 'resnet' else \
                self.backbone._out_feature_channels['stage4'],
                2,
                [256, 256],
                [4, 4],
            )

            for head in sorted(self.heads):
                num_output = self.heads[head]
                if head_conv > 0:
                    fc = nn.Sequential(
                        nn.Conv2d(256,
                                  head_conv,
                                  kernel_size=3,
                                  padding=1,
                                  bias=True), nn.ReLU(inplace=True),
                        nn.Conv2d(head_conv,
                                  num_output,
                                  kernel_size=1,
                                  stride=1,
                                  padding=0))
                    if 'hm' in head.lower():
                        fc[-1].bias.data.fill_(-2.19)
                    else:
                        fill_fc_weights(fc)
                else:
                    fc = nn.Conv2d(in_channels=256,
                                   out_channels=num_output,
                                   kernel_size=1,
                                   stride=1,
                                   padding=0)
                    if 'hm' in head.lower():
                        fc[-1].bias.data.fill_(-2.19)
                    else:
                        fill_fc_weights(fc)
                self.__setattr__(head.lower(), fc)
            if self.backbone_type == 'resnet': self.init_weights(50)
            return

        self.size_divisibility = self.backbone.size_divisibility
        for head in self.heads:
            classes = self.heads[head]
            if head_conv > 0:
                fc = nn.Sequential(
                    nn.Conv2d(
                        self.backbone.channels[self.backbone.first_level],
                        head_conv,
                        kernel_size=3,
                        padding=1,
                        bias=True), nn.ReLU(inplace=True),
                    nn.Conv2d(head_conv,
                              classes,
                              kernel_size=final_kernel,
                              stride=1,
                              padding=final_kernel // 2,
                              bias=True))
                if 'hm' in head.lower():
                    fc[-1].bias.data.fill_(-2.19)
                else:
                    fill_fc_weights(fc)
            else:
                fc = nn.Conv2d(
                    self.backbone.channels[self.backbone.first_level],
                    classes,
                    kernel_size=final_kernel,
                    stride=1,
                    padding=final_kernel // 2,
                    bias=True)
                if 'hm' in head:
                    fc.bias.data.fill_(-2.19)
                else:
                    fill_fc_weights(fc)
            self.__setattr__(head.lower(), fc)
Esempio n. 17
0
def d2_train_model(regist_train_name, regist_val_name, train_json_path,
                   train_images_dir, val_json_path, val_images_dir,
                   ims_per_batch, model_lr, bach_size_per_img, max_train_iter,
                   num_workers, num_labels):
    ## 1. models:
    model_name = "mask_rcnn_R_50_FPN_3x.yaml"
    cfgFile = "COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml"

    # work_root = os.getcwd()
    work_root = "/home/user/qunosen/2_project/4_train/2_zhuchao/6_d2_final_train/2_train/1_train_2020_9_17/test6"

    log_file = os.path.join(work_root, "log_dat.txt")

    d2_start = time.time()
    datetime_now = datetime.datetime.now()
    log = ("###" * 100 + "\n") * 5 + " %s\n" % (
        str(datetime_now)) + "model_name: %s  ..." % model_name
    print(log)
    print(log, file=open(log_file, "a"))

    log = "parameter setting:\n model_to_try:%s\n num_labels: %d\n ims_per_batch:%d\n num_workers:%d\n model_lr:%s\n max_train_iter:%d\n bach_size_per_img:%d\n" % \
          (model_name, num_labels, ims_per_batch, num_workers, str(model_lr), max_train_iter, bach_size_per_img)

    print(log)
    print(log, file=open(log_file, "a"))

    new_root = os.path.join(
        work_root,
        str(model_name) + "_%s_%s_%s_%s" %
        (str(model_lr), str(bach_size_per_img), str(max_train_iter),
         str(ims_per_batch)))

    if not os.path.exists(new_root): os.makedirs(new_root)
    os.chdir(new_root)

    # register_coco_instances(regist_train_name, {}, train_json_path, train_images_dir)
    # register_coco_instances(regist_val_name, {}, val_json_path, val_images_dir)

    if regist_train_name in DatasetCatalog._REGISTERED:
        log = 'regist_data exists before: %s , and try to del.... ' % regist_train_name
        print(log)
        print(log, file=open(log_file, "a"))
        DatasetCatalog._REGISTERED.pop(regist_train_name)
        DatasetCatalog._REGISTERED.pop(regist_val_name)

    else:
        log = 'regist_data : %s .... ' % regist_train_name
        print(log)
        print(log, file=open(log_file, "a"))
        register_coco_instances(regist_train_name, {}, train_json_path,
                                train_images_dir)
        register_coco_instances(regist_val_name, {}, val_json_path,
                                val_images_dir)

    train_metadata = MetadataCatalog.get(regist_train_name)
    val_metadata = MetadataCatalog.get(regist_val_name)
    trainset_dicts = DatasetCatalog.get(regist_train_name)

    ################
    # #### trainning:
    cfg = get_cfg()
    mode_config = cfgFile
    log = "model_to_train: %s ..." % mode_config
    print(log)
    print(log, file=open(log_file, "a"))

    cfg.merge_from_file(model_zoo.get_config_file(mode_config))

    cfg.DATASETS.TRAIN = (regist_train_name, )
    cfg.DATASETS.TEST = (regist_val_name,
                         )  # no metrics implemented for this dataset
    cfg.DATALOADER.NUM_WORKERS = num_workers
    cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url(
        mode_config)  ## out_model : ./output/model_final.pth
    # cfg.MODEL.WEIGHTS = '/home/user/tmp/pycharm_project_310/1_detectron2/ImageDetectionAPI/d2_object_detection/pre_trained_model/model_final_a54504.pkl'

    cfg.SOLVER.IMS_PER_BATCH = ims_per_batch
    cfg.SOLVER.BASE_LR = model_lr

    cfg.SOLVER.MAX_ITER = (
        max_train_iter
    )  # 300 iterations seems good enough, but you can certainly train longer
    cfg.MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE = (
        bach_size_per_img)  # faster, and good enough for this toy dataset
    cfg.MODEL.ROI_HEADS.NUM_CLASSES = num_labels  # len(select_cats)  # 5 classes ['chair', 'table', 'swivelchair', 'sofa', 'bed']
    os.makedirs(cfg.OUTPUT_DIR, exist_ok=True)
    trainer = DefaultTrainer(cfg)
    trainer.resume_or_load(resume=False)
    trainer.train()

    model_path = os.path.join(new_root, 'output/model_final.pth')

    if os.path.exists(model_path):
        log = "model_save: %s" % model_path
        print(log)
        print(log, file=open(log_file, "a"))

        #### predict
        cfg.MODEL.WEIGHTS = os.path.join(cfg.OUTPUT_DIR, "model_final.pth")
        cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.2  # set the testing threshold for this model
        cfg.DATASETS.TEST = (regist_val_name, )
        predictor = DefaultPredictor(cfg)

        out_model_dir = os.path.join(new_root, "output")
        out_dir = os.path.join(out_model_dir, 'result_' + str(model_name))
        if not os.path.exists(out_dir): os.makedirs(out_dir)

        test_dir = val_images_dir  # os.path.join(work_dir,"./val_images")
        # test_dir = "/home/user/qunosen/2_project/4_train/2_zhuchao/6_d2_final_train/1_data/1_data_2020_9_17/new_set_6/val"
        imgs_list = [
            os.path.join(test_dir, file_name)
            for file_name in os.listdir(test_dir)
            if file_name.endswith(".jpg") or file_name.endswith(".png")
            or file_name.endswith(".bmp") or file_name.endswith(".jpeg")
        ]

        for d in imgs_list:
            im = cv2.imread(d)
            outputs = predictor(im)
            v = Visualizer(im[:, :, ::-1],
                           metadata=train_metadata,
                           scale=0.9,
                           instance_mode=ColorMode.IMAGE_BW)
            v = v.draw_instance_predictions(outputs["instances"].to("cpu"))
            predict_file = os.path.join(
                out_dir,
                os.path.splitext(os.path.basename(d))[0] + "_predict.png")

            cv2.imwrite(predict_file, v.get_image()[:, :, ::-1])

            if os.path.exists(predict_file):
                print("Done: %s" % predict_file)

        #### evaluate
        evaluator = COCOEvaluator(regist_val_name,
                                  cfg,
                                  False,
                                  output_dir="./output/")
        val_loader = build_detection_test_loader(cfg, regist_val_name)
        my_eval = inference_on_dataset(trainer.model, val_loader, evaluator)
        print(my_eval)
        log = ("%s evaluate: \n" % (model_name), my_eval)
        print(log, file=open(log_file, "a"))

        ###############
        DatasetCatalog._REGISTERED.pop(regist_train_name)
        DatasetCatalog._REGISTERED.pop(regist_val_name)

        log = "clean regist_data: %s and %s" % (regist_train_name,
                                                regist_val_name)
        print(log)
        print(log, file=open(log_file, "a"))

        d2_end = time.clock()
        log = "model %s : it takes %s ." % (model_name, str(d2_end - d2_start))
        print(log)
        print(log, file=open(log_file, "a"))

        os.chdir(work_root)

    else:
        print("NotFound: {}".format(model_path))
Esempio n. 18
0
def get_detection_dataset_dicts(cfg,
                                dataset_names,
                                frames_per_group,
                                train=True,
                                proposal_files=None,
                                long_term=False):
    """
    Load and prepare dataset dicts for instance detection/segmentation and semantic segmentation.
    Args:
        dataset_names (list[str]): a list of dataset names
        filter_empty (bool): whether to filter out images without instance annotations
        proposal_files (list[str]): if given, a list of object proposal files
            that match each dataset in `dataset_names`.
    """
    def chunks(lst, n):
        """Yield successive n-sized chunks from lst."""
        for i in range(0, len(lst), n):
            yield lst[i:i + n]

    assert len(dataset_names)
    dataset_dicts = [
        DatasetCatalog.get(dataset_name) for dataset_name in dataset_names
    ]
    for dataset_name, dicts in zip(dataset_names, dataset_dicts):
        assert len(dicts), "Dataset '{}' is empty!".format(dataset_name)

    if proposal_files is not None:
        assert len(dataset_names) == len(proposal_files)
        # load precomputed proposals from proposal files
        dataset_dicts = [
            load_proposals_into_dataset(dataset_i_dicts, proposal_file)
            for dataset_i_dicts, proposal_file in zip(dataset_dicts,
                                                      proposal_files)
        ]

    dataset_dicts = list(itertools.chain.from_iterable(dataset_dicts))

    # Group frames by video
    dataset_dicts = itertools.groupby(dataset_dicts, key=lambda x: x["video"])
    dataset_dicts = [list(v) for k, v in dataset_dicts]

    if train:
        # Build frame batches per video
        dataset_dicts = [
            list(chunks(video_dicts, frames_per_group))
            for video_dicts in dataset_dicts
        ]

        # Assign consecutive batch ids per video
        dataset_dicts = [[(b_id, batch)
                          for b_id, batch in enumerate(video_batches)]
                         for video_batches in dataset_dicts]

        # Filter batches by length (last batch of each video might be shorter than frames_per_group)
        dataset_dicts = [[(b_id, batch) for b_id, batch in video_batches
                          if len(batch) == frames_per_group]
                         for video_batches in dataset_dicts]

        # Remove batches with incomplete GT tubelets
        dataset_dicts = filter_invalid_frame_groups(dataset_dicts,
                                                    frames_per_group)

        # Subsample videos
        if cfg.DATALOADER.TRAIN_SUBSAMPLING:
            for video_idx in range(len(dataset_dicts)):

                if long_term:
                    video_copy = copy.deepcopy(dataset_dicts[video_idx])
                    # Add long-term support frames
                    for i in range(len(video_copy)):

                        if len(video_copy) > 1:
                            valid = list(range(len(video_copy)))
                            valid.remove(i)
                            batch_1 = np.random.choice(valid)
                            if len(
                                    video_copy
                            ) > 2:  # We can't select two different long-term batches
                                valid.remove(batch_1)
                            batch_2 = np.random.choice(valid)
                        else:
                            batch_1 = 0
                            batch_2 = 0

                        dataset_dicts[video_idx][i] = \
                            (video_copy[i][0], video_copy[i][1] + video_copy[batch_1][1] + video_copy[batch_2][1])

                if len(
                        dataset_dicts[video_idx]
                ) <= cfg.DATALOADER.TRAIN_GROUPS_PER_VIDEO * frames_per_group:
                    continue

                filtered_batch_ids = [
                    b_id for b_id, _ in dataset_dicts[video_idx]
                ]

                # Select TRAIN_GROUPS_PER_VIDEO batches evenly spaced from the first valid batch to the last one
                sampled_idx = np.round(
                    np.linspace(filtered_batch_ids[0], filtered_batch_ids[-1],
                                cfg.DATALOADER.TRAIN_GROUPS_PER_VIDEO))

                # Map sampled_idx to filtered dataset_dicts (removing short batches and incomplete GT tubelets)
                mapped_idx = np.searchsorted(filtered_batch_ids, sampled_idx)

                # Apply the mask
                dataset_dicts[video_idx] = [
                    dataset_dicts[video_idx][idx] for idx in mapped_idx
                ]

        # Remove batch ids
        dataset_dicts = [[batch for _, batch in video_batches]
                         for video_batches in dataset_dicts]

        dataset_dicts = list(itertools.chain.from_iterable(dataset_dicts))

    else:  # test: batch size = 1
        # frame padding at the beginning of each video
        frame_padding = cfg.MODEL.SPATIOTEMPORAL.NUM_FRAMES - 1

        for v_idx in range(len(dataset_dicts)):
            v = dataset_dicts[v_idx]
            for frame in v:
                frame['is_padding'] = False

            padding = []
            for _ in range(frame_padding):
                padding.append(copy.deepcopy(v[0]))
                padding[-1]['is_padding'] = True

            dataset_dicts[v_idx] = padding + v

        # frame padding at the end of each video
        if cfg.MODEL.SPATIOTEMPORAL.FORWARD_AGGREGATION:
            for v_idx in range(len(dataset_dicts)):
                v = dataset_dicts[v_idx]
                padding = []
                for _ in range(frame_padding + 1):
                    padding.append(copy.deepcopy(v[-1]))
                    padding[-1]['is_padding'] = True

                # We need one extra left frame padding
                dataset_dicts[v_idx] = [copy.deepcopy(v[0])] + v + padding
                dataset_dicts[v_idx][0]['is_padding'] = True

        dataset_dicts = list(itertools.chain.from_iterable(dataset_dicts))
        dataset_dicts = [[dataset_dict] for dataset_dict in dataset_dicts]

    logger = logging.getLogger(__name__)
    logger.info("Generating {} frame groups with {} frames.".format(
        len(dataset_dicts), frames_per_group))

    return dataset_dicts
import random
from detectron2.utils.visualizer import Visualizer
from detectron2.data.catalog import MetadataCatalog, DatasetCatalog
import data_register
from cv2 import cv2

fruits_nuts_metadata = MetadataCatalog.get("test")
print(fruits_nuts_metadata)
dataset_dicts = DatasetCatalog.get("test")

d = dataset_dicts[1]
img = cv2.imread(d["file_name"])
visualizer = Visualizer(img[:, :, ::-1],
                        metadata=fruits_nuts_metadata,
                        scale=1)
vis = visualizer.draw_dataset_dict(d)

img = vis.get_image()[:, :, ::-1]
cv2.imshow('rr', img)
cv2.waitKey(0)
Esempio n. 20
0
import random
from detectron2.utils.visualizer import Visualizer
from detectron2.data.catalog import MetadataCatalog, DatasetCatalog
import data_reg
import cv2

scoliosis_metadata = MetadataCatalog.get("scoliosis")
print(scoliosis_metadata)
dataset_dicts = DatasetCatalog.get("scoliosis")

for d in random.sample(dataset_dicts, 3):
    img = cv2.imread(d["file_name"])
    visualizer = Visualizer(img[:, :, ::-1],
                            metadata=scoliosis_metadata,
                            scale=1)
    vis = visualizer.draw_dataset_dict(d)
    img = vis.get_image()[:, :, ::-1]
    cv2.imshow('A random labeled picture', img)
    cv2.waitKey(0)
from detectron2.engine import DefaultPredictor
from detectron2.config import get_cfg
from detectron2.utils.visualizer import Visualizer
from detectron2.data import MetadataCatalog
from detectron2.data.catalog import DatasetCatalog
from detectron2.data.datasets import register_coco_instances
from detectron2.evaluation import COCOEvaluator
from detectron2.data import build_detection_test_loader
from detectron2.evaluation import inference_on_dataset

register_coco_instances("my_dataset_train", {}, "content/train/_annotations.coco.json", "content/train")
register_coco_instances("my_dataset_val", {}, "content/valid/_annotations.coco.json", "content/valid")

#visualize training data
my_dataset_train_metadata = MetadataCatalog.get("my_dataset_train")
dataset_dicts = DatasetCatalog.get("my_dataset_train")

for d in random.sample(dataset_dicts, 3):
    img = cv2.imread(d["file_name"])
    visualizer = Visualizer(img[:, :, ::-1], metadata=my_dataset_train_metadata, scale=0.5)
    vis = visualizer.draw_dataset_dict(d)
    cv2.imshow('preview',vis.get_image()[:, :, ::-1])
    cv2.waitKey(100)

#Importing our own Trainer Module to use the COCO validation evaluation 
#during training. Otherwise no validation eval occurs.

class CocoTrainer(DefaultTrainer):

  @classmethod
  def build_evaluator(cls, cfg, dataset_name, output_folder=None):
Esempio n. 22
0
# os.system("python voc2coco.py --ann_dir=data/test --labels=data/labels.txt --output=data/test_annots.json")
run_win_cmd(
    "python voc2coco.py --ann_dir=data/train --labels=data/labels.txt --output=data/train_annots.json"
)
run_win_cmd(
    "python voc2coco.py --ann_dir=data/validate --labels=data/labels.txt --output=data/validate_annots.json"
)
run_win_cmd(
    "python voc2coco.py --ann_dir=data/test --labels=data/labels.txt --output=data/test_annots.json"
)
register_coco_instances("train", {}, "data/train_annots.json", "data/train")
register_coco_instances("validate", {}, "data/validate_annots.json",
                        "data/validate")
register_coco_instances("test", {}, "data/test_annots.json", "data/test")

dataset_dicts = DatasetCatalog.get("train")
print(dataset_dicts[0])
trainset_metadata = MetadataCatalog.get("train")
# import random
# for d in random.sample(dataset_dicts, 1):
#     img = cv2.imread(d["file_name"])
#     visualizer = Visualizer(img[:, :, ::-1], metadata=trainset_metadata, scale=0.5)
#     vis = visualizer.draw_dataset_dict(d)
#     cv2.imshow("image", vis.get_image()[:, :, ::-1])
#     cv2.waitKey()  # press to exit

cfg = get_cfg()

# Get the basic model configuration from the model zoo
cfg.merge_from_file(
    model_zoo.get_config_file("COCO-Detection/faster_rcnn_R_50_FPN_3x.yaml"))
Esempio n. 23
0
from detectron2.data.datasets import register_coco_instances
from detectron2.data.catalog import MetadataCatalog, DatasetCatalog
import random
from detectron2.utils.visualizer import Visualizer
import cv2
from PIL import Image
import numpy as np


register_coco_instances("fsod", {}, "annotations/instances_train2017.json", "images/")
m = MetadataCatalog.get("fsod")
print(m)
dataset_dicts = DatasetCatalog.get("fsod")

for d in random.sample(dataset_dicts, 1):
    img = cv2.imread(d["file_name"])
    visualizer = Visualizer(img[:, :, ::-1], metadata=m, scale=0.5)
    vis = visualizer.draw_dataset_dict(d)
    img = Image.fromarray(vis.get_image()[:, :, ::-1], 'RGB')
    img.show()
Esempio n. 24
0
def recall_ssopg():
    dataset_name = sys.argv[1]
    dir_in = sys.argv[2]

    assert dataset_name == "voc_2007_test"
    dataset_dicts = DatasetCatalog.get(dataset_name)

    height_box_all = None
    width_box_all = None
    level_box_all = None
    score_box_all = None

    cnt_yes = [0 for _ in iou_thres]
    cnt_gt = [0 for _ in iou_thres]
    for i, d in enumerate(dataset_dicts):
        print(i, d)
        if i % 1000 == 0:
            print("{}/{}".format(i + 1, len(dataset_dicts)))

        gpu_id = int(i / 1238)
        iter_id = i % 1238

        load_name = "i" + str(iter_id) + "_g" + str(gpu_id) + "_b0_box_proposal.npy"
        load_path = os.path.join(dir_in, load_name)
        i_boxes = np.load(load_path)

        load_name = "i" + str(iter_id) + "_g" + str(gpu_id) + "_b0_logit_proposal.npy"
        load_path = os.path.join(dir_in, load_name)
        i_scores = np.load(load_path)

        load_name = "i" + str(iter_id) + "_g" + str(gpu_id) + "_b0_level_proposal.npy"
        load_path = os.path.join(dir_in, load_name)
        i_levels = np.load(load_path)

        print(i_boxes, i_boxes.shape)
        print(i_scores, i_scores.shape)
        print(i_levels, i_levels.shape)

        # -------------------------------------------------------------------------------
        # sort by confidence
        sorted_ind = np.argsort(-(i_scores.flatten()))
        i_boxes = i_boxes[sorted_ind, ...]
        i_scores = i_scores[sorted_ind, ...]
        i_levels = i_levels[sorted_ind, ...]

        i_boxes = i_boxes[:max_num_box, ...]
        i_scores = i_scores[:max_num_box, ...]
        i_levels = i_levels[:max_num_box, ...]

        # random
        # number_of_rows = i_boxes.shape[0]
        # random_indices = np.random.choice(number_of_rows, size=min(number_of_rows, max_num_box), replace=False)
        # i_boxes = i_boxes[random_indices,...]
        # i_scores = i_scores[random_indices,...]
        # i_levels = i_levels[random_indices,...]

        # -------------------------------------------------------------------------------
        height = d["height"]
        width = d["width"]

        scale_factor = 1.0 * min(height, width) / 688
        i_boxes = i_boxes * scale_factor
        # -------------------------------------------------------------------------------

        # -------------------------------------------------------------------------------
        height_box = i_boxes[:, 3] - i_boxes[:, 1] + 1.0
        width_box = i_boxes[:, 2] - i_boxes[:, 0] + 1.0
        if height_box_all is None:
            height_box_all = height_box
            width_box_all = width_box
            level_box_all = i_levels
            score_box_all = i_scores
        else:
            # print(height_box.shape, width_box.shape)
            print(height_box_all.shape, width_box_all.shape, level_box_all.shape)
            height_box_all = np.concatenate((height_box_all, height_box), axis=0)
            width_box_all = np.concatenate((width_box_all, width_box), axis=0)
            level_box_all = np.concatenate((level_box_all, i_levels), axis=0)
            score_box_all = np.concatenate((score_box_all, i_scores), axis=0)
        # -------------------------------------------------------------------------------

        for a in d["annotations"]:
            print(a["bbox"])
            bbgt = a["bbox"]

            if "coco" in dataset_name:
                bbgt = [bbgt[0], bbgt[1], bbgt[0] + bbgt[2], bbgt[1] + bbgt[3]]

            ixmin = np.maximum(i_boxes[:, 0], bbgt[0])
            iymin = np.maximum(i_boxes[:, 1], bbgt[1])
            ixmax = np.minimum(i_boxes[:, 2], bbgt[2])
            iymax = np.minimum(i_boxes[:, 3], bbgt[3])
            iw = np.maximum(ixmax - ixmin + 1.0, 0.0)
            ih = np.maximum(iymax - iymin + 1.0, 0.0)
            inters = iw * ih

            # union
            uni = (
                (bbgt[2] - bbgt[0] + 1.0) * (bbgt[3] - bbgt[1] + 1.0)
                + (i_boxes[:, 2] - i_boxes[:, 0] + 1.0) * (i_boxes[:, 3] - i_boxes[:, 1] + 1.0)
                - inters
            )

            overlaps = inters / uni
            ovmax = np.max(overlaps)
            jmax = np.argmax(overlaps)

            cnt_gt = [v + 1 for i, v in enumerate(cnt_gt)]
            cnt_yes = [v + 1 if ovmax >= iou_thres[i] else v for i, v in enumerate(cnt_yes)]

            print(ovmax, jmax)

            print(cnt_yes, cnt_gt, [1.0 * a / b for a, b in zip(cnt_yes, cnt_gt)])
    print(cnt_yes, cnt_gt)
    print([1.0 * a / b for a, b in zip(cnt_yes, cnt_gt)])

    save_path = os.path.join("mrrp_h_" + str(max_num_box) + ".npy")
    np.save(save_path, height_box_all)

    save_path = os.path.join("mrrp_w_" + str(max_num_box) + ".npy")
    np.save(save_path, width_box_all)

    save_path = os.path.join("mrrp_l_" + str(max_num_box) + ".npy")
    np.save(save_path, level_box_all)

    save_path = os.path.join("mrrp_s_" + str(max_num_box) + ".npy")
    np.save(save_path, score_box_all)

    return [1.0 * a / b for a, b in zip(cnt_yes, cnt_gt)]
Esempio n. 25
0
def build_classification_train_loader(cfg, mapper=None, multiplier=1):
    """
    A data loader is created by the following steps:
    1. Use the dataset names in config to query :class:`DatasetCatalog`, and obtain a list of dicts.
    2. Start workers to work on the dicts. Each worker will:
       * Map each metadata dict into another format to be consumed by the model.
       * Batch them by simply putting dicts into a list.
    The batched ``list[mapped_dict]`` is what this dataloader will return.
    Args:
        cfg (CfgNode): the config
        mapper (callable): a callable which takes a sample (dict) from dataset and
            returns the format to be consumed by the model.
            By default it will be `DatasetMapper(cfg, True)`.
    Returns:
        an infinite iterator of training data
    """
    num_workers = get_world_size()
    images_per_batch = cfg.SOLVER.IMS_PER_BATCH
    sample_num = cfg.DATASETS.WEAK_CLASSIFIER_SAMPLE_NUM
    assert (
        images_per_batch % num_workers == 0
    ), "SOLVER.IMS_PER_BATCH ({}) must be divisible by the number of workers ({}).".format(
        images_per_batch, num_workers)
    assert (
        images_per_batch >= num_workers
    ), "SOLVER.IMS_PER_BATCH ({}) must be larger than the number of workers ({}).".format(
        images_per_batch, num_workers)
    images_per_worker = images_per_batch // num_workers
    images_per_worker = int(images_per_worker * multiplier)
    if sample_num > 0:
        np.random.seed(cfg.DATASETS.SAMPLE_SEED)
        print("Setting sampling seed:", cfg.DATASETS.SAMPLE_SEED)
        dataset_names = cfg.DATASETS.CLASSIFIER_TRAIN
        if isinstance(dataset_names, str):
            dataset_names = [dataset_names]
        dataset_dicts = [
            DatasetCatalog.get(dataset_name) for dataset_name in dataset_names
        ]
        dataset_dicts = list(itertools.chain.from_iterable(dataset_dicts))
        label_to_annotation_dict = {
            e: []
            for e in range(cfg.MODEL.ROI_HEADS.NUM_CLASSES)
        }
        for e in dataset_dicts:
            per_label_record = {}
            for ann in e['annotations']:
                if ann['category_id'] in per_label_record:
                    per_label_record[ann['category_id']]['annotations'].append(
                        ann)
                else:
                    record = copy.deepcopy(e)
                    # filter annotations
                    annotations_filtered = [ann]
                    record['annotations'] = annotations_filtered
                    per_label_record[ann['category_id']] = record
            for key in per_label_record.keys():
                label_to_annotation_dict[key].append(per_label_record[key])

        label_to_annotation_dict_sampled = {}
        for id_class, ann_list in label_to_annotation_dict.items():
            if id_class in cfg.DATASETS.FEWSHOT.BASE_CLASSES_ID:
                if not cfg.DATASETS.OVER_SAMPLE:
                    if cfg.DATASETS.BASE_MULTIPLIER > 0:
                        try:
                            ann_list_sampled = np.random.choice(
                                ann_list,
                                size=int(sample_num *
                                         cfg.DATASETS.BASE_MULTIPLIER),
                                replace=False)
                        except:
                            ann_list_sampled = np.random.choice(
                                ann_list,
                                size=int(sample_num *
                                         cfg.DATASETS.BASE_MULTIPLIER),
                                replace=True)
                    else:
                        ann_list_sampled = ann_list
                else:
                    print("BASE OVER SAMPLING")
                    ann_list_sampled = ann_list
                label_to_annotation_dict_sampled[id_class] = ann_list_sampled
            else:
                if not cfg.DATASETS.OVER_SAMPLE:
                    if cfg.DATASETS.BASE_MULTIPLIER > 0:
                        try:
                            ann_list_sampled = np.random.choice(
                                ann_list, size=sample_num, replace=False)
                        except:
                            ann_list_sampled = np.random.choice(
                                ann_list, size=sample_num, replace=True)
                        if cfg.DATASETS.NOVEL_MULTIPLER > 0:
                            ann_list_sampled = np.repeat(
                                ann_list_sampled, cfg.DATASETS.NOVEL_MULTIPLER)
                    else:
                        ann_list_sampled = []

                else:
                    try:
                        ann_list_sampled_temp = np.random.choice(
                            ann_list, size=sample_num, replace=False)
                        if not cfg.DATASETS.SAMPLE_WITH_REPLACEMENT:
                            print("OVER SAMPLING")
                            ann_list_sampled = np.random.choice(
                                ann_list_sampled_temp,
                                size=len(ann_list),
                                replace=True)
                        else:
                            ann_list_sampled_temp = np.random.choice(
                                ann_list, size=sample_num, replace=False)
                            num_repeat = len(ann_list) // len(
                                ann_list_sampled_temp)
                            num_remainder = len(ann_list) % len(
                                ann_list_sampled_temp)
                            ann_list_sampled = np.repeat(
                                ann_list_sampled_temp, num_repeat)
                            if num_remainder > 0:
                                ann_list_sampled = np.hstack(
                                    (ann_list_sampled,
                                     np.random.choice(ann_list_sampled_temp,
                                                      size=num_remainder,
                                                      replace=True)))
                            print("OVER SAMPLING FIXED NEW",
                                  len(ann_list_sampled_temp),
                                  len(ann_list_sampled))
                    except:
                        ann_list_sampled = ann_list
                label_to_annotation_dict_sampled[id_class] = ann_list_sampled
        dataset_dicts = []
        for k, v in label_to_annotation_dict_sampled.items():
            dataset_dicts.extend(v)

        DatasetCatalog.register("classifier_train_sampled",
                                lambda: dataset_dicts)
        MetadataCatalog.get("classifier_train_sampled").set(
            thing_classes=MetadataCatalog.get(dataset_names[0]).thing_classes,
            evaluator_type='pascal_voc')
        dataset_name = ('classifier_train_sampled', )
        # print([(x['image_id'], len(x['annotations'])) for x in dataset_dicts[:50]])
        # print_instances_class_histogram_1(dataset_dicts, MetadataCatalog.get(dataset_names[0]).thing_classes)
    else:
        dataset_name = cfg.DATASETS.CLASSIFIER_TRAIN

    dataset_dicts = get_detection_dataset_dicts(
        dataset_name,
        filter_empty=cfg.DATALOADER.FILTER_EMPTY_ANNOTATIONS,
        min_keypoints=cfg.MODEL.ROI_KEYPOINT_HEAD.MIN_KEYPOINTS_PER_IMAGE
        if cfg.MODEL.KEYPOINT_ON else 0,
        proposal_files=cfg.DATASETS.PROPOSAL_FILES_CLASSIFIER_TRAIN
        if cfg.MODEL.LOAD_PROPOSALS else None)

    dataset = DatasetFromList(dataset_dicts, copy=False)
    # # filtering
    # dataset_filtered = []
    # for sample in dataset:
    #     e_class_ids = set([e['category_id'] for e in sample['annotations']])
    #     for e_class_ids_ in e_class_ids:
    #         if e_class_ids_ in cfg.DATASETS.FEWSHOT.NOVEL_CLASSES_ID:
    #             dataset_filtered.append(sample)
    #             break
    # dataset = dataset_filtered
    if mapper is None:
        mapper = DatasetMapper(cfg, True)
    dataset = MapDataset(dataset, mapper)

    sampler_name = cfg.DATALOADER.SAMPLER_TRAIN
    logger = logging.getLogger(__name__)
    logger.info("Using training sampler {}".format(sampler_name))
    if sampler_name == "TrainingSampler":
        sampler = samplers.TrainingSampler(len(dataset))
    elif sampler_name == "RepeatFactorTrainingSampler":
        sampler = samplers.RepeatFactorTrainingSampler(
            dataset_dicts, cfg.DATALOADER.REPEAT_THRESHOLD)
    else:
        raise ValueError("Unknown training sampler: {}".format(sampler_name))

    if cfg.DATALOADER.ASPECT_RATIO_GROUPING:
        data_loader = torch.utils.data.DataLoader(
            dataset,
            sampler=sampler,
            num_workers=cfg.DATALOADER.NUM_WORKERS,
            batch_sampler=None,
            collate_fn=operator.itemgetter(
                0),  # don't batch, but yield individual elements
            worker_init_fn=worker_init_reset_seed,
        )  # yield individual mapped dict
        data_loader = AspectRatioGroupedDataset(data_loader, images_per_worker)
    else:
        batch_sampler = torch.utils.data.sampler.BatchSampler(
            sampler, images_per_worker, drop_last=True)
        # drop_last so the batch always have the same size
        data_loader = torch.utils.data.DataLoader(
            dataset,
            num_workers=cfg.DATALOADER.NUM_WORKERS,
            batch_sampler=batch_sampler,
            collate_fn=trivial_batch_collator,
            worker_init_fn=worker_init_reset_seed,
        )

    return data_loader
        evaluator_type='coco',  # 指定评估方式
        json_file=TRAIN_JSON,
        image_root=TRAIN_PATH)

    # DatasetCatalog.register("coco_my_val", lambda: load_coco_json(VAL_JSON, VAL_PATH, "coco_2017_val"))
    DatasetCatalog.register("coco_my_val", lambda: load_coco_json(VAL_JSON, VAL_PATH))
    MetadataCatalog.get("coco_my_val").set(  # thing_classes=CLASS_NAMES, # 可以选择开启,但是不能显示中文,所以本人关闭
        evaluator_type='coco',  # 指定评估方式
        json_file=VAL_JSON,
        image_root=VAL_PATH)


plain_register_dataset()
fruits_nuts_metadata = MetadataCatalog.get("coco_my_train")
print(fruits_nuts_metadata)
dataset_dicts = DatasetCatalog.get("coco_my_train")


# Inference with a keypoint detection model
WINDOW_NAME = "COCO detections"
cfg = get_cfg()
cfg.merge_from_file(model_zoo.get_config_file("COCO-Detection/faster_rcnn_R_50_FPN_1x.yaml"))
cfg.MODEL.WEIGHTS = "/home/bruce/PycharmProjects/detectron2/tools/output/model_0001784.pth"
print('loading from: {}'.format(cfg.MODEL.WEIGHTS))
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5   # set the testing threshold for this model
cfg.MODEL.ROI_HEADS.NUM_CLASSES = 11
cfg.DATASETS.TEST = ("coco_my_val", )
predictor = DefaultPredictor(cfg)


cv2.namedWindow(WINDOW_NAME, cv2.WINDOW_NORMAL)  # 调整图像
Esempio n. 27
0
def recall_mcg():
    dataset_name = sys.argv[1]
    dir_in = sys.argv[2]

    dataset_dicts = DatasetCatalog.get(dataset_name)

    cnt_yes = [0 for _ in iou_thres]
    cnt_gt = [0 for _ in iou_thres]
    for i, d in enumerate(dataset_dicts):
        print(i, d)
        if i % 1000 == 0:
            print("{}/{}".format(i + 1, len(dataset_dicts)))

        if "flickr" in dataset_name:
            index = os.path.basename(dataset_dicts[i]["file_name"])[:-4]
        elif "coco" in dataset_name:
            index = os.path.basename(dataset_dicts[i]["file_name"])[:-4]
        else:
            index = dataset_dicts[i]["image_id"]
        box_file = os.path.join(dir_in, "{}.mat".format(index))
        mat_data = sio.loadmat(box_file)
        if i == 0:
            print(mat_data.keys())
        if "flickr" in dataset_name:
            boxes_data = mat_data["bboxes"]
            scores_data = mat_data["bboxes_scores"]
        else:
            boxes_data = mat_data["boxes"]
            scores_data = mat_data["scores"]
        # selective search boxes are 1-indexed and (y1, x1, y2, x2)
        # Boxes from the MCG website are in (y1, x1, y2, x2) order
        boxes_data = boxes_data[:, (1, 0, 3, 2)] - 1
        # boxes_data_ = boxes_data.astype(np.uint16) - 1
        # boxes_data = boxes_data_[:, (1, 0, 3, 2)]

        # -------------------------------------------------------------------------------
        # sort by confidence
        sorted_ind = np.argsort(-(scores_data.flatten()))
        boxes_data = boxes_data[sorted_ind, :]
        scores_data = scores_data[sorted_ind, :]

        boxes_data = boxes_data[:max_num_box, ...]
        scores_data = scores_data[:max_num_box, ...]

        # random
        # number_of_rows = boxes_data.shape[0]
        # random_indices = np.random.choice(number_of_rows, size=min(number_of_rows, max_num_box), replace=False)
        # boxes_data = boxes_data[random_indices,...]
        # scores_data = scores_data[random_indices,...]

        # -------------------------------------------------------------------------------

        for a in d["annotations"]:
            print(a["bbox"])
            bbgt = a["bbox"]

            if "coco" in dataset_name:
                bbgt = [bbgt[0], bbgt[1], bbgt[0] + bbgt[2], bbgt[1] + bbgt[3]]

            ixmin = np.maximum(boxes_data[:, 0], bbgt[0])
            iymin = np.maximum(boxes_data[:, 1], bbgt[1])
            ixmax = np.minimum(boxes_data[:, 2], bbgt[2])
            iymax = np.minimum(boxes_data[:, 3], bbgt[3])
            iw = np.maximum(ixmax - ixmin + 1.0, 0.0)
            ih = np.maximum(iymax - iymin + 1.0, 0.0)
            inters = iw * ih

            # union
            uni = (
                (bbgt[2] - bbgt[0] + 1.0) * (bbgt[3] - bbgt[1] + 1.0)
                + (boxes_data[:, 2] - boxes_data[:, 0] + 1.0)
                * (boxes_data[:, 3] - boxes_data[:, 1] + 1.0)
                - inters
            )

            overlaps = inters / uni
            ovmax = np.max(overlaps)
            jmax = np.argmax(overlaps)

            cnt_gt = [v + 1 for i, v in enumerate(cnt_gt)]
            cnt_yes = [v + 1 if ovmax >= iou_thres[i] else v for i, v in enumerate(cnt_yes)]

            print(ovmax, jmax)

            print(cnt_yes, cnt_gt, [1.0 * a / b for a, b in zip(cnt_yes, cnt_gt)])
    print(cnt_yes, cnt_gt)
    print([1.0 * a / b for a, b in zip(cnt_yes, cnt_gt)])

    return [1.0 * a / b for a, b in zip(cnt_yes, cnt_gt)]
Esempio n. 28
0
def get_classification_datasets(dataset_names):
    assert len(dataset_names)
    datasets = [
        DatasetCatalog.get(dataset_name) for dataset_name in dataset_names
    ]
    return datasets
Esempio n. 29
0
def recall_eb():
    dataset_name = sys.argv[1]
    file_in = sys.argv[2]

    dataset_dicts = DatasetCatalog.get(dataset_name)

    print(sio.loadmat(file_in))
    # exit()
    mat_data = sio.loadmat(file_in)
    boxes_data = mat_data["boxes"].ravel()
    scores_data = mat_data["boxScores"].ravel()
    assert boxes_data.shape[0] == len(dataset_dicts)
    assert scores_data.shape[0] == len(dataset_dicts)

    cnt_yes = [0 for _ in iou_thres]
    cnt_gt = [0 for _ in iou_thres]
    for i, d in enumerate(dataset_dicts):
        print(i, d)
        if i % 1000 == 0:
            print("{}/{}".format(i + 1, len(dataset_dicts)))

        # selective search boxes are 1-indexed and (y1, x1, y2, x2)
        i_boxes = boxes_data[i][:, (1, 0, 3, 2)] - 1
        i_scores = scores_data[i][:]

        # -------------------------------------------------------------------------------
        # sort by confidence
        sorted_ind = np.argsort(-(i_scores.flatten()))
        i_boxes = i_boxes[sorted_ind, :]
        i_scores = i_scores[sorted_ind, :]

        i_boxes = i_boxes[:max_num_box, ...]
        i_scores = i_scores[:max_num_box, ...]

        # random
        # number_of_rows = i_boxes.shape[0]
        # random_indices = np.random.choice(number_of_rows, size=min(number_of_rows, max_num_box), replace=False)
        # i_boxes = i_boxes[random_indices,...]
        # i_scores = i_scores[random_indices,...]
        # -------------------------------------------------------------------------------

        for a in d["annotations"]:
            print(a["bbox"])
            bbgt = a["bbox"]

            if "coco" in dataset_name:
                bbgt = [bbgt[0], bbgt[1], bbgt[0] + bbgt[2], bbgt[1] + bbgt[3]]

            ixmin = np.maximum(i_boxes[:, 0], bbgt[0])
            iymin = np.maximum(i_boxes[:, 1], bbgt[1])
            ixmax = np.minimum(i_boxes[:, 2], bbgt[2])
            iymax = np.minimum(i_boxes[:, 3], bbgt[3])
            iw = np.maximum(ixmax - ixmin + 1.0, 0.0)
            ih = np.maximum(iymax - iymin + 1.0, 0.0)
            inters = iw * ih

            # union
            uni = (
                (bbgt[2] - bbgt[0] + 1.0) * (bbgt[3] - bbgt[1] + 1.0)
                + (i_boxes[:, 2] - i_boxes[:, 0] + 1.0) * (i_boxes[:, 3] - i_boxes[:, 1] + 1.0)
                - inters
            )

            overlaps = inters / uni
            ovmax = np.max(overlaps)
            jmax = np.argmax(overlaps)

            cnt_gt = [v + 1 for i, v in enumerate(cnt_gt)]
            cnt_yes = [v + 1 if ovmax >= iou_thres[i] else v for i, v in enumerate(cnt_yes)]

            print(ovmax, jmax)

            print(cnt_yes, cnt_gt, [1.0 * a / b for a, b in zip(cnt_yes, cnt_gt)])
    print(cnt_yes, cnt_gt)
    print([1.0 * a / b for a, b in zip(cnt_yes, cnt_gt)])

    return [1.0 * a / b for a, b in zip(cnt_yes, cnt_gt)]
Esempio n. 30
0
def fsod_get_detection_dataset_dicts(dataset_names,
                                     filter_empty=True,
                                     min_keypoints=0,
                                     proposal_files=None):
    """
    Load and prepare dataset dicts for instance detection/segmentation and semantic segmentation.
    Args:
        dataset_names (list[str]): a list of dataset names
        filter_empty (bool): whether to filter out images without instance annotations
        min_keypoints (int): filter out images with fewer keypoints than
            `min_keypoints`. Set to 0 to do nothing.
        proposal_files (list[str]): if given, a list of object proposal files
            that match each dataset in `dataset_names`.
    """
    assert len(dataset_names)
    dataset_dicts_original = [
        DatasetCatalog.get(dataset_name) for dataset_name in dataset_names
    ]
    for dataset_name, dicts in zip(dataset_names, dataset_dicts_original):
        assert len(dicts), "Dataset '{}' is empty!".format(dataset_name)

    if proposal_files is not None:
        assert len(dataset_names) == len(proposal_files)
        # load precomputed proposals from proposal files
        dataset_dicts_original = [
            load_proposals_into_dataset(dataset_i_dicts, proposal_file)
            for dataset_i_dicts, proposal_file in zip(dataset_dicts_original,
                                                      proposal_files)
        ]

    if 'train' not in dataset_names[0]:
        dataset_dicts = list(
            itertools.chain.from_iterable(dataset_dicts_original))
    else:
        dataset_dicts_original = list(
            itertools.chain.from_iterable(dataset_dicts_original))
        dataset_dicts_original = filter_images_with_only_crowd_annotations(
            dataset_dicts_original)
        ###################################################################################
        # split image-based annotations to instance-based annotations for few-shot learning
        dataset_dicts = []
        index_dicts = []
        split_flag = True
        if split_flag:
            for record in dataset_dicts_original:
                file_name = record['file_name']
                height = record['height']
                width = record['width']
                image_id = record['image_id']
                annotations = record['annotations']
                category_dict = {}
                for ann_id, ann in enumerate(annotations):

                    ann.pop("segmentation", None)
                    ann.pop("keypoints", None)

                    category_id = ann['category_id']
                    if category_id not in category_dict.keys():
                        category_dict[category_id] = [ann]
                    else:
                        category_dict[category_id].append(ann)

                for key, item in category_dict.items():
                    instance_ann = {}
                    instance_ann['file_name'] = file_name
                    instance_ann['height'] = height
                    instance_ann['width'] = width

                    instance_ann['annotations'] = item

                    dataset_dicts.append(instance_ann)

    has_instances = "annotations" in dataset_dicts[0]
    # Keep images without instance-level GT if the dataset has semantic labels.
    if filter_empty and has_instances and "sem_seg_file_name" not in dataset_dicts[
            0]:
        dataset_dicts = filter_images_with_only_crowd_annotations(
            dataset_dicts)

    if min_keypoints > 0 and has_instances:
        dataset_dicts = filter_images_with_few_keypoints(
            dataset_dicts, min_keypoints)

    if has_instances:
        try:
            class_names = MetadataCatalog.get(dataset_names[0]).thing_classes
            check_metadata_consistency("thing_classes", dataset_names)
            print_instances_class_histogram(dataset_dicts, class_names)
        except AttributeError:  # class names are not available for this dataset
            pass
    return dataset_dicts