Issue with the model

I have passed all internal grading for soft dice loss and dice coefficient. But my model does not work (model statement provided in util.py). The error points to some issue in loss metric. But since I passed all grading, I do not know how to debug. There is some way model handles custom loss function which does not match the model computation; but what is it? This is the error from model = util.unet_model_3d(loss_function=soft_dice_loss, metrics=[dice_coefficient]) cell:


InvalidArgumentError Traceback (most recent call last)
/opt/conda/lib/python3.6/site-packages/tensorflow_core/python/client/session.py in _do_call(self, fn, *args)
1364 try:
→ 1365 return fn(*args)
1366 except errors.OpError as e:

/opt/conda/lib/python3.6/site-packages/tensorflow_core/python/client/session.py in _run_fn(feed_dict, fetch_list, target_list, options, run_metadata)
1349 return self._call_tf_sessionrun(options, feed_dict, fetch_list,
→ 1350 target_list, run_metadata)
1351

/opt/conda/lib/python3.6/site-packages/tensorflow_core/python/client/session.py in _call_tf_sessionrun(self, options, feed_dict, fetch_list, target_list, run_metadata)
1442 fetch_list, target_list,
→ 1443 run_metadata)
1444

InvalidArgumentError: You must feed a value for placeholder tensor ‘activation_15_target’ with dtype float and shape [?,?,?,?,?]
[[{{node activation_15_target}}]]

During handling of the above exception, another exception occurred:

InvalidArgumentError Traceback (most recent call last)
in ()
----> 1 model = util.unet_model_3d(loss_function=soft_dice_loss, metrics=[dice_coefficient])

~/work/W3A1/util.py in unet_model_3d(loss_function, input_shape, pool_size, n_labels, initial_learning_rate, deconvolution, depth, n_base_filters, include_label_wise_dice_coefficients, metrics, batch_normalization, activation_name)
190
191 model.compile(optimizer=Adam(lr=initial_learning_rate), loss=loss_function,
→ 192 metrics=metrics)
193 return model
194

/opt/conda/lib/python3.6/site-packages/keras/engine/training.py in compile(self, optimizer, loss, metrics, loss_weights, sample_weight_mode, weighted_metrics, target_tensors, **kwargs)
220 skip_target_masks=[l is None for l in self.loss_functions],
221 sample_weights=self.sample_weights,
→ 222 masks=masks)
223
224 # Compute total loss.

/opt/conda/lib/python3.6/site-packages/keras/engine/training.py in _handle_metrics(self, outputs, targets, skip_target_masks, sample_weights, masks)
869
870 self._handle_per_output_metrics(
→ 871 self._per_output_metrics[i], target, output, output_mask)
872 self._handle_per_output_metrics(
873 self._per_output_weighted_metrics[i],

/opt/conda/lib/python3.6/site-packages/keras/engine/training.py in _handle_per_output_metrics(self, metrics_dict, y_true, y_pred, mask, weights)
840 with K.name_scope(metric_name):
841 training_utils.call_metric_function(
→ 842 metric_fn, y_true, y_pred, weights=weights, mask=mask)
843
844 def _handle_metrics(self,

/opt/conda/lib/python3.6/site-packages/keras/engine/training_utils.py in call_metric_function(metric_fn, y_true, y_pred, weights, mask)
1031
1032 if y_pred is not None:
→ 1033 update_ops = metric_fn.update_state(y_true, y_pred, sample_weight=weights)
1034 with K.control_dependencies(update_ops): # For TF
1035 metric_fn.result()

/opt/conda/lib/python3.6/site-packages/keras/utils/metrics_utils.py in decorated(metric_obj, *args, **kwargs)
40 “”“Decorated function with add_update().”“”
41
—> 42 update_op = update_state_fn(*args, **kwargs)
43 metric_obj.add_update(update_op)
44 return update_op

/opt/conda/lib/python3.6/site-packages/keras/metrics.py in update_state(self, y_true, y_pred, sample_weight)
316 y_pred, y_true = losses_utils.squeeze_or_expand_dimensions(y_pred, y_true)
317
→ 318 matches = self._fn(y_true, y_pred, **self._fn_kwargs)
319 return super(MeanMetricWrapper, self).update_state(
320 matches, sample_weight=sample_weight)

in dice_coefficient(y_true, y_pred, axis, epsilon)
23 # Convert to NumPy arrays if they are TensorFlow tensors
24 if K.is_tensor(y_true):
—> 25 y_true = K.eval(y_true)
26 if K.is_tensor(y_pred):
27 y_pred = K.eval(y_pred)

/opt/conda/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py in eval(x)
926 “”"
927 if _is_tf_1():
→ 928 return to_dense(x).eval(session=get_session())
929 if hasattr(x, ‘numpy’):
930 with context.eager_mode():

/opt/conda/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py in eval(self, feed_dict, session)
796
797 “”"
→ 798 return _eval_using_default_session(self, feed_dict, self.graph, session)
799
800 def experimental_ref(self):

/opt/conda/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py in _eval_using_default_session(tensors, feed_dict, graph, session)
5405 "the tensor’s graph is different from the session’s "
5406 “graph.”)
→ 5407 return session.run(tensors, feed_dict)
5408
5409

/opt/conda/lib/python3.6/site-packages/tensorflow_core/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata)
954 try:
955 result = self._run(None, fetches, feed_dict, options_ptr,
→ 956 run_metadata_ptr)
957 if run_metadata:
958 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

