Mjas
Mjas

Reputation: 129

Reshaping image in Pyfaster RCNN CAFFE model

I am working on a project to train the Pyfaster RCNN model using CAFFE. test.prototxt uses the below input parameters:

name: "ZF"

input: "data"
input_shape {
  dim: 1
  dim: 3
  dim: 224
  dim: 224
}

When demo.py is called this prototxt file is used. Can someone please tell me where exactly the the demo image gets reshaped to the above dimensions. I have traced that all the way back to the fast_rcnn.test.py file which has a function called im_detect. that has a line which I believe does the reshaping:

def im_detect(net, im, boxes=None):
    """Detect object classes in an image given object proposals.

    Arguments:
        net (caffe.Net): Fast R-CNN network to use
        im (ndarray): color image to test (in BGR order)
        boxes (ndarray): R x 4 array of object proposals or None (for RPN)

    Returns:
        scores (ndarray): R x K array of object class scores (K includes
            background as object category 0)
        boxes (ndarray): R x (4*K) array of predicted bounding boxes
    """
    blobs, im_scales = _get_blobs(im, boxes)

    # When mapping from image ROIs to feature map ROIs, there's some aliasing
    # (some distinct image ROIs get mapped to the same feature ROI).
    # Here, we identify duplicate feature ROIs, so we only compute features
    # on the unique subset.
    if cfg.DEDUP_BOXES > 0 and not cfg.TEST.HAS_RPN:
        v = np.array([1, 1e3, 1e6, 1e9, 1e12])
        hashes = np.round(blobs['rois'] * cfg.DEDUP_BOXES).dot(v)
        _, index, inv_index = np.unique(hashes, return_index=True,
                                        return_inverse=True)
        blobs['rois'] = blobs['rois'][index, :]
        boxes = boxes[index, :]

    if cfg.TEST.HAS_RPN:
        im_blob = blobs['data']
        blobs['im_info'] = np.array(
            [[im_blob.shape[2], im_blob.shape[3], im_scales[0]]],
            dtype=np.float32)

    # reshape network inputs
    net.blobs['data'].reshape(*(blobs['data'].shape))
    if cfg.TEST.HAS_RPN:
        net.blobs['im_info'].reshape(*(blobs['im_info'].shape))
    else:
        net.blobs['rois'].reshape(*(blobs['rois'].shape))

But I am still not able to figure out how can I get to the file/code where these dimensions are defined.

Any help would be appreciated.

Upvotes: 1

Views: 169

Answers (1)

Shai
Shai

Reputation: 114866

Look at the line

  # reshape network inputs
  net.blobs['data'].reshape(*(blobs['data'].shape))

As you can see, the input blob 'data' is reshaped according to the input image size. Once you forward caffe will reshape all consequent blobs according to the input shape

Upvotes: 1

Related Questions