Faster-RCNN的blob文件详解
pytorch编写Faster-RCNN的下载地址
目录下的blob文件的位置
im_list_to_blob()这个函数是转成统一长宽的图片格式,选择最大长和最大宽,最后用图片填充不够的地方用0补充。
def im_list_to_blob(ims):
max_shape = np.array([im.shape for im in ims]).max(axis=0)
num_images = len(ims)
blob = np.zeros((num_images, max_shape[0], max_shape[1], 3),
dtype=np.float32)
for i in xrange(num_images):
im = ims[i]
blob[i, 0:im.shape[0], 0:im.shape[1], :] = im
return blob
这个函数是将输入的图像转为blob格式,格式样式是 [ 图片数量,图片最大长,图片最大宽,RGB3通道 ] 。
输入的ims是以下这种数据,列表 [ numpy数组构成的图片1 ,...... ,numpy数组构成的图片n ]
im是图片,[im.shape for im in ims] 最后构成一个图像尺寸的列表 [ 图片1尺寸,....,图片n尺寸 ]是一维的,
然后np.array([im.shape for im in ims]) 一下就变成二维,格式如下,.max(axis=0)表示取纵轴最大的数字,就是取最大长和宽。
((图片1尺寸), ((图片1长,图片1宽,图片1深度),
................. = ....................................................
(图片n尺寸)) (图片n长,图片n宽,图片n深度))
最后往创建好的blob中填充图片值。
总程序
# --------------------------------------------------------
# Fast R-CNN
# Copyright (c) 2015 Microsoft
# Licensed under The MIT License [see LICENSE for details]
# Written by Ross Girshick
# --------------------------------------------------------
"""Blob helper functions."""
import numpy as np
# from scipy.misc import imread, imresize
import cv2
try:
xrange # Python 2
except NameError:
xrange = range # Python 3
def im_list_to_blob(ims):
"""Convert a list of images into a network input.
Assumes images are already prepared (means subtracted, BGR order, ...).
"""
max_shape = np.array([im.shape for im in ims]).max(axis=0)
num_images = len(ims)
blob = np.zeros((num_images, max_shape[0], max_shape[1], 3),
dtype=np.float32)
for i in xrange(num_images):
im = ims[i]
blob[i, 0:im.shape[0], 0:im.shape[1], :] = im
return blob
def prep_im_for_blob(im, pixel_means, target_size, max_size):
"""Mean subtract and scale an image for use in a blob."""
im = im.astype(np.float32, copy=False)
im -= pixel_means
# im = im[:, :, ::-1]
im_shape = im.shape
im_size_min = np.min(im_shape[0:2])
im_size_max = np.max(im_shape[0:2])
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) > max_size:
# im_scale = float(max_size) / float(im_size_max)
# im = imresize(im, im_scale)
im = cv2.resize(im, None, None, fx=im_scale, fy=im_scale,
interpolation=cv2.INTER_LINEAR)
return im, im_scale