ResNet won first place in the Large Scale Visual Recognition Challenge (ILSVRC) in 2015. It was the first neural network not affected by the “vanishing gradient” problem. TensorFlow makes it easy to build ResNet models: you can run pre-trained ResNet-50 models or build your own custom ResNet implementation.
What is ResNet?
Residual Network (ResNet) is a Convolutional Neural Network (CNN) architecture, designed to train very deep neural networks. In theory, a deeper neural network should perform better on the training set because of additional layers processing smaller and smaller features. In reality, the frequency of training errors increases when the network is too deep. With ResNet, the training error actually decreases as the network gets deeper.
ResNet provides a breakthrough solution to the “vanishing gradient” problem. Vanishing gradient is a difficulty encountered when you train artificial neural networks with gradient-based methods like backpropagation. With these methods, the gradients of the loss function approach zero as you add more layers to the network. This makes it hard to learn and tune the parameters of the earlier layers in the network.
What are Identity Shortcut Connections?
ResNet is based on “shortcut connections”. This is a way to skip the training of one or more layers — creating a residual block. Residual blocks allow you to train much deeper neural networks. ResNet structured by taking many of these blocks and stacking them together to form a deep network.
Issues in Running ResNet
ResNet can have between dozens and thousands of convolutional layers and can take a long time to train and execute —from hours to several weeks in extreme cases. You will need to distribute a ResNet model across multiple GPUs, and if performance is insufficient, scale out to multiple machines.
However, you’ll find that running a deep learning model on multiple machines is difficult:
On-premises—you will need to set up multiple machines for deep learning, manually run experiments and utilize resources.
In the cloud—you can spin up machines quickly, but you will need to build and test machine images, and manually run experiments on each machine. You’ll need to “babysit” your machines to ensure an experiment is always running, and to avoid wasting money with expensive GPU machines.
Options for Running ResNet on TensorFlow
With this, you have learned the basics of ResNet but the real challenge is to learn how to train ResNet model for Image Classification. Learn the Implementation through video and code walkthrough.
Image Classification using a Pre-Trained ResNet Model
The TensorFlow official models are a collection of example models that use TensorFlow’s high-level APIs. The official TensorFlow Resnet model contains an implementation of ResNet for the ImageNet.
You can download pre-trained versions of ResNet using Keras. Below is the example to load pre-trained model of Resent50
Step 1– Load the required packages
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from tensorflow.keras.applications.resnet50 import preprocess_input
from tensorflow.keras.applications.resnet50 import decode_predictions
from tensorflow.keras.applications.resnet50 import ResNet50
Step 2 – Load Model and its summary
model = ResNet50()
model.summary()
Step 3 – Load image, convert it to numpy array, reshape the images to fit the model and finally pre-process it for Resnet Model
image = load_img('IMG_1018.jpg', target_size=(224, 224))
image = img_to_array(image)
image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
image = preprocess_input(image)
Step 4 – Predict the probability and convert to class labels, calculate highest probability and show classification
pred = model.predict(image)
label = decode_predictions(pred)
label = label[0][0]
print(label)
Output – (‘n01622779’, ‘great_grey_owl‘, 0.99860114)
Train TensorFlow ResNet From Scratch for Image Classification on your own Dataset
While transfer learning is a powerful technique, you’ll find it valuable to learn how to train ResNet from scratch. Become familiar with the full training process, from launching TensorFlow, downloading and preparing ImageNet, to documenting and reporting training results.
To illustrate the process, here is TensorFlow code on how to train a ResNet model from scratch in TensorFlow:
You can implement different versions of ResNet – ResNet18, ResNet34, ResNet50, ResNet101, ResNet152 using TensorFlow-2.0. Let me give you walk through of Jupyter Notebook file checked-in in github repository https://github.com/indiantechwarrior/Resnet_Model_TF2.0
Steps 1 – Clone the Resnet code from below github link
!git clone https://github.com/indiantechwarrior/Resnet_Model_TF2.0
Steps 2 – Mount the Google Drive
from google.colab import drive
drive.mount('/content/drive')
Step 3 – Change the directory
import os
os.chdir("/content/Resnet_Model_TF2.0/")
Step 4 – Unzip the Dataset from Google Drive
!unzip /content/drive/My\Drive/colabData/imageclassify/My_Dataset.zip
Post this just drag and drop all classes to “original_dataset” or you can use command
as mentioned below Step5 so that folder structure should look like:
|——original dataset
|——class_name_0
|——class_name_1
|——class_name_2
|——class_name_3
Step 5 – Move files and folders
import os
os.chdir("/content/Resnet_Model_TF2.0/My_Dataset")
!mv -v "/content/Resnet_Model_TF2.0/My_Dataset/" "/content/Resnet_Model_TF2.0/original_dataset/"
import os
os.chdir("/content/Resnet_Model_TF2.0/")
Step 6 – Run split_dataset.py to split the raw dataset into train set, valid set and test set
!python split_dataset.py
Step 7 – Train the model on your own dataset
!python train.py
Step 8 – Update config parameters and the training model from the different Resnet models from config.py
Step 9 – Start the training by executing the file as mentioned below
!python train.py
Step 10 – Evaluate the accuracy by executing
!python evaluate.py