<jupyter_start><jupyter_text>SVHN  dataset Classifier  -esp-Clasificador para la base de datos del SVHN con numeros tomados de google maps de direcciones reales. -eng-MPL and CNN neural networks for the SVHN dataset (set of google maps photos from real houses directions)<jupyter_code>#import all the libraries need it
import tensorflow as tf
from scipy.io import loadmat
import matplotlib.pyplot as plt
import numpy as np
from tensorflow.keras.layers import Dense, Flatten, Conv2D, MaxPooling2D
from tensorflow.keras.models import Sequential
from tensorflow.keras.callbacks import ModelCheckpoint
from tensorflow.keras import regularizers
from tensorflow.keras.layers import Dropout<jupyter_output><empty_output><jupyter_text>I work with two files: `train_32x32.mat` and `test_32x32.mat`.The main goal is to build, train and evaluate a neural network able to classifies a real-word images into one of ten diferent classes<jupyter_code>from google.colab import drive
drive.mount('/content/drive')
# Load the dataset from your Drive folder

train = loadmat('/content/drive/MyDrive/Introduction_tensorflow_week_5_assignment/train_32x32.mat')
test = loadmat('/content/drive/MyDrive/Introduction_tensorflow_week_5_assignment/test_32x32.mat')


#Train and Test are dictionaries with 'X' and 'y' as the keys for the data_input and labels respectively.<jupyter_output><empty_output><jupyter_text>Preprocessing the datasetThe train images are in the format of rgb images, with a shape of (32,32,3). The first step was converted that into a single channel (grayscale) and divided the values by 255 in order to normalize the data.<jupyter_code>#Extracting the training and the test images and labels
train_data = train['X']/255
si = lambda x : 0 if x==10 else (x)
train_labels = [si(x[:][0]) for x in train['y']]
train_labels = np.array(train_labels)
test_data = test['X']/255
test_labels = [si(y[0]) for y in test['y']]
test_labels = np.array(test_labels)

#With this the format of the label change, from a single number to a categorical list with a lenght of 10.
train_targets = tf.keras.utils.to_categorical(train_labels)
test_targets = tf.keras.utils.to_categorical(test_labels)
#Selecting a random images and labels and printing them 
random = np.random.randint(0,1000,3)
for i in random:
  plt.imshow(train_data[:,:,:,i])
  plt.title('The label is --- {} ---'.format(train_targets[i]))
  plt.show()
#grayscaling train and test data. 
gray_train = []
for i in range(train_data.shape[3]):
  a = (train_data[:,:,0,i]+train_data[:,:,1,i]+train_data[:,:,2,i])/3
  gray_train.append(a)
train_data_gray = np.asarray(gray_train)

gray_test = []
for i in range(test_data.shape[3]):
  a = (test_data[:,:,0,i]+test_data[:,:,1,i]+test_data[:,:,2,i])/3
  gray_test.append(a)
test_data_gray = np.asarray(gray_test)
test_data_gray.shape
random_2 = np.random.randint(0,1000,3)
for i in random_2:
  plt.imshow(train_data_gray[i,:,:])
  plt.title('The label is --- {} ---'.format(train_labels[i]))
  plt.show()<jupyter_output><empty_output><jupyter_text>MLP neural network classifier<jupyter_code>#model with 5 dense layers, L2 regularization, dropout and Adam optimizer
def get_mpl_model(wd,t_d,d_r):
  model = Sequential([
                      Dense(512, kernel_regularizer=regularizers.L2(wd),kernel_initializer='he_normal', bias_initializer='ones',input_shape=t_d),
                      Dropout(d_r),
                      Dense(256, kernel_regularizer=regularizers.L2(wd),activation='relu'),
                      Dropout(d_r),
                      Dense(128, kernel_regularizer=regularizers.L2(wd),activation='relu'),
                      Dropout(d_r),
                      Dense(64, kernel_regularizer=regularizers.L2(wd),activation='relu'),
                      Dropout(d_r),
                      Flatten(),
                      Dense(10, activation='softmax')
  ])
  opt = tf.keras.optimizers.Adam(learning_rate=0.0005)
  model.compile(
      optimizer=opt,
      loss='categorical_crossentropy',
      metrics=['accuracy']
  )
  
  return model
