So I’m trying to fine tune ResNet50, but i’m new doing that, so I’m trying with a binary classification, but I’m not sure if I’m setting it correctly, because my accuracy at the end is weird, apparently the epochs reach 0,8, but when I’m testing it is very low, here’s my code:
import tensorflow as tf
from sklearn.metrics import roc_auc_score
from tensorflow.keras.layers import Dense, Flatten, Dropout
from tensorflow.keras.callbacks import EarlyStopping
base_model = tf.keras.applications.ResNet50(
include_top=False,
weights='imagenet',
input_shape=(224, 224, 3),
classifier_activation='sigmoid',
)
pre_process_fn = tf.keras.applications.resnet50.preprocess_input
x = base_model.output
x = Flatten()(x)
x = Dense(1024, activation='relu')(x)
x = Dense(512, activation='relu')(x)
x = Dense(256, activation='relu')(x)
x = Dropout(0.5)(x)
predictions = Dense(1, activation='sigmoid')(x)
fine_tune_model = tf.keras.Model(inputs=base_model.input, outputs=predictions)
for layer in base_model.layers:
layer.trainable = False
fine_tune_model.compile(loss=tf.keras.losses.BinaryCrossentropy(),
metrics=[tf.keras.metrics.BinaryAccuracy()],
optimizer=tf.keras.optimizers.Adam(learning_rate=0.0001))
new_size = (224, 224)
def preprocess(image, label):
image = tf.image.resize(image, new_size)
image = pre_process_fn(image)
return image, label
train_dataset = train_dataset.map(preprocess)
val_dataset = val_dataset.map(preprocess)
test_dataset = test_dataset.map(preprocess)
BATCH_SIZE = 32
train_dataset = train_dataset.shuffle(1000).batch(BATCH_SIZE)
val_dataset = val_dataset.batch(BATCH_SIZE)
test_dataset = test_dataset.batch(BATCH_SIZE)
early_stopping = EarlyStopping(monitor='val_loss', patience=10, restore_best_weights=True)
history_fine_tune_model = fine_tune_model.fit(train_dataset, epochs=100,
validation_data=val_dataset, callbacks=[early_stopping])
test_loss, test_acc = fine_tune_model.evaluate(test_dataset, verbose=1)
y_pred_prob = fine_tune_model.predict(test_dataset)
auc = roc_auc_score(test_labels, y_pred_prob)
print("AUC:", auc)
print('nTest accuracy:', test_acc)
New contributor
William Fernandez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.