当前位置:网站首页>教你自己训练的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看我另一个博客:
边栏推荐
- 表单文本框的使用(二) 输入过滤(合成事件)
- Enter the parallel world
- 清除app data以及获取图标
- Informatics Olympiad 1337: [example 3-2] word search tree | Luogu p5755 [noi2000] word search tree
- 【愚公系列】2022年7月 Go教学课程 004-Go代码注释
- Cutting edge technology for cultivating robot education creativity
- Document method
- 14、Transformer--VIT TNT BETR
- Mongodb/ document operation
- 2022 Beijing eye health products exhibition, eye care products exhibition, China eye Expo held in November
猜你喜欢
解析创客教育的知识迁移和分享精神
[record of question brushing] 1 Sum of two numbers
CTF reverse Foundation
Specification of protein quantitative kit for abbkine BCA method
Make Jar, Not War
When steam education enters personalized information technology courses
Abnova maxpab mouse derived polyclonal antibody solution
Enter the parallel world
Abnova丨荧光染料 620-M 链霉亲和素方案
鸿蒙系统控制LED的实现方法之经典
随机推荐
14、Transformer--VIT TNT BETR
Usaco3.4 "broken Gong rock" band raucous rockers - DP
Analysis of steam education mode under the integration of five Education
Duchefa cytokinin dihydrozeatin (DHZ) instructions
Duchefa丨D5124 MD5A 培养基中英文说明书
欢迎来战,赢取丰厚奖金:Code Golf 代码高尔夫挑战赛正式启动
Y57. Chapter III kubernetes from entry to proficiency -- business image version upgrade and rollback (30)
Abnova丨血液总核酸纯化试剂盒预装相关说明书
Abnova丨 CD81单克隆抗体相关参数和应用
National Eye Care Education Conference, 2022 the Fourth Beijing International Youth eye health industry exhibition
Abbkine trakine F-actin Staining Kit (green fluorescence) scheme
Duchefa p1001 plant agar Chinese and English instructions
小程序全局配置
Abnova丨 MaxPab 小鼠源多克隆抗体解决方案
PyTorch 1.12发布,正式支持苹果M1芯片GPU加速,修复众多Bug
Abnova丨培养细胞总 RNA 纯化试剂盒中英文说明书
Duchefa细胞分裂素丨二氢玉米素 (DHZ)说明书
Selenium element information
Dry goods navigation in this quarter | Q2 2022
线程池的使用