model = get_mpl_model(0.5e-5,train_data_gray[0].shape,0.30)
model.summary()
#Callbacks
def scheduler(epoch,lr):
  if epoch<10:
    return lr 
  else:
    return lr*tf.math.exp(-0.05)

def checkpoint_best():
  path = 'checkpoint_best/checkpoint'
  checkp = ModelCheckpoint(
      filepath=path,
      save_weights_only=True,
      save_freq='epoch',
      monitor='val_accuracy',
      save_best_only=True,
      verbose=1
  )
  return checkp

def checkpoint_best_cnn():
  path = 'checkpoint_best_cnn/checkpoint'
  checkp = ModelCheckpoint(
      filepath=path,
      save_weights_only=True,
      save_freq='epoch',
      monitor='val_accuracy',
      save_best_only=True,
      verbose=1
  )
  return checkp
def train_model(model, train_data, train_labels, epochs):
  callbacks = [tf.keras.callbacks.LearningRateScheduler(scheduler),checkpoint_best()]
  return model.fit(train_data, train_labels,epochs=epochs, 
      batch_size=16, validation_split=0.15,
      callbacks=callbacks,
      verbose=1)
def train_cnn_model(model, train_data, train_labels, epochs):
  callbacks = [tf.keras.callbacks.LearningRateScheduler(scheduler),checkpoint_best_cnn()]
  return model.fit(x=train_data, y=train_labels,epochs=epochs, 
      batch_size=32, validation_split=0.15,
      callbacks=callbacks,
      verbose=1)
history = train_model(model, train_data_gray, train_targets, 30)
#plotting the loss for training and validation set
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('loss vs epochs')
plt.xlabel('epoch')
plt.ylabel('loss')
plt.legend(['Training', 'Validation'], loc='upper right')
plt.show()
#plotting the accuracy for both, training and validation set. 
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.xlabel('epoch')
plt.ylabel('accuracy')
plt.legend(['training_acc','Val_acc'])
plt.show()
def get_test_accuracy(model, x_test, y_test):
  test_loss, test_acc = model.evaluate(x=x_test, y=y_test, verbose=0)
  print('accuracy: {acc:0.3f}'.format(acc=test_acc))
  print('loss: {loss:0.3f}'.format(loss=test_loss))
get_test_accuracy(model, test_data_gray, test_targets)
model.load_weights('/content/drive/MyDrive/Introduction_tensorflow_week_5_assignment/checkpoint_best/checkpoint')
get_test_accuracy(model, test_data_gray, test_targets)<jupyter_output>accuracy: 0.712<jupyter_text>CNN neural network classifier<jupyter_code>#CNN classifer 
def get_cnn_model(td_shape,w_d,d_r):
  model = Sequential([
    Conv2D(32, kernel_size=(3,3), activation='relu', padding='same',input_shape=td_shape,
           kernel_initializer='glorot_uniform', bias_initializer='zeros'),
    MaxPooling2D((2,2)),
    Dropout(d_r),
    Conv2D(16, kernel_size=(2,2),activation='relu', padding='same'),
    MaxPooling2D((2,2)),
    Dropout(d_r),
    Flatten(),
    Dense(128, activation='relu', kernel_regularizer=tf.keras.regularizers.L2(w_d)),
    Dropout(d_r),
    Dense(64, activation='relu', kernel_regularizer=tf.keras.regularizers.L2(w_d)),
    Dropout(d_r),
    Dense(10, activation='softmax')
  ])
  opt = tf.keras.optimizers.Adam(learning_rate=0.0005)
  model.compile(optimizer=opt, loss='categorical_crossentropy',metrics=['accuracy'])
  return model
