Ejemplo n.º 1
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]

    pool = mp.Pool(mp.cpu_count())
    func = partial(process_image, model, args)
    pool.map(func, [im_name for im_name in im_list])
    pool.close()
    pool.join()
Ejemplo n.º 2
0
    def __init__(self,
                 config,
                 im_list,
                 model=None,
                 gpu_id=0):  # im_list passed from cityscapes dataset
        self.nb_features = config['nb_features']
        self.split = config['split']
        self.im_list = im_list

        workspace.GlobalInit(['caffe2', '--caffe2_log_level=0'])
        if not cfg.is_immutable(
        ):  # just in case feature extractor has not been set up already
            dset = b'cityscapes_fine_instanceonly_seg_' + self.split
            args = Namespace(
                cfg_file=MASK_RCNN_CONFIG,
                wait=True,
                multi_gpu_testing=False,
                range=None,  #[0, 3],
                opts=['OUTPUT_DIR', config['save']])

            merge_cfg_from_file(args.cfg_file)
            if args.opts is not None:
                merge_cfg_from_list(args.opts)
                assert_and_infer_cfg()

        if model is None or model == False:
            self.model = initialize_model_from_cfg(instanciate_head_also=True)
        else:
            self.model = model

        gpu_dev = caffe2_core.DeviceOption(caffe2_pb2.CUDA, gpu_id)
        name_scope = 'gpu_{}'.format(gpu_id)
        # Subsampler - originally inside the FPN network. But we don't want to predict the subsampled features.
        # Instead, we want to predict the features, and then use the same subsampling operator to obtain the subsampled features
        with caffe2_core.NameScope(name_scope):
            with caffe2_core.DeviceScope(gpu_dev):
                self.subsampler = caffe2_core.CreateOperator(
                    "MaxPool",  # operator
                    ["predicted_fpn_res5_2_sum"],  #input blobs
                    ["predicted_fpn_res5_2_sum_subsampled_2x"],  #output blobs
                    kernel=1,
                    pad=0,
                    stride=2,
                    deterministic=1)

        self.timers = {
            k: Timer()
            for k in [
                'im_detect_bbox', 'im_detect_mask', 'misc_bbox', 'misc_mask',
                'im_forward_backbone'
            ]
        }

        # For evaluation with respect to the dataset's gt, we save the prediction of the annotated frame for each sequence
        self.num_classes = cfg.MODEL.NUM_CLASSES
        self.num_images = len(self.im_list)
        self.all_boxes_ann_frame, self.all_segms_ann_frame, _ = empty_results(
            self.num_classes, self.num_images)
        self.id_sequences = []
        self.gpu_id = gpu_id
Ejemplo n.º 3
0
def main(args):
    merge_cfg_from_file(args.cfg)
    cfg.NUM_GPUS = 1
    for i, weights_file in enumerate(args.weights_list):
        args.weights_list[i] = cache_url(weights_file, cfg.DOWNLOAD_CACHE)
    assert_and_infer_cfg(cache_urls=False)

    preffix_list = args.preffix_list if len(args.preffix_list) \
        else [""] * len(args.weights_list)
    model = model_builder.create(cfg.MODEL.TYPE, train=False)
    # Initialize GPU from weights files
    for i, weights_file in enumerate(args.weights_list):
        nu.initialize_gpu_from_weights_file(model,
                                            weights_file,
                                            gpu_id=0,
                                            preffix=preffix_list[i])
    nu.broadcast_parameters(model)
    blobs = {}
    # Save all parameters
    for param in model.params:
        scoped_name = str(param)
        unscoped_name = c2_utils.UnscopeName(scoped_name)
        if unscoped_name not in blobs:
            if workspace.HasBlob(scoped_name):
                blobs[unscoped_name] = workspace.FetchBlob(scoped_name)
    # Save merged weights file
    save_object(dict(blobs=blobs), args.output_wts)
