Exemple #1
0
def main():
    # Initialize C2
    workspace.GlobalInit(
        ['caffe2', '--caffe2_log_level=0', '--caffe2_gpu_memory_tracking=1']
    )
    # Set up logging and load config options
    logger = setup_logging(__name__)
    logging.getLogger('detectron.roi_data.loader').setLevel(logging.INFO)
    args = parse_args()
    logger.info('Called with args:')
    logger.info(args)
    if args.cfg_file is not None:
        merge_cfg_from_file(args.cfg_file)
    if args.opts is not None:
        merge_cfg_from_list(args.opts)
    assert_and_infer_cfg()
    smi_output, cuda_ver, cudnn_ver = c2_utils.get_nvidia_info()
    logger.info("cuda version : {}".format(cuda_ver))
    logger.info("cudnn version: {}".format(cudnn_ver))
    logger.info("nvidia-smi output:\n{}".format(smi_output))
    logger.info('Training with config:')
    logger.info(pprint.pformat(cfg))
    # Note that while we set the numpy random seed network training will not be
    # deterministic in general. There are sources of non-determinism that cannot
    # be removed with a reasonble execution-speed tradeoff (such as certain
    # non-deterministic cudnn functions).
    np.random.seed(cfg.RNG_SEED)
    # Execute the training run
    checkpoints = detectron.utils.train.train_model()
    # Test the trained model
    if not args.skip_test:
        test_model(checkpoints['final'], args.multi_gpu_testing, args.opts)
Exemple #2
0
def main():
    workspace.GlobalInit(['caffe2', '--caffe2_log_level=0'])
    args = parse_args()
    logger.info('Called with args:')
    logger.info(args)
    if args.cfg_file is not None:
        merge_cfg_from_file(args.cfg_file)
    if args.opts is not None:
        merge_cfg_from_list(args.opts)
    cfg.NUM_GPUS = 1
    assert_and_infer_cfg()
    logger.info('Conerting model with config:')
    logger.info(pprint.pformat(cfg))

    assert not cfg.MODEL.KEYPOINTS_ON, "Keypoint model not supported."
    assert not cfg.MODEL.MASK_ON, "Mask model not supported."
    assert not cfg.FPN.FPN_ON, "FPN not supported."
    assert not cfg.RETINANET.RETINANET_ON, "RetinaNet model not supported."

    # load model from cfg
    model, blobs = load_model(args)

    net = core.Net('')
    net.Proto().op.extend(copy.deepcopy(model.net.Proto().op))
    net.Proto().external_input.extend(
        copy.deepcopy(model.net.Proto().external_input))
    net.Proto().external_output.extend(
        copy.deepcopy(model.net.Proto().external_output))
    net.Proto().type = args.net_execution_type
    net.Proto().num_workers = 1 if args.net_execution_type == 'simple' else 4

    # Reset the device_option, change to unscope name and replace python operators
    convert_net(args, net.Proto(), blobs)

    # add operators for bbox
    add_bbox_ops(args, net, blobs)

    if args.fuse_af:
        print('Fusing affine channel...')
        net, blobs = mutils.fuse_net_affine(
            net, blobs)

    if args.use_nnpack:
        mutils.update_mobile_engines(net.Proto())

    # generate init net
    empty_blobs = ['data', 'im_info']
    init_net = gen_init_net(net, blobs, empty_blobs)

    if args.device == 'gpu':
        [net, init_net] = convert_model_gpu(args, net, init_net)

    net.Proto().name = args.net_name
    init_net.Proto().name = args.net_name + "_init"

    if args.test_img is not None:
        verify_model(args, [net, init_net], args.test_img)

    _save_models(net, init_net, args)
Exemple #3
0
def main(args):
    logger = logging.getLogger(__name__)
    merge_cfg_from_file(args.cfg)
    cfg.NUM_GPUS = 1
    args.weights = cache_url(args.weights, cfg.DOWNLOAD_CACHE)
    assert_and_infer_cfg(cache_urls=False)
    model = model_engine.initialize_model_from_cfg(args.weights)
    start = timeit.default_timer()

    if os.path.isdir(args.im_or_folder):
        im_list = glob.iglob(args.im_or_folder + '/*.' + args.image_ext)
    else:
        im_list = [args.im_or_folder]

    # extract bboxes from bottom-up attention model
    image_bboxes = {}
    if args.bbox_file is not None:
        image_bboxes = extract_bboxes(args.bbox_file)

    count = 0
    if not os.path.exists(args.output_dir):
        os.makedirs(args.output_dir)

    for i, im_name in enumerate(im_list):
        im_base_name = os.path.basename(im_name)
        image_id = int(im_base_name.split(".")[0].split("_")[-1])  # for COCO
        if image_id % args.total_group == args.group_id:
            bbox = image_bboxes[image_id] if image_id in image_bboxes else None
            im = cv2.imread(im_name)
            if im is not None:
                outfile = os.path.join(args.output_dir,
                                       im_base_name.replace('jpg', 'npy'))
                lock_folder = outfile.replace('npy', 'lock')
                if not os.path.exists(lock_folder) and os.path.exists(outfile):
                    continue
                if not os.path.exists(lock_folder):
                    os.makedirs(lock_folder)

                result = get_detections_from_im(cfg,
                                                model,
                                                im,
                                                image_id,
                                                args.feat_name,
                                                args.min_bboxes,
                                                args.max_bboxes,
                                                bboxes=bbox)

                np.save(outfile, result)
                os.rmdir(lock_folder)

            count += 1

            if count % 100 == 0:
                end = timeit.default_timer()
                epoch_time = end - start
                print('process {:d} images after {:.1f} s'.format(
                    count, epoch_time))
Exemple #4
0
def main():
    # Initialize C2
    workspace.GlobalInit(
        ['caffe2', '--caffe2_log_level=0', '--caffe2_gpu_memory_tracking=1']
    )
    # Set up logging and load config options
    logger = setup_logging(__name__)
    logging.getLogger('detectron.roi_data.loader').setLevel(logging.INFO)
    args = parse_args()
    logger.info('Called with args:')
    logger.info(args)
    if args.cfg_file is not None:
        merge_cfg_from_file(args.cfg_file)
    if args.opts is not None:
        merge_cfg_from_list(args.opts)
    assert_and_infer_cfg()
    smi_output, cuda_ver, cudnn_ver = c2_utils.get_nvidia_info()
    logger.info("cuda version : {}".format(cuda_ver))
    logger.info("cudnn version: {}".format(cudnn_ver))
    logger.info("nvidia-smi output:\n{}".format(smi_output))
    logger.info('Training with config:')
    logger.info(pprint.pformat(cfg))
    # Note that while we set the numpy random seed network training will not be
    # deterministic in general. There are sources of non-determinism that cannot
    # be removed with a reasonble execution-speed tradeoff (such as certain
    # non-deterministic cudnn functions).
    np.random.seed(cfg.RNG_SEED)
    # Execute the training run
    checkpoints = detectron.utils.train_wsl.train_model()
    # Test the trained model
    if not args.skip_test:
        test_model(checkpoints['final'], args.multi_gpu_testing, args.opts)
        print('reprint snapshot name for the result: ', checkpoints['final'])

        if 'voc' in cfg.TRAIN.DATASETS[0]:
            TEST_DATASETS = cfg.TEST.DATASETS
            TEST_PROPOSAL_FILES = cfg.TEST.PROPOSAL_FILES
            cfg.immutable(False)
            cfg.TEST.DATASETS = cfg.TRAIN.DATASETS
            cfg.TEST.PROPOSAL_FILES = cfg.TRAIN.PROPOSAL_FILES
            cfg.immutable(True)
            test_model(checkpoints['final'], args.multi_gpu_testing, args.opts)
            print('reprint snapshot name for the result: ', checkpoints['final'])

            cfg.immutable(False)
            cfg.TEST.DATASETS = TEST_DATASETS
            cfg.TEST.PROPOSAL_FILES = TEST_PROPOSAL_FILES
            cfg.immutable(True)

        cfg.immutable(False)
        cfg.TEST.BBOX_AUG.ENABLED = False
        cfg.VIS = False
        cfg.immutable(True)

        for snapshot in sorted(checkpoints.iterkeys(), reverse=True):
            test_model(checkpoints[snapshot], args.multi_gpu_testing, args.opts)
            print('reprint snapshot name for the result: ', snapshot, checkpoints[snapshot])
