If you have trained a model with 1-dimensional gray image, and want to classify another gray image, the following is the hack worked for me:
- copy the offical
- specify input_dim as 1, 1, x, x in deploy.prototxt
- change all call to
caffe.io.load_image(fname)in classify.py to
caffe.io.load_image(fname, False)because if you do not specify the second parameter as
Truewill be used by default, the meaning of the second parameter is to tell
load_imagewhether the image is color or gray, if it’s in color, then the returned image will have shape (width, height, 3) or (width, height, 4) depending on whether the alpha channel exists. If you specify
False, the shape will be (width, height, 1) as you want.
- specify –channel_swap ‘0’ in
python classify.pybecause this value is to reorder RGB to BGR, let’s say we have an image
imis in numpy array format, and
im.shape = (10, 10, 3), then caffe will do
im = im[:, :, channel_wap]to swap channels, if you do not specify
--channel_swap, it will be
"2,1,0"by default, then in caffe,
im = im[:, :, [2, 1, 0]], but the gray image’s shape is really (10, 10, 1) (if you follow the 2 step), so an
index out of boundsexception will be raised. So just specify ‘0’ to
--channel_swap, then caffe will run
im = im[:, :, ], that’s fine.
then just use the official classify.py.
Here is the gist of
test.sh worked for me.
Hope it will work for you too.