【596】keras显示网络结构图
参考1:【推荐】怎么显示Keras的网络结构和其中的参数
参考2:【推荐】Mac BigSur:安装homebrew(国内源)+Graphviz
也可以在线实现,https://netron.app,需要 *.h5 文件,信息更智能,但是没有输入,不过用了不同颜色显示。(示例图在文末)
1. 安装 pydot
1 | pip install pydot |
2. 安装 pydot-ng
1 | pip install pydot - ng |
3. 安装 homebrew(国内源)
打开终端,输入以下代码:
1 | / bin / zsh - c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)" |
选择序号:1
执行脚本:Y
4. 安装 Graphviz
homebrew安装完毕后,运行 brew install graphviz即可
1 | brew install graphviz |
参考2可以有效解决
举例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | from tensorflow.keras import layers def get_model(img_size, num_classes): # 二维变三维,元组加法相当于 concat inputs = keras. Input (shape = img_size + ( 3 ,)) ### [First half of the network: downsampling inputs] ### # Entry block x = layers.Conv2D( 32 , 3 , strides = 2 , padding = "same" )(inputs) x = layers.BatchNormalization()(x) x = layers.Activation( "relu" )(x) previous_block_activation = x # Set aside residual # Blocks 1, 2, 3 are identical apart from the feature depth. for filters in [ 64 , 128 , 256 ]: x = layers.Activation( "relu" )(x) x = layers.SeparableConv2D(filters, 3 , padding = "same" )(x) x = layers.BatchNormalization()(x) x = layers.Activation( "relu" )(x) x = layers.SeparableConv2D(filters, 3 , padding = "same" )(x) x = layers.BatchNormalization()(x) x = layers.MaxPooling2D( 3 , strides = 2 , padding = "same" )(x) # Project residual residual = layers.Conv2D(filters, 1 , strides = 2 , padding = "same" )( previous_block_activation ) x = layers.add([x, residual]) # Add back residual previous_block_activation = x # Set aside next residual ### [Second half of the network: upsampling inputs] ### for filters in [ 256 , 128 , 64 , 32 ]: x = layers.Activation( "relu" )(x) x = layers.Conv2DTranspose(filters, 3 , padding = "same" )(x) x = layers.BatchNormalization()(x) x = layers.Activation( "relu" )(x) x = layers.Conv2DTranspose(filters, 3 , padding = "same" )(x) x = layers.BatchNormalization()(x) x = layers.UpSampling2D( 2 )(x) # Project residual residual = layers.UpSampling2D( 2 )(previous_block_activation) residual = layers.Conv2D(filters, 1 , padding = "same" )(residual) x = layers.add([x, residual]) # Add back residual previous_block_activation = x # Set aside next residual # Add a per-pixel classification layer outputs = layers.Conv2D(num_classes, 3 , activation = "softmax" , padding = "same" )(x) # Define the model model = keras.Model(inputs, outputs) return model # Free up RAM in case the model definition cells were run multiple times keras.backend.clear_session() # Build model model = get_model(img_size, num_classes) model.summary() # 结构图显示 from keras.utils.vis_utils import plot_model plot_model(model, to_file = 'Flatten.png' , show_shapes = True ) |
输出
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | Model: "model" __________________________________________________________________________________________________ Layer ( type ) Output Shape Param # Connected to = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = input_1 (InputLayer) [( None , 160 , 160 , 3 ) 0 __________________________________________________________________________________________________ conv2d (Conv2D) ( None , 80 , 80 , 32 ) 896 input_1[ 0 ][ 0 ] __________________________________________________________________________________________________ batch_normalization (BatchNorma ( None , 80 , 80 , 32 ) 128 conv2d[ 0 ][ 0 ] __________________________________________________________________________________________________ activation (Activation) ( None , 80 , 80 , 32 ) 0 batch_normalization[ 0 ][ 0 ] __________________________________________________________________________________________________ activation_1 (Activation) ( None , 80 , 80 , 32 ) 0 activation[ 0 ][ 0 ] __________________________________________________________________________________________________ separable_conv2d (SeparableConv ( None , 80 , 80 , 64 ) 2400 activation_1[ 0 ][ 0 ] __________________________________________________________________________________________________ batch_normalization_1 (BatchNor ( None , 80 , 80 , 64 ) 256 separable_conv2d[ 0 ][ 0 ] __________________________________________________________________________________________________ activation_2 (Activation) ( None , 80 , 80 , 64 ) 0 batch_normalization_1[ 0 ][ 0 ] __________________________________________________________________________________________________ separable_conv2d_1 (SeparableCo ( None , 80 , 80 , 64 ) 4736 activation_2[ 0 ][ 0 ] __________________________________________________________________________________________________ batch_normalization_2 (BatchNor ( None , 80 , 80 , 64 ) 256 separable_conv2d_1[ 0 ][ 0 ] __________________________________________________________________________________________________ max_pooling2d (MaxPooling2D) ( None , 40 , 40 , 64 ) 0 batch_normalization_2[ 0 ][ 0 ] __________________________________________________________________________________________________ conv2d_1 (Conv2D) ( None , 40 , 40 , 64 ) 2112 activation[ 0 ][ 0 ] __________________________________________________________________________________________________ add (Add) ( None , 40 , 40 , 64 ) 0 max_pooling2d[ 0 ][ 0 ] conv2d_1[ 0 ][ 0 ] __________________________________________________________________________________________________ activation_3 (Activation) ( None , 40 , 40 , 64 ) 0 add[ 0 ][ 0 ] __________________________________________________________________________________________________ separable_conv2d_2 (SeparableCo ( None , 40 , 40 , 128 ) 8896 activation_3[ 0 ][ 0 ] __________________________________________________________________________________________________ batch_normalization_3 (BatchNor ( None , 40 , 40 , 128 ) 512 separable_conv2d_2[ 0 ][ 0 ] __________________________________________________________________________________________________ activation_4 (Activation) ( None , 40 , 40 , 128 ) 0 batch_normalization_3[ 0 ][ 0 ] __________________________________________________________________________________________________ separable_conv2d_3 (SeparableCo ( None , 40 , 40 , 128 ) 17664 activation_4[ 0 ][ 0 ] __________________________________________________________________________________________________ batch_normalization_4 (BatchNor ( None , 40 , 40 , 128 ) 512 separable_conv2d_3[ 0 ][ 0 ] __________________________________________________________________________________________________ max_pooling2d_1 (MaxPooling2D) ( None , 20 , 20 , 128 ) 0 batch_normalization_4[ 0 ][ 0 ] __________________________________________________________________________________________________ conv2d_2 (Conv2D) ( None , 20 , 20 , 128 ) 8320 add[ 0 ][ 0 ] __________________________________________________________________________________________________ add_1 (Add) ( None , 20 , 20 , 128 ) 0 max_pooling2d_1[ 0 ][ 0 ] conv2d_2[ 0 ][ 0 ] __________________________________________________________________________________________________ activation_5 (Activation) ( None , 20 , 20 , 128 ) 0 add_1[ 0 ][ 0 ] __________________________________________________________________________________________________ separable_conv2d_4 (SeparableCo ( None , 20 , 20 , 256 ) 34176 activation_5[ 0 ][ 0 ] __________________________________________________________________________________________________ batch_normalization_5 (BatchNor ( None , 20 , 20 , 256 ) 1024 separable_conv2d_4[ 0 ][ 0 ] __________________________________________________________________________________________________ activation_6 (Activation) ( None , 20 , 20 , 256 ) 0 batch_normalization_5[ 0 ][ 0 ] __________________________________________________________________________________________________ separable_conv2d_5 (SeparableCo ( None , 20 , 20 , 256 ) 68096 activation_6[ 0 ][ 0 ] __________________________________________________________________________________________________ batch_normalization_6 (BatchNor ( None , 20 , 20 , 256 ) 1024 separable_conv2d_5[ 0 ][ 0 ] __________________________________________________________________________________________________ max_pooling2d_2 (MaxPooling2D) ( None , 10 , 10 , 256 ) 0 batch_normalization_6[ 0 ][ 0 ] __________________________________________________________________________________________________ conv2d_3 (Conv2D) ( None , 10 , 10 , 256 ) 33024 add_1[ 0 ][ 0 ] __________________________________________________________________________________________________ add_2 (Add) ( None , 10 , 10 , 256 ) 0 max_pooling2d_2[ 0 ][ 0 ] conv2d_3[ 0 ][ 0 ] __________________________________________________________________________________________________ activation_7 (Activation) ( None , 10 , 10 , 256 ) 0 add_2[ 0 ][ 0 ] __________________________________________________________________________________________________ conv2d_transpose (Conv2DTranspo ( None , 10 , 10 , 256 ) 590080 activation_7[ 0 ][ 0 ] __________________________________________________________________________________________________ batch_normalization_7 (BatchNor ( None , 10 , 10 , 256 ) 1024 conv2d_transpose[ 0 ][ 0 ] __________________________________________________________________________________________________ activation_8 (Activation) ( None , 10 , 10 , 256 ) 0 batch_normalization_7[ 0 ][ 0 ] __________________________________________________________________________________________________ conv2d_transpose_1 (Conv2DTrans ( None , 10 , 10 , 256 ) 590080 activation_8[ 0 ][ 0 ] __________________________________________________________________________________________________ batch_normalization_8 (BatchNor ( None , 10 , 10 , 256 ) 1024 conv2d_transpose_1[ 0 ][ 0 ] __________________________________________________________________________________________________ up_sampling2d_1 (UpSampling2D) ( None , 20 , 20 , 256 ) 0 add_2[ 0 ][ 0 ] __________________________________________________________________________________________________ up_sampling2d (UpSampling2D) ( None , 20 , 20 , 256 ) 0 batch_normalization_8[ 0 ][ 0 ] __________________________________________________________________________________________________ conv2d_4 (Conv2D) ( None , 20 , 20 , 256 ) 65792 up_sampling2d_1[ 0 ][ 0 ] __________________________________________________________________________________________________ add_3 (Add) ( None , 20 , 20 , 256 ) 0 up_sampling2d[ 0 ][ 0 ] conv2d_4[ 0 ][ 0 ] __________________________________________________________________________________________________ activation_9 (Activation) ( None , 20 , 20 , 256 ) 0 add_3[ 0 ][ 0 ] __________________________________________________________________________________________________ conv2d_transpose_2 (Conv2DTrans ( None , 20 , 20 , 128 ) 295040 activation_9[ 0 ][ 0 ] __________________________________________________________________________________________________ batch_normalization_9 (BatchNor ( None , 20 , 20 , 128 ) 512 conv2d_transpose_2[ 0 ][ 0 ] __________________________________________________________________________________________________ activation_10 (Activation) ( None , 20 , 20 , 128 ) 0 batch_normalization_9[ 0 ][ 0 ] __________________________________________________________________________________________________ conv2d_transpose_3 (Conv2DTrans ( None , 20 , 20 , 128 ) 147584 activation_10[ 0 ][ 0 ] __________________________________________________________________________________________________ batch_normalization_10 (BatchNo ( None , 20 , 20 , 128 ) 512 conv2d_transpose_3[ 0 ][ 0 ] __________________________________________________________________________________________________ up_sampling2d_3 (UpSampling2D) ( None , 40 , 40 , 256 ) 0 add_3[ 0 ][ 0 ] __________________________________________________________________________________________________ up_sampling2d_2 (UpSampling2D) ( None , 40 , 40 , 128 ) 0 batch_normalization_10[ 0 ][ 0 ] __________________________________________________________________________________________________ conv2d_5 (Conv2D) ( None , 40 , 40 , 128 ) 32896 up_sampling2d_3[ 0 ][ 0 ] __________________________________________________________________________________________________ add_4 (Add) ( None , 40 , 40 , 128 ) 0 up_sampling2d_2[ 0 ][ 0 ] conv2d_5[ 0 ][ 0 ] __________________________________________________________________________________________________ activation_11 (Activation) ( None , 40 , 40 , 128 ) 0 add_4[ 0 ][ 0 ] __________________________________________________________________________________________________ conv2d_transpose_4 (Conv2DTrans ( None , 40 , 40 , 64 ) 73792 activation_11[ 0 ][ 0 ] __________________________________________________________________________________________________ batch_normalization_11 (BatchNo ( None , 40 , 40 , 64 ) 256 conv2d_transpose_4[ 0 ][ 0 ] __________________________________________________________________________________________________ activation_12 (Activation) ( None , 40 , 40 , 64 ) 0 batch_normalization_11[ 0 ][ 0 ] __________________________________________________________________________________________________ conv2d_transpose_5 (Conv2DTrans ( None , 40 , 40 , 64 ) 36928 activation_12[ 0 ][ 0 ] __________________________________________________________________________________________________ batch_normalization_12 (BatchNo ( None , 40 , 40 , 64 ) 256 conv2d_transpose_5[ 0 ][ 0 ] __________________________________________________________________________________________________ up_sampling2d_5 (UpSampling2D) ( None , 80 , 80 , 128 ) 0 add_4[ 0 ][ 0 ] __________________________________________________________________________________________________ up_sampling2d_4 (UpSampling2D) ( None , 80 , 80 , 64 ) 0 batch_normalization_12[ 0 ][ 0 ] __________________________________________________________________________________________________ conv2d_6 (Conv2D) ( None , 80 , 80 , 64 ) 8256 up_sampling2d_5[ 0 ][ 0 ] __________________________________________________________________________________________________ add_5 (Add) ( None , 80 , 80 , 64 ) 0 up_sampling2d_4[ 0 ][ 0 ] conv2d_6[ 0 ][ 0 ] __________________________________________________________________________________________________ activation_13 (Activation) ( None , 80 , 80 , 64 ) 0 add_5[ 0 ][ 0 ] __________________________________________________________________________________________________ conv2d_transpose_6 (Conv2DTrans ( None , 80 , 80 , 32 ) 18464 activation_13[ 0 ][ 0 ] __________________________________________________________________________________________________ batch_normalization_13 (BatchNo ( None , 80 , 80 , 32 ) 128 conv2d_transpose_6[ 0 ][ 0 ] __________________________________________________________________________________________________ activation_14 (Activation) ( None , 80 , 80 , 32 ) 0 batch_normalization_13[ 0 ][ 0 ] __________________________________________________________________________________________________ conv2d_transpose_7 (Conv2DTrans ( None , 80 , 80 , 32 ) 9248 activation_14[ 0 ][ 0 ] __________________________________________________________________________________________________ batch_normalization_14 (BatchNo ( None , 80 , 80 , 32 ) 128 conv2d_transpose_7[ 0 ][ 0 ] __________________________________________________________________________________________________ up_sampling2d_7 (UpSampling2D) ( None , 160 , 160 , 64 ) 0 add_5[ 0 ][ 0 ] __________________________________________________________________________________________________ up_sampling2d_6 (UpSampling2D) ( None , 160 , 160 , 32 ) 0 batch_normalization_14[ 0 ][ 0 ] __________________________________________________________________________________________________ conv2d_7 (Conv2D) ( None , 160 , 160 , 32 ) 2080 up_sampling2d_7[ 0 ][ 0 ] __________________________________________________________________________________________________ add_6 (Add) ( None , 160 , 160 , 32 ) 0 up_sampling2d_6[ 0 ][ 0 ] conv2d_7[ 0 ][ 0 ] __________________________________________________________________________________________________ conv2d_8 (Conv2D) ( None , 160 , 160 , 3 ) 867 add_6[ 0 ][ 0 ] = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = Total params: 2 , 058 , 979 Trainable params: 2 , 055 , 203 Non - trainable params: 3 , 776 __________________________________________________________________________________________________ |
结构图
netron.app 示例
分类:
AI Related
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
2012-07-05 【055】长江水文数据自动记录程序