0

I wanted to use the hausdorff I found here (https://github.com/N0vel/weighted-hausdorff-distance-tensorflow-keras-loss) as loss in my U-Net, but when I try to do so I get the following error:

ValueError: Dimensions must be equal, but are 3 and 2 for 'loss/conv2d_19_loss/MatMul' (op: 'MatMul') with input shapes: [?,3], [2,16384].

I don't really understand the code of the hausdorff distance, but I put the same batch size that my network uses in the loop. I also tried to print the shape of y_true and y_pred using another loss function to see the size that was wanted, but it only printed Tensor("loss/conv2d_19_loss/strided_slice:0", shape=(?, ?, ?), dtype=float32). I tried to find the problem by following the path of the error, but I didn't uderstood the code.

    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].

I found nothing on hausdorff loss implementation, so I hope someone will find the problem. Thanks a lot!

Hagear
  • 3
  • 2
  • `ValueError: Dimensions must be equal, but are 3 and 2 for 'loss/conv2d_19_loss/MatMul' (op: 'MatMul') with input shapes: [?,3], [2,16384].` The error is that it is performing Matrix Multiplication 'MatMul' but received unequal dimension '3' no.of columns from 1st matrix and '2' no.of rows from second matrix. please check your shapes. – Harish Vutukuri Nov 20 '19 at 15:00
  • Yeah I understand the error, but like I said when I tried to print the shape of y_pred and y_true (in the keras files), it gave me shapes with ?. My network works with Dice or Crossentropy, so I think it is linked to the hausdorff code. – Hagear Nov 21 '19 at 14:21
  • Late response, but if anyone else has issues with the posted implementation, feel free to try my implementation of the same loss : https://github.com/danielenricocahall/Keras-Weighted-Hausdorff-Distance-Loss/tree/master/CustomLosses – danielcahall Apr 10 '20 at 12:38

0 Answers0