I 've been trying to train a pre-existing model on an architectural dataset, and no matter what model i try or changes i do, i never seem to get past 60-66% (maybe 70% at best once) validation accuracy, while accuracy itself reaches up to 97%.
So far i have tried ResNet50, ResNet152, InceptionResNetV2, VGG16, i have also tried different optimizers and different image augmentations. Dataset i am using is https://www.kaggle.com/datasets/dumitrux/architectural-styles-dataset only difference is I trimmed off few classes, in particular:
- Achaemenid architecture
- American Foursquare architecture
- Ancient Egyptian architecture
Without those classes the dataset contains 8935 images, for testing and validation split i am using 20% resulting in 7163 images for training and 1790 images for validation. The code i am using:
image_size = (224, 224)
batch_size = 12
aug = ImageDataGenerator(
#rescale=1./255,
rotation_range=10,
#zoom_range=0.10,
width_shift_range=0.1,
height_shift_range=0.1,
#shear_range=0.10,
horizontal_flip=True,
fill_mode="constant",
preprocessing_function=tf.keras.applications.vgg16.preprocess_input)
raw =ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input)
train_generator = aug.flow_from_directory(
'/content/drive/MyDrive/datasetV2',
target_size=image_size,
batch_size=batch_size,
class_mode='sparse')
validation_generator = raw.flow_from_directory(
"/content/drive/MyDrive/dataset_validationV2",
target_size=image_size,
batch_size=batch_size,
class_mode='sparse',
seed=1337)
# model = tf.keras.applications.resnet.ResNet152(
# include_top=True,
# weights='imagenet',
# #weights=None,
# input_tensor=None,
# input_shape=None,
# pooling=None,
# classes=1000,
# #classes=16
# )
# model = tf.keras.applications.inception_resnet_v2.InceptionResNetV2(
# include_top=True,
# weights="imagenet",
# input_tensor=None,
# input_shape=None,
# pooling=None,
# classes=1000,
# classifier_activation="softmax"
# )
model = tf.keras.applications.VGG16(
include_top=True,
weights="imagenet",
input_tensor=None,
input_shape=None,
pooling=None,
classes=1000,
classifier_activation="softmax",
)
# model = tf.keras.applications.resnet50.ResNet50(
# include_top=True,
# weights='imagenet',
# input_tensor=None,
# input_shape=None,
# pooling=None,
# classes=1000,
# )
epochs = 40
callbacks = [
keras.callbacks.ModelCheckpoint("save_at_{epoch}.h5"),
]
model.compile(optimizer='adagrad',loss = tf.keras.losses.SparseCategoricalCrossentropy(), metrics=['accuracy'])
history = model.fit(train_generator, steps_per_epoch=192, epochs=epochs, validation_data=validation_generator, callbacks=callbacks)
Note that everything commented out in the code is what i tried using with no real changes to the resulting validation_accuracy. Of course for each different model i used its own preprocessing and changed the input size to be matching the one each model is made for. I have also tried different optimizers, different batch sizes and number of steps, with same results.
Here is a graph from training InceptionResNetV2 with 32 as batch size and 256 steps, with the same image augmentations you see in the code (optimizer used was Adagrad, Adamax only made validation fluctuate up and down more)
And here is a graph from ResNet152 with Adamax optimizer (same batch and steps, of course with the image dimensions and preprocessing required for ResNet152)
Another example is this graph of when i trained VGG16 model, with batch size of 12 and 192 steps and no image augmentation, just to rule out its those that ruin my validation accuracy, once again getting stuck after 60%, if i let it run for another 40 epochs i might get to 65% but the gain has diminishing returns, eventually hovering around the same percentage.
All the trainings go like this, after 20-30 epochs val_accuracy stays between 60-68%
I have looked at different Stack Overflow posts, or GitHub issues regarding similar problems, and none of the "fixes" i found has helped me. Anyone knows a solution?