基于MobileNet v2的猫狗分类+检测模型。它训练得很好,但现在我想为它添加指标,但我做不到。下面是代码的主要部分:
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)
它工作得很好,但如果我取消注释metrics行,我得到这个错误:
ValueError: as_list() is not defined on an unknown TensorShape.
如果我使用对象而不是字符串(metrics=[Accuracy(), MeanIoU(2)]
),它会给出这个错误:
TypeError: '>' not supported between instances of 'NoneType' and 'int'
我做错了什么,我怎么能解决这个问题?
UPD:如果我对两个输出(metrics=[[Accuracy()], [Accuracy()]]
)使用准确性,我训练没有任何错误,所以我得出结论,我的代码中MeanIoU
有问题。
顺便说一句,有对批(8)个样本的预测(两个输出:类+坐标为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))
我认为可能是格式的MeanIoU是错误的,但数组的4个数字似乎有效的MeanIoU,不是吗?
正如我在这里回答的,正确的指标是:BinaryAccuracy
和custom MeanIoU(据我所知,默认的MeanIoU不适用于盒回归)。工作代码片段在第一个链接。