Skip to content

Converting Caffemodels To PyTorch

ProGamerGov edited this page Nov 17, 2019 · 6 revisions

There are many different projects for converting models of various formats, to PyTorch's .pth format, but for this guide, I am going to use a modified version of jcjohnson's pytorch-vgg. Other conversion tools may result in compatibility errors with Neural-Style-PT, and I have only tested extensively with pytorch-vgg.

The unmodified version of jcjohnson's pytorch-vgg should work as well, but there may be unforeseen issues with the model definitions, as it relies on PyTorch's pretrained model definitions. PyTorch also does not have a default NIN model definition.


Dependencies:


Lua/Torch7:

The Lua/Torch7 requirements for the model conversion scripts are:

  • Torch7

  • cutorch

  • cunn

  • cudnn

  • loadcaffe

You can follow the setup guide from Neural-Style's INSTALL.md (omitting the Neural-Style specific parts), to get the required Torch packages for model conversion.

PyTorch:

PyTorch removed support for Torch model conversions after version 0.4.1, so make sure to install version 0.4.1 if you want to convert models:

pip3 install torch==0.4.1 torchvision==0.2.1

For PyTorch, only the Torch and Torchvision packages are required.

You can follow the setup guide from Neural-Style-PT's INSTALL.md (omitting the Neural-Style-PT specific parts), to get the required PyTorch packages for model conversion.


Installing pytorch-vgg:


cd ~/
git clone https://github.com/ProGamerGov/pytorch-vgg.git
cd pytorch-vgg

We will also need the CaffeLoader script from Neural-Style-PT, for the model definitions:

wget https://raw.githubusercontent.com/ProGamerGov/neural-style-pt/master/CaffeLoader.py

Usage:


Below is an example of converting the default VGG-19 model that Neural-Style uses, to PyTorch's .pth format:

First the model is converted to a .t7 format, from it's caffemodel and prototxt files:

th caffemodel_to_t7.lua -input_prototxt VGG_ILSVRC_19_layers_deploy.prototxt -input_caffemodel VGG_ILSVRC_19_layers.caffemodel -output_t7 VGG_ILSVRC_19_layers.t7

Then the model is converted from a .t7 format, to a .pth format:

python t7_to_state_dict.py --input_t7 VGG_ILSVRC_19_layers.t7 --model_name VGG_ILSVRC_19_layers
  • The t7_to_state_dict.py has been modified so that the --model_name parameter value is used to search for the definition used to convert the model. Only VGG-16, VGG-19, and NIN models are supported currently, though it should be easy to add support for other models.

To correct the model file's weight and bias names so that it's compatible with neural-style-pt, you can use this script: https://gist.github.com/ProGamerGov/1bc833a8ae91f81e7e40037d052f8193


You can find a list of Caffe models that should be compatible with these conversion scripts, on the Neural-Style wiki: https://github.com/jcjohnson/neural-style/wiki/Using-Other-Neural-Models


Another conversion tool that can be used is: https://github.com/clcarwin/convert_torch_to_pytorch. Though you have to use PyTorch-vgg's caffemodel to t7 script first.