caffe 09 win10 使用训练好模型为给定图片分类
2017-03-15 10:39
661 查看
# 使用已有模型和训练好的网络 # D:/git/DeepLearning/caffe/test_model.py # 源自 http://edu.csdn.net/course/detail/3506 视频 import numpy as np; import sys; # 指定python接口文件路径 caffe_root='D:/git/DeepLearning/caffe/build/x64/install/'; sys.path.insert(0, caffe_root+'python') import caffe; # 引入caffepython接口 # 设置设备类型 # caffe_set_mode_cpu(); # 使用cpu caffe.set_mode_gpu(); # 使用gpu # 指定使用的最优化网络 model_def = 'models/bvlc_reference_caffenet/deploy.prototxt'; # 指定训练好的模型 model_weights = 'models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel'; net = caffe.Net(model_def, # defines the structure of the model model_weights, # contains the trained weights caffe.TEST) # use test mode (3.g., don't perform dropout) # create transformer for the input called 'data' # ilsvrc_2012_mean.npy均值文件,代码库里提供的 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)); transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape}); transformer.set_transpose('data', (2,1,0)); # move image channels to outermost dimension transformer.set_mean('data', mu); # subtract the dataset-mean value in each channel transformer.set_raw_scale('data', 255); # rescale from [0,1] to [0,255] transformer.set_channel_swap('data', (2,1,0)); # swap channels from RGB to BGR net.blobs['data'].reshape(1, # batch size 3, # 3-channel (BGR) images 227, 227); # image size is 227x227 image = caffe.io.load_image('D:/git/DeepLearning/caffe/examples/images/cat.jpg'); transformed_image = transformer.preprocess('data', image); #copy the image data into the memory allocated for the net net.blobs['data'].data[...] = transformed_image; ### perform classification output = net.forward(); # the output probability vector for the first image in the batch output_prob = output['prob'][0]; print('predicted class is:', output_prob.argmax());
运行结果:
相关文章推荐
- caffe+python 使用训练好的VGG16模型 对 单张图片进行分类,输出置信度
- caffe+python 使用训练好的VGG16模型 对 单张图片进行分类,输出置信度
- 爬取百度图片各种狗狗的图片,使用caffe训练模型分类
- caffe的python接口学习:用训练好的模型(caffemodel或者h5)来分类新的图片
- Linux下使用Caffe对图片进行训练并分类的简单流程
- 用训练好的模型(caffemodel)来分类新的图片
- 使用现有的基于caffe训练好的imagenet model进行图片分类
- 使用caffe中的imagenet对自己的图片进行分类训练(超级详细版)
- 使用caffe中的imagenet对自己的图片进行分类训练(超级详细版)
- 使用Caffe训练适合自己样本集的AlexNet网络模型,并对其进行分类
- 使用caffe训练一个多标签分类/回归模型
- caffe的python接口学习(6):用训练好的模型(caffemodel)来分类新的图片
- [原]有关Caffe训练好的模型在Python接口下使用分类不准确的问题解决
- 使用caffe中的imagenet对自己的图片进行分类训练(超级详细版)
- caffe的python接口学习(6):用训练好的模型(caffemodel)来分类新的图片
- 用caffe自带的训练好的模型测试图片的分类结果,实现啦啦啦
- caffe的python接口学习(6):用训练好的模型(caffemodel)来分类新的图片
- !!使用Caffe对图片进行训练并分类的简单流程
- Caffe 之 使用非图片的鸢尾花(IRIS)数据集(hdf5格式) 训练网络模型
- 21天实战caffe - 运行基于已训练模型的图片分类例程 - 报错:Unable to decode image