def generate_predicitions_from_frames(images, config_file, weights):
    """Generator yields inferred boxes and keypoints for each image in a provided iterable of images
    Args:
        images: iterable images
        config_file: Detectron configuration file
        weights: pretrained weights
    Returns:
        yields i, im, cls_boxes, cls_segms, cls_keyps
    """
    logger = logging.getLogger(__name__)
    merge_cfg_from_file(config_file)
    cfg.NUM_GPUS = 1
    weights = cache_url(weights, cfg.DOWNLOAD_CACHE)
    assert_and_infer_cfg(cache_urls=False)
    model = infer_engine.initialize_model_from_cfg(weights)
    for i, im in enumerate(images):
        logger.info("Processing frame {}".format(i))
        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)"
            )
        yield i, im, cls_boxes, cls_segms, cls_keyps
Ejemplo n.º 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)
    model = infer_engine.initialize_model_from_cfg(args.weights)

    ## setting params for each pedestrian
    ## params = {"im_name":input image path, "model":the model above, "out_name":output path,
    ##            'bbox':input bbox path (npy)} bbox need to have n * 4 shape with n is the number of bboxes
    ##            the four dimensions are (axis of left top point, h, w)
    params_list = []
    for folder in os.listdir(args.im_or_folder):
        os.system("mkdir -p " +
                  "/workspace/caozhangjie/DensePose/JAAD_result/" + folder)
        im_list = glob.iglob(args.im_or_folder + '/' + folder + '/*.' +
                             args.image_ext)
        for im_name in im_list:
            out_name = "/workspace/caozhangjie/DensePose/JAAD_result/" + folder + '/' + im_name.split(
                "/")[-1].split(".")[0] + ".pkl"
            img_name = '/data/JAAD_clip_images/' + folder + '.mp4/' + str(
                int(im_name.split("/")[-1].split(".")[0])) + '.jpg'
            params_list.append({
                "im_name": img_name,
                "model": model,
                "out_name": out_name,
                'bbox': im_name
            })
    #pickle.dump(params_list, open("JAAD_param_list.pkl", "wb"))

    #params_list = pickle.load(open("JAAD_param_list.pkl", "r"))
    for params in params_list:
        run_one_image(params)
 def __init__(self, model_config):
     merge_cfg_from_file(model_config)
     cfg.NUM_GPUS = 1
     assert_and_infer_cfg()
     print(cfg.TEST.WEIGHTS)
     self.__model = infer_engine.initialize_model_from_cfg(cfg.TEST.WEIGHTS)
     self.__dataset = JsonDataset(cfg.TRAIN.DATASETS[0])
Ejemplo n.º 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)
    model = infer_engine.initialize_model_from_cfg(args.weights)

    filenum = 0

    for root, dirs, files in os.walk(args.deepfashion):
        for file in files:
            if file.endswith(".jpg"):
                image_path = os.path.join(root, file)
                iuv = extract_iuv(image_path, model, infer_engine)

                if iuv is not None:
                    image_name = os.path.splitext(file)[0]
                    iuv_path = os.path.join(root, image_name + "_IUV.npy")
                    np.save(iuv_path, iuv)

                    iuv_img_path = os.path.join(root, image_name + "_IUV.png")
                    cv2.imwrite(iuv_img_path, iuv)
                else:
                    print("Could not extract IUV for: " + image_path)

                print('filenum: {:d}'.format(filenum))
                filenum = filenum + 1
Ejemplo n.º 8
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()
    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)
Ejemplo n.º 9
0
def init_caffe2():
    """Initialize caffe2 ONCE so we don't have to do it over and over.
    """
    workspace.GlobalInit(['caffe2', '--caffe2_log_level=0'])
    logger = logging.getLogger(__name__)

    weights = "/app/model_final.pkl"

    arg_cfg = os.path.join(
        os.environ['DETECTRON_HOME'],
        'configs/12_2017_baselines/e2e_mask_rcnn_R-101-FPN_2x.yaml')
    output_ext = 'pdf'

    merge_cfg_from_file(arg_cfg)
    cfg.NUM_GPUS = 1
    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, gpu_id=gpu_id)

    return model
Ejemplo n.º 10
0
    def __init__(
        self,
        cf='/detectron/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%3Acoco_2014_valminusminival/generalized_rcnn/model_final.pkl'
    ):

        workspace.GlobalInit(['caffe2', '--caffe2_log_level=0'])
        c2_utils.import_detectron_ops()
        # OpenCL may be enabled by default in OpenCV3; disable it because it's not
        # thread safe and causes unwanted GPU memory allocations.
        cv2.ocl.setUseOpenCL(False)
        # cfg path
        self.cfg = cf
        # weights path
        self.weights = weights
        self.weights = cache_url(self.weights, cfg.DOWNLOAD_CACHE)
        merge_cfg_from_file(self.cfg)
        cfg.NUM_GPUS = 1
        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(self.weights)
        dummy_coco_dataset = dummy_datasets.get_coco_dataset()
