Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

7/7/22, 12:03 Salazar Francisco C1_W4_Lab_3_compacted_images.

ipynb - Colaboratory

Effect of Compacted Images in Training


In this notebook, you will see how reducing the target size of the generator images will affect the
architecture and performance of your model. This is a useful technique in case you need to
speed up your training or save compute resources. Let's begin!

IMPORTANT NOTE: This notebook is designed to run as a Colab. Running it on your local
machine might result in some of the code blocks throwing errors.

As before, start downloading the train and validation sets:

# Download the training set

!wget https://storage.googleapis.com/tensorflow-1-public/course2/week3/horse-or-human.zip

# Download the validation set

!wget https://storage.googleapis.com/tensorflow-1-public/course2/week3/validation-horse-or

Then unzip them:

import zipfile

# Unzip training set

local_zip = './horse-or-human.zip'

zip_ref = zipfile.ZipFile(local_zip, 'r')

zip_ref.extractall('./horse-or-human')

# Unzip validation set

local_zip = './validation-horse-or-human.zip'

zip_ref = zipfile.ZipFile(local_zip, 'r')

zip_ref.extractall('./validation-horse-or-human')

zip_ref.close()

Then define the directories containing the images:

import os

# Directory with training horse pictures

train_horse_dir = os.path.join('./horse-or-human/horses')

# Directory with training human pictures

train_human_dir = os.path.join('./horse-or-human/humans')

# Directory with validation horse pictures

https://colab.research.google.com/drive/1vJlR0BHS0l27KoR-SgQ35iVD8F7ARPU4#scrollTo=xPv_aeoGFg0N&printMode=true 1/11
7/7/22, 12:03 Salazar Francisco C1_W4_Lab_3_compacted_images.ipynb - Colaboratory

validation_horse_dir = os.path.join('./validation-horse-or-human/horses')

# Directory with validation human pictures

validation_human_dir = os.path.join('./validation-horse-or-human/humans')

You can check that the directories are not empty and that the train set has more images than the
validation set:

train_horse_names = os.listdir(train_horse_dir)

print(f'TRAIN SET HORSES: {train_horse_names[:10]}')

train_human_names = os.listdir(train_human_dir)

print(f'TRAIN SET HUMANS: {train_human_names[:10]}')

validation_horse_hames = os.listdir(validation_horse_dir)

print(f'VAL SET HORSES: {validation_horse_hames[:10]}')

validation_human_names = os.listdir(validation_human_dir)

print(f'VAL SET HUMANS: {validation_human_names[:10]}')

print(f'total training horse images: {len(os.listdir(train_horse_dir))}')

print(f'total training human images: {len(os.listdir(train_human_dir))}')

print(f'total validation horse images: {len(os.listdir(validation_horse_dir))}')

print(f'total validation human images: {len(os.listdir(validation_human_dir))}')

Build the Model


The model will follow the same architecture as before but they key difference is in the
input_shape parameter of the first Conv2D layer. Since you will be compacting the images later
in the generator, you need to specify the expected image size here. So instead of 300x300 as in
the previous two labs, you specify a smaller 150x150 array.

import tensorflow as tf

