Migrating a Model to Keras 2.0

Keras v2.0 has been released for a couple of months now – v2.0.0 released on 5th May, 2017, while the latest version is 2.0.8 at the time of this writing. It brought in a lot of new features and improvements, but also made some syntax changes. Trying to run a code with the old syntax may result in anything from a flood of deprecation warnings, to not being able to run the code at all. Since there are many code examples online which uses the older syntax – including some older posts in Codes of Interest – it’s better to know how to get such older syntax model to work on the 2.0 API.
The complete list of changes in Keras v2.0 was extensive, but the following list would help you to narrow down majority of the changes.
The most prominent change is the changing of image_dim_ordering parameter to image_data_format, and its associated values from “tf“, and “th” to “channels_last” and “channels_first“. We talked about this change in detail in our earlier post “What is the image_data_format parameter in Keras, and why is it important“.
Likewise, in all the places where “dim_ordering” argument/parameter was used, it has been changed to “data_format“.
All of the Convolution* layers have now need renamed to Conv*.
E.g. Convolution2D is renamed to Conv2D

Make sure you update your imports as well,
E.g. If the older code was

 from keras.layers.convolutional import Convolution2D  
 from keras.layers.convolutional import MaxPooling2D  
 ....  
 model.add(Convolution2D(.....  

Change it to,

 from keras.layers.convolutional import Conv2D  
 from keras.layers.convolutional import MaxPooling2D  
 ....  
 model.add(Conv2D(.....  

The filter dimensions parameter is now a single tuple.
E.g.

 model.add(Convolution2D(20, 5, 5,  

changed to,

 model.add(Conv2D(20, (5, 5),  

The border_mode parameter has been changed to padding.
E.g. Change

 model.add(Convolution2D(20, 5, 5, border_mode="same",  

to

 model.add(Conv2D(20, (5, 5), padding="same",  

In model.fit() the nb_epoch parameter has been changed to epochs.
E.g.Change

 model.fit(trainData, trainLabels, batch_size=128, nb_epoch=20, verbose=1)  

to

 model.fit(trainData, trainLabels, batch_size=128, epochs=20, verbose=1)  

These changes would set you up to solve most of the issues encountered when migrating to Keras 2.0.
For a full list of changes, you can check the Keras 2.0.0 Release Notes.

Build Deeper: The Path to Deep Learning

Learn the bleeding edge of AI in the most practical way: By getting hands-on with Python, TensorFlow, Keras, and OpenCV. Go a little deeper…

Get your copy now!


Posted

in

,

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *