MNIST手写数字二进制数据集的读取 发表于 2016-09-07 | 分类于 机器学习 | 官网如下:http://yann.lecun.com/exdb/mnist/ 首页详细地介绍了数据集的二进制格式,读取非常简单,要注意的是用大端模式,还有读取的图片的方向问题。 Matlab代码如下12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576clear ;close all;%% 读图像文件%使用大端模式fp_image=fopen('t10k-images.idx3-ubyte','rb','b');%头标识image_magic=fread(fp_image,1,'int');%图像数目image_num=fread(fp_image,1,'int');%图像宽度image_width=fread(fp_image,1,'int');%图像高度image_height=fread(fp_image,1,'int');fprintf('image file info:\nmagic:%d\nnumber:%d\nsize:%d*%d\n',image_magic,image_num,image_width,image_height);%% 读标签文件fp_label=fopen('t10k-labels.idx1-ubyte','rb','b');label_magic=fread(fp_label,1,'int');label_num=fread(fp_label,1,'int');fprintf('label file info:\nmagic:%d\nnumber:%d\n',label_magic,label_num);images_data=zeros(image_num,image_width,image_height);images_label=zeros(image_num,1,'int32');%% 顺序读N=min(image_num,label_num);for i =1:N % 先上下镜像,然后顺时针旋转90度 image=fread(fp_image,[image_width,image_height],'uchar'); image=flipud(image); image=rot90(image,-1); images_data(i,:,:)=image; images_label(i)=fread(fp_label,1,'uchar'); end%% 关闭文件fclose(fp_image);fclose(fp_label);%% 其他处理过程%% 创建文件夹,保存文件write_file=0;if(write_file) for i=0:9 dir=sprintf('D:/DP/%d',i); if(~exist(dir,'dir') ) mkdir(dir); end end images_count=zeros(10,1,'int32'); for i=1:N idx = images_label(i)+1; path = sprintf('D:/DP/%d/%d.jpg',images_label(i),images_count(idx)); out=squeeze(images_data(i,:,:)); imwrite(out,path); images_count(idx)=images_count(idx)+1; endelse end 本地保存结果(例数字6文件夹下的图片) 坚持原创技术分享,您的支持将鼓励我继续创作! 赏 微信打赏 支付宝打赏