手把手教你用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放在这个文件夹下,图片如下:

手把手教你用matlab做深度学习_第1张图片

3.取出数据后可以使用matlab显示数据

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

显示图片如下:

手把手教你用matlab做深度学习_第2张图片

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

你可能感兴趣的:(matlab,deeplearning)