怎么读取caffe2输出的参数模型,为minidb格式的文件?

-------------------------------------------------------------- 问题说明 2.0 --------------------------------------------------------------
1. 我想做什么:读取caffe2训练后,输出的model文件,该文件格式为 minidb,caffe2给出的文档中( https://caffe2.ai/docs/tutorial-MNIST.html#main_wrap )在文章末尾,有如下语句进行了存储:

# save the model to a file. Use minidb as the file format 
pe.save_to_db("minidb", os.path.join(root_folder, "mnist_model.minidb"), pe_meta) 
print("The deploy model is saved to: " + root_folder + "/mnist_model.minidb")
 
2.为什么要这么做:要在一个不支持caffe2的设备上部署CNN,所以需要手写forward过程进行预测,进一步要读取训练好的参数;
-------------------------------------------------------------- 问题说明 2.0 --------------------------------------------------------------
 
-------------------------------------------------------------- 问题说明 1.0 --------------------------------------------------------------
各位大神好:
我想自己写一个forward过程,只是想用到caffe2训练的参数,不知道怎么读取这个二进制文件,还请赐教
-------------------------------------------------------------- 问题说明 1.0 --------------------------------------------------------------
已邀请:

breadbread1984 - Caffe2话题版主。深度学习研究者,Caffe的爱好者

赞同来自: alex68

用save操作符把要保存的blob输出,用load操作在init网络中符读入。非常简单

alex68 - 一般不扯淡~

赞同来自:

 
首先 一些现有的caffe2模型可以在这里下载:https://github.com/caffe2/caffe2/wiki/Model-Zoo
 
然后,利用现有的模型参数进行预测的代码:
# load up the caffe2 workspace
from caffe2.python import workspace
# choose your model here (use the downloader first)
from caffe2.python.models import squeezenet as mynet
# helper image processing functions
import caffe2.python.tutorials.helpers as helpers

import skimage.io
from matplotlib import pyplot as plt

# load the pre-trained model
init_net = mynet.init_net
predict_net = mynet.predict_net
# you must name it something
predict_net.name = "squeezenet_predict"
workspace.RunNetOnce(init_net)
workspace.CreateNet(predict_net)
p = workspace.Predictor(init_net.SerializeToString(), predict_net.SerializeToString())

# use whatever image you want (local files or urls)
#img_pth = "https://upload.wikimedia.org/w ... ot%3B
#img_pth = "https://upload.wikimedia.org/w ... ot%3B
img_pth = "https://cdn.pixabay.com/photo/ ... ot%3B
# average mean to subtract from the image
mean = 128
# the size of images that the model was trained with
input_size = 227

# use the image helper to load the image and convert it to NCHW
img = helpers.loadToNCHW(img_pth, mean, input_size)

# submit the image to net and get a tensor of results

results = p.run([img])
response = helpers.parseResults(results)
# and lookup our result from the list
print response

#show result on image
img_mat = skimage.io.imread(img_pth)
skimage.io.imshow(img_mat)
plt.title(response,{'fontsize': '20'})
plt.savefig('pretzel.jpg')
plt.show()

其实官方doc里面都有
 

要回复问题请先登录注册