I'm getting this error when I tried to select a custom number of images for training and validation "Index in position 3 exceeds array bounds (must not exceed 1)."
조회 수: 1 (최근 30일)
이전 댓글 표시
%% Inicializes the IDE enviornement
clc
clear
close all
%% Load the Image data
% Images
trainImagesFile = "train-images-idx3-ubyte.gz";
testImagesFile = "t10k-images-idx3-ubyte.gz";
img_T = LoadImageData(trainImagesFile);
img_V = LoadImageData(testImagesFile);
%Labels
trainLabelFile = "train-labels-idx1-ubyte.gz";
testLabelFile = "t10k-labels-idx1-ubyte.gz";
labels_T = LoadLabelData(trainLabelFile);
labels_V = LoadLabelData(testLabelFile);
% Shows 40.000 images
% figure
% montage(img_T(:,:,1:100))
% figure
% montage(img_T(:,:,1:60000))
labels_T=labels_T(:,:,1:30000);
labels_V=labels_V(:,:,1:6000);
img_T=img_T(:,:,1:30000);
img_V=img_V(:,:,1:6000);
%% Saves a MAT file for further processing
% Adds the channel dimention
img_T = reshape(img_T,[28,28,1,30000]);
img_V = reshape(img_V,[28,28,1,6000]);
% Transforms to the correct type for classification
labels_T = categorical(labels_T);
labels_V = categorical(labels_V);
save 'MNIST_Train.mat' img_T labels_T
save 'MNIST_Validation.mat' img_V labels_V
% Loads a compressed IDX3 file with image data
function X = LoadImageData(filename)
dataFolder = fullfile(tempdir,'mnist');
gunzip(filename,dataFolder);
[~,name,~] = fileparts(filename);
[fileID,errmsg] = fopen(fullfile(dataFolder,name),'r','b');
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;
fclose(fileID);
end
% Loads a compressed IDX1 file with label data
function labels = LoadLabelData(filename)
dataFolder = fullfile(tempdir,'mnist3');
gunzip(filename,dataFolder);
[~,name,~] = fileparts(filename);
[fid,errmsg] = fopen(fullfile(dataFolder,name),'r','b');
% Read digit labels
header = fread(fid, 1, 'int32');
if header ~= 2049
error('Invalid label file header');
end
count = fread(fid, 1, 'int32');
labels = fread(fid, count, 'uint8');
fclose(fid);
end
You need mnist database
댓글 수: 0
답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!