<jupyter_start><jupyter_text>Image classifier for the The Street View House Numbers (SVHN) Dataset In this notebook, you will use a neural network that classifies real-world images digits. Youwill use concepts from throughout this course to run different part of model on either CPU or GPU and compare the results.<jupyter_code>import tensorflow as tf
import time
from scipy.io import loadmat
!wget http://ufldl.stanford.edu/housenumbers/train_32x32.mat
!wget http://ufldl.stanford.edu/housenumbers/test_32x32.mat
train = loadmat('train_32x32.mat')
test = loadmat('test_32x32.mat')
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from tensorflow.keras.models import Sequential, load_model
from tensorflow.keras.layers import Dense, Flatten, Conv2D, MaxPooling2D, BatchNormalization, Dropout
from tensorflow.keras.callbacks import ModelCheckpoint, ReduceLROnPlateau, EarlyStopping

%matplotlib inline
x_train = train['X']/255.
y_train = train['y']

x_test = test['X']/255.
y_test = test['y']
y_train[y_train == 10] = 0
y_test[y_test == 10] = 0
num_plts = 15
fig, ax = plt.subplots(1, num_plts, figsize=(num_plts, 1))
im_set = np.random.random_integers(1, x_train.shape[3], num_plts)

for i in range(len(im_set)):
    ax[i].imshow(x_train[:,:,:,im_set[i]])
    ax[i].axis('off')
    ax[i].set_title(y_train[im_set[i]][0])

plt.show()
x_train_gs = np.mean(x_train, axis = 2, keepdims=True)
x_train_gs = x_train_gs.transpose(3,0,1,2)
x_test_gs = np.mean(x_test, axis = 2, keepdims=True)
x_test_gs = x_test_gs.transpose(3,0,1,2)
fig, ax = plt.subplots(1, num_plts, figsize=(num_plts, 1))
for i in range(len(im_set)):
    ax[i].imshow(x_train_gs[im_set[i],:,:,0], cmap = 'gray')
    ax[i].axis('off')
    ax[i].set_title(y_train[im_set[i]][0])

plt.show()<jupyter_output><empty_output><jupyter_text>MLP neural network classi<jupyter_code>def get_model(input_shape, dropout_rate, nn_units):
    model = Sequential([Flatten(input_shape = input_shape),
                        Dense(nn_units, activation = 'relu', kernel_initializer = 'he_uniform'),
                        Dropout(dropout_rate),
                        Dense(nn_units, activation = 'relu', kernel_initializer = 'he_uniform'),
                        Dropout(dropout_rate),
                        Dense(nn_units, activation = 'relu', kernel_initializer = 'he_uniform'),
                        Dropout(dropout_rate),
                        Dense(nn_units, activation = 'relu', kernel_initializer = 'he_uniform'),
                        Dropout(dropout_rate),
                        Dense(10, activation = 'softmax')])
    model.compile(optimizer = 'adam', loss = 'sparse_categorical_crossentropy', metrics=["accuracy"])
    
    return model
model_mlp = get_model(x_train_gs.shape[1:4],0,64)
model_mlp.summary()
checkpoint_best_path = 'mlp_chkpnts_bst/chkpnt'
checkpoint_best = ModelCheckpoint(filepath=checkpoint_best_path, save_weights_only=True, monitor='val_accuracy', save_best_only = True, verbose = 1)
earlystop = EarlyStopping(monitor='loss', patience = 5)
epochs = 32
batch_size = 2*64
start = time.time()
history_mlp = model_mlp.fit(x_train_gs, y_train, epochs = epochs, batch_size=batch_size, 
                        validation_split = 0.15, callbacks=[checkpoint_best, earlystop])
