【发布时间】:2017-04-20 06:44:50
【问题描述】:
我的问题是关于 caffe 测试结果。 Python 脚本结果不等于 caffe 测试结果。我使用了 Alexnet,我的测试准确率为 0,9033。
Caffe 测试准确率:0.9033
Python 准确度: 0.8785
我使用了 40000 张图片进行测试。误分类图像的数量应该是3868。但是我的python结果中的误分类图像数量是4859。这是什么问题?
谢谢。
这是我的 caffe 测试命令:
…/build/tools/caffe test --model …/my_deploy.prototxt --weights …/alex_24_11__iter_200000.caffemodel -gpu 0 -iterations 800
之后,我找到并尝试了一个带有我的测试数据的 python 脚本,但我没有得到相同的结果。 我之前在另一个数据集上使用过这个脚本,并且在我的 caffe 测试中得到了相同的准确度,但我在训练和测试期间都没有使用均值文件。但是现在我在训练和测试中都使用了平均文件。可能是平均文件有问题,但我使用了从教程中找到的所有内容。
- 我创建了 lmdb。
- 我使用 compute_image_mean 从 lmdb 创建平均文件。的大小 lmdb 中的图像为 256x256。
- 我在 alexnet 中使用了 227x227 的图像。
Python 脚本:
caffe.set_mode_gpu()
model_def = '…/my_deploy.prototxt'
model_weights = '… /alex_24_11__iter_200000.caffemodel'
net = caffe.Net(model_def, model_weights, caffe.TEST)
blob = caffe.proto.caffe_pb2.BlobProto()
data = open( '.../image_mean.binaryproto' , 'rb' ).read()
blob.ParseFromString(data)
arr = np.array( caffe.io.blobproto_to_array(blob) )
out = arr[0]
np.save( '.../imageMean.npy' , out )
mu = np.load('…/imageMean.npy')
mu = mu.mean(1).mean(1)
transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})
transformer.set_transpose('data', (2,0,1))
transformer.set_mean('data', mu)
transformer.set_raw_scale('data', 255)
transformer.set_channel_swap('data', (2,1,0))
net.blobs['data'].reshape(1, 3, 227, 227)
f = open('…/val.txt', 'r')
f2 = open('…/result.txt', 'a')
for x in range(0,40000):
a=f.readline()
a=a.split(' ')
image = caffe.io.load_image('… /'+a[0])
transformed_image = transformer.preprocess('data', image)
net.blobs['data'].data[...] = transformed_image
output = net.forward()
output_prob = output['prob'][0]
f2.write(str(a[0]))
f2.write(str(' '))
f2.write(str(output_prob.argmax()))
f2.write('\n')
我的 deploy.prototxt 的第一层
layer {
name: "input"
type: "Input"
top: "data"
input_param { shape: { dim: 1 dim: 3 dim: 227 dim: 227 } }
}
我的 deploy.prototxt 的最后一层
layer {
name: "prob"
type: "Softmax"
bottom: "fc8-16"
top: "prob"
}
其他层等于train_val.prototxt。
【问题讨论】:
标签: python deep-learning caffe pycaffe