运行环境:
Windows下TensorFlow2.0+Keras
- 问题一:版本升级导致的错误(主要是本人小白菜。。。)
TensorFlow2.0与1.X的API命称和参数有所改变。例如:
1 tf.to_float,tf.to_int32 => tf.cast(x,dtype) or tf.compat.v1.to_float
2 tf.log => tf.math.log
3 tf.rint => tf.math.rint
4 tf.sets.set_intersection => tf.sets.intersection
5 tf.sparse_tensor_to_dense => tf.sparse.to_dense
6 tf.random_shuffle => tf.random.shuffle
7 tf.nn.softmax_cross_entropy_with_logits_v2 => tf.nn.softmax_cross_entropy_with_logits
8 tf.reduce_mean => tf.math.reduce_mean
9 tf.add_n => tf.math.add_n
。。。。。。
###########################不理解的问题###############################
报错:using a tf.Tensor
as a Python bool
is not allowed in Graph execution. Use Eager execution or decorate this function with @tf.function.
if layer.output in self.keras_model.losses:
continue
报错:using a `tf.Tensor` as a Python `bool` is not allowed in Graph execution. Use Eager execution or decorate this function with @tf.function.
语句作用:逐表添加loss函数
解决方法:
1 exists_names=[] # 创建空表,有就不再添加
for name in loss_names:
layer = self.keras_model.get_layer(name)
#if layer is None or layer.output in self.keras_model.losses:
# continue
2 if name in exists_names:
#存在就转到下一次循环
contine
#入表
3 exists_names.append(name)
描述:都是为了添加需要的loss,但我用2.0版本的时候的layer.output和self.keras_model.losses两个组合时会报错,所以重建了个列表进行添加操作。
参考:这位兄弟让我很感激:https://blog.csdn.net/na_fantastic/article/details/102548647
############ keras2.3.0有API改动,改用add_metric##########
报错:AttributeError: ‘Model’ object has no attribute ‘metrics_tensors’
############# keras2.3.0有API改动,改用add_metric##########
#self.keras_model.metrics_names.append(name)
#self.keras_model.metrics_tensors.append(tf.math.reduce_mean(layer.output, keepdims=True))
self.keras_model.add_metric(layer.output, name=name)
注意:add_metric(value, aggregation=None, name=None)中的参数aggregation有的版本可能没有。比如我的:
def add_metric(self, value, name=None):
"""Adds metric tensor to the layer.
# Arguments
value: Metric tensor.
name: String metric name.
################scipy 版本问题############################
报错:module ‘scipy.misc’ has no attribute ‘imresize’
np.round(scipy.misc.imresize(class_mask.astype(float), (gt_h, gt_w),
interp='nearest') / 255.0).astype(bool)
描述:这个版本没有imresize这个函数
解决方法:检查是否有pillow 并且 scipy==1.2.1 退回到1.21版本(这方法有点无脑缺啥补啥。。。)
TypeError: crop_and_resize_v2() got an unexpected keyword argument ‘box_ind’
box_ind参数名改为 box_indices
output = tf.image.crop_and_resize(image=features,
boxes=rois,
box_ind=batch_index,
box_indices=batch_index,
## box_ind参数名改为 box_indices
crop_size=self.pool_size)
TypeError: unhashable type: ‘ListWrapper’
# 增加损失函数,首先清除之前的,防止重复
keras_model._losses = []
keras_model._per_input_losses = {}
layer_exists_name = []
for name in loss_names:
layer = keras_model.get_layer(name)
if layer is None or name in layer_exists_name:
continue
layer_exists_name.append(name)
loss = (tf.math.reduce_mean(layer.output, keepdims=True)
* loss_weights.get(name, 1.))
keras_model.add_loss(loss)
# 增加 loss 时出现报错TypeError: unhashable type: 'ListWrapper'
列表不可哈希,这里错误来源不太懂
# keras_model._losses = []
# keras_model._per_input_losses = {}
注释掉这两句就行
-
显存不足:
-
后续补充