使用多层Keras模型作为卷积滤波器

时间:2017-05-14 18:46:55

标签: keras keras-layer

我在现有模型的最后一层有一个双层双向循环堆栈,它被训练用于从常量大小的图像中提取文本。我在重复堆栈之前向现有架构添加了一个额外的卷积层,以便在较大的图像上启用文本检测。该模型如下所示:

Layer (type)                 Output Shape              Param #
=================================================================
the_input (InputLayer)       (None, 64, 128, 3)        0
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 64, 128, 48)       3648
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 32, 64, 48)        0
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 32, 64, 64)        76864
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 16, 64, 64)        0
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 16, 64, 128)       204928
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 8, 32, 128)        0
_________________________________________________________________
conv2d_4 (Conv2D)            (None, 1, 1, 2048)        67110912
_________________________________________________________________
lambda_1 (Lambda)            (None, 1, 1, 1)           0
=================================================================

conv2d_4是新的卷积层,用于启用大小不是128 x 64的图像.lambda_1是一个存在指示符变量,每个滑动窗口位置有一个值。创建以下模型的代码如下:

input_data = Input(name='the_input', shape=input_shape)

def add_conv(prev_component, kernel_dims, num_filters, max_pool_dims=None, padding='same'):
    cnv = Conv2D(num_filters, kernel_dims, activation='relu',
                 padding=padding)(prev_component)
    if max_pool_dims is not None:
        cnv = MaxPooling2D(pool_size=max_pool_dims)(cnv)
    return cnv

prev = input_data
prev = add_conv(prev, (5, 5), 48, max_pool_dims=(2, 2))
prev = add_conv(prev, (5, 5), 64, max_pool_dims=(2, 1))
prev = add_conv(prev, (5, 5), 128, max_pool_dims=(2, 2))
#[kernel_height, kernel_width, prev_filter_count, new_filter_count]
prev = add_conv(prev, (8, 32), time_dense_size, padding='valid')

def is_plate_func(windowed_data):
    is_plate_w = K.variable(K.truncated_normal(stddev=0.1, shape=(1, 1, 2048, 1)))
    is_plate_b = K.variable(K.constant(.1, shape=[1]))
    is_plate_out = K.bias_add(K.conv2d(windowed_data, is_plate_w), is_plate_b)
    return is_plate_out

is_plate_lambda = Lambda(is_plate_func)(prev)
Model(inputs=input_data, outputs=[is_plate_lambda]).summary()

我希望以与lambda_1相同的方式附加到模型的循环堆栈的代码如下:

bdrnn_model = Sequential()
bdrnn_model.add(Reshape((2048, 1), input_shape=(1, 1, 2048)))
for idx in range(num_backward_layers):
    if idx == num_backward_layers - 1:
        merge_mode = 'concat'
    else:
        merge_mode = 'sum'
    bdrnn_model.add(Bidirectional(GRU(rnn_size, return_sequences=True),
                                  merge_mode=merge_mode))
bdrnn_model.add(Dense(num_output_characters + 2, activation='relu'))

有没有办法让Keras使用'bdrnn_model'作为卷积滤波器?

0 个答案:

没有答案