DownloadCIFAR10.m error while I try to prepare the CIFAR10 dataset.
9 views (last 30 days)
Show older comments
Nasir Sinani
on 22 Dec 2018
Commented: Walter Roberson
on 2 May 2020
I tried to create a new script called DownloadCIFAR10.m and copied a code from: https://www.mathworks.com/matlabcentral/mlc-downloads/downloads/submissions/62990/versions/3/previews/DeepLearningDemos/DownloadCIFAR10.m/index.html as shown below:
%% Download the CIFAR-10 dataset
if ~exist('cifar-10-batches-mat','dir')
cifar10Dataset = 'cifar-10-matlab';
disp('Downloading 174MB CIFAR-10 dataset...');
websave([cifar10Dataset,'.tar.gz'],...
['https://www.cs.toronto.edu/~kriz/',cifar10Dataset,'.tar.gz']);
gunzip([cifar10Dataset,'.tar.gz'])
delete([cifar10Dataset,'.tar.gz'])
untar([cifar10Dataset,'.tar'])
delete([cifar10Dataset,'.tar'])
end
%% Prepare the CIFAR-10 dataset
if ~exist('cifar10Train','dir')
disp('Saving the Images in folders. This might take some time...');
saveCIFAR10AsFolderOfImages('cifar-10-batches-mat', pwd, true);
end
I can download the dataset but when I try to prepare it I get this error: Undefined function or variable 'saveCIFAR10AsFolderOfImages'.
Please help
Thanks in advance....
0 Comments
Accepted Answer
Walter Roberson
on 22 Dec 2018
5 Comments
Akhil Anchan
on 1 May 2020
Where do I implement the saveCIFARcode in this program?
%% Download the CIFAR-10 dataset
if ~exist('cifar-10-batches-mat','dir')
cifar10Dataset = 'cifar-10-matlab';
disp('Downloading 174MB CIFAR-10 dataset...');
websave([cifar10Dataset,'.tar.gz'],...
['https://www.cs.toronto.edu/~kriz/',cifar10Dataset,'.tar.gz']);
gunzip([cifar10Dataset,'.tar.gz'])
delete([cifar10Dataset,'.tar.gz'])
untar([cifar10Dataset,'.tar'])
delete([cifar10Dataset,'.tar'])
end
function saveCIFAR10AsFolderOfImages(inputPath, outputPath, varargin)
% saveCIFAR10AsFolderOfImages Save the CIFAR-10 dataset as a folder of images
% saveCIFAR10AsFolderOfImages(inputPath, outputPath) takes the CIFAR-10
% dataset located at inputPath and saves it as a folder of images to the
% directory outputPath. If inputPath or outputPath is an empty string, it
% is assumed that the current folder should be used.
%
% saveCIFAR10AsFolderOfImages(..., labelDirectories) will save the
% CIFAR-10 data so that instances with the same label will be saved to
% sub-directories with the name of that label.
% Check input directories are valid
if(~isempty(inputPath))
assert(exist(inputPath,'dir') == 7);
end
if(~isempty(outputPath))
assert(exist(outputPath,'dir') == 7);
end
% Check if we want to save each set with the same labels to its own
% directory.
if(isempty(varargin))
labelDirectories = false;
else
assert(nargin == 3);
labelDirectories = varargin{1};
end
% Set names for directories
trainDirectoryName = 'cifar10Train';
testDirectoryName = 'cifar10Test';
% Create directories for the output
mkdir(fullfile(outputPath, trainDirectoryName));
mkdir(fullfile(outputPath, testDirectoryName));
if(labelDirectories)
labelNames = {'airplane','automobile','bird','cat','deer','dog','frog','horse','ship','truck'};
iMakeTheseDirectories(fullfile(outputPath, trainDirectoryName), labelNames);
iMakeTheseDirectories(fullfile(outputPath, testDirectoryName), labelNames);
for i = 1:5
iLoadBatchAndWriteAsImagesToLabelFolders(fullfile(inputPath,['data_batch_' num2str(i) '.mat']), fullfile(outputPath, trainDirectoryName), labelNames, (i-1)*10000);
end
iLoadBatchAndWriteAsImagesToLabelFolders(fullfile(inputPath,'test_batch.mat'), fullfile(outputPath, testDirectoryName), labelNames, 0);
else
for i = 1:5
iLoadBatchAndWriteAsImages(fullfile(inputPath,['data_batch_' num2str(i) '.mat']), fullfile(outputPath, trainDirectoryName), (i-1)*10000);
end
iLoadBatchAndWriteAsImages(fullfile(inputPath,'test_batch.mat'), fullfile(outputPath, testDirectoryName), 0);
end
end
function iLoadBatchAndWriteAsImagesToLabelFolders(fullInputBatchPath, fullOutputDirectoryPath, labelNames, nameIndexOffset)
load(fullInputBatchPath);
data = data'; %#ok<NODEF>
data = reshape(data, 32,32,3,[]);
data = permute(data, [2 1 3 4]);
for i = 1:size(data,4)
imwrite(data(:,:,:,i), fullfile(fullOutputDirectoryPath, labelNames{labels(i)+1}, ['image' num2str(i + nameIndexOffset) '.png']));
end
end
function iLoadBatchAndWriteAsImages(fullInputBatchPath, fullOutputDirectoryPath, nameIndexOffset)
load(fullInputBatchPath);
data = data'; %#ok<NODEF>
data = reshape(data, 32,32,3,[]);
data = permute(data, [2 1 3 4]);
for i = 1:size(data,4)
imwrite(data(:,:,:,i), fullfile(fullOutputDirectoryPath, ['image' num2str(i + nameIndexOffset) '.png']));
end
end
function iMakeTheseDirectories(outputPath, directoryNames)
for i = 1:numel(directoryNames)
mkdir(fullfile(outputPath, directoryNames{i}));
end
end
%% Prepare the CIFAR-10 dataset
if ~exist('cifar10Train','dir')
disp('Saving the Images in folders. This might take some time...');
saveCIFAR10AsFolderOfImages('cifar-10-batches-mat', pwd, true);
end
Walter Roberson
on 2 May 2020
Grab the file I linked to and store it as saveCIFAR10AsFolderOfImages.m in the same directory that you have the other code.
More Answers (0)
See Also
Categories
Find more on Point Cloud Processing 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!