Multiple outputs in Keras gives value error

481 views Asked by At

I am implementing a modification of the U-net for semantic segmentation.

I have two outputs from the network :

    model = Model(input=inputs, output= [conv10, dense3])
    model.compile(optimizer=Adam(lr=1e-5), loss=common_loss, metrics=[common_loss])

where common loss is defined as :

def common_loss(y_true, y_pred):
    segmentation_loss  = categorical_crossentropy(y_true[0], y_pred[0])
    classifiction_loss = categorical_crossentropy(y_true[1], y_pred[1])
    return segmentation_loss + alpha * classifiction_loss

When I run this I get an value error as:

File "y-net.py", line 138, in <module>
    train_and_predict()
File "y-net.py", line 133, in train_and_predict
    callbacks=[model_checkpoint], validation_data=(X_val, [y_img_val, y_class_val]))
  File "/home/gpu_users/meetshah/miniconda2/envs/check/lib/python2.7/site-packages/keras/engine/training.py", line 1124, in fit
    callback_metrics=callback_metrics)
  File "/home/gpu_users/meetshah/miniconda2/envs/check/lib/python2.7/site-packages/keras/engine/training.py", line 848, in _fit_loop
    callbacks.on_batch_end(batch_index, batch_logs)
  File "/home/gpu_users/meetshah/miniconda2/envs/check/lib/python2.7/site-packages/keras/callbacks.py", line 63, in on_batch_end
    callback.on_batch_end(batch, logs)
  File "/home/gpu_users/meetshah/miniconda2/envs/check/lib/python2.7/site-packages/keras/callbacks.py", line 191, in on_batch_end
    self.progbar.update(self.seen, self.log_values)
  File "/home/gpu_users/meetshah/miniconda2/envs/check/lib/python2.7/site-packages/keras/utils/generic_utils.py", line 147, in update
    if abs(avg) > 1e-3:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

My implementation and the entire trace can be found here :

https://gist.github.com/meetshah1995/19d54270e8d1b20f814e6c1495facc6a

1

There are 1 answers

1
Thomas Pinetz On BEST ANSWER

You can see how to implement multiple metrics with multiple outputs here: https://github.com/EdwardTyantov/ultrasound-nerve-segmentation/blob/master/u_model.py.

    model.compile(optimizer=optimizer,
              loss={'main_output': dice_coef_loss, 'aux_output': 'binary_crossentropy'},
              metrics={'main_output': dice_coef, 'aux_output': 'acc'},
              loss_weights={'main_output': 1., 'aux_output': 0.5})

I am not sure, if combined output metrics are supported yet.