简体   繁体   English

如何正确使用 tfa.metrics.F1Score 和 image_dataset_from_directory?

[英]How to use tfa.metrics.F1Score with image_dataset_from_directory correctly?

Colab code is here : Colab 代码在这里

I am following the docs here to get the result for multiclass prediction我正在关注此处的文档以获得多类预测的结果

When I train using当我训练使用

#last layer
tf.keras.layers.Dense(2, activation='softmax')

model.compile(optimizer="adam",
              loss=tf.keras.losses.CategoricalCrossentropy(),
              metrics=[tf.keras.metrics.CategoricalAccuracy(),
                       tfa.metrics.F1Score(num_classes=2, average='macro')])

I get我明白了

144/144 [==] - 8s 54ms/step - loss: 0.0613 - categorical_accuracy: 0.9789 - f1_score: 0.9788 - val_loss: 0.0826 - val_categorical_accuracy: 0.9725 - val_f1_score: 0.9722

When I do:当我做:

model.evaluate(val_ds)

I get我明白了

16/16 [==] - 0s 15ms/step - loss: 0.0826 - categorical_accuracy: 0.9725 - f1_score: 0.9722
[0.08255868405103683, 0.9725490212440491, 0.9722140431404114]

I would like to use the metric.result as in the official website.我想在官方网站上使用metric.result When I load the below code, I get 0.4875028 which is wrong.当我加载下面的代码时,我得到0.4875028这是错误的。 How can I get the correct predicted_categories and true_categories ?如何获得正确的true_categories predicted_categories

metric = tfa.metrics.F1Score(num_classes=2, average='macro')

predicted_categories = model.predict(val_ds)
true_categories = tf.concat([y for x, y in val_ds], axis=0).numpy() 

metric.update_state(true_categories, predicted_categories)
result = metric.result()
print(result.numpy())

#0.4875028

Here is how I loaded my data这是我加载数据的方式

train_ds = tf.keras.preprocessing.image_dataset_from_directory(
    main_folder,
    validation_split=0.1,
    subset="training",
    label_mode='categorical',
    seed=123,
    image_size=(dim, dim))

val_ds = tf.keras.preprocessing.image_dataset_from_directory(
    main_folder,
    validation_split=0.1,
    subset="validation",
    label_mode='categorical',
    seed=123,
    image_size=(dim, dim))

From: https://www.tensorflow.org/api_docs/python/tf/keras/preprocessing/image_dataset_from_directory来自: https://www.tensorflow.org/api_docs/python/tf/keras/preprocessing/image_dataset_from_directory

tf.keras.preprocessing.image_dataset_from_directory(
    directory, labels='inferred', label_mode='int',
    class_names=None, color_mode='rgb', batch_size=32, image_size=(256,
    256), shuffle=True, seed=None, validation_split=None, subset=None,
    interpolation='bilinear', follow_links=False
)

The shuffle by default is True , and that is a problem for your val_ds , which we do not want to shuffle.默认情况下, shuffleTrue ,这对您的val_ds来说是个问题,我们不想随机播放。

The correct metrics are the ones reported during the training;正确的指标是训练期间报告的指标; Also I recommend that you can also manually retrieve your validation dataset and check the metrics once you make predictions on it (not necessarily via flow_from_directory() ).此外,我建议您也可以手动检索验证数据集并在对其进行预测后检查指标(不一定通过flow_from_directory() )。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 keras image_dataset_from_directory 与自定义结构一起使用? - How to use keras image_dataset_from_directory with custom structures? 丢失 function 与 image_dataset_from_directory 一起使用 - Loss function to use with 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? Tensorflow image_dataset_from_directory 用于输入数据集和 output 数据集 - Tensorflow image_dataset_from_directory for input dataset and output dataset 如何将从 image_dataset_from_directory 获得的数据集拆分为数据和标签? - How can I split the dataset obtained from image_dataset_from_directory into data and labels? 是否可以从 image_dataset_from_directory 获取图像名称? - is it possible to get image name from image_dataset_from_directory? 如何在专辑 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? 如何从 Keras 中的 image_dataset_from_directory() 从 MapDataset 附加或获取文件名? - How to attach or get filenames from MapDataset from image_dataset_from_directory() in Keras?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM