当前位置:网站首页>教你自己训练的pytorch模型转caffe(三)
教你自己训练的pytorch模型转caffe(三)
2022-07-05 20:41:00 【FeboReigns】
这一节是用caffe推理得到pytorh一样的结果
参考:使用Caffe的Python接口进行推理 - 简书 (jianshu.com)
#coding = utf-8
import numpy as np
import sys, os
sys.path.insert(0, caffe_root + 'python')
import caffe
os.chdir(caffe_root)
#caffe.set_device(0)
#caffe.set_mode_gpu()
####下面三行根据自己的路径修改
net_file = './dock_googlenet_dog.prototxt'
caffe_model = './dock_googlenet_dog.caffemodel'
imagenet_labels_filename = './class_names.txt'
# load model
net = caffe.Net(net_file, caffe_model, caffe.TEST)
#resize
transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})
#通道数拿到前面来 变为【1,3 224,224】
transformer.set_transpose('data', (2, 0, 1))
# # 调用均值文件
# # transformer.set_mean('data', np.load(mean_file).mean(1).mean(1))
#python中将图片存储为[0, 1],而caffe中将图片存储为[0, 255],所以需要一个转换
# transformer.set_raw_scale('data', 255)
#caffe中图片是RGB格式,而网络格式BGR,要转化
# transformer.set_channel_swap('data', (2, 1, 0))
###根据自己的路径更改
im = caffe.io.load_image('./cat.2.jpg')
net.blobs['data'].data[...] = transformer.preprocess('data',im)
# net.blobs['data'].data[...] =
out = net.forward()
print (net.blobs['data'].data.shape)
print (net.blobs['data'].data.dtype)
print (net.blobs[net.blobs.keys()[-1]].data.shape)
print (net.blobs[net.blobs.keys()[-1]].data.dtype)
labels = np.loadtxt(imagenet_labels_filename, str, delimiter='\t')
top_k = net.blobs[net.blobs.keys()[-1]].data[0].flatten().argsort()[-1:-6:-1]
for i in np.arange(top_k.size):
print (top_k[i], labels[top_k[i]])
#打印最后得分
print (net.blobs[net.blobs.keys()[-1]].data[0])./class_names.txt就是
0 dog
1 cat注意我训练是没有使用normlize,
而且我的格式是RGB的,所以预处理我有几个没用
caffe的caffe.io.load_image('./cat.2.jpg')是RGB的

我的也是RBG的

如果你用老哥的docker环境跑我的代码会报三个错
###got an unexpected keyword argument ‘as_grey‘
https://blog.csdn.net/hjxu2016/article/details/113541609
###TypeError: 'float' object cannot be interpreted as an integer
https://blog.csdn.net/qq_28634403/article/details/81224291
###dict_keys‘ object is not subscriptable
https://blog.csdn.net/le___le/article/details/103617431我用的自己配置的caffe,我的caffe没有ceil mode参数,用round_mode代替的,如果ceil mode为true 则round_mode为0,否则反之。准确度基本不会影响Pytorch maxpool的ceil_mode及与caffe中maxpool的比较_zl3090的博客-CSDN博客
如何自己配caffe看我另一个博客:
边栏推荐
- AI automatically generates annotation documents from code
- 2020 CCPC Weihai - A. golden spirit (thinking), D. ABC project (big number decomposition / thinking)
- 欢迎来战,赢取丰厚奖金:Code Golf 代码高尔夫挑战赛正式启动
- 【UE4】UnrealInsight获取真机性能测试报告
- Welcome to the game and win rich bonuses: Code Golf Challenge officially launched
- Abnova丨 MaxPab 小鼠源多克隆抗体解决方案
- Chemical properties and application instructions of prosci Lag3 antibody
- PyTorch 1.12发布,正式支持苹果M1芯片GPU加速,修复众多Bug
- Classic implementation method of Hongmeng system controlling LED
- 表单文本框的使用(二) 输入过滤(合成事件)
猜你喜欢

Return to blowing marshland -- travel notes of zhailidong, founder of duanzhitang

2022 Beijing eye health products exhibition, eye care products exhibition, China eye Expo held in November
![[Yugong series] go teaching course in July 2022 004 go code Notes](/img/18/ffbab0a251dc2b78eb09ce281c2703.png)
[Yugong series] go teaching course in July 2022 004 go code Notes

2.8、项目管理过程基础知识
mysql全面解析json/数组

Kubernetes resource object introduction and common commands (V) - (configmap & Secret)

Applet global configuration

How to form standard interface documents

Abnova丨CRISPR SpCas9 多克隆抗体方案

小程序页面导航
随机推荐
Make Jar, Not War
How to choose a good external disk platform, safe and formal?
Redis唯一ID生成器的实现
19 mongoose modularization
手机开户股票开户安全吗?我家比较偏远,有更好的开户途径么?
The Chinese Academy of Management Sciences gathered industry experts, and Fu Qiang won the title of "top ten youth" of think tank experts
Norgen AAV extractant box instructions (including features)
鸿蒙系统控制LED的实现方法之经典
Abnova丨CRISPR SpCas9 多克隆抗体方案
When JS method passes long type ID value, precision loss will occur
Classic implementation method of Hongmeng system controlling LED
Duchefa丨D5124 MD5A 培养基中英文说明书
Mongodb/ document operation
Is the securities account given by the school of Finance and business safe? Can I open an account?
2022北京眼睛健康用品展,护眼产品展,中国眼博会11月举办
[quick start of Digital IC Verification] 2. Through an example of SOC project, understand the architecture of SOC and explore the design process of digital system
How to renew NPDP? Here comes the operation guide!
Introduction to dead letter queue (two consumers, one producer)
Abnova cyclosporin a monoclonal antibody and its research tools
欢迎来战,赢取丰厚奖金:Code Golf 代码高尔夫挑战赛正式启动