NotImplementedError:无法将符号张量(up_sampling2d_4_target:0)转换为numpy数组 [英] NotImplementedError: Cannot convert a symbolic Tensor (up_sampling2d_4_target:0) to a numpy array
问题描述
NotImplementedError:无法转换符号张量 (up_sampling2d_4_target:0)转换为numpy数组.
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错误回溯(最近的调用 最后) 14 15,而True: ---> 16 loss_history = fit() 如果np.min([loss_history中的mh的[mh.history ['val_loss']为mh])< 17 -0.2: 18休息
--------------------------------------------------------------------------- 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,衰减= 1e-6),损失= IoU,指标= ['binary_accuracy']) 3 4步数=最小值(MAX_TRAIN_STEPS,train_df.shape [0]//BATCH_SIZE) 5 aug_gen = create_aug_gen(make_image_gen(train_df))
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 在_method_wrapper(self,* args,** kwargs)中 455 self._self_setattr_tracking = False#pylint:disable =受保护的访问 456尝试: -> 457 result = method(self,* args,** kwargs) 458最后: 459 self._self_setattr_tracking = previous_value#pylint:disable =受保护的访问
~/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 在编译中(自我,优化器,损失,指标,loss_weights, sample_weight_mode,weighted_metrics,target_tensors,分布, ** kwargs) 371 372#创建模型损失和加权指标子图. -> 373个self._compile_weights_loss_and_weighted_metrics() 374 375#用于训练,测试和预测的功能
~/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 在_method_wrapper(self,* args,** kwargs)中 455 self._self_setattr_tracking = False#pylint:disable =受保护的访问 456尝试: -> 457 result = method(self,* args,** kwargs) 458最后: 459 self._self_setattr_tracking = previous_value#pylint:disable =受保护的访问
~/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
在_compile_weights_loss_and_weighted_metrics(自身,sample_weights)中
1651#loss_weight_2 * output_2_loss_fn(...)+
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
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 在_prepare_total_loss(self,masks)中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 per_sample_losses,
~/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 在通话中(self,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():
~/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):
(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])
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)
<array_function internals> in amax(*args, **kwargs)
〜/venv/lib/python3.7/site-packages/numpy/core/fromnumeric.py在
amax(a,轴,输出,keepdims,初始位置在哪里)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
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在 _wrapreduction(obj,ufunc,method,axis,dtype,out,** kwargs) 88折返幅度减少(轴=轴,输出=出,** passkwargs) 89 ---> 90 return ufunc.reduce(obj,axis,dtype,out,** passkwargs) 91 92
~/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 在数组中(自己) 734 def 数组(个体): 735提高了NotImplementedError(无法将符号张量({})转换为小数" -> 736数组.".format(self.name)) 737 738分 len (自己):
~/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:无法转换符号张量 (up_sampling2d_4_target:0)转换为numpy
NotImplementedError: Cannot convert a symbolic Tensor (up_sampling2d_4_target:0) to a numpy
数组.
推荐答案
您不能将numpy
与张量流Tensor
一起使用,因为它们是两回事.
You can't use numpy
with a tensorflow Tensor
they're two different things.
问题在这里:
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)
这篇关于NotImplementedError:无法将符号张量(up_sampling2d_4_target:0)转换为numpy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!