Exemple #1
0
        for v in value:
            object_list.append(v)

    messages.append('\n')
    messages.append('import mmcv\n')
    messages.append("constant = mmcv.load('deployment.yml')\n")
    messages.append('\n')
    messages.append('class ObjectManage:\n')
    for v in object_list:
        messages.append(f'    {class_to_object_name(v)} = {v}()\n')
    messages.append('    pass\n')

    with open(file_path, 'a') as f:
        f.writelines(messages)


def class_to_object_name(class_name):
    list_ = []
    for m in [*class_name]:
        if m in upper:
            list_.append(obj[m])
        else:
            list_.append(m)
    name_str = ''.join(list_)
    return name_str[1:]


if __name__ == "__main__":
    opts = mmcv.load('target_project_infomation.yml')
    create_object_script(opts)
Exemple #2
0
def test_object_sample():
    db_sampler = mmcv.ConfigDict({
        'data_root': './tests/data/kitti/',
        'info_path': './tests/data/kitti/kitti_dbinfos_train.pkl',
        'rate': 1.0,
        'prepare': {
            'filter_by_difficulty': [-1],
            'filter_by_min_points': {
                'Pedestrian': 10
            }
        },
        'classes': ['Pedestrian', 'Cyclist', 'Car'],
        'sample_groups': {
            'Pedestrian': 6
        }
    })
    np.random.seed(0)
    object_sample = ObjectSample(db_sampler)
    points = np.fromfile(
        './tests/data/kitti/training/velodyne_reduced/000000.bin',
        np.float32).reshape(-1, 4)
    annos = mmcv.load('./tests/data/kitti/kitti_infos_train.pkl')
    info = annos[0]
    rect = info['calib']['R0_rect'].astype(np.float32)
    Trv2c = info['calib']['Tr_velo_to_cam'].astype(np.float32)
    annos = info['annos']
    loc = annos['location']
    dims = annos['dimensions']
    rots = annos['rotation_y']
    gt_names = annos['name']

    gt_bboxes_3d = np.concatenate([loc, dims, rots[..., np.newaxis]],
                                  axis=1).astype(np.float32)
    gt_bboxes_3d = CameraInstance3DBoxes(gt_bboxes_3d).convert_to(
        Box3DMode.LIDAR, np.linalg.inv(rect @ Trv2c))
    CLASSES = ('Pedestrian', 'Cyclist', 'Car')
    gt_labels = []
    for cat in gt_names:
        if cat in CLASSES:
            gt_labels.append(CLASSES.index(cat))
        else:
            gt_labels.append(-1)
    gt_labels = np.array(gt_labels, dtype=np.long)
    points = LiDARPoints(points, points_dim=4)
    input_dict = dict(points=points,
                      gt_bboxes_3d=gt_bboxes_3d,
                      gt_labels_3d=gt_labels)
    input_dict = object_sample(input_dict)
    points = input_dict['points']
    gt_bboxes_3d = input_dict['gt_bboxes_3d']
    gt_labels_3d = input_dict['gt_labels_3d']
    repr_str = repr(object_sample)
    expected_repr_str = 'ObjectSample sample_2d=False, ' \
                        'data_root=./tests/data/kitti/, ' \
                        'info_path=./tests/data/kitti/kitti' \
                        '_dbinfos_train.pkl, rate=1.0, ' \
                        'prepare={\'filter_by_difficulty\': [-1], ' \
                        '\'filter_by_min_points\': {\'Pedestrian\': 10}}, ' \
                        'classes=[\'Pedestrian\', \'Cyclist\', \'Car\'], ' \
                        'sample_groups={\'Pedestrian\': 6}'
    assert repr_str == expected_repr_str
    assert points.tensor.numpy().shape == (800, 4)
    assert gt_bboxes_3d.tensor.shape == (1, 7)
    assert np.all(gt_labels_3d == [0])
Exemple #3
0
 def load_proposals(self, proposal_file):
     return mmcv.load(proposal_file)
        "If disparity map given, it will be divided by {:.2f} to get the real disparity value"
        .format(disp_div_factor))

    print("Initial Model ... ")
    model = init_model(config_path, checkpoint_path, device)
    print("Model initialed!")

    print("Start Inference ... ")
    inference_stereo(
        model,
        batchesDict,
        log_dir,
        pad_to_shape,
        crop_shape,
        scale_factor,
        disp_div_factor,
        device,
    )
    print("Inference Done!")

    print("Start Visualization ... ")
    for batch in batchesDict:
        pkl_path = os.path.join(
            log_dir, batch['left_image_path'].split('/')[-1].split('.')[0],
            'result.pkl')
        print("Visualize ", pkl_path)
        result_pkl = mmcv.load(pkl_path)
        visualize_disp(result_pkl)

    print("Done!")
def main():
    args = parse_args()

    assert args.out or args.eval or args.format_only or args.show, \
        ('Please specify at least one operation (save/eval/format/show the '
         'results) with the argument "--out", "--eval", "--format_only" '
         'or "--show"')

    # if args.eval and args.format_only:
    #     raise ValueError('--eval and --format_only cannot be both specified')

    if args.out is not None and not args.out.endswith(('.pkl', '.pickle')):
        raise ValueError('The output file must be a pkl file.')

    cfg = mmcv.Config.fromfile(args.config)
    # set cudnn_benchmark
    if cfg.get('cudnn_benchmark', False):
        torch.backends.cudnn.benchmark = True
    cfg.model.pretrained = None
    cfg.data.test.test_mode = True

    # init distributed env first, since logger depends on the dist info.
    if args.launcher == 'none':
        distributed = False
    else:
        distributed = True
        init_dist(args.launcher, **cfg.dist_params)

    # create work_dir with timestamp tag
    timestamp = time.strftime('%Y%m%d_%H%M%S', time.localtime())
    cfg.work_dir = osp.dirname(args.checkpoint)
    if args.out is None:
        args.out = osp.join(
            cfg.work_dir,
            '{}_{}'.format(timestamp,
                           osp.basename(args.checkpoint).replace('pth',
                                                                 'pkl')))

    mmcv.mkdir_or_exist(osp.abspath(cfg.work_dir))
    # init the logger before other steps
    log_file = osp.join(cfg.work_dir, '{}_test.log'.format(timestamp))
    logger = get_root_logger(log_file=log_file, log_level=cfg.log_level)

    # log some basic info
    logger.info('Command-line argument:\n{}'.format(' '.join(sys.argv[1:])))
    logger.info('Distributed training: {}'.format(distributed))
    logger.info('MMDetection Version: {}'.format(__version__))
    logger.info('Config:\n{}'.format(cfg.text))

    # build the dataloader
    # TODO: support multiple images per gpu (only minor changes are needed)
    dataset = build_dataset(cfg.data.test)
    data_loader = build_dataloader(dataset,
                                   imgs_per_gpu=1,
                                   workers_per_gpu=cfg.data.workers_per_gpu,
                                   dist=distributed,
                                   shuffle=False)

    # build the model and load checkpoint
    model = build_detector(cfg.model, train_cfg=None, test_cfg=cfg.test_cfg)
    fp16_cfg = cfg.get('fp16', None)
    if fp16_cfg is not None:
        wrap_fp16_model(model)
    checkpoint = load_checkpoint(model, args.checkpoint, map_location='cpu')
    # old versions did not save class info in checkpoints, this walkaround is
    # for backward compatibility
    if 'CLASSES' in checkpoint['meta']:
        model.CLASSES = checkpoint['meta']['CLASSES']
    else:
        model.CLASSES = dataset.CLASSES
    if args.use_cache and osp.exists(args.out):
        print('\nreuse results from {}'.format(args.out))
        outputs = mmcv.load(args.out)
    else:
        if not distributed:
            model = MMDataParallel(model, device_ids=[0])
            outputs = single_gpu_test(model, data_loader, args.show)
        else:
            model = MMDistributedDataParallel(
                model.cuda(),
                device_ids=[torch.cuda.current_device()],
                broadcast_buffers=False)
            outputs = multi_gpu_test(model, data_loader, args.tmpdir,
                                     args.gpu_collect)
        print('\nwriting results to {}'.format(args.out))
        mmcv.dump(outputs, args.out)

    rank, _ = get_dist_info()
    if rank == 0:
        kwargs = {} if args.options is None else args.options
        if args.format_only:
            save_dir = osp.join(cfg.work_dir,
                                osp.basename(args.out).split('.')[0] + '_out')
            print('\nsaving results to {}'.format(save_dir))
            kwargs['data_loader'] = data_loader
            kwargs['save_dir'] = save_dir
            dataset.format_results(outputs, **kwargs)
        if args.eval:
            kwargs['logger'] = logger
            dataset.evaluate(outputs, args.eval, **kwargs)
 def load_annotations(self, ann_file):
     img_infos = mmcv.load(ann_file)
     for item in img_infos:
         item.update({'id': item['filename'].split('/')[-1].split('.')[0]})
     return img_infos
