macOS Sierra (10.12.4)下Caffe通过Python接口加载binaryproto格式的均值文件

macOS Sierra (10.12.4)下Caffe通过Python接口加载均值文件的时候,都是加载的.npy格式的文件,这个格式是Python存储的格式,跟我们经常下载到的.binaryproto格式的均值文件是不同的,这样就导致了加载问题。
.binaryprotoGoogleProtocol Buffer序列化后的数据,而.npy格式是Pythonnumpy模块序列化后的数据。

之所以会出现两种不同的存储格式,目前猜测是由于目前Python 3不能很好的支持Protocol Buffer导致的。

Python下是不能直接加载.binaryproto格式的数据的,必须进行一次转换才行,示例代码如下:

#coding=utf-8
#加载必要的库
import numpy as np
 
import sys,os

#设置当前目录
caffe_root = '/Users/Source/caffe/distribute/'
sys.path.append(caffe_root + 'python')

import caffe
os.chdir(caffe_root)

memnet_root = '/Users/Source/caffe/'
model_def =memnet_root + 'models/memnet/deploy.prototxt'
pretrained_model=memnet_root + 'models/memnet/memnet.caffemodel'
means_file=memnet_root + 'models/memnet/mean.binaryproto'

caffe.set_mode_cpu() 
blobProto=caffe.io.caffe_pb2.BlobProto()
binProtoFile=open(means_file,'rb')
blobProto.ParseFromString(binProtoFile.read())
means = caffe.io.blobproto_to_array(blobProto)[0]
binProtoFile.close()

# Make detector.
detector = caffe.Detector(model_def, pretrained_model, mean=means)

参考链接


发布者

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注