Exemple #5
0
def main(args):
    logger = logging.getLogger(__name__)

    merge_cfg_from_file(args.cfg)
    cfg.NUM_GPUS = 1
    args.weights = cache_url(args.weights, cfg.DOWNLOAD_CACHE)
    assert_and_infer_cfg(cache_urls=False)

    assert not cfg.MODEL.RPN_ONLY, \
        'RPN models are not supported'
    assert not cfg.TEST.PRECOMPUTED_PROPOSALS, \
        'Models that require precomputed proposals are not supported'

    model = infer_engine.initialize_model_from_cfg(args.weights)
    dummy_coco_dataset = dummy_datasets.get_coco_dataset()

    if os.path.isdir(args.im_or_folder):
        im_list = glob.iglob(args.im_or_folder + '/*.' + args.image_ext)
    else:
        im_list = [args.im_or_folder]

    for i, im_name in enumerate(im_list):
        out_name = os.path.join(
            args.output_dir, '{}'.format(os.path.basename(im_name) + '.' + args.output_ext)
        )
        logger.info('Processing {} -> {}'.format(im_name, out_name))
        im = cv2.imread(im_name)
        timers = defaultdict(Timer)
        t = time.time()
        with c2_utils.NamedCudaScope(0):
            cls_boxes, cls_segms, cls_keyps = infer_engine.im_detect_all(
                model, im, None, timers=timers
            )
        logger.info('Inference time: {:.3f}s'.format(time.time() - t))
        for k, v in timers.items():
            logger.info(' | {}: {:.3f}s'.format(k, v.average_time))
        if i == 0:
            logger.info(
                ' \ Note: inference on the first image will be slower than the '
                'rest (caches and auto-tuning need to warm up)'
            )

        vis_utils.vis_one_image(
            im[:, :, ::-1],  # BGR -> RGB for visualization
            im_name,
            args.output_dir,
            cls_boxes,
            cls_segms,
            cls_keyps,
            dataset=dummy_coco_dataset,
            box_alpha=0.3,
            show_class=True,
            thresh=0.7,
            kp_thresh=2,
            ext=args.output_ext,
            out_when_no_box=args.out_when_no_box
        )
Exemple #6
0
 def __init__(self, cfgPath, weights, gpu_id, if_visual):
     self.gpu_id = gpu_id
     self.cfgPath = cfgPath
     self.weights = weights
     self.if_visual = if_visual
     merge_cfg_from_file(self.cfgPath)
     assert_and_infer_cfg(cache_urls=False, make_immutable=False)
     self.model = infer_engine.initialize_model_from_cfg(self.weights,
                                                         gpu_id=self.gpu_id)
Exemple #7
0
def main(args):
    logger = logging.getLogger(__name__)

    merge_cfg_from_file(args.cfg)
    cfg.NUM_GPUS = 1
    args.weights = cache_url(args.weights, cfg.DOWNLOAD_CACHE)
    assert_and_infer_cfg(cache_urls=False)

    assert not cfg.MODEL.RPN_ONLY, \
        'RPN models are not supported'
    assert not cfg.TEST.PRECOMPUTED_PROPOSALS, \
        'Models that require precomputed proposals are not supported'

    model = infer_engine.initialize_model_from_cfg(args.weights)
    dummy_coco_dataset = dummy_datasets.get_coco_dataset()

    if os.path.isdir(args.im_or_folder):
        im_list = glob.iglob(args.im_or_folder + '/*.' + args.image_ext)
    else:
        im_list = [args.im_or_folder]

    for i, im_name in enumerate(im_list):
        out_name = os.path.join(
            args.output_dir, '{}'.format(os.path.basename(im_name) + '.' + args.output_ext)
        )
        logger.info('Processing {} -> {}'.format(im_name, out_name))
        im = cv2.imread(im_name)
        timers = defaultdict(Timer)
        t = time.time()
        with c2_utils.NamedCudaScope(0):
            cls_boxes, cls_segms, cls_keyps = infer_engine.im_detect_all(
                model, im, None, timers=timers
            )
        logger.info('Inference time: {:.3f}s'.format(time.time() - t))
        for k, v in timers.items():
            logger.info(' | {}: {:.3f}s'.format(k, v.average_time))
        if i == 0:
            logger.info(
                ' \ Note: inference on the first image will be slower than the '
                'rest (caches and auto-tuning need to warm up)'
            )

        vis_utils.vis_one_image(
            im[:, :, ::-1],  # BGR -> RGB for visualization
            im_name,
            args.output_dir,
            cls_boxes,
            cls_segms,
            cls_keyps,
            dataset=dummy_coco_dataset,
            box_alpha=0.3,
            show_class=True,
            thresh=args.thresh,
            kp_thresh=args.kp_thresh,
            ext=args.output_ext,
            out_when_no_box=args.out_when_no_box
        )
Exemple #8
0
def main(args):
    logger = logging.getLogger(__name__)

    merge_cfg_from_file(args.cfg)
    cfg.NUM_GPUS = 1
    args.weights = cache_url(args.weights, cfg.DOWNLOAD_CACHE)
    assert_and_infer_cfg(cache_urls=False)

    assert not cfg.MODEL.RPN_ONLY, \
        'RPN models are not supported'
    assert not cfg.TEST.PRECOMPUTED_PROPOSALS, \
        'Models that require precomputed proposals are not supported'

    model = infer_engine.initialize_model_from_cfg(args.weights)
    dummy_coco_dataset = dummy_datasets.get_coco_dataset()

    '''
    add onboard cam support 
    '''
    cam = cv2.VideoCapture("nvcamerasrc ! video/x-raw(memory:NVMM), width=(int)1280, height=(int)720,format=(string)I420, framerate=(fraction)30/1 ! nvvidconv flip-method=0 ! video/x-raw, format=(string)BGRx ! videoconvert ! video/x-raw, format=(string)BGR ! appsink")
    if cam.isOpened():
        print("camara open succeded")
        im_name = "Detection"
        while True:
            _, im = cam.read();
            timers = defaultdict(Timer)
            t = time.time()
            with c2_utils.NamedCudaScope(0):
                cls_boxes, cls_segms, cls_keyps = infer_engine.im_detect_all(
                    model, im, None, timers=timers
                )
            logger.info('Inference time: {:.3f}s'.format(time.time() - t))
            '''
            for k, v in timers.items():
                logger.info(' | {}: {:.3f}s'.format(k, v.average_time))
            '''
            vis_utils.vis_cam_image(
                im[:, :, ::-1],  # BGR -> RGB for visualization
                im_name,
                cls_boxes,
                cls_segms,
                cls_keyps,
                dataset=dummy_coco_dataset,
                box_alpha=0.3,
                show_class=True,
                thresh=0.7,
                kp_thresh=2
            )
            """
            key=cv2.waitKey(10)
            if key == 27: # Check for ESC key
                cv2.destroyAllWindows()
                break ;
            """
    else:
        print("camera open failed")