def get_data_sample():
    def _parse_h36m_imgname(imgname):
        """Parse imgname to get information of subject, action and camera.

        A typical h36m image filename is like:
        S1_Directions_1.54138969_000001.jpg
        """
        subj, rest = osp.basename(imgname).split('_', 1)
        action, rest = rest.split('.', 1)
        camera, rest = rest.split('_', 1)
        return subj, action, camera

    ann_flle = 'tests/data/h36m/test_h36m.npz'
    camera_param_file = 'tests/data/h36m/cameras.pkl'

    data = np.load(ann_flle)
    cameras = mmcv.load(camera_param_file)

    _imgnames = data['imgname']
    _joints_2d = data['part'][:, H36M_JOINT_IDX].astype(np.float32)
    _joints_3d = data['S'][:, H36M_JOINT_IDX].astype(np.float32)
    _centers = data['center'].astype(np.float32)
    _scales = data['scale'].astype(np.float32)

    frame_ids = [0]
    target_frame_id = 0

    results = {
        'frame_ids': frame_ids,
        'target_frame_id': target_frame_id,
        'input_2d': _joints_2d[frame_ids, :, :2],
        'input_2d_visible': _joints_2d[frame_ids, :, -1:],
        'input_3d': _joints_3d[frame_ids, :, :3],
        'input_3d_visible': _joints_3d[frame_ids, :, -1:],
        'target': _joints_3d[target_frame_id, :, :3],
        'target_visible': _joints_3d[target_frame_id, :, -1:],
        'imgnames': _imgnames[frame_ids],
        'scales': _scales[frame_ids],
        'centers': _centers[frame_ids],
    }

    # add camera parameters
    subj, _, camera = _parse_h36m_imgname(_imgnames[frame_ids[0]])
    results['camera_param'] = cameras[(subj, camera)]

    # add image size
    results['image_width'] = results['camera_param']['w']
    results['image_height'] = results['camera_param']['h']

    # add ann_info
    ann_info = {}
    ann_info['num_joints'] = 17
    ann_info['joint_weights'] = np.full(17, 1.0, dtype=np.float32)
    ann_info['flip_pairs'] = [[1, 4], [2, 5], [3, 6], [11, 14], [12, 15],
                              [13, 16]]
    ann_info['upper_body_ids'] = (0, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)
    ann_info['lower_body_ids'] = (1, 2, 3, 4, 5, 6)
    ann_info['use_different_joint_weights'] = False

    results['ann_info'] = ann_info

    return results
    parser.add_argument(
        '--out', type=str, help='output path of gathered metrics to be stored')
    parser.add_argument(
        '--not-show', action='store_true', help='not show metrics')

    args = parser.parse_args()
    return args


if __name__ == '__main__':
    args = parse_args()

    root_path = args.root
    metrics_out = args.out
    benchmark_json_path = args.benchmark_json
    model_configs = mmcv.load(benchmark_json_path)['models']

    result_dict = {}
    for config in model_configs:
        config_name = osp.split(config)[-1]
        config_name = osp.splitext(config_name)[0]
        result_path = osp.join(root_path, config_name)
        if osp.exists(result_path):
            # 1 read config
            cfg = mmcv.Config.fromfile(config)
            total_epochs = cfg.total_epochs
            final_results = cfg.evaluation.metric
            if not isinstance(final_results, list):
                final_results = [final_results]
            final_results_out = []
            for key in final_results:
