【发布时间】:2021-01-20 08:59:14
【问题描述】:
我正在创建一个完全卷积的神经网络,它给定输入中的图像,能够识别其中的区域(黑色,0),还可以识别背景(白色,255)。 我的目标是二值化图像(范围 0-255),我想在我的两个语义类(0 或 255)之间取得一些平衡。 事实上,我得到的“特殊”区域 (0) 是背景区域 (255) 的 1.8 倍,所以我需要抵消这种影响,我想更多地惩罚在背景上犯错误的事实,以避免仅预测特殊区域。
我尝试关注一些关于它的主题,这似乎并不难,但我在我的实施中陷入困境,我真的不知道为什么。
每次我的实现在编译阶段工作,但只有在拟合步骤中它才会返回错误。
到目前为止,这是我尝试过的:
import keras.backend as kb
def custom_binary_crossentropy(y_true, y_pred):
"""
Used to reequilibrate the data, as there is more black (0., articles), than white (255., non-articles) on the pages.
"""
if y_true >=128: # Half the 0-255 range
return -1.8*kb.log(y_pred/255.)
else:
return -kb.log(1-(y_pred/255.))
但它返回了:
InvalidArgumentError: The second input must be a scalar, but it has shape [16,256,256]
[[{{node gradient_tape/custom_binary_crossentropy/cond/StatelessIf/gradient_tape/custom_binary_crossentropy/weighted_loss/Mul/_17}}]] [Op:__inference_train_function_24327]
Function call stack:
train_function
我不太明白这个错误。
我之前尝试过:
def custom_binary_crossentropy(y_true, y_pred):
"""
Used to reequilibrate the data, as there is more black (0., articles), than white (255., non-articles) on the pages.
"""
if y_true >=128: # Half the 0-255 range
return 1.8*keras.losses.BinaryCrossentropy(y_true, y_pred)
else:
return keras.losses.BinaryCrossentropy(y_true, y_pred)
但我得到了:
TypeError: in user code:
/Users/axeldurand/opt/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py:806 train_function *
return step_function(self, iterator)
<ipython-input-67-7b6815236f63>:6 custom_binary_crossentropy *
return -1.8*keras.losses.BinaryCrossentropy(y_true, y_pred)
TypeError: unsupported operand type(s) for *: 'float' and 'BinaryCrossentropy'
我有点困惑,Keras 总是让它变得如此简单,我必须省略一些简单的东西,但我真的不明白。
【问题讨论】:
标签: python keras loss-function cross-entropy