python


5、matlab实现

<pre><code>img_path = 'D:\work\jupyterNotebook\baidu_dianshi\data\class'; imds = imageDatastore(img_path, ... 'IncludeSubfolders',true,'LabelSource','foldernames');</code></pre> <pre><code>figure; perm = randperm(2000,20); for i = 1:20 subplot(4,5,i); imshow(imds.Files{perm(i)}); end </code></pre> <p><img src="https://www.showdoc.cc/server/api/common/visitfile/sign/4b852b95ee3fe56f3099b70975cf9004?showdoc=.jpg" alt="" /></p> <pre><code>labelCount = countEachLabel(imds)</code></pre> <pre><code>CITY 55 DESERT 636 FARMLAND 218 LAKE 171 MOUNTAIN 464 OCEAN 456 </code></pre> <pre><code>img = readimage(imds,1); size(img)</code></pre> <pre><code>ans = 1×3 256 256 3</code></pre> <pre><code>net = alexnet; net.Layers</code></pre> <pre><code>ans = 25x1 Layer array with layers: 1 'data' Image Input 227x227x3 images with 'zerocenter' normalization 2 'conv1' Convolution 96 11x11x3 convolutions with stride [4 4] and padding [0 0 0 0] 3 'relu1' ReLU ReLU 4 'norm1' Cross Channel Normalization cross channel normalization with 5 channels per element 5 'pool1' Max Pooling 3x3 max pooling with stride [2 2] and padding [0 0 0 0] 6 'conv2' Convolution 256 5x5x48 convolutions with stride [1 1] and padding [2 2 2 2] 7 'relu2' ReLU ReLU 8 'norm2' Cross Channel Normalization cross channel normalization with 5 channels per element 9 'pool2' Max Pooling 3x3 max pooling with stride [2 2] and padding [0 0 0 0] 10 'conv3' Convolution 384 3x3x256 convolutions with stride [1 1] and padding [1 1 1 1] 11 'relu3' ReLU ReLU 12 'conv4' Convolution 384 3x3x192 convolutions with stride [1 1] and padding [1 1 1 1] 13 'relu4' ReLU ReLU 14 'conv5' Convolution 256 3x3x192 convolutions with stride [1 1] and padding [1 1 1 1] 15 'relu5' ReLU ReLU 16 'pool5' Max Pooling 3x3 max pooling with stride [2 2] and padding [0 0 0 0] 17 'fc6' Fully Connected 4096 fully connected layer 18 'relu6' ReLU ReLU 19 'drop6' Dropout 50% dropout 20 'fc7' Fully Connected 4096 fully connected layer 21 'relu7' ReLU ReLU 22 'drop7' Dropout 50% dropout 23 'fc8' Fully Connected 1000 fully connected layer 24 'prob' Softmax softmax 25 'output' Classification Output crossentropyex with 'tench' and 999 other classes</code></pre> <pre><code>layersTransfer = net.Layers(2:end-3);</code></pre> <pre><code>layersTransfer = 21x1 Layer array with layers: 1 'conv1' Convolution 96 11x11x3 convolutions with stride [4 4] and padding [0 0 0 0] 2 'relu1' ReLU ReLU 3 'norm1' Cross Channel Normalization cross channel normalization with 5 channels per element 4 'pool1' Max Pooling 3x3 max pooling with stride [2 2] and padding [0 0 0 0] 5 'conv2' Convolution 256 5x5x48 convolutions with stride [1 1] and padding [2 2 2 2] 6 'relu2' ReLU ReLU 7 'norm2' Cross Channel Normalization cross channel normalization with 5 channels per element 8 'pool2' Max Pooling 3x3 max pooling with stride [2 2] and padding [0 0 0 0] 9 'conv3' Convolution 384 3x3x256 convolutions with stride [1 1] and padding [1 1 1 1] 10 'relu3' ReLU ReLU 11 'conv4' Convolution 384 3x3x192 convolutions with stride [1 1] and padding [1 1 1 1] 12 'relu4' ReLU ReLU 13 'conv5' Convolution 256 3x3x192 convolutions with stride [1 1] and padding [1 1 1 1] 14 'relu5' ReLU ReLU 15 'pool5' Max Pooling 3x3 max pooling with stride [2 2] and padding [0 0 0 0] 16 'fc6' Fully Connected 4096 fully connected layer 17 'relu6' ReLU ReLU 18 'drop6' Dropout 50% dropout 19 'fc7' Fully Connected 4096 fully connected layer 20 'relu7' ReLU ReLU 21 'drop7' Dropout 50% dropout</code></pre> <pre><code>layers = [ imageInputLayer([256 256 3]) layersTransfer fullyConnectedLayer(6,'WeightLearnRateFactor',20,'BiasLearnRateFactor',20) softmaxLayer classificationLayer]; </code></pre> <pre><code>options = trainingOptions('sgdm', ... 'MiniBatchSize',10, ... 'MaxEpochs',6, ... 'InitialLearnRate',1e-4, ... 'ValidationData',imdsValidation, ... 'ValidationFrequency',3, ... 'ValidationPatience',Inf, ... 'Verbose',false, ... 'Plots','training-progress');</code></pre> <pre><code>netTransfer = trainNetwork(imdsTrain,layers,options);</code></pre> <p><img src="https://www.showdoc.cc/server/api/common/visitfile/sign/4cb77ae83ef568837d456d9c72564bcb?showdoc=.jpg" alt="" /></p> <pre><code>YPred = classify(netTransfer,imdsValidation); YValidation = imdsValidation.Labels; accuracy = sum(YPred == YValidation)/numel(YValidation)</code></pre> <p>accuracy = 0.9400</p> <h5>训练完可以把所有变量保存到mat文件</h5> <hr /> <hr /> <h5>测试时加载mat文件,对未知数据测试</h5> <pre><code>YPred = classify(netTransfer,imdsValidation); YValidation = imdsValidation.Labels; accuracy = sum(YPred == YValidation)/numel(YValidation)</code></pre> <p>accuracy = 0.9400</p> <pre><code>pre_img_path = 'D:\work\jupyterNotebook\baidu_dianshi\data\pre_data'; pre_imds = imageDatastore(pre_img_path, ... 'IncludeSubfolders',true,'LabelSource','foldernames');</code></pre> <pre><code>pre_Pred = DESERT OCEAN MOUNTAIN MOUNTAIN LAKE DESERT LAKE LAKE ...</code></pre> <hr /> <p>导出变量</p> <pre><code>pfiles = pre_imds.Files </code></pre> <pre><code>pfiles = {'D:\work\jupyterNotebook\baidu_dianshi\data\pre_data\MWI_KD96UmGQ6KWVeohF.jpg'} {'D:\work\jupyterNotebook\baidu_dianshi\data\pre_data\MWI_KDDTIKoSiQgwZiUQ.jpg'} {'D:\work\jupyterNotebook\baidu_dianshi\data\pre_data\MWI_KERiJD55HvBKIhmL.jpg'} {'D:\work\jupyterNotebook\baidu_dianshi\data\pre_data\MWI_KF9KpqQNNMVSsUKH.jpg'} ....</code></pre> <pre><code>save pfile.mat pfiles</code></pre> <pre><code>% cellstr 直接转为cell array类型 便于导入python cell_pre = cellstr(pre_Pred); save cell_str.mat cell_pre </code></pre>

页面列表

ITEM_HTML