【问题标题】:How to add more layers to existing model (eg. teachable machine application model)?如何为现有模型添加更多层(例如可教机器应用模型)?
【发布时间】:2021-02-17 23:13:29
【问题描述】:

我正在尝试通过在输出层之前添加更多层来使用来自可教机器应用程序https://teachablemachine.withgoogle.com/ 的谷歌模型。 当我重新训练模型时,总是返回这个错误:

ValueError:dense_25 层的输入 0 与该层不兼容:输入形状的预期轴 -1 具有值 5,但接收到形状为 [20, 512] 的输入

这是我的方法:

重新训练模型时返回错误:

如果我在不添加新层的情况下重新训练模型,它就可以正常工作。 谁能告诉我是什么问题?

【问题讨论】:

    标签: machine-learning computer-vision keras-layer conv-neural-network image-classification


    【解决方案1】:

    更新答案

    如果你想为预训练模型在两层之间添加层,它并不像使用 add 方法添加层那么简单。如果这样做会导致意外的行为

    错误分析:

    如果你编译模型如下(如你指定):

    model.layers[-1].add(Dense(512, activation ="relu"))
    model.add(Dense(128, activation="relu"))
    model.add(Dense(32))
    model.add(Dense(5))
    

    模型摘要的输出:

    Model: "sequential_12"
    _________________________________________________________________
    Layer (type)                 Output Shape              Param #   
    =================================================================
    sequential_9 (Sequential)    (None, 1280)              410208    
    _________________________________________________________________
    sequential_11 (Sequential)   (None, 512)               131672    
    _________________________________________________________________
    dense_12 (Dense)             (None, 128)               768       
    _________________________________________________________________
    dense_13 (Dense)             (None, 32)                4128      
    _________________________________________________________________
    dense_14 (Dense)             (None, 5)                 165       
    =================================================================
    Total params: 546,941
    Trainable params: 532,861
    Non-trainable params: 14,080
    _________________________________________________________________
    

    这里的一切看起来都不错,但仔细看看:

    for l in model.layers:
      print("layer : ", l.name, ", expects input  of shape : ",l.input_shape)
    

    输出:

    layer :  sequential_9 , expects input  of shape :  (None, 224, 224, 3)
    layer :  sequential_11 , expects input  of shape :  (None, 1280)
    layer :  dense_12 , expects input  of shape :  (None, 5) <-- **PROBLEM**
    layer :  dense_13 , expects input  of shape :  (None, 128)
    layer :  dense_14 , expects input  of shape :  (None, 32)
    

    问题 这里是dense_12 期望输入形状为(None, 5) 但它应该期望输入形状为(None, 512),因为我们已将Dense(512) 添加到sequential_11,可能的原因将像上面指定的那样添加层可能不会更新一些属性,例如sequential_11的输出形状,因此在前向传递期间,sequential_11的输出和dense_12层的输入之间存在不匹配(在您的情况下为dense_25)

    可能的解决方法是:

    对于您的问题“在sequential_9 和sequential_11 之间添加层”,您可以在sequential_9 和sequential_11 之间添加任意数量的层,但始终确保最后添加层的输出形状应与sequ​​ential_11 预期的输入形状相匹配。在本例中为 1280。

    代码:

    sequential_1 = model.layers[0] # re-using pre-trained model
    sequential_2 = model.layers[1]
    
    from tensorflow.keras.layers import Input
    from tensorflow.keras.layers import Dense
    from tensorflow.keras.models import Model
    
    inp_sequential_1 = Input(sequential_1.layers[0].input_shape[1:])
    out_sequential_1 = sequential_1(inp_sequential_1)
    
    #adding layers in between sequential_9 and sequential_11
    out_intermediate = Dense(512, activation="relu")(out_sequential_1)
    out_intermediate = Dense(128, activation ="relu")(out_intermediate)
    out_intermediate = Dense(32, activation ="relu")(out_intermediate)
    
    # always make sure to include a layer with output shape matching input shape of sequential 11, in this case 1280
    out_intermediate = Dense(1280, activation ="relu")(out_intermediate)
    
    output = sequential_2(out_intermediate) # output of intermediate layers are given to sequential_11 
    
    final_model = Model(inputs=inp_sequential_1, outputs=output)
    

    模型摘要的输出:

    Model: "functional_3"
    _________________________________________________________________
    Layer (type)                 Output Shape              Param #   
    =================================================================
    input_5 (InputLayer)         [(None, 224, 224, 3)]     0         
    _________________________________________________________________
    sequential_9 (Sequential)    (None, 1280)              410208    
    _________________________________________________________________
    dense_15 (Dense)             (None, 512)               655872    
    _________________________________________________________________
    dense_16 (Dense)             (None, 128)               65664     
    _________________________________________________________________
    dense_17 (Dense)             (None, 32)                4128      
    _________________________________________________________________
    dense_18 (Dense)             (None, 1280)              42240     
    _________________________________________________________________
    sequential_11 (Sequential)   (None, 5)                 128600    
    =================================================================
    Total params: 1,306,712
    Trainable params: 1,292,632
    Non-trainable params: 14,080
    

    【讨论】:

    • 感谢您的宝贵时间。我确实听从了您的建议,不幸的是,它返回了另一个错误。
    • 它返回了另一个错误:----> 4 input = Input(model.layers[0].input_shape[0]) 296 '输入,不能同时输入。') 297如果 batch_input_shape 为 None 且 shape 为 None 且 tensor 为 None:--> 298 raise ValueError('请向 Input 提供 shape' 299 ' 或 tensor 参数。请注意,' 300 'shape 不会包括批次 ' ValueError: 请向输入提供 shape 或 tensor 参数。请注意,shape 不包括批次维度。
    • 哦,我忘了。 layer.input_shape 将第一个轴返回为 None ,表示可变批量大小。不包括将修复错误。
    • 它可以工作,但它破坏了原始模型架构。让我们回到上面的原始模型,是否可以在sequential_9和sequential_11之间添加一些层而不破坏其架构?您可以从下面的 github 链接获取模型:github.com/seansothey/Research_Project/blob/main/…
    • 太棒了!!!它有效,非常感谢您的时间。还有一个问题,之所以在后面输出之前添加dense layer 1280,是为了遵循原来的架构,因为sequential_11预计要连接1280个单元。我说的对吗?
    猜你喜欢
    • 2019-09-08
    • 1970-01-01
    • 2019-10-28
    • 1970-01-01
    • 2019-03-21
    • 1970-01-01
    • 1970-01-01
    • 2011-05-19
    • 1970-01-01
    相关资源
    最近更新 更多