[英]Loss function to use with image_dataset_from_directory
I am confused about the appropriate loss function to use as i am generating my dataset using image_dataset_from_directory.我对使用 image_dataset_from_directory 生成数据集时使用的适当损失 function 感到困惑。
Data Generator数据生成器
Train火车
train_ds = tf.keras.utils.image_dataset_from_directory(
'/content/dataset/train',
validation_split=0.05,
subset="training",
seed=123,
image_size=(IMAGE_SIZE, IMAGE_SIZE),
batch_size=BATCH_SIZE)
Validation验证
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
'/content/dataset/val', image_size=(IMAGE_SIZE, IMAGE_SIZE), batch_size=BATCH_SIZE
)
Model Model
rn50v2_model = Sequential()
pretrained_model = tf.keras.applications.ResNet50V2(
include_top=False,
weights="imagenet",
input_shape=(IMAGE_SIZE, IMAGE_SIZE, 3),
pooling='avg',
classes = 2
)
print(pretrained_model.summary())
rn50v2_model.add(pretrained_model)
rn50v2_model.add(Flatten())
rn50v2_model.add(Dense(512, activation='relu'))
rn50v2_model.add(Dense(2, activation='softmax'))
#print(rn50v2_model.summary())
rn50v2_model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
when i tested my model, i got my result one hot encoded like below:当我测试我的 model 时,我得到了一个热编码的结果,如下所示:
array([[0.24823777, 0.7517622 ]], dtype=float32)
I would prefer to use categorical_crossentropy
but pls explain this behaviour i cant seem to find information on the official documentation我更喜欢使用categorical_crossentropy
但请解释这种行为我似乎无法在官方文档中找到信息
Your result is not one hot encoded.您的结果不是一个热编码。 It is the result of your output layer squished by the softmax function.这是你的 output 层被 softmax function 压扁的结果。 This function transforms all values between 0 and 1 and these values sum to 1. You can apply np.argmax(predictions, axis=-1)
to these "probabilities" to get the corresponding class.此 function 转换 0 和 1 之间的所有值,并且这些值总和为 1。您可以将np.argmax(predictions, axis=-1)
应用于这些“概率”以获得相应的 class。 To use categorical_crossentropy
, try changing your label_mode
to categorical
, which will automatically generated one-hot-encoded labels:要使用categorical_crossentropy
,请尝试将您的label_mode
更改为categorical
,这将自动生成 one-hot-encoded 标签:
train_ds = tf.keras.utils.image_dataset_from_directory(
'/content/dataset/train',
validation_split=0.05,
subset="training",
seed=123,
label_mode = 'categorical',
image_size=(IMAGE_SIZE, IMAGE_SIZE),
batch_size=BATCH_SIZE)
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
'/content/dataset/val',
subset="validation",
seed=123,
label_mode = 'categorical',
image_size=(IMAGE_SIZE, IMAGE_SIZE),
batch_size=BATCH_SIZE)
You could also consider using binary_crossentropy
if you only have two classes.如果你只有两个类,你也可以考虑使用binary_crossentropy
。 You would have to change your loss function and output layer:您将不得不更改您的损失 function 和 output 层:
rn50v2_model.add(Dense(1, activation='sigmoid'))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.