<jupyter_start><jupyter_text>Introducing CNNS with TensorFlow  Load libraries<jupyter_code># data tools
import os
import numpy as np
import matplotlib.pyplot as plt

# sklearn tools
from sklearn.preprocessing import LabelBinarizer
from sklearn.metrics import classification_report

# tf tools
from tensorflow.keras.datasets import cifar10
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import (Conv2D, 
                                     MaxPooling2D, 
                                     Activation, 
                                     Flatten, 
                                     Dense)
from tensorflow.keras.utils import plot_model
from tensorflow.keras.optimizers import SGD
from tensorflow.keras import backend as K<jupyter_output><empty_output><jupyter_text>__Define plotting function__<jupyter_code>def plot_history(H, epochs):
    plt.style.use("seaborn-colorblind")

    plt.figure(figsize=(12,6))
    plt.subplot(1,2,1)
    plt.plot(np.arange(0, epochs), H.history["loss"], label="train_loss")
    plt.plot(np.arange(0, epochs), H.history["val_loss"], label="val_loss", linestyle=":")
    plt.title("Loss curve")
    plt.xlabel("Epoch")
    plt.ylabel("Loss")
    plt.tight_layout()
    plt.legend()

    plt.subplot(1,2,2)
    plt.plot(np.arange(0, epochs), H.history["accuracy"], label="train_acc")
    plt.plot(np.arange(0, epochs), H.history["val_accuracy"], label="val_acc", linestyle=":")
    plt.title("Accuracy curve")
    plt.xlabel("Epoch")
    plt.ylabel("Accuracy")
    plt.tight_layout()
    plt.legend()
    plt.show()<jupyter_output><empty_output><jupyter_text>__Load cifar10 data__<jupyter_code>(X_train, y_train), (X_test, y_test) = cifar10.load_data()
X_train = X_train/255
X_test = X_test/255
# create one-hot encodings
lb = LabelBinarizer()
y_train = lb.fit_transform(y_train)
y_test = lb.fit_transform(y_test)
# initialize label names for CIFAR-10 dataset
label_names = ["airplane", "automobile", 
              "bird", "cat", 
              "deer", "dog", 
              "frog", "horse", 
              "ship", "truck"]<jupyter_output><empty_output><jupyter_text>ShallowNet  ShallowNet architecture: >INPUT => CONV => ReLU => FC<jupyter_code>#initalise model
model = Sequential()

# define CONV => ReLU
model.add(Conv2D(32, 
                (3,3),
                padding = "same",
                input_shape = (32, 32, 3)))
model.add(Activation("relu"))
          
# FC classifier
model.add(Flatten())
model.add(Dense(128))
model.add(Activation("relu"))
model.add(Dense(10))
model.add(Activation("softmax"))<jupyter_output><empty_output><jupyter_text>__Compile model__<jupyter_code>sgd = SGD(learning_rate =.001)
model.compile(loss="categorical_crossentropy",
              optimizer=sgd,
              metrics=["accuracy"])<jupyter_output><empty_output><jupyter_text>__Model summary__<jupyter_code>model.summary()<jupyter_output>Model: "sequential_4"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 conv2d_4 (Conv2D)           (None, 32, 32, 32)        896       
                                                                 
 activation_6 (Activation)   (None, 32, 32, 32)        0         
                                                                 
 flatten_4 (Flatten)         (None, 32768)             0         
                                                                 
 dense_4 (Dense)             (None, 128)               4194432   
                                                                 
 activation_7 (Activation)   (None, 128)               0         
                                                                 
 dense_5 (Dense)             (None, 10)                1290      
                                                      [...]<jupyter_text>__Plot model__<jupyter_code>plot_model(model, show_shapes=True, show_layer_names=True)<jupyter_output><empty_output><jupyter_text>__Train model__<jupyter_code>H = model.fit(X_train, y_train, 
              validation_data=(X_test, y_test), 
              batch_size=32,
              epochs=20,
              verbose=1)<jupyter_output>Epoch 1/20
