如何使用嵌套模型显示 Tensorflow 模型中的所有层?
How to show all layers in a Tensorflow model with nested model?
如何使用模型库显示 tensorflow 模型中的所有图层?
base_model = keras.applications.MobileNetV3Small(
input_shape=model_input_shape,
include_top=False,
weights="imagenet",
)
# =================== build model
model = keras.Sequential(
[
keras.Input(shape=image_shape),
preprocessing.Resizing(*model_input_shape[:2]),
preprocessing.Rescaling(1.0 / 255),
base_model,
layers.GlobalAveragePooling2D(),
# missing dropout
layers.Dense(1, activation="sigmoid"),
]
)
model.summary()
输出是这样的:
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
resizing (Resizing) (None, 224, 224, 3) 0
_________________________________________________________________
rescaling_1 (Rescaling) (None, 224, 224, 3) 0
_________________________________________________________________
MobilenetV3small (Functional (None, 7, 7, 1024) 1529968 <---------- why can't I see all layers here?
_________________________________________________________________
global_average_pooling2d (Gl (None, 1024) 0
_________________________________________________________________
dense (Dense) (None, 1) 1025
如何显示所有图层?
for layer in model.layers:
print(layer)
上面也有同样的问题。我做错了什么?
在这样的设置中,base_model
充当单层,即。成为嵌套。要检查它,您可以尝试
model.layers[2].summary()
for i, layer in enumerate(model.layers):
if i == 2:
for nested_layer in layer.layers:
print(nested_layer)
或者,更直观地说,您可以使用 this solution.
def summary_plus(layer, i=0):
if hasattr(layer, 'layers'):
if i != 0:
layer.summary()
for l in layer.layers:
i += 1
summary_plus(l, i=i)
summary_plus(model)
或者,您也可以使用plot_model
功能
keras.utils.plot_model(
model,
expand_nested=True # < make it true
)
更新 1:就此问题提出。 Keras #15239。希望能尽快解决。
更新 2:model.summary
现在有 expand_nested
参数。 #15251
如何使用模型库显示 tensorflow 模型中的所有图层?
base_model = keras.applications.MobileNetV3Small(
input_shape=model_input_shape,
include_top=False,
weights="imagenet",
)
# =================== build model
model = keras.Sequential(
[
keras.Input(shape=image_shape),
preprocessing.Resizing(*model_input_shape[:2]),
preprocessing.Rescaling(1.0 / 255),
base_model,
layers.GlobalAveragePooling2D(),
# missing dropout
layers.Dense(1, activation="sigmoid"),
]
)
model.summary()
输出是这样的:
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
resizing (Resizing) (None, 224, 224, 3) 0
_________________________________________________________________
rescaling_1 (Rescaling) (None, 224, 224, 3) 0
_________________________________________________________________
MobilenetV3small (Functional (None, 7, 7, 1024) 1529968 <---------- why can't I see all layers here?
_________________________________________________________________
global_average_pooling2d (Gl (None, 1024) 0
_________________________________________________________________
dense (Dense) (None, 1) 1025
如何显示所有图层?
for layer in model.layers:
print(layer)
上面也有同样的问题。我做错了什么?
在这样的设置中,base_model
充当单层,即。成为嵌套。要检查它,您可以尝试
model.layers[2].summary()
for i, layer in enumerate(model.layers):
if i == 2:
for nested_layer in layer.layers:
print(nested_layer)
或者,更直观地说,您可以使用 this solution.
def summary_plus(layer, i=0):
if hasattr(layer, 'layers'):
if i != 0:
layer.summary()
for l in layer.layers:
i += 1
summary_plus(l, i=i)
summary_plus(model)
或者,您也可以使用plot_model
功能
keras.utils.plot_model(
model,
expand_nested=True # < make it true
)
更新 1:就此问题提出。 Keras #15239。希望能尽快解决。
更新 2:model.summary
现在有 expand_nested
参数。 #15251