Keras 使用 TimeDistributed 预训练 CNN

rAy*_*yyy 7 python keras tensorflow recurrent-neural-network resnet

这是我的问题,我想在 TimeDistributed 层中使用预训练 CNN 网络之一。但是我在实施它时遇到了一些问题。

这是我的模型:

def bnn_model(max_len):
    # sequence length and resnet input size
    x = Input(shape=(maxlen, 224, 224, 3))

    base_model = ResNet50.ResNet50(weights='imagenet',  include_top=False)

    for layer in base_model.layers:
        layer.trainable = False

    som = TimeDistributed(base_model)(x)

    #the ouput of the model is [1, 1, 2048], need to squeeze
    som = Lambda(lambda x: K.squeeze(K.squeeze(x,2),2))(som)

    bnn = Bidirectional(LSTM(300))(som)
    bnn = Dropout(0.5)(bnn)

    pred = Dense(1, activation='sigmoid')(bnn)

    model = Model(input=x, output=pred)

    model.compile(optimizer=Adam(lr=1.0e-5), loss="mse", metrics=["accuracy"])

    return model
Run Code Online (Sandbox Code Playgroud)

编译模型时我没有错误。但是当我开始训练时,我收到以下错误:

tensorflow/core/framework/op_kernel.cc:975] Invalid argument: You must feed a value for placeholder tensor 'input_2' with dtype float
[[Node: input_2 = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/gpu:0"]()]]
Run Code Online (Sandbox Code Playgroud)

我检查过并且确实发送了 float32,但是对于 input1,input2 是 pretrain Resnet 中存在的输入。

只是在这里有一个概述是模型摘要。(注意:奇怪的是它没有显示 Resnet 内部发生的事情,但没关系)

____________________________________________________________________________________________________
Layer (type)                     Output Shape          Param #     Connected to                     
====================================================================================================
input_1 (InputLayer)             (None, 179, 224, 224, 0                                            
____________________________________________________________________________________________________
timedistributed_1 (TimeDistribut (None, 179, 1, 1, 204 23587712    input_1[0][0]                    
____________________________________________________________________________________________________
lambda_1 (Lambda)                (None, 179, 2048)     0           timedistributed_1[0][0]          
____________________________________________________________________________________________________
bidirectional_1 (Bidirectional)  (None, 600)           5637600     lambda_1[0][0]                   
____________________________________________________________________________________________________
dropout_1 (Dropout)              (None, 600)           0           bidirectional_1[0][0]            
____________________________________________________________________________________________________
dense_1 (Dense)                  (None, 1)             601         dropout_1[0][0]                  
====================================================================================================
Total params: 29,225,913
Trainable params: 5,638,201
Non-trainable params: 23,587,712
____________________________________________________________________________________________________
Run Code Online (Sandbox Code Playgroud)

我猜我没有正确使用 TimeDistributed 并且我看到没有人试图这样做。我希望有人可以指导我。

编辑:

问题来自ResNet50.ResNet50(weights='imagenet', include_top=False)于在图中创建自己的输入这一事实。

所以我想我需要做一些类似的事情,ResNet50.ResNet50(weights='imagenet', input_tensor=x, include_top=False)但我不知道如何将它与TimeDistributed.

我试过

base_model = Lambda(lambda x : ResNet50.ResNet50(weights='imagenet',  input_tensor=x, include_top=False))
som = TimeDistributed(base_model)(in_ten)
Run Code Online (Sandbox Code Playgroud)

但它不起作用。

rAy*_*yyy 1

我的快速解决方案有点难看。

我只是复制了 ResNet 的代码,并将 TimeDistributed 添加到所有层,然后将权重从“基本”ResNet 加载到我的自定义 ResNet 上。

笔记:

为了能够分析这样的图像序列,确实需要 GPU 上的大量内存。