Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Simon-Hohberg/8feb2d2065204a1ba3ae to your computer and use it in GitHub Desktop.
Save Simon-Hohberg/8feb2d2065204a1ba3ae to your computer and use it in GitHub Desktop.
Create a binary proto file that contains a blob with the mean.
import sys
caffe_root = '/path/to/caffe/'
sys.path.insert(0, caffe_root + 'python')
import caffe.proto.caffe_pb2 as caffe_proto
def create_mean_file(filename, mean):
"""
Create a binaryproto file with a single blob containing the given pixel-wise mean.
Overwrites any existing file with the same name.
:param filename: Filename to be used
:type filename: str
:param mean: Numpy array holding the mean
:type mean: numpy.multiarray.ndarray
"""
blob = caffe_proto.BlobProto()
blob.shape.dim.extend(map(int, mean.shape))
blob.data.extend(mean.flatten().tolist())
# legacy
blob.channels = mean.shape[0]
blob.height = mean.shape[1]
blob.width = mean.shape[2]
blob.num = 1
with open(filename, "wb") as f:
f.write(blob.SerializeToString())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment