image_output_11.png image_output_24.png image_output_25.png text_plain_output_5.png text_plain_output_15.png image_output_17.png text_plain_output_9.png image_output_14.png text_plain_output_20.png image_output_23.png text_plain_output_4.png text_plain_output_13.png image_output_13.png image_output_5.png text_plain_output_14.png image_output_18.png image_output_21.png text_plain_output_27.png text_plain_output_10.png text_plain_output_6.png image_output_7.png text_plain_output_24.png text_plain_output_21.png text_plain_output_25.png image_output_20.png text_plain_output_18.png text_plain_output_3.png image_output_4.png text_plain_output_22.png text_plain_output_7.png image_output_8.png text_plain_output_16.png image_output_16.png text_plain_output_8.png text_plain_output_26.png image_output_27.png image_output_6.png text_plain_output_23.png image_output_12.png image_output_22.png text_plain_output_2.png text_plain_output_1.png image_output_3.png text_plain_output_19.png image_output_2.png image_output_1.png image_output_10.png text_plain_output_17.png text_plain_output_11.png text_plain_output_12.png image_output_15.png image_output_9.png image_output_19.png image_output_26.png
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout

from tensorflow.keras.preprocessing.image import ImageDataGenerator

import matplotlib.pyplot as plt

import tensorflow as tf

import os
import numpy as np
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
data_dir = 'images/'
img_width, img_height = (128, 128)
batch_size = 32
train_datagen = ImageDataGenerator(rescale=1.0 / 255, validation_split=0.2)
train_generator = train_datagen.flow_from_directory(data_dir, target_size=(img_width, img_height), batch_size=batch_size, class_mode='categorical', color_mode='grayscale', subset='training')
validation_generator = train_datagen.flow_from_directory(data_dir, target_size=(img_width, img_height), batch_size=batch_size, class_mode='categorical', color_mode='grayscale', subset='validation')
def create_alexnet(input_shape, num_classes, activation):
    model = tf.keras.Sequential([Conv2D(96, (11, 11), activation=activation, input_shape=input_shape, strides=(4, 4), padding='same'), MaxPooling2D(pool_size=(3, 3), strides=(2, 2)), Conv2D(256, (5, 5), activation=activation, strides=(1, 1), padding='same'), MaxPooling2D(pool_size=(3, 3), strides=(2, 2)), Conv2D(384, (3, 3), activation=activation, strides=(1, 1), padding='same'), Conv2D(384, (3, 3), activation=activation, strides=(1, 1), padding='same'), Conv2D(256, (3, 3), activation=activation, strides=(1, 1), padding='same'), MaxPooling2D(pool_size=(3, 3), strides=(2, 2)), Flatten(), Dense(4096, activation=activation), Dropout(0.5), Dense(4096, activation=activation), Dropout(0.5), Dense(num_classes, activation='softmax')])
    return model
'\n# Define learning rate adjustment schemes\ndef lr_schedule_1(epoch, lr):\n    if epoch < 20:\n        return lr\n    elif epoch < 40:\n        return lr * 0.1\n    else:\n        return lr * 0.01\n\ndef lr_schedule_2(epoch, lr):\n    return lr * (1 / (1 + 0.1 * epoch))\n'
num_classes = 8
input_shape = (img_width, img_height, 1)
activations = ['relu', 'sigmoid', 'tanh']
optimizers = [tf.keras.optimizers.legacy.Adam(learning_rate=0.001), tf.keras.optimizers.legacy.Adam(learning_rate=0.0001), tf.keras.optimizers.legacy.Adam(learning_rate=1e-05), tf.keras.optimizers.legacy.SGD(learning_rate=0.001), tf.keras.optimizers.legacy.SGD(learning_rate=0.0001), tf.keras.optimizers.legacy.SGD(learning_rate=1e-05), tf.keras.optimizers.legacy.RMSprop(learning_rate=0.001), tf.keras.optimizers.legacy.RMSprop(learning_rate=0.0001), tf.keras.optimizers.legacy.RMSprop(learning_rate=1e-05)]
adams = optimizers[:3]
sgds = optimizers[3:6]
rmsprops = optimizers[6:]
for activation in activations:
    for optimizer in adams:
        initial_lr = round(float(optimizer.learning_rate), 5)
        optimizer._name = 'Adam'
        model = create_alexnet(input_shape, num_classes, activation)
        model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy'])
        history = model.fit(train_generator, epochs=50, validation_data=validation_generator)

for activation in activations:
    for optimizer in sgds:
        initial_lr = round(float(optimizer.learning_rate), 5)
        optimizer._name = 'SGD'
        print(f'Training with {activation} activation and optimizer {optimizer._name} with {initial_lr}')
        model = create_alexnet(input_shape, num_classes, activation)
        model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy'])
        history = model.fit(train_generator, epochs=50, validation_data=validation_generator)
        plt.figure(figsize=(12, 6))
        plt.subplot(1, 2, 1)
        plt.plot(history.history['loss'], label='Train Loss')
        plt.plot(history.history['val_loss'], label='Validation Loss')
        plt.xlabel('Epochs')
        plt.ylabel('Loss')
        plt.title(f'Loss per epoch with {activation} and optimizer {optimizer._name} with {initial_lr}')
        plt.legend()
        plt.subplot(1, 2, 2)
        plt.plot(history.history['accuracy'], label='Train Accuracy')
        plt.plot(history.history['val_accuracy'], label='Validation Accuracy')
        plt.xlabel('Epochs')
        plt.ylabel('Accuracy')
        plt.title(f'Accuracy per epoch with {activation} and optimizer {optimizer._name} with {initial_lr}')
        plt.legend()
        plt.show()