问题遇到的现象和发生背景
加载中间层模型输出的时候报错
> raise AttributeError('Layer ' + self.name + ' has no inbound nodes.')
E AttributeError: Layer resnet has no inbound nodes.
问题相关代码,请勿粘贴截图
这是Resnet50模型
class ResNet(Model):
def get_config(self):
config = {
"block_layers": self.block_layers,
"base_filters": self.base_filters,
"block_type": self.block_type
}
base_config = super(ResNet, self).get_config()
return dict(list(base_config.items()) + list(config.items()))
def __init__(self, block_layers, name="resnet", base_filters=64, block_type='deep'):
"""
pa
:param block_layers:
:param base_filters:
:param block_type: deep or simple
"""
super(ResNet, self).__init__(name=name)
self.block_layers = block_layers
self.base_filters = base_filters
self.block_type = block_type
self.feature = Sequential([
layers.Conv2D(filters=base_filters, kernel_size=7, strides=2),
layers.BatchNormalization(),
layers.ReLU(),
layers.MaxPool2D(pool_size=3, strides=2),
])
nlb_dims = base_filters * 4
if block_type == "deep":
nlb_dims = base_filters * 16
self.block1 = build_block(base_filters, block_layers[0], init_stride=1, block_type=block_type,
is_downsample=True, name="res_block_1")
self.block2 = build_block(base_filters * 2, block_layers[1], block_type=block_type, name='res_block_2')
self.block3 = build_block(base_filters * 4, block_layers[2], block_type=block_type, name='res_block_3')
# 非局部网络
self.nlb = NonLocalBlock(input_dims=nlb_dims)
self.block4 = build_block(base_filters * 8, block_layers[3], block_type=block_type, name='res_block_4')
def call(self, inputs, training=None, mask=None):
x = inputs
x = self.feature(x)
x = self.block1(x)
x = self.block2(x)
x = self.block3(x)
x = self.nlb(x)
x = self.block4(x)
return x
这是让Resnet50作为中间层的模型
class TestModel(Model):
def __init__(self, drop_rate=0.5, in_shape=(128, 128, 1), block_layers=(3, 4, 6, 3)):
super(HomographyModel, self).__init__()
# 特征提取
self.resnet = ResNet(block_layers=block_layers)
# self.resnet = resnet(block_layers=block_layers)
self.avgpool = layers.GlobalAvgPool2D(name="gap")
self.fc = Sequential([
layers.Flatten(),
layers.Dense(1024),
layers.BatchNormalization(),
layers.ReLU(),
layers.Dropout(rate=self.drop_rate),
layers.Dense(8)], name="fc")
self.build([(None,) + in_shape, (None,) + in_shape])
运行结果及报错内容
print(model.get_layer("resnet").output.shape)
if not self._inbound_nodes:
raise AttributeError('Layer ' + self.name + ' has no inbound nodes.')
E AttributeError: Layer resnet has no inbound nodes.
我的解答思路和尝试过的方法
将类方式建模改为方法建模,但是精度就下不去了