利用python接口进行单张图片测试

import numpy as np
import sys
caffe_root = '/home/tyd/caffe/' #the file should be run from (caffe_root)/examples 
sys.path.insert(0,caffe_root + 'python')

import caffe
caffe.set_mode_cpu()

model_def = '/home/tyd/caffe/models/bvlc_reference_caffenet/deploy.prototxt'
model_weights = '/home/tyd/caffe_case/bvlc_reference_caffenet.caffemodel'

net = caffe.Net(model_def,model_weights,caffe.TEST)

mu = np.load(caffe_root + 'python/caffe/imagenet/ilsvrc_2012_mean.npy')
mu = mu.mean(1).mean(1)
print 'mean-subtracted values:',zip('BGR',mu)

#create transform for the input called 'data'
tranformer = caffe.io.Transformer({'data':net.blobs['data'].data.shape})

tranformer.set_transpose('data':(2,0,1)) #move image channels to ermost dimension
tranformer.set_mean('data':mu) #subtract the dataset mean value in each channels
tranformer.set_raw_scale('data':255) #rescale from [0,1] to [0,255]
tranformer.set_channel_swap('data':(2,1,0)) #swap channels from RGB TO BGR 

net.blobs['data'].reshape(1, # batch size
                          3, #3-channel BGR image
                          277,277) #image size is 277x277
                          
image = caffe.io.load_image(caffe_root+'examples/images/cat.jpg)
#cope the image data into the memory allocated for the net 
net.blobs['data'].data[...] = transformed_image

###perform classification
output = net.forward()

output_prob= output['prob'][0]

print 'predicted class is :',output_prob.argmax()

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值