caffe binaryproto 与 npy相互转换的实例讲解
作者:无眠栀 时间:2021-10-22 15:38:48
在caffe中,如果使用的是c++接口,均值文件默认为.binaryproto格式,而如果使用的是python接口,均值文件默认的是numpy的.npy格式,在工作中有时需要将两者进行互相转换,具体方式如下:
binaryproto -> npy
import numpy as np
import caffe
import sys
blob = caffe.proto.caffe_pb2.BlobProto()
data = open( 'mean.binaryproto' , 'rb' ).read()
blob.ParseFromString(data)
arr = np.array( caffe.io.blobproto_to_array(blob) )
out = arr[0]
np.save( 'mean.npy' , out )
npy -> binaryproto
data_mean is 1*H*W
import numpy as np
import caffe
import sys
blob = caffe.proto.caffe_pb2.BlobProto()
with open('mean.npy','rb') as f:
data_mean = numpy.load(f)
blob.channels=1
blob.height = data_mean.shape[0]
blob.width = data_mean.shape[1]
blob.data.extend(data_mean.astype(float).flat)
binaryproto_file = open('mean.binaryproto', 'wb' )
binaryproto_file.write(blob.SerializeToString())
binaryproto_file.close()
来源:https://blog.csdn.net/may0324/article/details/52316967
标签:caffe,binaryproto,npy
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python中re.findall()用法详解
2023-08-09 23:46:14
![](https://img.aspxhome.com/file/2023/1/61191_0s.png)
Python三维绘图之Matplotlib库的使用方法
2023-01-22 02:02:04
![](https://img.aspxhome.com/file/2023/7/95457_0s.png)
js实现将选中内容分享到新浪或腾讯微博
2023-08-25 07:39:02
![](https://img.aspxhome.com/file/2023/5/56225_0s.jpg)
js清空form表单中的内容示例
2023-08-23 16:22:02
微信跳一跳自动运行python脚本
2023-11-22 01:42:29
php全局变量和类配合使用深刻理解
2023-11-18 19:50:17
Python解析多帧dicom数据详解
2022-08-13 21:16:13
使用python-cv2实现视频的分解与合成的示例代码
2021-06-21 11:51:46
asp用正则过滤html标签
2008-01-19 16:34:00
python、Matlab求定积分的实现
2021-08-25 15:43:28
![](https://img.aspxhome.com/file/2023/8/90808_0s.jpg)
讲解Python中运算符使用时的优先级
2022-12-28 14:40:17
![](https://img.aspxhome.com/file/2023/2/92382_0s.jpg)
详解python日期时间处理2
2021-05-20 19:27:15
![](https://img.aspxhome.com/file/2023/3/86613_0s.png)
ASP 改良版MD5、SHA256多重加密类(二次及多次)
2012-11-30 20:26:47
Python实现批量下载图片的方法
2022-11-17 07:20:08
用js实现用户输入密码的强度
2007-11-22 21:41:00
python-yml文件读写与xml文件读写
2022-06-16 06:43:50
![](https://img.aspxhome.com/file/2023/9/67919_0s.png)
用python写测试数据文件过程解析
2022-03-24 16:54:57
![](https://img.aspxhome.com/file/2023/8/69588_0s.png)
实用技巧:优化SQL Server数据库查询方法
2009-02-04 13:46:00
在CMD命令行中运行python脚本的方法
2023-08-03 18:54:38
![](https://img.aspxhome.com/file/2023/9/60939_0s.jpg)
关于字符集和Unicode的相关知识[译]
2010-03-13 14:17:00
![](https://img.aspxhome.com/file/UploadPic/20103/13/01-36s.png)