Ejemplo n.º 11
0
    def initialization(self):

        merge_cfg_from_file(self.MODEL_CFG_FILE)
        cfg.NUM_GPUS = 1
        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(
            self.MODEL_WEIGHTS_FILE)
        self.dummy_coco_dataset = dummy_datasets.get_coco_dataset()

        meta_data = pd.read_csv(self.THRESHOLDS_FILE).to_dict(orient='records')
        #print (meta_data)
        classes = ['background']
        thresholds = [1.1]
        for item in meta_data:
            name, thr = item['name'], item['threshold']
            classes.append(name)
            thresholds.append(thr)

        self.dummy_coco_dataset.classes = {
            i: name
            for i, name in enumerate(classes)
        }

        self.classes = classes
        self.thresholds = thresholds
Ejemplo n.º 12
0
def main(args):
    logger = logging.getLogger(__name__)
    merge_cfg_from_file(args.cfg)
    cfg.TEST.WEIGHTS = args.weights
    cfg.NUM_GPUS = 1
    assert_and_infer_cfg()
    model = infer_engine.initialize_model_from_cfg(args.weights)
    dummy_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):
        logger.info('Processing {}'.format(im_name))
        im = cv2.imread(im_name)
        timers = defaultdict(Timer)
        t = time.time()
        with c2_utils.NamedCudaScope(0):
            cls_scores, _, _ = infer_engine.im_detect_all(model,
                                                          im,
                                                          None,
                                                          timers=timers)
        logger.info('Inference time: {:.3f}s'.format(time.time() - t))
        cl = np.argmax(cls_scores)
        for k, v in timers.items():
            logger.info(' | {}: {:.3f}s'.format(k, v.average_time))
            logger.info(' | Class is: {}'.format(dummy_dataset.classes[cl]))
            logger.info(' | Class Confidance is: {:.2f}%'.format(
                cls_scores[cl] * 100))
        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)')
Ejemplo n.º 13
0
def main(args):

    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 im_name in 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)
        img1, img2 = image_utils.split_two(im)
        base_name = os.path.basename(im_name)
        name_txt = os.path.splitext(base_name)[0]
        single_process(args, dummy_coco_dataset, img1, base_name + "_1", model)
        single_process(args, dummy_coco_dataset, img2, base_name + "_2", model)
Ejemplo n.º 14
0
 def initModel(self, cfg_path, wts_path, labelmap):
     merge_cfg_from_file(cfg_path)
     # cfg.TEST.WEIGHTS = wts_path
     cfg.NUM_GPUS = 1
     assert_and_infer_cfg()
     self.labelmap = labelmap
     self.model = infer_engine.initialize_model_from_cfg(wts_path)
Ejemplo n.º 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)

    # extract source
    if os.path.isfile(args.source):
    	print('extracting source ...')
    	extract_and_save_iuv(args.source, args.outdir, model, infer_engine)
    else:
    	sys.exit("source not a file")

    # extract target(s)
    if os.path.isfile(args.target):
    	print('extracting target ...')
    	if os.path.isdir(args.target):
    		for idx, file in enumerate(os.listdir(args.target)):
    			if file.endswith(args.target_ext):
    				target_outdir = os.path.join(args.outdir, os.path.basename(args.target))
    				extract_and_save_iuv(file, target_outdir, model, infer_engine)
    			print('\t target file ... {:d}'.format(idx))
    	else:
    		extract_and_save_iuv(args.target, args.outdir, model, infer_engine)
    else:
    	sys.exit("target not a file")
Ejemplo n.º 16
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)

        _ = checkpoints.pop('final', None)
        for snapshot in sorted(checkpoints.keys(), reverse=True):
            test_model(checkpoints[snapshot], args.multi_gpu_testing,
                       args.opts)
            print('reprint snapshot name for the result: ', snapshot,
                  checkpoints[snapshot])
