<jupyter_start><jupyter_text>**Classification Model for CIFAR 100**<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>Importing the **CIFAR 100** dataset<jupyter_code>from tensorflow.keras.datasets import cifar100<jupyter_output><empty_output><jupyter_text>Splitting the dataset into Train and Test sets<jupyter_code>(x_train,y_train),(x_test,y_test) = cifar100.load_data()<jupyter_output><empty_output><jupyter_text>Some random images from the CIFAR 100 dataset.Shape of every image is ( 32 x 32 x 3 )<jupyter_code>fig, axes1 = plt.subplots(5,5,figsize=(8,8))
for j in range(5):
    for k in range(5):
        i = np.random.choice(range(len(x_train)))
        axes1[j][k].set_axis_off()
        axes1[j][k].imshow(x_train[i:i+1][0])
print('Maximum Value: ',x_train.max())
print('Shape of image: ',x_train.shape[1:])<jupyter_output>Maximum Value:  255
Shape of image:  (32, 32, 3)<jupyter_text>Preprcessing the training and test sets inorder to have values of the array of images between 0 and 1.<jupyter_code>x_train = x_train/255
x_test = x_test/255
print('Maximum Value: ',x_train.max())
print('Shape of label: ',y_train.shape[1:])<jupyter_output>Shape of label:  (1,)<jupyter_text>One Hot Encoding the labels of the image since classification is a categorical task.The cell transforms single column of the label y into 100 columns since there are total 100 different classes in the dataset.<jupyter_code>from tensorflow.keras.utils import to_categorical
y_cat_train = to_categorical(y_train)
y_cat_test = to_categorical(y_test)
print('Shape of label: ',y_train.shape[1:])<jupyter_output>Shape of label:  (100,)<jupyter_text>Defining the input shape.The images already of the shape (32 x 32 x 3) but it is good practice to define is separately. It is helpful during model creation and training.<jupyter_code>input_shape = (32,32,3)<jupyter_output><empty_output><jupyter_text>Important imports for creating the model.<jupyter_code>from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D,Flatten,MaxPooling2D,Dense
from tensorflow.keras.layers import Dropout,ELU,LeakyReLU,BatchNormalization<jupyter_output><empty_output><jupyter_text>Creating the model.<jupyter_code>model = Sequential()
model.add(Conv2D(filters=128, kernel_size=(2,2),activation='relu',input_shape=input_shape))
model.add(Conv2D(filters=128, kernel_size=(2,2),activation='relu'))
model.add(Conv2D(filters=64, kernel_size=(2,2),activation='relu'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2,2)))

model.add(Conv2D(filters=32, kernel_size=(2,2),activation=LeakyReLU(0.1),input_shape=input_shape))
model.add(Conv2D(filters=32, kernel_size=(2,2),activation=LeakyReLU(0.1),input_shape=input_shape))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(MaxPooling2D(pool_size=(2,2)))

model.add(Flatten())

model.add(Dense(units=512,activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(units=125,activation='relu'))
model.add(Dropout(0.15))
model.add(Dense(units=100,activation='softmax'))
model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])

model.summary()<jupyter_output>Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d (Conv2D)              (None, 31, 31, 128)       1664      
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 30, 30, 128)       65664     
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 29, 29, 64)        32832     
_________________________________________________________________
batch_normalization (BatchNo (None, 29, 29, 64)        256       
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 14, 14, 64)        0         
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 13, 13, 32)        8224      
________________________________________________________[...]<jupyter_text>Instance of the Early Stopping<jupyter_code>from tensorflow.keras.callbacks import EarlyStopping
early_stop = EarlyStopping(monitor='val_loss',patience=5)<jupyter_output><empty_output><jupyter_text>Fitting the training set to the model and validating the model on the test set.<jupyter_code>model.fit(x=x_train,y=y_cat_train,epochs=50,validation_data=(x_test,y_cat_test),callbacks=[early_stop])<jupyter_output>Epoch 1/50
1563/1563 [==============================] - 39s 25ms/step - loss: 4.0264 - accuracy: 0.0846 - val_loss: 3.6731 - val_accuracy: 0.1396
Epoch 2/50
1563/1563 [==============================] - 39s 25ms/step - loss: 3.5370 - accuracy: 0.1564 - val_loss: 3.5345 - val_accuracy: 0.1670
Epoch 3/50
1563/1563 [==============================] - 39s 25ms/step - loss: 3.3112 - accuracy: 0.1989 - val_loss: 3.1168 - val_accuracy: 0.2425
Epoch 4/50
1563/1563 [==============================] - 39s 25ms/step - loss: 3.1717 - accuracy: 0.2241 - val_loss: 2.9470 - val_accuracy: 0.2728
Epoch 5/50
1563/1563 [==============================] - 39s 25ms/step - loss: 3.0609 - accuracy: 0.2468 - val_loss: 2.8691 - val_accuracy: 0.2921
Epoch 6/50
1563/1563 [==============================] - 39s 25ms/step - loss: 2.9693 - accuracy: 0.2614 - val_loss: 2.8405 - val_accuracy: 0.2944
Epoch 7/50
1563/1563 [==============================] - 39s 25ms/step - loss: 2.8769 - accuracy: 0.2800 - val_loss: 2.8233 -[...]<jupyter_text>The list of losses and accuracies of each epoch during the training.<jupyter_code>results = pd.DataFrame(model.history.history)
results[['loss','val_loss']].plot()<jupyter_output><empty_output><jupyter_text>Looking at the graphs we can say that the model is not overfitting.<jupyter_code>results[['accuracy','val_accuracy']].plot()<jupyter_output><empty_output><jupyter_text>Saving the model.<jupyter_code>model.save('CIFAR_100_Classifier_model.h5')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16010371090584993, 'fraction_numerical': 0.07227353751417923, 'mean_word_length': 6.454106280193237, 'pattern_counts': {'":': 0, '<': 52, '<?xml version=': 0, '>': 52, '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': '14', 'dir': 'jupyter-structured-clean-dedup', 'id': '590298', 'license': 'Apache-2.0', 'n_tokens_mistral': 2357, 'n_tokens_neox': 2042, 'n_words': 417, 'path': 'CIFAR_100_Classifier/CIFAR_100_CNN.ipynb', 'repo_name': 'kushagras71/CNN_models', 'size': '6171'}