Я пытаюсь реализовать пользовательскую функцию потерь для модели сегментации изображения.У меня проблемы с выяснением, как получить размер пересечения между двумя наборами.
Код, который у меня пока есть:
def SSCLoss(y_true, y_pred):
y_true = K.flatten(y_true)
y_pred = K.flatten(y_pred)
yTestUn, idx = tf.unique(y_true)
yPredUn, idx = tf.unique(y_pred)
maxLen = tf.math.maximum(tf.size(yTestUn), tf.size(yPredUn))
numEqual = tf.size(tf.sets.set_intersection(tf.dtypes.cast(yTestUn,tf.uint16), tf.dtypes.cast(yPredUn,tf.uint16)))
return tf.math.subtract(tf.constant(1), tf.math.divide(numEqual, maxLen))
Я получаю ошибку:
ValueError: Shape must be at least rank 2 but is rank 1 for 'loss/activation_1_loss/DenseToDenseSetOperation' (op: 'DenseToDenseSetOperation') with input shapes: [?], [?].
Реализация этой функции не в TensorFlow:
def SSC(y_true, y_pred):
if (y_true.ndim > 1):
y_true = np.array(y_true).ravel()
if (y_pred.ndim > 1):
y_pred = np.array(y_pred).ravel()
yTestUn = pd.Series(y_true).unique()
yPredUn = pd.Series(y_pred).unique()
maxLen = float(max(len(yTestUn),len(yPredUn)))
numEqual = float(len(set(list(yTestUn)) - (set(list(yTestUn)) - set(list(yPredUn)))))
return 1-float(numEqual/maxLen)
Формула:
![formula](https://i.stack.imgur.com/6nhfB.gif)