def main(args):
    logger = logging.getLogger(__name__)

    merge_cfg_from_file(args.cfg)
    cfg.NUM_GPUS = 1
    args.weights = cache_url(args.weights, cfg.DOWNLOAD_CACHE)
    assert_and_infer_cfg(cache_urls=False)

    assert not cfg.MODEL.RPN_ONLY, \
        'RPN models are not supported'
    assert not cfg.TEST.PRECOMPUTED_PROPOSALS, \
        'Models that require precomputed proposals are not supported'

    model = infer_engine.initialize_model_from_cfg(args.weights)
    dummy_coco_dataset = dummy_datasets.get_coco_dataset()

    logger.info(
        ' \ Note: inference on the first image will be slower than the '
        'rest (caches and auto-tuning need to warm up)'
    )

    while True:
        reply = {}
        #  Wait for next request from client
        im,extra = zmqa.recv(socket)
        if extra is not None and 'fname' in extra:
            print("Received request %s" % extra)
            reply['fname']=extra['fname']
        else:
            print("Received request %s" % extra)
            reply['fname'] = 'input.jpg'
        # set file name
        im_name=reply['fname']
        timers = defaultdict(Timer)
        t = time.time()
        with c2_utils.NamedCudaScope(0):
            cls_boxes, cls_segms, cls_keyps = infer_engine.im_detect_all(
                model, im, None, timers=timers
            )
        logger.info('Inference time: {:.3f}s'.format(time.time() - t))
        for k, v in timers.items():
            logger.info(' | {}: {:.3f}s'.format(k, v.average_time))

        out_name = os.path.join(
            args.output_dir, '{}'.format(os.path.basename(im_name) + '.' + args.output_ext)
        )
        masks=do_one_image_opencv(
            im,  # BGR -> RGB for visualization
            cls_boxes,
            cls_segms,
            cls_keyps,
            thresh=0.7,
            reply=reply
        )
        zmqa.send(socket, masks,extra=reply)
Exemple #10
0
def main(args):
    logger = logging.getLogger(__name__)
    merge_cfg_from_file(args.cfg)
    cfg.NUM_GPUS = 1
    args.weights = cache_url(args.weights, cfg.DOWNLOAD_CACHE)
    assert_and_infer_cfg(cache_urls=False)
    model = infer_engine.initialize_model_from_cfg(args.weights)
    dummy_coco_dataset = dummy_datasets.get_coco_dataset()


    # load match scene
    match_scene_dict = pickle.load(open('{}/pkl/match_scene_intervals_dict.pkl'.format(args.data_dir), 'rb'))
    HW_foreground = set()
    JZ_foreground = set()
    for match in match_scene_dict['HW_foreground']:
        for fid in range(match[1], match[2]):
            HW_foreground.add(fid)
    for match in match_scene_dict['JZ_foreground']:
        for fid in range(match[1], match[2]):
            JZ_foreground.add(fid)

    # detect frame
    cap = cv2.VideoCapture('{}/videos/{}'.format(args.data_dir, args.input_video))
    result_list = []
    fid = 0
    ret = True

    # create dir
    for player in ['HW', 'JZ']:
        dir = '{data_dir}/image/img_{player}'.format(data_dir=args.data_dir, player=player)
        if not os.path.exists(dir):
            os.makedirs(dir)
        dir = '{data_dir}/image/densepose_{player}'.format(data_dir=args.data_dir, player=player)
        if not os.path.exists(dir):
            os.makedirs(dir)

    vid = 65
    while ret:
        if fid % 1000 == 0:
            print("Inferring frame %d" % fid)
        ret, image = cap.read()
        if fid in HW_foreground or fid in JZ_foreground:
            player = 'HW' if fid in HW_foreground else 'JZ'
            img_path = '{data_dir}/image/img_{player}/img_{vid}_{fid}_{player}.jpg'.format(data_dir=args.data_dir, player=player, vid=vid, fid=fid)
            pose_path = '{data_dir}/image/densepose_{player}/densepose_{vid}_{fid}_{player}.jpg'.format(data_dir=args.data_dir, player=player, vid=vid, fid=fid)

            result_one_image = infer_one_frame(image, model, img_path, pose_path)
            result_one_image['foreground'] = player
            result_one_image['fid'] = fid
            result_list.append(result_one_image)
        fid += 1
        # if len(result_list) > 100:
        #     break

    pickle.dump(result_list, open('{}/pkl/result.pkl'.format(args.data_dir), 'wb'), protocol=2)
 def test_gpu_random_input(self):
     X = np.random.rand(5, 4)
     track_n_rois = [2, 3]
     for output in ['Cosine', 'MatchNet']:
         cfg.immutable(False)
         cfg.TRCNN.OUTPUT = output
         assert_and_infer_cfg(cache_urls=False)
         Y_exp = self._add_track_outputs_np(X.copy(), track_n_rois)
         Y_act = self._add_track_outputs(
             X, np.array(track_n_rois, dtype=np.int32))
         np.testing.assert_allclose(Y_act, Y_exp, rtol=1e-06)
def get_model():
    workspace.GlobalInit(['caffe2', '--caffe2_log_level=0'])
    merge_cfg_from_file(
        '/home/bryan/code/detectron/configs/12_2017_baselines/e2e_mask_rcnn_R-101-FPN_2x.yaml'
    )
    cfg.NUM_GPUS = 1
    assert_and_infer_cfg(cache_urls=False)
    DOWNLOAD_CACHE = '/tmp/detectron-download-cache'
    weights_url = 'https://s3-us-west-2.amazonaws.com/detectron/35861858/12_2017_baselines/e2e_mask_rcnn_R-101-FPN_2x.yaml.02_32_51.SgT4y1cO/output/train/coco_2014_train:coco_2014_valminusminival/generalized_rcnn/model_final.pkl'
    weights = cache_url(weights_url, DOWNLOAD_CACHE)
    return infer_engine.initialize_model_from_cfg(weights)
Exemple #13
0
def main(args):
    """Extract bottom-up features from all images in a directory using
    a pre-trained Detectron model, and save them in HDF format.

    Parameters
    ----------
    args : argparse.Namespace
        Parsed command-line arguments.
    """

    # specifically for visual genome
    detectron_config.MODEL.NUM_ATTRIBUTES = -1
    merge_cfg_from_file(args.config)

    # override some config options and validate the config
    detectron_config.NUM_GPUS = 1
    detectron_config.TRAIN.CPP_RPN = 'none'
    assert_and_infer_cfg(cache_urls=False)

    # initialize model
    detectron_model = infer_engine.initialize_model_from_cfg(args.weights, args.gpu_id)

    # list of paths (example: "coco_train2014/COCO_train2014_000000123456.jpg")
    image_paths = []
    for image_root in args.image_root:
        image_paths.extend([os.path.join(image_root, name)
                            for name in glob.glob(os.path.join(image_root, "*.jpg"))
                            if name not in {".", ".."}])

    # create an output HDF to save extracted features
    save_h5 = h5py.File(args.save_path, "w")
    image_ids_h5d = save_h5.create_dataset(
        "image_ids", (len(image_paths), )
    )

    # 'features' is a chunked dataset, each chunk holds features from one image
    features_h5d = save_h5.create_dataset(
        "features", (len(image_paths), args.max_boxes, args.feat_dims),
        chunks=(1, args.max_boxes, args.feat_dims)
    )

    for idx, image_path in tqdm(enumerate(image_paths)):
        try:
            image_ids_h5d[idx] = image_id_from_path(image_path)

            image = cv2.imread(image_path)
            # we only care about features, not classes
            _, _, _, bottomup_features = detect_image(detectron_model, image, args)
            features_h5d[idx] = bottomup_features
        except:
            print(f"\nWarning: features from {idx}, {image_path} failed to extract.\n")
    # set current split name in attributrs of file, for tractability
    save_h5.attrs["split"] = args.split
    save_h5.close()
    def __init__(self, config_file, weights_file, gpu_id):
        c2_utils.import_detectron_ops()

        workspace.GlobalInit(['caffe2', '--caffe2_log_level=3'])

        merge_cfg_from_file(config_file)
        cfg.NUM_GPUS = 1
        assert_and_infer_cfg(cache_urls=False)
        self.__model = infer_engine.initialize_model_from_cfg(weights_file,
                                                              gpu_id=gpu_id)
        self.gpu_id = gpu_id