model = tf.keras.models.Sequential([

    # Note the input shape is the desired size of the image 150x150 with 3 bytes color

    # This is the first convolution

    tf.keras.layers.Conv2D(16, (3,3), activation='relu', input_shape=(150, 150, 3)),

    tf.keras.layers.MaxPooling2D(2, 2),

    # The second convolution

    tf.keras.layers.Conv2D(32, (3,3), activation='relu'),

    tf.keras.layers.MaxPooling2D(2,2),

    # The third convolution

    tf.keras.layers.Conv2D(64, (3,3), activation='relu'),

    tf.keras.layers.MaxPooling2D(2,2),

#     # The fourth convolution (You can uncomment the 4th and 5th conv layers later to see 
#     tf.keras.layers.Conv2D(64, (3,3), activation='relu'),

#     tf.keras.layers.MaxPooling2D(2,2),

#     # The fifth convolution

https://colab.research.google.com/drive/1vJlR0BHS0l27KoR-SgQ35iVD8F7ARPU4#scrollTo=xPv_aeoGFg0N&printMode=true 2/11
7/7/22, 12:03 Salazar Francisco C1_W4_Lab_3_compacted_images.ipynb - Colaboratory

#     tf.keras.layers.Conv2D(64, (3,3), activation='relu'),

#     tf.keras.layers.MaxPooling2D(2,2),

    # Flatten the results to feed into a DNN

    tf.keras.layers.Flatten(),

    # 512 neuron hidden layer

    tf.keras.layers.Dense(512, activation='relu'),

    # Only 1 output neuron. It will contain a value from 0-1 where 0 for 1 class ('horses'
    tf.keras.layers.Dense(1, activation='sigmoid')

])

You can see the difference from previous models when you print the model.summary() . As
expected, there will be less inputs to the Dense layer at the end of the model compared to the
previous labs. This is because you used the same number of max pooling layers in your model.
And since you have a smaller image to begin with (150 x 150), then the output after all the
pooling layers will also be smaller.

model.summary()

You will use the same settings for training:

from tensorflow.keras.optimizers import RMSprop

model.compile(loss='binary_crossentropy',

              optimizer=RMSprop(learning_rate=0.001),

              metrics=['accuracy'])

Data Preprocessing
Now you will instantiate the data generators. As mentioned before, you will be compacting the
image by specifying the target_size parameter. See the simple change below:

from tensorflow.keras.preprocessing.image import ImageDataGenerator

# All images will be rescaled by 1./255

train_datagen = ImageDataGenerator(rescale=1/255)

validation_datagen = ImageDataGenerator(rescale=1/255)

# Flow training images in batches of 128 using train_datagen generator

train_generator = train_datagen.flow_from_directory(

        './horse-or-human/',  # This is the source directory for training images

        target_size=(150, 150),  # All images will be resized to 150x150

        batch_size=128,
        # Since you used binary_crossentropy loss, you need binary labels

        class_mode='binary')

# Flow training images in batches of 128 using train_datagen generator

validation_generator = validation_datagen.flow_from_directory(

        './validation-horse-or-human/',  # This is the source directory for training image

https://colab.research.google.com/drive/1vJlR0BHS0l27KoR-SgQ35iVD8F7ARPU4#scrollTo=xPv_aeoGFg0N&printMode=true 3/11
7/7/22, 12:03 Salazar Francisco C1_W4_Lab_3_compacted_images.ipynb - Colaboratory

        target_size=(150, 150),  # All images will be resized to 150x150

        batch_size=32,

        # Since you used binary_crossentropy loss, you need binary labels

        class_mode='binary')

Training
Now you're ready to train and see the results. Note your observations about how fast the model
trains and the accuracies you're getting in the train and validation sets.

history = model.fit(

      train_generator,

      steps_per_epoch=8,  

      epochs=15,

      verbose=1,

      validation_data = validation_generator,

      validation_steps=8)

Model Prediction
As usual, it is also good practice to try running your model over some handpicked images. See if
you got better, worse, or the same performance as the previous lab.

Important Note: Due to some compatibility issues, the following code block will result in an error
after you select the images(s) to upload if you are running this notebook as a Colab on the
Safari browser. For all other browsers, continue with the next code block and ignore the next
one after it.

For Safari users: please comment out or skip the code block below, uncomment the next code
block and run it.

## CODE BLOCK FOR NON-SAFARI BROWSERS

## SAFARI USERS: PLEASE SKIP THIS BLOCK AND RUN THE NEXT ONE INSTEAD

import numpy as np

from google.colab import files

from keras.preprocessing import image

uploaded = files.upload()

for fn in uploaded.keys():

  # predicting images

  path = '/content/' + fn

  img = image.load_img(path, target_size=(150, 150))

  x = image.img_to_array(img)

  x /= 255

  x = np.expand_dims(x, axis=0)

  images = np.vstack([x])

