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,而True: ---> 16 loss_history = fit() 17如果np.min([mh.history ['val_loss'] for mh in loss_history])<-0.2: 18休息
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))
〜/ 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 / 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 / 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 / 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 在_prepare_total_loss(self,masks)中1711 1712如果 hasattr(loss_fn,'减少'): -> 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 / 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():
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])
<< strong> 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在 _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 / tensorflow_core / python / framework / ops.py 在数组中(自己) 734 def 数组(个体): 735提高了NotImplementedError(“无法将符号张量({})转换为小数” -> 736“数组。”。format(self.name)) 737 738分 len (自己):
NotImplementedError:无法转换符号张量 (up_sampling2d_4_target:0)转换为numpy
数组。
答案 0 :(得分: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)