<jupyter_start><jupyter_text>Image processing with CNN (LeNet)Based on URL  https://github.com/RichmondAlake/tensorflow_2_tutorials/blob/master/13_lenet-5.ipynb<jupyter_code>import tensorflow as tf
import numpy as np
import time
import os
from tensorflow import keras<jupyter_output><empty_output><jupyter_text>MNIST dataset<jupyter_code>(train_x, train_y), (test_x, test_y) = keras.datasets.mnist.load_data()
train_x = train_x / 255.0
test_x = test_x / 255.0

train_x = tf.expand_dims(train_x, 3)
test_x = tf.expand_dims(test_x, 3)

val_x = train_x[:5000]
val_y = train_y[:5000]<jupyter_output><empty_output><jupyter_text>LeNet (1990) model<jupyter_code>lenet_5_model = keras.models.Sequential([
    keras.layers.Conv2D(6, kernel_size=5, strides=1,  activation='tanh', input_shape=train_x[0].shape, padding='same'),
    keras.layers.AveragePooling2D(),
    keras.layers.Conv2D(16, kernel_size=5, strides=1, activation='tanh', padding='valid'),
    keras.layers.AveragePooling2D(),
    keras.layers.Flatten(),
    keras.layers.Dense(120, activation='tanh'),
    keras.layers.Dense(84, activation='tanh'),
    keras.layers.Dense(10, activation='softmax')
])

lenet_5_model.summary()
lenet_5_model.compile(optimizer='adam', loss=keras.losses.sparse_categorical_crossentropy, metrics=['accuracy'])<jupyter_output><empty_output><jupyter_text>Training and Evaluation<jupyter_code>def get_run_logdir():
    run_id = time.strftime("run_%Y_%m_%d-%H_%M_%S")
    return os.path.join(root_logdir, run_id)

root_logdir = os.path.join(os.curdir, "logs\\fit\\")
run_logdir = get_run_logdir()
tensorboard_cb = keras.callbacks.TensorBoard(run_logdir)

lenet_5_model.fit(train_x, 
                  train_y, 
                  epochs=5, 
                  validation_data=(val_x, val_y), 
                  callbacks=[tensorboard_cb]
                 )
lenet_5_model.evaluate(test_x, test_y)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1328781512605042, 'fraction_numerical': 0.026785714285714284, 'mean_word_length': 5.2052117263843645, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 1, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '356150', 'license': 'Apache-2.0', 'n_tokens_mistral': 770, 'n_tokens_neox': 729, 'n_words': 94, 'path': 'cvicenia/notebooks/tyzden-12/IAU_LeNet_hand_writing_recognition.ipynb', 'repo_name': 'xmajzel/2020-2021', 'size': '1904'}