#-*- coding:utf-8 -*- import numpy as np import sys import cv2 caffe_root = '/home/ubuntu/caffe/' #修改成你的Caffe项目路径 sys.path.insert(0,caffe_root+'python') import caffe imgs = np.zeros([1,3,200,200]) caffe.set_mode_gpu() model_def = '/home/ubuntu/caffe/examples/danhdf5/test_deploy.prototxt' model_weights = '/home/ubuntu/caffe/examples/danhdf5/model/solver_iter_2000.caffemodel' # 修改成你的caffemodel文件的路径 net = caffe.Net(model_def, # defines the structure of the model model_weights, # contains the trained weights caffe.TEST) # use test mode (e.g., don't perform dropout) net.blobs['data'].reshape(1, 3, 200, 200) #修改输入格式 net.blobs['label_x'].reshape(1, 1, 1) net.blobs['label_y'].reshape(1, 1, 1) image = cv2.imread('/home/ubuntu/caffe_case/HDF5/image1/2.jpg') #输入读取图片 image = image.transpose(2,0,1) #imgsmean = cv2.imread('imgsmean.jpg') #imgsmean = imgsmean.transpose(2,0,1) image = (image)/255 imgs[0,:,:,:] = image label_x = [[2.4]] label_y = [[8.34]] net.blobs['data'].data[...] = imgs net.blobs['label_x'].data[...] = label_x net.blobs['label_y'].data[...] = label_y output = net.forward() #前向传播 #output_prob = output['ip2_x'] output_loss_x = output['loss_x'] #输入前向传播后的loss_x层 output_loss_y = output['loss_y'] #print 'predicted is :',output_prob print 'loss_x:',output_loss_x print 'loss_y:',output_loss_y
使用caffe模型预测
最新推荐文章于 2019-05-25 23:53:48 发布