Main Content

La traduzione di questa pagina non è aggiornata. Fai clic qui per vedere l'ultima versione in inglese.

SeriesNetwork

Rete in serie per il Deep Learning

Descrizione

Una rete in serie è una rete neurale per il Deep Learning i cui livelli sono disposti uno dopo l’altro. Ha un singolo livello di input e un singolo livello di output.

Creazione

Esistono diversi modi per creare un oggetto SeriesNetwork:

Nota

Per scoprire altre reti preaddestrate, come googlenet e resnet50, vedere Reti neurali profonde preaddestrate.

Proprietà

espandi tutto

proprietà è di sola lettura.

Livelli di rete, specificati come un array di Layer.

proprietà è di sola lettura.

Nomi dei livelli di input della rete, specificati come un array di celle dei vettori di carattere.

Tipi di dati: cell

Nomi dei livelli di output della rete, specificati come un array di celle dei vettori di carattere.

Tipi di dati: cell

Funzioni oggetto

activationsCompute deep learning network layer activations
classifyClassify data using trained deep learning neural network
predictPredict responses using trained deep learning neural network
predictAndUpdateStatePredict responses using a trained recurrent neural network and update the network state
classifyAndUpdateStateClassify data using a trained recurrent neural network and update the network state
resetStateReset state parameters of neural network
plotPlot neural network architecture

Esempi

comprimi tutto

Caricare una rete neurale convoluzionale AlexNet preaddestrata ed esaminare i livelli e le classi.

Caricare la rete AlexNet preaddestrata utilizzando alexnet. L’output net è un oggetto SeriesNetwork.

net = alexnet
net = 
  SeriesNetwork with properties:

    Layers: [25×1 nnet.cnn.layer.Layer]

Utilizzando la proprietà Layers si visualizza l’architettura della rete. La rete è composta da 25 livelli. Sono presenti 8 livelli con pesi apprendibili: 5 livelli convoluzionali e 3 livelli completamente connessi.

net.Layers
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'    Grouped Convolution           2 groups of 128 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'    Grouped Convolution           2 groups of 192 3x3x192 convolutions with stride [1  1] and padding [1  1  1  1]
    13   'relu4'    ReLU                          ReLU
    14   'conv5'    Grouped Convolution           2 groups of 128 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

È possibile vedere i nomi delle classi apprese dalla rete visualizzando la proprietà Classes del livello di output della classificazione (il livello finale). Visualizzare le prime 10 classi selezionando i primi 10 elementi.

net.Layers(end).Classes(1:10)
ans = 10×1 categorical array
     tench 
     goldfish 
     great white shark 
     tiger shark 
     hammerhead 
     electric ray 
     stingray 
     cock 
     hen 
     ostrich 

Specificare il file di esempio 'digitsnet.prototxt' da importare.

protofile = 'digitsnet.prototxt';

Importare i livelli di rete.

layers = importCaffeLayers(protofile)
layers = 

  1x7 Layer array with layers:

     1   'testdata'   Image Input             28x28x1 images
     2   'conv1'      Convolution             20 5x5x1 convolutions with stride [1  1] and padding [0  0]
     3   'relu1'      ReLU                    ReLU
     4   'pool1'      Max Pooling             2x2 max pooling with stride [2  2] and padding [0  0]
     5   'ip1'        Fully Connected         10 fully connected layer
     6   'loss'       Softmax                 softmax
     7   'output'     Classification Output   crossentropyex with 'class1', 'class2', and 8 other classes

Caricare i dati come un oggetto ImageDatastore.

digitDatasetPath = fullfile(matlabroot,'toolbox','nnet', ...
    'nndemos','nndatasets','DigitDataset');
imds = imageDatastore(digitDatasetPath, ...
    'IncludeSubfolders',true, ...
    'LabelSource','foldernames');

Il datastore contiene 10.000 immagini sintetiche di cifre da 0 a 9. Le immagini sono generate applicando trasformazioni casuali a immagini di cifre create con diversi font. L'immagine di ogni cifra è di 28x28 pixel. Il datastore contiene lo stesso numero di immagini per categoria.

Visualizzare alcune delle immagini presenti nel datastore.

figure
numImages = 10000;
perm = randperm(numImages,20);
for i = 1:20
    subplot(4,5,i);
    imshow(imds.Files{perm(i)});
    drawnow;
end

Figure contains 20 axes objects. Axes object 1 contains an object of type image. Axes object 2 contains an object of type image. Axes object 3 contains an object of type image. Axes object 4 contains an object of type image. Axes object 5 contains an object of type image. Axes object 6 contains an object of type image. Axes object 7 contains an object of type image. Axes object 8 contains an object of type image. Axes object 9 contains an object of type image. Axes object 10 contains an object of type image. Axes object 11 contains an object of type image. Axes object 12 contains an object of type image. Axes object 13 contains an object of type image. Axes object 14 contains an object of type image. Axes object 15 contains an object of type image. Axes object 16 contains an object of type image. Axes object 17 contains an object of type image. Axes object 18 contains an object of type image. Axes object 19 contains an object of type image. Axes object 20 contains an object of type image.

Dividere il datastore in modo che ogni categoria del set di addestramento abbia 750 immagini e il set di prova contenga le immagini rimanenti di ciascuna etichetta.

numTrainingFiles = 750;
[imdsTrain,imdsTest] = splitEachLabel(imds,numTrainingFiles,'randomize');

splitEachLabel divide i file immagine in digitData in due nuovi datastore imdsTrain e imdsTest.

Definire l’architettura della rete neurale convoluzionale.

layers = [ ...
    imageInputLayer([28 28 1])
    convolution2dLayer(5,20)
    reluLayer
    maxPooling2dLayer(2,'Stride',2)
    fullyConnectedLayer(10)
    softmaxLayer
    classificationLayer];

Impostare le opzioni sulle impostazioni predefinite per la discesa stocastica del gradiente con momento. Impostare il numero massimo di epoche a 20 e avviare l'addestramento con una velocità di apprendimento iniziale di 0,0001.

options = trainingOptions('sgdm', ...
    'MaxEpochs',20,...
    'InitialLearnRate',1e-4, ...
    'Verbose',false, ...
    'Plots','training-progress');

Addestrare la rete.

net = trainNetwork(imdsTrain,layers,options);

Figure Training Progress (23-Mar-2023 10:40:33) contains 2 axes objects and another object of type uigridlayout. Axes object 1 with xlabel Iteration, ylabel Loss contains 6 objects of type patch, text, line. Axes object 2 with xlabel Iteration, ylabel Accuracy (%) contains 6 objects of type patch, text, line.

Eseguire la rete addestrata sul set di prova che non è stato utilizzato per addestrare la rete, e prevedere le etichette delle immagini (cifre).

YPred = classify(net,imdsTest);
YTest = imdsTest.Labels;

Calcolare la precisione. L'accuratezza è il rapporto tra il numero di etichette vere nei dati di prova che corrispondono alle classificazioni di classify e il numero di immagini nei dati di prova.

accuracy = sum(YPred == YTest)/numel(YTest)
accuracy = 0.9412

Funzionalità estese

Cronologia versioni

Introdotto in R2016a