显示mnist手写数字
生活随笔
收集整理的這篇文章主要介紹了
显示mnist手写数字
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
前言
可視化什么的,總是好的
方法一
其實也就是用到了Ruslan Salakhutdinov and Geoff Hinton提供的工具包
% Version 1.000 % % Code provided by Ruslan Salakhutdinov and Geoff Hinton % % Permission is granted for anyone to copy, use, modify, or distribute this % program and accompanying programs and documents for any purpose, provided % this copyright notice is retained and prominently displayed, along with % a note saying that the original programs are available from our % web page. % The programs and documents are distributed without any warranty, express or % implied. As the programs were written for research purposes only, they have % not been tested to the degree that would be advisable in any important % application. All use of these programs is entirely at the user's own risk.% This program reads raw MNIST files available at % http://yann.lecun.com/exdb/mnist/ % and converts them to files in matlab format % Before using this program you first need to download files: % train-images-idx3-ubyte.gz train-labels-idx1-ubyte.gz % t10k-images-idx3-ubyte.gz t10k-labels-idx1-ubyte.gz % and gunzip them. You need to allocate some space for this. % This program was originally written by Yee Whye Teh % Work with test files first fprintf(1,'You first need to download files:\n train-images-idx3-ubyte.gz\n train-labels-idx1-ubyte.gz\n t10k-images-idx3-ubyte.gz\n t10k-labels-idx1-ubyte.gz\n from http://yann.lecun.com/exdb/mnist/\n and gunzip them \n'); gunzip train-images-idx3-ubyte.gz gunzip train-labels-idx1-ubyte.gz gunzip t10k-images-idx3-ubyte.gz gunzip t10k-labels-idx1-ubyte.gz f = fopen('t10k-images-idx3-ubyte','r'); [a,count] = fread(f,4,'int32');g = fopen('t10k-labels-idx1-ubyte','r'); [l,count] = fread(g,2,'int32');fprintf(1,'Starting to convert Test MNIST images (prints 10 dots) \n'); n = 1000;Df = cell(1,10); for d=0:9,Df{d+1} = fopen(['test' num2str(d) '.ascii'],'w'); end;for i=1:10,%讀了10次fprintf('.');rawimages = fread(f,28*28*n,'uchar');rawlabels = fread(g,n,'uchar');rawimages = reshape(rawimages,28*28,n);%每一列就是一張圖片,總共1000張for j=1:n,fprintf(Df{rawlabels(j)+1},'%3d ',rawimages(:,j));%把每一行讀取進入Df{}文件中去,每一個標簽對應每一類圖片fprintf(Df{rawlabels(j)+1},'\n'); %讀取一列換一行end; end;fprintf(1,'\n'); for d=0:9,fclose(Df{d+1});D = load(['test' num2str(d) '.ascii'],'-ascii');fprintf('%5d Digits of class %d\n',size(D,1),d);save(['test' num2str(d) '.mat'],'D','-mat'); end;% Work with trainig files second f = fopen('train-images-idx3-ubyte','r'); [a,count] = fread(f,4,'int32');g = fopen('train-labels-idx1-ubyte','r'); [l,count] = fread(g,2,'int32');fprintf(1,'Starting to convert Training MNIST images (prints 60 dots)\n'); n = 1000;Df = cell(1,10); for d=0:9,Df{d+1} = fopen(['digit' num2str(d) '.ascii'],'w'); end;for i=1:60,fprintf('.');rawimages = fread(f,28*28*n,'uchar');rawlabels = fread(g,n,'uchar');rawimages = reshape(rawimages,28*28,n);for j=1:n,fprintf(Df{rawlabels(j)+1},'%3d ',rawimages(:,j));fprintf(Df{rawlabels(j)+1},'\n');end; end;fprintf(1,'\n'); for d=0:9,fclose(Df{d+1});D = load(['digit' num2str(d) '.ascii'],'-ascii');fprintf('%5d Digits of class %d\n',size(D,1),d);save(['digit' num2str(d) '.mat'],'D','-mat'); end;%dos('rm *.ascii');%這個刪除命令不好用 dos('del *.ascii')%我們用這個刪除命令mnist壓縮包下載:鏈接:http://pan.baidu.com/s/1qXA3u9m 密碼:2gza
顯示的時候很簡單,上面代碼運行以后生成了這幾個文件:
隨便打開一個,得到一個名稱為D的二維矩陣,然后采用如下代碼就可顯示
?imshow(reshape(D(1,:),28,28))
就可以得到顯示
方法二
也是比較常見的一個代碼,包含分別提取圖像和標簽的代碼,和上述實現差不多,但是封裝到函數看起來更加方便
讀取圖片的代碼:loadMNISTImages.m
讀取標簽的代碼:loadMNISTLabels.m
function labels = loadMNISTLabels(filename) %loadMNISTLabels returns a [number of MNIST images]x1 matrix containing %the labels for the MNIST imagesfp = 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);end使用方法 data = loadMNISTImages('train-images-idx3-ubyte')'; labels = loadMNISTLabels('train-labels-idx1-ubyte'); image=reshape(data(1,:),28,28); imshow(image)%顯示圖片 labels(1,1)%查看標簽
總結
以上是生活随笔為你收集整理的显示mnist手写数字的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 维信卡卡贷只能还信用卡吗?这些事项要注意
- 下一篇: 信用卡还款日当天可以刷卡吗?还款日前一天