Home

Awesome

Caffe-to-Keras Weight Converter


Contents

  1. Overview
  2. Dependencies
  3. How to use it
  4. Important notes
  5. ToDo
  6. Converted weights
  7. Why it is better to convert weights only, not model definitions

Overview

This is a Python tool to extract weights from a .caffemodel file and do either of two things:

  1. Export the Caffe weights to an HDF5 file that is compatible with Keras 2. Or
  2. Export the Caffe weights to a pickled file that contains the weights as plain Numpy arrays along with some other information (such as layer types and names for all layers). This format may be useful if you want to load the weights into a deep learning framework other than Keras.

That is, this is mainly a Caffe-to-Keras weight converter, but you can also have it export the weights into a simpler, possibly more familiar Python format (list of dictionaries) instead.

Further below you can also find a list of links to weights for various models that I ported to Keras using this very converter.

There are tools out there that attempt to convert both the model definition and the weights of a Caffe model to a given other deep learning framework (like the great caffe-tensorflow), but I don't believe that's the right approach. If you'd like to know why, read below. This program converts the weights only, not the model definition.

Dependencies

The bad news is that you need to have Caffe with Pycaffe installed to use this converter. The good news is that you don't need to have any clue about how to use Caffe, it just needs to be installed.

How to use it

1. Command line interface

To convert a .caffemodel file to a Keras-compatible HDF5 file with verbose console output:

python caffe_weight_converter.py 'desired/name/of/your/output/file/without/file/extension' \
                                 'path/to/the/caffe/model/definition.prototxt' \
                                 'path/to/the/caffe/weights.caffemodel' \
                                 --verbose

To extract the weights as Numpy arrays and save them in a pickled file along with layer types, names, inputs and outputs:

python caffe_weight_converter.py 'desired/name/of/your/output/file/without/file/extension' \
                                 'path/to/the/caffe/model/definition.prototxt' \
                                 'path/to/the/caffe/weights.caffemodel' \
                                 --format=pickle

The command line interface takes three positional arguments in this order:

For more details about the available options, execute

python caffe_weight_converter.py --help

2. Use within another Python program or Jupyter notebook

from caffe_weight_converter import convert_caffemodel_to_keras, convert_caffemodel_to_dict

Read the documentation in caffe_weight_converter.py for details on how to use these two functions.

Important notes

ToDo

Converted weights

I'll post any weights that I ported to Keras here. The filenames of the weight files are always the same as the names of the original .caffemodel files from which they were ported.

Why it is better to convert weights only, not model definitions

There are a few reasons why I think it makes more sense to have a converter that does't try to translate the model definition, but instead converts the model's weights only: