<jupyter_start><jupyter_text>Data Augmentation<jupyter_code>import pandas as pd
import numpy as np

import os
import datetime

import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPool2D, Dense, Flatten, Dropout
from tensorflow.keras.utils import to_categorical
from sklearn.metrics import accuracy_score

%load_ext tensorboard

import matplotlib.pyplot as plt
from skimage import color, exposure
cd /content/drive/My Drive/Colab Notebooks/dataworkshop_matrix/dw_matrix_road_sign/dw_matrix_road_sign/data 

train = pd.read_pickle('train.p')
test = pd.read_pickle('test.p')

X_train, y_train = train['features'], train['labels']
X_test, y_test = test['features'], test['labels']
#y_train should have only one dimension, be careful not to overwrite this!

if y_train.ndim == 1: y_train = to_categorical(y_train)
if y_test.ndim == 1: y_test = to_categorical(y_test)
#Basic Model
input_shape = X_train.shape[1:]  #(32, 32, 3)
num_classes = y_train.shape[1]
model = Sequential([
    Conv2D(64, kernel_size=(3,3), activation='relu', input_shape=input_shape),

    Flatten(),

    Dense(num_classes, activation='softmax'),

])

model.compile(loss='categorical_crossentropy', optimizer='Adam', metrics=['accuracy'])
model.summary()
def get_cnn_v1(input_shape, num_classes):
  return Sequential([
    Conv2D(64, kernel_size=(3,3), activation='relu', input_shape=input_shape),

    Flatten(),

    Dense(num_classes, activation='softmax'),

])

def train_model(model, X_train, y_train, params_fit={}):
  model.compile(loss='categorical_crossentropy', optimizer='Adam', metrics=['accuracy'])

  logdir = os.path.join('logs', datetime.datetime.now().strftime('%Y%m%d-%H%M%S'))
  tensorboard_callback = tf.keras.callbacks.TensorBoard(logdir, histogram_freq=1)

  model.fit(
      X_train,
      y_train,
      batch_size=params_fit.get('batch size', 128),
      epochs = params_fit.get('epochs', 5),
      verbose = params_fit.get('verbose', 1),
      validation_data = params_fit.get('validation_data', (X_train, y_train) ),
      callbacks= [tensorboard_callback] #visualisation
    
  )
  
  return model
model = get_cnn_v1(input_shape, num_classes)
model_trained = train_model(model, X_train, y_train )
y_pred_prob = model_trained.predict(X_test)
y_pred_prob[400]
plt.imshow(X_test[400])
df = pd.read_csv('signnames.csv')
labels_dict = df.to_dict()['b']
#Argmax returns index with the highest value
labels_dict[ np.argmax( y_pred_prob[400] ) ]
# Cat - visualization for index 400

plt.bar(range(43), y_pred_prob[400])
labels_dict[ 2 ] 
#Axis 1 means we want to iterative throughout the whole matrix
#Axis 0 would give us only one value


def predict(model_trained, X_test, y_test, scoring=accuracy_score):

  y_test_norm = np.argmax(y_test, axis = 1)

  y_pred_prob = model_trained.predict(X_test)
  y_pred = np.argmax( y_pred_prob, axis=1 )

  return scoring(y_test_norm, y_pred)
predict(model_trained, X_test, y_test)<jupyter_output><empty_output><jupyter_text>Basic Model ~82% Architecture Engineering<jupyter_code>def train_and_predict (model):
  model_trained = train_model(model, X_train, y_train )
  return predict(model_trained, X_test, y_test)
def get_cnn_v2(input_shape, num_classes):
  return Sequential([
    Conv2D(64, kernel_size=(3,3), activation='relu', input_shape=input_shape),
    MaxPool2D(),
    Dropout(0.3),

    Flatten(),

    Dense(1024, activation='relu'),
    Dropout(0.3),
    Dense(num_classes, activation='softmax'),

])

train_and_predict (get_cnn_v2(input_shape, num_classes) )
def get_cnn_v3(input_shape, num_classes):
  return Sequential([
    Conv2D(64, kernel_size=(3,3), activation='relu', input_shape=input_shape),
    Conv2D(64, kernel_size=(3,3), activation='relu'),
    MaxPool2D(),
    Dropout(0.3),

    Conv2D(64, kernel_size=(3,3), activation='relu', input_shape=input_shape),
    Conv2D(64, kernel_size=(3,3), activation='relu' ),
    MaxPool2D(),
    Dropout(0.3),

    Flatten(),

    Dense(1024, activation='relu'),
    Dropout(0.3),
    Dense(num_classes, activation='softmax'),

])

train_and_predict (get_cnn_v3(input_shape, num_classes) )
def get_cnn_v4(input_shape, num_classes):
  return Sequential([
    Conv2D(64, kernel_size=(3,3), activation='relu', input_shape=input_shape, padding='same'),
    Conv2D(64, kernel_size=(3,3), activation='relu'),
    MaxPool2D(),
    Dropout(0.3),

    Conv2D(64, kernel_size=(3,3), activation='relu', padding='same'),
    Conv2D(64, kernel_size=(3,3), activation='relu' ),
    MaxPool2D(),
    Dropout(0.3),

    Conv2D(64, kernel_size=(3,3), activation='relu', padding='same'),
    Conv2D(64, kernel_size=(3,3), activation='relu' ),
    MaxPool2D(),
    Dropout(0.3),

    Flatten(),

    Dense(1024, activation='relu'),
    Dropout(0.3),
    Dense(num_classes, activation='softmax'),

])

