-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcifar_dataset.m
More file actions
67 lines (54 loc) · 2.15 KB
/
cifar_dataset.m
File metadata and controls
67 lines (54 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
%% Download and Load CIFAR-10 Data
clear;
url = 'https://www.cs.toronto.edu/~kriz/cifar-10-matlab.tar.gz';
downloadFolder = tempdir;
filename = fullfile(downloadFolder,'cifar-10-matlab.tar.gz');
dataFolder = fullfile(downloadFolder,'cifar-10-batches-mat');
if ~exist(dataFolder,'dir')
fprintf("Downloading CIFAR-10 dataset (175 MB)... ");
websave(filename,url);
untar(filename,downloadFolder);
fprintf("Done.\n")
end
%% Load CIFAR-10 Data
[XTrain,YTrain,XValidation,YValidation] = loadCIFARData(downloadFolder);
trainLabels = YTrain;
testLabels = YValidation;
trainData = zeros(size(XTrain,1),size(XTrain, 2), 1, size(XTrain, 4));
testData = zeros(size(XValidation,1),size(XValidation, 2), 1, size(XValidation, 4));
%% Convert to Grayscale
% convert training images to grayscale
for i = 1:size(XTrain, 4)
trainData(:,:,1,i) = rgb2gray(XTrain(:,:,:,i));
end
% convert test images to grayscale
for i = 1:size(XValidation, 4)
testData(:,:,1,i) = rgb2gray(XValidation(:,:,:,i));
end
%% Reshape the Data
trainData = squeeze(trainData);
testData = squeeze(testData);
trainData = orzReshape(trainData, 1);
testData = orzReshape(testData, 1);
%% Convert to numerical labels
testLabels = convertToNumericalLabels(testLabels);
trainLabels = convertToNumericalLabels(trainLabels);
%% Convert to Matlab Subspace Format
trainData = convertDatasetToMatlabSubspaceFormat(trainData, trainLabels);
testData = convertDatasetToMatlabSubspaceFormat(testData, testLabels);
%% Display images
imageSize = 32;
data = reshape(trainData, imageSize, imageSize, size(trainData, 2), size(trainData, 3));
figure; % Create a new figure window
for i = 1:10 % Loop through each of the 10 classes
% random integer, used to display random image
idx = randi([1, 100]);
img = squeeze(data(:,:,idx,i)); % Extract the first 16x16 image of the i-th class
subplot(2, 5, i); % Arrange the plots in a 2x5 grid
imagesc(img); % Display the image
axis square; % Make each subplot square in shape
colormap gray; % Use grayscale colors
title(sprintf('Class %d', i)); % Title each subplot with its class number
end
%% Save the dataset
save('cifar10.mat', 'trainData', 'testData');