• 【596】keras显示网络结构图


    参考1:【推荐】怎么显示Keras的网络结构和其中的参数

    参考2:【推荐】Mac BigSur:安装homebrew(国内源)+Graphviz

    参考3:mac下的Graphviz安装及使用

    参考4:Mac 安装 Graphviz-python

      也可以在线实现,https://netron.app,需要 *.h5 文件,信息更智能,但是没有输入,不过用了不同颜色显示。(示例图在文末)


    1. 安装 pydot

    pip install pydot

    2. 安装 pydot-ng

    pip install pydot-ng

    3. 安装 homebrew(国内源)

      打开终端,输入以下代码:

    /bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"
    

      选择序号:1

      执行脚本:Y 

    4. 安装 Graphviz

      homebrew安装完毕后,运行 brew install graphviz即可

    brew install graphviz

      参考2可以有效解决

      举例:

    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)
    

      输出

    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 示例 

    本地版安装教程:https://github.com/lutzroeder/netron 

  • 相关阅读:
    http协议的状态码——400,401,403,404,500,502,503,301,302等常见网页错误代码
    JS中的动态合集与静态合集
    对联
    诗词
    文言文
    Youth Is Not a Time of Life
    JS探秘——那些你理解存在偏差的问题
    JS中的加号+运算符详解
    支持HTTP2协议
    银行卡信息查询接口
  • 原文地址:https://www.cnblogs.com/alex-bn-lee/p/14974873.html
Copyright © 2020-2023  润新知