matlab做深度学习(一)- --CNN1

1.使用深度学习做目标检测

上一篇博客已经讲解了怎么用matlab导入数据。

[trainingImages,trainingLabels,testImages,testLabels] = helperCIFAR10Data.load('cifar10Data');
 
 

使用这个指令就可以导入CIFAR-10 data的数据。

使用下面指令查看样本和图片大小:

size(trainingImages)
 
 

CIFAR-10 数据集有10类,使用指令列出:


 
 
  1. numImageCategories = 10;
  2. categories(trainingLabels)

1.接下来我们来建立CNN模型,这里建立输入层:


 
 
  1. % Create the image input layer for 32x32x3 CIFAR-10 images
  2. [height, width, numChannels, ~] = size(trainingImages);
  3. imageSize = [height width numChannels];
  4. inputLayer = imageInputLayer(imageSize)

2.建立网络中间层


 
 
  1. % Convolutional layer parameters filter size
  2. filterSize = [5 5];
  3. numFilters = 32;
  4. middleLayers = [
  5. % The first convolutional layer has a bank of 32 5x5x3 filters. A
  6. % symmetric padding of 2 pixels is added to ensure that image borders
  7. % are included in the processing. This is important to avoid
  8. % information at the borders being washed away too early in the
  9. % network.
  10. convolution2dLayer(filterSize, numFilters, 'Padding', 2) %(n+2p-f)/s+1
  11. % Note that the third dimension of the filter can be omitted because it
  12. % is automatically deduced based on the connectivity of the network. In
  13. % this case because this layer follows the image layer, the third
  14. % dimension must be 3 to match the number of channels in the input
  15. % image.
  16. % Next add the ReLU layer:
  17. reluLayer()
  18. % Follow it with a max pooling layer that has a 3x3 spatial pooling area
  19. % and a stride of 2 pixels. This down-samples the data dimensions from
  20. % 32x32 to 15x15.
  21. maxPooling2dLayer(3, 'Stride', 2)
  22. % Repeat the 3 core layers to complete the middle of the network.
  23. convolution2dLayer(filterSize, numFilters, 'Padding', 2)
  24. reluLayer()
  25. maxPooling2dLayer(3, 'Stride',2)
  26. convolution2dLayer(filterSize, 2 * numFilters, 'Padding', 2)
  27. reluLayer()
  28. maxPooling2dLayer(3, 'Stride',2)
  29. ]

3.最后定义输出层


 
 
  1. finalLayers = [
  2. % Add a fully connected layer with 64 output neurons. The output size of
  3. % this layer will be an array with a length of 64.
  4. fullyConnectedLayer(64)
  5. % Add an ReLU non-linearity.
  6. reluLayer
  7. % Add the last fully connected layer. At this point, the network must
  8. % produce 10 signals that can be used to measure whether the input image
  9. % belongs to one category or another. This measurement is made using the
  10. % subsequent loss layers.
  11. fullyConnectedLayer(numImageCategories)
  12. % Add the softmax loss layer and classification layer. The final layers use
  13. % the output of the fully connected layer to compute the categorical
  14. % probability distribution over the image classes. During the training
  15. % process, all the network weights are tuned to minimize the loss over this
  16. % categorical distribution.
  17. softmaxLayer
  18. classificationLayer
  19. ]

 

4.合并

 


 
 
  1. layers = [
  2. inputLayer
  3. middleLayers
  4. finalLayers
  5. ]

5.定义输入层权值,

layers(2).Weights = 0.0001 * randn([filterSize numChannels numFilters]);
 
 

6.这里参数解释,sgdm就是stochastic gradient descent with momentum(动量的随机梯度下降法),Momentum是动量参数为0.9,InitialLearnRate初始学习速率0.001,L2Regularization=0.004是L2正则化系数,LearnRateDropFactor=0.1、LearnRateDropPeriod=8是每8个epoces使得学习速率乘以一个0.1的比例因子,MaxEpochs= 40最大训练为40个epoces,MiniBatchSize=128为Batch为128,Verbose =true就是把信息打印到命令窗口

Note that the training algorithm uses a mini-batch size of 128 images. If using a GPU for training, this size may need to be lowered due to memory constraints on the GPU.


 
 
  1. % Set the network training options
  2. opts = trainingOptions('sgdm', ...
  3. 'Momentum', 0.9, ...
  4. 'InitialLearnRate', 0.001, ...
  5. 'LearnRateSchedule', 'piecewise', ...
  6. 'LearnRateDropFactor', 0.1, ...
  7. 'LearnRateDropPeriod', 8, ...
  8. 'L2Regularization', 0.004, ...
  9. 'MaxEpochs', 40, ...
  10. 'MiniBatchSize', 128, ...
  11. 'Verbose', true);

 

7.这里,doTraining设置为false了,就是直接导入已经训练好的模型,你也可以把doTraining改为True,自己改模型训练,下一篇博客应该教大家怎么改模型,怎么训练


 
 
  1. % A trained network is loaded from disk to save time when running the
  2. % example. Set this flag to true to train the network.
  3. doTraining = false;
  4. if doTraining
  5. % Train a network.
  6. cifar10Net = trainNetwork(trainingImages, trainingLabels, layers, opts);
  7. else
  8. % Load pre-trained detector for the example.
  9. load('rcnnStopSigns.mat','cifar10Net')
  10. end

 

8.这里,你可以看到权值


 
 
  1. % Extract the first convolutional layer weights
  2. w = cifar10Net.Layers(2).Weights;
  3. % rescale the weights to the range [0, 1] for better visualization
  4. w = rescale(w);
  5. figure
  6. montage(w)

 

9.到这里,你已经成功了,可以看到accuracy

To completely validate the training results, use the CIFAR-10 test data to measure the classification accuracy of the network. A low accuracy score indicates additional training or additional training data is required. The goal of this example is not necessarily to achieve 100% accuracy on the test set, but to sufficiently train a network for use in training an object detector.


 
 
  1. % Run the network on the test set.
  2. YTest = classify(cifar10Net, testImages);
  3. % Calculate the accuracy.
  4. accuracy = sum(YTest == testLabels)/numel(testLabels)

下面给出我的训练过程:

运行结果:

如果你想直观的看训练过程,只要增加一条即可:

% Set the network training options
opts = trainingOptions('sgdm', ...
    'Momentum', 0.9, ...
    'InitialLearnRate', 0.001, ...
    'LearnRateSchedule', 'piecewise', ...
    'LearnRateDropFactor', 0.1, ...
    'LearnRateDropPeriod', 8, ...
    'L2Regularization', 0.004, ...
    'MaxEpochs', 40, ...
    'MiniBatchSize', 128, ...
    'Verbose', true,...
 'Plots','training-progress');

这里,你发现了与上面的不同吗?对,增加了'Plots','training-progress'这一条。

现在看看重新运行效果:

看,这个整个训练过程就可以看出来了,是不是很直观。从上面可以看出训练的精度在80%左右,下一篇博客将介绍怎么提高训练精度。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值