コード例 #1
0
def sample(model, model_path, output_name='gen/gen'):
    pred = PredictConfig(session_init=get_model_loader(model_path),
                         model=model,
                         input_names=['z'],
                         output_names=[output_name, 'z'])
    pred = SimpleDatasetPredictor(pred, RandomZData((100, args.z_dim)))
    for o in pred.get_result():
        o = o[0] + 1
        o = o * 128.0
        o = np.clip(o, 0, 255)
        o = o[:, :, :, ::-1]
        stack_patches(o, nr_row=10, nr_col=10, viz=True)
コード例 #2
0
ファイル: DCGAN.py プロジェクト: ahuirecome/tensorpack
def sample(model, model_path, output_name='gen/gen'):
    pred = PredictConfig(
        session_init=get_model_loader(model_path),
        model=model,
        input_names=['z'],
        output_names=[output_name, 'z'])
    pred = SimpleDatasetPredictor(pred, RandomZData((100, args.z_dim)))
    for o in pred.get_result():
        o = o[0] + 1
        o = o * 128.0
        o = np.clip(o, 0, 255)
        o = o[:, :, :, ::-1]
        stack_patches(o, nr_row=10, nr_col=10, viz=True)
コード例 #3
0
ファイル: Image2Image.py プロジェクト: jiajiehe/CS238Project
def sample(datadir, model_path):
    pred = PredictConfig(session_init=get_model_loader(model_path),
                         model=Model(),
                         input_names=['input', 'output'],
                         output_names=['viz'])

    imgs = glob.glob(os.path.join(datadir, '*.jpg'))
    ds = ImageFromFile(imgs, channel=3, shuffle=True)
    ds = MapData(ds, lambda dp: split_input(dp[0]))
    ds = AugmentImageComponents(ds, [imgaug.Resize(256)], (0, 1))
    ds = BatchData(ds, 6)

    pred = SimpleDatasetPredictor(pred, ds)
    for o in pred.get_result():
        o = o[0][:, :, :, ::-1]
        stack_patches(o, nr_row=3, nr_col=2, viz=True)
コード例 #4
0
def infer_folder(datadir, model_path):
    pred = PredictConfig(
        session_init=get_model_loader(model_path),
        model=Model(),
        input_names=['input', 'output'],
        output_names=['viz'])

    imgs = glob.glob(os.path.join(datadir, '*' + IMG_TYPE))
    ds = CustomDataFlow(datadir, 'test', shuffle=True)
    ds = AugmentImageComponents(ds, [imgaug.Resize(256)], (0, 1))
    ds = BatchData(ds, 6)

    pred = SimpleDatasetPredictor(pred, ds)
    for o in pred.get_result():
        o = o[0][:, :, :, ::-1]
        stack_patches(o, nr_row=3, nr_col=2, viz=True)
コード例 #5
0
ファイル: train.py プロジェクト: woojoung/tensorpack
def visualize(model, model_path, nr_visualize=100, output_dir='output'):
    """
    Visualize some intermediate results (proposals, raw predictions) inside the pipeline.
    """
    df = get_train_dataflow()  # we don't visualize mask stuff
    df.reset_state()

    pred = OfflinePredictor(
        PredictConfig(model=model,
                      session_init=get_model_loader(model_path),
                      input_names=['image', 'gt_boxes', 'gt_labels'],
                      output_names=[
                          'generate_{}_proposals/boxes'.format(
                              'fpn' if cfg.MODE_FPN else 'rpn'),
                          'generate_{}_proposals/probs'.format(
                              'fpn' if cfg.MODE_FPN else 'rpn'),
                          'fastrcnn_all_probs',
                          'final_boxes',
                          'final_probs',
                          'final_labels',
                      ]))

    if os.path.isdir(output_dir):
        shutil.rmtree(output_dir)
    utils.fs.mkdir_p(output_dir)
    with tqdm.tqdm(total=nr_visualize) as pbar:
        for idx, dp in itertools.islice(enumerate(df.get_data()),
                                        nr_visualize):
            img = dp[0]
            if cfg.MODE_MASK:
                gt_boxes, gt_labels, gt_masks = dp[-3:]
            else:
                gt_boxes, gt_labels = dp[-2:]

            rpn_boxes, rpn_scores, all_probs, \
                final_boxes, final_probs, final_labels = pred(img, gt_boxes, gt_labels)

            # draw groundtruth boxes
            gt_viz = draw_annotation(img, gt_boxes, gt_labels)
            # draw best proposals for each groundtruth, to show recall
            proposal_viz, good_proposals_ind = draw_proposal_recall(
                img, rpn_boxes, rpn_scores, gt_boxes)
            # draw the scores for the above proposals
            score_viz = draw_predictions(img, rpn_boxes[good_proposals_ind],
                                         all_probs[good_proposals_ind])

            results = [
                DetectionResult(*args)
                for args in zip(final_boxes, final_probs, final_labels,
                                [None] * len(final_labels))
            ]
            final_viz = draw_final_outputs(img, results)

            viz = tpviz.stack_patches(
                [gt_viz, proposal_viz, score_viz, final_viz], 2, 2)

            if os.environ.get('DISPLAY', None):
                tpviz.interactive_imshow(viz)
            cv2.imwrite("{}/{:03d}.png".format(output_dir, idx), viz)
            pbar.update()
