def register_hico_instances(name, metadata, json_file, image_root, evaluator_type): """ Register a hico-det dataset in COCO's json annotation format for human-object interaction detection (i.e., `instances_hico_*.json` in the dataset). This is an example of how to register a new dataset. You can do something similar to this function, to register new datasets. Args: name (str): the name that identifies a dataset, e.g. "hico-det". metadata (dict): extra metadata associated with this dataset. You can leave it as an empty dict. json_file (str): path to the json instance annotation file. image_root (str or path-like): directory which contains all the images. """ # 1. register a function which returns dicts DatasetCatalog.register( name, lambda: load_hico_json(json_file, image_root, name)) # 2. Optionally, add metadata about this dataset, # since they might be useful in evaluation, visualization or logging MetadataCatalog.get(name).set(json_file=json_file, image_root=image_root, evaluator_type=evaluator_type, **metadata)
def register_dacon_instances(name, metadata, json_file, image_root): """ Register a dataset in COCO's json annotation format for instance detection, instance segmentation and keypoint detection. (i.e., Type 1 and 2 in http://cocodataset.org/#format-data. `instances*.json` and `person_keypoints*.json` in the dataset). This is an example of how to register a new dataset. You can do something similar to this function, to register new datasets. Args: name (str): the name that identifies a dataset, e.g. "coco_2014_train". metadata (dict): extra metadata associated with this dataset. You can leave it as an empty dict. json_file (str): path to the json instance annotation file. image_root (str or path-like): directory which contains all the images. """ assert isinstance(name, str), name assert isinstance(json_file, (str, os.PathLike)), json_file assert isinstance(image_root, (str, os.PathLike)), image_root # 1. register a function which returns dicts DatasetCatalog.register( name, lambda: load_dacon_json(json_file, image_root, name)) # 2. Optionally, add metadata about this dataset, # since they might be useful in evaluation, visualization or logging MetadataCatalog.get(name).set(json_file=json_file, image_root=image_root, evaluator_type="coco", **metadata)
def generate_datasets(self, Ntraining, method, gt_frames): dataset_train, dataset_val = get_dicts(Ntraining, method, gt_frames) for d in ['train', 'val']: DatasetCatalog.register(d + '_set', lambda d=d: dataset_train if d == 'train' else dataset_val) MetadataCatalog.get(d + '_set').set( thing_classes=['Person', 'None', 'Car'])
def plain_register_dataset(): DatasetCatalog.register("coco_my_train", lambda: load_coco_json(TRAIN_JSON, TRAIN_PATH)) MetadataCatalog.get("coco_my_train").set( # thing_classes=CLASS_NAMES, # 可以选择开启,但是不能显示中文,所以本人关闭 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)
def register_vcoco_instances(name, metadata, json_file, image_root, evaluator_type): """ Register a vcoco dataset in COCO's json annotation format for human-object interaction detection (i.e., `instances_hico_*.json` in the dataset). Args: see `register_hico_instances` """ # 1. register a function which returns dicts DatasetCatalog.register( name, lambda: load_vcoco_json(json_file, image_root, name)) # 2. Optionally, add metadata about this dataset, # since they might be useful in evaluation, visualization or logging MetadataCatalog.get(name).set(json_file=json_file, image_root=image_root, evaluator_type=evaluator_type, **metadata)
def main(args): cfg = setup(args) json_dir = cfg.INPUT.DIR for d in ["train", "val"]: json_path = json_dir + f"plane_net_{d}_coco_format.json" with open(json_path, "r") as f: dataset_dicts = json.load(f) DatasetCatalog.register("plane_" + d, dataset_dicts) MetadataCatalog.get("plane_" + d).set(thing_classes=["plane"]) plane_metadata = MetadataCatalog.get("plane_train") if args.eval_only: model = Trainer.build_model(cfg) DetectionCheckpointer(model, save_dir=cfg.OUTPUT_DIR).resume_or_load( cfg.MODEL.WEIGHTS, resume=args.resume) res = Trainer.test(cfg, model) if comm.is_main_process(): verify_results(cfg, res) return res trainer = Trainer(cfg) trainer.resume_or_load(resume=args.resume) return trainer.train()
def register_torchvision_test_instance(name, data_root): DatasetCatalog.register( name, lambda: load_torchvision_json(data_root, name, False))
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
def register_dacon_rotated_train_instance(name, json_file, image_root): DatasetCatalog.register( name, lambda: load_dacon_rotated_train_json(json_file, image_root, name))
objs = [] for k, val in enumerate(unval[:-1]): gt_mask = np.asarray(idmap == val, order="F") gt_bbox = toBbox(gt_mask) obj = { "bbox": gt_bbox, "bbox_mode": BoxMode.XYXY_ABS, # wait for confirm "segmentation": gt_mask, "category_id": 0, } objs.append(obj) record["annotations"] = objs record["junc_heat_map"] = jmap return record def get_plane_dicts(root): filelist = sorted(glob.glob(f"{root}/[0-9]*/*_0_a0_label.npz")) dataset_dicts = [] print("filelist length is%s" % len(filelist)) for idx in range(len(filelist)): single_record = process_item(filelist[idx], idx) dataset_dicts.append(single_record) for d in ["train", "val"]: DatasetCatalog.register("plane_" + d, lambda d=d: get_plane_dicts(rootdir + d)) MetadataCatalog.get("plane_" + d).set(thing_classes=["plane"]) plane_metadata = MetadataCatalog.get("plane_train")
def register_dota_test_instance(name, json_file, image_root): DatasetCatalog.register( name, lambda: load_dota_test_json(json_file, image_root, name))
def register_imgnet_test_instance(name, data_root): DatasetCatalog.register(name, lambda: load_imgnet_test_json(data_root, name))