mean.binaryproto转mean.npy
使用Caffe的C++接口进行操作时,需要的图像均值文件是pb格式,例如常见的均值文件名为mean.binaryproto;但在使用python接口进行操作时,需要的图像均值文件是numpy格式,例如mean.npy。所以在跨语言进行操作时,需要将mean.binaryproto转换成mean.npy,转换代码如下:
import sys
sys.path.append("/home/aaa/caffe/python")
sys.path.append("/home/aaa/caffe/python/caffe")
import caffe
import numpy as np
MEAN_PROTO_PATH = 'imagenet_mean.binaryproto'
MEAN_NPY_PATH = 'imagenet_mean.npy'
blob = caffe.proto.caffe_pb2.BlobProto()
data = open(MEAN_PROTO_PATH, 'rb' ).read()
blob.ParseFromString(data)
array = np.array(caffe.io.blobproto_to_array(blob))
mean_npy = array[0]
np.save(MEAN_NPY_PATH ,mean_npy)
生成的结果如下: imagenet_mean.npy