caffe: Check failed: bottom[0]->num_axes() == first_spatial_axis + num_spatial_axes_ (3 vs. 4) bottom num_axes 可能不会改变

caffe: Check failed: bottom[0]->num_axes() == first_spatial_axis + num_spatial_axes_ (3 vs. 4) bottom num_axes may not change

我修改了FCN网络并设计了一个新的网络,其中我使用两个ImageData Layer作为输入参数,并希望网络产生一张图片作为输出。 这是 train_val.prototxt and the deploy.prototxt

原图和标注均为灰度图,尺寸均为224*224。 我已经训练了一个caffemodel并使用infer.py使用caffemodel做分割,但是遇到错误:

 Check failed: bottom[0]->num_axes() == first_spatial_axis + num_spatial_axes_ (3 vs. 4) bottom num_axes may not change.

这里是 infer.py 文件:

import numpy as np
from PIL import Image
caffe_root = '/home/zhaimo/' 
import sys
sys.path.insert(0, caffe_root + 'caffe-master/python')

import caffe
im = Image.open('/home/zhaimo/fcn-master/data/vessel/test/13.png')
in_ = np.array(im, dtype=np.float32)
#in_ = in_[:,:,::-1]
#in_ -= np.array((104.00698793,116.66876762,122.67891434))
#in_ = in_.transpose((2,0,1))


net = caffe.Net('/home/zhaimo/fcn-master/mo/deploy.prototxt', '/home/zhaimo/fcn-master/mo/snapshot/train/_iter_200000.caffemodel', caffe.TEST)
net.blobs['data'].reshape(1, *in_.shape)
net.blobs['data'].data[...] = in_
net.forward()
out = net.blobs['score'].data[0].argmax(axis=0)

plt.axis('off')
plt.savefig('/home/zhaimo/fcn-master/mo/result/13.png')

如何解决这个问题?

您的网络需要形状为 1x1xHxW 的 4D 输入。那是一个批次,只有一个通道,其高度和宽度为 HxW。因此,输入有两个单独的前导维度。 您为您的网络提供的是带有单个 2D 图像的批次,即您的 in_shape 仅为 HxW - 您缺少通道维度的单一维度。
要解决您的问题,您需要显式添加单例维度:

 net.blobs['data'].reshape(1, 1, *in_.shape)

至于你得到的 KeyError,你的 net 没有任何名为 'score' 的 blob,你有 'upscore1''prob':

 out = net.blobs['prob'].data[0].argmax(axis=0)