1563/1563 [==============================] - 13s 8ms/step - loss: 2.0690 - accuracy: 0.2681 - val_loss: 1.9191 - val_accuracy: 0.3256
Epoch 2/20
1563/1563 [==============================] - 12s 8ms/step - loss: 1.8518 - accuracy: 0.3537 - val_loss: 1.8084 - val_accuracy: 0.3694
Epoch 3/20
1563/1563 [==============================] - 12s 8ms/step - loss: 1.7792 - accuracy: 0.3766 - val_loss: 1.7560 - val_accuracy: 0.3862
Epoch 4/20
1563/1563 [==============================] - 12s 8ms/step - loss: 1.7274 - accuracy: 0.3994 - val_loss: 1.6970 - val_accuracy: 0.4097
Epoch 5/20
1563/1563 [==============================] - 12s 8ms/step - loss: 1.6865 - accuracy: 0.4115 - val_loss: 1.6742 - val_accuracy: 0.4133
Epoch 6/20
1563/1563 [==============================] - 12s 8ms/step - loss: 1.6499 - accuracy: 0.4265 - val_loss: 1.6321 - val_accuracy: 0.4266
Epoch 7/20
1563/1563 [==============================] - 12s 8ms/step - loss: 1.6161 - accuracy: 0.4362 - val_loss: 1.5981 - val_ac[...]<jupyter_text>__Evaluate model__<jupyter_code>predictions = model.predict(X_test, batch_size=32)
print(classification_report(y_test.argmax(axis=1),
                            predictions.argmax(axis=1),
                            target_names=label_names))
plot_history(H, 20)<jupyter_output><empty_output><jupyter_text>LeNet LeNet architecture:>INPUT => CONV => ReLU => MAXPOOL => CONV => ReLU => MAXPOOL => FC => ReLU => FC __Define model__<jupyter_code># define model
model = Sequential()

# first set of layers CONV => RELU => MAXPOOL
model.add(Conv2D(32, (3,3),
                 padding="same",
                 input_shape=(32,32,3)))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size = (2,2),
                       strides = (2,2)))

# second set of layers CONV => RELU => MAXPOOL
model.add(Conv2D(50, (5,5),
                 padding="same"))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size = (2,2),
                       strides = (2,2)))

# FC => RELU
model.add(Flatten())
model.add(Dense(128))
model.add(Activation("relu"))

# softmax classifier
model.add(Dense(10))
model.add(Activation("softmax"))<jupyter_output><empty_output><jupyter_text>__Compile model__<jupyter_code>sgd = SGD(learning_rate=0.001)
model.compile(loss="categorical_crossentropy",
              optimizer=sgd,
              metrics=["accuracy"])<jupyter_output><empty_output><jupyter_text>__Model summary__<jupyter_code>model.summary()<jupyter_output>Model: "sequential_6"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 conv2d_7 (Conv2D)           (None, 32, 32, 32)        896       
                                                                 
 activation_13 (Activation)  (None, 32, 32, 32)        0         
                                                                 
 max_pooling2d_2 (MaxPooling  (None, 16, 16, 32)       0         
 2D)                                                             
                                                                 
 conv2d_8 (Conv2D)           (None, 16, 16, 50)        40050     
                                                                 
 activation_14 (Activation)  (None, 16, 16, 50)        0         
                                                                 
 max_pooling2d_3 (MaxPooling  (None, 8, 8, 50)        [...]<jupyter_text>__Train model__<jupyter_code># train model
H = model.fit(X_train, y_train, 
              validation_data=(X_test, y_test), 
              batch_size=32,
              epochs=20,
              verbose=1)
plot_history(H,20)
predictions = model.predict(X_test, batch_size=32)
print(classification_report(y_test.argmax(axis=1),
                            predictions.argmax(axis=1),
                            target_names=label_names))<jupyter_output>precision    recall  f1-score   support

    airplane       0.62      0.58      0.60      1000
  automobile       0.63      0.68      0.65      1000
        bird       0.43      0.42      0.43      1000
         cat       0.41      0.37      0.39      1000
        deer       0.53      0.39      0.45      1000
         dog       0.51      0.42      0.46      1000
        frog       0.62      0.65      0.64      1000
       horse       0.49      0.73      0.58      1000
        ship       0.62      0.68      0.65      1000
       truck       0.61      0.56      0.59      1000

    accuracy                           0.55     10000
   macro avg       0.55      0.55      0.54     10000
weighted avg       0.55      0.55      0.54     10000
{'fraction_non_alphanumeric': 0.14335273052820055, 'fraction_numerical': 0.06882273948075202, 'mean_word_length': 1.934975369458128, 'pattern_counts': {'":': 2, '<': 49, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 30, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '508959', 'license': 'MIT', 'n_tokens_mistral': 3152, 'n_tokens_neox': 2707, 'n_words': 517, 'path': 'notebooks/session9_inclass_rdkm.ipynb', 'repo_name': 'Rysias/cds-visual', 'size': '8936'}