previous: Own distorted understanding
Some time ago with the MATLAB feature, after all, I am small white, matlab is the easiest for me to program (although, Daniel's structure is very good, I can only waste time complexity, alas ~ ~ ~), but the effect is still possible.
Recently need to rewrite the code with Python, from yesterday day to this morning, I use Python features and MATLAB is not the same .... Oh, my God.... How could it be. The same structure, the same parameters, the same picture, how could it not be the same.
later, I carefully compared the two code and found the following two points need special attention.
1. Note that in Python
Transformer=caffe.io.transformer ({' Data ': net.blobs[' data '].data.shape})
The size of the picture in this sentence is (160,160,3)
The size of the picture in Transformer.set_transpose (' Data ', (2,0,1)) is then changed (3,160,160)
and Transformer.set_mean (' Data ', Np.load (meanfile)), mean file size is (3,160,160), so in order to accommodate the mean file dimension, deliberately change the data dimension.
However, into the Caffe, the dimension to become (160,160,3) Ah, (good trouble, but the great God must have a good method, I do not know, I hope you will advise me ~ ~ ~ Here, Xinyu thanked everyone ~ ~ ~), so, added a sentence transformer.set_channel_ Swap (' Data ', (2,1,0)). Here why is (2,1,0), not (1,2,0), is because to and my Matlab match, my matlab is height*width*3, so here is the case. You should also pay attention to this feature when you mention it.
2. Great attention.
Matlab in the import of pictures is (0,255) is put, but. Python imported into the Caffe is not OH. Caffe.io.load_image (image), Caffe the imported picture is (0,1).
Oh, my God, I've been on this one all night and I've found this problem.
Yes, if you want the same result as your MATLAB, then you have to write Caffe.io.load_image (image) *255.
After tossing, finally the same ...
This is very important, we must pay attention to it.
Summary:
1 Dimension problem
2 Caffe Input picture problem
Post: The kindness of netizens help
When I thought I was particularly rational, I had an online friend (csdn: nusit_305, thanks to him), telling me that my understanding was wrong, from my understanding of the Python library, and the Caffe Python interface is not deep. Misled everyone. I'm sorry.
The following is the correct explanation:
(1) Python from the image Library to read the image is H*w*c dimension order, Caffe is the use of OPENCV, read the image is C*h*w dimension order, Io.set_transpose () function is to achieve h*w*c conversion to c*h*w, so that unified into n*c*h *W the data format in a BLOB.
(2) Read the image is normal RGB channel storage, using Io.set_channel_swap () is to convert RGB to BGR Channel order.
(3) is not caffe imported picture is (0,1), because the Python interface to use the Matplotlib library, the library load the image value is [0,1], you can use the interface function Set_raw_scale (' data ', 255) Implementation [0,1]–> [0,255].