caffe预测、特征可视化python接口调用


网上有很多caffe训练好的模型,有的时候我们仅仅想要方便的调用已经训练好的模型,利用python进行预测分类测试,查看可视化结果,这个时候,我们可以使用caffe为我们写好的python接口文件,我们在安装caffe的时候,有一步:make pycaffe。这个便是安装caffe的python 结果函数,把自己用的代码记录一下,以便日后直接复制粘贴使用。感觉使用python就是轻松,如果用caffe的c++接口,挺麻烦的。

下面的使用例子是自己搞利用CNN进行性别预测的python接口调用实例:

代码来源:

http://prog3.com/sbdm/blog/hjimce/article/details/48972877#comments

# coding=utf-8
OS import
Numpy as NP import
Matplotlib import pyplot as PLT from
CV2 import
Shutil import
Time import

# because RGB and BGR need to change to display
Showimage def (im):
If im.ndim = = 3:
Im = im[:::::: -1]
Plt.set_cmap ('jet')
Plt.imshow (IM)
Plt.show ()

Display # feature visualization, padval is used to adjust the brightness
Vis_square def (data, padsize=1, padval=0):
Data = data.min ()
/ = data.max (data)

# because we want to put a layer of feature maps are shown in a figure. Therefore, it is necessary to calculated for each picture to take the proportion of the figure, and draw the position
N = int (np.ceil (data.shape[0]) (np.sqrt))
Padding = (0, n * 2 - data.shape[0]), (0, padsize), (0, padsize) + (0, 0) * (data.ndim - 3)
Data = np.pad (data, padding, mode='constant', constant_values= (padval, padval))

Tile the filters into an image #
Data = data.reshape ((n, n) +.Transpose (0, 2, 1, 3,) + tuple (range (4, data.ndim + 1)) () (data.shape[1:] ().
Data = data.reshape ((n * data.shape[1] * n * data.shape[3] *) + data.shape[4:])

Showimage (data)


# set the path where the Caffe source code
Caffe_root ='../../../caffe/'
Sys import
Sys.path.insert (0, caffe_root +'python')
Caffe import




# mean file loading
Mean_filename='./imagenet_mean.binaryproto'
Proto_data = open (mean_filename, "RB").Read ()
A = caffe.io.caffe_pb2.BlobProto.FromString (proto_data)
Mean = caffe.io.blobproto_to_array (a) [0]

# create a network, and have good training model file loading
Gender_net_pretrained='./caffenet_train_iter_1500.caffemodel'
Gender_net_model_file='./deploy_gender.prototxt'
Gender_net = caffe.Classifier (gender_net_model_file, gender_net_pretrained, mean=mean,
Channel_swap= (2,1,0), #RGB channel and BGR
Raw_scale=255, # picture normalized to 0~1
Image_dims= (256, 256) #) set the input image size


# classification and visual features can be predicted
Gender_list=['Male','Female']
Input_image = caffe.io.load_image ('1.jpg') # read pictures

Prediction_gender=gender_net.predict ([input_image]) # picture sex forecast
# print our training parameters of each layer shape
'params:'print
K V, in gender_net.params.items for ():
'weight:'print
Print (k, v[0].data.shape) # in blob parameters of each layer, using vector to store two blob variable Caffe, v[0] weight
'b:'print
Print (k, v[1].data.shape) v[1] # bias
#conv1 filter visualization
Filters = gender_net.params['conv1'][0].data
Vis_square (0, 2, 3, 1,,)
#conv2 filter visualization
'''filters = gender_net.params['conv2'][0].data
Vis_square (filters[: 48].reshape (48**2, 5, 5 '))
# feature map
'feature maps:'print
K V, in gender_net.blobs.items for ():
Print (k, v.data.shape);
Feat = gender_net.blobs[k].data[0,0:4]# shows the name of the K network layer, the first picture generated by the 4 maps feature
Vis_square (feat, padval=1)





# shows the original image, and the classification results
Str_gender=gender_list[prediction_gender[0].argmax ()]
Str_gender print

Plt.imshow (input_image)
Plt.title (str_gender)
Plt.show ()






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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值