def classify(net, in_im, net_name, im_list, gt_labels):
config = tf.ConfigProto(gpu_options=tf.GPUOptions(allow_growth=True))
imgs = open(im_list).readlines()
gt_labels = open(gt_labels).readlines()
fool_rate = 0
top_1 = 0
with tf.Session(config=config) as sess:
sess.run(tf.global_variables_initializer())
for i,name in enumerate(imgs):
if net_name == 'caffenet':
im = img_preprocess(name.strip(), size=227)
else:
im = img_preprocess(name.strip())
softmax_scores = sess.run(net['prob'], feed_dict={in_im: im})
if i!=0 and i%1000 == 0:
print 'iter: {:5d}\ttop-1: {:04.2f}\tfooling-rate: {:04.2f}'.format(i, (top_1/float(i))*100, (fool_rate)/float(i)*100)
if np.argmax(softmax_scores[0]) == int(gt_labels[i].strip()):
top_1 += 1
if np.argmax(softmax_scores[0]) != np.argmax(softmax_scores[1]):
fool_rate += 1
print 'Top-1 Accuracy = {:.2f}'.format(top_1/500.0)
print 'Fooling Rate = {:.2f}'.format(fool_rate/500.0)
评论列表
文章目录