When a model is trained, we usually judge it by the final loss of the model and the accuracy on the validation set. However, for classification problems, if we just know the overall classification of the correct rate
Obviously not enough, so only know the model for each class of classification results and the correct rate in order to better understand this model.
Here is a well-trained model to test the test set and output the implementation of the classification results for each sample.
The code is as follows:
#Coding=utf-8 ImportOSImportCaffeImportNumPy as NP root='/home/liuyun/caffe/' #root directoryDeploy=root +'Models/bvlc_googlenet/deploy.prototxt' #模型对应的Deploy fileCaffe_model=root +'Models/ccc/c2/bvlc_googlenet_iter_10000.caffemodel' #well-trained Caffemodel########################################## ImportOsdir= root+'examples/ccc/test/' # test data path filelist=[] # for full path and file name filenames=Os.listdir (dir) # Read all files in a directory forFninchFilenames:fullfilename=Os.path.join (DIR,FN) filelist.append (fullfilename) # connect to full path
# img=root+ ' data/drive/test/60337.jpg ' #随机找的一张待测图片 # ############################################## defTest (IMG): # Define testing function net= Caffe.net (Deploy,caffe_model,caffe. TEST)#load model and network #Picture preprocessing settingsTransformer = Caffe.io.Transformer ({'Data': net.blobs['Data'].data.shape})#set the shape format of the picture (1,3,28,28)Transformer.set_transpose ('Data', (2,0,1))#change the order of dimensions from the original picture (28,28,3) to (3,28,28) #Transformer.set_mean (' Data ', Np.load (mean_file). Mean (1). Mean (1)) #减去均值, there is no mean reduction in the previous training model .Transformer.set_raw_scale ('Data', 255)#Zoom to "0,255"Transformer.set_channel_swap ('Data', (2,1,0))#swap channels To change the image from RGB to BGRim=caffe.io.load_image (IMG)#Loading Picturesnet.blobs['Data'].data[...] = transformer.preprocess ('Data', IM)#perform the picture preprocessing action set above and load the picture into the blob #Perform testsout =Net.forward () labels= Np.loadtxt (Labels_filename, str, delimiter='\ t')#read the category name fileprob= net.blobs['prob'].data[0].flatten ()#Take out the last layer (prob) belongs to the probability value of a category and print, ' prob ' is the name of the last layer PrintProb Order=prob.argsort () [999]#sort the probability value, take out the maximum value of the ordinal, 9 refers to the classification of 90 refers to the number of classes in the deploy file; #the Argsort () function is arranged from small to large Print 'The class is:', Labels[order]#convert the ordinal to the corresponding category name and printF=file ("/home/liuyun/caffe/examples/ccc/label.txt","A +") F.writelines (Labels[order]+'\ n' # writes output to the Label.txt file
##################################################################labels_filename = root +'Examples/ccc/dr.txt' #category name file, converting a numeric label back to the category name forIinchRange (0, Len (filelist)): IMG=filelist[i] Test (IMG)
Caffe---Test model classification results and output (python)