Usuario:


Modelo vgg16

Descripción

>Top


El modelo vgg16 se especializa en análisis de imágenes y esta disponible desde Keras. Se carga a tensorflow (tf) mediante keras.applications:

import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Activation, Dense, Flatten, BatchNormalization, Conv2D, MaxPool2D

# cargar modelo vgg16
vgg16_model = tf.keras.applications.vgg16.VGG16()

ID:(13759, 0)



Formar modelo

Descripción

>Top


Antes de proceder a entrenar el modelo se deben bloquear las capas que no deben ser entrenadas ya que ya lo fueron en la estructuración del modelo.

# definir modelo
vgg_model = Sequential()
# Bloqueo de 16 capas para que no sean entrenadas
for layer in vgg16_model.layers[:-1]:
    vgg_model.add(layer)

ID:(13760, 0)



Mostrar modelo

Descripción

>Top


Se puede mostrar con summary el resumen del modelo:

# mostrar el resumen del modelo
vgg_model.summary()

Hay que hacer notar que

- los comandos MaxPooling van reduciendo el tamaño de las imágenes desde los 224 a 112, 56, 28, 14 y 7

- las convoluciones (Conv2D) para ello van aumentando las dimensiones de la paleta de colores de 64 a 128, 256 y 512

Model: 'sequential_1'

_________________________________________________________________

Layer (type) Output Shape Param #

=================================================================

block1_conv1 (Conv2D) (None, 224, 224, 64) 1792

_________________________________________________________________

block1_conv2 (Conv2D) (None, 224, 224, 64) 36928

_________________________________________________________________

block1_pool (MaxPooling2D) (None, 112, 112, 64) 0

_________________________________________________________________

block2_conv1 (Conv2D) (None, 112, 112, 128) 73856

_________________________________________________________________

block2_conv2 (Conv2D) (None, 112, 112, 128) 147584

_________________________________________________________________

block2_pool (MaxPooling2D) (None, 56, 56, 128) 0

_________________________________________________________________

block3_conv1 (Conv2D) (None, 56, 56, 256) 295168

_________________________________________________________________

block3_conv2 (Conv2D) (None, 56, 56, 256) 590080

_________________________________________________________________

block3_conv3 (Conv2D) (None, 56, 56, 256) 590080

_________________________________________________________________

block3_pool (MaxPooling2D) (None, 28, 28, 256) 0

_________________________________________________________________

block4_conv1 (Conv2D) (None, 28, 28, 512) 1180160

_________________________________________________________________

block4_conv2 (Conv2D) (None, 28, 28, 512) 2359808

_________________________________________________________________

block4_conv3 (Conv2D) (None, 28, 28, 512) 2359808

_________________________________________________________________

block4_pool (MaxPooling2D) (None, 14, 14, 512) 0

_________________________________________________________________

block5_conv1 (Conv2D) (None, 14, 14, 512) 2359808

_________________________________________________________________

block5_conv2 (Conv2D) (None, 14, 14, 512) 2359808

_________________________________________________________________

block5_conv3 (Conv2D) (None, 14, 14, 512) 2359808

_________________________________________________________________

block5_pool (MaxPooling2D) (None, 7, 7, 512) 0

_________________________________________________________________

flatten (Flatten) (None, 25088) 0

_________________________________________________________________

fc1 (Dense) (None, 4096) 102764544

_________________________________________________________________

fc2 (Dense) (None, 4096) 16781312

=================================================================

Total params: 134,260,544

Trainable params: 134,260,544

Non-trainable params: 0

_________________________________________________________________

ID:(13761, 0)



Bloquear capas ya entrenadas

Descripción

>Top


Para evitar que en el proceso de aprendizaje de vuelva a entrenar las capas del modelo original se procede al bloqueo del parámetro trainable:

# bloqueo de capas
for layer in vgg_model.layers:
    layer.trainable = False


Para aprovechar la información ya obtenida en el aprendizaje en modelo original se debe bloquear el aprendizaje de dichas capa.

ID:(13762, 0)



Agregar capa final

Descripción

>Top


El modelo se adapta agregando una capa final del tamaño de las categorías que se quieren predecir:

