0
$\begingroup$

No matter what I change, the accuracy of this ResNet I have modified to perform binary classification seems to stay the same. Loss decreases slightly then fluctuates while accuracy is always 0.4031. What's going on here? I thought it had something to do with the class_weight parameter on .fit() but it doesn't change even when I comment this line out.

import tensorflow as tf
import tensorflow.keras as keras
import pandas as pd
import os

train_ds = keras.utils.image_dataset_from_directory(
    "gaussian_filtered_images/binary/",
    batch_size=32,
    label_mode="binary",
    image_size=(224,224),
    shuffle=True,
    seed=123,
    validation_split=0.2,
    subset="training"
)
val_ds = keras.utils.image_dataset_from_directory(
    "gaussian_filtered_images/binary/",
    batch_size=32,
    label_mode="int",
    image_size=(224,224),
    shuffle=True,
    seed=123,
    validation_split=0.2,
    subset="validation"
)
normalization_layer = tf.keras.layers.Rescaling(1./255)
train_ds = train_ds.map(lambda x, y: (normalization_layer(x), y))
val_ds = val_ds.map(lambda x, y: (normalization_layer(x), y))

model = keras.Sequential()
pretrained_section = tf.keras.applications.ResNet50(include_top=False, input_shape=(224,224,3),  weights="imagenet")
for layer in pretrained_section.layers:
    layer.trainable=False
model.add(pretrained_section)

model.add(keras.layers.Flatten())
model.add(keras.layers.Dense(512, activation="relu"))
model.add(keras.layers.Dense(1, activation="softmax"))
model.compile(optimizer='SGD', loss=keras.losses.BinaryCrossentropy(), metrics=[keras.metrics.Accuracy(), keras.metrics.Precision(), keras.metrics.Recall(), keras.metrics.AUC()])

history = model.fit(
    train_ds,
    validation_data=val_ds,
    epochs=20,
    callbacks=None,
    class_weight={0:1, 1:2175/688}
)
```
$\endgroup$

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.