NotImplementedError: Cannot convert a symbolic Tensor (up_sampling2d_4_target:0) to a numpy array.
got following error
import keras.backend as K
from keras.optimizers import Adam
from keras.losses import binary_crossentropy
## intersection over union
def IoU(y_true, y_pred, eps=1e-6):
if np.max(y_true) == 0.0:
return IoU(1-y_true, 1-y_pred) ## empty image; calc IoU of zeros
intersection = K.sum(y_true * y_pred, axis=[1,2,3])
union = K.sum(y_true, axis=[1,2,3]) + K.sum(y_pred, axis=[1,2,3]) - intersection
return -K.mean( (intersection + eps) / (union + eps), axis=0)
--------------------------------------------------------------------------- NotImplementedError Traceback (most recent call last) in 14 15 while True: ---> 16 loss_history = fit() 17 if np.min([mh.history['val_loss'] for mh in loss_history]) < -0.2: 18 break
in fit() 1 def fit(): ----> 2 seg_model.compile(optimizer=Adam(1e-3, decay=1e-6), loss=IoU, metrics=['binary_accuracy']) 3 4 step_count = min(MAX_TRAIN_STEPS, train_df.shape[0]//BATCH_SIZE) 5 aug_gen = create_aug_gen(make_image_gen(train_df))
~/venv/lib/python3.7/site-packages/tensorflow_core/python/training/tracking/base.py in _method_wrapper(self, *args, **kwargs) 455 self._self_setattr_tracking = False # pylint: disable=protected-access 456 try: --> 457 result = method(self, *args, **kwargs) 458 finally: 459 self._self_setattr_tracking = previous_value # pylint: disable=protected-access
~/venv/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training.py in compile(self, optimizer, loss, metrics, loss_weights, sample_weight_mode, weighted_metrics, target_tensors, distribute, **kwargs) 371 372 # Creates the model loss and weighted metrics sub-graphs. --> 373 self._compile_weights_loss_and_weighted_metrics() 374 375 # Functions for train, test and predict will
~/venv/lib/python3.7/site-packages/tensorflow_core/python/training/tracking/base.py in _method_wrapper(self, *args, **kwargs) 455 self._self_setattr_tracking = False # pylint: disable=protected-access 456 try: --> 457 result = method(self, *args, **kwargs) 458 finally: 459 self._self_setattr_tracking = previous_value # pylint: disable=protected-access
~/venv/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training.py in _compile_weights_loss_and_weighted_metrics(self, sample_weights)
1651 # loss_weight_2 * output_2_loss_fn(...) + 1652 # layer losses. -> 1653 self.total_loss = self._prepare_total_loss(masks) 1654 1655 def _prepare_skip_target_masks(self):~/venv/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training.py in _prepare_total_loss(self, masks) 1711 1712 if hasattr(loss_fn, 'reduction'): -> 1713 per_sample_losses = loss_fn.call(y_true, y_pred) 1714 weighted_losses = losses_utils.compute_weighted_loss( 1715 per_sample_losses,
~/venv/lib/python3.7/site-packages/tensorflow_core/python/keras/losses.py in call(self, y_true, y_pred) 219 y_pred, y_true = tf_losses_util.squeeze_or_expand_dimensions( 220 y_pred, y_true) --> 221 return self.fn(y_true, y_pred, **self._fn_kwargs) 222 223 def get_config(self):
in IoU(y_true, y_pred, eps) 5 ## intersection over union 6 def IoU(y_true, y_pred, eps=1e-6): ----> 7 if np.max(y_true) == 0.0: 8 return IoU(1-y_true, 1-y_pred) ## empty image; calc IoU of zeros 9 intersection = K.sum(y_true * y_pred, axis=[1,2,3])
<array_function internals> in amax(*args, **kwargs)
~/venv/lib/python3.7/site-packages/numpy/core/fromnumeric.py in amax(a, axis, out, keepdims, initial, where) 2619 """ 2620
return _wrapreduction(a, np.maximum, 'max', axis, None, out, -> 2621 keepdims=keepdims, initial=initial, where=where) 2622 2623~/venv/lib/python3.7/site-packages/numpy/core/fromnumeric.py in _wrapreduction(obj, ufunc, method, axis, dtype, out, **kwargs) 88 return reduction(axis=axis, out=out, **passkwargs) 89 ---> 90 return ufunc.reduce(obj, axis, dtype, out, **passkwargs) 91 92
~/venv/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py in array(self) 734 def array(self): 735 raise NotImplementedError("Cannot convert a symbolic Tensor ({}) to a numpy" --> 736 " array.".format(self.name)) 737 738 def len(self):
NotImplementedError: Cannot convert a symbolic Tensor (up_sampling2d_4_target:0) to a numpy
array.