Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
1.7 kB
2
Indexable
Never
% Step 1: Training Script for Two Objects
clear;
clc;

% Load MobileNetV2
net = mobilenetv2;

% Data Directory
dataDir1 = fullfile(pwd, 'face1'); % Assuming data for face 1 is stored in a folder named 'face1'
dataDir2 = fullfile(pwd, 'face2'); % Assuming data for face 2 is stored in a folder named 'face2'

imds1 = imageDatastore(dataDir1, 'IncludeSubfolders', true, 'LabelSource', 'foldernames');
imds2 = imageDatastore(dataDir2, 'IncludeSubfolders', true, 'LabelSource', 'foldernames');

% Combine data from both objects
imds = imageDatastore(cat(1, imds1.Files, imds2.Files), 'IncludeSubfolders', true, 'LabelSource', 'foldernames');

% Split data into training and validation sets (80-20 split)
[imdsTrain, imdsValidation] = splitEachLabel(imds, 0.8, 'randomized');

% Data Augmentation
augmenter = imageDataAugmenter( ...
    'RandXReflection', true, ...
    'RandYReflection', true, ...
    'RandRotation', [-10 10]);

% Preprocess Images
inputSize = net.Layers(1).InputSize;
augmentedTrainingData = augmentedImageDatastore(inputSize(1:2), imdsTrain, 'DataAugmentation', augmenter);
augmentedValidationData = augmentedImageDatastore(inputSize(1:2), imdsValidation);

% Options for training
options = trainingOptions('sgdm', ...
    'MiniBatchSize', 32, ...
    'MaxEpochs', 10, ...
    'InitialLearnRate', 1e-4, ...
    'ValidationData', augmentedValidationData, ...
    'ValidationFrequency', 10, ...
    'Verbose', true, ...
    'Plots', 'training-progress');

% Train the network
net = trainNetwork(augmentedTrainingData, net, options);

% Save the trained model
save('faceRecognitionModel_2objects.mat', 'net');
Leave a Comment