简体   繁体   中英

How to apply TimeDistributed layer on a CNN block?

Here is my attempt:

inputs = Input(shape=(config.N_FRAMES_IN_SEQUENCE, config.IMAGE_H, config.IMAGE_W, config.N_CHANNELS))

def cnn_model(inputs):
    x = Conv2D(filters=32, kernel_size=(3,3), padding='same', activation='relu')(inputs)
    x = MaxPooling2D(pool_size=(2, 2))(x)

    x = Conv2D(filters=32, kernel_size=(3,3), padding='same', activation='relu')(x)
    x = MaxPooling2D(pool_size=(2, 2))(x)

    x = Conv2D(filters=64, kernel_size=(3,3), padding='same', activation='relu')(x)
    x = MaxPooling2D(pool_size=(2, 2))(x)

    x = Conv2D(filters=64, kernel_size=(3,3), padding='same', activation='relu')(x)
    x = MaxPooling2D(pool_size=(2, 2))(x)

    x = Conv2D(filters=128, kernel_size=(3,3), padding='same', activation='relu')(x)
    x = MaxPooling2D(pool_size=(2, 2))(x)

    return x

x = TimeDistributed(cnn_model)(inputs)

Which gives the following error:

AttributeError: 'function' object has no attribute 'built'

You need to use Lambda layer and wrap your function inside it:

# cnn_model function the same way as you defined it ...

x = TimeDistributed(Lambda(cnn_model))(inputs)

Alternatively, you can define that block as a model and then apply TimeDistributed layer on it:

def cnn_model():
    input_frame = Input(shape=(config.IMAGE_H, config.IMAGE_W, config.N_CHANNELS))

    x = Conv2D(filters=32, kernel_size=(3,3), padding='same', activation='relu')(input_frame)
    x = MaxPooling2D(pool_size=(2, 2))(x)

    x = Conv2D(filters=32, kernel_size=(3,3), padding='same', activation='relu')(x)
    x = MaxPooling2D(pool_size=(2, 2))(x)

    x = Conv2D(filters=64, kernel_size=(3,3), padding='same', activation='relu')(x)
    x = MaxPooling2D(pool_size=(2, 2))(x)

    x = Conv2D(filters=64, kernel_size=(3,3), padding='same', activation='relu')(x)
    x = MaxPooling2D(pool_size=(2, 2))(x)

    x = Conv2D(filters=128, kernel_size=(3,3), padding='same', activation='relu')(x)
    x = MaxPooling2D(pool_size=(2, 2))(x)

    model = Model(input_frame, x)
    return model

inputs = Input(shape=(config.N_FRAMES_IN_SEQUENCE, config.IMAGE_H, config.IMAGE_W, config.N_CHANNELS))

x = TimeDistributed(cnn_model())(inputs)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM