Learn to Code in TensorFlow2 : Part2

Divyanshu Raj
3 min readJan 9, 2020

--

In this article we will learn how to write ResNet18 model in TensorFlow2 and how to view the graph of the created model.

Source Code

Assumption is that you are aware of ResNet architecture and how residual network works. Have a look at the data preparation done in the previous part.

Defining a custom model (ResNet18)

Common Layers:

Batch normalization and Relu are most commonly used layers, so we define them in a separate class.

Input Layer => Batch Normalization => ReLu => Output Layer
Common Layer when flag != 0

Please note that if anywhere you use common layers back to back you will get stuck into NaN (Not a Number) issue. See this blog on how to resolve NaN issue.

ResBLK:

We need 3 versions of ResNet blocks where:

  1. We do not reduce the dimension.
  2. We reduce the dimension and increase the number of channels.
  3. We take the combination of pooling for last block.

This code can achieve all three with combinations of flags:

1. No reduction in dimension of layers
2. Dimension of layers changes
3. Last block.

ResNet18 Model:

ResNet18 Full Graph

Generally the issue faced here is the Graph. Thanks to Kinshuk Sarabhai for helping me with the graph.

Viewing the Graph with TensorBoard for the defined model

After you have defined the model, you can write a small code to create the graph and verify visually if you have written the graph/model you were expecting.

!ls logs/func/

You will see the folder created with current timestamp. For example : 20200109–135021

Launch the TensorBoard and click on Graph to view it.

%tensorboard --logdir logs/func/20200109-135021/

You can do all of the above in Google Colab itself.

Now that we have data ready and the model ready, we can proceed to training them in next part.

--

--

Divyanshu Raj
Divyanshu Raj

Written by Divyanshu Raj

SDE@Amazon | divraz.github.io | Connect with me on LinkedIn @divyanshu-raj.

No responses yet