Caffeでのモデルテストの階調図テスト設定

1790 ワード

【0、参考資料】


https://blog.csdn.net/weixin_40695510/article/details/81281442

【1、画像を読み込んで階調図に変換する】

img=caffe.io.load_image(img_path, color=False) 
  • この文は画像を読み込み、単一チャネル階調図に変換する、値はfloat型であり、範囲は0~1.0
  • である.
  • 例を挙げて、自分で書いたので、誤り訂正を望んでいます:
  • #coding=utf-8
    import os
    import sys
    import numpy as np 
    import cv2
    
    caffe_root = '/your/caffe/address/' # /home/myname/caffe/
    filepath = '/my/file/address/'
    sys.path.insert(0, caffe_root+'python') # import insert 
    import caffe 
    
    caffe.set_mode_gpu() #  GPU 
    os.chdir(caffe_root) #  。
    
    deploy = filepath + 'test_net/ResNet18_deploy.prototxt' # 
    caffe_model = filepath + 'model/ResNet18/_iter_10500.caffemodel' # 
    
    dir = filepath + 'selected_test/' # 
    
    filelist = []
    labels = [] 
    with open(filepath + 'Test.txt','r') as f:
        for line in f:
            a = line.split(' ')[0]
            b = line.split(' ')[1][0]
            filelist.append(a)
            labels.append(b)
    
    net = caffe.Net(deploy, caffe_model, caffe.TEST)
    
    #preprocess
    transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})
    transformer.set_transpose('data', (2, 0, 1))
    transformer.set_raw_scale('data', 255)
    
    total = len(filelist)
    right = 0.0
    accuracy = 0.0 
    
    for i in range(total):
        img = filelist[i
        im = caffe.io.load_image(dir+img, color = False) #color = False 
        net.blobs['data'].data[...]=transformer.preprocess('data',im)
        out = net.forward()
        output_prob = out['prob'][0]
        if int(output_prob.argmax()) == int(labels[i]):
            right += 1
    
    if right != 0:
        accuracy = right/total
    
    print("this accuracy is {}".format(accuracy))