对测试集test中的图片进行预测
#!/usr/bin/env python
import numpy as np
import matplotlib.pyplot as plt
import sys,os
import pylab
caffe_root = "C:\\MYCaffe\\caffe-master\\Build\\x64\\Release\\pycaffe"
sys.path.insert(0, caffe_root)
import caffe
print('import success')
#######################################################################################
os.chdir(caffe_root)
if not os.path.isfile("D:\CaffeInfo\D_TrainVal\VGG19_layers_prelu_iter_40000.caffemodel"):
print "caffemodel is not exist..."
else:
print "Ready to Go ..."
caffe.set_mode_gpu()
net = caffe.Net("D:\CaffeInfo\D_TrainVal\VGG19_deploy_1.prototxt",
"D:\CaffeInfo\D_TrainVal\VGG19_layers_prelu_iter_40000.caffemodel",
caffe.TEST)
print('fdsadfasdfasdfsadfsdfasdfsadf\nfasdfsadfsadf\n')
print(net.blobs['data'].data.shape)
feat = [ [0 for i in range(3)] for j in range(96768)]
k = 0
right=0
imagePath = "D:/CaffeInfo/B_DataCreate/test_image/"
for filename in os.listdir(imagePath):
print('\n\n\n')
print(filename)
im = caffe.io.load_image(imagePath + filename)
if filename[0] == 'N' :
label = 0
else:
label = 1
################################################################################
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 )
binMean="D:\\CaffeInfo\\B_DataCreate\\mean.binaryproto"
npyMean="D:\\CaffeInfo\\B_DataCreate\\mean.binaryproto.npy"
convert_mean(binMean,npyMean)
#print(np.load(npyMean))
#print("np.load(npyMean).shape")
#print(np.load(npyMean).shape)
################################################################################
transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})
transformer.set_transpose('data', (2,0,1))
transformer.set_mean('data', np.load(npyMean).mean(1).mean(1))
transformer.set_raw_scale('data', 255)
transformer.set_channel_swap('data', (2,1,0))
net.blobs['data'].data[...] = transformer.preprocess('data',im)
inputData=net.blobs['data'].data
################################################################################
net.forward()
# for layer_name, blob in net.blobs.iteritems():
# print layer_name + '\t' + str(blob.data.shape)
feat12 = net.blobs['prob'].data[0].flatten()
feat[k][0] = feat12[0] #probability of normal
feat[k][1] = feat12[1] #probability of cancer
feat[k][2] = label #original standard label
aa = label == 1 & int(feat12[1] > feat12[0])
bb = label == 0 & int(feat12[1] < feat12[0])
if aa | bb :
right = right + 1
print('right = ' + str(right) )
k = k + 1
accu = float( right/k );
print('ALL = ' + str(k))
print(label)
print( ' normal cancer' )
print(feat12)
################################################################################
y_true = [ x[2] for x in feat ]
y_scores = [ x[1] for x in feat ]
np.savetxt('D:/CaffeInfo/D_TrainVal/' + 'y_true.txt', y_true);
np.savetxt('D:/CaffeInfo/D_TrainVal/' + 'y_scores.txt', y_scores);
#np.savetxt('D:/CaffeInfo/D_TrainVal/' + 'rightsumaccuAUC.txt', rightsumaccuAUC);