<jupyter_start><jupyter_text>LeNet Architecture<jupyter_code>import os
import numpy as np
import seaborn as sns

from matplotlib import pyplot as plt
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, AveragePooling2D, Dense, Flatten, Input
from tensorflow.keras.activations import tanh, sigmoid
from tensorflow.keras.optimizers import SGD
from tensorflow.keras.losses import binary_crossentropy
from tensorflow.keras.callbacks import ModelCheckpoint, LearningRateScheduler
from tensorflow.keras.utils import plot_model
from tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img, img_to_array
from sklearn.preprocessing import MultiLabelBinarizer<jupyter_output><empty_output><jupyter_text>LeNet model<jupyter_code>leNet = Sequential()

# input layer
leNet.add(Input(shape=(56, 56, 3), name='input_layer'))

# convolution 1
leNet.add(Conv2D(filters=6, kernel_size=(5, 5), strides=(1, 1), padding='same', activation=tanh, name='conv_5x5_1'))

# subsampling (Avg. pool)
leNet.add(AveragePooling2D(pool_size=(2, 2)))

# convolution 2
leNet.add(Conv2D(filters=16, kernel_size=(5, 5), strides=(1, 1), padding='valid', activation=tanh, name='conv_5x5_2'))

# subsampling (Avg. pool)
leNet.add(AveragePooling2D(pool_size=(2, 2)))

# convolution 3
leNet.add(Conv2D(filters=120, kernel_size=(5, 5), strides=(1, 1), padding='valid', activation=tanh, name='conv_5x5_3'))

# flatten
leNet.add(Flatten())

# classifier
leNet.add(Dense(units=84, activation=tanh, name='fully_con_1'))

# output layer
leNet.add(Dense(units=42, activation=sigmoid, name='output_layer'))

leNet.summary()
plot_model(model=leNet,
           to_file='LeNet.png',
           show_shapes=True,
           show_dtype=True,
           show_layer_names=True,
           dpi=150)<jupyter_output><empty_output><jupyter_text>learning rate schedule in LeNet<jupyter_code>def lr_schedule(epoch):
    if epoch <= 2:
        lr = 5e-4
    elif 2 < epoch <= 5:
        lr = 2e-4
    elif 5 < epoch <= 9:
        lr = 5e-5
    else:
        lr = 1e-5

    return lr
lr_scheduler = LearningRateScheduler(lr_schedule)
check_point = ModelCheckpoint(filepath='LeNet.hdf5',
                              monitor='val_loss',
                              save_best_only=True)

callbacks = [lr_scheduler, check_point]
leNet.compile(optimizer=SGD(),
              loss=binary_crossentropy,
              metrics=['accuracy'])
tree_types = os.listdir('../../data/image data/train')
X = []
y = []
val_x = []
val_y = []

for tree in tree_types:
    tree_path = os.path.join('../../data/image data/train', tree)
    tree_disease_types = os.listdir(tree_path)

    for disease in tree_disease_types:
        img_name = os.listdir(os.path.join(tree_path, disease))

        for img in img_name:
            image_path = os.path.join(tree_path, disease, img)
            image = img_to_array(load_img(image_path, target_size=(56, 56)))
            X.append(image)
            y.append([tree, disease])

for tree in tree_types:
    tree_path = os.path.join('../../data/image data/validation', tree)
    tree_disease_types = os.listdir(tree_path)

    for disease in tree_disease_types:
        img_name = os.listdir(os.path.join(tree_path, disease))

        for img in img_name:
            image_path = os.path.join(tree_path, disease, img)
            image = img_to_array(load_img(image_path, target_size=(56, 56)))
            val_x.append(image)
            val_y.append([tree, disease])

X = np.asarray(X)
val_x = np.asarray(val_x)
mlb = MultiLabelBinarizer()
y = mlb.fit_transform(y)
val_y = mlb.transform(val_y)
generator = ImageDataGenerator(rescale=1. / 255)
X.shape, y.shape, val_x.shape, val_y.shape
train_gen = generator.flow(X, y, batch_size=32)
val_gen = generator.flow(val_x, val_y, batch_size=32)<jupyter_output><empty_output><jupyter_text>training<jupyter_code>leNet_history = leNet.fit(train_gen,
                          batch_size=32,
                          epochs=20,
                          callbacks=callbacks,
                          validation_data=val_gen)
figure, axes = plt.subplots(nrows=1, ncols=2, figsize=[18, 6], dpi=300)
axes = axes.ravel()
epochs = list(range(len(leNet_history.history['loss'])))

sns.lineplot(x=epochs, y=leNet_history.history['loss'], ax=axes[0], label='loss')
sns.lineplot(x=epochs, y=leNet_history.history['val_loss'], ax=axes[0], label='val loss')
sns.lineplot(x=epochs, y=leNet_history.history['accuracy'], ax=axes[1], label='accuracy')
sns.lineplot(x=epochs, y=leNet_history.history['val_accuracy'], ax=axes[1], label='val accuracy')
axes[0].set_xlabel('epoch')
axes[0].set_ylabel('loss')
axes[1].set_xlabel('epoch')
axes[1].set_ylabel('accuracy')
plt.savefig('LeNet_train_history.png')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13301315514721238, 'fraction_numerical': 0.021716433493422425, 'mean_word_length': 4.0903294367693945, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 17, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '388201', 'license': 'Apache-2.0', 'n_tokens_mistral': 1776, 'n_tokens_neox': 1698, 'n_words': 302, 'path': 'models/architecture/LeNet.ipynb', 'repo_name': 'ashen007/Identify-Diseases-in-Crops-leaf', 'size': '4789'}