Skip to content

Instantly share code, notes, and snippets.

@crowsonkb
Created January 17, 2022 09:16
Show Gist options
  • Save crowsonkb/e32680de3efb9434676cf5ab4c944e9b to your computer and use it in GitHub Desktop.
Save crowsonkb/e32680de3efb9434676cf5ab4c944e9b to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
"""Dumps a Caffe binary model to a pickle of NumPy arrays."""
import argparse
from collections import OrderedDict
import os
import pickle
def main():
p = argparse.ArgumentParser(description=__doc__)
p.add_argument('model', type=str,
help='the model definition (.prototxt)')
p.add_argument('weights', type=str,
help='the model weights (.caffemodel)')
p.add_argument('output', type=str,
help='the output file')
args = p.parse_args()
os.environ['GLOG_minloglevel'] = '1'
import caffe
caffe.set_mode_cpu()
net = caffe.Net(args.model, 1, weights=args.weights)
out = OrderedDict()
for name, layer in net.layer_dict.items():
out[name] = [blob.data for blob in layer.blobs]
for i, blob in enumerate(layer.blobs):
print(f'Found layer {name}, blob {i}, shape {blob.data.shape}')
with open(args.output, 'wb') as fp:
pickle.dump(out, fp)
print(f'Written to {args.output}.')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment