在Pyfaster RCNN CAFFE模型中重塑图像

时间:2017-10-05 16:45:35

标签: caffe

我正在开发一个使用CAFFE训练Pyfaster RCNN模型的项目。 test.prototxt使用以下输入参数:

name: "ZF"

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

当调用demo.py时,使用此原型文件。有人可以告诉我在哪里将演示图像重新整形为上述尺寸。我已经追溯到fast_rcnn.test.py文件,该文件有一个名为im_detect的函数。有一条线我相信它会重塑:

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

但我仍然无法弄清楚如何找到定义这些尺寸的文件/代码。

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:1)

看一下

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

如您所见,输入blob 'data'根据输入图像大小进行了重新整形。一旦forward caffe将根据输入形状重塑所有后续blob