Upgrading old tensorflow codes to new tensorflow 2.0+ style.

moonhwan Jeong
2 min readAug 8, 2020

In this post I will introduce an example of how to upgrade legacy tensorflow codes to new style. tensorflow 2.0 recommends using Keras which is a well known high level API. Their sequential style approach makes your code more simple. And the script would be worked line by line, so we can easily understand the working process.

Let me introduce a guide by my BEGAN repository

1. Prepare a dataset

For preparing the dataset, at the previous version, I made the ImageIterator class which provides preprocessing and iterator. Obviously, it is not quite hard, cos we can find a kind guide on the homepage for that. But it looks cumbersome. In tensorflow 2.0 you don’t have to make that kind of class, we just use ImageDataGenerator which is a predefined API for data generation. We can set parameters for preprocessing and data augmentation when we create ImageDataGenerator. I gave some parameters for normalization and horizontal flipping as data augmentation as shown in below.

2. Design a network.

The began network consists of a decoder and an encoder. In the previous repository, to implement a Convolution Layer, I used 3 low level functions as shown in below.

And I also predefined some parameters about layer weights.

So, someone used to define a custom function for reducing code lines and better readability.

But, with Keras, I only needed just a line.

The high level API includes parameters on how to define activation and bias. So we don’t have to make any custom function. We just use predefined Keras APIs. I could design the decoder network using about 20 lines. Awesome!!

Before training the network we have to make a model which takes inputs and returns outputs. Keras provides an API for that.
Look at the codes to feel how simple it is.

It is interesting I didn’t write any lines for defining variable scope like previous codes. It works automatically on the Keras API.

3. Train and Test a Model

3.1 Train

What I really love about Keras is I don’t have to use sess.run anymore. I think that the old style process makes codes quite complex. Look at the flow, we can easily follow these lines how this is gonna work.

The model only had been trained for 5 hours on a Nvidia 1080TI. But the generator could show quite impressive outputs.

3.2 Test

Look at the super tiny test codes. There’s no excuse for not updating your legacy repository.

I also updated my DCGAN repository. Visit the repository, switch branches and check updated codes. I derived help from a tutorial that introduced the tensorflow homepage. I would like to recommend you read the tutorial too.

--

--