Contenido principal

Realizar predicciones con un objeto dlnetwork

En este ejemplo se muestra cómo realizar predicciones con un objeto dlnetwork haciendo bucles con minilotes.

Para conjuntos grandes de datos o cuando se predice con hardware con memoria limitada, realice predicciones haciendo bucles con minilotes de los datos usando la función minibatchpredict.

Cargar un objeto dlnetwork

Cargue un objeto dlnetwork entrenado y los nombres de clase correspondientes en el área de trabajo. La red neuronal tiene una entrada y dos salidas. Toma imágenes de dígitos escritos a mano como entrada y predice la etiqueta de los dígitos y el ángulo de rotación.

load dlnetDigits

Cargar datos para predicción

Cargue los datos de prueba de dígitos para la predicción.

load DigitsDataTest

Visualice los nombres de las clases.

classNames
classNames = 10×1 cell
    {'0'}
    {'1'}
    {'2'}
    {'3'}
    {'4'}
    {'5'}
    {'6'}
    {'7'}
    {'8'}
    {'9'}

Visualice algunas de las imágenes y las etiquetas y ángulos de rotación correspondientes.

numObservations = size(XTest,4);
numPlots = 9;
idx = randperm(numObservations,numPlots);

figure
for i = 1:numPlots
    nexttile(i)
    I = XTest(:,:,:,idx(i));
    label = labelsTest(idx(i));
    imshow(I)
    title("Label: " + string(label) + newline + "Angle: " + anglesTest(idx(i)))
end

Figure contains 9 axes objects. Hidden axes object 1 with title Label: 8 Angle: 5 contains an object of type image. Hidden axes object 2 with title Label: 9 Angle: -45 contains an object of type image. Hidden axes object 3 with title Label: 1 Angle: -11 contains an object of type image. Hidden axes object 4 with title Label: 9 Angle: -40 contains an object of type image. Hidden axes object 5 with title Label: 6 Angle: -42 contains an object of type image. Hidden axes object 6 with title Label: 0 Angle: -18 contains an object of type image. Hidden axes object 7 with title Label: 2 Angle: -9 contains an object of type image. Hidden axes object 8 with title Label: 5 Angle: -17 contains an object of type image. Hidden axes object 9 with title Label: 9 Angle: -27 contains an object of type image.

Hacer predicciones

Realice predicciones con la función minibatchpredict y convierta las puntuaciones de clasificación en etiquetas con la función scores2label. De forma predeterminada, la función minibatchpredict usa una GPU en caso de que esté disponible. Para utilizar una GPU se requiere una licencia de Parallel Computing Toolbox™ y un dispositivo GPU compatible. Para obtener información sobre los dispositivos compatibles, consulte GPU Computing Requirements (Parallel Computing Toolbox). De lo contrario, la función usa la CPU. Para seleccionar el entorno de ejecución manualmente, utilice el argumento ExecutionEnvironment de la función minibatchpredict.

[scoresTest,Y2Test] = minibatchpredict(net,XTest);
Y1Test = scores2label(scoresTest,classNames);

Visualice algunas de las predicciones.

idx = randperm(numObservations,numPlots);

figure
for i = 1:numPlots
    nexttile(i)
    I = XTest(:,:,:,idx(i));
    label = Y1Test(idx(i));
    imshow(I)
    title("Label: " + string(label) + newline + "Angle: " + Y2Test(idx(i)))
end

Figure contains 9 axes objects. Hidden axes object 1 with title Label: 9 Angle: 20.3954 contains an object of type image. Hidden axes object 2 with title Label: 1 Angle: 3.7015 contains an object of type image. Hidden axes object 3 with title Label: 9 Angle: 23.5494 contains an object of type image. Hidden axes object 4 with title Label: 9 Angle: -36.4954 contains an object of type image. Hidden axes object 5 with title Label: 4 Angle: 16.4279 contains an object of type image. Hidden axes object 6 with title Label: 7 Angle: 3.0644 contains an object of type image. Hidden axes object 7 with title Label: 1 Angle: 33.1356 contains an object of type image. Hidden axes object 8 with title Label: 4 Angle: 30.7531 contains an object of type image. Hidden axes object 9 with title Label: 9 Angle: 0.55886 contains an object of type image.

Consulte también

| | | |

Temas