图像转化为手写体mnist格式的数据

mnist就是一种二进制格式,这里采用matlab代码将图像转化

1、首先将图像转化二进制

filename='train-images-idx3-ubyte';
fp = fopen(filename, 'rb');
assert(fp ~= -1, ['Could not open ', filename, '']);

magic = fread(fp, 1, 'int32', 0, 'ieee-be');
assert(magic == 2051, ['Bad magic number in ', filename, '']);

numImages = fread(fp, 1, 'int32', 0, 'ieee-be');
numRows = fread(fp, 1, 'int32', 0, 'ieee-be');
numCols = fread(fp, 1, 'int32', 0, 'ieee-be');

images = fread(fp, inf, 'unsigned char');
images = reshape(images, numCols, numRows, numImages);
images = permute(images,[2 1 3]);

fclose(fp);
%%%%
f1 = fopen('test_mnist.bin', 'wb');
fwrite(f1,magic,'int32', 0, 'ieee-be');
fwrite(f1,numImages,'int32', 0, 'ieee-be');
fwrite(f1,numRows,'int32', 0, 'ieee-be');
fwrite(f1,numRows,'int32', 0, 'ieee-be');
images = permute(images,[2 1 3]);
images=reshape(images, numCols*numRows*numImages,1);
fwrite(f1,images,'unsigned char');
fclose(f1);




2、将label转化为二进制

filename='train-labels-idx1-ubyte';
fp = fopen(filename, 'rb');
assert(fp ~= -1, ['Could not open ', filename, '']);

magic = fread(fp, 1, 'int32', 0, 'ieee-be');
assert(magic == 2049, ['Bad magic number in ', filename, '']);

numLabels = fread(fp, 1, 'int32', 0, 'ieee-be');

labels = fread(fp, inf, 'unsigned char');

assert(size(labels,1) == numLabels, 'Mismatch in label count');

fclose(fp);
f2=fopen('test_mnist_lable.bin', 'wb');
fwrite(f2,magic,'int32', 0, 'ieee-be');
fwrite(f2,numLabels,'int32', 0, 'ieee-be');
fwrite(f2,labels,'unsigned char');
fclose(f2);











发布了34 篇原创文章 · 获赞 10 · 访问量 16万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览