Exemple #9
0
def lvis_fast_eval_recall(results,
                          lvisEval,
                          max_dets,
                          iou_thrs=np.arange(0.5, 0.96, 0.05)):
    if mmcv.is_str(results):
        assert results.endswith('.pkl')
        results = mmcv.load(results)
        if isinstance(results[0], tuple):
            proposals = []
            for idx in range(len(results)):
                proposals.append(results[idx][-1])
            results = proposals
        assert isinstance(results, list), \
            'results must be a list of numpy arrays, not {}'.format(type(results))
    elif not isinstance(results, list):
        raise TypeError(
            'results must be a list of numpy arrays or a filename, not {}'.
            format(type(results)))

    img_ids = lvisEval.lvis_gt.get_img_ids()
    area_rngs = lvisEval.params.area_rng

    freq_groups = [[] for _ in lvisEval.params.img_count_lbl]
    cat_data = lvisEval.lvis_gt.load_cats(lvisEval.params.cat_ids)
    for idx, _cat_data in enumerate(cat_data):
        frequency = _cat_data["frequency"]
        freq_groups[lvisEval.params.img_count_lbl.index(frequency)].append(idx+1)

    gt_bboxes_all = defaultdict(list)
    dt_bboxes_all = []
    for area_idx in range(len(area_rngs)):
        for group_idx in range(len(freq_groups)):
            gt_bboxes_all[group_idx, area_idx] = []

    gt_bboxes = defaultdict(list)
    all_num = np.zeros((len(freq_groups), len(area_rngs)))
    group_num = np.zeros((len(freq_groups), 1))
    for i in range(len(img_ids)):
        dt_bboxes = []
        for area_idx in range(len(area_rngs)):
            for group_idx in range(len(freq_groups)):
                gt_bboxes[group_idx, area_idx] = []

        ann_ids = lvisEval.lvis_gt.get_ann_ids(img_ids=[img_ids[i]])
        ann_info = lvisEval.lvis_gt.load_anns(ann_ids)
        det_ids = lvisEval.lvis_dt.get_ann_ids(img_ids=[img_ids[i]])
        det_info = lvisEval.lvis_dt.load_anns(det_ids)

        img_pl = set()
        for ann in ann_info:
            img_pl.add(ann["category_id"])
        img_pl_group = dict()
        for cat in img_pl:
            for group_idx, freq_group in enumerate(freq_groups):
                if cat in freq_group:
                    img_pl_group[cat] = group_idx
                    break

        for ann in ann_info:
            for area_idx, area_rng in enumerate(area_rngs):
                if area_rng[0] <= ann['area'] < area_rng[1]:
                    group_idx = img_pl_group[ann['category_id']]
                    if area_idx == 0:
                        group_num[group_idx] += 1  # only count for area all
                    x1, y1, w, h = ann['bbox']
                    gt_bboxes[group_idx, area_idx].append(
                        [x1, y1, x1 + w - 1, y1 + h - 1])

        for det in det_info:
            x1, y1, w, h = det['bbox']
            dt_bboxes.append([x1, y1, x1 + w - 1, y1 + h - 1, det['score']])

        for area_idx in range(len(area_rngs)):
            for group_idx in range(len(freq_groups)):
                if len(gt_bboxes[group_idx, area_idx]) == 0:
                    gt_bboxes_all[group_idx, area_idx].append(np.zeros((0, 4)))
                else:
                    all_num[group_idx, area_idx] += len(gt_bboxes[group_idx, area_idx])
                    gt_bboxes_all[group_idx, area_idx].append(
                        np.array(gt_bboxes[group_idx, area_idx], dtype=np.float32))

        if len(dt_bboxes) == 0:
            dt_bboxes_all.append(np.zeros((0, 5)))
        else:
            dt_bboxes_all.append(
                np.array(dt_bboxes, dtype=np.float32))

    ar_all = np.zeros((len(freq_groups), len(area_rngs), len(max_dets)))
    for group_idx in range(len(freq_groups)):
        for area_idx in range(len(area_rngs)):
            recalls = eval_recalls(
                gt_bboxes_all[group_idx, area_idx],
                dt_bboxes_all,
                max_dets, iou_thrs, print_summary=False)
            ar_all[group_idx, area_idx, :] = recalls.mean(axis=1)
    ar_all = np.transpose(ar_all, (2, 0, 1))

    area_rng_name = ["all", "small", "medium", "large"]
    freq_group_name = ["rare", "common", "freq"]

    table_data = [['Num'] + area_rng_name]
    for group_idx in range(len(freq_groups)):
        row = ['{}'.format(int(val)) for val in all_num[group_idx, :].tolist()]
        row.insert(0, freq_group_name[group_idx])
        table_data.append(row)
    print(AsciiTable(table_data).table)

    recalls = np.zeros((max_dets.size, len(freq_groups) + len(area_rngs)))
    for det_num_idx, max_det in enumerate(max_dets):
        ar = ar_all[det_num_idx, :, :]
        ar_cat_all = np.sum(ar * group_num, axis=0) / np.sum(group_num)
        for area_idx in range(len(area_rngs)):
            recalls[det_num_idx, area_idx] = ar_cat_all[area_idx]  # all small meduim large
        for group_idx in range(len(freq_groups)):
            recalls[det_num_idx, group_idx + len(area_rngs)] = ar[group_idx, 0]  # rare common freq

        row_header = ['AR@{}'.format(max_det)] + area_rng_name
        table_data = [row_header]
        for group_idx in range(len(freq_groups)):
            row = ['{:.3f}'.format(val) for val in ar[group_idx, :].tolist()]
            row.insert(0, freq_group_name[group_idx])
            table_data.append(row)
        table = AsciiTable(table_data)
        print(table.table)

    row_header = ['AR'] + area_rng_name + freq_group_name
    table_data = [row_header]
    for i, num in enumerate(max_dets):
        row = [
            '{:.3f}'.format(val)
            for val in recalls[i, :].tolist()
        ]
        row.insert(0, num)
        table_data.append(row)
    table = AsciiTable(table_data)
    print(table.table)
    def main(self):
        split = self.split
        scene = self.scene  # "all" or a single scene
        sel_scene_ids = self.sel_scene_ids
        data_root = self.data_root

        for scene_id in tqdm(sel_scene_ids, postfix=f"{split}_{scene}"):
            print("split: {} scene: {}".format(split, scene_id))
            scene_root = osp.join(data_root, f"{scene_id:06d}")

            gt_dict = mmcv.load(osp.join(scene_root, "scene_gt.json"))
            # gt_info_dict = mmcv.load(osp.join(scene_root, "scene_gt_info.json"))
            # cam_dict = mmcv.load(osp.join(scene_root, "scene_camera.json"))

            for str_im_id in tqdm(gt_dict, postfix=f"{scene_id}"):
                int_im_id = int(str_im_id)

                for anno_i, anno in enumerate(gt_dict[str_im_id]):
                    obj_id = anno["obj_id"]
                    if obj_id not in idx2class:
                        continue

                    R = np.array(anno["cam_R_m2c"],
                                 dtype="float32").reshape(3, 3)
                    t = np.array(anno["cam_t_m2c"], dtype="float32") / 1000.0
                    # pose = np.hstack([R, t.reshape(3, 1)])

                    save_path = osp.join(
                        xyz_root,
                        f"{scene_id:06d}/{int_im_id:06d}_{anno_i:06d}-xyz.pkl",
                    )
                    # if osp.exists(save_path) and osp.getsize(save_path) > 0:
                    #     continue

                    render_obj_id = cls_indexes.index(obj_id)  # 0-based
                    bgr_gl, depth_gl = self.get_renderer().render(
                        render_obj_id, IM_W, IM_H, K, R, t, near, far)
                    mask = (depth_gl > 0).astype("uint8")

                    if mask.sum(
                    ) == 0:  # NOTE: this should be ignored at training phase
                        print(
                            f"not visible, split {split} scene {scene_id}, im {int_im_id} obj {idx2class[obj_id]} {obj_id}"
                        )
                        print(f"{save_path}")
                        xyz_info = {
                            "xyz_crop": np.zeros((IM_H, IM_W, 3),
                                                 dtype=np.float16),
                            "xyxy": [0, 0, IM_W - 1, IM_H - 1],
                        }
                        if VIS:
                            im_path = osp.join(
                                data_root,
                                f"{scene_id:06d}/rgb/{int_im_id:06d}.jpg",
                            )
                            im = mmcv.imread(im_path)

                            mask_path = osp.join(
                                data_root,
                                f"{scene_id:06d}/mask/{int_im_id:06d}_{anno_i:06d}.png",
                            )
                            mask_visib_path = osp.join(
                                data_root,
                                f"{scene_id:06d}/mask_visib/{int_im_id:06d}_{anno_i:06d}.png",
                            )
                            mask_gt = mmcv.imread(mask_path, "unchanged")
                            mask_visib_gt = mmcv.imread(
                                mask_visib_path, "unchanged")

                            show_ims = [
                                bgr_gl[:, :, [2, 1, 0]],
                                im[:, :, [2, 1, 0]],
                                mask_gt,
                                mask_visib_gt,
                            ]
                            show_titles = [
                                "bgr_gl",
                                "im",
                                "mask_gt",
                                "mask_visib_gt",
                            ]
                            grid_show(show_ims, show_titles, row=2, col=2)
                            raise RuntimeError(
                                f"split {split} scene {scene_id}, im {int_im_id}"
                            )
                    else:
                        x1, y1, x2, y2 = mask2bbox_xyxy(mask)
                        xyz_np = misc.calc_xyz_bp_fast(depth_gl, R, t, K)
                        xyz_crop = xyz_np[y1:y2 + 1, x1:x2 + 1]
                        xyz_info = {
                            "xyz_crop": xyz_crop.astype(
                                "float16"
                            ),  # save disk space w/o performance drop
                            "xyxy": [x1, y1, x2, y2],
                        }

                        if VIS:
                            print(
                                f"xyz_crop min {xyz_crop.min()} max {xyz_crop.max()}"
                            )
                            show_ims = [
                                bgr_gl[:, :, [2, 1, 0]],
                                get_emb_show(xyz_np),
                                get_emb_show(xyz_crop),
                            ]
                            show_titles = ["bgr_gl", "xyz", "xyz_crop"]
                            grid_show(show_ims, show_titles, row=1, col=3)

                    if not args.no_save:
                        mmcv.mkdir_or_exist(osp.dirname(save_path))
                        mmcv.dump(xyz_info, save_path)
        if self.renderer is not None:
            self.renderer.close()
Exemple #11
0
def test_batched_nms():
    try:
        import mmcv
        from mmcv.ops import batched_nms
    except (ImportError, ModuleNotFoundError):
        pytest.skip('test requires compilation')

    # trt config
    os.environ['ONNX_BACKEND'] = 'MMCVTensorRT'
    fp16_mode = False
    max_workspace_size = 1 << 30
    data = mmcv.load('./tests/data/batched_nms_data.pkl')
    nms_cfg = dict(type='nms', iou_threshold=0.7)
    boxes = data['boxes'].cuda()
    scores = data['scores'].cuda()
    idxs = data['idxs'].cuda()
    class_agnostic = False

    nms = partial(batched_nms, nms_cfg=nms_cfg, class_agnostic=class_agnostic)
    wrapped_model = WrapFunction(nms)
    wrapped_model.cpu().eval()
    input_data = (boxes.detach().cpu(), scores.detach().cpu(),
                  idxs.detach().cpu())
    input_names = ['boxes', 'scores', 'idxs']
    output_names = ['dets', 'inds']
    with torch.no_grad():
        torch.onnx.export(wrapped_model,
                          input_data,
                          onnx_file,
                          export_params=True,
                          keep_initializers_as_inputs=True,
                          input_names=input_names,
                          output_names=output_names,
                          opset_version=11)
    onnx_model = onnx.load(onnx_file)
    # create trt engine and wraper
    opt_shape_dict = {
        'boxes': [list(boxes.shape),
                  list(boxes.shape),
                  list(boxes.shape)],
        'scores': [list(scores.shape),
                   list(scores.shape),
                   list(scores.shape)],
        'idxs': [list(idxs.shape),
                 list(idxs.shape),
                 list(idxs.shape)]
    }
    trt_engine = onnx2trt(onnx_model,
                          opt_shape_dict,
                          fp16_mode=fp16_mode,
                          max_workspace_size=max_workspace_size)
    save_trt_engine(trt_engine, trt_file)
    trt_model = TRTWraper(trt_file, input_names, output_names)

    with torch.no_grad():
        trt_outputs = trt_model({
            'boxes': boxes,
            'scores': scores,
            'idxs': idxs
        })
        trt_dets = trt_outputs['dets']
        trt_inds = trt_outputs['inds']
        trt_inds = trt_inds.long()

    # compute pytorch_output
    with torch.no_grad():
        pytorch_outputs = wrapped_model(boxes, scores, idxs)
        pytorch_dets, pytorch_inds = pytorch_outputs
    # allclose
    if os.path.exists(onnx_file):
        os.remove(onnx_file)
    if os.path.exists(trt_file):
        os.remove(trt_file)
    num_boxes = pytorch_dets.shape[0]
    trt_dets = trt_dets[:num_boxes, ...]
    trt_inds = trt_inds[:num_boxes]
    trt_scores = trt_dets[:, 4]
    pytorch_scores = pytorch_dets[:, 4]

    os.environ.pop('ONNX_BACKEND')
    assert torch.allclose(pytorch_scores, trt_scores)
    assert torch.equal(pytorch_inds, trt_inds)
