pycaffe - Getting different accuracies using different caffe classes(98.65 vs 98.1 vs 98.20) -
when train , test model using caffe's command line interface, e.g. 98.65%
whereas when myself write code(given below) calculate accuracy same pre-trained model, e.g 98.1%
using caffe.net
.
straight forward , have no idea causing issue.
tried using caffe.classifier
, predict
method, , yet lesser accuracy(i.e. 98.20%
!)
here snippet of code wrote:
import sys import caffe import numpy np import lmdb import argparse collections import defaultdict sklearn.metrics import classification_report sklearn.metrics import confusion_matrix import matplotlib.pyplot plt import itertools sklearn.metrics import roc_curve, auc import random if __name__ == "__main__": parser = argparse.argumentparser() parser.add_argument('--proto', help='path network prototxt file(deploy)', type=str, required=true) parser.add_argument('--model', help='path caffemodel file', type=str, required=true) parser.add_argument('--mean', help='path mean file(.binaryproto)', type=str, required=true) #group = parser.add_mutually_exclusive_group(required=true) parser.add_argument('--db_type', help='lmdb or leveldb', type=str, required=true) parser.add_argument('--db_path', help='path lmdb/leveldb dataset', type=str, required=true) args = parser.parse_args() predicted_lables=[] true_labels = [] misclassified =[] class_names = ['unsafe','safe'] count=0 correct = 0 batch=[] plabe_ls=[] batch_size = 50 cropx = 224 cropy = 224 = 0 multi_crop = false use_caffe_classifier = true caffe.set_mode_gpu() # extract mean mean image file mean_blobproto_new = caffe.proto.caffe_pb2.blobproto() f = open(args.mean, 'rb') mean_blobproto_new.parsefromstring(f.read()) mean_image = caffe.io.blobproto_to_array(mean_blobproto_new) f.close() net = caffe.classifier(args.proto, args.model, mean = mean_image[0].mean(1).mean(1), image_dims = (224, 224)) net1 = caffe.net(args.proto, args.model, caffe.test) net1.blobs['data'].reshape(batch_size, 3,224, 224) data_blob_shape = net1.blobs['data'].data.shape #check , see if lmdb or leveldb if(args.db_type.lower() == 'lmdb'): lmdb_env = lmdb.open(args.db_path) lmdb_txn = lmdb_env.begin() lmdb_cursor = lmdb_txn.cursor() key, value in lmdb_cursor: count += 1 datum = caffe.proto.caffe_pb2.datum() datum.parsefromstring(value) label = int(datum.label) image = caffe.io.datum_to_array(datum).astype(np.float32) #key,image,label #buffer n image if(count % 5000 == 0): print('{0} samples processed far'.format(count)) if(i < batch_size): i+=1 inf= key,image,label batch.append(inf) #print(key) if(i >= batch_size): #process n image ims=[] x in range(len(batch)): img = batch[x][1] #img has c,w,h shape! gone through transpose , channel swap when being saved lmdb! #method iii : use center crop caffe in test time if (use_caffe_classifier != true): #center crop c,w,h = img.shape startx = h//2 - cropx//2 starty = w//2 - cropy//2 img = img[:, startx:startx + cropx, starty:starty + cropy] #transpose image can subtract mean img = img.transpose(2,1,0) img -= mean_image[0].mean(1).mean(1) #transpose original state img = img.transpose(2,1,0) ims.append(img) else: ims.append(img.transpose(2,1,0)) if (use_caffe_classifier != true): net1.blobs['data'].data[...] = ims[:] out_1 = net1.forward() plabe_ls = out_1['pred'] else: out_1 = net.predict(np.asarray(ims), oversample=multi_crop) plabe_ls = out_1 plbl = np.asarray(plabe_ls) plbl = plbl.argmax(axis=1) j in range(len(batch)): if (plbl[j] == batch[j][2]): correct+=1 else: misclassified.append(batch[j][0]) predicted_lables.append(plbl[j]) true_labels.append(batch[j][2]) batch.clear() = 0 sys.stdout.write("\raccuracy: %.2f%%" % (100.*correct/count)) sys.stdout.flush() print(", %i/%i corrects" % (correct, count))
what causing difference in accuracies ?
more information :
using python3.5 on windows.
read images lmdb
dataset.
images have 256x256
, center cropped size 224x224
.
finetuned on googlenet
.
caffe.predict
work had change classify.py
in training, use caffes
defaults, such random crops @ training , center crop @ test-time.
changes:
changed line 35 to:
self.transformer.set_transpose(in_, (2, 1, 0))
and line 99 :
predictions = predictions.reshape((len(predictions) // 10, 10, -1))
1) first off, need revert line 35 (32?) of classify.py
: self.transformer.set_transpose(in_, (2, 1, 0))
original self.transformer.set_transpose(in_, (2, 0, 1))
. expects hwc , transforms internally chw downstream processing.
2) run classifier branch is. you're bad result. please check this. if so, means image database not cwh you've commented, chw. after you've confirmed this, make change classifier branch: ims.append(img.transpose(2,1,0))
become ims.append(img.transpose(1,2,0))
. re-test classifier branch. result should 98.2%
(goto step 3) or 98.65%
(try step 4).
3) if result in step 3 98.2%
, undo second change classify.py
. theoretically, images have height/width //
, /
should have no difference. if differ or crashes, wrong image database -- assumption of image size incorrect. need check these. off pixel or so, , explain slight discrepancies in accuracy.
4) if result in step 3 98.65%
, need make changes caffe.net
branch of code. database images chw, need make first transpose: img = img.transpose(1,2,0)
, second transpose after mean subtraction img = img.transpose(2,0,1)
. run caffe.net
branch. if still 98.1%
before, should check mean subtraction performed correctly network.
in steps (2) , (4), it's possible worse results, means problem difference in mean subtraction trained net vs expectations in python code. check this.
about 98.2%
caffe.classifier
:
if @ lines 78 - 80, center crop done along crop_dims
, not img_dims
. if further @ line 42 on caffe.classifier
constructor, crop_dims
never user-determined. it's determined size of net's input blobs. lastly, @ line 70, img_dims
used resize images prior center cropping. what's happening setup is: a) images first getting resized 224 x 224, uselessly getting center cropped 224 x 224 ( assume hxw net ). results poorer 98.65%
. need change img_dims = (256, 256)
. prevents resizing. crop picked automatically net , should 98.65%
.
Comments
Post a Comment