train_and_predict (get_cnn_v4(input_shape, num_classes) )
def get_cnn_v5(input_shape, num_classes):
  return Sequential([
    Conv2D(64, kernel_size=(3,3), activation='relu', input_shape=input_shape, padding='same'),
    Conv2D(64, kernel_size=(3,3), activation='relu'),
    MaxPool2D(),
    Dropout(0.3),

    Conv2D(64, kernel_size=(3,3), activation='relu', padding='same'),
    Conv2D(64, kernel_size=(3,3), activation='relu' ),
    MaxPool2D(),
    Dropout(0.3),

    Conv2D(64, kernel_size=(3,3), activation='relu', padding='same'),
    Conv2D(64, kernel_size=(3,3), activation='relu' ),
    MaxPool2D(),
    Dropout(0.3),

    Flatten(),

    Dense(1024, activation='relu'),
    Dropout(0.3),

    Dense(1024, activation='relu'),
    Dropout(0.3),
    
    Dense(num_classes, activation='softmax'),

])

train_and_predict (get_cnn_v5(input_shape, num_classes) )<jupyter_output>Epoch 1/5
272/272 [==============================] - 5s 18ms/step - loss: 2.6148 - accuracy: 0.2684 - val_loss: 1.3913 - val_accuracy: 0.5451
Epoch 2/5
272/272 [==============================] - 5s 17ms/step - loss: 1.0757 - accuracy: 0.6402 - val_loss: 0.3501 - val_accuracy: 0.8800
Epoch 3/5
272/272 [==============================] - 5s 17ms/step - loss: 0.4722 - accuracy: 0.8462 - val_loss: 0.1401 - val_accuracy: 0.9600
Epoch 4/5
272/272 [==============================] - 5s 17ms/step - loss: 0.2947 - accuracy: 0.9100 - val_loss: 0.0907 - val_accuracy: 0.9723
Epoch 5/5
272/272 [==============================] - 5s 17ms/step - loss: 0.2158 - accuracy: 0.9356 - val_loss: 0.0440 - val_accuracy: 0.9859<jupyter_text>Decreasing the total number of parameters by reducing RGB dimension (to 1 = gray)<jupyter_code>color.rgb2gray( X_train[0] ).shape
plt.imshow( color.rgb2gray( X_train[0] ) )
plt.imshow( color.rgb2gray( X_train[0] ), cmap=plt.get_cmap('gray') )
#Shape return only a size of a matrix, if we need only a matrix then don't use it 

X_train_gray = color.rgb2gray( X_train )
X_test_gray = color.rgb2gray( X_test )
#Previously we had 3 dimensions so now we have to reshape it

input_shape
X_train_gray.reshape(-1,32,32, 1).shape
X_train_gray = color.rgb2gray( X_train ).reshape(-1,32,32, 1)
X_test_gray = color.rgb2gray( X_test ).reshape(-1,32,32, 1)<jupyter_output><empty_output><jupyter_text>We will use v5 with gray images<jupyter_code>model = get_cnn_v5( (32,32,1 ), num_classes)
model_trained = train_model(model, X_train_gray, y_train, params_fit={})
predict(model_trained, X_test_gray, y_test)<jupyter_output>Epoch 1/5
272/272 [==============================] - 5s 18ms/step - loss: 3.0873 - accuracy: 0.1579 - val_loss: 1.1404 - val_accuracy: 0.6325
Epoch 2/5
272/272 [==============================] - 5s 17ms/step - loss: 0.7047 - accuracy: 0.7695 - val_loss: 0.1430 - val_accuracy: 0.9599
Epoch 3/5
272/272 [==============================] - 5s 17ms/step - loss: 0.2521 - accuracy: 0.9193 - val_loss: 0.0666 - val_accuracy: 0.9788
Epoch 4/5
272/272 [==============================] - 5s 17ms/step - loss: 0.1651 - accuracy: 0.9478 - val_loss: 0.0324 - val_accuracy: 0.9910
Epoch 5/5
272/272 [==============================] - 5s 17ms/step - loss: 0.1214 - accuracy: 0.9614 - val_loss: 0.0218 - val_accuracy: 0.9934<jupyter_text>Final score 1 RGB ~96%3RGB achieved 97%<jupyter_code><jupyter_output><empty_output><jupyter_text>How does the image look like before and after changes?<jupyter_code>def preproc_img(img):
  hsv = color.rgb2hsv(img)
  hsv[:, :, 2] = exposure.equalize_adapthist(hsv[:, :, 2])
  img = color.hsv2rgb(hsv)

  return img
#BEFORE
plt.imshow(X_train[400] )
#AFTER
plt.imshow( preproc_img(X_train[400]))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17480296435713447, 'fraction_numerical': 0.0665804023056111, 'mean_word_length': 5.283813747228382, 'pattern_counts': {'":': 0, '<': 23, '<?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': '123989', 'license': 'MIT', 'n_tokens_mistral': 3544, 'n_tokens_neox': 3166, 'n_words': 629, 'path': 'day4.ipynb', 'repo_name': 'kordusmonika/dw_matrix_road_sign', 'size': '8501'}