Converting Caffemodels To PyTorch - ProGamerGov/pytorch-vgg GitHub Wiki
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:
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.
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