Skip to content

Train your neural network with Keras and transfer the pre-trained weights to a PyTorch model.

Notifications You must be signed in to change notification settings

execreate/keras_to_pytorch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Train with Keras, evaluate with PyTorch

I came up with the code in this repository while doing my homeworks on Introduction to Deep Learning class. At the moment the assignments in this class are based on PyTorch machine learning framework which does not support GPU acceleration on macOS with AMD GPUs. In order to utilize the hardware at my disposal and significantly improve the training time, while playing around with neural networks to solve my assignments, I started training my networks using Keras with PlaidML backend. At the end of the day I had to convert my Keras model into a PyTorch model in order to submit my solution for evaluation.

Spoiler alert

It did not work, the PyTorch model produces terrible loss :(
Refer plug_n_play.ipynb for more details

Getting started

Create a virtual environment with Python 3.8 using virtualenv or anaconda and install the requirements:

pip install -r requirements.txt

Also make sure to set the default device for PladML:

plaidml-setup

Start a jupyter notebook server (just run jupyter notebook in your console) and feel free to play around with plug_n_play.ipynb.

Going deeper

You can also use Tensorflow backend for Keras instead of PlaidML, just make sure to change the imports.

Take a look on models/model_constructor.py to see how Keras and PyTorch models are constructed. In models/utils.py you'll find the code, which copies the weights from Keras model to a PyTorch model.

About

Train your neural network with Keras and transfer the pre-trained weights to a PyTorch model.

Topics

Resources

Stars

Watchers

Forks