Learn to Code in TensorFlow2 : Part2
In this article we will learn how to write ResNet18 model in TensorFlow2 and how to view the graph of the created model.
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
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:
- We do not reduce the dimension.
- We reduce the dimension and increase the number of channels.
- We take the combination of pooling for last block.
This code can achieve all three with combinations of flags:
ResNet18 Model:
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.