/opt/conda/lib/python3.6/site-packages/tensorflow_core/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
1178 if final_fetches or final_targets or (handle and feed_dict_tensor):
1179 results = self._do_run(handle, final_targets, final_fetches,
→ 1180 feed_dict_tensor, options, run_metadata)
1181 else:
1182 results =

/opt/conda/lib/python3.6/site-packages/tensorflow_core/python/client/session.py in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
1357 if handle is None:
1358 return self._do_call(_run_fn, feeds, fetches, targets, options,
→ 1359 run_metadata)
1360 else:
1361 return self._do_call(_prun_fn, handle, feeds, fetches)

/opt/conda/lib/python3.6/site-packages/tensorflow_core/python/client/session.py in _do_call(self, fn, *args)
1382 ‘\nsession_config.graph_options.rewrite_options.’
1383 ‘disable_meta_optimizer = True’)
→ 1384 raise type(e)(node_def, op, message)
1385
1386 def _extend_graph(self):

InvalidArgumentError: You must feed a value for placeholder tensor ‘activation_15_target’ with dtype float and shape [?,?,?,?,?]
[[node activation_15_target (defined at /opt/conda/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py:1748) ]]

Original stack trace for ‘activation_15_target’:
File “/opt/conda/lib/python3.6/runpy.py”, line 193, in _run_module_as_main
main”, mod_spec)
File “/opt/conda/lib/python3.6/runpy.py”, line 85, in _run_code
exec(code, run_globals)
File “/opt/conda/lib/python3.6/site-packages/ipykernel_launcher.py”, line 16, in
app.launch_new_instance()
File “/opt/conda/lib/python3.6/site-packages/traitlets/config/application.py”, line 658, in launch_instance
app.start()
File “/opt/conda/lib/python3.6/site-packages/ipykernel/kernelapp.py”, line 486, in start
self.io_loop.start()
File “/opt/conda/lib/python3.6/site-packages/tornado/platform/asyncio.py”, line 132, in start
self.asyncio_loop.run_forever()
File “/opt/conda/lib/python3.6/asyncio/base_events.py”, line 421, in run_forever
self._run_once()
File “/opt/conda/lib/python3.6/asyncio/base_events.py”, line 1426, in _run_once
handle._run()
File “/opt/conda/lib/python3.6/asyncio/events.py”, line 127, in _run
self._callback(*self._args)
File “/opt/conda/lib/python3.6/site-packages/tornado/platform/asyncio.py”, line 122, in _handle_events
handler_func(fileobj, events)
File “/opt/conda/lib/python3.6/site-packages/tornado/stack_context.py”, line 300, in null_wrapper
return fn(*args, **kwargs)
File “/opt/conda/lib/python3.6/site-packages/zmq/eventloop/zmqstream.py”, line 450, in _handle_events
self._handle_recv()
File “/opt/conda/lib/python3.6/site-packages/zmq/eventloop/zmqstream.py”, line 480, in _handle_recv
self._run_callback(callback, msg)
File “/opt/conda/lib/python3.6/site-packages/zmq/eventloop/zmqstream.py”, line 432, in _run_callback
callback(*args, **kwargs)
File “/opt/conda/lib/python3.6/site-packages/tornado/stack_context.py”, line 300, in null_wrapper
return fn(*args, **kwargs)
File “/opt/conda/lib/python3.6/site-packages/ipykernel/kernelbase.py”, line 283, in dispatcher
return self.dispatch_shell(stream, msg)
File “/opt/conda/lib/python3.6/site-packages/ipykernel/kernelbase.py”, line 233, in dispatch_shell
handler(stream, idents, msg)
File “/opt/conda/lib/python3.6/site-packages/ipykernel/kernelbase.py”, line 399, in execute_request
user_expressions, allow_stdin)
File “/opt/conda/lib/python3.6/site-packages/ipykernel/ipkernel.py”, line 208, in do_execute
res = shell.run_cell(code, store_history=store_history, silent=silent)
File “/opt/conda/lib/python3.6/site-packages/ipykernel/zmqshell.py”, line 537, in run_cell
return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
File “/opt/conda/lib/python3.6/site-packages/IPython/core/interactiveshell.py”, line 2662, in run_cell
raw_cell, store_history, silent, shell_futures)
File “/opt/conda/lib/python3.6/site-packages/IPython/core/interactiveshell.py”, line 2785, in _run_cell
interactivity=interactivity, compiler=compiler, result=result)
File “/opt/conda/lib/python3.6/site-packages/IPython/core/interactiveshell.py”, line 2901, in run_ast_nodes
if self.run_code(code, result):
File “/opt/conda/lib/python3.6/site-packages/IPython/core/interactiveshell.py”, line 2961, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File “”, line 1, in
model = util.unet_model_3d(loss_function=soft_dice_loss, metrics=[dice_coefficient])
File “/home/jovyan/work/W3A1/util.py”, line 192, in unet_model_3d
metrics=metrics)
File “/opt/conda/lib/python3.6/site-packages/keras/engine/training.py”, line 196, in compile
dtype=K.dtype(self.outputs[i]))
File “/opt/conda/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py”, line 736, in placeholder
shape=shape, ndim=ndim, dtype=dtype, sparse=sparse, name=name)
File “/opt/conda/lib/python3.6/site-packages/tensorflow_core/python/keras/backend.py”, line 1051, in placeholder
x = array_ops.placeholder(dtype, shape=shape, name=name)
File “/opt/conda/lib/python3.6/site-packages/tensorflow_core/python/ops/array_ops.py”, line 2619, in placeholder
return gen_array_ops.placeholder(dtype=dtype, shape=shape, name=name)
File “/opt/conda/lib/python3.6/site-packages/tensorflow_core/python/ops/gen_array_ops.py”, line 6669, in placeholder
“Placeholder”, dtype=dtype, shape=shape, name=name)
File “/opt/conda/lib/python3.6/site-packages/tensorflow_core/python/framework/op_def_library.py”, line 794, in _apply_op_helper
op_def=op_def)
File “/opt/conda/lib/python3.6/site-packages/tensorflow_core/python/util/deprecation.py”, line 507, in new_func
return func(*args, **kwargs)
File “/opt/conda/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py”, line 3357, in create_op
attrs, op_def, compute_device)
File “/opt/conda/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py”, line 3426, in _create_op_internal
op_def=op_def)
File “/opt/conda/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py”, line 1748, in init
self._traceback = tf_stack.extract_stack()

