내가 링크어떻게하면 MATLAB에서 MNIST 숫자 및 레이블 데이터를로드 할 수 있습니까?
https://github.com/bd622/DiscretHashing
이산 해싱에 주어진 코드를 실행하려고하면 대략 가까운 이웃 검색에 사용되는 차원 축소 방법이다. http://yann.lecun.com/exdb/mnist/에서 사용할 수있는 MNIST 데이터베이스의 구현에로드하려고합니다. 압축 된 gz 형식의 파일을 추출했습니다. MNIST 데이터베이스가 나는 다음과 같은 오류가 무엇입니까 Reading MNIST Image Database binary file in MATLAB
에서 제공 읽을 수있는 솔루션을 사용
:
문제 1
clear all;
close all;
%//Open file
fid = fopen('t10k-images-idx3-ubyte', 'r');
A = fread(fid, 1, 'uint32');
magicNumber = swapbytes(uint32(A));
%//For each image, store into an individual cell
imageCellArray = cell(1, totalImages);
for k = 1 : totalImages
%//Read in numRows*numCols pixels at a time
A = fread(fid, numRows*numCols, 'uint8');
%//Reshape so that it becomes a matrix
%//We are actually reading this in column major format
%//so we need to transpose this at the end
imageCellArray{k} = reshape(uint8(A), numCols, numRows)';
end
%//Close the file
fclose(fid);
다음은 Error using fread
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in Reading (line 7)
A = fread(fid, 1, 'uint32');
코드입니다
업데이트 : 문제 하나 해결하고 수정 된 코드는
clear all;
close all;
%//Open file
fid = fopen('t10k-images.idx3-ubyte', 'r');
A = fread(fid, 1, 'uint32');
magicNumber = swapbytes(uint32(A));
%//Read in total number of images
%//A = fread(fid, 4, 'uint8');
%//totalImages = sum(bitshift(A', [24 16 8 0]));
%//OR
A = fread(fid, 1, 'uint32');
totalImages = swapbytes(uint32(A));
%//Read in number of rows
%//A = fread(fid, 4, 'uint8');
%//numRows = sum(bitshift(A', [24 16 8 0]));
%//OR
A = fread(fid, 1, 'uint32');
numRows = swapbytes(uint32(A));
%//Read in number of columns
%//A = fread(fid, 4, 'uint8');
%//numCols = sum(bitshift(A', [24 16 8 0]));
%// OR
A = fread(fid, 1, 'uint32');
numCols = swapbytes(uint32(A));
for k = 1 : totalImages
%//Read in numRows*numCols pixels at a time
A = fread(fid, numRows*numCols, 'uint8');
%//Reshape so that it becomes a matrix
%//We are actually reading this in column major format
%//so we need to transpose this at the end
imageCellArray{k} = reshape(uint8(A), numCols, numRows)';
end
%//Close the file
fclose(fid);
문제 2 : 내가 코드에 MNIST의 4 개 파일을 적용하는 방법을 이해할 수 없다
. 코드에 변수가 포함되어 있습니다.
traindata = double(traindata);
testdata = double(testdata);
구현에 적용 할 수 있도록 MNIST 데이터베이스를 어떻게 준비합니까?
UPDATE : 나는 솔루션을 구현하지만 이러한 파일
demo.m
퍼센트이가 MNIST 데이터에 읽을 수있는 함수를 호출하는 주요 파일입니다이 오류
Error using fread
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in mnist_parse (line 11)
A = fread(fid1, 1, 'uint32');
가 계속
clear all
clc
[Trainimages, Trainlabels] = mnist_parse('C:\Users\Desktop\MNIST\train-images-idx3-ubyte', 'C:\Users\Desktop\MNIST\train-labels-idx1-ubyte');
[Testimages, Testlabels] = mnist_parse('t10k-images-idx3-ubyte', 't10k-labels-idx1-ubyte');
k=5;
digit = images(:,:,k);
lbl = label(k);
function [images, labels] = mnist_parse(path_to_digits, path_to_labels)
% Open files
fid1 = fopen(path_to_digits, 'r');
% The labels file
fid2 = fopen(path_to_labels, 'r');
% Read in magic numbers for both files
A = fread(fid1, 1, 'uint32');
magicNumber1 = swapbytes(uint32(A)); % Should be 2051
fprintf('Magic Number - Images: %d\n', magicNumber1);
A = fread(fid2, 1, 'uint32');
magicNumber2 = swapbytes(uint32(A)); % Should be 2049
fprintf('Magic Number - Labels: %d\n', magicNumber2);
% Read in total number of images
% Ensure that this number matches with the labels file
A = fread(fid1, 1, 'uint32');
totalImages = swapbytes(uint32(A));
A = fread(fid2, 1, 'uint32');
if totalImages ~= swapbytes(uint32(A))
error('Total number of images read from images and labels files are not the same');
end
fprintf('Total number of images: %d\n', totalImages);
% Read in number of rows
A = fread(fid1, 1, 'uint32');
numRows = swapbytes(uint32(A));
% Read in number of columns
A = fread(fid1, 1, 'uint32');
numCols = swapbytes(uint32(A));
fprintf('Dimensions of each digit: %d x %d\n', numRows, numCols);
% For each image, store into an individual slice
images = zeros(numRows, numCols, totalImages, 'uint8');
for k = 1 : totalImages
% Read in numRows*numCols pixels at a time
A = fread(fid1, numRows*numCols, 'uint8');
% Reshape so that it becomes a matrix
% We are actually reading this in column major format
% so we need to transpose this at the end
images(:,:,k) = reshape(uint8(A), numCols, numRows).';
end
% Read in the labels
labels = fread(fid2, totalImages, 'uint8');
% Close the files
fclose(fid1);
fclose(fid2);
end
오류는 자명하지만, 잘못된 파일 이름에'fopen'을 사용했습니다. ''t10k-images-idx3-ubyte''가 파일의 * 전체 * 이름이고 현재 MATLAB 경로에 있는지 확인하십시오. 그렇지 않으면 열려고하는 파일의 * 절대 * 절대 경로인지 확인하십시오. – excaza
@excaza : 파일 읽기 작업으로 인한 첫 번째 문제와 오류가 해결되었습니다. 실제로 파일 이름에 문제가있었습니다. 하지만 지금은 데이터베이스를 사용하는 방법에 대한 단서가 없으며 4 개의 파일을 사용하는 방법을 이해할 수 없습니다. traindata 변수에 train-images.idx3-ubyte 파일이 포함되어 있다고 생각합니다. 그러면 어느 것이 testdata가 될 것인가? 그리고 어떻게 2 레이블 데이터베이스 파일을 사용해야합니까? 도와주세요 – SKM
@rayryeng : 답변을 구현할 때 파일 읽기 작업으로 인해 오류가 발생하는 이유를 알려주십시오. 질문에 새 업데이트를 올렸습니다. 시간과 노력에 감사드립니다. – SKM