手把手教你用matlab做深度學習

首先,給出下載數據方法(這裏編譯環境是matlab2018a):

1.下載 CIFAR-10 圖片數據

cifar10Data = tempdir;

url = 'https://www.cs.toronto.edu/~kriz/cifar-10-matlab.tar.gz';

helperCIFAR10Data.download(url,cifar10Data);

這裏給出了helperCIFAR10Data文件內容:

% This is helper class to download and import the CIFAR-10 dataset. The
% dataset is downloaded from:
%
%  https://www.cs.toronto.edu/~kriz/cifar-10-matlab.tar.gz
%
% References
% ----------
% Krizhevsky, Alex, and Geoffrey Hinton. "Learning multiple layers of
% features from tiny images." (2009).

classdef helperCIFAR10Data
    
    methods(Static)
        
        %------------------------------------------------------------------
        function download(url, destination)
            if nargin == 1
                url = 'https://www.cs.toronto.edu/~kriz/cifar-10-matlab.tar.gz';
            end        
            
            unpackedData = fullfile(destination, 'cifar-10-batches-mat');
            if ~exist(unpackedData, 'dir')
                fprintf('Downloading CIFAR-10 dataset...');     
                untar(url, destination); 
                fprintf('done.\n\n');
            end
        end
        
        %------------------------------------------------------------------
        % Return CIFAR-10 Training and Test data.
        function [XTrain, TTrain, XTest, TTest] = load(dataLocation)         
            
            location = fullfile(dataLocation, 'cifar-10-batches-mat');
            
            [XTrain1, TTrain1] = loadBatchAsFourDimensionalArray(location, 'data_batch_1.mat');
            [XTrain2, TTrain2] = loadBatchAsFourDimensionalArray(location, 'data_batch_2.mat');
            [XTrain3, TTrain3] = loadBatchAsFourDimensionalArray(location, 'data_batch_3.mat');
            [XTrain4, TTrain4] = loadBatchAsFourDimensionalArray(location, 'data_batch_4.mat');
            [XTrain5, TTrain5] = loadBatchAsFourDimensionalArray(location, 'data_batch_5.mat');
            
            XTrain = cat(4, XTrain1, XTrain2, XTrain3, XTrain4, XTrain5);
            TTrain = [TTrain1; TTrain2; TTrain3; TTrain4; TTrain5];
            
            [XTest, TTest] = loadBatchAsFourDimensionalArray(location, 'test_batch.mat');
                      
        end
    end
end

function [XBatch, TBatch] = loadBatchAsFourDimensionalArray(location, batchFileName)
load(fullfile(location,batchFileName));
XBatch = data';
XBatch = reshape(XBatch, 32,32,3,[]);
XBatch = permute(XBatch, [2 1 3 4]);
TBatch = convertLabelsToCategorical(location, labels);
end

function categoricalLabels = convertLabelsToCategorical(location, integerLabels)
load(fullfile(location,'batches.meta.mat'));
categoricalLabels = categorical(integerLabels, 0:9, label_names);
end

這裏的helperCIFAR10Data。

2.用上面的程序下載太慢了,你也可以建個文件夾,直接用瀏覽器打開https://www.cs.toronto.edu/~kriz/cifar-10-matlab.tar.gz地址下載放到自己新建的文件夾下,然後matlab打開這個文件夾即可。這裏,我建的文件夾名字爲cifar10Data,把下載的文件cifar-10-batches-mat放在這個文件夾下,圖片如下:

3.取出數據後可以使用matlab顯示數據

[trainingImages,trainingLabels,testImages,testLabels] = helperCIFAR10Data.load('cifar10Data');
figure
thumbnails = trainingImages(:,:,:,1:100);
montage(thumbnails)

顯示圖片如下:

Note: This example requires Computer Vision System Toolbox™, Image Processing Toolbox™, Neural Network Toolbox™, and Statistics and Machine Learning Toolbox™.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章