Hello @Dennis_Sinitsky

I hope you are doing the assignment in Coursera Jupyter environment

Also the error shows some issue with metadata, so I hope there was no deletion of any files, cells or editing or addition of any cell.

Next the issue seems to be more related to soft_dice_loss and dice_cofficient, so make sure you have not hard coded path for these code cells and used K.mean.

Regards
DP

I rewrote the code to include K.mean (before I did loop / num_classes). It passes all local tests. But model = util.unet_model_3d keeps yielding the same error.

Can you DM me your codes once.

Hello @Dennis_Sinitsky

  1. In UNQ_C2 (UNIQUE CELL IDENTIFIER, DO NOT EDIT)
    def standardize(image):
    under the if statement, you have added an else statement which is not required, kindly remove it
    divide by the standard deviation (only if it is different from zero)
    else:
    centered_scaled = centered

  2. UNQ_C3 (UNIQUE CELL IDENTIFIER, DO NOT EDIT)
    def single_class_dice_coefficient

FOR DICE NUMERATOR As I said in the previous comments, you have used incorrect operator to calculate dice_numerator and dice_denominator
while calculating instead of using * operator between y_pred and y_true values you have used tf.multiply.
Also while calculating numerator, remember it is
({2 \times \sum_{i, j} f(x){ij} \times y{ij} )+ \epsilon}
notice there need to be one more tuple for A * B and epsilon without tuple

FOR DICE DENOMINATOR: you have applied incorrect code which would be used in soft dice loss for dice_denominator. Also same mistake as far as for the operator * needs to be used, avoid using tf.multiply.

  1. UNQ_C4 (UNIQUE CELL IDENTIFIER, DO NOT EDIT)
    def dice_coefficient

Removed the below code
Convert to NumPy arrays if they are TensorFlow tensors
if K.is_tensor(y_true):
y_true = K.eval(y_true)
if K.is_tensor(y_pred):
y_pred = K.eval(y_pred)
num_classes = y_true.shape[0]
print(“Number of classes:”, num_classes)

for dice cofficient, there is an instructions mentioned as
Implement the mean dice coefficient below. This should not be very different from your singe-class implementation.
So your codes for dice numerator and dice denominator will be same as in single class cofficient.
The only difference in this code cell would be for the dice cofficient where you will calculate mean(dice_numerator/dice_denomination) (but remember Please use the K.mean function to take the average of the three classes.)
Also don’t forget that extra tuple required before the epsilon for dice_numerator

  1. UNQ_C5 (UNIQUE CELL IDENTIFIER, DO NOT EDIT)
    def soft_dice_loss
    your codes are written correctly but dice_numerator requires an extra tuple for 2 * K.mean of y_pred and y_true with axis
    also remember dice_loss is 1 - K.mean of dice_numerator/dice_denominator and not 1.0-K.mean. change 1.0 to 1

  2. No codes written for
    UNQ_C6 (UNIQUE CELL IDENTIFIER, DO NOT EDIT)
    def compute_class_sens_spec

here you calculate tp, tn, fp, and fn using numpy sum using the respective class pred and class labels for their respective classes.
Then using this tp, tn, fp and fn you calculate sensitivity and specificity(EXTRA HINT: MAKE SURE YOU TUPLE THE DENOMINATORS FOR BOTH SENSITIVITY AND SPECIFICITY)

Let me know if the issue is resolved, feel free to ask any doubt.

Regards
DP

now OK, thanks!