Exemple #15
0
def main(args):
    logger = logging.getLogger(__name__)
    merge_cfg_from_file(args.cfg)
    cfg.NUM_GPUS = 1
    args.weights = cache_url(args.weights, cfg.DOWNLOAD_CACHE)
    assert_and_infer_cfg(cache_urls=False)
    model = infer_engine.initialize_model_from_cfg(args.weights)
    dummy_coco_dataset = dummy_datasets.get_coco_dataset()

    if os.path.isdir(args.im_or_folder):
        im_list = glob.iglob(args.im_or_folder + '/*/*.' + args.image_ext)
    else:
        im_list = [args.im_or_folder]

    for i, im_name in enumerate(im_list):
        vid = im_name.split("/")[1]
        output_dir = args.output_dir + vid
        try:
            os.mkdir(output_dir)
        except OSError:
            pass
        out_name = os.path.join(
            output_dir, '{}'.format(os.path.basename(im_name) + '.pdf'))
        if os.path.exists(out_name):
            continue

        logger.info('Processing {} -> {}'.format(im_name, out_name))
        im = cv2.imread(im_name)
        timers = defaultdict(Timer)
        t = time.time()
        with c2_utils.NamedCudaScope(0):
            cls_boxes, cls_segms, cls_keyps, cls_bodys = infer_engine.im_detect_all(
                model, im, None, timers=timers)
        logger.info('Inference time: {:.3f}s'.format(time.time() - t))
        for k, v in timers.items():
            logger.info(' | {}: {:.3f}s'.format(k, v.average_time))
        if i == 0:
            logger.info(
                ' \ Note: inference on the first image will be slower than the '
                'rest (caches and auto-tuning need to warm up)')

        vis_one_image(
            im[:, :, ::-1],  # BGR -> RGB for visualization
            im_name,
            output_dir,
            cls_boxes,
            cls_segms,
            cls_keyps,
            cls_bodys,
            dataset=dummy_coco_dataset,
            box_alpha=0.3,
            show_class=True,
            thresh=0.7,
            kp_thresh=2)
Exemple #16
0
def main(args):
    #ros initialization
    rospy.init_node('get_image', anonymous=True)

    args.im_or_folder = get_image()

    while not rospy.is_shutdown():
        merge_cfg_from_file(args.cfg)
        cfg.NUM_GPUS = 1
        args.weights = cache_url(args.weights, cfg.DOWNLOAD_CACHE)
        assert_and_infer_cfg(cache_urls=False)

        assert not cfg.MODEL.RPN_ONLY, \
            'RPN models are not supported'
        assert not cfg.TEST.PRECOMPUTED_PROPOSALS, \
            'Models that require precomputed proposals are not supported'

        model = infer_engine.initialize_model_from_cfg(args.weights)
        dummy_coco_dataset = dummy_datasets.get_coco_dataset()
        # set im list to be the ros image
        im = args.im_or_folder

        timers = defaultdict(Timer)
        with c2_utils.NamedCudaScope(0):
            cls_boxes, cls_segms, cls_keyps = infer_engine.im_detect_all(
                model, im, None, timers=timers)
        # calls the method that performs actual detection and inference
        fig = vis_utils.vis_one_image_opencv(
            im[:, :, ::-1],  # BGR -> RGB for visualization
            cls_boxes,
            cls_segms,
            cls_keyps,
            dataset=dummy_coco_dataset,
            show_class=True,
            thresh=args.thresh,
            kp_thresh=args.kp_thresh,
        )

        # img is rgb, convert to opencv's default bgr
        img = cv2.cvtColor(fig, cv2.COLOR_RGB2BGR)

        image_publisher = rospy.Publisher('detectron_output',
                                          Image,
                                          queue_size=10)
        brdg = CvBridge()
        image_publisher.publish(brdg.cv2_to_imgmsg(img, "bgr8"))
        try:
            start_time = time.time()
            print(time.time() - start_time)
            main(args)
        except KeyboardInterrupt:
            print("shutting down")
            cv2.destroyAllWindows()
    def listen(self, is_debug=True):

        currt = os.path.dirname(os.path.abspath(__file__))
        l1 = "./config/args.yaml"
        l1 = os.path.join(currt, l1)

        ####################
        args = yaml.load(open(l1))
        self.args = args
        print(args)
        ######init  ops env
        ####################

        self.logger = logging.getLogger(__name__)
        merge_cfg_from_file(args["cfg"])
        cfg.NUM_GPUS = 1
        weights = args["weights"]
        assert_and_infer_cfg(cache_urls=False)

        assert not cfg.MODEL.RPN_ONLY, \
        'RPN models are not supported'
        assert not cfg.TEST.PRECOMPUTED_PROPOSALS, \
        'Models that require precomputed proposals are not supported'

        self.model = infer_engine.initialize_model_from_cfg(weights)
        self.timers = defaultdict(Timer)

        #dummy_coco_dataset = dummy_datasets.get_coco_dataset()

        with c2_utils.NamedCudaScope(0):
            ####################
            ###### listen
            ####################
            null_ct = 0
            while True:
                data_recv = self._getdata()
                if data_recv is None or len(data_recv) <= 0:
                    if is_debug:
                        null_ct += 1
                        if null_ct > 100:
                            break
                    continue
                self.logger.info(' \ Note:  find a image ,shape: ' +
                                 str(data_recv[0][2].shape))
                collect_list = []
                for msg_id, _, img in data_recv:
                    matting = self._process(img)

                    collect_list.append((msg_id, img, matting))

                self._response(collect_list)
                self.logger.info(' \ Note:  send a image ,len: ' +
                                 str(len(collect_list)) + "  \ ")
Exemple #18
0
def main(args):
    logger = logging.getLogger(__name__)
    merge_cfg_from_file(args.cfg)
    cfg.NUM_GPUS = 1
    args.weights = cache_url(args.weights, cfg.DOWNLOAD_CACHE)
    assert_and_infer_cfg(cache_urls=False)
    model = infer_engine.initialize_model_from_cfg(args.weights)
    dummy_coco_dataset = dummy_datasets.get_coco_dataset()

    print("capturing video " + args.input)
    cap = cv2.VideoCapture(args.input)
    total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
    # pdb.set_trace()
    grab = 1
    if (cap.isOpened() == False):
        print("Error opening video stream or file")
        exit
    while (cap.isOpened() and grab <= total_frames):
        grab += 1
        ret_val, im = cap.read()
        #skips intermediate frames
        #if grab%2 !=0:
        #    continue
        #uncomment to resize image
        #im = cv2.resize(im, (int(1280/1),int(720/1)))
        timers = defaultdict(Timer)
        t = time.time()
        with c2_utils.NamedCudaScope(0):
            cls_boxes, cls_segms, cls_keyps, cls_bodys = infer_engine.im_detect_all(
                model, im, None, timers=timers)
        output_name = 'frame' + str(grab).zfill(4) + '.mp4'
        print("| Analysed frame {0} / {1}  in {2}ms".format(
            grab, total_frames, int(1000. * (time.time() - t))))
        #print('\t | Inference time: {:.3f}s'.format(time.time() - t))
        #for k, v in timers.items():
        #    print('\t | {}: {:.3f}s'.format(k, v.average_time))
        ret = vis_utils.vis_one_image(
            im[:, :, ::-1],  # BGR -> RGB for visualization
            output_name,
            args.output_dir,
            cls_boxes,
            cls_segms,
            cls_keyps,
            cls_bodys,
            dataset=dummy_coco_dataset,
            box_alpha=0.3,
            show_class=False,
            thresh=0.7,
            kp_thresh=2)

    cap.release()
    cv2.destroyAllWindows()
