arr*_*sea 2 python keras tensorflow
简而言之,我正在努力尝试将 a 中的图像每像素类别掩码tf.data.Dataset从整数类编码转换为单热编码。
考虑这里的图像分割 tensorflow 教程示例:https ://www.tensorflow.org/tutorials/images/segmentation 。
输入是图像,输出是按像素整数标记的类别掩码。在他们的示例中,掩码在每个像素处都有一个由整数表示的类别值:{0、1 或 2}。
的train和test的变量的类型的tf.data.Dataset ,并且每个样品是(图像,掩模)元组。
这种形式的掩码/输出与sparse_categorical_crossentropy教程中的损失函数是一致的。但是,我希望能够使用其他需要单热编码的损失函数。
我一直在尝试tf.keras.utils.to_categorical()使用 map() 调用通过函数转换数据集,即:
def mask_to_categorical(image, mask):
mask = tf.keras.utils.to_categorical(mask,3)
return image, mask
train = train.map(mask_to_categorical)
Run Code Online (Sandbox Code Playgroud)
但是,这失败并出现错误,例如:
{...}/tensorflow_core/python/keras/utils/np_utils.py:40 to_categorical
y = np.array(y, dtype='int')
TypeError: __array__() takes 1 positional argument but 2 were given
Run Code Online (Sandbox Code Playgroud)
笔记:
到目前为止,我的搜索指出急切/非急切问题是一个可能的原因。值得一提的是,我通过以下方式验证了我正在以 Eager 模式运行:
>>> print('tf.executing_eagerly() = ', tf.executing_eagerly())
tf.executing_eagerly() = True
Run Code Online (Sandbox Code Playgroud)
有什么建议?谢谢!
小智 6
尝试修改您的单热编码函数,如下所示:
def mask_to_categorical(image, mask):
mask = tf.one_hot(tf.cast(mask, tf.int32), 3)
mask = tf.cast(mask, tf.float32)
return image, mask
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1952 次 |
| 最近记录: |