[英]Can't add standard metrics for multioutput model
I have classification + detection model of cats and dogs based on MobileNet v2.我有基于MobileNet v2的猫狗分类+检测model。 It trains well, but now I want to add metrics for it and I can't do that.它训练得很好,但现在我想为它添加指标,但我做不到。 Here is the main part of code:这是代码的主要部分:
def localization_loss(y_true, yhat):
delta_coord = tf.reduce_sum(tf.square(y_true[:,:2] - yhat[:,:2]))
h_true = y_true[:,3] - y_true[:,1]
w_true = y_true[:,2] - y_true[:,0]
h_pred = yhat[:,3] - yhat[:,1]
w_pred = yhat[:,2] - yhat[:,0]
delta_size = tf.reduce_sum(tf.square(w_true - w_pred) + tf.square(h_true-h_pred))
return delta_coord + delta_size
classloss = tf.keras.losses.BinaryCrossentropy()
regressloss = localization_loss
opt = tf.keras.optimizers.Adam(learning_rate=0.0001, decay=0.001)
model.compile(
optimizer = opt,
loss=[classloss, regressloss],
# metrics=["accuracy", "meaniou"],
)
hist = model.fit(train, epochs=10, validation_data=valid)
It works fine, but if I uncomment metrics line, I get this error:它工作正常,但如果我取消注释 metrics 行,我会收到此错误:
ValueError: as_list() is not defined on an unknown TensorShape.
If I use objects instead of strings ( metrics=[Accuracy(), MeanIoU(2)]
), it gives this error:如果我使用对象而不是字符串( metrics=[Accuracy(), MeanIoU(2)]
),它会给出此错误:
TypeError: '>' not supported between instances of 'NoneType' and 'int'
What am I doing wrong and how can I fix this?我做错了什么,我该如何解决?
UPD: If I use accuracy for both outputs ( metrics=[[Accuracy()], [Accuracy()]]
), I train without any error, so I conclude there is something wrong with MeanIoU
in my code. UPD:如果我对两个输出( metrics=[[Accuracy()], [Accuracy()]]
)都使用准确度,我的训练没有任何错误,所以我得出结论,我的代码中的MeanIoU
有问题。
Btw, there is prediction for batch(8) samples (two outputs: class + coordinates as 4 numbers):顺便说一句,有 batch(8) 个样本的预测(两个输出:class + 坐标为 4 个数字):
(array([[0.7866989 ],
[0.973974 ],
[0.9148978 ],
[0.28471756],
[0.9899457 ],
[0.99033797],
[0.7237025 ],
[0.81942046]], dtype=float32),
array([[0.2515184 , 0.25495493, 0.3642715 , 0.09299589],
[0.87964845, 0.3134839 , 0.54833114, 0.36701256],
[0.0304133 , 0.45813853, 0.19692126, 0.244534 ],
[0.22500503, 0.70299083, 0.00123629, 0.41123846],
[0.37099576, 0.6092719 , 0.13407992, 0.40188596],
[0.32103425, 0.6240243 , 0.02281341, 0.03058532],
[0.28678325, 0.19885723, 0.50342166, 0.57963324],
[0.41590106, 0.21439987, 0.94105315, 0.3379435 ]], dtype=float32))
I thought may be format for MeanIoU is wrong, but arrays of 4 numbers seems valid for MeanIoU, doesn't it?我认为 MeanIoU 的格式可能是错误的,但是 4 个数字中的 arrays 似乎对 MeanIoU 有效,不是吗?
As I answered here , correct metrics are: BinaryAccuracy
and custom MeanIoU ( default MeanIoU is not applicable to bboxes regression as I understood).正如我在这里回答的那样,正确的指标是: BinaryAccuracy
和自定义 MeanIoU (据我所知, 默认 MeanIoU不适用于 bboxes 回归)。 Working code snippet is in the first link.工作代码片段在第一个链接中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.