**If you find content helpful and want to quote this blog, please provide the link [ayst123] ( http://blog.youkuaiyun.com/ayst123/article/details/44223321 )
correct me if I am wrong
BGR or RGB?
Caffe uses opencv to convert image to datum. The channels will be disordered to BGR rather than RGB by OpenCV. It means all blobs in caffe are in the BGR format.
When doing test, we have to make image be in the BGR format.
The prediction code
import caffe
###################################set mode
caffe.set_mode_cpu()
net = caffe.Net(caffe_root + 'models/bvlc_reference_caffenet/deploy.prototxt',caffe_root +'models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel',caffe.TEST)
# input preprocessing: 'data' is the name of the input blob == net.inputs[0]
transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})
transformer.set_transpose('data', (2,0,1))
transformer.set_mean('data', np.load(caffe_root + 'python/caffe/imagenet/ilsvrc_2012_mean.npy').mean(1).mean(1)) # mean pixel
transformer.set_raw_scale('data', 255) # the reference model operates on images in [0,255] range instead of [0,1]
transformer.set_channel_swap('data', (2,1,0)) # the reference model has channels in BGR order instead of RGB
###########################################predict
net.blobs['data'].reshape(1,3,227,227)
net.blobs['data'].data[...] = transformer.preprocess('data', caffe.io.load_image(caffe_root + 'examples/images/cat.jpg'))
out = net.forward()
Explanation
caffe.io.load_image converts image to numpy, which range is [0,1].
def load_image(filename, color=True):
"""
Load an image converting from grayscale or alpha as needed.
Take
filename: string
color: flag for color format. True (default) loads as RGB while False
loads as intensity (if image is already grayscale).
Give
image: an image with type np.float32 in range [0, 1]
of size (H x W x 3) in RGB or
of size (H x W x 1) in grayscale.
"""
img = skimage.img_as_float(skimage.io.imread(filename)).astype(np.float32)
if img.ndim == 2:
img = img[:, :, np.newaxis]
if color:
img = np.tile(img, (1, 1, 3))
elif img.shape[2] == 4:
img = img[:, :, :3]
return img
transformer. preprocess
net.blobs['data'].data[...] = transformer.preprocess('data', caffe.io.load_image(caffe_root + 'examples/images/cat.jpg'))
do
- convert to single
- resize to input dimensions (preserving number of channels)
- transpose dimensions to K x H x W
- reorder channels (for instance color to BGR)
- scale raw input (e.g. from [0, 1] to [0, 255] for ImageNet models)
- subtract mean
- scale feature
If loading image directly, the python will make its range be [0,1]. That is why we need to set scale raw
forward
out = net.forward()
It will output all output blob. If we want to add additional blob as output, it is better to use
out = net.forward( [‘blob_name’] )
**If you find content helpful and want to quote this blog, please provide the link [ayst123] ( http://blog.youkuaiyun.com/ayst123/article/details/44223321 )