Exemple #19
0
def main(args):
    logger = logging.getLogger(__name__)
    merge_cfg_from_file(args.cfg)
    cfg.NUM_GPUS = 1
    args.weights = cache_url(args.weights, cfg.DOWNLOAD_CACHE)
    assert_and_infer_cfg(cache_urls=False)
    model = infer_engine.initialize_model_from_cfg(args.weights)
    dummy_coco_dataset = dummy_datasets.get_coco_dataset()

    h5py_file_path = ('/media/hdd1/tanya/open-pose/'
                      'paired_filenames512_image_keypoints512_main.h5')
    hf = h5py.File(h5py_file_path, 'r')

    IUV_image_list = []


    for i in range(len(hf['images'])):
        im = hf['images'][i]
        timers = defaultdict(Timer)
        t = time.time()
        with c2_utils.NamedCudaScope(0):
            cls_boxes, cls_segms, cls_keyps, cls_bodys = infer_engine.im_detect_all(
                model, im, None, timers=timers
            )
        logger.info('Inference time: {:.3f}s'.format(time.time() - t))
        for k, v in timers.items():
            logger.info(' | {}: {:.3f}s'.format(k, v.average_time))
        if i == 0:
            logger.info(
                ' \ Note: inference on the first image will be slower than the '
                'rest (caches and auto-tuning need to warm up)'
            )

        IUV_image = vis_utils.vis_one_image(
                im[:, :, ::-1],  # BGR -> RGB for visualization
                'dummy',
                args.output_dir,
                cls_boxes,
                cls_segms,
                cls_keyps,
                cls_bodys,
                dataset=dummy_coco_dataset,
                box_alpha=0.3,
                show_class=True,
                thresh=0.7,
                kp_thresh=2)
        IUV_image_list.append(IUV_image)
    IUV_images_final = np.stack(IUV_image_list, 0)
    with h5py.File('./paired_filenames512_image_keypoints_withIUV.h5', 'w') as f:
        f.create_dataset('images', data=np.array(hf['images']))
        f.create_dataset('keypoints', data=np.array(hf['keypoints']))
        f.create_dataset('IUV', data=IUV_images_final)
Exemple #20
0
def main(args):
    logger = logging.getLogger(__name__)
    dummy_coco_dataset = dummy_datasets.get_coco_dataset()
    cfg_orig = load_cfg(yaml.dump(cfg))
    im = cv2.imread(args.im_file)

    if args.rpn_pkl is not None:
        proposal_boxes, _proposal_scores = get_rpn_box_proposals(im, args)
        workspace.ResetWorkspace()
    else:
        proposal_boxes = None

    cls_boxes, cls_segms, cls_keyps = None, None, None
    for i in range(0, len(args.models_to_run), 2):
        pkl = args.models_to_run[i]
        yml = args.models_to_run[i + 1]
        cfg.immutable(False)
        merge_cfg_from_cfg(cfg_orig)
        merge_cfg_from_file(yml)
        if len(pkl) > 0:
            weights_file = pkl
        else:
            weights_file = cfg.TEST.WEIGHTS
        cfg.NUM_GPUS = 1
        assert_and_infer_cfg(cache_urls=False)
        model = model_engine.initialize_model_from_cfg(weights_file)
        with c2_utils.NamedCudaScope(0):
            cls_boxes_, cls_segms_, cls_keyps_ = \
                model_engine.im_detect_all(model, im, proposal_boxes)
        cls_boxes = cls_boxes_ if cls_boxes_ is not None else cls_boxes
        cls_segms = cls_segms_ if cls_segms_ is not None else cls_segms
        cls_keyps = cls_keyps_ if cls_keyps_ is not None else cls_keyps
        workspace.ResetWorkspace()

    out_name = os.path.join(
        args.output_dir, '{}'.format(os.path.basename(args.im_file) + '.pdf')
    )
    logger.info('Processing {} -> {}'.format(args.im_file, out_name))

    vis_utils.vis_one_image(
        im[:, :, ::-1],
        args.im_file,
        args.output_dir,
        cls_boxes,
        cls_segms,
        cls_keyps,
        dataset=dummy_coco_dataset,
        box_alpha=0.3,
        show_class=True,
        thresh=0.7,
        kp_thresh=2
    )
Exemple #21
0
def get_detectron_result(im):
    """
    功能: 获取传入图像的检测结果

    输入参数列表:
        im: BGR格式图片
    返回参数列表:
        img: 检测结果图片
        detectron_result_info: 检测结果字典
    """
    setup_logging(__name__)
    logger = logging.getLogger(__name__)
    dummy_coco_dataset = dummy_datasets.get_coco_dataset()
    cfg_orig = load_cfg(yaml.dump(cfg))

    if rpn_pkl is not None:
        proposal_boxes, _proposal_scores = get_rpn_box_proposals(im)
        workspace.ResetWorkspace()
    else:
        proposal_boxes = None

    cls_boxes, cls_segms, cls_keyps = None, None, None
    pkl = rpn_pkl
    yml = rpn_cfg
    cfg.immutable(False)
    merge_cfg_from_cfg(cfg_orig)
    merge_cfg_from_file(yml)
    if len(pkl) > 0:
        weights_file = pkl
    else:
        weights_file = cfg.TEST.WEIGHTS
    cfg.NUM_GPUS = 1
    assert_and_infer_cfg(cache_urls=False)
    model = model_engine.initialize_model_from_cfg(weights_file)
    with c2_utils.NamedCudaScope(0):
        cls_boxes_, cls_segms_, cls_keyps_ = \
            model_engine.im_detect_all(model, im, proposal_boxes)
    cls_boxes = cls_boxes_ if cls_boxes_ is not None else cls_boxes
    cls_segms = cls_segms_ if cls_segms_ is not None else cls_segms
    cls_keyps = cls_keyps_ if cls_keyps_ is not None else cls_keyps
    workspace.ResetWorkspace()

    img, detectron_result_info = vis_utils.vis_one_image_opencv(
        im,
        cls_boxes,
        cls_segms,
        cls_keyps,
        dataset=dummy_coco_dataset,
        show_box=True,
        show_class=True)

    return img, detectron_result_info
Exemple #22
0
def get_rpn_box_proposals(im, args):
    cfg.immutable(False)
    merge_cfg_from_file(args.rpn_cfg)
    cfg.NUM_GPUS = 1
    cfg.MODEL.RPN_ONLY = True
    cfg.TEST.RPN_PRE_NMS_TOP_N = 10000
    cfg.TEST.RPN_POST_NMS_TOP_N = 2000
    assert_and_infer_cfg(cache_urls=False)

    model = model_engine.initialize_model_from_cfg(args.rpn_pkl)
    with c2_utils.NamedCudaScope(0):
        boxes, scores = rpn_engine.im_proposals(model, im)
    return boxes, scores
