首页 文章

Python测试结果与caffe测试结果不一样

提问于
浏览
1

我的问题是关于caffe测试结果 . Python脚本结果不等于caffe测试结果 . 我使用了Alexnet,我的测试精度是0,9033 .

Caffe测试精度:0.9033

Python准确度:0.8785

我用40000张图片进行测试 . 错误分类图像的数量应该是3868.但是我的python结果中错误分类图像的数量是4859.问题是什么?

谢谢 .

Here is my caffe test command:

…/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 script:

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')

First layer of my deploy.prototxt

layer {
  name: "input"
  type: "Input"
  top: "data"
  input_param { shape: { dim: 1 dim: 3 dim: 227 dim: 227 } }
}

Last layer of my deploy.prototxt

layer {
  name: "prob"
  type: "Softmax"
  bottom: "fc8-16"
  top: "prob"
}

其他图层等于train_val.prototxt .

1 回答

  • 0

    创建LMDB并处理测试数据时,请检查预处理是否相同 .

    例如,如果您使用:

    transformer.set_channel_swap('data', (2,1,0))
    

    你应该确保你的LMDB也交换了这些通道(我假设这是一个RGB到BGR的转换) .

    特别是,你说你在训练期间使用了平均值 image . 但是,在 Transformer 中,您正在计算并减去平均值 pixel . 这可以解释你的两个准确度之间的微小差异 .

相关问题