Extracting weights from .caffemodel without caffe installed in Python

jeandut picture jeandut · Jun 1, 2016 · Viewed 10.9k times · Source

Is there a relatively simple way to extract weights in Python from one of the many pretrained models in Caffe Zoo WITHOUT CAFFE (nor pyCaffe)? i.e. parsing .caffemodel to hdf5/numpy or whatever format that can be read by Python?

All the answers I found use C++ code with caffe classes or Pycaffe. I have looked at pycaffe's code it looks like you really need caffe to make sense of the binary is that the only solution?

Answer

Lolo picture Lolo · Mar 16, 2018

I had to resolve that exact issue just now. Assuming you have a .caffemodel (binary proto format), it turns out to be quite simple.

  1. Download the latest caffe.proto

  2. Compile into python library: protoc --python_out=. caffe.proto

  3. Import and parse

The sample code below

import numpy as np
import sys, os
import argparse
import caffe_pb2 as cq

f = open('VGG_ILSVRC_16_layers.caffemodel', 'r')
cq2 = cq.NetParameter()
cq2.ParseFromString(f.read())
f.close()
print "name 1st layer: " + cq2.layers[0].name 

produces for me:

name 1st layer: conv1_1

Obviously you can extract anything else you want from your object. I just printed the name of my first layer as an example. Also, your model may be expressing the layers in either the layers array (deprecated) or the layer (no 's') array, but you get the gist.