Faster-RCNN_TF icon indicating copy to clipboard operation
Faster-RCNN_TF copied to clipboard

How to add conditions to Faster RCNN region proposal function?

Open hadign20 opened this issue 7 years ago • 2 comments

I'm trying to add 2 or 3 simple conditions (if statement) to the faster_RCNN network structure so it would produce less regions based on these conditions. I don't know how the entire code works exactly and I just wanted to know where (which file) exactly am I supposed to add the conditions.

There is prepare_roidb function in FasteTF/lib/gt_data_layer/roidb.py file, where the sizes of rois are defined:

def prepare_roidb(imdb):
    """Enrich the imdb's roidb by adding some derived quantities that
    are useful for training. This function precomputes the maximum
    overlap, taken over ground-truth boxes, between each ROI and
    each ground-truth box. The class with maximum overlap is also
    recorded.
    """
    cache_file = os.path.join(imdb.cache_path, imdb.name + '_gt_roidb_prepared.pkl')
    if os.path.exists(cache_file):
        with open(cache_file, 'rb') as fid:
            imdb._roidb = cPickle.load(fid)
        print '{} gt roidb prepared loaded from {}'.format(imdb.name, cache_file)
        return

    roidb = imdb.roidb
    for i in xrange(len(imdb.image_index)):
        roidb[i]['image'] = imdb.image_path_at(i)
        boxes = roidb[i]['boxes']
        labels = roidb[i]['gt_classes']
        info_boxes = np.zeros((0, 18), dtype=np.float32)

        if boxes.shape[0] == 0:
            roidb[i]['info_boxes'] = info_boxes
            continue

        # compute grid boxes
        s = PIL.Image.open(imdb.image_path_at(i)).size
        image_height = s[1]
        image_width = s[0]
        boxes_grid, cx, cy = get_boxes_grid(image_height, image_width)
        
        # for each scale
        for scale_ind, scale in enumerate(cfg.TRAIN.SCALES):
            boxes_rescaled = boxes * scale

            # compute overlap
            overlaps = bbox_overlaps(boxes_grid.astype(np.float), boxes_rescaled.astype(np.float))
            max_overlaps = overlaps.max(axis = 1)
            argmax_overlaps = overlaps.argmax(axis = 1)
            max_classes = labels[argmax_overlaps]

            # select positive boxes
            fg_inds = []
            for k in xrange(1, imdb.num_classes):
                fg_inds.extend(np.where((max_classes == k) & (max_overlaps >= cfg.TRAIN.FG_THRESH))[0])

            if len(fg_inds) > 0:
                gt_inds = argmax_overlaps[fg_inds]
                # bounding box regression targets
                gt_targets = _compute_targets(boxes_grid[fg_inds,:], boxes_rescaled[gt_inds,:])
                # scale mapping for RoI pooling
                scale_ind_map = cfg.TRAIN.SCALE_MAPPING[scale_ind]
                scale_map = cfg.TRAIN.SCALES[scale_ind_map]
                # contruct the list of positive boxes
                # (cx, cy, scale_ind, box, scale_ind_map, box_map, gt_label, gt_sublabel, target)
                info_box = np.zeros((len(fg_inds), 18), dtype=np.float32)
                info_box[:, 0] = cx[fg_inds]
                info_box[:, 1] = cy[fg_inds]
                info_box[:, 2] = scale_ind
                info_box[:, 3:7] = boxes_grid[fg_inds,:]
                info_box[:, 7] = scale_ind_map
                info_box[:, 8:12] = boxes_grid[fg_inds,:] * scale_map / scale
                info_box[:, 12] = labels[gt_inds]
                info_box[:, 14:] = gt_targets
                info_boxes = np.vstack((info_boxes, info_box))

        roidb[i]['info_boxes'] = info_boxes

    with open(cache_file, 'wb') as fid:
        cPickle.dump(roidb, fid, cPickle.HIGHEST_PROTOCOL)
    print 'wrote gt roidb prepared to {}'.format(cache_file)

I want to define some conditions so that it won't produce any regions with width or height more than a value like max_length. I don't know where exactly I should add these conditions.

I would appreciate if experts could help me out with this. thanks

hadign20 avatar Jun 08 '17 10:06 hadign20

In training, lib/fast_rcnn/train.py imports these modules or functions:

from fast_rcnn.config import cfg import gt_data_layer.roidb as gdl_roidb import roi_data_layer.roidb as rdl_roidb from roi_data_layer.layer import RoIDataLayer from utils.timer import Timer import numpy as np import os import tensorflow as tf import sys from tensorflow.python.client import timeline import time

In testing, lib/fast_rcnn/test.py imports these:

from fast_rcnn.config import cfg, get_output_dir import argparse from utils.timer import Timer import numpy as np import cv2 from utils.cython_nms import nms, nms_new from utils.boxes_grid import get_boxes_grid import cPickle import heapq from utils.blob import im_list_to_blob import os import math from rpn_msr.generate import imdb_proposals_det import tensorflow as tf from fast_rcnn.bbox_transform import clip_boxes, bbox_transform_inv import matplotlib.pyplot as plt from tensorflow.python.client import timeline import time

I guess the thing you are looking for should be in somewhere in these bold-typed files/modules/functions, or directly in train.py and test.py

cemysf avatar Jun 20 '17 15:06 cemysf

Hi @hadi-ghnd, did you figure out how to do this? I have the same problem

fedhere avatar Jul 24 '18 15:07 fedhere