示例代码:
model = Model(inputs=self.inpt, outputs=self.net)
model.compile(loss='categorical_crossentropy', optimizer='adadelta', metrics=['accuracy'])
print("[INFO] Method 1...")
model.summary()
print("[INFO] Method 2...")
for i in range(len(model.layers)):
print(model.get_layer(index=i).output)
print("[INFO] Method 3...")
for layer in model.layers:
print(layer.output_shape)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2019/5/20
# @Author : Chen
from keras.models import Model
from keras.layers import Dense, Flatten, Input
from keras.layers import Conv2D
class Example:
def __init__(self):
self.inpt = Input(shape=(224, 224, 3))
self.net = self.build_network()
def build_network(self):
inpt = self.inpt
x = Conv2D(64, kernel_size=(3, 3), padding='same', activation='relu')(inpt)
...
x = Flatten()(x)
x = Dense(1000)(x)
return x
def get_layer(self):
model = Model(inputs=self.inpt, outputs=self.net)
model.compile(loss='categorical_crossentropy', optimizer='adadelta', metrics=['accuracy'])
print("[INFO] Method 1...")
model.summary()
print("[INFO] Method 2...")
for i in range(len(model.layers)):
print(model.get_layer(index=i).output)
print("[INFO] Method 3...")
for layer in model.layers:
print(layer.output_shape)
if __name__ == '__main__':
ex = Example()
ex.get_layer()
输出结果:
[INFO] Method 1...
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) (None, 224, 224, 3) 0
_________________________________________________________________
conv2d_1 (Conv2D) (None, 224, 224, 64) 1792
_________________________________________________________________
flatten_1 (Flatten) (None, 3211264) 0
_________________________________________________________________
dense_1 (Dense) (None, 1000) -108370229
=================================================================
Total params: -1,083,700,504
Trainable params: -1,083,700,504
Non-trainable params: 0
_________________________________________________________________
[INFO] Method 2...
Tensor("input_1:0", shape=(?, 224, 224, 3), dtype=float32)
Tensor("conv2d_1/Relu:0", shape=(?, 224, 224, 64), dtype=float32)
Tensor("flatten_1/Reshape:0", shape=(?, ?), dtype=float32)
Tensor("dense_1/BiasAdd:0", shape=(?, 1000), dtype=float32)
[INFO] Method 3...
(None, 224, 224, 3)
(None, 224, 224, 64)
(None, 3211264)
(None, 1000)
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Keras输出每一层网络大小 - Python技术站