top of page

INTRODUCTION TO RESNET

Updated: Aug 18, 2022

INTRODUCTION

There was a time when one of the popular approaches, in computer vision, was to make deeper layers in order to archive the improvement. But with this approach comes some obstacles. Because of making deeper layers, the accuracy became saturated and degraded rapidly.


ResNet, which stands for Residual Network, comes with a solution to this problem. It was introduced by Shaoqing Ren, Kaiming He, Jian Sun, and Xiangyu Zhang.


To tackle the problem of training on deeper models, the ResNet uses Residual blocks.


In ResNet, the neural networks are feedforward with shortcut connections, and each shortcut connection forms a residual block. Shortcut connections are the skipping layers in the neural network. Here the shortcut connections perform identity mapping, and they are added to the stacked layers. And because of these, the ResNet, instead of learning low/mid/high level features, learns the residuals by using shortcut connections.


ARCHITECTURE OF RESNET


In ResNet, the ConvNet uses filters of size (3x3), and it follows two simple design rules:


(i) The layers should have the same number of filters for the same output feature map size; and (ii) if the feature map size is halved, the number of filters is doubled so as to preserve the time complexity per layer.


The downsampling is performed directly by convolution layers that have a strided of two. The network end layers consist of a global average pooling layer and 1000 fully-connected layers using softmax activation.


IMPLEMENTATION

Import the essential libraries


import keras
import numpy as np
from tensorflow.keras.applications import ResNet50

from keras.preprocessing import image
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.applications.vgg16 import preprocess_input
from keras.applications.vgg16 import decode_predictions
from keras.utils.vis_utils import plot_model

Upload the weights to the RESNET model

resnet_model = ResNet50(weights='imagenet')

Plot the architecture of RESNET model

plot_model(resnet_model, to_file='resnet_model.png')

Get the information about the structure of RESNET model,

print(resnet_model.summary())

Load the sample image, I used an image of snow bear. You can download the image using this link: https://unsplash.com/photos/qQWV91TTBrE

sample_image = load_img('/content/snow_bear.jpg', target_size=(224, 224))
# convert the image to numpy array
sample_image = img_to_array(sample_image)

# reshape the image for the model
sample_image = sample_image.reshape((1, sample_image.shape[0], sample_image.shape[1], sample_image.shape[2]))

# preprocessing the image
sample_image = preprocess_input(sample_image)

# predicting the probability of the image belong to a particular class
y_pred = resnet_model.predict(sample_image)

# converting the probability to class label
label = decode_predictions(y_pred)

# retrieve the result with highest probability
label = label[0][0]

# classification result
print('%s (%.2f%%)' % (label[1], label[2]*100))

RESULT

ice_bear (99.99%)
If you need implementation for any of the topics mentioned above or assignment help on any of its variants, feel free to contact us.

Commentaires


bottom of page