Note
主要是之前在跑目標(biāo)檢測代碼時發(fā)現(xiàn)很多變量名都roixxx的,雖然之前看過相關(guān)的知識但是感覺又被繞暈了,同時發(fā)現(xiàn)了一片寫ROI Pooling之后反向傳播相關(guān)的知乎專欄于是做個筆記記錄下。同時為了日后更方便進行修改與調(diào)整加深印象。
FasterRCNN的結(jié)構(gòu)

faster-RCNN
輸入圖像的預(yù)處理
目標(biāo)檢測輸入圖像預(yù)處理的原因:
- 數(shù)據(jù)集圖片大小不一,需要將大小不一的圖片在一個batch內(nèi)組成統(tǒng)一的size
- 寬高比過于極端的圖片需要特定的裁剪方案
- 將不同大小的圖片縮放到同一尺度
處理過程如下圖所示

Preprocess
輸入圖像的縮放結(jié)果是將短邊尺度縮放到TargetSize,但如果這樣子縮放長邊超過了maxSize,那么縮放長邊,圖像的長寬比理論上是不變的。
圖像預(yù)處理代碼如下
def _get_image_blob(im):
"""Converts an image into a network input.
Arguments:
im (ndarray): a color image in BGR order
Returns:
blob (ndarray): a data blob holding an image pyramid
im_scale_factors (list): list of image scales (relative to im) used
in the image pyramid
"""
im_orig = im.astype(np.float32, copy=True)
im_orig -= cfg.PIXEL_MEANS
im_shape = im_orig.shape
im_size_min = np.min(im_shape[0:2])
im_size_max = np.max(im_shape[0:2])
processed_ims = []
im_scale_factors = []
for target_size in cfg.TEST.SCALES:
im_scale = float(target_size) / float(im_size_min)
# Prevent the biggest axis from being more than MAX_SIZE
if np.round(im_scale * im_size_max) > cfg.TEST.MAX_SIZE:
im_scale = float(cfg.TEST.MAX_SIZE) / float(im_size_max)
im = cv2.resize(im_orig, None, None, fx=im_scale, fy=im_scale,
interpolation=cv2.INTER_LINEAR)
im_scale_factors.append(im_scale)
processed_ims.append(im)
# Create a blob to hold the input images
blob = im_list_to_blob(processed_ims)
return blob, np.array(im_scale_factors)
cfg(配置文件)里的TEST.SCALES其實是TargetSize默認(rèn)為600,TEST.MAX_SIZE默認(rèn)為1000。
上述操作目前只解決了尺度相同的問題,但若要batch size > 1還需要其他處理
數(shù)據(jù)的載入
數(shù)據(jù)通過roibatchLoader載入dataset,然后將dataset裝入dataloader,此變換在minibatch.py內(nèi),實現(xiàn)后通過roibatchLoacer類內(nèi)調(diào)用
image->feature maps
一般情況下通過一系列卷積層(vgg16 or resnet)得到一個基準(zhǔn)特征圖(base feature map)上圖中黑黑的那塊。base feature map是512通道的。
forward
# feed image data to base model to obtain base feature map
base_feat = self.RCNN_base(im_data)
RPN網(wǎng)絡(luò)

RPN
參考
Object Detection and Classification using R-CNNs
一文讀懂Faster RCNN
faster-rcnn代碼閱讀理解(2)