0

my code will run only one time after that it will only display how many images found in each flow from directory. It run the tuner once and then won't run it again, maybe it thinks I have already ran the code and don'tt need to again. I think it has a built in checkpoint?

this is my code.

`import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt
import os
import pathlib
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import keras_tuner as kt
train_directory = '/home/student/Documents/ML & AI Team/Benjamin Collier/datasets/labeled folders/training'
test_directory = "/home/student/Documents/ML & AI Team/Benjamin Collier/datasets/labeled folders/test"
image_width = 250
image_height = 250
batch_size = 64

pys_d = tf.config.list_physical_devices("GPU")

#tf.config.set_visible_devices(pys_d[0], 'GPU')
tf.config.experimental.set_memory_growth(pys_d[0],True)


datagen = ImageDataGenerator(
    rescale = 1./255,
    validation_split=0.0,
    data_format='channels_last',
    horizontal_flip=True, 
    vertical_flip=True,
    rotation_range=15,
    brightness_range=[0.6,1.5],
    width_shift_range=0.05,
    height_shift_range=0.05,
    #dtype = tf.float32,
)
train_gen = datagen.flow_from_directory(
    train_directory,
    target_size=(image_width,image_height),
    batch_size = batch_size,
    #color_mode = 'grayscale',
    class_mode = 'sparse',
    shuffle =True,
    subset = 'training',
    seed=13,

)
testgen = ImageDataGenerator(
    rescale = 1./255,
    validation_split=0.0,
    data_format='channels_last',


    #dtype = tf.float32,
)
test_gen = testgen.flow_from_directory(
    test_directory,
    target_size=(image_width,image_height),
    batch_size = batch_size,
    #color_mode = 'grayscale',
    class_mode = 'sparse',
    shuffle = True,
    subset = 'training',
    seed=13,

)

def model_finder(hp):
  model = tf.keras.Sequential()
  hp_layer_1 = hp.Int('layer_1',min_value=10,max_value=60, step=5)
  hp_layer_2 = hp.Int('layer_2',min_value=10,max_value=60, step=5)
  hp_layer_1fliter = hp.Int('kernel_1',min_value=1,max_value=6, step=1)
  hp_maxpool = hp.Int('maxpool',min_value=1,max_value=4, step=1)
  hp_maxpool2 = hp.Int('maxpool2',min_value=1,max_value=4,step=1)
  hp_maxpool3 = hp,Int('maxpool3',min_value=1,max_value=4,step=1)
  hp_layer_2fliter = hp.Int('kernel_2',min_value=1,max_value=6, step=1)
  hp_layer_3 = hp.Int('layer_3',min_value=10,max_value=60,step=5)
  hp_layer_3kernel = hp.Int('kernel_3',min_value=1,max_value=6, step=1)
  hp_layer_4 = hp.Int('layer_3',min_value=64,max_value=256,step=64)
  hp_learning_rate = hp.Choice('learning_rate', values=[1e-2,1e-3,1e-4])

  model.add(tf.keras.layers.Conv2D(filters=hp_layer_1, activation='relu',kernel_size=hp_layer_1fliter,input_shape=(image_width,image_height,3)))
  model.add(tf.keras.layers.MaxPool2D(pool_size=(hp_maxpool)))
  model.add(tf.keras.layers.Conv2D(filters=hp_layer_2, activation='relu',kernel_size=hp_layer_2fliter))
  model.add(tf.keras.layers.MaxPool2D(pool_size=(hp_maxpool2)))
  model.add(tf.keras.layers.Conv2D(filters=hp_layer_3, activation='relu',kernel_size=hp_layer_3kernel))
  model.add(tf.keras.layers.MaxPool2d(pool_size=(hp_maxpool3)))
  model.add(tf.keras.layers.Flatten())
  model.add(tf.keras.layers.Dropout(0.1))
  model.add(tf.keras.layers.Dense(units=hp_layer_4,activation='relu'))
  model.add(tf.keras.layers.Dense(2, activation='sigmoid'))

  model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=hp_learning_rate),
                loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
                metrics=['accuracy'])
  return model

tuner = kt.Hyperband(model_finder,
                     objective='val_accuracy',
                     max_epochs=30,
                     )

stop_early = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=5)
tuner.search(train_gen, epochs=50,validation_data=test_gen, callbacks=[stop_early])


`

I tried killing previos python task to see if it was interfiring with my program some how.

benjam
  • 1

1 Answers1

0

You have to set the overwrite parameter of the Tuner to true.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 13 '23 at 05:29