VAE import custom data
Show older comments
I recently set up a VAE based on this link: https://au.mathworks.com/help/deeplearning/ug/train-a-variational-autoencoder-vae-to-generate-images.html
I was able to implement this line by line. That being said, I am trying to get my own custom data uploaded to the Ai for training.
I have 2 rar files, with color images (1024x1024 [will be resized]). The first file has a lot of images for training, and the second file has a few images for testing the algorithm.
The issue is, the VAE used in the link uses a function called "processImagesMNIST", I have looked into the function that makes this up, not sure on how I have to structure my training and testing data to meet the criteria that the function allows for.
Or is there another way to upload my own training data that the VAE can use, without using the special function?
function X = processImagesMNIST(filename)
% The MNIST processing functions extract the data from the downloaded IDX
% [What are IDX files???]
% files into MATLAB arrays. The processImagesMNIST function performs these
% operations: Check if the file can be opened correctly. Obtain the magic
% number by reading the first four bytes. The magic number is 2051 for
% image data, and 2049 for label data. Read the next 3 sets of 4 bytes,
% which return the number of images, the number of rows, and the number of
% columns. Read the image data. Reshape the array and swaps the first two
% dimensions due to the fact that the data was being read in column major
% format. Ensure the pixel values are in the range [0,1] by dividing them
% all by 255, and converts the 3-D array to a 4-D dlarray object. Close the
% file.
%[What do I need to do to make my dataset applicable for this function? Or is there another way to implement my own data?]
dataFolder = fullfile(tempdir,'mnist');
gunzip(filename,dataFolder)
[~,name,~] = fileparts(filename);
[fileID,errmsg] = fopen(fullfile(dataFolder,name),'r','b');
if fileID < 0
error(errmsg);
end
magicNum = fread(fileID,1,'int32',0,'b');
if magicNum == 2051
fprintf('\nRead MNIST image data...\n')
end
numImages = fread(fileID,1,'int32',0,'b');
fprintf('Number of images in the dataset: %6d ...\n',numImages);
numRows = fread(fileID,1,'int32',0,'b');
numCols = fread(fileID,1,'int32',0,'b');
X = fread(fileID,inf,'unsigned char');
X = reshape(X,numCols,numRows,numImages);
X = permute(X,[2 1 3]);
X = X./255;
X = reshape(X, [28,28,1,size(X,3)]);
fclose(fileID);
end
Answers (1)
Image Analyst
on 8 Feb 2023
I would just extract all your images to regular image files (like PNG images) and then instead of calling
X = processImagesMNIST(filename)
just use imread()
X = imread(filename)
18 Comments
Glacial Claw
on 9 Feb 2023
Image Analyst
on 9 Feb 2023
Extract them first with a normal unzipping program. I didn't delve into the algorithm but if you need a 4-D array then you can use cat(4, ......) to put your 3-D RGB images into a 4-D array or cat(3, ....) to put your 2-D gray scale images into a 3-D array.
Glacial Claw
on 11 Feb 2023
Glacial Claw
on 11 Feb 2023
Image Analyst
on 11 Feb 2023
You need to concatenate onto something. So try
filePattern = fullfile('VAE_redone\Ai training data\', '*.jpg');
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
[filepath,name,ext] = fileparts(filePattern);
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
fprintf('Now reading %s\n', fullFileName);
thisImage = imread(fullFileName);
if k == 1
Train_datastored = thisImage; % First image has nothing to be concatenated onto so just assign the first image.
else
% Concatenate onto existing 4-D array.
Train_datastored = cat(4, Train_datastored, thisImage);
end
end
XTrain = Train_datastored;
But I think the training functions can take a list of filenames and don't need a 4-D array of images. Also don't use "image" as the name of your variable because it is a built-in function.
Glacial Claw
on 12 Feb 2023
Image Analyst
on 12 Feb 2023
Then that means it never got into the loop. Set a breakpoint on this line:
if k == 1
and see if it stops there.
Glacial Claw
on 13 Feb 2023
Image Analyst
on 13 Feb 2023
Yes. Did execution stop there when you said to run? What is length(theFiles)? Are you sure that theFiles is not empty and length(theFIles) is 1 or greater?
Glacial Claw
on 14 Feb 2023
Image Analyst
on 14 Feb 2023
Not sure why you didn't answer my questions. So all I can assume is that theFiles is empty and thus never enters the loop so Train_datastored never gets assigned. Thus when you get to line 16 it throws the error.
I don't see how defining a class would help anything. I searched this page for classdef (the function that creates a class) and I don't see where you ever called it to define a class. I'm not sure where you think it was "previously mentioned above".
Glacial Claw
on 14 Feb 2023
Glacial Claw
on 14 Feb 2023
Image Analyst
on 15 Feb 2023
I can see that theFiles is empty. So it did not find any jpg files in the folder you told it to look in. Pick a different folder that actually has files in it.
Glacial Claw
on 15 Feb 2023
Image Analyst
on 15 Feb 2023
Not true. Look at the screenshot you posted. See in the workspace panel that theFiles is a 0x1 struct. That means there are zero files there. Check your folder name or file pattern to make sure every character is correct. If there are files there then theFiles will not have 0 for a size.
Glacial Claw
on 16 Feb 2023
Glacial Claw
on 17 Feb 2023
Edited: Glacial Claw
on 20 Feb 2023
Categories
Find more on Deep Learning Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


