Search code examples
pythontensorflowkeras

The following argument(s) are not supported with the native Keras format: ['options']


I am building a Keras deep learning Algorithm on dogs vs cats dataset. I am able to run my code in colab. But in Jupyter lab I am getting this error.

The following argument(s) are not supported with the native Keras format: ['options']

Below is the code:

import os
import shutil
import pathlib

original_dir = pathlib.Path("/content/drive/MyDrive/Reva/dogs_vs_cats/train/train")
new_base_dir = pathlib.Path("/content/drive/MyDrive/Reva/dogs_vs_cats/")

def make_subset(subset_name, start_index, end_index):
    for category in ("cat", "dog"):
        dir = new_base_dir / subset_name / category

        # Check if the folder exists and delete it if it does
        if os.path.exists(dir):
            shutil.rmtree(dir)

        # Create the folder again
        os.makedirs(dir)

        fnames = [f"{category}.{i}.jpg" for i in range(start_index, end_index)]
        for fname in fnames:
            shutil.copyfile(src=original_dir / fname,
                            dst=dir / fname)

make_subset("train", start_index=0, end_index=1000)
make_subset("validation", start_index=1000, end_index=1500)
make_subset("test", start_index=1500, end_index=2500)

from tensorflow.keras.utils import image_dataset_from_directory

train_dataset = image_dataset_from_directory(
    new_base_dir / "train",
    image_size=(180, 180),
    batch_size=32)
validation_dataset = image_dataset_from_directory(
    new_base_dir / "validation",
    image_size=(180, 180),
    batch_size=32)
test_dataset = image_dataset_from_directory(
    new_base_dir / "test",
    image_size=(180, 180),
    batch_size=32)

from tensorflow import keras
from tensorflow.keras import layers

inputs = keras.Input(shape=(180, 180, 3))
x = layers.Rescaling(1./255)(inputs)
x = layers.Conv2D(filters=32, kernel_size=3, activation="relu")(x)
x = layers.MaxPooling2D(pool_size=2)(x)
x = layers.Conv2D(filters=64, kernel_size=3, activation="relu")(x)
x = layers.MaxPooling2D(pool_size=2)(x)
x = layers.Conv2D(filters=128, kernel_size=3, activation="relu")(x)
x = layers.MaxPooling2D(pool_size=2)(x)
x = layers.Conv2D(filters=256, kernel_size=3, activation="relu")(x)
x = layers.MaxPooling2D(pool_size=2)(x)
x = layers.Conv2D(filters=256, kernel_size=3, activation="relu")(x)
x = layers.Flatten()(x)
outputs = layers.Dense(1, activation="sigmoid")(x)
model = keras.Model(inputs=inputs, outputs=outputs)

model.compile(loss="binary_crossentropy",
              optimizer="rmsprop",
              metrics=["accuracy"])

callbacks = [
    keras.callbacks.ModelCheckpoint(
        filepath="convnet_from_scratch.keras",
        save_best_only=True,
        monitor="val_loss")
]

history = model.fit(
    train_dataset,
    epochs=30,
    validation_data=validation_dataset,
    callbacks=callbacks)

I need to know how to resolve the above code. Any suggestions to improve the time required to run the code is also welcome.


Solution

  • As I mentioned in the comments, there seems to be a weird behaviour related to keras saving and also versioning of TF/Keras. I could replicate your error when running TF/Keras with version 2.13 (newest right now) on colab. Standard install on colab is 2.12, where the error doesn't come up.
    So one solution would be to downgrade TF/Keras to 2.12.x, or change

    keras.callbacks.ModelCheckpoint(
            filepath="convnet_from_scratch.keras",
            ..)
    

    to

    keras.callbacks.ModelCheckpoint(
            filepath="convnet_from_scratch.x",
            ..)
    

    where x stands for whatever you fancy (NOT "keras") to not save in the .keras format.