My results are not reproducible while the global seed is set. Am I doing something wrong or is this an issue of the macbook M1?
I am using:
- MacOS 12.5
- Python 3.10.6
- tensorflow-macos 2.9.2
The terminal is opened using the use rosetta option.
Besides, the model is also unusually bad.
The datasets are from https://www.kaggle.com/competitions/dogs-vs-cats
# Imports.
import tensorflow as tf
import numpy as np
# Some global variables.
seed = 1234
batch_size = 32
learning_rate = 0.001
width = 180
height = 180
epochs = 250
# Set the seed.
tf.random.set_seed(seed);
np.random.seed(seed);
# Create the cnn model
def cnn_model():
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(32, (3,3), activation="relu", input_shape=(width, height, 3)),
tf.keras.layers.MaxPooling2D((2,2)),
tf.keras.layers.Conv2D(64, (3,3), activation="relu"),
tf.keras.layers.MaxPooling2D((2,2)),
tf.keras.layers.Conv2D(128, (3,3), activation="relu"),
tf.keras.layers.MaxPooling2D((2,2)),
tf.keras.layers.Conv2D(128, (3,3), activation="relu"),
tf.keras.layers.MaxPooling2D((2,2)),
tf.keras.layers.Flatten(),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(512, activation="relu"),
tf.keras.layers.Dense(1, activation="sigmoid"),
])
opt = tf.keras.optimizers.Adam(learning_rate=0.001)
model.compile(optimizer=opt, loss="binary_crossentropy", metrics=["accuracy"])
return model
# Load an image for predictions.
def load_image(path):
img = tf.keras.preprocessing.image.load_img(
path,
target_size=(width, height),
color_mode="rgb",
)
img_tensor = tf.keras.preprocessing.image.img_to_array(img)
img_tensor = np.expand_dims(img_tensor, axis=0)
img_tensor /= 255.
return img_tensor
# Dump the evaluation metrics to the console.
def dump_metrics(message, model, metrics):
index, s = 0, ""
for name in model.metrics_names:
s += f" - {name}: {round(metrics[index], 3)}"
index += 1
print(f"{message} {s[3:]}")
# Define the image data generator.
train_datagen = tf.keras.preprocessing.image.ImageDataGenerator(
rescale = 1./255,
# rotation_range = 45,
# width_shift_range = 0.2,
# height_shift_range = 0.2,
# shear_range = 0.2,
# zoom_range = 0.2
)
val_datagen = tf.keras.preprocessing.image.ImageDataGenerator(rescale = 1./255)
# Load the training dataset.
train_df = train_datagen.flow_from_directory(
"datasets/train/",
class_mode="binary",
batch_size=batch_size,
target_size=(width, height),
shuffle=True,
color_mode="rgb",
seed=seed,
)
train_samples_count = len(train_df)
# Load the validation dataset.
val_df = val_datagen.flow_from_directory(
"datasets/val/",
class_mode="binary",
batch_size=batch_size,
target_size=(width, height),
shuffle=True,
color_mode="rgb",
seed=seed,
)
val_samples_count = len(train_df)
# Create the model.
model = cnn_model();
model.summary()
# Training callbacks.
callbacks = []
callbacks.append(tf.keras.callbacks.EarlyStopping(monitor="val_loss", patience=5, restore_best_weights=True))
# Train the model.
history = model.fit(
train_df,
steps_per_epoch=train_samples_count // batch_size,
epochs=epochs,
validation_data=val_df,
validation_steps=val_samples_count // batch_size,
callbacks=callbacks,
)
dump_metrics("Training:", model, model.evaluate(train_df, verbose=0))
dump_metrics("Valiation:", model, model.evaluate(val_df, verbose=0))