如何在caffe python上测试FCN(voc-fcn8s)?

时间:2016-10-21 10:22:56

标签: python caffe convolution

我想测试shelhamer给出的带有图像的FCN caffemodel:

enter image description here

但我不确定如何运行测试程序并显示标记图像。

我的代码如下:

import caffe
caffe_root = 'fcn.berkeleyvision.org-master/voc-fcn8s/'
model_def = caffe_root + 'deploy.prototxt'
model_weights = caffe_root + 'fcn8s-heavy-pascal.caffemodel'
test_image = caffe_root + 'test.jpg'

net = caffe.Net(model_def, model_weights, caffe.TEST)
image = caffe.io.load_image(test_image)

那么下一步有什么能帮助我的呢?我在这里连续几天都在苦苦挣扎。

2 个答案:

答案 0 :(得分:3)

Here是我用来对一批文件进行推理的脚本。我认为您正在寻找的命令是

net.forward()

要从网络获取输出图像,请使用以下命令。

out = net.blobs['score'].data # Extract the output
out = out.argmax(axis=1) # Get the labels at each pixel
out = out.transpose(1, 2, 0) # Reshape the output into an image
out = np.tile(out, (1,3))

答案 1 :(得分:3)

您可以参考存储库中的infer.py

# load image, switch to BGR, subtract mean, and make dims C x H x W for Caffe
im = Image.open('pascal/VOC2010/JPEGImages/2007_000129.jpg')
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))

# load net
net = caffe.Net('voc-fcn8s/deploy.prototxt', 'voc-fcn8s/fcn8s-heavy-pascal.caffemodel', caffe.TEST)
# shape for input (data blob is N x C x H x W), set data
net.blobs['data'].reshape(1, *in_.shape)
net.blobs['data'].data[...] = in_
# run net and take argmax for prediction
net.forward()
out = net.blobs['score'].data[0].argmax(axis=0)

重塑数据层至关重要,因为测试图像的形状可能不同。