SoFunction
Updated on 2024-10-28

python with caffe to change the order of channels

Put the channel in front:

image = (path + file)

image = (image, (48, 48), interpolation=cv2.INTER_CUBIC) aaaa= (image,(2, 0, 1)) print(aaaa)

Image original shape: (48, 48, 3), changed shape: (3,48,48)

Note: reshape does not solve channel conversion problems.

The problem of channel conversion when pycaffe does recognition:

One thing to note is that the channel for color images in Caffe is the BGR format, and the image storage is [0,255].

.load_image way view plai cop

image = .load_image(image_file) #load image

Use .load_image() to read in RGB format and 0~1 (float)

So set transformer.set_raw_scale('data',255) (scale to 0~255) in transformer before proceeding with recognition

and transformer.set_channel_swap('data',(2,1,0) (transforms RGB to BGR)

# python stores images as [0, 1] and caffe stores them as [0, 255], so a conversion is required
transformer.set_raw_scale('data', 255)  # Scaled to [0, 255].
transformer.set_channel_swap('data', (2,1,0)) #switching channel,Send the image from theRGBchange intoBGR(caffeThe picture in theBGRspecification,而原始specification是RGB,So to convert) 

2. Use () to read the image

() interface to read the image, read in the direct BGR format and 0~255

So no more scaling to [0,255] and channel transform [2,1,0], no transformer.set_raw_scale('data',255) and transformer.set_channel_swap('data',(2,1,0))

3. Use PIL to read the picture

For color images, regardless of the image format is PNG, or BMP, or JPG, in PIL, using the Image module's open() function to open, the mode of the returned image object is "RGB". For grayscale image, no matter the image format is PNG, BMP, or JPG, after opening, its mode is "L". So you need to convert the format, but do not need to scale to [0, 255].

data = ((+img_list)) 
data = (data,(2,0,1))# Conversion channels
data[[0,2],...] = data[[2,0],...] #RGB→BGR 

4. for matlab

The format of blobs in Caffe is N*C*H*W, which are Number, Channel, and WidthHeight and WidthWidth, respectively.

Whereas in matlab it is width before height, i.e. [w, h], the channel of the image is RGB

So it needs to be converted accordingly:

im_data = im (:,:,[3,2,1]) ; %RGB to BGR

im_data = permute(im_data, [2,1,3]); % rotate height and width

Finally, share a typical python recognition code for Caffe:

# -*- coding: utf-8 -*- 
""" 
Created on Sun May 28 16:00:47 2017 
@author: fancp,CPU mode under #windows
""" 
import numpy as np 
import caffe 
import sys 
caffe_root = 'F:/Caffe' ######### your own Caffe path
(0, caffe_root + '/python') 
 
size = 227 #Training image sizes
image_file = 'F:/.../.../'#Image Path
model_def = 'F:/.../.../'#deploy model file location
model_weights = 'F:/.../.../_iter_20000.caffemodel'# Position of the trained model
net = (model_def, model_weights, )  
 
# Load the mean file
mu = (caffe_root + '/python/caffe/imagenet/ilsvrc_2012_mean.npy') ###caffe comes with its own files.
mu = (1).mean(1) # average over pixels to obtain the mean (BGR) pixel values 
########################### These 5 sentences below are equivalent to the two sentences above, choose one #################
#blob = .caffe_pb2.BlobProto() 
#mean_data = open( '' , 'rb' ).read() 
#(mean_data) 
#mu = (.blobproto_to_array(blob)) 
#mu = (1).mean(1).mean(1) 
############################################################################## 
#Image Preprocessing
transformer = ({'data': ['data'].}) ## Set the shape of the image (1,3,227,227), the size is specified by the deploy file.
# python reads the image file format H×W×K, need to be converted to K×H×W
transformer.set_transpose('data', (2,0,1)) # Change the order of the dimensions, from the original image (227,227,3) to (3,227,227)
transformer.set_mean('data', mu)   # Each channel minus the mean
 
# python stores images as [0, 1] and caffe stores them as [0, 255], so a conversion is required
transformer.set_raw_scale('data', 255)  # Scale to between [0, 255].
transformer.set_channel_swap('data', (2,1,0)) #swap channels to change the image from RGB to BGR (the image in caffe is in BGR format, while the original format is RGB, so it has to be converted)
['data'].reshape(1,3,size, size) # Convert the input image format to a suitable format (same as for the deploy file)
#Above this sentence, the first parameter: the number of images the second parameter: the number of channels the third parameter: the height of the image the fourth parameter: the width of the image
 
image = .load_image(image_file) #Load the picture
# Use the above for the just-loaded image #
['data'].data[...] = ('data', image)  
 
### perform classification 
caffe.set_mode_cpu() 
output = () 
#print output 
output_prob = output['prob'][0].argmax() # Give the category with the highest probability,need to correspond themselves to the categories we agreed upon 

This above python with caffe to change the order of the channel is all that I have shared with you.