Ejemplo n.º 17
0
    def __init__(self,
                 myargv=None,
                 detectron_dir=os.path.expanduser(
                     os.path.join('~', 'libraries', 'detectron'))):
        workspace.GlobalInit(['caffe2'])
        setup_logging(__name__)
        self.logger = logging.getLogger(__name__)
        args = parse_args(myargv)

        # config
        # model_config_filename=os.path.join(detectron_dir, 'configs/12_2017_baselines/e2e_keypoint_rcnn_X-101-32x8d-FPN_s1x.yaml')
        model_config_filename = os.path.join(
            detectron_dir,
            'configs/12_2017_baselines/e2e_keypoint_rcnn_R-50-FPN_s1x.yaml')
        args.cfg = model_config_filename
        merge_cfg_from_file(args.cfg)
        cfg.NUM_GPUS = 1

        # weights
        # model_weights = 'https://dl.fbaipublicfiles.com/detectron/37732318/12_2017_baselines/e2e_keypoint_rcnn_X-101-32x8d-FPN_s1x.yaml.16_55_09.Lx8H5JVu/output/train/keypoints_coco_2014_train%3Akeypoints_coco_2014_valminusminival/generalized_rcnn/model_final.pkl'
        model_weights = 'https://dl.fbaipublicfiles.com/detectron/37697714/12_2017_baselines/e2e_keypoint_rcnn_R-50-FPN_s1x.yaml.08_44_03.qrQ0ph6M/output/train/keypoints_coco_2014_train%3Akeypoints_coco_2014_valminusminival/generalized_rcnn/model_final.pkl'
        cfg.DOWNLOAD_CACHE = os.path.join(detectron_dir,
                                          'detectron-download-cache')
        args.weights = cache_url(model_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'

        self.model = infer_engine.initialize_model_from_cfg(args.weights)
        self.dummy_coco_dataset = dummy_datasets.get_coco_dataset()
Ejemplo n.º 18
0
def main(args):
    logger = logging.getLogger(__name__)
    dummy_coco_dataset = dummy_datasets.get_coco_dataset()
    cfg_orig = load_cfg(envu.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, cls_bodys = None, 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_ , cls_bodys_= \
                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
        cls_bodys = cls_bodys_ if cls_bodys_ is not None else cls_bodys

        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))

    with open('test_vis.pkl', 'w') as f:
        pickle.dump(
            {
                'im': im,
                'cls_boxes': np.array(cls_boxes),
                'cls_bodys': np.array(cls_bodys)
            }, f)

    vis_utils.vis_one_image(im[:, :, ::-1],
                            args.im_file,
                            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)
    def setup_model_and_tracker(self, config_file):

        detectron_root = os.path.join(
            os.path.dirname(inspect.getfile(detectron)), os.pardir)

        if not os.path.exists(config_file):
            rospy.logerr(
                "config file '{}' does not exist. ".format(config_file) +
                "Please specify a valid model config file for the " +
                "model_config ros param. See " +
                "https://github.com/marinaKollmitz/mobilityaids_detector " +
                "for setup instructions")
            exit(0)  #TODO throw exception

        merge_cfg_from_file(config_file)

        #absolute output dir path
        cfg.OUTPUT_DIR = os.path.join(detectron_root, cfg.OUTPUT_DIR)

        weights_file = os.path.join(detectron_root, cfg.TEST.WEIGHTS)
        val_dataset = cfg.TRACK.VALIDATION_DATASET

        assert_and_infer_cfg()
        self.model = infer_engine.initialize_model_from_cfg(weights_file)

        #initialize tracker
        class_thresh, obs_model, meas_cov = validate_tracking_params(
            weights_file, val_dataset)
        self.tracker = Tracker(meas_cov, obs_model, use_hmm=True)
        self.cla_thresholds = class_thresh
Ejemplo n.º 20
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)
Ejemplo n.º 21
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
        outfile = os.path.join(args.output_dir,
                               im_base_name.replace('jpg', 'npy'))
        '''if outfile.exists():
            continue'''
        #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:

            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))