from glob import glob

if __name__ == '__main__':

    parser = argparse.ArgumentParser()
    parser.add_argument('--sub_csv_pattern')
    parser.add_argument('--parents_only', type=int, default=0)
    parser.add_argument('--no_expand', type=int, default=0)
    parser.add_argument('--thres', type=float)
    args = parser.parse_args()

    data_dir = '/Users/bo_liu/Documents/open-images/data/'
    repo_dir = '/Users/bo_liu/Documents/open-images/open-images/'
    sub_dir = '/Users/bo_liu/Documents/open-images/subs/'

    all_keyed_child = mmcv.load(data_dir + 'seg_all_keyed_child.pkl')

    sub_csvs = sorted(glob(args.sub_csv_pattern))

    for sub_csv in sub_csvs:
        assert 'of25.csv' in sub_csv or 'of25_msk_vote' in sub_csv
        if 'of25_msk_vote' in sub_csv:
            k = int(sub_csv.split('of25_msk_vote')[-2].split('_')[-1])
        else:
            k = int(sub_csv.replace('of25.csv', '').split('_')[-1])
        assert k >= 0 and k <= 24

        import gc
        gc.collect()
        sub = pd.read_csv(sub_csv)
    def __init__(self,
                 info_path,
                 data_root,
                 rate,
                 prepare,
                 sample_groups, 
                 classes=None, 
                 attr_classes=None, 
                 points_loader=dict(
                     type='LoadPointsFromFile',
                     coord_type='LIDAR',
                     load_dim=4,
                     use_dim=[0, 1, 2, 3])): # Feng Xiang added attr_classes input
        super().__init__()
        self.data_root = data_root
        self.info_path = info_path
        self.rate = rate
        self.prepare = prepare
        self.classes = classes
        # Feng Xiang code
        # code begin
        self.attr_classes = attr_classes
        # self.attr_cat2label = {name: i for i, name in enumerate(attr_classes)}
        # self.attr_label2cat = {i: name for i, name in enumerate(attr_classes)}
        # print(self.attr_classes)
        # code end
        self.cat2label = {name: i for i, name in enumerate(classes)}
        self.label2cat = {i: name for i, name in enumerate(classes)}
        self.points_loader = mmcv.build_from_cfg(points_loader, PIPELINES)

        db_infos = mmcv.load(info_path)

        # filter database infos
        from mmdet3d.utils import get_root_logger
        logger = get_root_logger()
        for k, v in db_infos.items():
            logger.info(f'load {len(v)} {k} database infos')
        for prep_func, val in prepare.items():
            db_infos = getattr(self, prep_func)(db_infos, val)
        logger.info('After filter database:')
        for k, v in db_infos.items():
            logger.info(f'load {len(v)} {k} database infos')

        self.db_infos = db_infos

        # load sample groups
        # TODO: more elegant way to load sample groups
        self.sample_groups = []
        for name, num in sample_groups.items():
            self.sample_groups.append({name: int(num)})

        self.group_db_infos = self.db_infos  # just use db_infos
        self.sample_classes = []
        # Feng Xiang code
        # code begin
        self.sample_attr_classes = []
        # code end
        self.sample_max_nums = []
        for group_info in self.sample_groups:
            self.sample_classes += list(group_info.keys())
            self.sample_max_nums += list(group_info.values())

        self.sampler_dict = {}
        for k, v in self.group_db_infos.items():
            self.sampler_dict[k] = BatchSampler(v, k, shuffle=True)
Exemple #14
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--ann-file',
                        type=str,
                        default='tests/data/h36m/test_h36m_body3d.npz')
    parser.add_argument('--camera-param-file',
                        type=str,
                        default='tests/data/h36m/cameras.pkl')
    parser.add_argument('--img-root', type=str, default='tests/data/h36m')
    parser.add_argument('--out-file',
                        type=str,
                        default='tests/data/h36m/h36m_coco.json')
    parser.add_argument('--full-img-name', action='store_true')

    args = parser.parse_args()

    h36m_data = np.load(args.ann_file)
    h36m_camera_params = mmcv.load(args.camera_param_file)
    h36m_coco = {}

    # categories
    h36m_cats = [{
        'supercategory':
        'person',
        'id':
        1,
        'name':
        'person',
        'keypoints': [
            'root (pelvis)', 'left_hip', 'left_knee', 'left_foot', 'right_hip',
            'right_knee', 'right_foot', 'spine', 'thorax', 'neck_base', 'head',
            'left_shoulder', 'left_elbow', 'left_wrist', 'right_shoulder',
            'right_elbow', 'right_wrist'
        ],
        'skeleton': [[0, 1], [1, 2], [2, 3], [0, 4], [4, 5], [5, 6], [0, 7],
                     [7, 8], [8, 9], [9, 10], [8, 11], [11, 12], [12, 13],
                     [8, 14], [14, 15], [15, 16]],
    }]

    # images
    imgnames = h36m_data['imgname']
    if not args.full_img_name:
        imgnames = [osp.basename(fn) for fn in imgnames]
    tasks = [(idx, fn, args.img_root) for idx, fn in enumerate(imgnames)]

    h36m_imgs = mmcv.track_parallel_progress(_get_img_info, tasks, nproc=12)

    # annotations
    kpts_2d = h36m_data['part']
    kpts_3d = h36m_data['S']
    centers = h36m_data['center']
    scales = h36m_data['scale']
    tasks = [(idx, ) + args + (h36m_camera_params, )
             for idx, args in enumerate(
                 zip(kpts_2d, kpts_3d, centers, scales, imgnames))]

    h36m_anns = mmcv.track_parallel_progress(_get_ann, tasks, nproc=12)

    # remove invalid data
    h36m_imgs = [img for img in h36m_imgs if img is not None]
    h36m_img_ids = set([img['id'] for img in h36m_imgs])
    h36m_anns = [ann for ann in h36m_anns if ann['image_id'] in h36m_img_ids]

    h36m_coco = {
        'categories': h36m_cats,
        'images': h36m_imgs,
        'annotations': h36m_anns,
    }

    mmcv.dump(h36m_coco, args.out_file)
