<jupyter_start><jupyter_text>Tuning Hyperparameters with Kears Tuner 2 - Task: Find best Number of Filters in ConvLayers and best Learning Rate for SGD Optimizer- Dataset: Cropped and Resized Fundus Images- Original Colorscheme<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras_preprocessing.image import ImageDataGenerator
from keras.layers import Dense, Activation, Flatten, Dropout, BatchNormalization
from keras.layers import Conv2D, MaxPooling2D
from keras import regularizers, optimizers
from tensorflow.keras.callbacks import EarlyStopping
from keras_preprocessing.image import ImageDataGenerator
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Activation, Flatten, Dropout, Conv2D, MaxPooling2D
from tensorflow.keras.optimizers import Adam, SGD
df = pd.read_csv('/content/drive/MyDrive/Anomaly Detection/train_binary.csv')
df.normal = df.normal.astype('str')
df.normal.value_counts()
# As data is ordered in dataframe after augmentation, shuffle it
from sklearn.utils import shuffle 
df = shuffle(df)
# Directory
indir = r"D:\data\Projects\notebooks\RetinaAI\Anomaly Detection\train"
datagen = ImageDataGenerator(rescale=1./255., validation_split = 0.25)
train_gen = datagen.flow_from_dataframe(dataframe = df, 
                                            directory = indir, 
                                            x_col = "filename", 
                                            y_col = 'normal',
                                            batch_size = 64, 
                                            seed = 2, 
                                            shuffle = True, 
                                            class_mode = "binary", 
                                            target_size = (300,300),
                                            subset='training')
val_gen = datagen.flow_from_dataframe(dataframe = df, 
                                            directory = indir, 
                                            x_col = "filename", 
                                            y_col = 'normal',
                                            batch_size = 64, 
                                            seed = 2, 
                                            shuffle = True, 
                                            class_mode = "binary", 
                                            target_size = (300,300),
                                            subset='validation')<jupyter_output>Found 1515 validated image filenames belonging to 2 classes.<jupyter_text>Tune Hyperparameters<jupyter_code>from kerastuner.tuners import Hyperband
from kerastuner.engine.hyperparameters import HyperParameters
LOG_DIR = 'log_dir'
def build_model(hp):
    model = Sequential()

    model.add(Conv2D(hp.Int('conv_1', min_value=32, max_value=128, step=32), 
                     (3, 3), kernel_initializer='he_uniform', padding='same', activation='relu',  input_shape=(300,300,3)))
    model.add(Conv2D(hp.Int('conv_2', min_value=32, max_value=128, step=32), 
                     (3, 3), kernel_initializer='he_uniform', padding='same', activation='relu'))
    model.add(MaxPooling2D((2, 2)))

    model.add(Conv2D(hp.Int('conv_3', min_value=64, max_value=256, step=64), 
                     (3, 3), kernel_initializer='he_uniform', padding='same', activation='relu'))
    model.add(Conv2D(hp.Int('conv_4', min_value=64, max_value=256, step=64), 
                     (3, 3), kernel_initializer='he_uniform', padding='same', activation='relu'))
    model.add(MaxPooling2D((2, 2)))
    
    model.add(Conv2D(hp.Int('conv_5', min_value=64, max_value=256, step=64), 
                     (3, 3), kernel_initializer='he_uniform', padding='same', activation='relu')))
    model.add(Conv2D(hp.Int('conv_6', min_value=64, max_value=256, step=64), 
                     (3, 3), kernel_initializer='he_uniform', padding='same', activation='relu'))
    model.add(MaxPooling2D((2, 2)))
    
    model.add(Flatten())
    model.add(Dense(hp.Int('units', min_value=64, max_value=256, step=64), activation='relu'))
    model.add(Dense(1, activation='sigmoid'))
    
    
    lr = hp.Choice("learning_rate", values=[1e-1, 1e-2, 1e-3])
    opt = SGD(learning_rate=lr, momentum=0.9)
    
    model.compile(optimizer=opt, loss='binary_crossentropy', metrics=['accuracy'])
    
    return(model)
tuner = Hyperband(build_model,
                     objective="val_accuracy",
                     max_epochs=20,
                     factor=3,seed=2,
                     directory=config.OUTPUT_PATH,
                     project_name=args["tunes"])
step_size_train = train_gen.n//train_gen.batch_size
step_size_val = val_gen.n//val_gen.batch_size
es = EarlyStopping(monitor='val_acc', patience = 5)
tuner.search(x=train_gen, 
             validation_data=val_gen, 
             steps_per_epoch=step_size_train, 
             validation_steps=step_size_val, 
             epochs=1, 
             verbose=2
             )
print(tuner.results_summary())
tuner.get_best_models()[0].summary()
#tuner.get_best_models()[0].predict()<jupyter_output>Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d (Conv2D)              (None, 298, 298, 64)      1792      
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 296, 296, 32)      18464     
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 148, 148, 32)      0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 146, 146, 64)      18496     
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 144, 144, 32)      18464     
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 72, 72, 32)        0         
________________________________________________________[...]
{'fraction_non_alphanumeric': 0.10765743888619071, 'fraction_numerical': 0.036425449247207385, 'mean_word_length': 2.337655321447866, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 36, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '497029', 'license': 'MIT', 'n_tokens_mistral': 1816, 'n_tokens_neox': 1624, 'n_words': 337, 'path': '02_Anomaly Detection/Modell_0.1_KerasTuner2.ipynb', 'repo_name': 'daimonae/retina', 'size': '6177'}