0% found this document useful (0 votes)
8 views5 pages

ML Lab 3

This lab report discusses training a deep learning model for fruit classification using transfer learning with the Xception architecture in MATLAB. Task 1 involves loading the Xception model, preprocessing and splitting a fruit dataset, customizing the model layers for the task, setting training options, training on augmented data, and saving the trained model. Task 2 fine-tunes the pretrained Xception network on the dataset.

Uploaded by

Jahangir Awan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views5 pages

ML Lab 3

This lab report discusses training a deep learning model for fruit classification using transfer learning with the Xception architecture in MATLAB. Task 1 involves loading the Xception model, preprocessing and splitting a fruit dataset, customizing the model layers for the task, setting training options, training on augmented data, and saving the trained model. Task 2 fine-tunes the pretrained Xception network on the dataset.

Uploaded by

Jahangir Awan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Department of Computer Science HITEC

University, Taxila BS Computer Science


Program (Batch 2021)

CS-428 Introduction to Machine Learning


3(2+1)
Section B

Lab Report # 3
Muhammad Jahangir
21-cs-089
Basit Ali
21-cs-104
Instructor: Ms. Faiza Jahangir
Task 1
Write a MATLAB code that will load “xception” model and train your
desired dataset and get the trained file.

Steps
1 First download the Dataset form Kaggle

2 Resize All images to 299*299 by using following code


clc
close all
file=dir('C:\Users\Muhammad Jahangir\Desktop\Fruits
Classification\test\Strawberry\');
for i=3 : size(file,1)
s='C:\Users\Muhammad Jahangir\Desktop\Fruits
Classification\test\Strawberry\';
f=imread([s file(i).name]);
img=imresize(f, [299 299]);
imwrite(img,[file(i).name '.jpg']);
%f1{i,1}=f1;
end
3 Install the xception model and load in MATLAB

Code
clc;
close all;
clear;
datasetpath = fullfile("C:\Users\Muhammad Jahangir\Desktop\Fruits
Classification\test\");
imds = imageDatastore(datasetpath,...
'IncludeSubfolders',true,...
'LabelSource','foldernames');

labels = countEachLabel(imds);
num_images = length(imds.Files);
num_folds =2;

perm = randperm(num_images,6);

for fold_idx=1:length(perm)
subplot(2,3,fold_idx);
imshow(imread(imds.Files{perm(fold_idx)}));
title(sprintf("%s",imds.Labels(perm(fold_idx))));
end

for fold=1 : num_folds


train_idx = fold : num_folds : num_images;
train_img = subset(imds,train_idx);
train_label = countEachLabel(train_img);
test_idx = setdiff(1:length(imds.Files),train_idx);
test_img = subset(imds,test_idx);
test_lab = countEachLabel(test_img);
end
net = xception;
m_instance = layerGraph(net);
clear net;
number_of_classes = numel(categories(train_img.Labels));
new_fc_layer = fullyConnectedLayer(number_of_classes,'Name','Mfa',...
'WeightLearnRateFactor',10,...
'BiasLearnRateFactor',10);
m_instance = replaceLayer(m_instance,'predictions',new_fc_layer);
new_softmax_layer = softmaxLayer('name','softmax');
m_instance =
replaceLayer(m_instance,'predictions_softmax',new_softmax_layer);
newclasslayer = classificationLayer('Name','Newlayer');
m_instance =
replaceLayer(m_instance,'ClassificationLayer_predictions',newclasslaye
r);
options = trainingOptions('sgdm',...
'ExecutionEnvironment','auto',...
'MaxEpochs',3,'MiniBatchSize',8,...
'Shuffle','every-epoch', ...
'InitialLearnRate',0.0001, ...
'Verbose',false, ...
'Plots','training-progress',...
'LearnRateSchedule','piecewise');
augmenter = imageDataAugmenter( ...
'RandRotation',[-5 5],'RandXReflection',1,...
'RandYReflection',1,'RandXShear',[-0.05 0.05],'RandYShear',[-
0.05 0.05]);
auimds = augmentedImageDatastore([299
299],train_img,'DataAugmentation',...
augmenter,'ColorPreprocessing','none','OutputSizeMode','resize');
netTransfer = trainNetwork(auimds,m_instance,options);
augtestimds = augmentedImageDatastore([299 299],imdsTest);
fruits=netTransfer;
save ('fruits','fruits')
Result
Explanation
This MATLAB code trains a deep learning model for fruit classification using transfer learning
with the Xception architecture. Here's a brief summary of its main steps:

• It loads a dataset of fruit images and displays a random subset for visualization.
• The dataset is split into 2 folds for cross-validation.
• It customizes the Xception neural network by replacing its final classification layers with
new layers suitable for fruit classification.
• Training options are set, specifying optimization settings, data augmentation techniques,
and other parameters.
• An augmented image data store is created for training, applying data augmentation to the
training images.
• The custom Xception-based network is trained on the augmented data.
• The trained model is saved as 'fruits' for future use.

Task 2
Analyze network “Xception” and write a MATLAB code that will fine-
tuned your network.
new_fc_layer = fullyConnectedLayer(number_of_classes,'Name','Mfa',...
'WeightLearnRateFactor',10,...
'BiasLearnRateFactor',10);
m_instance = replaceLayer(m_instance,'predictions',new_fc_layer);

new_softmax_layer = softmaxLayer('name','softmax');
m_instance =
replaceLayer(m_instance,'predictions_softmax',new_softmax_layer);

newclasslayer = classificationLayer('Name','Newlayer');
m_instance =
replaceLayer(m_instance,'ClassificationLayer_predictions',newclasslaye
r);

Conclusion
This MATLAB code trains a deep learning model for fruit classification using transfer learning
with the Xception architecture. We customizes a pre-trained Xception network, and trains it
using augmented data, ultimately saving the trained model. We Fine-tuning the network on the
dataset using the specified training options.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy