简体   繁体   English

丢失 function 与 image_dataset_from_directory 一起使用

[英]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.

相关问题 如何将 keras image_dataset_from_directory 与自定义结构一起使用? - How to use keras image_dataset_from_directory with custom structures? Tensorflow image_dataset_from_directory function label 形状 - Tensorflow image_dataset_from_directory function label shape Tensorflow image_dataset_from_directory 用于输入数据集和 output 数据集 - Tensorflow image_dataset_from_directory for input dataset and output dataset 从 image_dataset_from_directory function 生成的数据集不包括 batch size - Dataset generated from image_dataset_from_directory function does not include batch size 如何在专辑 label 中调整数据集 label 的大小以使用 tensorflow image_dataset_from_directory ZC1C425268E687A945D - How resize dataset label in albumentations label to work with tensorflow image_dataset_from_directory function? 如何查看keras的image_dataset_from_directory function生成的数据集? - How to view the dataset generated by the image_dataset_from_directory function of keras? 是否可以从 image_dataset_from_directory 获取图像名称? - is it possible to get image name from image_dataset_from_directory? Keras:如何使用`image_dataset_from_directory`加载测试集? - Keras: How to use `image_dataset_from_directory` to load test set? 如何理解 image_dataset_from_directory() 并将其用作 X、y 输入? - How understand image_dataset_from_directory() and use it as X, y input? 如何正确使用 tfa.metrics.F1Score 和 image_dataset_from_directory? - How to use tfa.metrics.F1Score with image_dataset_from_directory correctly?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM