0

my neural network written in keras, for the problem of binary image classification, after selecting hyperparameters using the keras tuner, produces only zeros.

import keras_tuner
from kerastuner import BayesianOptimization
from keras_tuner import Objective
from tensorflow.keras.models import Model
from tensorflow.keras.applications import Xception
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Dropout
from tensorflow.keras.layers import Flatten
        
  
from torch.nn.modules import activation
        
def build_model(hp):
        
    # create the base pre-trained model
    base_model = Xception(include_top=False, input_shape=(224, 224, 3))
          
    base_model.trainable  = False
        
    x = base_model.output
    x = Flatten()(x)
        
    hp_units = hp.Int('units', min_value=32, max_value=4096, step=32)
    x = Dense(units = hp_units, activation="relu")(x)
          
    hp_rate = hp.Float('rate', min_value = 0.01, max_value=0.9, step=0.01)
    x = Dropout(rate = hp_rate)(x)
        
    predictions = Dense(1, activation='sigmoid')(x)
        
    # this is the model we will train
    model = Model(inputs=base_model.input, outputs=predictions)
        
    hp_learning_rate = hp.Float('learning_rate', max_value = 1e-2, min_value = 1e-7, step = 0.0005)
    optimizer = hp.Choice('optimizer', ['adam', 'sgd', 'adagrad', 'rmsprop'])
        
    model.compile(optimizer,
                  loss=tf.keras.losses.BinaryCrossentropy(),
                  metrics=['accuracy'])
        
    return model
    
stop_early = keras.callbacks.EarlyStopping(monitor='val_loss', patience=5, min_delta= 0.0001)
    
tuner = BayesianOptimization(
        hypermodel = build_model,
        objective = Objective(name="val_accuracy",direction="max"),
        max_trials = 10,
        directory='/content/best_model_s',
        overwrite=False
        )
    
tuner.search(train_batches,
             validation_data = valid_batches,
             epochs = 100,
             callbacks=[stop_early]
                 )
    
best_hps=tuner.get_best_hyperparameters(num_trials=1)[0]
    
model = tuner.hypermodel.build(best_hps)
history = model.fit(train_batches, validation_data = valid_batches ,epochs=50)
    
val_acc_per_epoch = history.history['val_accuracy']
best_epoch = val_acc_per_epoch.index(max(val_acc_per_epoch)) + 1
print('Best epoch: %d' % (best_epoch,))
    
best_model = tuner.hypermodel.build(best_hps)
    
# Retrain the model
best_model.fit(train_batches, validation_data = valid_batches , epochs=best_epoch)
    
test_generator.reset()
predict = best_model.predict_generator(test_generator, steps = len(test_generator.filenames))

I'm guessing that maybe the problem is that the ImageDataGenerator is fed to train with 2 batches of 16 images each, and to test the ImageDataGenerator with 2 batches of 4 images (each batch has an equal number of class representatives).I also noticed that with a small number of epochs, the neural network produces values ​​from 0 to 1, but the more epochs, the closer the response of the neural network is to zero. For a solution, I tried to stop training as soon as the next 5 iterations do not decrease the loss on validation. Again, it seems to me that the matter is in the validation sample, it is very small.

Any advice?

desertnaut
  • 57,590
  • 26
  • 140
  • 166
  • is your dataset highly imbalanced? – Zabir Al Nazi Sep 20 '22 at 08:20
  • Classes are balanced. There are 40 images in total, of which 20 images belong to one class and 20 to another. I took 4 images from both classes and formed a validation set. So there are 32 images in the training set and 8 in the validation set. I created an ImageDataGenerator for both the training and validation sets. In the training one there are 2 packages of 16 images in each (of which 9 images belong to one class, 7 to another), and in the control one there are two packages of 4 images in each (of which 2 images belong to one class, 2 to another). – Eduard Ganzha Sep 20 '22 at 09:00
  • Total number of samples in the dataset? – Zabir Al Nazi Sep 20 '22 at 10:04
  • What do you mean by sample? – Eduard Ganzha Sep 20 '22 at 10:20
  • Maybe 40 samples are not enough, I see you freeze the weight. But that should not be enough. – Zabir Al Nazi Sep 20 '22 at 10:22
  • To somehow increase the data, I used augmentation, but apparently this is not enough? – Eduard Ganzha Sep 20 '22 at 11:18

0 Answers0