<jupyter_start><jupyter_text>Import Libraries<jupyter_code>import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import os<jupyter_output><empty_output><jupyter_text>Importing dataset<jupyter_code>from keras.datasets import cifar10
(x_train, y_train), (x_test,y_test) = cifar10.load_data()
print(x_train.shape,y_train.shape)
print(x_test.shape,y_test.shape)
print(y_test)
NUM_CLASSES = 10
cifar10_classes = ["airplane", "automobile", "bird", "cat", "deer", 
                   "dog", "frog", "horse", "ship", "truck"]<jupyter_output><empty_output><jupyter_text>Normalising The input<jupyter_code>x_train = x_train/255.0
x_test = x_test/255.0<jupyter_output><empty_output><jupyter_text>Creating a Model  Don't use Dropout in Convolution layers. Use them only in Fully connected layers(0.5-0.7). If adding in conv layer, use smaller values(0.1-0.2).<jupyter_code>model = tf.keras.Sequential([
        tf.keras.layers.Conv2D(64,(3,3),activation='relu',input_shape=(32,32,3)),
        tf.keras.layers.BatchNormalization(),
        tf.keras.layers.MaxPooling2D(2,2),

        tf.keras.layers.Conv2D(128,(3,3),activation='relu'),
        tf.keras.layers.BatchNormalization(),
        tf.keras.layers.MaxPooling2D(2,2),

        tf.keras.layers.Conv2D(256,(3,3),activation='relu'),
        tf.keras.layers.BatchNormalization(),
        tf.keras.layers.MaxPooling2D(2,2),

        # tf.keras.layers.Conv2D(512,(1,1),activation='relu'),
        # tf.keras.layers.BatchNormalization(),
        # tf.keras.layers.MaxPooling2D(2,2),

        tf.keras.layers.Flatten(),
        tf.keras.layers.Dense(128,activation='relu'),
        tf.keras.layers.Dropout(0.4),
        tf.keras.layers.BatchNormalization(),

        tf.keras.layers.Dense(256,activation='relu'),
        tf.keras.layers.Dropout(0.4),
        tf.keras.layers.BatchNormalization(),

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

        tf.keras.layers.Dense(1024,activation='relu'),
        tf.keras.layers.Dropout(0.4),
        tf.keras.layers.BatchNormalization(),

        tf.keras.layers.Dense(10,activation='softmax')
        ]
    )
model.summary()<jupyter_output>Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d (Conv2D)              (None, 30, 30, 64)        1792      
_________________________________________________________________
batch_normalization (BatchNo (None, 30, 30, 64)        256       
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 15, 15, 64)        0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 13, 13, 128)       73856     
_________________________________________________________________
batch_normalization_1 (Batch (None, 13, 13, 128)       512       
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 6, 6, 128)         0         
________________________________________________________[...]<jupyter_text>Compiling the Model<jupyter_code>model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['acc'])<jupyter_output><empty_output><jupyter_text>Defining Callback Function<jupyter_code>class myCallBack(tf.keras.callbacks.Callback):
  def on_epoch_end(self,epoch,logs={}):
    if(logs.get('acc')>0.95):
      self.model_stop_training = True
callbacks = myCallBack()<jupyter_output><empty_output><jupyter_text>Fitting the model<jupyter_code>history = model.fit(
            x = x_train, y = y_train,
            epochs = 30,
            validation_data = (x_test,y_test),
            verbose = 1,
            callbacks = [callbacks]
          )<jupyter_output>Epoch 1/30
1563/1563 [==============================] - 14s 9ms/step - loss: 1.8937 - acc: 0.3410 - val_loss: 1.4946 - val_acc: 0.4605
Epoch 2/30
1563/1563 [==============================] - 14s 9ms/step - loss: 1.3857 - acc: 0.5110 - val_loss: 1.3916 - val_acc: 0.5039
Epoch 3/30
1563/1563 [==============================] - 13s 9ms/step - loss: 1.1898 - acc: 0.5867 - val_loss: 1.1166 - val_acc: 0.6096
Epoch 4/30
1563/1563 [==============================] - 13s 9ms/step - loss: 1.0464 - acc: 0.6445 - val_loss: 1.0303 - val_acc: 0.6495
Epoch 5/30
1563/1563 [==============================] - 13s 9ms/step - loss: 0.9531 - acc: 0.6782 - val_loss: 1.0347 - val_acc: 0.6482
Epoch 6/30
1563/1563 [==============================] - 14s 9ms/step - loss: 0.8507 - acc: 0.7172 - val_loss: 1.1280 - val_acc: 0.6273
Epoch 7/30
1563/1563 [==============================] - 13s 9ms/step - loss: 0.7731 - acc: 0.7445 - val_loss: 1.0475 - val_acc: 0.6423
Epoch 8/30
1563/1563 [==============================] -[...]<jupyter_text>Visualising our model accuracy<jupyter_code>import matplotlib.pyplot as plt
acc = history.history['acc']
val_acc = history.history['val_acc']
loss = history.history['loss']
val_loss = history.history['val_loss']

epochs = range(len(acc))

plt.plot(epochs, acc, 'b', label='Training accuracy')
plt.plot(epochs, val_acc, 'r', label='Validation accuracy')
plt.title('Training and validation accuracy')
plt.legend()

plt.figure()

plt.plot(epochs, loss, 'b', label='Training Loss')
plt.plot(epochs, val_loss, 'r', label='Validation Loss')
plt.title('Training and validation loss')
plt.legend()

plt.show()<jupyter_output><empty_output><jupyter_text>Totally Unexpected behavior(Increasing loss and decreasing accuracy) encountered earlier was due to wrong loss function and wrong ImageDataGenerator definition  Introducing Data Augmentation (it might help)<jupyter_code>(x_train, y_train), (x_test,y_test) = cifar10.load_data()
train_datagen = ImageDataGenerator(
                  rescale = 1.0/255,
                  horizontal_flip = True,
                  rotation_range = 20,
                  shear_range = 0.15,
                  width_shift_range = 0.1,
                  height_shift_range = 0.1,
                  zoom_range = 0.2 
                )
training_generator = train_datagen.flow(
                  x_train,
                  y_train,
                  batch_size = 20
                )
validation_datagen = ImageDataGenerator(
                  rescale = 1.0/255 
                )
validation_generator = validation_datagen.flow(
                  x_test,
                  y_test,
                  batch_size = 20
                )
model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['acc'])
history = model.fit(
            training_generator,
            epochs = 10,
            steps_per_epoch = 50000//20,
            validation_data = validation_generator,
            validation_steps =  10000//20,
            verbose = 1
          )

import matplotlib.pyplot as plt
acc = history.history['acc']
val_acc = history.history['val_acc']
loss = history.history['loss']
val_loss = history.history['val_loss']

epochs = range(len(acc))

plt.plot(epochs, acc, 'b', label='Training accuracy')
plt.plot(epochs, val_acc, 'r', label='Validation accuracy')
plt.title('Training and validation accuracy')
plt.legend()

plt.figure()

plt.plot(epochs, loss, 'b', label='Training Loss')
plt.plot(epochs, val_loss, 'r', label='Validation Loss')
plt.title('Training and validation loss')
plt.legend()

plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16224814422057265, 'fraction_numerical': 0.059782608695652176, 'mean_word_length': 3.5044776119402985, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '392431', 'license': 'MIT', 'n_tokens_mistral': 2773, 'n_tokens_neox': 2420, 'n_words': 429, 'path': '4. CIFAR-10/CIFAR_10.ipynb', 'repo_name': 'ankitgoyal0301/Deep-Learning', 'size': '7544'}