def parse_results(config_file, resultfile, dstpath, type):
    cfg = Config.fromfile(config_file)

    data_test = cfg.data['test']
    dataset = get_dataset(data_test)
    outputs = mmcv.load(resultfile)
    if type == 'OBB':
        #  dota1 has tested
        obb_results_dict = OBBDetComp4(dataset, outputs)
        current_thresh = 0.1
    elif type == 'HBB':
        # dota1 has tested
        hbb_results_dict = HBBDet2Comp4(dataset, outputs)
    elif type == 'HBBOBB':
        # dota1 has tested
        # dota2
        hbb_results_dict, obb_results_dict = HBBOBB2Comp4(dataset, outputs)
        current_thresh = 0.3
    elif type == 'Mask':
        # TODO: dota1 did not pass
        # dota2, hbb has passed, obb has passed
        hbb_results_dict, obb_results_dict = HBBSeg2Comp4(dataset, outputs)
        current_thresh = 0.3
    #print(obb_results_dict)
    print(len(obb_results_dict))

    dataset_type = cfg.dataset_type

    if 'obb_results_dict' in vars():
        if not os.path.exists(os.path.join(dstpath, 'Task1_results')):
            os.makedirs(os.path.join(dstpath, 'Task1_results'))

        for cls in obb_results_dict:
            with open(os.path.join(dstpath, 'Task1_results', cls + '.txt'),
                      'w') as obb_f_out:
                for index, outline in enumerate(obb_results_dict[cls]):
                    if index != (len(obb_results_dict[cls]) - 1):
                        obb_f_out.write(outline + '\n')
                    else:
                        obb_f_out.write(outline)

        if not os.path.exists(os.path.join(dstpath, 'Task1_results_nms')):
            os.makedirs(os.path.join(dstpath, 'Task1_results_nms'))

        mergebypoly_multiprocess(os.path.join(dstpath, 'Task1_results'),
                                 os.path.join(dstpath, 'Task1_results_nms'),
                                 nms_type=r'py_cpu_nms_poly_fast',
                                 o_thresh=current_thresh)

        OBB2HBB(os.path.join(dstpath, 'Task1_results_nms'),
                os.path.join(dstpath, 'Transed_Task2_results_nms'))

    if 'hbb_results_dict' in vars():
        if not os.path.exists(os.path.join(dstpath, 'Task2_results')):
            os.makedirs(os.path.join(dstpath, 'Task2_results'))
        for cls in hbb_results_dict:
            with open(os.path.join(dstpath, 'Task2_results', cls + '.txt'),
                      'w') as f_out:
                for index, outline in enumerate(hbb_results_dict[cls]):
                    if index != (len(hbb_results_dict[cls]) - 1):
                        f_out.write(outline + '\n')
                    else:
                        f_out.write(outline)

        if not os.path.exists(os.path.join(dstpath, 'Task2_results_nms')):
            os.makedirs(os.path.join(dstpath, 'Task2_results_nms'))
        mergebyrec(os.path.join(dstpath, 'Task2_results'),
                   os.path.join(dstpath, 'Task2_results_nms'))
Exemple #16
0
 def load_annotations(self, ann_file):
     return mmcv.load(ann_file, encoding='latin1')
    def after_train_epoch(self, runner):
        if not self.every_n_epochs(runner, self.interval):
            return

        runner.logger.info(
            "Start Visualizing on {} dataset({} images).".format(
                self.dataset.name, len(self.dataset)))

        # get prog bar
        if runner.rank == 0:
            prog_bar = mmcv.ProgressBar(len(self.dataset))
        else:
            prog_bar = None

        runner.model.eval()
        results = [None for _ in range(len(self.dataset))]
        for idx in range(runner.rank, len(self.dataset), runner.world_size):
            data = self.dataset[idx]
            data_gpu = scatter(collate([data], samples_per_gpu=1),
                               [torch.cuda.current_device()])[0]

            # compute output
            with torch.no_grad():
                result = runner.model(data_gpu)

            # convert result to suitable visualization image
            item = self.dataset.data_list[idx]
            result['leftImage'] = imread(
                osp.join(self.cfg.data.vis.data_root,
                         item['left_image_path'])).astype(np.float32)
            result['rightImage'] = imread(
                osp.join(self.cfg.data.vis.data_root,
                         item['right_image_path'])).astype(np.float32)

            image_name = item['left_image_path'].split('/')[-1]
            result = prepare_visualize(result, runner.epoch + 1,
                                       self.cfg.work_dir, image_name)

            results[idx] = result

            batch_size = runner.world_size
            if runner.rank == 0:
                for _ in range(batch_size):
                    prog_bar.update()

        if runner.rank == 0:
            print('\n')
            dist.barrier()
            for i in range(1, min(runner.world_size, len(self.dataset))):
                tmp_file = osp.join(runner.work_dir, 'temp_{}.pkl'.format(i))
                tmp_results = mmcv.load(tmp_file)
                for idx in range(i, len(results), runner.world_size):
                    results[idx] = tmp_results[idx]
                os.remove(tmp_file)
            self.visualize(runner, results)
        else:
            tmp_file = osp.join(runner.work_dir,
                                'temp_{}.pkl'.format(runner.rank))
            mmcv.dump(results, tmp_file)
            dist.barrier()

        dist.barrier()
        torch.cuda.empty_cache()
Exemple #18
0
    def __init__(self,
                 ann_file,
                 img_prefix,
                 img_scale,
                 img_norm_cfg,
                 multiscale_mode='value',
                 size_divisor=None,
                 flip_ratio=0,
                 with_ignore=False,
                 extra_aug=None,
                 resize_keep_ratio=True,
                 sample_file=None,
                 num_classes=8,
                 test_mode=False):
        super(Vision3DDataset, self).__init__()
        # prefix of images path
        self.img_prefix = img_prefix
        self.name = osp.basename(ann_file).split('.')[0]

        # load annotations (and proposals)
        self.raw_annotations = self.load_annotations(ann_file)

        # support dict or list
        if isinstance(self.raw_annotations, list):
            self.ids = range(len(self.raw_annotations))
        elif isinstance(self.raw_annotations, dict):
            if sample_file is not None and osp.isfile(sample_file):
                self.ids = mmcv.load(sample_file, encoding='latin1')
            else:
                self.ids = sorted(list(self.raw_annotations.keys()))
        else:
            raise Exception("Unrecognized type of annotations: {}".format(
                type(self.raw_annotations)))

        # filter images with no annotation during training
        if not test_mode:
            self._filter_imgs()

        # (long_edge, short_edge) or [(long1, short1), (long2, short2), ...]
        self.img_scales = img_scale if isinstance(img_scale,
                                                  list) else [img_scale]
        assert mmcv.is_list_of(self.img_scales, tuple)
        # normalization configs
        self.img_norm_cfg = img_norm_cfg

        # multi-scale mode (only applicable for multi-scale training)
        self.multiscale_mode = multiscale_mode
        assert multiscale_mode in ['value', 'range']

        # flip ratio
        self.flip_ratio = flip_ratio
        assert flip_ratio >= 0 and flip_ratio <= 1
        # padding border to ensure the image size can be divided by
        # size_divisor (used for FPN)
        self.size_divisor = size_divisor

        # some datasets provide bbox annotations as ignore/crowd/difficult,
        self.with_ignore = with_ignore

        # in test mode or not
        self.test_mode = test_mode

        # set group flag for the sampler
        if not self.test_mode:
            self._set_group_flag()
        # transforms
        self.img_transform = ImageTransform(
            size_divisor=self.size_divisor, **self.img_norm_cfg)
        self.point_transform = PointTransform()
        self.numpy2tensor = Numpy2Tensor()

        # if use extra augmentation
        if extra_aug is not None:
            self.extra_aug = ExtraAugmentation(**extra_aug)
        else:
            self.extra_aug = None

        # image rescale if keep ratio
        self.resize_keep_ratio = resize_keep_ratio

        self.num_classes = num_classes
def inference_multi_modality_detector(model, pcd, image, ann_file):
    """Inference point cloud with the multimodality detector.

    Args:
        model (nn.Module): The loaded detector.
        pcd (str): Point cloud files.
        image (str): Image files.
        ann_file (str): Annotation files.

    Returns:
        tuple: Predicted results and data from pipeline.
    """
    cfg = model.cfg
    device = next(model.parameters()).device  # model device
    # build the data pipeline
    test_pipeline = deepcopy(cfg.data.test.pipeline)
    test_pipeline = Compose(test_pipeline)
    box_type_3d, box_mode_3d = get_box_type(cfg.data.test.box_type_3d)
    # get data info containing calib
    data_infos = mmcv.load(ann_file)
    image_idx = int(re.findall(r'\d+', image)[-1])  # xxx/sunrgbd_000017.jpg
    for x in data_infos:
        if int(x['image']['image_idx']) != image_idx:
            continue
        info = x
        break
    data = dict(
        pts_filename=pcd,
        img_prefix=osp.dirname(image),
        img_info=dict(filename=osp.basename(image)),
        box_type_3d=box_type_3d,
        box_mode_3d=box_mode_3d,
        img_fields=[],
        bbox3d_fields=[],
        pts_mask_fields=[],
        pts_seg_fields=[],
        bbox_fields=[],
        mask_fields=[],
        seg_fields=[])

    # depth map points to image conversion
    if box_mode_3d == Box3DMode.DEPTH:
        data.update(dict(calib=info['calib']))

    data = test_pipeline(data)

    # LiDAR to image conversion
    if box_mode_3d == Box3DMode.LIDAR:
        rect = info['calib']['R0_rect'].astype(np.float32)
        Trv2c = info['calib']['Tr_velo_to_cam'].astype(np.float32)
        P2 = info['calib']['P2'].astype(np.float32)
        lidar2img = P2 @ rect @ Trv2c
        data['img_metas'][0].data['lidar2img'] = lidar2img
    elif box_mode_3d == Box3DMode.DEPTH:
        data['calib'][0]['Rt'] = data['calib'][0]['Rt'].astype(np.float32)
        data['calib'][0]['K'] = data['calib'][0]['K'].astype(np.float32)

    data = collate([data], samples_per_gpu=1)
    if next(model.parameters()).is_cuda:
        # scatter to specified GPU
        data = scatter(data, [device.index])[0]
    else:
        # this is a workaround to avoid the bug of MMDataParallel
        data['img_metas'] = data['img_metas'][0].data
        data['points'] = data['points'][0].data
        data['img'] = data['img'][0].data
        if box_mode_3d == Box3DMode.DEPTH:
            data['calib'][0]['Rt'] = data['calib'][0]['Rt'][0].data
            data['calib'][0]['K'] = data['calib'][0]['K'][0].data

    # forward the model
    with torch.no_grad():
        result = model(return_loss=False, rescale=True, **data)
    return result, data
