Borrar filtros
Borrar filtros

The format of data for a deep learning model CAE.

48 visualizaciones (últimos 30 días)
영광 박
영광 박 el 18 de Jul. de 2024 a las 3:17
Respondida: Sahas el 18 de Jul. de 2024 a las 11:08
I'm looking to create a deep learning CAE model. Inside the "trainnet" folder are JPEG files that I intend to use as input and output images for training the model. How should I format these images to trainnet for learning? For example, please explain the format and method for data within "trainnet(X)". Thank you.

Respuestas (1)

Sahas
Sahas el 18 de Jul. de 2024 a las 11:08
As per my understanding, you want to create a Convolutional Autoencoder model, and need guidance in formatting the images that you have and use “trainnet” function for training the network.
Please go through the provided sample script below which makes use of image formatting functions such as “imageDatastore”, “augmentedimageDatastore” various image formatting that MATLAB provides and in the end use the “trainnet” function to train the network. The “trainnet” function is supported in MATLAB R2023b onwards.
% Name of the folder where input images are stored
imageFolder = 'trainnet';
% Using imageDatastore to store and accessing the images
imds = imageDatastore(imageFolder, 'FileExtensions', '.jpg', 'LabelSource', 'none');
% Read the first image from imageDatastore and get it's metadata
sampleImage = readimage(imds, 1);
[imageHeight, imageWidth, numChannels] = size(sampleImage);
% Preprocess the images using "augmentedImageDatastore" if necessary
% Initialize an array to store the images
numImages = numel(imds.Files);
X = zeros(imageHeight, imageWidth, numChannels, numImages, 'single');
for i = 1:numImages
img = readimage(imds, i);
% Convert to single precision
img = im2single(img);
X(:, :, :, i) = img;
end
% Split the data into training and validation sets, if required
[trainInd, valInd] = dividerand(numImages, 0.8, 0.2);
XTrain = X(:, :, :, trainInd);
XVal = X(:, :, :, valInd);
% Define the CAE Layers according to your architechture
% NOTE: This is just a sample CAE architecture
layers = [
imageInputLayer([imageHeight, imageWidth, numChannels], 'Normalization', 'none')
convolution2dLayer(3, 16, 'Padding', 'same')
reluLayer
maxPooling2dLayer(2, 'Stride', 2)
convolution2dLayer(3, 8, 'Padding', 'same')
reluLayer
maxPooling2dLayer(2, 'Stride', 2)
transposedConv2dLayer(3, 8, 'Cropping', 'same')
reluLayer
transposedConv2dLayer(3, 16, 'Cropping', 'same')
reluLayer
transposedConv2dLayer(3, numChannels, 'Cropping', 'same')
regressionLayer];
% Specify training options
options = trainingOptions('adam', ...
'MaxEpochs', 50, ...
'InitialLearnRate', 1e-3, ...
'MiniBatchSize', 32, ...
'Shuffle', 'every-epoch', ...
'ValidationData', {XVal, XVal}, ...
'ValidationFrequency', 10, ...
'Verbose', false, ...
'Plots', 'training-progress');
% Train the CAE
net = trainnet(XTrain, XTrain, layers, options);
You can also use the examples of “trainAutoencoder” function in the below documentation link as an alternative to “trainnet” function:
Please see the below links for further understanding about various image formatting and preprocessing techniques MATLAB provides:
I hope this beneficial!

Categorías

Más información sobre Deep Learning for Image Processing en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by