[Python] My tensor flow model cannot get further accuracy

Discussão em 'Python' iniciado por Stack, Julho 29, 2025.

  1. Stack

    Stack Membro Participativo

    i have been testing this library recently, i'm not a good programer now i fugure it out not even with the support of AI i could be able to improve my tensor flow model, no matter what i try it just doesn't work.

    ok i'll start with the base structure of my code and what it supuse to do, first i feed the model with a small data set of less of 3k of images of graphs (2d images), the number of classes are two, and the out put is binary.

    def scheduler(epoch, lr):
    if epoch % 10 == 0 and epoch != 0:
    return lr * 0.5
    return lr

    lr_callback = LearningRateScheduler(scheduler)

    early_stop = EarlyStopping(
    monitor='val_loss',
    patience=50,
    restore_best_weights=True,
    verbose=1
    )

    nombre = f"modelo_AD_{datetime.now().strftime('%Y%m%d_%H%M%S')}.h5"

    checkpoint = ModelCheckpoint(
    filepath=f'/content/drive/MyDrive/modelos_guardados/{nombre}',
    monitor='val_accuracy',
    save_best_only=True,
    verbose=1
    )


    Also used this one with the help of grok

    from tensorflow.keras.callbacks import ReduceLROnPlateau

    reduce_lr = ReduceLROnPlateau(
    monitor='val_loss',
    factor=0.2, # Reduce lr a 20% del valor actual
    patience=5, # Espera 5 épocas sin mejora
    min_lr=1e-6, # Límite inferior para lr
    verbose=1
    )

    early_stop = EarlyStopping(
    monitor='val_loss',
    patience=10, # Reducir a 10 épocas
    restore_best_weights=True,
    verbose=1
    )

    import os
    from tensorflow.keras.callbacks import ModelCheckpoint
    from datetime import datetime

    nombre = f"modelo_AD_{datetime.now().strftime('%Y%m%d_%H%M%S')}.h5"
    os.makedirs('/content/drive/MyDrive/modelos_guardados', exist_ok=True) # Crear directorio si no existe
    checkpoint = ModelCheckpoint(
    filepath=f'/content/drive/MyDrive/modelos_guardados/{nombre}',
    monitor='val_loss', # Cambiar a val_loss
    save_best_only=True,
    verbose=1
    )

    X_entrenamiento_AD = X [:2364]
    X_validacion_AD = X [2364:]
    y_entrenamiento_AD = y [:2364]
    y_validacion_AD = y [2364:]


    I used this model

    modeloCNN2 = tf.keras.models.Sequential([
    tf.keras.layers.Conv2D(32, (3,3), activation='relu', input_shape=input_shape),
    tf.keras.layers.MaxPooling2D(2, 2),
    BatchNormalization(),
    tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
    tf.keras.layers.MaxPooling2D(2, 2),
    BatchNormalization(),
    tf.keras.layers.Conv2D(128, (3,3), activation='relu'),
    tf.keras.layers.MaxPooling2D(2, 2),
    BatchNormalization(),
    tf.keras.layers.Dropout(0.5),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(250, activation='relu', kernel_regularizer=tf.keras.regularizers.l2(0.001)),
    BatchNormalization(),
    tf.keras.layers.Dense(1, activation='sigmoid')
    ])
    modeloCNN2.compile(
    optimizer="Adam",
    loss='binary_crossentropy',
    metrics=['accuracy']
    )


    and also this one

    #EXPERIMENTAL

    modeloCNN2 = tf.keras.models.Sequential([

    tf.keras.layers.Conv2D(256, (3,3), activation='relu', input_shape=input_shape, padding="same"),
    tf.keras.layers.MaxPooling2D(2, 2),
    BatchNormalization(),

    tf.keras.layers.Conv2D(128, (3,3), activation='relu', padding="same"),
    tf.keras.layers.MaxPooling2D(2, 2),
    BatchNormalization(),

    Conv2D(16, (3,3), activation='relu', padding="same"),
    AveragePooling2D(2,2),
    BatchNormalization(),


    tf.keras.layers.Dropout(0.3),
    tf.keras.layers.Flatten(),

    tf.keras.layers.Dense(128, activation='relu', kernel_regularizer=tf.keras.regularizers.l2(0.001)),
    BatchNormalization(),

    tf.keras.layers.Dense(64, activation='relu', kernel_regularizer=tf.keras.regularizers.l2(0.001)),
    BatchNormalization(),
    tf.keras.layers.Dense(1, activation='sigmoid')
    ])

    modeloCNN2.compile(
    optimizer="Adam",
    loss='binary_crossentropy',
    metrics=['accuracy']
    )


    And i started the training with this code in both cases, changing the callbacks depending of what callback block i preferred, in order to test i used both

    tensorboardCNN2 = TensorBoard(log_dir='logs/cnn2_AD')
    modeloCNN2.fit(data_gen_entrenamiento,
    batch_size=32,
    validation_data=(X_validacion_AD, y_validacion_AD),
    steps_per_epoch=int(np.ceil(len(X_entrenamiento_AD) / float(32))),
    validation_steps=int(np.ceil(len(X_validacion_AD) / float(32))),
    epochs=100,
    callbacks=[tensorboardCNN2, checkpoint, reduce_lr, early_stop])


    So the problem is that the result is always the same, High training value accuracy, low loss in training, mid (between 50% of val accuracy) and mid (between 50% of val loss), in both cases, i tested other optimizer instead of adam, but they just get it worst. At first i started with a data set of 20k images but the results were worst, so i started over with a smaller a newer data set that was validated to avoid redundant data.

    Also i decreased and increased the layers but it just make the training faster or slower, the best val accuracy i got was 60% with a val loss of 50%, no matter what i move i just can have better results.

    Continue reading...

Compartilhe esta Página