A PyTorch-Keras converter made for Cadene's pretrained models.
Also converts some simple PyTorch models. See supported layers for more details.
These instructions will get you a copy of the project up and running on your local machine.
Python : supported versions : >=3.5
You can also install python with Anaconda.
Download files:
git clone https://github.com/sonibla/pytorch_keras_converter.git
cd pytorch_keras_converter
Then install pytorch_keras_converter
using setup.py
The following command should install automatically pytorch_keras_converter
and every dependency:
python3 setup.py install --user
To install on a particular version of Python (here 3.7):
python3.7 setup.py install --user
To install on the entire system (requires administrator privileges):
sudo python setup.py install
If modules pip
or setuptools
aren't installed on your Python environment:
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py --user
Dependencies should install automatically when running setup.py
. But if it fails, install them manually:
python -m pip install torch pretrainedmodels tensorflow graphviz numpy h5py tqdm --user
Remove --user
to install on the whole system, replace python
with python3.7
to install on Python 3.7.
- To convert
se_resnet50
:
>>> import pytorch_keras_converter as pkc
>>> pkc.cadene_to_tf(['se_resnet50'], quiet=False)
- To convert
bninception
andfbresnet152
:
>>> import pytorch_keras_converter as pkc
>>> pkc.cadene_to_tf(['bninception', 'fbresnet152'])
- To convert
se_resnet152
with random weights:
>>> import pytorch_keras_converter as pkc
>>> pkc.cadene_to_tf(['se_resnet152(pretrained=None)'])
- To automatically convert many models:
Create a file containing on each line one model's name. For example:
resnet18
resnet34
resnet50(pretrained=None)
resnet50
Let's store this file in models.txt
.
Now, in a Python shell:
>>> import pytorch_keras_converter as pkc
>>> pkc.cadene_to_tf('models.txt')
def convert(model,
input_shape,
weights=True,
quiet=True,
ignore_tests=False,
input_range=None,
save=None,
filename=None,
directory=None):
- model: A Keras or PyTorch model or layer to convert
- input_shape (list, tuple or int): Input shape, without batchsize.
- weights (bool): Also convert weights. If set to False, only convert model architecture
- quiet (bool): If False, display a progress bar and some messages
- ignore_tests (bool):
If tests should be ignored. If weights is False, tests are automatically ignored.
- If set to True, converted model will still be tested by security. If models are not identical, it will only print a warning.
- If set to False, and models are not identical, RuntimeWarning will be raised
- input_range: Optional. A list of 2 elements containing max and min values to give as input to the model when performing the tests. If None, models will be tested on samples from the "standard normal" distribution.
- save: If model should be exported to a hdf5 file.
- filename: Optional. Filename to give to model's hdf5 file. If filename is not None and save is not False, then save will automatically be set to True
- directory: Optional. Where to save model's hdf5 file. If directory is not None and save is not False, then save will automatically be set to True
- RuntimeWarning: If converted and original model aren't identical, and ignore_tests is False
If model has been exported to a file, it will return the name of the file. Else, it returns the converted model.
def convert_and_save(model,
input_shape,
weights=True,
quiet=True,
ignore_tests=False,
input_range=None,
filename=None,
directory=None):
- model: A Keras or PyTorch model or layer to convert
- input_shape (list, tuple or int): Input shape, without batchsize.
- weights (bool): Also convert weights. If set to False, only convert model architecture
- quiet (bool): If False, display a progress bar and some messages.
- ignore_tests (bool):
If tests should be ignored. If weights is False, tests are automatically ignored.
- If set to True, converted model will still be tested by security. If models are not identical, it will only print a warning.
- If set to False, and models are not identical, RuntimeWarning will be raised
- input_range: Optional. A list of 2 elements containing max and min values to give as input to the model when performing the tests. If None, models will be tested on samples from the "standard normal" distribution.
- filename: Optional. Filename to give to model's hdf5 file. If filename is not None and save is not False, then save will automatically be set to True
- directory: Optional. Where to save model's hdf5 file. If directory is not None and save is not False, then save will automatically be set to True
Name of created hdf5 file
def cadene_to_tf(modelList=None,
outputDirectory=None,
quiet=True):
- modelList: A tuple or list of names of the models to convert OR A str telling the emplacement of a file containing names of models to convert (one model per line). If you want a particular config for each model, put it between parenthesis after model's name, for example: 'se_resnet50(pretrained=None)'
- outputDirectory (str): Optionnal. Where hdf5 files should be saved
- quiet (bool): If False, display a progress bar
- TypeError: If modelList wasn't a str, list, or tuple
A list of created files
Supported Cadene's models
- SE-ResNet50
- SE-ResNet101
- SE-ResNet152
- CaffeResNet101
- BNInception
- FBResNet152
- ResNet18
- ResNet34
- ResNet50
- ResNet101
- ResNet152
Some simple PyTorch models are supported. Supported layers are:
- torch.nn.AvgPool2d(count_include_pad=True)
- torch.nn.MaxPool2d(dilation=1)
- torch.nn.Batchnorm2d
- torch.nn.Conv2d(groups=1, padding_mode='zeros')
- torch.nn.Linear
- torch.nn.ZeroPad2d
- torch.nn.Dropout
- torch.nn.Sequential
- torch.nn.ReLU
- torch.nn.Sigmoid
- torch.nn.AdaptiveAvgPool2d(output_size=1)
To convert a custom PyTorch model, use pytorch_keras_converter.API
.
If pretrainedmodels
module isn't available, you can still convert models manually.
- Download
pretrainedmodels
from GitHub
git clone https://github.com/Cadene/pretrained-models.pytorch.git
- Open a Python shell
cd pretrained-models.pytorch
python
- Create a model
>>> import pretrainedmodels
>>> model = pretrainedmodels.se_resnet50()
>>> input_shape = pretrainedmodels.pretrained_settings['se_resnet50']['input_size']
- Convert your model
>>> import pytorch_keras_converter as pkc
>>> pkc.API.convert_and_save(model, input_shape)
If you use pytorch_keras_converter.API
or pytorch_keras_converter.cadene_to_tf
it will only show a warning : Warning: tests unavailable!
.
If you manually test models using pytorch_keras_converter.tests
it will raise the exception InvalidArgumentError
.
This is because some TensorFlow layers only support NHWC (i.e channels last) on CPU.
Using Anaconda solves this issue.
This repository is fully PEP8 compliant.
When reporting issues, please specify your OS, your version of Python, versions of every dependency and if you're using a particular environment (Anaconda for example).
You can use pip freeze
to see versions of your Python modules.
This project is licensed under the MIT License - see the LICENSE file for details