I’ve been working on a gender/age prediction model, I started with age but was having issues so I switched to gender, thinking it may work better. I am using Keras with Tensorflow. My loss and accuracy, even when working with age, has improved, but the val_accuracy or val_loss tend to stagnate very quickly or even get worse as time goes on.
Most sources I’ve found discuss overfitting, which makes sense, but I’ve tried many techniques to prevent this with no luck. Dropout layers, preprocessing, data augmentation, early stopping, and more, but nothing has helped. From a simple model to adding a lot of suggestions, it has always had the same problem. I tried a few existing models with no help such as MobileNetV2,
for index, row in trainprofile_df.iterrows():
user_data = (row['gender'])
train_labels.append(user_data)
train_ds = tf.keras.utils.image_dataset_from_directory(
"I:/Programming/Python/training/image/",
labels=None,
color_mode="rgb",
image_size=(50, 50),
batch_size=32)
images = []
for image_batch in train_ds:
images.extend(image_batch)
train_ds_array = np.array(images)
labels_array = np.array(train_labels)
X_train, X_test, y_train, y_test = train_test_split(train_ds_array, labels_array, test_size=0.2, random_state=42)
train_array = np.array(X_train)
train_array_val = np.array(X_test)
train_array_val = (train_array_val / 255.0)
mean = np.mean(train_array_val, axis=0)
std = np.std(train_array_val, axis=0)
centered_standardized_X = (train_array_val - mean) / std
X = centered_standardized_X
X = (train_array / 255.0)
X = preprocess_input(X)
early_stopping = EarlyStopping(monitor='val_loss', patience=15, restore_best_weights=True)
conv_base = tf.keras.applications.MobileNetV2(weights='imagenet', include_top=False, input_shape=(50, 50, 3), classifier_activation="relu")
imgModel = models.Sequential([
conv_base,
layers.BatchNormalization(),
layers.Dropout(0.25),
layers.Flatten(),
layers.Dense(256, activation='relu'),
layers.Dropout(0.25),
layers.Dense(1, activation='sigmoid')
])
imgModel.compile(optimizer=Adam(learning_rate=0.0001), loss='binary_crossentropy', metrics=['accuracy'])
imgModel.summary()
history = imgModel.fit(X, y_train, epochs=100, validation_data=(train_array_val, y_test), callbacks=[early_stopping])
I’m pretty new to this, so I might be making an obvious mistake.
EvanAbrahamson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.