pdfcoffee

soumyasankar99
from soumyasankar99 More from this publisher
09.05.2023 Views

Chapter 4Figure 12: An example of CIFAR-10 imagesThe goal is to recognize previously unseen images and assign them to one of the 10classes. Let us define a suitable deep net.First of all, we import a number of useful modules, define a few constants, and loadthe dataset (the full code including the load operations is available online):import tensorflow as tffrom tensorflow.keras import datasets, layers, models, optimizers# CIFAR_10 is a set of 60K images 32x32 pixels on 3 channelsIMG_CHANNELS = 3IMG_ROWS = 32IMG_COLS = 32# constantBATCH_SIZE = 128EPOCHS = 20CLASSES = 10VERBOSE = 1VALIDATION_SPLIT = 0.2OPTIM = tf.keras.optimizers.RMSprop()[ 123 ]

Convolutional Neural NetworksOur net will learn 32 convolutional filters, each with a 3×3 size. The output dimensionis the same one as the input shape, so it will be 32×32 and the activation function usedis a ReLU function, which is a simple way of introducing non-linearity. After that wehave a max pooling operation with pool size 2×2 and a Dropout of 25%:# define the convnetdef build(input_shape, classes):model = models.Sequential()model.add(layers.Convolution2D(32, (3, 3), activation='relu',input_shape=input_shape))model.add(layers.MaxPooling2D(pool_size=(2, 2)))model.add(layers.Dropout(0.25))The next stage in the deep pipeline is a dense network with 512 units and ReLUactivation followed by a dropout at 50% and by a softmax layer with 10 classesas output, one for each category:model.add(layers.Flatten())model.add(layers.Dense(512, activation='relu'))model.add(layers.Dropout(0.5))model.add(layers.Dense(classes, activation='softmax'))return modelAfter defining the network, we can train the model. In this case, we split the dataand compute a validation set in addition to the training and testing sets. The trainingis used to build our models, the validation is used to select the best performingapproach, while the test set is used to check the performance of our best modelson fresh, unseen data:# use TensorBoard, princess Aurora!callbacks = [# Write TensorBoard logs to './logs' directorytf.keras.callbacks.TensorBoard(log_dir='./logs')]# trainmodel.compile(loss='categorical_crossentropy', optimizer=OPTIM,metrics=['accuracy'])model.fit(X_train, y_train, batch_size=BATCH_SIZE,epochs=EPOCHS, validation_split=VALIDATION_SPLIT,verbose=VERBOSE, callbacks=callbacks)score = model.evaluate(X_test, y_test,batch_size=BATCH_SIZE, verbose=VERBOSE)print("\nTest score:", score[0])print('Test accuracy:', score[1])[ 124 ]

Chapter 4

Figure 12: An example of CIFAR-10 images

The goal is to recognize previously unseen images and assign them to one of the 10

classes. Let us define a suitable deep net.

First of all, we import a number of useful modules, define a few constants, and load

the dataset (the full code including the load operations is available online):

import tensorflow as tf

from tensorflow.keras import datasets, layers, models, optimizers

# CIFAR_10 is a set of 60K images 32x32 pixels on 3 channels

IMG_CHANNELS = 3

IMG_ROWS = 32

IMG_COLS = 32

# constant

BATCH_SIZE = 128

EPOCHS = 20

CLASSES = 10

VERBOSE = 1

VALIDATION_SPLIT = 0.2

OPTIM = tf.keras.optimizers.RMSprop()

[ 123 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!