Ejemplo n.º 22
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):
        output_image_dir = os.path.join(args.output_dir, 'images')
        out_name = os.path.join(
            output_image_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,
        #    output_image_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
        #)

        _write_to_txt(cls_boxes, cls_segms, cls_keyps, im_name,
                      dummy_coco_dataset)
Ejemplo n.º 23
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)
Ejemplo n.º 24
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)
Ejemplo n.º 25
0
 def __init__(self, cfg_path, weights_path=""):
     logger = logging.getLogger(__name__)
     merge_cfg_from_file(args.cfg)
     cfg.NUM_GPUS = 1
     weights = cache_url(weights_path, cfg.DOWNLOAD_CACHE)
     assert_and_infer_cfg(cache_urls=False)
     self.model = infer_engine.initialize_model_from_cfg(weights)
     self.dummy_coco_dataset = dummy_datasets.get_coco_dataset()
Ejemplo n.º 26
0
 def test_merge_cfg_from_file(self):
     with tempfile.NamedTemporaryFile() as f:
         envu.yaml_dump(cfg, f)
         s = cfg.MODEL.TYPE
         cfg.MODEL.TYPE = 'dummy'
         assert cfg.MODEL.TYPE != s
         core_config.merge_cfg_from_file(f.name)
         assert cfg.MODEL.TYPE == s
Ejemplo n.º 27
0
 def test_merge_cfg_from_file(self):
     with tempfile.NamedTemporaryFile(mode='w') as f:
         yaml.dump(cfg, f)
         s = cfg.MODEL.TYPE
         cfg.MODEL.TYPE = 'dummy'
         assert cfg.MODEL.TYPE != s
         core_config.merge_cfg_from_file(f.name)
         assert cfg.MODEL.TYPE == s
Ejemplo n.º 28
0
def get_model(cfg_file, weights_file):
    merge_cfg_from_file(cfg_file)
    cfg.TRAIN.WEIGHTS = ''  # NOTE: do not download pretrained model weights
    cfg.TEST.WEIGHTS = weights_file
    cfg.NUM_GPUS = 1
    assert_and_infer_cfg()
    model = infer_engine.initialize_model_from_cfg()
    return model
Ejemplo n.º 29
0
def main(args):
    logger = logging.getLogger(__name__)
    dummy_nucoco_dataset = dummy_datasets.get_nucoco_dataset()
    cfg_orig = load_cfg(envu.yaml_dump(cfg))
    
    ## Load image
    coco = COCO_PLUS(args.ann_file, args.imgs_dir)
    image_id = coco.dataset['images'][args.im_ind]['id']
    img_path = os.path.join(args.imgs_dir, coco.imgs[image_id]["file_name"])
    im = cv2.imread(img_path)

    ## Get the proposals for this image
    proposals = rrpn_loader(args.rpn_pkl)
    proposal_boxes = proposals[image_id]['boxes']
    _proposal_scores = proposals[image_id]['scores']
    workspace.ResetWorkspace()

    ## run models
    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(img_path) + '.pdf')
    )
    logger.info('Processing {} -> {}'.format(img_path, out_name))

    vis_utils.vis_one_image(
        im[:, :, ::-1],
        img_path,
        args.output_dir,
        cls_boxes,
        cls_segms,
        cls_keyps,
        dataset=dummy_nucoco_dataset,
        box_alpha=0.3,
        show_class=True,
        thresh=0.7,
        kp_thresh=2
    )
Ejemplo n.º 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()
    submit_result = []
    result_file_name = 'detectron_val_result.txt'

    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) + '.pdf'))
        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)')

        result = vis_utils.vis_one_image_bbox(
            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)
        if result:
            submit_result.extend(result)
        logger.info('Image {}.'.format(i))

    # Write file
    with open(result_file_name, 'wb') as result_file:
        for item in submit_result:
            result_file.write("%s\n" % item)

    logger.info(
        'The result file has been written in {}.'.format(result_file_name))
Ejemplo n.º 31
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()

    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)

        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,
                                               cls_boxes,
                                               cls_segms,
                                               cls_keyps,
                                               cls_bodys,
                                               thresh=0.9,
                                               kp_thresh=2,
                                               dataset=dummy_coco_dataset,
                                               show_class=True)

        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)
        cv2.imshow('output', texout)