def get_distortions_from_file(filename):

    eval_output = mmcv.load(filename)

    return get_distortions_from_results(eval_output)
Exemple #21
0
def main():
    args = parse_args()

    assert args.out or args.show or args.json_out, \
        ('Please specify at least one operation (save or show the results) '
         'with the argument "--out" or "--show" or "--json_out"')

    if args.out is not None and not args.out.endswith(('.pkl', '.pickle')):
        raise ValueError('The output file must be a pkl file.')

    if args.json_out is not None and args.json_out.endswith('.json'):
        args.json_out = args.json_out[:-5]

    cfg = mmcv.Config.fromfile(args.config)
    # set cudnn_benchmark
    if cfg.get('cudnn_benchmark', False):
        torch.backends.cudnn.benchmark = True
    cfg.model.pretrained = None
    cfg.data.test.test_mode = True

    # init distributed env first, since logger depends on the dist info.
    if args.launcher == 'none':
        distributed = False
    else:
        distributed = True
        init_dist(args.launcher, **cfg.dist_params)

    # build the dataloader
    # TODO: support multiple images per gpu (only minor changes are needed)
    dataset = build_dataset(cfg.data.test)
    ## uncomment to only eval on first 100 imgs
    # dataset.img_infos = dataset.img_infos[:100]
    data_loader = build_dataloader(dataset,
                                   imgs_per_gpu=1,
                                   workers_per_gpu=cfg.data.workers_per_gpu,
                                   dist=distributed,
                                   shuffle=False)

    # build the model and load checkpoint
    model = build_detector(cfg.model, train_cfg=None, test_cfg=cfg.test_cfg)
    fp16_cfg = cfg.get('fp16', None)
    if fp16_cfg is not None:
        wrap_fp16_model(model)
    checkpoint = load_checkpoint(model, args.checkpoint, map_location='cpu')
    # old versions did not save class info in checkpoints, this walkaround is
    # for backward compatibility
    if 'CLASSES' in checkpoint['meta']:
        model.CLASSES = checkpoint['meta']['CLASSES']
    else:
        model.CLASSES = dataset.CLASSES

    if args.existing_json:
        result_files = {
            'bbox': './coco_maskrcnn_r50fpn.pkl.bbox.json',
            'segm': './coco_maskrcnn_r50fpn.pkl.segm.json',
            'proposal': './coco_maskrcnn_r50fpn.pkl.proposal.json'
        }
        eval_types = ['proposal_fast']
        result_files = mmcv.load('./debug_coco_maskrcnn_r50fpn.pkl')
        result_files = [item[2] for item in result_files]
        coco_eval(result_files, eval_types, dataset.coco)
        exit()

    if not distributed:
        model = MMDataParallel(model, device_ids=[0])
        outputs = single_gpu_test(model, data_loader, args.show)
    else:
        model = MMDistributedDataParallel(model.cuda())
        outputs = multi_gpu_test(model, data_loader, args.tmpdir)

    rank, _ = get_dist_info()
    if args.out and rank == 0:
        print('\nwriting results to {}'.format(args.out))
        mmcv.dump(outputs, args.out)
        eval_types = args.eval
        if eval_types:
            print('Starting evaluate {}'.format(' and '.join(eval_types)))
            if eval_types == ['proposal_fast']:
                result_file = args.out
                coco_eval(result_file, eval_types, dataset.coco)
            else:
                if not isinstance(outputs[0], dict):
                    result_files = results2json(dataset, outputs, args.out)
                    coco_eval(result_files, eval_types, dataset.coco)
                else:
                    for name in outputs[0]:
                        print('\nEvaluating {}'.format(name))
                        outputs_ = [out[name] for out in outputs]
                        result_file = args.out + '.{}'.format(name)
                        result_files = results2json(dataset, outputs_,
                                                    result_file)
                        coco_eval(result_files, eval_types, dataset.coco)

    # Save predictions in the COCO json format
    if args.json_out and rank == 0:
        if not isinstance(outputs[0], dict):
            results2json(dataset, outputs, args.json_out)
        else:
            for name in outputs[0]:
                outputs_ = [out[name] for out in outputs]
                result_file = args.json_out + '.{}'.format(name)
                results2json(dataset, outputs_, result_file)
def get_coco_style_results(filename,
                           task='bbox',
                           metric=None,
                           prints='mPC',
                           aggregate='benchmark'):

    assert aggregate in ['benchmark', 'all']

    if prints == 'all':
        prints = ['P', 'mPC', 'rPC']
    elif isinstance(prints, str):
        prints = [prints]
    for p in prints:
        assert p in ['P', 'mPC', 'rPC']

    if metric is None:
        metrics = [
            'AP', 'AP50', 'AP75', 'APs', 'APm', 'APl', 'AR1', 'AR10', 'AR100',
            'ARs', 'ARm', 'ARl'
        ]
    elif isinstance(metric, list):
        metrics = metric
    else:
        metrics = [metric]

    for metric_name in metrics:
        assert metric_name in [
            'AP', 'AP50', 'AP75', 'APs', 'APm', 'APl', 'AR1', 'AR10', 'AR100',
            'ARs', 'ARm', 'ARl'
        ]

    eval_output = mmcv.load(filename)

    num_distortions = len(list(eval_output.keys()))
    results = np.zeros((num_distortions, 6, len(metrics)), dtype='float32')

    for corr_i, distortion in enumerate(eval_output):
        for severity in eval_output[distortion]:
            for metric_j, metric_name in enumerate(metrics):
                mAP = eval_output[distortion][severity][task][metric_name]
                results[corr_i, severity, metric_j] = mAP

    P = results[0, 0, :]
    if aggregate == 'benchmark':
        mPC = np.mean(results[:15, 1:, :], axis=(0, 1))
    else:
        mPC = np.mean(results[:, 1:, :], axis=(0, 1))
    rPC = mPC / P

    print('\nmodel: {}'.format(osp.basename(filename)))
    if metric is None:
        if 'P' in prints:
            print('Performance on Clean Data [P] ({})'.format(task))
            print_coco_results(P)
        if 'mPC' in prints:
            print('Mean Performance under Corruption [mPC] ({})'.format(task))
            print_coco_results(mPC)
        if 'rPC' in prints:
            print('Realtive Performance under Corruption [rPC] ({})'.format(
                task))
            print_coco_results(rPC)
    else:
        if 'P' in prints:
            print('Performance on Clean Data [P] ({})'.format(task))
            for metric_i, metric_name in enumerate(metrics):
                print('{:5} =  {:0.3f}'.format(metric_name, P[metric_i]))
        if 'mPC' in prints:
            print('Mean Performance under Corruption [mPC] ({})'.format(task))
            for metric_i, metric_name in enumerate(metrics):
                print('{:5} =  {:0.3f}'.format(metric_name, mPC[metric_i]))
        if 'rPC' in prints:
            print('Relative Performance under Corruption [rPC] ({})'.format(
                task))
            for metric_i, metric_name in enumerate(metrics):
                print('{:5} => {:0.1f} %'.format(metric_name,
                                                 rPC[metric_i] * 100))

    return results