Exemple #23
0
 def setup(self):
     c2_utils.import_detectron_ops()
     cv2.ocl.setUseOpenCL(False)
     merge_cfg_from_file(
         '/DensePose/configs/DensePose_ResNet101_FPN_s1x-e2e.yaml')
     cfg.NUM_GPUS = 1
     weights = cache_url(
         'https://s3.amazonaws.com/densepose/DensePose_ResNet101_FPN_s1x-e2e.pkl',
         cfg.DOWNLOAD_CACHE)
     assert_and_infer_cfg(cache_urls=False)
     model = infer_engine.initialize_model_from_cfg(weights, 1)
     dummy_coco_dataset = dummy_datasets.get_coco_dataset()
     return model
Exemple #24
0
def get_rpn_box_proposals(im, args):
    cfg.immutable(False)
    merge_cfg_from_file(args.rpn_cfg)
    cfg.NUM_GPUS = 1
    cfg.MODEL.RPN_ONLY = True
    cfg.TEST.RPN_PRE_NMS_TOP_N = 10000
    cfg.TEST.RPN_POST_NMS_TOP_N = 2000
    assert_and_infer_cfg(cache_urls=False)

    model = model_engine.initialize_model_from_cfg(args.rpn_pkl)
    with c2_utils.NamedCudaScope(0):
        boxes, scores = rpn_engine.im_proposals(model, im)
    return boxes, scores
Exemple #25
0
def run_task(clip, background_image):

    workspace.GlobalInit(['caffe2', '--caffe2_log_level=0'])
    merge_cfg_from_file('/smartcrop/configs/e2e_mask_rcnn_R-101-FPN_2x.yaml')
    assert_and_infer_cfg()
    model = infer_engine.initialize_model_from_cfg(
        '/smartcrop/weights/e2e_mask_rcnn_R-101-FPN_2x/model_final.pkl')
    dataset = dummy_datasets.get_coco_dataset()

    clip = clip.fl_image(
        lambda image: extract_person(image, background_image, model, dataset))

    return clip
 def test_gpu_random_input(self):
     X = np.random.rand(1, 6).astype(np.float32)
     X_gt = np.random.randint(2, size=(1, 6)).astype(np.float32)
     for loss in [
             'Cosine', 'L2', 'L2Balanced', 'CrossEntropy',
             'CrossEntropyBalanced', 'CrossEntropyWeighted'
     ]:
         cfg.immutable(False)
         cfg.TRCNN.LOSS = loss
         assert_and_infer_cfg(cache_urls=False)
         Y_exp = self._add_track_losses_np(X.copy(), X_gt.copy())
         Y_act = self._add_track_losses(X.copy(), X_gt.copy())
         np.testing.assert_allclose(Y_act, Y_exp, rtol=1e-06)
def main(args):
    #logger = logging.getLogger(__name__)
    merge_cfg_from_file(args.cfg)
    cfg.NUM_GPUS = 1
    args.weights = cache_url(args.weights, cfg.DOWNLOAD_CACHE)
    assert_and_infer_cfg(cache_urls=False)
    model = infer_engine.initialize_model_from_cfg(args.weights)
    dummy_coco_dataset = dummy_datasets.get_coco_dataset()

    cam = cv2.VideoCapture(0)

    # Set Texture
    Tex_Atlas = cv2.imread(
        'DensePoseData/demo_data/texture_from_SURREAL.png')[:, :, ::-1] / 255.0
    #Tex_Atlas = cv2.imread('DensePoseData/demo_data/texture_atlas_200.png')[:,:,::-1]
    TextureIm = np.zeros([24, 200, 200, 3])
    #
    for i in range(4):
        for j in range(6):
            TextureIm[(6 * i +
                       j), :, :, :] = Tex_Atlas[(200 * j):(200 * j + 200),
                                                (200 * i):(200 * i + 200), :]

    while True:
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
        retval, im = cam.read()
        #imsmall = cv2.resize(im, None, fx=0.5, fy=0.5)
        im = cv2.resize(im, None, fx=0.5, fy=0.5)

        with c2_utils.NamedCudaScope(0):
            cls_boxes, cls_segms, cls_keyps, cls_bodys = infer_engine.im_detect_all(
                model, im, None, timers=None)

        iuvout, indsout = vis_utils.vis_webcam(
            im,
            boxes=cls_boxes,
            segms=cls_segms,
            keypoints=cls_keyps,
            body_uv=cls_bodys,
            thresh=0.9,
            kp_thresh=2,
            dpi=200,
            box_alpha=0.3,
            dataset=dummy_coco_dataset,
            show_class=True,
        )
        #cv2.imshow('input', im)
        texout = TransferTexturePure(TextureIm, im, iuvout)
        texout = cv2.resize(texout, None, fx=2.0, fy=2.0)
        cv2.imshow('output', texout)
Exemple #28
0
def main(args):
    logger = logging.getLogger(__name__)

    pdb.set_trace()
    merge_cfg_from_file(args.cfg)
    cfg.NUM_GPUS = 1
    args.weights = cache_url(args.weights, cfg.DOWNLOAD_CACHE)
    assert_and_infer_cfg(cache_urls=False)

    assert not cfg.MODEL.RPN_ONLY, \
        'RPN models are not supported'
    assert not cfg.TEST.PRECOMPUTED_PROPOSALS, \
        'Models that require precomputed proposals are not supported'

    model = infer_engine.initialize_model_from_cfg(args.weights)
    dummy_coco_dataset = dummy_datasets.get_coco_dataset()

    if os.path.isdir(args.im_or_folder):
        im_list = glob.iglob(args.im_or_folder + '/*.' + args.image_ext)
    else:
        im_list = [args.im_or_folder]

    print(im_list)

    for i, im_name in enumerate(im_list):
        im_basename = os.path.basename(im_name)
        out_name = os.path.join(
            args.output_dir, '{}'.format(im_basename + '.' + args.output_ext))
        logger.info('Processing {} -> {}'.format(im_name, out_name))
        im = cv2.imread(im_name)
        timers = defaultdict(Timer)
        t = time.time()
        with c2_utils.NamedCudaScope(0):
            cls_boxes, cls_segms, cls_keyps = infer_engine.im_detect_all(
                model, im, None, timers=timers)

            result_file = os.path.join(args.output_dir,
                                       im_basename + '_bbox.pkl')
            with open(result_file, 'wb') as f:
                boxes, _, _, _ = vis_utils.convert_from_cls_format(
                    cls_boxes, cls_segms, cls_keyps)
                print(boxes.shape)
                pickle.dump(cls_boxes, f)

        logger.info('Inference time: {:.3f}s'.format(time.time() - t))
        for k, v in timers.items():
            logger.info(' | {}: {:.3f}s'.format(k, v.average_time))
        if i == 0:
            logger.info(
                ' \ Note: inference on the first image will be slower than the '
                'rest (caches and auto-tuning need to warm up)')
