The format of data for a deep learning model CAE.
1 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(1 个)
Sahas
2024-7-18
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:
https://www.mathworks.com/help/matlab/ref/matlab.io.datastore.imagedatastore.html -- To manage a collection of data files
https://www.mathworks.com/help/deeplearning/ref/augmentedimagedatastore.html -- To preprocess the stored data files
I hope this beneficial!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Data Workflows 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!