python - 使用 Tensorflow 2.0 模型子类化访问层的输入/输出
问题描述
在大学练习中,我使用了 TF2.0 的模型子类 API。这是我的代码(它是 Alexnet 架构,如果您想知道的话……):
class MyModel(Model):
def __init__(self):
super(MyModel, self).__init__()
# OPS
self.relu = Activation('relu', name='ReLU')
self.maxpool = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), padding='valid', name='MaxPool')
self.softmax = Activation('softmax', name='Softmax')
# Conv layers
self.conv1 = Conv2D(filters=96, input_shape=(224, 224, 3), kernel_size=(11, 11), strides=(4, 4), padding='same',
name='conv1')
self.conv2a = Conv2D(filters=128, kernel_size=(5, 5), strides=(1, 1), padding='same', name='conv2a')
self.conv2b = Conv2D(filters=128, kernel_size=(5, 5), strides=(1, 1), padding='same', name='conv2b')
self.conv3 = Conv2D(filters=384, kernel_size=(3, 3), strides=(1, 1), padding='same', name='conv3')
self.conv4a = Conv2D(filters=192, kernel_size=(3, 3), strides=(1, 1), padding='same', name='conv4a')
self.conv4b = Conv2D(filters=192, kernel_size=(3, 3), strides=(1, 1), padding='same', name='conv4b')
self.conv5a = Conv2D(filters=128, kernel_size=(3, 3), strides=(1, 1), padding='same', name='conv5a')
self.conv5b = Conv2D(filters=128, kernel_size=(3, 3), strides=(1, 1), padding='same', name='conv5b')
# Fully-connected layers
self.flatten = Flatten()
self.dense1 = Dense(4096, input_shape=(100,), name='FC_4096_1')
self.dense2 = Dense(4096, name='FC_4096_2')
self.dense3 = Dense(1000, name='FC_1000')
# Network definition
def call(self, x, **kwargs):
x = self.conv1(x)
x = self.relu(x)
x = tf.nn.local_response_normalization(x, depth_radius=2, alpha=2e-05, beta=0.75, bias=1.0)
x = self.maxpool(x)
x = tf.concat((self.conv2a(x[:, :, :, :48]), self.conv2b(x[:, :, :, 48:])), 3)
x = self.relu(x)
x = tf.nn.local_response_normalization(x, depth_radius=2, alpha=2e-05, beta=0.75, bias=1.0)
x = self.maxpool(x)
x = self.conv3(x)
x = self.relu(x)
x = tf.concat((self.conv4a(x[:, :, :, :192]), self.conv4b(x[:, :, :, 192:])), 3)
x = self.relu(x)
x = tf.concat((self.conv5a(x[:, :, :, :192]), self.conv5b(x[:, :, :, 192:])), 3)
x = self.relu(x)
x = self.maxpool(x)
x = self.flatten(x)
x = self.dense1(x)
x = self.relu(x)
x = self.dense2(x)
x = self.relu(x)
x = self.dense3(x)
return self.softmax(x)
我的目标是访问任意层的输出(为了最大化特定神经元的激活,如果你必须确切知道:))。问题是尝试访问任何层的输出时,都会出现属性错误。例如:
model = MyModel()
print(model.get_layer('conv1').output)
# => AttributeError: Layer conv1 has no inbound nodes.
我在 SO 中发现了一些与此错误有关的问题,他们都声称我必须在第一层定义输入形状,但正如您所看到的 - 它已经完成(参见函数self.conv1
中的定义__init__
)!
我确实发现,如果我定义了一个keras.layers.Input
对象,我确实设法获得了 的输出conv1
,但是尝试访问更深层会失败,例如:
model = MyModel()
I = tf.keras.Input(shape=(224, 224, 3))
model(I)
print(model.get_layer('conv1').output)
# prints Tensor("my_model/conv1/Identity:0", shape=(None, 56, 56, 96), dtype=float32)
print(model.get_layer('FC_1000').output)
# => AttributeError: Layer FC_1000 has no inbound nodes.
我用谷歌搜索了我在路上遇到的每一个异常,但没有找到答案。在这种情况下,如何访问任何图层的输入/输出(或输入/输出 _shape 属性)?
解决方案
在子类模型中没有层图,它只是一段代码(模型call
函数)。创建模型类的实例时未定义层连接。因此,我们需要首先通过调用call
方法来构建模型。
试试这个:
model = MyModel()
inputs = tf.keras.Input(shape=(224,224,3))
model.call(inputs)
# instead of model(I) in your code.
完成后创建此模型图。
for i in model.layers:
print(i.output)
# output
# Tensor("ReLU_7/Relu:0", shape=(?, 56, 56, 96), dtype=float32)
# Tensor("MaxPool_3/MaxPool:0", shape=(?, 27, 27, 96), dtype=float32)
# Tensor("Softmax_1/Softmax:0", shape=(?, 1000), dtype=float32)
# ...
推荐阅读
- mysql - Node.JS async/await MySQL 获取插入行的结果
- jasper-reports - JasperReports 导出到 Json 未正确处理“null”
- android - 选择文本时展开带有 EditText 的对话框
- c# - 编写代码片段以在方法链接中使用
- python - 本地下载熊猫失败“无法导入名称'ndarray'”
- android - 我的 Android 应用程序的 Firebase Analytics StreamView 中的“(其他)”位置、“(其他)”应用程序版本是什么?
- android - Android Studio 生成旧版APK
- visual-studio - VS2017 MFC 警告:ID 0xE001 没有消息行提示
- c++ - 专门用于自定义容器的 std::swap 算法
- machine-learning - 验证准确率在训练准确率增加时波动?