Exemple #23
0
def main():
    args = parse_args()

    assert args.out or args.show, \
        ('Please specify at least one operation (save or show the results) '
         'with the argument "--out" or "--show"')

    if args.out is not None and not args.out.endswith(('.pkl', '.pickle')):
        raise ValueError('The output file must be a pkl file.')

    cfg = mmcv.Config.fromfile(args.config)
    # set cudnn_benchmark
    if cfg.get('cudnn_benchmark', False):
        torch.backends.cudnn.benchmark = True
    cfg.model.pretrained = None
    cfg.data.test.test_mode = True

    # init distributed env first, since logger depends on the dist info.
    if args.launcher == 'none':
        distributed = False
    else:
        distributed = True
        init_dist(args.launcher, **cfg.dist_params)

    # build the dataloader
    # TODO: support multiple images per gpu (only minor changes are needed)
    dataset = build_dataset(cfg.data.test)
    # data_loader = build_dataloader(
    #     dataset,
    #     imgs_per_gpu=1,
    #     workers_per_gpu=cfg.data.workers_per_gpu,
    #     dist=distributed,
    #     shuffle=False)
    #
    # # build the model and load checkpoint
    # model = build_detector(cfg.model, train_cfg=None, test_cfg=cfg.test_cfg)
    # fp16_cfg = cfg.get('fp16', None)
    # if fp16_cfg is not None:
    #     wrap_fp16_model(model)
    # checkpoint = load_checkpoint(model, args.checkpoint, map_location='cpu')
    # if args.det_ckpt is not None:
    #     print('Loading Detection Models!!!!!!')
    #     det_ckpt = load_checkpoint(model, args.det_ckpt, map_location='cpu')
    # # old versions did not save class info in checkpoints, this walkaround is
    # # for backward compatibility
    # if 'CLASSES' in checkpoint['meta']:
    #     model.CLASSES = checkpoint['meta']['CLASSES']
    # else:
    #     model.CLASSES = dataset.CLASSES
    #
    # if not distributed:
    #     model = MMDataParallel(model, device_ids=[0])
    #     outputs = single_gpu_test(model, data_loader, args.show)
    # else:
    #     model = MMDistributedDataParallel(model.cuda())
    #     outputs = multi_gpu_test(model, data_loader, args.tmpdir)

    outputs = mmcv.load(args.out)
    print('Finding video tubes...')
    track_results = finding_video_tubes(outputs, dataset)
    print('Finding video tubes done!')
    # draw_results(track_results, dataset, args.out, cfg)

    rank, _ = get_dist_info()
    if args.out and rank == 0:
        # print('\nwriting results to {}'.format(args.out))
        # mmcv.dump(outputs, args.out)
        # result_files = results2json(dataset, outputs['bbox_results'], args.out)
        # coco_eval(result_files, ['bbox'], cfg.data.test.ann_file)
        # For tracking
        # result_files = results2json(dataset, track_results['bbox_results'],
        #                             args.out)
        # coco_eval(result_files, ['bbox'], cfg.data.test.ann_file)
        print("Evaluating tracking...")
        mdat_eval(track_results['track_results'], dataset, args.out, cfg)
Exemple #24
0
# Copyright (c) OpenMMLab. All rights reserved.
# In this example, we convert babel120_train to MMAction2 format
# The required files can be downloaded from the homepage of BABEL project
import numpy as np
from mmcv import dump, load


def gen_babel(x, y):
    data = []
    for i, xx in enumerate(x):
        sample = dict()
        sample['keypoint'] = xx.transpose(3, 1, 2, 0).astype(np.float16)
        sample['label'] = y[1][0][i]
        names = [y[0][i], y[1][1][i], y[1][2][i], y[1][3][i]]
        sample['frame_dir'] = '_'.join([str(k) for k in names])
        sample['total_frames'] = 150
        data.append(sample)
    return data


x = np.load('train_ntu_sk_120.npy')
y = load('train_label_120.pkl')

data = gen_babel(x, y)
dump(data, 'babel120_train.pkl')
Exemple #25
0
def test_global_rot_scale_trans():
    angle = 0.78539816
    scale = [0.95, 1.05]
    trans_std = 1.0

    # rot_range should be a number or seq of numbers
    with pytest.raises(AssertionError):
        global_rot_scale_trans = GlobalRotScaleTrans(rot_range='0.0')

    # scale_ratio_range should be seq of numbers
    with pytest.raises(AssertionError):
        global_rot_scale_trans = GlobalRotScaleTrans(scale_ratio_range=1.0)

    # translation_std should be a positive number or seq of positive numbers
    with pytest.raises(AssertionError):
        global_rot_scale_trans = GlobalRotScaleTrans(translation_std='0.0')
    with pytest.raises(AssertionError):
        global_rot_scale_trans = GlobalRotScaleTrans(translation_std=-1.0)

    global_rot_scale_trans = GlobalRotScaleTrans(rot_range=angle,
                                                 scale_ratio_range=scale,
                                                 translation_std=trans_std,
                                                 shift_height=False)

    np.random.seed(0)
    points = np.fromfile('tests/data/scannet/points/scene0000_00.bin',
                         np.float32).reshape(-1, 6)
    annos = mmcv.load('tests/data/scannet/scannet_infos.pkl')
    info = annos[0]
    gt_bboxes_3d = info['annos']['gt_boxes_upright_depth']

    depth_points = DepthPoints(points.copy(),
                               points_dim=6,
                               attribute_dims=dict(color=[3, 4, 5]))
    gt_bboxes_3d = DepthInstance3DBoxes(gt_bboxes_3d.copy(),
                                        box_dim=gt_bboxes_3d.shape[-1],
                                        with_yaw=False,
                                        origin=(0.5, 0.5, 0.5))

    input_dict = dict(points=depth_points.clone(),
                      bbox3d_fields=['gt_bboxes_3d'],
                      gt_bboxes_3d=gt_bboxes_3d.clone())

    input_dict = global_rot_scale_trans(input_dict)
    trans_depth_points = input_dict['points']
    trans_bboxes_3d = input_dict['gt_bboxes_3d']

    noise_rot = 0.07667607233534723
    scale_factor = 1.021518936637242
    trans_factor = np.array([0.97873798, 2.2408932, 1.86755799])

    true_depth_points = depth_points.clone()
    true_bboxes_3d = gt_bboxes_3d.clone()
    true_depth_points, noise_rot_mat_T = true_bboxes_3d.rotate(
        noise_rot, true_depth_points)
    true_bboxes_3d.scale(scale_factor)
    true_bboxes_3d.translate(trans_factor)
    true_depth_points.scale(scale_factor)
    true_depth_points.translate(trans_factor)

    assert torch.allclose(trans_depth_points.tensor,
                          true_depth_points.tensor,
                          atol=1e-6)
    assert torch.allclose(trans_bboxes_3d.tensor,
                          true_bboxes_3d.tensor,
                          atol=1e-6)
    assert input_dict['pcd_scale_factor'] == scale_factor
    assert torch.allclose(input_dict['pcd_rotation'],
                          noise_rot_mat_T,
                          atol=1e-6)
    assert np.allclose(input_dict['pcd_trans'], trans_factor)

    repr_str = repr(global_rot_scale_trans)
    expected_repr_str = f'GlobalRotScaleTrans(rot_range={[-angle, angle]},' \
                        f' scale_ratio_range={scale},' \
                        f' translation_std={[trans_std for _ in range(3)]},' \
                        f' shift_height=False)'
    assert repr_str == expected_repr_str

    # points with shift_height but no bbox
    global_rot_scale_trans = GlobalRotScaleTrans(rot_range=angle,
                                                 scale_ratio_range=scale,
                                                 translation_std=trans_std,
                                                 shift_height=True)

    # points should have height attribute when shift_height=True
    with pytest.raises(AssertionError):
        input_dict = global_rot_scale_trans(input_dict)

    np.random.seed(0)
    shift_height = points[:, 2:3] * 0.99
    points = np.concatenate([points, shift_height], axis=1)
    depth_points = DepthPoints(points.copy(),
                               points_dim=7,
                               attribute_dims=dict(color=[3, 4, 5], height=6))

    input_dict = dict(points=depth_points.clone(), bbox3d_fields=[])

    input_dict = global_rot_scale_trans(input_dict)
    trans_depth_points = input_dict['points']
    true_shift_height = shift_height * scale_factor

    assert np.allclose(
        trans_depth_points.tensor.numpy(),
        np.concatenate([true_depth_points.tensor.numpy(), true_shift_height],
                       axis=1),
        atol=1e-6)
