Last active
April 15, 2019 08:08
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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