python and caffe method of changing channel order
- 2020-12-07 04:07:03
- OfStack
Put the channel in front:
image = cv2.imread(path + file)
image = cv2.resize(image, (48, 48), interpolation=cv2.INTER_CUBIC) aaaa= np.transpose(image,(2, 0, 1)) print(aaaa)
Image shape :(48,48, 3), shape :(3,48,48)
Note: reshape does not address channel conversion issues
Channel conversion problem during pycaffe identification:
One thing to note is that the channel of color image in Caffe is BGR format and the image storage is [0,255].
caffe.io.load_image mode view plai cop
image = ES28en.io.load_image (image_file) # load image
caffe. load_image() read in RGB format and 0~1(float)
So set transformer.set_raw_ES48en ('data',255) in transformer before identifying (scale to 0~255)
And transformer.set_channel_swap ('data',(2,1,0)(convert RGB to BGR)
# python Store the image as [0, 1] And the caffe Store the image as [0, 255] So we need to 1 A conversion
transformer.set_raw_scale('data', 255) # Zoom to the [0 . 255] between
transformer.set_channel_swap('data', (2,1,0)) # Swap the channel to the image by RGB into BGR ( caffe In the picture is BGR Format, whereas the original format is RGB , so convert)
2. Use cv2.imread () to read images
cv2.imread () interface reads the image, and reads it directly in BGR format and 0~255
So no need to scale to [0,255] and channel transform [2,1,0], no need for transformer.set_raw_scale ('data',255) and transformer.set_channel_swap ('data',(2,1,0))
3. Use PIL to read images
For color images, whether the image format is PNG, BMP, or JPG, in PIL, when the open() function of the Image module is turned on, the image object returned is in the mode "RGB". For grayscale images, whether the image format is PNG, BMP, or JPG, the mode is "L" when opened. So you need to convert the format, but you don't need to scale to [0,255]
data = np.array(Image.open(self.dataRoot+img_list))
data = np.transpose(data,(2,0,1))# Conversion channel
data[[0,2],...] = data[[2,0],...] #RGB - BGR
4. For matlab
The blobs format in Caffe is N*C*H*W, which is the number Number, the number of channels Channel, and the width Height and the width Width
In matlab, the width is first followed by the height, that is [w, h], and the image channel is RGB
Therefore, the corresponding transformation is needed:
im_data = im (:,:,[3,2,1]) ; %RGB to BGR
im_data = permute (im_data,1,3 [2]); Spin height and width
Finally, share a typical python identification code of Caffe:
# -*- coding: utf-8 -*-
"""
Created on Sun May 28 16:00:47 2017
@author: fancp . #windows Under the CPU model
"""
import numpy as np
import caffe
import sys
caffe_root = 'F:/Caffe' ######### Your own Caffe The path
sys.path.insert(0, caffe_root + '/python')
size = 227 # Training picture size
image_file = 'F:/.../.../nihao.jpg'# Image path
model_def = 'F:/.../.../deploy.prototxt'#deploy Model file location
model_weights = 'F:/.../.../_iter_20000.caffemodel'# Model positions after training
net = caffe.Net(model_def, model_weights, caffe.TEST)
# Load mean file
mu = np.load(caffe_root + '/python/caffe/imagenet/ilsvrc_2012_mean.npy') ###caffe Built-in file
mu = mu.mean(1).mean(1) # average over pixels to obtain the mean (BGR) pixel values
########################### The following 5 The same as the above two sentences, choose them 1#################
#blob = caffe.proto.caffe_pb2.BlobProto()
#mean_data = open( 'mean.binaryproto' , 'rb' ).read()
#blob.ParseFromString(mean_data)
#mu = np.array(caffe.io.blobproto_to_array(blob))
#mu = mu.mean(1).mean(1).mean(1)
##############################################################################
# Image preprocessing
transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape}) ## Image-Setting shape format (1,3,227,227) , the size of deploy The file specified
# python The image file format is H x W x K , need to be converted to K x H x W
transformer.set_transpose('data', (2,0,1)) # Change the dimension order from the original picture (227,227,3) into (3,227,227)
transformer.set_mean('data', mu) # Each of these channels minus the mean
# python Store the image as [0, 1] And the caffe Store the image as [0, 255] So we need to 1 A conversion
transformer.set_raw_scale('data', 255) # Zoom to 【 0 . 255 】 between
transformer.set_channel_swap('data', (2,1,0)) # Swap the channel to the image by RGB into BGR ( caffe In the picture is BGR Format, whereas the original format is RGB , so convert)
net.blobs['data'].reshape(1,3,size, size) # Convert the input image format to the appropriate format (and deploy Same file)
# The above sentence, no 1 Parameters: Number of images The first 2 A parameter : the number of channels The first 3 Three parameters: image height The first 4 10 parameters: picture width
image = caffe.io.load_image(image_file) # Loading pictures
# With the above transformer.preprocess To handle just loaded images
net.blobs['data'].data[...] = transformer.preprocess('data', image)
### perform classification
caffe.set_mode_cpu()
output = net.forward()
#print output
output_prob = output['prob'][0].argmax() # What is the most probable category? We have to go to the category we agreed on