Exemple #26
0
def main():
    args = parse_args()

    if 'cuda' in args.device.lower():
        if torch.cuda.is_available():
            with_cuda = True
        else:
            raise RuntimeError('No CUDA device found, please check it again.')
    else:
        with_cuda = False

    if args.root_work_dir is None:
        # get the current time stamp
        now = datetime.now()
        ts = now.strftime('%Y_%m_%d_%H_%M')
        args.root_work_dir = f'work_dirs/inference_speed_test_{ts}'
    mmcv.mkdir_or_exist(osp.abspath(args.root_work_dir))

    cfg = mmcv.load(args.config)
    dummy_datasets = mmcv.load(args.dummy_dataset_config)['dummy_datasets']

    results = []
    for i in range(args.priority + 1):
        models = cfg['model_list'][f'P{i}']
        for cur_model in models:
            cfg_file = cur_model['config']
            model_cfg = Config.fromfile(cfg_file)
            test_dataset = model_cfg['data']['test']
            dummy_dataset = dummy_datasets[test_dataset['type']]
            test_dataset.update(dummy_dataset)

            dataset = build_dataset(test_dataset)
            data_loader = build_dataloader(
                dataset,
                samples_per_gpu=args.batch_size,
                workers_per_gpu=model_cfg.data.workers_per_gpu,
                dist=False,
                shuffle=False)
            data_loader = IterLoader(data_loader)

            if 'pretrained' in model_cfg.model.keys():
                del model_cfg.model['pretrained']

            model = init_pose_model(model_cfg, device=args.device.lower())

            fp16_cfg = model_cfg.get('fp16', None)
            if fp16_cfg is not None:
                wrap_fp16_model(model)
            if args.fuse_conv_bn:
                model = fuse_conv_bn(model)

            # benchmark with several iterations and take the average
            pure_inf_time = 0
            speed = []
            for iteration in range(args.num_iters + args.num_warmup):
                data = next(data_loader)
                data['img'] = data['img'].to(args.device.lower())
                data['img_metas'] = data['img_metas'].data[0]

                if with_cuda:
                    torch.cuda.synchronize()

                start_time = time.perf_counter()
                with torch.no_grad():
                    model(return_loss=False, **data)

                if with_cuda:
                    torch.cuda.synchronize()
                elapsed = time.perf_counter() - start_time

                if iteration >= args.num_warmup:
                    pure_inf_time += elapsed
                    speed.append(1 / elapsed)

            speed_mean = np.mean(speed)
            speed_std = np.std(speed)

            split_line = '=' * 30
            result = f'{split_line}\nModel config:{cfg_file}\n' \
                     f'Device: {args.device}\n' \
                     f'Batch size: {args.batch_size}\n' \
                     f'Overall average speed: {speed_mean:.2f} \u00B1 ' \
                     f'{speed_std:.2f} items / s\n' \
                     f'Total iters: {args.num_iters}\n'\
                     f'Total time: {pure_inf_time:.2f} s \n{split_line}\n'\

            print(result)
            results.append(result)

    print('!!!Please be cautious if you use the results in papers. '
          'You may need to check if all ops are included and verify that the '
          'speed computation is correct.')
    with open(osp.join(args.root_work_dir, 'inference_speed.txt'), 'w') as f:
        for res in results:
            f.write(res)
Exemple #27
0
 def load_annotations(self, ann_file):
     return mmcv.load(ann_file)
Exemple #28
0
    def _file2dict(filename, use_predefined_variables=True):
        filename = osp.abspath(osp.expanduser(filename))
        check_file_exist(filename)
        fileExtname = osp.splitext(filename)[1]
        if fileExtname not in ['.py', '.json', '.yaml', '.yml']:
            raise IOError('Only py/yml/yaml/json type are supported now!')

        with tempfile.TemporaryDirectory() as temp_config_dir:
            temp_config_file = tempfile.NamedTemporaryFile(
                dir=temp_config_dir, suffix=fileExtname)
            if platform.system() == 'Windows':
                temp_config_file.close()
            temp_config_name = osp.basename(temp_config_file.name)
            # Substitute predefined variables
            if use_predefined_variables:
                Config._substitute_predefined_vars(filename,
                                                   temp_config_file.name)
            else:
                shutil.copyfile(filename, temp_config_file.name)
            # Substitute base variables from placeholders to strings
            base_var_dict = Config._pre_substitute_base_vars(
                temp_config_file.name, temp_config_file.name)

            if filename.endswith('.py'):
                temp_module_name = osp.splitext(temp_config_name)[0]
                sys.path.insert(0, temp_config_dir)
                Config._validate_py_syntax(filename)
                mod = import_module(temp_module_name)
                sys.path.pop(0)
                cfg_dict = {
                    name: value
                    for name, value in mod.__dict__.items()
                    if not name.startswith('__')
                }
                # delete imported module
                del sys.modules[temp_module_name]
            elif filename.endswith(('.yml', '.yaml', '.json')):
                import mmcv
                cfg_dict = mmcv.load(temp_config_file.name)
            # close temp file
            temp_config_file.close()

        cfg_text = filename + '\n'
        with open(filename, 'r', encoding='utf-8') as f:
            # Setting encoding explicitly to resolve coding issue on windows
            cfg_text += f.read()

        if BASE_KEY in cfg_dict:
            cfg_dir = osp.dirname(filename)
            base_filename = cfg_dict.pop(BASE_KEY)
            base_filename = base_filename if isinstance(
                base_filename, list) else [base_filename]

            cfg_dict_list = list()
            cfg_text_list = list()
            for f in base_filename:
                _cfg_dict, _cfg_text = Config._file2dict(osp.join(cfg_dir, f))
                cfg_dict_list.append(_cfg_dict)
                cfg_text_list.append(_cfg_text)

            base_cfg_dict = dict()
            for c in cfg_dict_list:
                if len(base_cfg_dict.keys() & c.keys()) > 0:
                    raise KeyError('Duplicate key is not allowed among bases')
                base_cfg_dict.update(c)

            # Subtitute base variables from strings to their actual values
            cfg_dict = Config._substitute_base_vars(cfg_dict, base_var_dict,
                                                    base_cfg_dict)

            base_cfg_dict = Config._merge_a_into_b(cfg_dict, base_cfg_dict)
            cfg_dict = base_cfg_dict

            # merge cfg_text
            cfg_text_list.append(cfg_text)
            cfg_text = '\n'.join(cfg_text_list)

        return cfg_dict, cfg_text
Exemple #29
0
 def load_annotations(self, ann_file):
     """Load annotation from annotation file."""
     return mmcv.load(ann_file)
Exemple #30
0
            for name in os.listdir(src_):
                src_f = os.path.join(src_, name)
                dst_f = os.path.join(dst, name)
                shutil.copyfile(src_f, dst_f)


if __name__ == "__main__":

    data_root = "../data/"
    print("generate normal image dir")
    moveFiles(data_root + 'normal_image', [
        data_root + 'guangdong1_round1_train1_20190818/normal_Images',
        data_root + 'guangdong1_round1_train2_20190828/normal_Images'
    ])
    print('done')
    class_to_ind = mmcv.load("./source/cls2ind.pkl")
    ind_to_class = mmcv.load("./source/ind2cls.pkl")
    a = mmcv.load(data_root + "guangdong1_round1_train1_20190818/" +
                  "Annotations/anno_train.json")
    for i in range(len(a)):
        a[i][
            "dir"] = data_root + "guangdong1_round1_train1_20190818/" + "defect_Images/" + a[
                i]['name']
    b = mmcv.load(data_root + "guangdong1_round1_train2_20190828/" +
                  "Annotations/anno_train.json")
    for i in range(len(b)):
        b[i][
            "dir"] = data_root + "guangdong1_round1_train2_20190828/" + "defect_Images/" + b[
                i]['name']
    annos = a + b
    data_dir = "../data"