train_data_gray_cnn = np.reshape(train_data_gray,(73257,32,32,1))
test_data_gray_cnn = np.reshape(test_data_gray,(26032,32,32,1))
cnn_model = get_cnn_model(train_data_gray_cnn[0].shape, 1e-5, 0.20)
cnn_model.summary()
history_cnn = train_cnn_model(cnn_model,train_data_gray_cnn,train_targets,30)
#plotting the loss for training and validation set
plt.plot(history_cnn.history['loss'])
plt.plot(history_cnn.history['val_loss'])
plt.title('loss vs epochs  CNN')
plt.xlabel('epoch')
plt.ylabel('loss')
plt.legend(['Training', 'Validation'], loc='upper right')
plt.show()
#plotting the accuracy for both, training and validation set. 
plt.plot(history_cnn.history['accuracy'])
plt.plot(history_cnn.history['val_accuracy'])
plt.title('accuracy vs epoch CNN')
plt.xlabel('epoch')
plt.ylabel('accuracy')
plt.legend(['training_acc','Val_acc'])
plt.show()
get_test_accuracy(cnn_model, test_data_gray_cnn,test_targets)<jupyter_output>accuracy: 0.901
loss: 0.351<jupyter_text>Get model predictions<jupyter_code>#MPL model
model = get_mpl_model(1e-5,train_data_gray[0].shape,0.30)
model.summary()
model.load_weights('/content/drive/MyDrive/Introduction_tensorflow_week_5_assignment/checkpoint_best/checkpoint')
get_test_accuracy(model, test_data_gray, test_targets)
index_random = np.random.choice(test_data_gray.shape[0],5)
random_test_data = test_data_gray[index_random, ...]
random_test_label = test_labels[index_random, ...]

mpl_predictions = model.predict(random_test_data)
fig,axes = plt.subplots(5,2, figsize=(15,10))
fig.subplots_adjust(hspace=0.4, wspace=0.1)

for i, (mpl_predictions, image, label) in enumerate(zip(mpl_predictions, random_test_data, random_test_label)):
  axes[i,0].imshow(np.squeeze(image))
  axes[i,0].get_xaxis().set_visible(False)
  axes[i,0].get_yaxis().set_visible(False)
  axes[i,0].text(10,-2, f'Digit {label}')
  axes[i,1].bar(np.arange(len(mpl_predictions)),mpl_predictions)
  axes[i,1].set_xticks(np.arange(len(mpl_predictions)))
  axes[i,1].set_title(f'Model prediction : {np.argmax(mpl_predictions)}')
plt.show()
#CNN model
cnn_model = get_cnn_model(train_data_gray_cnn[0].shape, 1e-5, 0.20)
cnn_model.summary()
cnn_model.load_weights('/content/drive/MyDrive/Introduction_tensorflow_week_5_assignment/checkpoint_best_cnn/checkpoint')
get_test_accuracy(cnn_model, test_data_gray_cnn,test_targets)
index_random = np.random.choice(test_data_gray.shape[0],5)
random_test_data = test_data_gray_cnn[index_random, ...]
random_test_label = test_labels[index_random, ...]

cnn_predictions = cnn_model.predict(random_test_data)
fig,axes = plt.subplots(5,2, figsize=(15,10))
fig.subplots_adjust(hspace=0.4, wspace=0.1)

for i, (mpl_predictions, image, label) in enumerate(zip(cnn_predictions, random_test_data, random_test_label)):
  axes[i,0].imshow(np.squeeze(image))
  axes[i,0].get_xaxis().set_visible(False)
  axes[i,0].get_yaxis().set_visible(False)
  axes[i,0].text(10,-2, f'Digit {label}')
  axes[i,1].bar(np.arange(len(mpl_predictions)),mpl_predictions)
  axes[i,1].set_xticks(np.arange(len(mpl_predictions)))
  axes[i,1].set_title(f'Model prediction : {np.argmax(mpl_predictions)}')
plt.show()
plt.savefig('gif.png')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13352517985611512, 'fraction_numerical': 0.02407673860911271, 'mean_word_length': 5.8054830287206265, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 23, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '394288', 'license': 'MIT', 'n_tokens_mistral': 3956, 'n_tokens_neox': 3739, 'n_words': 658, 'path': 'SVHN_classifier.ipynb', 'repo_name': 'jafetsierra/SVHN-classifier', 'size': '10425'}