https://colab.research.google.com/drive/1vJlR0BHS0l27KoR-SgQ35iVD8F7ARPU4#scrollTo=xPv_aeoGFg0N&printMode=true 4/11
7/7/22, 12:03 Salazar Francisco C1_W4_Lab_3_compacted_images.ipynb - Colaboratory

  classes = model.predict(images, batch_size=10)

  print(classes[0])

  if classes[0]>0.5:

    print(fn + " is a human")

  else:

    print(fn + " is a horse")

Safari users will need to upload the images(s) manually in their workspace. Please follow the
instructions, uncomment the code block below and run it.

Instructions on how to upload image(s) manually in a Colab:

1. Select the folder icon on the left menu bar .


2. Click on the folder with an arrow pointing upwards named ..
3. Click on the folder named tmp .
4. Inside of the tmp folder, create a new folder called images . You'll see the New folder
option by clicking the 3 vertical dots menu button next to the tmp folder.
5. Inside of the new images folder, upload an image(s) of your choice, preferably of either a
horse or a human. Drag and drop the images(s) on top of the images folder.
6. Uncomment and run the code block below.

# # CODE BLOCK FOR SAFARI USERS

# import numpy as np

# from keras.preprocessing import image

# import os

# images = os.listdir("/tmp/images")

# print(images)

# for i in images:

#  print()

#  # predicting images

#  path = '/tmp/images/' + i

#  img = image.load_img(path, target_size=(150, 150))

#  x = image.img_to_array(img)

#  x /= 255

#  x = np.expand_dims(x, axis=0)

#  images = np.vstack([x])

#  classes = model.predict(images, batch_size=10)

#  print(classes[0])

#  if classes[0]>0.5:

#    print(i + " is a human")

#  else:

#    print(i + " is a horse")

https://colab.research.google.com/drive/1vJlR0BHS0l27KoR-SgQ35iVD8F7ARPU4#scrollTo=xPv_aeoGFg0N&printMode=true 5/11
7/7/22, 12:03 Salazar Francisco C1_W4_Lab_3_compacted_images.ipynb - Colaboratory

Visualizing Intermediate Representations


You can also look again at the intermediate representations. You will notice that the output at
the last convolution layer is even more abstract because it contains fewer pixels than before.

%matplotlib inline

import matplotlib.pyplot as plt

import numpy as np

import random

from tensorflow.keras.preprocessing.image import img_to_array, load_img

# Define a new Model that will take an image as input, and will output

# intermediate representations for all layers in the previous model after

# the first.

successive_outputs = [layer.output for layer in model.layers[1:]]

