使用python进行错误的caffe预测

2024-03-29 02:24:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用python训练一个Alexnet。我使用caffe的测试工具几乎100%准确。但是我的python代码以正确的类返回。 训练时,我使用以下代码将数据转换为lmdb(我的图像是灰度的)

convert_imageset -resize_height=256 -resize_width=256 -shuffle -gray -backend=lmdb train.txt train_lmdb

并使用以下公式计算平均值:

compute_image_mean train_lmdb train_mean.binaryproto

我的数据层火车.txt看起来像:

layer {     
      name: "data"   
      type: "Data"    
      top: "data"    
      top: "label"
include {
     phase: TRAIN   }

 transform_param {
  mirror: false
  crop_size: 227
  mean_file: "train_mean.binaryproto"  
  }    

  data_param {
  source: "train_lmdb"
  backend: LMDB
  batch_size:64  } }

测试时,我使用以下python代码:

data = open('train_mean.binaryproto','rb').read()
blob.ParseFromString(data) arr =
np.array(caffe.io.blobproto_to_array(blob))[0,:,:,:].mean(0)

transformer = caffe.io.Transformer({'data':net.blobs['data'].data.shape}) 
transformer.set_mean('data', arr.mean(1).mean(0))
transformer.set_transpose('data', (2,0,1))
transformer.set_raw_scale('data', 255.0)
net.blobs['data'].reshape(1,1,227,227)

img = caffe.io.load_image('image_name', False) 
img = caffe.io.resize_image( img, (256,256), interp_order=3 )
transformed = transformer.preprocess('data', img) 
net.blobs['data'].data[...] = transformed
output = net.forward()
prediction_result = output['prob'].argmax()

我检查了转换后的图像,它看起来很好。我试图不调整大小,但它保持不变。有人知道出了什么问题吗?我已经被这个问题困扰了好几天了。你知道吗

[更新] 我检查了我的房间部署.prototxt以及火车.txt它们具有相同的网络结构。你知道吗


Tags: 代码ioimagetxtlmdbimgdatanet