endt = time.time()-start
print("Time for {} epochs: {:0.2f}ms".format(epochs,1000*endt))
plt.plot(history_mlp.history['loss'])
plt.plot(history_mlp.history['val_loss'])
plt.title('Loss vs. epochs')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['Training', 'Validation'], loc='upper right')
plt.show()
plt.plot(history_mlp.history['accuracy'])
plt.plot(history_mlp.history['val_accuracy'])
plt.title('Accuracy vs. epochs')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['Training', 'Validation'], loc='lower right')
plt.show()<jupyter_output><empty_output><jupyter_text>CNN neural network classifier<jupyter_code>def get_model_cnn(input_shape, dropout_rate, nn_units):
    model = Sequential([
        Conv2D(8, (3,3), padding = 'same', activation = 'relu', kernel_initializer = 'he_uniform', input_shape = input_shape),
        MaxPooling2D((3,3)),
        Conv2D(16, (3,3), padding = 'same', activation = 'relu', kernel_initializer = 'he_uniform'),
        MaxPooling2D((3,3)),
        Flatten(),
        Dense(nn_units, activation='relu', kernel_initializer = 'he_uniform'),
        Dropout(dropout_rate),
        Dense(nn_units, activation='relu', kernel_initializer = 'he_uniform'),
        Dropout(dropout_rate),
        Dense(10, activation='softmax'),
    ])
    
    model.compile(optimizer = 'adam', loss = 'sparse_categorical_crossentropy', metrics = ['accuracy'])
    
    return model
model_cnn = get_model_cnn(x_train_gs.shape[1:4], 0.5, 64)
model_cnn.summary()
checkpoint_best_path = 'cnn_chkpnts_bst/chkpnt'
checkpoint_best = ModelCheckpoint(filepath=checkpoint_best_path, save_weights_only=True, monitor='val_accuracy', save_best_only = True, verbose = 1)
earlystop = EarlyStopping(monitor='loss', patience = 5)
start = time.time()
history_cnn = model_cnn.fit(x_train_gs, y_train, epochs = epochs, batch_size=batch_size, 
                    validation_split = 0.15, callbacks=[checkpoint_best, earlystop])
endt = time.time()-start
print("Time for {} epochs: {:0.2f}ms".format(epochs,1000*endt))
plt.plot(history_cnn.history['loss'])
plt.plot(history_cnn.history['val_loss'])
plt.title('Loss vs. epochs')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['Training', 'Validation'], loc='upper right')
plt.show()
plt.plot(history_cnn.history['accuracy'])
plt.plot(history_cnn.history['val_accuracy'])
plt.title('Accuracy vs. epochs')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['Training', 'Validation'], loc='lower right')
plt.show()<jupyter_output><empty_output><jupyter_text>Evaluations<jupyter_code>model_cnn.evaluate(x_test_gs, y_test, verbose = 2)
model_mlp.evaluate(x_test_gs, y_test, verbose = 2)<jupyter_output>814/814 - 1s - loss: 0.8270 - accuracy: 0.7529<jupyter_text>Predictions<jupyter_code>preds_mlp = model_mlp.predict(x_test_gs)

### Predictions from MLP
num_plts = 5

fig, ax = plt.subplots(2, num_plts, figsize=(3*num_plts, 4))
im_set = np.random.random_integers(1, y_test.shape[0], num_plts)

for i in range(len(im_set)):
    ax[0, i].imshow(x_test[:,:,:,im_set[i]])
    ax[0, i].axis('off')
    ax[0, i].set_title('True label: {0:1d}'.format(y_test[im_set[i]][0]))
    ax[1, i].bar(x = range(10), height=preds_mlp[im_set[i],:])
    ax[1, i].set_title('Pred label: {0:1d}'.format(np.argmax(preds_mlp[im_set[i],:])))

plt.show()
preds_cnn = model_cnn.predict(x_test_gs)

### Predictions from CNN
num_plts = 5

fig, ax = plt.subplots(2, num_plts, figsize=(3*num_plts, 4))
im_set = np.random.random_integers(1, y_test.shape[0], num_plts)

for i in range(len(im_set)):
    ax[0, i].imshow(x_test[:,:,:,im_set[i]])
    ax[0, i].axis('off')
    ax[0, i].set_title('True label: {0:1d}'.format(y_test[im_set[i]][0]))
    ax[1, i].bar(x = range(10), height=preds_cnn[im_set[i],:])
    ax[1, i].set_title('Pred label: {0:1d}'.format(np.argmax(preds_cnn[im_set[i],:])))

plt.show()<jupyter_output><ipython-input-28-1b24431bdb07>:7: DeprecationWarning: This function is deprecated. Please call randint(1, 26032 + 1) instead
  im_set = np.random.random_integers(1, y_test.shape[0], num_plts)
{'fraction_non_alphanumeric': 0.1548775482282118, 'fraction_numerical': 0.025037624846080176, 'mean_word_length': 5.127409891031014, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '587558', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 2913, 'n_tokens_neox': 2748, 'n_words': 480, 'path': 'content/code/SVHN_class.ipynb', 'repo_name': 'ENCCS/Containers', 'size': '7309'}