def main(args):
    logger = logging.getLogger(__name__)

    merge_cfg_from_file(args.cfg)
    cfg.NUM_GPUS = 4
    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)
    # model = infer_engine.initialize_model_from_cfg(args.weights,gpu_id=3)
    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]
    len_str = str(len(os.listdir(args.im_or_folder)))
    for i, im_name in enumerate(im_list):
        print('~~~~~~~~~~~~~~~~~' + str(i) + '/' + len_str +
              "~~~~~~~~~~~~~~~~~~~~~")
        out_name = os.path.join(
            args.output_dir, '{}'.format(os.path.basename(im_name) + '.jpg'))
        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(  #此处输出的cls_boxes包含类别信息
                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)')
        # ipdb.set_trace()
        # print(cls_boxes)
        # print(cls_segms)
        # print(cls_keyps)
        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.5,
            kp_thresh=2,
            ext='jpg')
def main():
    c2_utils.import_contrib_ops()
    c2_utils.import_detectron_ops()
    cv2.ocl.setUseOpenCL(False)
    args = parse_args()
    input_file = args.input_file
    workspace.GlobalInit(
        ['caffe2', '--caffe2_log_level=0', '--caffe2_gpu_memory_tracking=1'])
    merge_cfg_from_file(
        '/home/LAB/wusj/exp/KL-Loss/configs/e2e_faster_rcnn_R-50-FPN_2x_entropy.yaml'
    )
    assert_and_infer_cfg(cache_urls=False)
    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))
    workspace.ResetWorkspace()

    np.random.seed(cfg.RNG_SEED)
    with open(input_file, 'r') as f:
        config = json.load(f)

        parameter = config['parameter']
        inference_id = parameter['inferenceId']

        dataset = parameter['dataSetName']
        model_file = parameter['modelPkl']
        task_id = inference_id
        image_id_list = parameter['imageIdList']

        load_data(dataset, image_id_list)
        roidb, result = test_net(model_file, dataset)

        tmp = []
        for i, entry in enumerate(roidb):
            tmp.append(entry)
        tmp.sort(cmp=compare)

        selectNum = parameter['selectNum']
        output = {}
        # time.sleep(3)
        output['annotationList'] = []
        output['selectImageIdList'] = []
        output['remainImageIdList'] = []
        for i in range(selectNum):
            output['annotationList'].append(result[str(tmp[i]['id'])])
            output['selectImageIdList'].append(str(tmp[i]['id']))
        for i in range(parameter['selectNum'], len(parameter['imageIdList'])):
            output['remainImageIdList'].append(str(tmp[i]['id']))
        output['remainImageNum'] = len(output['remainImageIdList'])
        output['inferenceId'] = inference_id
        output['dataSetName'] = dataset

        result_output_dir = '/home/LAB/wusj/fastwash_tmp/inference/'
        with open(result_output_dir + 'result_' + task_id, 'wt') as f2:
            json.dump(output, f2)
Ejemplo n.º 34
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
        )
Ejemplo n.º 35
0
 def test_deprecated_key_from_file(self):
     # You should see logger messages like:
     #   "Deprecated config key (ignoring): MODEL.DILATION"
     with tempfile.NamedTemporaryFile() as f:
         cfg2 = copy.deepcopy(cfg)
         cfg2.MODEL.DILATION = 2
         envu.yaml_dump(cfg2, f)
         with self.assertRaises(AttributeError):
             _ = cfg.MODEL.DILATION  # noqa
         core_config.merge_cfg_from_file(f.name)
         with self.assertRaises(AttributeError):
             _ = cfg.MODEL.DILATION  # noqa
Ejemplo n.º 36
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
    )
Ejemplo n.º 37
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
Ejemplo n.º 38
0
 def test_renamed_key_from_file(self):
     # You should see logger messages like:
     #  "Key EXAMPLE.RENAMED.KEY was renamed to EXAMPLE.KEY;
     #  please update your config"
     with tempfile.NamedTemporaryFile() as f:
         cfg2 = copy.deepcopy(cfg)
         cfg2.EXAMPLE = AttrDict()
         cfg2.EXAMPLE.RENAMED = AttrDict()
         cfg2.EXAMPLE.RENAMED.KEY = 'foobar'
         envu.yaml_dump(cfg2, f)
         with self.assertRaises(AttributeError):
             _ = cfg.EXAMPLE.RENAMED.KEY  # noqa
         with self.assertRaises(KeyError):
             core_config.merge_cfg_from_file(f.name)
Ejemplo n.º 39
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
Ejemplo n.º 40
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)
Ejemplo n.º 41
0
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)