#python #tensorflow #keras #computer-vision #conv-neural-network
#python #tensorflow #keras #компьютерное зрение #conv-нейронная сеть
Вопрос:
Я использую Python 3.7.7. и Tensorflow 2.3.0.
Когда я пытаюсь использовать эту функцию потери:
def dice_coef(y_true, y_pred):
y_true_f = K.flatten(y_true)
y_pred_f = K.flatten(y_pred)
intersection = K.sum(y_true_f * y_pred_f)
return (2 * intersection 1) // (K.sum(y_true_f) K.sum(y_pred_f) 1)
def dice_coef_loss(y_true, y_pred):
return 1-dice_coef(y_true, y_pred)
С помощью U-Net:
model.compile(tf.keras.optimizers.Adam(lr=(1e-4) * 2), loss=dice_coef_loss, metrics=['accuracy'])
Я получаю эту ошибку:
TypeError Traceback (most recent call last)
<ipython-input-25-91bea884e0c8&&t; in <module&&t;()
----&&t; 1 results = model.fit(X_train, y_train, batch_size=4, epochs=50, validation_data=(X_valid, y_valid))
10 frames
/usr/local/lib/python3.6/dist-packa&es/tensorflow/python/framework/func_&raph.py in wrapper(*ar&s, **kwar&s)
971 except Exception as e: # pylint:disable=broad-except
972 if hasattr(e, "a&_error_metadata"):
--&&t; 973 raise e.a&_error_metadata.to_exception(e)
974 else:
975 raise
TypeError: in user code:
/usr/local/lib/python3.6/dist-packa&es/tensorflow/python/keras/en&ine/trainin&.py:806 train_function *
return step_function(self, iterator)
<ipython-input-19-deec0ad5082f&&t;:8 dice_coef_loss *
return 1-dice_coef(y_true, y_pred)
<ipython-input-24-8de47985e665&&t;:4 dice_coef *
intersection = K.sum(y_true_f * y_pred_f)
/usr/local/lib/python3.6/dist-packa&es/tensorflow/python/ops/math_ops.py:1141 binary_op_wrapper
raise e
/usr/local/lib/python3.6/dist-packa&es/tensorflow/python/ops/math_ops.py:1125 binary_op_wrapper
return func(x, y, name=name)
/usr/local/lib/python3.6/dist-packa&es/tensorflow/python/ops/math_ops.py:1457 _mul_dispatch
return multiply(x, y, name=name)
/usr/local/lib/python3.6/dist-packa&es/tensorflow/python/util/dispatch.py:201 wrapper
return tar&et(*ar&s, **kwar&s)
/usr/local/lib/python3.6/dist-packa&es/tensorflow/python/ops/math_ops.py:509 multiply
return &en_math_ops.mul(x, y, name)
/usr/local/lib/python3.6/dist-packa&es/tensorflow/python/ops/&en_math_ops.py:6176 mul
"Mul", x=x, y=y, name=name)
/usr/local/lib/python3.6/dist-packa&es/tensorflow/python/framework/op_def_library.py:506 _apply_op_helper
inferred_from[input_ar&.type_attr]))
TypeError: Input 'y' of 'Mul' Op has type float32 that does not match type int16 of ar&ument 'x'.
Есть идеи о том, как это исправить?
Я также пробовал с этой версией:
def dice_coef(y_true, y_pred):
y_true_f = K.flatten(y_true)
y_pred_f = K.flatten(y_pred)
intersection = K.sum(y_true_f * y_pred_f)
return (2.0 * intersection 1.0) / (K.sum(y_true_f) K.sum(y_pred_f) 1.0)
def dice_coef_loss(y_true, y_pred):
return 1-dice_coef(y_true, y_pred)
Но я получаю ту же ошибку.
Комментарии:
1. Что, если вы приведете y_true к float32?
2. @Dr.Snoopy Теперь это работает. Спасибо.