Python调用已训练好的caffe模型进行分类
来源:互联网 发布:淘宝认证考试 编辑:程序博客网 时间:2024/04/30 23:51
python作为强大的解释型语言,其提供的库函数能够方便快速的实现常用的功能。本文用python调用caffe模型。
1.Setup
# set up Python environment: numpy for numerical routines, and matplotlib for plotting import numpy as np import matplotlib.pyplot as plt # display plots in this notebook %matplotlib inline # set display defaults plt.rcParams['figure.figsize'] = (10, 10) # large images plt.rcParams['image.interpolation'] = 'nearest' # don't interpolate: show square pixels plt.rcParams['image.cmap'] = 'gray' # use grayscale output rather than a (potentially misleading) color heatmap
2.Load caffe
# The caffe module needs to be on the Python path; # we'll add it here explicitly. import sys caffe_root = '/home/ubuntu/caffe/' # this file should be run from {caffe_root}/examples (otherwise change this line) sys.path.insert(0, caffe_root + 'python') import caffe # If you get "No module named _caffe", either you have not built pycaffe or you have the wrong path.
3.Import Net
caffe.set_mode_gpu() model_def = './deploy.prototxt' model_pretrained = './snapshot_iter_6720.caffemodel'# load the mean ImageNet image (as distributed with Caffe) for subtraction MEAN_PROTO_PATH = './mean.binaryproto' #这里是二进制文件,而不是Python的npy文件blob = caffe.proto.caffe_pb2.BlobProto()data = open(MEAN_PROTO_PATH, 'rb' ).read()blob.ParseFromString(data)array = np.array(caffe.io.blobproto_to_array(blob))# 将blob中的均值转换成numpy格式,array的shape (mean_number,channel, hight, width)mu = array[0]mean = mu.mean(1).mean(1) # average over pixels to obtain the mean (BGR) pixel values net = caffe.Classifier(model_def, model_pretrained,mean=mean, channel_swap=(2,1,0),#RGB通道与BGR raw_scale=255,#把图片归一化到0~1之间 image_dims=(256, 256))#设置输入图片的大小
4.Classifier
label_list=['BAC','Caocx2','MUC','NEG','RBC','SPURM','WBC','XIAOYUAN','YEA','YISHUI']input_image = caffe.io.load_image('1.jpg')#读取图片#显示原图片,以及分类预测结果prediction = net.predict([input_image])#图片分类str_gender=label_list[prediction[0].argmax()]print str_genderplt.imshow(input_image) plt.title(str_gender) plt.show()
结果如下:
0 0
- Python调用已训练好的caffe模型进行分类
- python接口调用已训练好的caffe模型测试分类
- 使用python调用训练好的caffe模型来分类
- 使用caffe训练好的模型进行分类
- 用训练好的caffe模型进行分类完整版
- caffe+python 使用训练好的VGG16模型 对 单张图片进行分类,输出置信度
- caffe+python 使用训练好的VGG16模型 对 单张图片进行分类,输出置信度
- caffe学习(六):使用python调用训练好的模型来分类(Ubuntu)
- caffe python接口:用训练好的模型来分类
- python 用opencv调用训练好的模型进行识别
- 基于caffe特征可视化 以及 用训练好的模型进行分类
- 基于caffe特征可视化 以及 用训练好的模型进行分类 2
- 使用MATLAB加载训练好的caffe模型进行识别分类
- 有关Caffe训练好的模型在Python接口下使用分类不准确的问题解决
- 有关Caffe训练好的模型在Python接口下使用分类不准确的问题解决
- 使用已训练好的caffe模型的步骤
- 在c++程序中调用caffe训练完毕的模型进行分类
- caffe提取已训练好模型的特征
- theano学习初步(二) 基础Tensor函数
- NOIP2008(2)排座椅
- 广播机制
- 测试URL接口的Thread
- 【caffe-Windows】训练自己数据——数据集格式转换
- Python调用已训练好的caffe模型进行分类
- Android设备连接Unity Profiler性能分析器
- 算法学习之素数
- redis的linux下的单机安装、集群安装及测试
- 写操作系统只能用汇编和C语言吗
- git本地仓库连接多远程
- 12.【链表】Linked List Cycle--Accepted code
- 为什么在6plus上打印的屏幕高度是6的高度
- cookie&session