Fine-tuning in machine learning deep neural network
Fine-Tuning we optimize both the weights of the new classification layers we have added, as well as some or all of the layers from the model.
Here we will learn how to implement the "Hot Dog Recognition".
First import all the libraries related to the this:
#import the libraries
%matplotlib inline
import d2l
from mxnet import gluon, init, np, npx
from mxnet.gluon import nn
npx.set_np()
Obtaining the Dataset
This dataset contains 1,4001,400 positive images containing hot dogs and the same number of negative images containing other foods. 1,0001,000 images of various classes are used for training and the rest are used for testing.
Download data set
# Saved in the d2l package for later use
d2l.DATA_HUB['hotdog'] = (d2l.DATA_URL+'hotdog.zip',
'fba480ffa8aa7e0febbb511d181409f899b9baa5')
data_dir = d2l.download_extract('hotdog')
Now reading the train and test dataset using - ImageFolderDataset
#reading the train and test dataset
train_imgs = gluon.data.vision.ImageFolderDataset(data_dir + 'train')
test_imgs = gluon.data.vision.ImageFolderDataset(data_dir + 'test')
Here 8 positive and 8 negative images are here:
#reading the positive and negative images
hotdogs = [train_imgs[i][0] for i in range(8)]
not_hotdogs = [train_imgs[-i - 1][0] for i in range(8)]
d2l.show_images(hotdogs + not_hotdogs, 2, 8, scale=1.4);
Output:
After this normalizing the image as per given pixel
#normalizing the images
normalize = gluon.data.vision.transforms.Normalize(
[0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
train_augs = gluon.data.vision.transforms.Compose([
gluon.data.vision.transforms.RandomResizedCrop(224),
gluon.data.vision.transforms.RandomFlipLeftRight(),
gluon.data.vision.transforms.ToTensor(),
normalize])
test_augs = gluon.data.vision.transforms.Compose([
gluon.data.vision.transforms.Resize(256),
gluon.data.vision.transforms.CenterCrop(224),
gluon.data.vision.transforms.ToTensor(),
normalize])
Defining and Initializing the Model
Here we are using the pre-trained ResNet-18
Here, we specify pretrained=True to automatically download and load the pre-trained model parameters.
#defining and intializing the model
pretrained_net = gluon.model_zoo.vision.resnet18_v2(pretrained=True)
Finding the output:
pretrained_net.output
Get your project or assignment completed by Deep learning expert and experienced developers and researchers.
OR
If you have project files, You can send at codersarts@gmail.com directly