<jupyter_start><jupyter_text>Street View House Numbers32x32 MNIST like dataset Step 1: Load and visualize data<jupyter_code># import packages
import scipy
import numpy as np
from scipy.io import loadmat

import matplotlib.pyplot as plt
%matplotlib inline

# Load data
train_data = loadmat('train_32x32.mat')
test_data = loadmat('test_32x32.mat')<jupyter_output><empty_output><jupyter_text>Step 2: Display and Normalize training set<jupyter_code># Reshape the data
X_train = np.transpose(train_data['X'], (3, 0, 1, 2))
X_test = np.transpose(test_data['X'], (3, 0, 1, 2))

y_train = train_data['y'][:,0]
y_test = test_data['y'][:,0]

# Display the training and predicted data
def plot_images(img,labels,nrows,ncols,pred_labels=None):
    fig = plt.figure(figsize = (20,6));
    axes = fig.subplots(nrows,ncols)
    for i, ax in enumerate(axes.flat):
        ax.imshow(img[i])
        ax.set_xticks([]); ax.set_yticks([])
        if pred_labels is None:
            ax.set_title('True: %d' % labels[i])
        else:
            ax.set_title('True: {0}, Pred: {1}'.format(labels[i], np.argmax(pred_labels[i])))

plot_images(X_train, y_train, 2, 8)
y_train.shape
print(X_train.shape)
print(type(X_train))
# Normalize input
X_train = (X_train / 255.0).astype(np.float32)
X_test = (X_test / 255.0).astype(np.float32)<jupyter_output><empty_output><jupyter_text>Step 3: Preparing training output values<jupyter_code># import tensflow packages
import tensorflow as tf
import warnings
warnings.filterwarnings('ignore')
from tensorflow import keras
from tensorflow.keras.utils import to_categorical

# Digit 10 --> label 0
y_train[y_train==10] = 0
y_test[y_test==10] = 0

# One-hot encoding
y_train_enc = to_categorical(y_train)
y_test_enc = to_categorical(y_test)
np.unique(y_train)<jupyter_output><empty_output><jupyter_text>Step 4 - Model<jupyter_code>from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Dropout, Flatten, Dense, Activation

num_classes = 10      # Number of classes to model
model = Sequential([
    Conv2D(32, kernel_size=(3,3), padding='same', input_shape=(32, 32, 3)),  
    Activation('relu'),
    Conv2D(32, kernel_size=(3,3), padding='same'),
    Activation('relu'),
    MaxPooling2D(pool_size=(2,2)),
    Conv2D(32, kernel_size=(3,3), padding='same'),
    Activation('relu'),
    Conv2D(32, kernel_size=(3,3), padding='same'),
    Activation('relu'),
    Flatten(),
    Dropout(0.5),
    Dense(512),
    Activation('relu'),
    Dropout(0.5),
    Dense(128),
    Activation('relu'),
    Dense(num_classes, activation='softmax')])

model.compile("adam", "sparse_categorical_crossentropy", metrics=['accuracy'])<jupyter_output><empty_output><jupyter_text>Step 5 - Training and evaluating the model<jupyter_code>history_cnn = model.fit(X_train, y_train,
                      epochs=5, verbose=1, validation_split=.1)

scores = model.evaluate(X_test, y_test, verbose = 0)
print("TEST SET: %s: %.2f%%" % (cnn.metrics_names[1], scores[1]*100))<jupyter_output>Train on 65931 samples, validate on 7326 samples
Epoch 1/5
65931/65931 [==============================] - 9s 140us/sample - loss: 1.2823 - acc: 0.5612 - val_loss: 0.4927 - val_acc: 0.8464
Epoch 2/5
65931/65931 [==============================] - 9s 135us/sample - loss: 0.5197 - acc: 0.8402 - val_loss: 0.3627 - val_acc: 0.8894
Epoch 3/5
65931/65931 [==============================] - 9s 134us/sample - loss: 0.4319 - acc: 0.8679 - val_loss: 0.3369 - val_acc: 0.9004
Epoch 4/5
65931/65931 [==============================] - 9s 136us/sample - loss: 0.3907 - acc: 0.8808 - val_loss: 0.3151 - val_acc: 0.9028
Epoch 5/5
65931/65931 [==============================] - 9s 136us/sample - loss: 0.3615 - acc: 0.8887 - val_loss: 0.2999 - val_acc: 0.9110
TEST SET: acc: 90.16%<jupyter_text>Step 6 - Predicting using the model<jupyter_code>predictions = model.predict(X_test)<jupyter_output><empty_output><jupyter_text>Step 7 - Comparing the results<jupyter_code>plot_images(X_test, y_test, 2, 8, predictions)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17535193875030872, 'fraction_numerical': 0.07508026673252655, 'mean_word_length': 5.062874251497006, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 29, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '538326', 'license': 'MIT', 'n_tokens_mistral': 1729, 'n_tokens_neox': 1512, 'n_words': 316, 'path': 'number_recognition.ipynb', 'repo_name': 'Sanjana7395/House-number-recognition', 'size': '4049'}