I have trained a cnn model,but the test accuracy is different from caffe and self tested

42 views
Skip to first unread message

hang chen

unread,
Aug 15, 2017, 5:52:20 AM8/15/17
to Caffe Users


Hi,

    After i trained a CNN model, i use python to predict. But I don’t get the same result.At the end of training phase, caffe report the accuracy on test images is 91.2%. But when I used python interface to do predict on the test data, I find the accuracy which was computed by the python script is about 79%. 

    I used this script on same dataset before and I got the same accuracy with my caffe test but I didn’t use mean file neither during train nor during test. But now I used mean file both train and test. May be there is a problem in mean file but I used everything that I found from the tutorials.

   Does anyone can help me?

   1.The format of the image is png,the size is 96 * 96

   2.I used compute_image_mean to create mean file from lmdb

   3.I set :transform_param {mean_file: "mean.binaryproto"}

   4.In python use this script to create mean.npy :


def convert_mean(binMean,npyMean):
blob = caffe.proto.caffe_pb2.BlobProto()
bin_mean = open(binMean, 'rb').read()
blob.ParseFromString(bin_mean)
arr = np.array(caffe.io.blobproto_to_array(blob))
npy_mean = arr[0]
np.save(npyMean, npy_mean)

    

    5.Python script 


net = caffe.Net(model_def,  model_weights, caffe.TEST) 

convert_mean(binMean
, npyMean)
mu = np.load(npyMean)
mu = mu.mean(
1).mean(1)
print 'mean-subtracted values:', zip('BGR', mu)

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

image = caffe.io.load_image(imgSrc)
transformed_image = transformer.preprocess('data', image)

net.blobs['data'].data[...] = transformed_image

output = net.forward()
output_prob = output[
'prob']


   

Reply all
Reply to author
Forward
0 new messages