コード例 #6
0
def apply(model, model_path, images, ground_truth=None):
    left = cv2.imread(images[0])
    h, w = left.shape[:2]
    newh = (h // 64) * 64
    neww = (w // 64) * 64
    aug = imgaug.CenterCrop((newh, neww))
    left = aug.augment(left)

    predict_func = OfflinePredictor(
        PredictConfig(model=model(height=newh, width=neww),
                      session_init=get_model_loader(model_path),
                      input_names=['left', 'right'],
                      output_names=['prediction']))

    for right in images[1:]:
        right = aug.augment(cv2.imread(right))

        left_input, right_input = [
            x.astype('float32').transpose(2, 0, 1)[None, ...]
            for x in [left, right]
        ]
        output = predict_func(left_input, right_input)[0].transpose(0, 2, 3, 1)
        flow = Flow()

        img = flow.visualize(output[0])
        patches = [left, right, img * 255.]
        if ground_truth is not None:
            patches.append(flow.visualize(Flow.read(ground_truth)) * 255.)
        img = viz.stack_patches(patches, 2, 2)

        cv2.imshow('flow output', img)
        cv2.imwrite('flow_prediction.png', img)
        cv2.waitKey(0)

        left = right
コード例 #7
0
ファイル: Image2Image.py プロジェクト: tobyma/tensorpack
def sample(datadir, model_path):
    pred = PredictConfig(
        session_init=get_model_loader(model_path),
        model=Model(),
        input_names=['input', 'output'],
        output_names=['viz'])

    imgs = glob.glob(os.path.join(datadir, '*.jpg'))
    ds = ImageFromFile(imgs, channel=3, shuffle=True)
    ds = MapData(ds, lambda dp: split_input(dp[0]))
    ds = AugmentImageComponents(ds, [imgaug.Resize(256)], (0, 1))
    ds = BatchData(ds, 6)

    pred = SimpleDatasetPredictor(pred, ds)
    for o in pred.get_result():
        o = o[0][:, :, :, ::-1]
        stack_patches(o, nr_row=3, nr_col=2, viz=True)
コード例 #8
0
ファイル: train.py プロジェクト: aljosaosep/mprcnn
def visualize(model_path, nr_visualize=50, output_dir='output'):
    df = get_train_dataflow_coco()  # we don't visualize mask stuff
    df.reset_state()

    pred = OfflinePredictor(
        PredictConfig(model=Model(),
                      session_init=get_model_loader(model_path),
                      input_names=['image', 'gt_boxes', 'gt_labels'],
                      output_names=[
                          'generate_rpn_proposals/boxes',
                          'generate_rpn_proposals/probs',
                          'fastrcnn_all_probs',
                          'final_boxes',
                          'final_probs',
                          'final_labels',
                      ]))

    if os.path.isdir(output_dir):
        shutil.rmtree(output_dir)
    utils.fs.mkdir_p(output_dir)
    with tqdm.tqdm(total=nr_visualize) as pbar:
        for idx, dp in itertools.islice(enumerate(df.get_data()),
                                        nr_visualize):
            img, _, _, gt_boxes, gt_labels = dp

            rpn_boxes, rpn_scores, all_probs, \
                final_boxes, final_probs, final_labels = pred(img, gt_boxes, gt_labels)

            # draw groundtruth boxes
            gt_viz = draw_annotation(img, gt_boxes, gt_labels)
            # draw best proposals for each groundtruth, to show recall
            proposal_viz, good_proposals_ind = draw_proposal_recall(
                img, rpn_boxes, rpn_scores, gt_boxes)
            # draw the scores for the above proposals
            score_viz = draw_predictions(img, rpn_boxes[good_proposals_ind],
                                         all_probs[good_proposals_ind])

            if config.USE_SECOND_HEAD:
                results = [
                    SecondDetectionResult(*args)
                    for args in zip(final_boxes, final_probs, final_labels,
                                    [None] * len(final_labels))
                ]
            else:
                results = [
                    DetectionResult(*args)
                    for args in zip(final_boxes, final_probs, final_labels,
                                    [None] * len(final_labels))
                ]
            final_viz = draw_final_outputs(img, results)

            viz = tpviz.stack_patches(
                [gt_viz, proposal_viz, score_viz, final_viz], 2, 2)

            if os.environ.get('DISPLAY', None):
                tpviz.interactive_imshow(viz)
            cv2.imwrite("{}/{:03d}.png".format(output_dir, idx), viz)
            pbar.update()
コード例 #9
0
def sample(model_path, output_name, BATCH_SIZE, Net, res):
    #output_name = 'gen/toRGBtemp_128_256/output'
    BATCH_SIZE = int(BATCH_SIZE)
    pred = PredictConfig(
        session_init=get_model_loader(model_path),
        model=Net.Model(),
        input_names=['z'],
        output_names=[output_name, 'z'])
    inputNoise = RandomZData((BATCH_SIZE, 512))
    #inputNoise = ZData((BATCH_SIZE, 512))
    pred = SimpleDatasetPredictor(pred, inputNoise)
    its = 0
    for o in pred.get_result():
        o = o[0] + 1
        o = o * 128.0
        o = np.clip(o, 0, 255)
        o = o[:, :, :, ::-1]
        stack_patches(o, nr_row=10, nr_col=10, viz=True)
コード例 #10
0
def sample(model_path):
    pred = OfflinePredictor(
        PredictConfig(session_init=get_model_loader(model_path),
                      model=Model(),
                      input_names=['z_code', 'z_noise'],
                      output_names=['gen/viz']))

    # sample all one-hot encodings (10 times)
    z_cat = np.tile(np.eye(10), [10, 1])
    # sample continuos variables from -2 to +2 as mentioned in the paper
    z_uni = np.linspace(-2.0, 2.0, num=100)
    z_uni = z_uni[:, None]

    IMG_SIZE = 400

    while True:
        # only categorical turned on
        z_noise = np.random.uniform(-1, 1, (100, NOISE_DIM))
        zc = np.concatenate((z_cat, z_uni * 0, z_uni * 0), axis=1)
        o = pred(zc, z_noise)[0]
        viz1 = viz.stack_patches(o, nr_row=10, nr_col=10)
        viz1 = cv2.resize(viz1, (IMG_SIZE, IMG_SIZE))

        # show effect of first continous variable with fixed noise
        zc = np.concatenate((z_cat, z_uni, z_uni * 0), axis=1)
        o = pred(zc, z_noise * 0)[0]
        viz2 = viz.stack_patches(o, nr_row=10, nr_col=10)
        viz2 = cv2.resize(viz2, (IMG_SIZE, IMG_SIZE))

        # show effect of second continous variable with fixed noise
        zc = np.concatenate((z_cat, z_uni * 0, z_uni), axis=1)
        o = pred(zc, z_noise * 0)[0]
        viz3 = viz.stack_patches(o, nr_row=10, nr_col=10)
        viz3 = cv2.resize(viz3, (IMG_SIZE, IMG_SIZE))

        canvas = viz.stack_patches([viz1, viz2, viz3],
                                   nr_row=1,
                                   nr_col=3,
                                   border=5,
                                   bgcolor=(255, 0, 0))

        viz.interactive_imshow(canvas)
コード例 #11
0
ファイル: train.py プロジェクト: quanlzheng/tensorpack
def visualize(model, model_path, nr_visualize=100, output_dir='output'):
    """
    Visualize some intermediate results (proposals, raw predictions) inside the pipeline.
    """
    df = get_train_dataflow()   # we don't visualize mask stuff
    df.reset_state()

    pred = OfflinePredictor(PredictConfig(
        model=model,
        session_init=get_model_loader(model_path),
        input_names=['image', 'gt_boxes', 'gt_labels'],
        output_names=[
            'generate_{}_proposals/boxes'.format('fpn' if cfg.MODE_FPN else 'rpn'),
            'generate_{}_proposals/scores'.format('fpn' if cfg.MODE_FPN else 'rpn'),
            'fastrcnn_all_scores',
            'output/boxes',
            'output/scores',
            'output/labels',
        ]))

    if os.path.isdir(output_dir):
        shutil.rmtree(output_dir)
    utils.fs.mkdir_p(output_dir)
    with tqdm.tqdm(total=nr_visualize) as pbar:
        for idx, dp in itertools.islice(enumerate(df), nr_visualize):
            img = dp[0]
            if cfg.MODE_MASK:
                gt_boxes, gt_labels, gt_masks = dp[-3:]
            else:
                gt_boxes, gt_labels = dp[-2:]

            rpn_boxes, rpn_scores, all_scores, \
                final_boxes, final_scores, final_labels = pred(img, gt_boxes, gt_labels)

            # draw groundtruth boxes
            gt_viz = draw_annotation(img, gt_boxes, gt_labels)
            # draw best proposals for each groundtruth, to show recall
            proposal_viz, good_proposals_ind = draw_proposal_recall(img, rpn_boxes, rpn_scores, gt_boxes)
            # draw the scores for the above proposals
            score_viz = draw_predictions(img, rpn_boxes[good_proposals_ind], all_scores[good_proposals_ind])

            results = [DetectionResult(*args) for args in
                       zip(final_boxes, final_scores, final_labels,
                           [None] * len(final_labels))]
            final_viz = draw_final_outputs(img, results)

            viz = tpviz.stack_patches([
                gt_viz, proposal_viz,
                score_viz, final_viz], 2, 2)

            if os.environ.get('DISPLAY', None):
                tpviz.interactive_imshow(viz)
            cv2.imwrite("{}/{:03d}.png".format(output_dir, idx), viz)
            pbar.update()
コード例 #12
0
def sample(model_path):
    pred = OfflinePredictor(PredictConfig(
        session_init=get_model_loader(model_path),
        model=Model(),
        input_names=['z_code', 'z_noise'],
        output_names=['gen/viz']))

    # sample all one-hot encodings (10 times)
    z_cat = np.tile(np.eye(10), [10, 1])
    # sample continuos variables from -2 to +2 as mentioned in the paper
    z_uni = np.linspace(-2.0, 2.0, num=100)
    z_uni = z_uni[:, None]

    IMG_SIZE = 400

    while True:
        # only categorical turned on
        z_noise = np.random.uniform(-1, 1, (100, NOISE_DIM))
        zc = np.concatenate((z_cat, z_uni * 0, z_uni * 0), axis=1)
        o = pred(zc, z_noise)[0]
        viz1 = viz.stack_patches(o, nr_row=10, nr_col=10)
        viz1 = cv2.resize(viz1, (IMG_SIZE, IMG_SIZE))

        # show effect of first continous variable with fixed noise
        zc = np.concatenate((z_cat, z_uni, z_uni * 0), axis=1)
        o = pred(zc, z_noise * 0)[0]
        viz2 = viz.stack_patches(o, nr_row=10, nr_col=10)
        viz2 = cv2.resize(viz2, (IMG_SIZE, IMG_SIZE))

        # show effect of second continous variable with fixed noise
        zc = np.concatenate((z_cat, z_uni * 0, z_uni), axis=1)
        o = pred(zc, z_noise * 0)[0]
        viz3 = viz.stack_patches(o, nr_row=10, nr_col=10)
        viz3 = cv2.resize(viz3, (IMG_SIZE, IMG_SIZE))

        canvas = viz.stack_patches(
            [viz1, viz2, viz3],
            nr_row=1, nr_col=3, border=5, bgcolor=(255, 0, 0))

        viz.interactive_imshow(canvas)
コード例 #13
0
def sample(model_path):
    pred = PredictConfig(session_init=get_model_loader(model_path),
                         model=Model(),
                         input_names=['label', 'z'],
                         output_names=['gen/gen'])

    ds = MapData(RandomZData((100, 100)),
                 lambda dp: [np.arange(100) % 10, dp[0]])
    pred = SimpleDatasetPredictor(pred, ds)
    for o in pred.get_result():
        o = o[0] * 255.0
        viz = stack_patches(o, nr_row=10, nr_col=10)
        viz = cv2.resize(viz, (800, 800))
        interactive_imshow(viz)
コード例 #14
0
def sample(model_path):
    pred = PredictConfig(
        session_init=get_model_loader(model_path),
        model=Model(),
        input_names=['label', 'z'],
        output_names=['gen/gen'])

    ds = MapData(RandomZData((100, 100)),
                 lambda dp: [np.arange(100) % 10, dp[0]])
    pred = SimpleDatasetPredictor(pred, ds)
    for o in pred.get_result():
        o = o[0] * 255.0
        viz = stack_patches(o, nr_row=10, nr_col=10)
        viz = cv2.resize(viz, (800, 800))
        interactive_imshow(viz)
コード例 #15
0
def visualize(model_path, nr_visualize=50, output_dir='output'):
    pred = OfflinePredictor(
        PredictConfig(model=Model(),
                      session_init=get_model_loader(model_path),
                      input_names=['image', 'gt_boxes', 'gt_labels'],
                      output_names=[
                          'generate_rpn_proposals/boxes',
                          'generate_rpn_proposals/probs',
                          'fastrcnn_all_probs',
                          'fastrcnn_fg_probs',
                          'fastrcnn_fg_boxes',
                      ]))
    df = get_train_dataflow()
    df.reset_state()

    if os.path.isdir(output_dir):
        shutil.rmtree(output_dir)
    utils.fs.mkdir_p(output_dir)
    with tqdm.tqdm(total=nr_visualize) as pbar:
        for idx, dp in itertools.islice(enumerate(df.get_data()),
                                        nr_visualize):
            img, _, _, gt_boxes, gt_labels = dp

            rpn_boxes, rpn_scores, all_probs, fg_probs, fg_boxes = pred(
                img, gt_boxes, gt_labels)

            gt_viz = draw_annotation(img, gt_boxes, gt_labels)
            proposal_viz, good_proposals_ind = draw_proposal_recall(
                img, rpn_boxes, rpn_scores, gt_boxes)
            score_viz = draw_predictions(img, rpn_boxes[good_proposals_ind],
                                         all_probs[good_proposals_ind])

            fg_boxes = clip_boxes(fg_boxes, img.shape[:2])
            fg_viz = draw_predictions(img, fg_boxes, fg_probs)

            results = nms_fastrcnn_results(fg_boxes, fg_probs)
            final_viz = draw_final_outputs(img, results)

            viz = tpviz.stack_patches(
                [gt_viz, proposal_viz, score_viz, fg_viz, final_viz], 2, 3)

            if os.environ.get('DISPLAY', None):
                tpviz.interactive_imshow(viz)
            cv2.imwrite("{}/{:03d}.png".format(output_dir, idx), viz)
            pbar.update()
コード例 #16
0
def predict_unlabeled(model,
                      model_path,
                      nr_visualize=100,
                      output_dir='output_patch_samples'):
    """Predict the pseudo label information of unlabeled data."""

    assert cfg.EVAL.PSEUDO_INFERENCE, 'set cfg.EVAL.PSEUDO_INFERENCE=True'
    df, dataset_size = get_eval_unlabeled_dataflow(cfg.DATA.TRAIN,
                                                   return_size=True)
    df.reset_state()
    predcfg = PredictConfig(
        model=model,
        session_init=SmartInit(model_path),
        input_names=['image'],  # ['image', 'gt_boxes', 'gt_labels'],
        output_names=[
            'generate_{}_proposals/boxes'.format(
                'fpn' if cfg.MODE_FPN else 'rpn'),
            'generate_{}_proposals/scores'.format(
                'fpn' if cfg.MODE_FPN else 'rpn'),
            'fastrcnn_all_scores',
            'output/boxes',
            'output/scores',  # score of the labels
            'output/labels',
        ])
    pred = OfflinePredictor(predcfg)

    if os.path.isdir(output_dir):
        if os.path.isfile(os.path.join(output_dir, 'pseudo_data.npy')):
            os.remove(os.path.join(output_dir, 'pseudo_data.npy'))
        if not os.path.isdir(os.path.join(output_dir, 'vis')):
            os.makedirs(os.path.join(output_dir, 'vis'))
        else:
            shutil.rmtree(os.path.join(output_dir, 'vis'))
            fs.mkdir_p(output_dir + '/vis')
    else:
        fs.mkdir_p(output_dir)
        fs.mkdir_p(output_dir + '/vis')
    logger.warning('-' * 100)
    logger.warning('Write to {}'.format(output_dir))
    logger.warning('-' * 100)

    with tqdm.tqdm(total=nr_visualize) as pbar:
        for idx, dp in itertools.islice(enumerate(df), nr_visualize):
            img, img_id = dp  # dp['image'], dp['img_id']
            rpn_boxes, rpn_scores, all_scores, \
                final_boxes, final_scores, final_labels = pred(img)
            outs = {
                'proposals_boxes': rpn_boxes,  # (?,4)
                'proposals_scores': rpn_scores,  # (?,)
                'boxes': final_boxes,
                'scores': final_scores,
                'labels': final_labels
            }
            ratios = [10,
                      10]  # [top 20% as background, bottom 20% as background]
            bg_ind, fg_ind = custom.find_bg_and_fg_proposals(all_scores,
                                                             ratios=ratios)

            bg_viz = draw_predictions(img, rpn_boxes[bg_ind],
                                      all_scores[bg_ind])

            fg_viz = draw_predictions(img, rpn_boxes[fg_ind],
                                      all_scores[fg_ind])

            results = [
                DetectionResult(*args)
                for args in zip(final_boxes, final_scores, final_labels,
                                [None] * len(final_labels))
            ]
            final_viz = draw_final_outputs(img, results)

            viz = tpviz.stack_patches([bg_viz, fg_viz, final_viz], 2, 2)

            if os.environ.get('DISPLAY', None):
                tpviz.interactive_imshow(viz)
            assert cv2.imwrite('{}/vis/{:03d}.png'.format(output_dir, idx),
                               viz)
            pbar.update()
    logger.info('Write {} samples to {}'.format(nr_visualize, output_dir))

    ## Parallel inference the whole unlabled data
    pseudo_preds = collections.defaultdict(list)

    num_tower = max(cfg.TRAIN.NUM_GPUS, 1)
    graph_funcs = MultiTowerOfflinePredictor(predcfg, list(
        range(num_tower))).get_predictors()
    dataflows = [
        get_eval_unlabeled_dataflow(cfg.DATA.TRAIN,
                                    shard=k,
                                    num_shards=num_tower)
        for k in range(num_tower)
    ]

    all_results = multithread_predict_dataflow(dataflows, graph_funcs)

    for id, result in tqdm.tqdm(enumerate(all_results)):
        img_id = result['image_id']
        outs = {
            'proposals_boxes':
            result['proposal_box'].astype(np.float16),  # (?,4)
            'proposals_scores':
            result['proposal_score'].astype(np.float16),  # (?,)
            # 'frcnn_all_scores': result['frcnn_score'].astype(np.float16),
            'boxes': result['bbox'].astype(np.float16),  # (?,4)
            'scores': result['score'].astype(np.float16),  # (?,)
            'labels': result['category_id'].astype(np.float16)  # (?,)
        }
        pseudo_preds[img_id] = outs
    logger.warn('Writing to {}'.format(
        os.path.join(output_dir, 'pseudo_data.npy')))
    try:
        dd.io.save(os.path.join(output_dir, 'pseudo_data.npy'), pseudo_preds)
    except RuntimeError:
        logger.error('Save failed. Check reasons manually...')
コード例 #17
0
def collect_samples(model,
                    model_path,
                    nr_visualize=100,
                    output_dir='output_patch_samples'):
    """
    Visualize some intermediate results (proposals, raw predictions) inside the
    pipeline.
    """
    df = get_train_dataflow()
    df.reset_state()

    pred = OfflinePredictor(
        PredictConfig(model=model,
                      session_init=SmartInit(model_path),
                      input_names=['image', 'gt_boxes', 'gt_labels'],
                      output_names=[
                          'generate_{}_proposals/boxes'.format(
                              'fpn' if cfg.MODE_FPN else 'rpn'),
                          'generate_{}_proposals/scores'.format(
                              'fpn' if cfg.MODE_FPN else 'rpn'),
                          'fastrcnn_all_scores',
                          'output/boxes',
                          'output/scores',
                          'output/labels',
                      ]))

    if os.path.isdir(output_dir):
        shutil.rmtree(output_dir)
    fs.mkdir_p(output_dir)
    with tqdm.tqdm(total=nr_visualize) as pbar:
        for idx, dp in itertools.islice(enumerate(df), nr_visualize):
            img, gt_boxes, gt_labels = dp['image'], dp['gt_boxes'], dp[
                'gt_labels']

            rpn_boxes, rpn_scores, all_scores, \
                final_boxes, final_scores, final_labels = pred(img, gt_boxes, gt_labels)
            # draw groundtruth boxes
            gt_viz = draw_annotation(img, gt_boxes, gt_labels)

            ratios = [10,
                      10]  #[top 20% as background, bottom 20% as background]
            bg_ind, fg_ind = custom.find_bg_and_fg_proposals(all_scores,
                                                             ratios=ratios)

            bg_viz = draw_predictions(img, rpn_boxes[bg_ind],
                                      all_scores[bg_ind])

            fg_viz = draw_predictions(img, rpn_boxes[fg_ind],
                                      all_scores[fg_ind])

            # draw best proposals for each groundtruth, to show recall
            proposal_viz, good_proposals_ind = draw_proposal_recall(
                img, rpn_boxes, rpn_scores, gt_boxes)
            # draw the scores for the above proposals
            score_viz = draw_predictions(img, rpn_boxes[good_proposals_ind],
                                         all_scores[good_proposals_ind])

            results = [
                DetectionResult(*args)
                for args in zip(final_boxes, final_scores, final_labels,
                                [None] * len(final_labels))
            ]
            final_viz = draw_final_outputs(img, results)

            viz = tpviz.stack_patches([gt_viz, bg_viz, fg_viz, final_viz], 2,
                                      2)

            if os.environ.get('DISPLAY', None):
                tpviz.interactive_imshow(viz)
            cv2.imwrite('{}/{:03d}.png'.format(output_dir, idx), viz)
            pbar.update()
コード例 #18
0
def do_visualize(model, model_path, nr_visualize=100, output_dir='output'):
    """
    Visualize some intermediate results (proposals, raw predictions) inside the pipeline.
    """
    df = get_train_dataflow()  # we don't visualize mask stuff
    df.reset_state()

    pred = OfflinePredictor(
        PredictConfig(
            model=model,
            session_init=get_model_loader(model_path),
            input_names=['images', 'orig_image_dims', 'gt_boxes', 'gt_labels'],
            output_names=[
                'generate_{}_proposals_topk_per_image/boxes'.format(
                    'fpn' if cfg.MODE_FPN else 'rpn'),
                'generate_{}_proposals_topk_per_image/scores'.format(
                    'fpn' if cfg.MODE_FPN else 'rpn'),
                'fastrcnn_all_scores',
                'output/boxes',
                'output/scores',
                'output/labels',
            ]))

    if os.path.isdir(output_dir):
        shutil.rmtree(output_dir)
    utils.fs.mkdir_p(output_dir)
    with tqdm.tqdm(total=nr_visualize) as pbar:
        for idx, dp in itertools.islice(enumerate(df), nr_visualize):
            img, gt_boxes, gt_labels = dp['images'], dp['gt_boxes'], dp[
                'gt_labels']
            orig_shape = img.shape[:2]
            rpn_boxes, rpn_scores, all_scores, \
                final_boxes, final_scores, final_labels = pred(np.expand_dims(img, axis=0),
                                                               np.expand_dims(np.array(img.shape), axis=0),
                                                               np.expand_dims(gt_boxes, axis=0),
                                                               np.expand_dims(gt_labels, axis=0))

            # draw groundtruth boxes
            gt_viz = draw_annotation(img, gt_boxes, gt_labels)
            # draw best proposals for each groundtruth, to show recall
            # custom op creates different shape for boxes, convert back to original
            rpn_boxes = np.array([i[1:] for i in rpn_boxes])
            proposal_viz, good_proposals_ind = draw_proposal_recall(
                img, rpn_boxes, rpn_scores, gt_boxes)
            # draw the scores for the above proposals
            score_viz = draw_predictions(img, rpn_boxes[good_proposals_ind],
                                         all_scores[good_proposals_ind])

            results = [
                DetectionResult(*args)
                for args in zip(final_boxes, final_scores, final_labels,
                                [None] * len(final_labels))
            ]
            final_viz = draw_final_outputs(img, results)

            viz = tpviz.stack_patches(
                [gt_viz, proposal_viz, score_viz, final_viz], 2, 2)

            if os.environ.get('DISPLAY', None):
                tpviz.interactive_imshow(viz)
            cv2.imwrite("{}/{:03d}.png".format(output_dir, idx), viz)
            pbar.update()