# agregar capa final (densa)
vgg_model.add(Dense(units=len(classes),activation='softmax'))


El units debe ser igualado al numero de clases que se pretenden pronosticar.

ID:(13763, 0)



Mostrar modelo modificado

Descripción

>Top


Para mostrar como se modifico el modelo se puede nuevamente usar la función summary:

# mostrar modelo vgg
vgg_model.summary()

En el listado de capas se observa al final la capa densa con las clases que se desean pronosticar:

Model: 'sequential_1'

_________________________________________________________________

Layer (type) Output Shape Param #

=================================================================

block1_conv1 (Conv2D) (None, 224, 224, 64) 1792

_________________________________________________________________

block1_conv2 (Conv2D) (None, 224, 224, 64) 36928

_________________________________________________________________

block1_pool (MaxPooling2D) (None, 112, 112, 64) 0

_________________________________________________________________

block2_conv1 (Conv2D) (None, 112, 112, 128) 73856

_________________________________________________________________

block2_conv2 (Conv2D) (None, 112, 112, 128) 147584

_________________________________________________________________

block2_pool (MaxPooling2D) (None, 56, 56, 128) 0

_________________________________________________________________

block3_conv1 (Conv2D) (None, 56, 56, 256) 295168

_________________________________________________________________

block3_conv2 (Conv2D) (None, 56, 56, 256) 590080

_________________________________________________________________

block3_conv3 (Conv2D) (None, 56, 56, 256) 590080

_________________________________________________________________

block3_pool (MaxPooling2D) (None, 28, 28, 256) 0

_________________________________________________________________

block4_conv1 (Conv2D) (None, 28, 28, 512) 1180160

_________________________________________________________________

block4_conv2 (Conv2D) (None, 28, 28, 512) 2359808

_________________________________________________________________

block4_conv3 (Conv2D) (None, 28, 28, 512) 2359808

_________________________________________________________________

block4_pool (MaxPooling2D) (None, 14, 14, 512) 0

_________________________________________________________________

block5_conv1 (Conv2D) (None, 14, 14, 512) 2359808

_________________________________________________________________

block5_conv2 (Conv2D) (None, 14, 14, 512) 2359808

_________________________________________________________________

block5_conv3 (Conv2D) (None, 14, 14, 512) 2359808

_________________________________________________________________

block5_pool (MaxPooling2D) (None, 7, 7, 512) 0

_________________________________________________________________

flatten (Flatten) (None, 25088) 0

_________________________________________________________________

fc1 (Dense) (None, 4096) 102764544

_________________________________________________________________

fc2 (Dense) (None, 4096) 16781312

_________________________________________________________________

dense_1 (Dense) (None, 290) 1188130

=================================================================

Total params: 135,448,674

Trainable params: 1,188,130

Non-trainable params: 134,260,544

_________________________________________________________________

ID:(13764, 0)



Compilar modelo

Descripción

>Top


Para armar el modelo se debe proceder a compilarlo:

# Compilar el modelo
vgg_model.compile(optimizer=Adam(learning_rate=0.0001), loss='categorical_crossentropy',metrics=['accuracy'])

ID:(13765, 0)



Proceso de aprendizaje

Descripción

>Top


# Proceso de aprendizaje con el modelo vgg16 modificado
vgg_model.fit(x=train_batches, validation_data=validate_batches, epochs=5,verbose=2)

Epoch 1/5

373/373 - 336s - loss: 2.4201 - accuracy: 0.2790 - val_loss: 2.2614 - val_accuracy: 0.3023

Epoch 2/5

373/373 - 340s - loss: 1.7795 - accuracy: 0.4362 - val_loss: 2.0955 - val_accuracy: 0.3639

Epoch 3/5

373/373 - 339s - loss: 1.5116 - accuracy: 0.5241 - val_loss: 2.0217 - val_accuracy: 0.3868

Epoch 4/5

373/373 - 339s - loss: 1.3313 - accuracy: 0.5754 - val_loss: 1.9925 - val_accuracy: 0.3926

Epoch 5/5

373/373 - 341s - loss: 1.1973 - accuracy: 0.6296 - val_loss: 1.9473 - val_accuracy: 0.4169

ID:(13766, 0)