Exemple #29
0
def main(args):
    logger = logging.getLogger(__name__)
    merge_cfg_from_file(args.cfg)
    cfg.NUM_GPUS = 4
    args.weights = '/detectron-download-cache/35861858/12_2017_baselines/e2e_mask_rcnn_R-101-FPN_2x.yaml.02_32_51.SgT4y1cO/output/train/coco_2014_train:coco_2014_valminusminival/generalized_rcnn/model_final.pkl' #cache_url(args.weights, cfg.DOWNLOAD_CACHE)
    print (args.weights)
    assert_and_infer_cfg(cache_urls=False)
    model = infer_engine.initialize_model_from_cfg(args.weights)
    dummy_coco_dataset = dummy_datasets.get_coco_dataset()

    if os.path.isdir(args.im_or_folder):
        im_list = glob.iglob(args.im_or_folder + '/*.' + args.image_ext)
    else:
        im_list = [args.im_or_folder]
    
    cv2.namedWindow("Video", cv2.WND_PROP_FULLSCREEN)
    cv2.setWindowProperty("Video",cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN)
    cap=cv2.VideoCapture(0)
    width = cap.get(3)
    height = cap.get(4)
    cap.set(3,1280)
    cap.set(4,720)
    print (width, height)
    while True:
         ret, im = cap.read()
         timers = defaultdict(Timer)
         im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
         with c2_utils.NamedCudaScope(0):
            cls_boxes, cls_segms, cls_keyps = infer_engine.im_detect_all(
                model, im, None, timers=timers
            )
         
         deframe = vis_utils.vis_one_image_opencv(
            im[:, :, ::-1],  # BGR -> RGB for visualization
            cls_boxes,
            cls_segms,
            cls_keyps,
            dataset=dummy_coco_dataset,
            show_class = True,
            thresh=0.7,
            kp_thresh=2,
            show_box=True
         )
                
         cv2.imshow('Video',deframe)
        
         if cv2.waitKey(1) & 0xFF == ord('q'):
             break

    cap.release()
    cv2.destroyAllWindows()    
Exemple #30
0
def main(args):
    logger = logging.getLogger(__name__)
    merge_cfg_from_file(args.cfg)
    cfg.NUM_GPUS = 1
    args.weights = cache_url(args.weights, cfg.DOWNLOAD_CACHE)
    assert_and_infer_cfg(cache_urls=False)
    model = infer_engine.initialize_model_from_cfg(args.weights)
    dummy_coco_dataset = dummy_datasets.get_coco_dataset()
    zmq_video = args.source == "zmq"
    frameId = 0

    if zmq_video:
        context = zmq.Context()
        socket = context.socket(zmq.REP)
        socket.bind("tcp://*:7001")
    else:
        # From virtual camera video and its associated timestamp file on Drive PX2,e.g."./lane/videofilepath.h264"
        cap = cv2.VideoCapture(args.source)

    while True:
        if zmq_video:
            try:
                message = socket.recv()
                print("Received message length:" + str(len(message)) +
                      " type:" + str(type(message)))
                socket.send("ok")
                position = message.find(ZMQ_SEPER, 0, 100)
                frameId = message[:position]
                message = message[position + len(ZMQ_SEPER):]
                img_np = np.fromstring(message, np.uint8)
                img_np = img_np.reshape((400, 1400, 3))
                print("nparr type:" + str(type(img_np)) + " shape:" +
                      str(img_np.shape))
                ret = True
            except KeyboardInterrupt:
                print("interrupt received, stopping...")
                socket.close()
                context.term()
                ret = False
                cap.release()
        else:
            ret, img_np = cap.read()
            frameId += 1

        # read completely or raise exception
        if not ret:
            print("cannot get frame")
            break

        hanle_frame(args, frameId, img_np, logger, model, dummy_coco_dataset)
def main(args):
    logger = logging.getLogger(__name__)
    merge_cfg_from_file(args.cfg)
    cfg.NUM_GPUS = 1
    args.weights = cache_url(args.weights, cfg.DOWNLOAD_CACHE)
    assert_and_infer_cfg(cache_urls=False)
    model = infer_engine.initialize_model_from_cfg(args.weights)
    dataset_name = cfg.TEST.DATASETS[0]
    dummy_coco_dataset = JsonDataset(dataset_name)
    # dummy_coco_dataset = dummy_datasets.get_paris_dataset()

    vid_dir = '/coco/paris_dataset/PARIS_demo.mp4'
    cap = cv2.VideoCapture(vid_dir)
    ret, im = cap.read()
    count = 0
    while ret:
        im_name = str(count)
        out_name = os.path.join(args.output_dir,
                                '{}'.format(str(count) + '.jpg'))
        logger.info('Processing frame -> {}'.format(count))
        timers = defaultdict(Timer)
        t = time.time()
        with c2_utils.NamedCudaScope(0):
            cls_boxes, cls_segms, cls_keyps, cls_bodys = infer_engine.im_detect_all(
                model, im, None, timers=timers)
        logger.info('Inference time: {:.3f}s'.format(time.time() - t))
        for k, v in timers.items():
            logger.info(' | {}: {:.3f}s'.format(k, v.average_time))

        vis_im = vis_utils.vis_one_image(
            im[:, :, ::-1],  # BGR -> RGB for visualization
            im_name,
            args.output_dir,
            cls_boxes,
            cls_segms,
            cls_keyps,
            cls_bodys,
            dataset=dummy_coco_dataset,
            box_alpha=0.3,
            show_class=True,
            thresh=0.7,
            kp_thresh=2)
        # cv2.imshow('frame', vis_im)
        # cv2.waitKey(10)

        ret, im = cap.read()
        count += 1
    cap.release()
    cv2.destroyAllWindows()
Exemple #32
0
def setup():
    global dummy_coco_dataset
    cfg_file = 'configs/12_2017_baselines/e2e_mask_rcnn_R-101-FPN_2x.yaml'
    weights = 'https://dl.fbaipublicfiles.com/detectron/35861858/12_2017_baselines/e2e_mask_rcnn_R-101-FPN_2x.yaml.02_32_51.SgT4y1cO/output/train/coco_2014_train:coco_2014_valminusminival/generalized_rcnn/model_final.pkl'
    merge_cfg_from_file(cfg_file)
    cfg.NUM_GPUS = 1
    weights = cache_url(weights, cfg.DOWNLOAD_CACHE)
    assert_and_infer_cfg(cache_urls=False)
    assert not cfg.MODEL.RPN_ONLY, \
        'RPN models are not supported'
    assert not cfg.TEST.PRECOMPUTED_PROPOSALS, \
        'Models that require precomputed proposals are not supported'
    model = infer_engine.initialize_model_from_cfg(weights)
    dummy_coco_dataset = dummy_datasets.get_coco_dataset()
    return model
Exemple #33
0
def main(args):
    logger = logging.getLogger(__name__)

    merge_cfg_from_file(args.cfg)
    cfg.NUM_GPUS = 1
    args.weights = cache_url(args.weights, cfg.DOWNLOAD_CACHE)
    assert_and_infer_cfg(cache_urls=False)

    assert not cfg.MODEL.RPN_ONLY, \
        'RPN models are not supported'
    assert not cfg.TEST.PRECOMPUTED_PROPOSALS, \
        'Models that require precomputed proposals are not supported'

    model = infer_engine.initialize_model_from_cfg(args.weights)
    dummy_coco_dataset = dummy_datasets.get_coco_dataset()

    if os.path.isdir(args.im_or_folder):
        im_list = glob.iglob(args.im_or_folder + '/*.' + args.image_ext)
    else:
        im_list = [args.im_or_folder]

    for i, im_name in enumerate(im_list):
        #if i > 10:
        #    continue
        out_name = os.path.join(
            args.output_dir, '{}'.format(os.path.basename(im_name)[:-len(args.image_ext)] + 'mat')
        )
        
        im = cv2.imread(im_name)
        timers = defaultdict(Timer)
        t = time.time()
        with c2_utils.NamedCudaScope(0):
            cls_boxes, cls_segms, cls_keyps = infer_engine.im_detect_all(
                model, im, None, timers=timers
            )
        boxes, segms, keyps, classes = vis_utils.convert_from_cls_format(cls_boxes, cls_segms, cls_keyps)
        if boxes is None:
            continue

        segms = vis_utils.mask_util.decode(segms)

        valid_inds = np.greater(boxes[:, 4], 0.5)
        boxes = boxes[valid_inds, :]
        segms = segms[:, :, valid_inds]
        classes = np.array(classes)[valid_inds]
        class_names = np.asarray(([coco_to_pascal_name(coco_classes[c-1]) for c in classes]), dtype='object')

        sio.savemat(out_name, {'masks': segms, 'boxes': boxes, 'classes': class_names});
