@[TOC](#931-> raise ValueError(error_str) ValueError: The following Variables were created within a Lambda layer (anchors)but are not tracked by said layer报错记录)
使用说明
使用的是Colab运行 MaskRCNN 链接[https://github.com/matterport/Mask_RCNN]
colab中使用命令:!git clone https://github.com/matterport/Mask_RCNN.git 就可以下载
版本:【Keras+tensorflow/tensorflow-gpu+h5py】
!pip install keras == 2.4.3
!pip install tensorflow == 2.3.1
!pip install tensorflow-gpu == 2.3.1
!pip install h5py==2.10.0
报错记录
- 出错位置:
model = modellib.MaskRCNN(mode=“training”, config=config,model_dir=MODEL_DIR)
该行代码出现红色波浪线 - 报错显示
图片展示:
错误提示:
ValueError:
The following Variables were created within a Lambda layer (anchors)
but are not tracked by said layer:
<tf.Variable ‘anchors/Variable:0’ shape=(1, 16368, 4) dtype=float32>
The layer cannot safely ensure proper Variable reuse across multiple
calls, and consquently this behavior is disallowed for safety. Lambda
layers are not well suited to stateful computation; instead, writing a
subclassed Layer is the recommend way to define layers with
Variables.
修改方法:
找到位于file: /mrcnn/model.py line: 1935repo 中): IN[0]: 的这行代码
anchors = KL.Lambda(lambda x: tf.Variable(anchors), name=“anchors”)(input_image)
改为:
class AnchorsLayer(tf.keras.layers.Layer):
def __init__(self, name="anchors", **kwargs):
super(AnchorsLayer, self).__init__(name=name, **kwargs)def call(self, anchor):
return anchor
def get_config(self) :
config = super(AnchorsLayer, self).get_config()
return config
anchor_layer = AnchorsLayer(name="anchors")
anchors = anchor_layer(anchors)
参考链接
链接1: https://qa.1r1g.com/sf/ask/4555140411/.
链接2: https://blog.csdn.net/qq_22520587/article/details/104823338.