我想将在这里(https://github.com/N0vel/weighted-hausdorff-distance-tensorflow-keras-loss)找到的hausdorff用作我的U-Net的损失,但是当我尝试这样做时,出现以下错误:
ValueError: Dimensions must be equal, but are 3 and 2 for 'loss/conv2d_19_loss/MatMul' (op: 'MatMul') with input shapes: [?,3], [2,16384].
我不太了解hausdorff距离的代码,但是我在循环中放入了与我的网络相同的批处理大小。我还尝试使用另一个损失函数来打印y_true和y_pred的形状,以查看所需的大小,但只打印了Tensor("loss/conv2d_19_loss/strided_slice:0", shape=(?, ?, ?), dtype=float32)
。
我试图通过遵循错误的路径来查找问题,但我没有理解代码。
Traceback (most recent call last):
File "/home/etudiant_master/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/framework/common_shapes.py", line 686, in _call_cpp_shape_fn_impl
input_tensors_as_shapes, status)
File "/home/etudiant_master/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/framework/errors_impl.py", line 473, in __exit__
c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: Dimensions must be equal, but are 3 and 2 for 'loss/conv2d_19_loss/MatMul' (op: 'MatMul') with input shapes: [?,3], [2,16384].
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/etudiant_master/Documents/Marouane/Scripts/Test_parameters.py", line 367, in <module>
test_hyperparam_list('Loss', LOSS)
File "/home/etudiant_master/Documents/Marouane/Scripts/Test_parameters.py", line 339, in test_hyperparam_list
test_ligne = leave_one_out_model_test(batch_size, nb_epoch, validation_split, kernels, kernel_size, dropout_rate, pooling_size, block_number, path_test = path_test, iteration = var, metrics = metrics, optimizer = optimizer, loss = loss, activation = activation, activation2 = activation2)
File "/home/etudiant_master/Documents/Marouane/Scripts/Test_parameters.py", line 123, in leave_one_out_model_test
model = train_model.CNNs_layers(kernels = kernels, kernel_size = kernel_size, dropout_rate = dropout_rate, pooling_size = pooling_size, block_number = block_number, activation = activation, activation2 = activation2, metrics = metrics, optimizer = optimizer, loss = loss)
File "/home/etudiant_master/Documents/Marouane/Scripts/train_model.py", line 120, in CNNs_layers
model.compile(optimizer= optimizer, loss=loss, metrics=[metrics])
File "/home/etudiant_master/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/keras/_impl/keras/engine/training.py", line 849, in compile
output_loss = weighted_loss(y_true, y_pred, sample_weight, mask)
File "/home/etudiant_master/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/keras/_impl/keras/engine/training.py", line 454, in weighted
score_array = fn(y_true, y_pred)
File "/home/etudiant_master/Documents/Marouane/Scripts/Metrics.py", line 73, in Weighted_Hausdorff_loss
d_matrix = tf.sqrt(tf.maximum(tf.reshape(tf.reduce_sum(gt_b*gt_b, axis=1), (-1, 1)) + tf.reduce_sum(all_img_locations*all_img_locations, axis=1)-2*(tf.matmul(gt_b, tf.transpose(all_img_locations))), 0.0))
File "/home/etudiant_master/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/ops/math_ops.py", line 2022, in matmul
a, b, transpose_a=transpose_a, transpose_b=transpose_b, name=name)
File "/home/etudiant_master/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/ops/gen_math_ops.py", line 2516, in _mat_mul
name=name)
File "/home/etudiant_master/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper
op_def=op_def)
File "/home/etudiant_master/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3162, in create_op
compute_device=compute_device)
File "/home/etudiant_master/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3208, in _create_op_helper
set_shapes_for_outputs(op)
File "/home/etudiant_master/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 2427, in set_shapes_for_outputs
return _set_shapes_for_outputs(op)
File "/home/etudiant_master/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 2400, in _set_shapes_for_outputs
shapes = shape_func(op)
File "/home/etudiant_master/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 2330, in call_with_requiring
return call_cpp_shape_fn(op, require_shape_fn=True)
File "/home/etudiant_master/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/framework/common_shapes.py", line 627, in call_cpp_shape_fn
require_shape_fn)
File "/home/etudiant_master/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/framework/common_shapes.py", line 691, in _call_cpp_shape_fn_impl
raise ValueError(err.message)
ValueError: Dimensions must be equal, but are 3 and 2 for 'loss/conv2d_19_loss/MatMul' (op: 'MatMul') with input shapes: [?,3], [2,16384].
我没有发现关于hausdorff损失的实现,所以我希望有人能找到问题所在。 非常感谢!