1. 程式人生 > >caffe訓練後模型測試

caffe訓練後模型測試

# coding:utf-8
import sys
import numpy as np
sys.path.append('/home/hadoop/caffe/python')
import caffe

WEIGHTS_FILE = '/home/hadoop/桌面/eye_data3/eyes_lmdb/snapshot/solver_iter_20000.caffemodel'
DEPLOY_FILE = '/home/hadoop/桌面/eye_data3/eyes_lmdb/deploy.prototxt'
IMAGE_SIZE = (32, 32)

caffe.set_mode_gpu()
net = caffe.Net(DEPLOY_FILE, WEIGHTS_FILE, caffe.TEST)
net.blobs['data'].reshape(1, 3, *IMAGE_SIZE)
transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})
transformer.set_transpose('data', (2,0,1))
mu = np.load("/home/hadoop/桌面/eye_data3/eyes_lmdb/mean.npy")
mu = mu.mean(1).mean(1)
transformer.set_mean('data', mu)
transformer.set_raw_scale('data', 255)
# transformer.set_raw_scale('data', 1)
transformer.set_channel_swap('data', (2, 1, 0))  # swap channels from RGB to BGR
image_list = "/home/hadoop/桌面/10.13眼睛閉合度/eye1_txt_connect.txt"
img_path = "/home/hadoop/桌面/10.13眼睛閉合度/eyes1/"
with open(image_list, 'r') as f:
    for line in f.readlines():
        filename = line.split(" ")[0]
        # print img_path+filename
        image = caffe.io.load_image(img_path+filename)
        # print image.shape
        transformed_image = transformer.preprocess('data', image)
        net.blobs['data'].data[...] = transformed_image
        output = net.forward()
        score = output['fc'].argmax()

        # score = output['pred']
        print('The predicted score for {} is {}'.format(filename, score))

caffe模型測試