Exemple #34
0
def inference(cfg_path, weights, img_pillow, output_dir):
    logger = logging.getLogger(__name__)

    merge_cfg_from_file(cfg_path)
    #print( "cfg : ", cfg )
    assert_and_infer_cfg(cache_urls=False, make_immutable=False)

    cfg.NUM_GPUS = 1
    weights = cache_url(weights, cfg.DOWNLOAD_CACHE)
    model = infer_engine.initialize_model_from_cfg(weights)
    dummy_coco_dataset = dummy_datasets.get_coco_dataset()

    im_name = "test"
    #img_cv = cv2.imread(im_name)
    img_np = np.asarray(img_pillow)
    img_cv = cv2.cvtColor(img_np, cv2.COLOR_RGB2BGR)

    timers = defaultdict(Timer)
    t = time.time()
    with c2_utils.NamedCudaScope(0):
        cls_boxes, cls_segms, cls_keyps, cls_bodys = infer_engine.im_detect_all(
            model, img_cv, None, timers=timers)

    logger.info('Inference time: {:.3f}s'.format(time.time() - t))
    for k, v in timers.items():
        logger.info(' | {}: {:.3f}s'.format(k, v.average_time))

    vis_utils.vis_one_image(
        img_cv[:, :, ::-1],  # BGR -> RGB for visualization
        im_name,
        output_dir,
        cls_boxes,
        cls_segms,
        cls_keyps,
        cls_bodys,
        dataset=dummy_coco_dataset,
        box_alpha=0.3,
        show_class=True,
        thresh=0.7,
        kp_thresh=2)

    IUV_SaveName = os.path.basename(im_name).split('.')[0] + '_IUV.png'
    INDS_SaveName = os.path.basename(im_name).split('.')[0] + '_INDS.png'
    iuv_pillow = Image.open(os.path.join(output_dir,
                                         '{}'.format(IUV_SaveName)))
    inds_pillow = Image.open(
        os.path.join(output_dir, '{}'.format(INDS_SaveName)))
    return iuv_pillow, inds_pillow
Exemple #35
0
def main():
    args = parse_args()
    merge_cfg_from_file(args.cfg)
    merge_cfg_from_list(args.opts)
    assert_and_infer_cfg()
    model, blobs = convert_tools.load_model(args)

    convert_main_net(args, model.net.Proto(), blobs)
    if args.mask_dir:
        convert_mask_net(args, model.mask_net.Proto())
    if args.corresp:
        classes = getattr(dummy_datasets, 'get_{}_dataset'.format(args.corresp))().classes
        corresp = '\n'.join('{} {}'.format(i, classes[i]) for i, _ in enumerate(classes))
        with open(args.out_dir + '/corresp.txt', 'w') as f:
            f.write(corresp)
    return 0
                roi_data_loader._minibatch_queue.qsize(),
                cfg.DATA_LOADER.MINIBATCH_QUEUE_SIZE
            )
        )
        # Sleep to simulate the time taken by running a little network
        time.sleep(opts.sleep_time)
        # To inspect:
        # blobs = workspace.FetchBlobs(all_blobs)
        # from IPython import embed; embed()
    logger.info('Shutting down data loader...')
    roi_data_loader.shutdown()


if __name__ == '__main__':
    workspace.GlobalInit(['caffe2', '--caffe2_log_level=0'])
    logger = setup_logging(__name__)
    logger.setLevel(logging.DEBUG)
    logging.getLogger('detectron.roi_data.loader').setLevel(logging.INFO)
    np.random.seed(cfg.RNG_SEED)
    args = parse_args()
    logger.info('Called with args:')
    logger.info(args)
    if args.cfg_file is not None:
        merge_cfg_from_file(args.cfg_file)
    if args.opts is not None:
        merge_cfg_from_list(args.opts)
    assert_and_infer_cfg()
    logger.info('Running with config:')
    logger.info(pprint.pformat(cfg))
    main(args)
def main(args):
    glob_keypoints = []

    logger = logging.getLogger(__name__)

    merge_cfg_from_file(args.cfg)
    cfg.NUM_GPUS = 1
    args.weights = cache_url(args.weights, cfg.DOWNLOAD_CACHE)
    assert_and_infer_cfg(cache_urls=False)

    assert not cfg.MODEL.RPN_ONLY, \
        'RPN models are not supported'
    assert not cfg.TEST.PRECOMPUTED_PROPOSALS, \
        'Models that require precomputed proposals are not supported'

    model = infer_engine.initialize_model_from_cfg(args.weights)
    dummy_coco_dataset = dummy_datasets.get_coco_dataset()

    if os.path.isdir(args.im_or_folder):
        im_list = glob.iglob(args.im_or_folder + '/*' + '.png')
    else:
        im_list = [args.im_or_folder]
    im_list = sorted(im_list)
    for i, im_name in enumerate(im_list):
        out_name = os.path.join(
            args.output_dir, '{}'.format(os.path.basename(im_name) + '.' + args.output_ext)
        )
        logger.info('Processing {} -> {}'.format(im_name, out_name))
        im = cv2.imread(im_name)
        timers = defaultdict(Timer)
        t = time.time()
        with c2_utils.NamedCudaScope(0):
            cls_boxes, cls_segms, cls_keyps = infer_engine.im_detect_all(
                model, im, None, timers=timers
            )
        logger.info('Inference time: {:.3f}s'.format(time.time() - t))
        for k, v in timers.items():
            logger.info(' | {}: {:.3f}s'.format(k, v.average_time))
        if i == 0:
            logger.info(
                ' \ Note: inference on the first image will be slower than the '
                'rest (caches and auto-tuning need to warm up)'
            )

        vis_utils.vis_one_image(
            im[:, :, ::-1],  # BGR -> RGB for visualization
            im_name,
            args.output_dir,
            cls_boxes,
            cls_segms,
            cls_keyps,
            dataset=dummy_coco_dataset,
            box_alpha=0.3,
            show_class=True,
            thresh=args.thresh,
            kp_thresh=args.kp_thresh,
            ext=args.output_ext,
            out_when_no_box=args.out_when_no_box
        )

        cls_boxes_np = np.asarray(cls_boxes)
        cls_boxes_prob = cls_boxes_np[1][:,4]
        idx_max_prob = np.argmax(cls_boxes_prob)

        cls_keyps_max_prob = cls_keyps[1][idx_max_prob]
        pose_x_y_prob_after_softmax = cls_keyps_max_prob[[0,1,3]]
        glob_keypoints.append(np.transpose(pose_x_y_prob_after_softmax))

    dictionarry_keypoints={'S1': {'Directions 1' : np.asarray([glob_keypoints])}}
    metadata = {'layout_name': 'h36m', 'num_joints': 17, 'keypoints_symmetry': [[4, 5, 6, 11, 12, 13], [1, 2, 3, 14, 15, 16]]}
    #np.savez(os.path.join('/home/narvis/Dev/VideoPose3D/data', "data_2d_detections.npz"), metadata=metadata, positions_2d=dictionarry_keypoints)
    np.savez(os.path.join(args.output_dir, "data_2d_detections.npz"), metadata=metadata, positions_2d=dictionarry_keypoints)