visualization_model = tf.keras.models.Model(inputs = model.input, outputs = successive_out

# Prepare a random input image from the training set.

horse_img_files = [os.path.join(train_horse_dir, f) for f in train_horse_names]

human_img_files = [os.path.join(train_human_dir, f) for f in train_human_names]

img_path = random.choice(horse_img_files + human_img_files)

img = load_img(img_path, target_size=(150, 150))  # this is a PIL image

x = img_to_array(img)  # Numpy array with shape (150, 150, 3)

x = x.reshape((1,) + x.shape)  # Numpy array with shape (1, 150, 150, 3)

# Scale by 1/255

x /= 255

# Run the image through the network, thus obtaining all

# intermediate representations for this image.

successive_feature_maps = visualization_model.predict(x)

# These are the names of the layers, so you can have them as part of the plot

layer_names = [layer.name for layer in model.layers[1:]]

# Display the representations

for layer_name, feature_map in zip(layer_names, successive_feature_maps):

  if len(feature_map.shape) == 4:

    # Just do this for the conv / maxpool layers, not the fully-connected layers

    n_features = feature_map.shape[-1]  # number of features in feature map

    # The feature map has shape (1, size, size, n_features)

    size = feature_map.shape[1]

    

    # Tile the images in this matrix

    display_grid = np.zeros((size, size * n_features))

    for i in range(n_features):

      x = feature_map[0, :, :, i]

      x -= x.mean()

      x /= x.std()

https://colab.research.google.com/drive/1vJlR0BHS0l27KoR-SgQ35iVD8F7ARPU4#scrollTo=xPv_aeoGFg0N&printMode=true 6/11
7/7/22, 12:03 Salazar Francisco C1_W4_Lab_3_compacted_images.ipynb - Colaboratory

      x *= 64

      x += 128

      x = np.clip(x, 0, 255).astype('uint8')

    

      # Tile each filter into this big horizontal grid

      display_grid[:, i * size : (i + 1) * size] = x

    

    # Display the grid

    scale = 20. / n_features

    plt.figure(figsize=(scale * n_features, scale))

    plt.title(layer_name)

    plt.grid(False)

    plt.imshow(display_grid, aspect='auto', cmap='viridis')

Clean Up
Please run the following cell to terminate the kernel and free memory resources:

import os, signal

os.kill(os.getpid(), signal.SIGKILL)

Wrap Up
In this lab, you saw how compacting images affected your previous model. This is one technique
to keep in mind especially when you are still in the exploratory phase of your own projects. You
can see if a smaller model behaves just as well as a large model so you can have faster training.
You also saw how easy it is to customize your images for this adjustment in size by simply
changing a parameter in the ImageDataGenerator class.

Actividad
1. Descargar el siguiente dataset: link dataset
2. Descomprir el archivo Images.zip
3. Agrupar las imágenes de acuerdo a su clase en 6 subdirectorios con nombre: blas, oxi, tae,
ophi, strong, rhy. Como regla general, la clase o label está codificada en la primera palabra
del nombre de archivo. Como excepción, note que existe imágenes que comienzan con tae
y tae2 pero que corresponden a la misma clase tae.
4. Subir el dataset con los subdirectorios a su Google Drive (subir comprimido en zip).
5. Conectar su Google Drive a Google Colab. Este paso es necesario para evitar subir el
dataset manualmente cada vez que se inicie una sesión en colab. Descomprimir el .zip de
Google Drive en el disco local de la máquina de Google Colab
6. Crear un Image Generator que apunte al directorio donde se encuentran las imágenes en
sus correspondientes 6 subdirectorios. Este será su generador para el conjunto de
https://colab.research.google.com/drive/1vJlR0BHS0l27KoR-SgQ35iVD8F7ARPU4#scrollTo=xPv_aeoGFg0N&printMode=true 7/11
7/7/22, 12:03 Salazar Francisco C1_W4_Lab_3_compacted_images.ipynb - Colaboratory

entrenamiento. Escalar las imágenes en el Image Generator de modo que tengan 100x100
pixeles. Modificar el Image Generator para el problema multiclase de este dataset. Esta
actividad no contempla el uso de un Image Generator de validación.
7. Crear una CNN con la misma arquitectura usada en esta notebook (ver arriba el código
fuente usado). Realice las modificaciones necesarias para que la CNN sea compatible con
su Image Generator
8. Entrenar la CNN usando el Image Generator creado durante 10 épocas. Si el
entrenamiento demora demasiado, reducir a 5 épocas.
9. Al finalizar el entrenamiento, evaluar el accuracy en el conjunto de entrenamiento.

#Conexión Colab a Drive

from google.colab import drive

drive.mount('/content/gdrive/', force_remount=True)

%cd gdrive/MyDrive/itinerarioIIproyectoI

Mounted at /content/gdrive/

/content/gdrive/MyDrive/itinerarioIIproyectoI

# Directorio que apunta al conjunto de entrenamiento

import os

# Directorio con nuestras fotos de la carpeta blas de entrenamiento

train_blas_dir = os.path.join('/content/gdrive/MyDrive/itinerarioIIproyectoI/dataset/blas'

# Directorio con nuestras fotos de la carpeta oxi de entrenamiento

train_oxi_dir = os.path.join('./dataset/oxi')

# Directorio con nuestras fotos de la carpeta tae de entrenamiento

train_tae_dir = os.path.join('./dataset/tae')

# Directorio con nuestras fotos de la carpeta ophi de entrenamiento

train_ophi_dir = os.path.join('./dataset/ophi')

# Directorio con nuestras fotos de la carpeta strong de entrenamiento

train_strong_dir = os.path.join('./dataset/strong')

# Directorio con nuestras fotos de la carpeta rhy de entrenamiento

train_rhy_dir = os.path.join('./dataset/rhy')

# Número total de imágenes de cada subdirectorio:

print('imágenes totales del subdirectorio blas de entrenamiento:', len(os.listdir(train_bl
print('imágenes totales del subdirectorio oxi de entrenamiento:', len(os.listdir(train_oxi_
print('imágenes totales del subdirectorio tae de entrenamiento:', len(os.listdir(train_tae_
print('imágenes totales del subdirectorio ophi de entrenamiento:', len(os.listdir(train_op

https://colab.research.google.com/drive/1vJlR0BHS0l27KoR-SgQ35iVD8F7ARPU4#scrollTo=xPv_aeoGFg0N&printMode=true 8/11
7/7/22, 12:03 Salazar Francisco C1_W4_Lab_3_compacted_images.ipynb - Colaboratory

print('imágenes totales del subdirectorio strong de entrenamiento:', len(os.listdir(train_
print('imágenes totales del subdirectorio rhy de entrenamiento:', len(os.listdir(train_rhy_

imágenes totales del subdirectorio blas de entrenamiento: 950

imágenes totales del subdirectorio oxi de entrenamiento: 346

imágenes totales del subdirectorio tae de entrenamiento: 357

imágenes totales del subdirectorio ophi de entrenamiento: 246

imágenes totales del subdirectorio strong de entrenamiento: 649

imágenes totales del subdirectorio rhy de entrenamiento: 1073

# PASO 2 Preprocesamiento de datos, train generator y escalada a 100x100

from tensorflow.keras.preprocessing.image import ImageDataGenerator

# All images will be rescaled by 1./255
train_datagen = ImageDataGenerator(rescale=1/255)

# Flow training images in batches of 128 using train_datagen generator
train_generator = train_datagen.flow_from_directory(
        './dataset/',  # This is the source directory for training images
        target_size=(100, 100),  # All images will be resized to 100x100
        batch_size=128,
        # Since we use binary_crossentropy loss, we need binary labels
        class_mode='binary')

Found 3616 images belonging to 6 classes.

#PASO 3 Cración CNN

import tensorflow as tf

model = tf.keras.models.Sequential([
    # Note the input shape is the desired size of the image 100x100 with 3 bytes color
    # This is the first convolution
    tf.keras.layers.Conv2D(16, (3,3), activation='relu', input_shape=(100, 100, 3)),
    tf.keras.layers.MaxPooling2D(2, 2),
    # The second convolution
    tf.keras.layers.Conv2D(32, (3,3), activation='relu'),
    tf.keras.layers.MaxPooling2D(2,2),
    # The third convolution
    tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
    tf.keras.layers.MaxPooling2D(2,2),
#     # The fourth convolution (You can uncomment the 4th and 5th conv layers later to see 
#     tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
#     tf.keras.layers.MaxPooling2D(2,2),
#     # The fifth convolution
#     tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
#     tf.keras.layers.MaxPooling2D(2,2),
    # Flatten the results to feed into a DNN
    tf.keras.layers.Flatten(),
    # 512 neuron hidden layer
    tf.keras.layers.Dense(512, activation='relu'),
    # Only 1 output neuron. It will contain a value from 0-1 where 0 for 1 class ('horses'
    tf.keras.layers.Dense(1, activation='sigmoid')
])

https://colab.research.google.com/drive/1vJlR0BHS0l27KoR-SgQ35iVD8F7ARPU4#scrollTo=xPv_aeoGFg0N&printMode=true 9/11
7/7/22, 12:03 Salazar Francisco C1_W4_Lab_3_compacted_images.ipynb - Colaboratory

# Arquitectura de red y formas de salida

model.summary()

Model: "sequential"

_________________________________________________________________

Layer (type) Output Shape Param #

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

conv2d (Conv2D) (None, 98, 98, 16) 448

max_pooling2d (MaxPooling2D (None, 49, 49, 16) 0

conv2d_1 (Conv2D) (None, 47, 47, 32) 4640

max_pooling2d_1 (MaxPooling (None, 23, 23, 32) 0

2D)

conv2d_2 (Conv2D) (None, 21, 21, 64) 18496

max_pooling2d_2 (MaxPooling (None, 10, 10, 64) 0

2D)

flatten (Flatten) (None, 6400) 0

dense (Dense) (None, 512) 3277312

dense_1 (Dense) (None, 1) 513

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

Total params: 3,301,409

Trainable params: 3,301,409

Non-trainable params: 0

_________________________________________________________________

#PASO 4 Entrenamiento CNN

from tensorflow.keras.optimizers import RMSprop

model.compile(loss='binary_crossentropy',

              optimizer=RMSprop(learning_rate=0.001),

              metrics=['accuracy'])

history = model.fit(

      train_generator,

      steps_per_epoch=8,  

      epochs=15,

      verbose=1)

Epoch 1/15

8/8 [==============================] - 132s 15s/step - loss: -1875.0579 - accuracy: 0


Epoch 2/15

8/8 [==============================] - 94s 12s/step - loss: -50848.0117 - accuracy: 0


Epoch 3/15

8/8 [==============================] - 77s 9s/step - loss: -375648.5000 - accuracy: 0


Epoch 4/15

8/8 [==============================] - 56s 7s/step - loss: -1332631.2500 - accuracy:


https://colab.research.google.com/drive/1vJlR0BHS0l27KoR-SgQ35iVD8F7ARPU4#scrollTo=xPv_aeoGFg0N&printMode=true 10/11
7/7/22, 12:03 Salazar Francisco C1_W4_Lab_3_compacted_images.ipynb - Colaboratory

Epoch 5/15

8/8 [==============================] - 43s 6s/step - loss: -3439160.5000 - accuracy:


Epoch 6/15

8/8 [==============================] - 32s 4s/step - loss: -6767718.5000 - accuracy:


Epoch 7/15

8/8 [==============================] - 26s 3s/step - loss: -12906124.0000 - accuracy


Epoch 8/15

8/8 [==============================] - 23s 3s/step - loss: -20874626.0000 - accuracy


Epoch 9/15

8/8 [==============================] - 20s 2s/step - loss: -32254486.0000 - accuracy


Epoch 10/15

8/8 [==============================] - 19s 2s/step - loss: -47864604.0000 - accuracy


Epoch 11/15

8/8 [==============================] - 17s 2s/step - loss: -64943448.0000 - accuracy


Epoch 12/15

8/8 [==============================] - 14s 2s/step - loss: -86599432.0000 - accuracy


Epoch 13/15

8/8 [==============================] - 13s 2s/step - loss: -139284672.0000 - accuracy


Epoch 14/15

8/8 [==============================] - 11s 1s/step - loss: -186142368.0000 - accuracy


Epoch 15/15

8/8 [==============================] - 13s 2s/step - loss: -231006720.0000 - accuracy

#PASO 5 Evaluación accuracy del conjunto de entrenamiento

h = history.history['accuracy']
h = sum(h)/len(h)
print('Accuracy promedio: ',h)

Accuracy promedio: 0.06833468054731687

check 0 s completado a las 12:03

https://colab.research.google.com/drive/1vJlR0BHS0l27KoR-SgQ35iVD8F7ARPU4#scrollTo=xPv_aeoGFg0N&printMode=true 11/11

You might also like