【问题标题】:NotImplementedError: Cannot convert a symbolic Tensor (up_sampling2d_4_target:0) to a numpy arrayNotImplementedError:无法将符号张量 (up_sampling2d_4_target:0) 转换为 numpy 数组
【发布时间】:2020-06-11 07:24:29
【问题描述】:

NotImplementedError:无法转换符号张量 (up_sampling2d_4_target:0) 到一个 numpy 数组。

出现以下错误

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(最近调用 最后)在 14 15 而真: ---> 16 loss_history = fit() 17 如果 np.min([mh.history['val_loss'] for mh in loss_history])

in fit() 1个定义适合(): ----> 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 在 _method_wrapper(self, *args, **kwargs) 455 self._self_setattr_tracking = False #pylint:禁用=保护访问 456 尝试: --> 457 结果 = 方法(自我,*args,**kwargs) 最后458: 459 self._self_setattr_tracking = previous_value # pylint: disable=protected-access

~/venv/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training.py 在编译(自我,优化器,损失,指标,损失权重, sample_weight_mode,weighted_metrics,target_tensors,distribute, **kwargs) 371 372 # 创建模型损失和加权指标子图。 --> 373 self._compile_weights_loss_and_weighted_metrics() 374 375 # 训练、测试和预测函数

~/venv/lib/python3.7/site-packages/tensorflow_core/python/training/tracking/base.py 在 _method_wrapper(self, *args, **kwargs) 455 self._self_setattr_tracking = False #pylint:禁用=保护访问 456 尝试: --> 457 结果 = 方法(自我,*args,**kwargs) 最后458: 459 self._self_setattr_tracking = previous_value # pylint: disable=protected-access

~/venv/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training.py 在 _compile_weights_loss_and_weighted_metrics(self, sample_weights)
第1651章 1652#层损失。 -> 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 在 _prepare_total_loss(self, mask) 1711 1712 如果 hasattr(loss_fn, 'reduction'): -> 1713 per_sample_losses = loss_fn.call(y_true,y_pred)1714 weighted_losses = loss_utils.compute_weighted_loss( 第1715章

~/venv/lib/python3.7/site-packages/tensorflow_core/python/keras/losses.py 在通话中(自我,y_true,y_pred) 219 y_pred,y_true = tf_losses_util.squeeze_or_expand_dimensions( 220 y_pred, y_true) --> 221 返回 self.fn(y_true, y_pred, **self._fn_kwargs) 222 223 def get_config(自我):

in IoU(y_true, y_pred, eps) 5 ## 联合上的交集 6 def IoU(y_true, y_pred, eps=1e-6): ----> 7 如果 np.max(y_true) == 0.0: 8 return IoU(1-y_true, 1-y_pred) ##空图像;计算零的 IoU 9 交点 = 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 amax(a, axis, out, keepdims, initial, where) 2619 """ 2620
return _wrapreduction(a, np.maximum, 'max', 轴, 无, out, -> 2621 keepdims=keepdims, initial=initial, where=where) 2622 2623

~/venv/lib/python3.7/site-packages/numpy/core/fromnumeric.py _wrapreduction(obj,ufunc,方法,轴,dtype,out,**kwargs) 88 返回缩减(axis=axis,out=out,**passkwargs) 89 ---> 90 返回 ufunc.reduce(obj, axis, dtype, out, **passkwargs) 91 92

~/venv/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py 在数组(自我) 第734章 735 raise NotImplementedError("无法将符号张量 ({}) 转换为 numpy" --> 736 "数组。".format(self.name)) 737 738 def len(自我):

NotImplementedError:无法转换符号张量 (up_sampling2d_4_target:0) 到一个 numpy

数组。

【问题讨论】:

    标签: tensorflow machine-learning keras tensorflow2.0 tensor


    【解决方案1】:

    您不能将 numpy 与张量流 Tensor 一起使用,它们是两个不同的东西。

    问题出在这里:

    if np.max(y_true) == 0.0:
        return IoU(1-y_true, 1-y_pred) ## empty image; calc IoU of zeros
    

    你需要这些行来代替:

    is_zero = K.equal(y_true, 0)
    y_true = K.switch(is_zero, 1-y_true, y_true)
    y_pred = K.switch(is_zero, 1-y_pred, y_pred)
    

    【讨论】:

    • 你能像上面那样做 if 语句吗?不知道如何转换它
    • 答案就在其中,就是替代品。
    猜你喜欢
    • 2020-02-17
    • 2022-01-12
    • 1970-01-01
    • 1970-01-01
    • 2022-10-05
    • 1970-01-01
    • 2020-10-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多