ypwhs / captcha_break

验证码识别
MIT License
2.72k stars 686 forks source link

ctc.ipynb merge([gru_1, gru_1b], mode='sum') 找不到这个函数 #23

Open QGB opened 5 years ago

QGB commented 5 years ago

WARNING:tensorflow:From /home/qgb/.local/lib/python3.7/site-packages/tensorflow/python/framework/op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version. Instructions for updating: Colocations handled automatically by placer. /root/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:8: UserWarning: Update your Conv2D call to the Keras 2 API: Conv2D(32, (3, 3), activation="relu")

/root/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:9: UserWarning: Update your Conv2D call to the Keras 2 API: Conv2D(32, (3, 3), activation="relu") if name == 'main': /root/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:17: UserWarning: Update your GRU call to the Keras 2 API: GRU(128, return_sequences=True, name="gru1", kernel_initializer="he_normal") /root/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:18: UserWarning: Update your GRU call to the Keras 2 API: GRU(128, return_sequences=True, go_backwards=True, name="gru1_b", kernel_initializer="he_normal")

TypeError Traceback (most recent call last)

in 17 gru_1 = GRU(rnn_size, return_sequences=True, init='he_normal', name='gru1')(x) 18 gru_1b = GRU(rnn_size, return_sequences=True, go_backwards=True, init='he_normal', name='gru1_b')(x) ---> 19 gru1_merged = merge([gru_1, gru_1b], mode='sum') 20 21 gru_2 = GRU(rnn_size, return_sequences=True, init='he_normal', name='gru2')(gru1_merged) TypeError: 'module' object is not callable
ypwhs commented 5 years ago

你可以使用最新的API:https://keras.io/layers/wrappers/#bidirectional

比如:Bidirectional(GRU(128), merge_mode='sum')

ypwhs commented 5 years ago

2019 更新的代码已经使用了新版 API:

input_tensor = Input((height, width, 3))
x = input_tensor
for i, n_cnn in enumerate([2, 2, 2, 2, 2]):
    for j in range(n_cnn):
        x = Conv2D(32*2**min(i, 3), kernel_size=3, padding='same', kernel_initializer='he_uniform')(x)
        x = BatchNormalization()(x)
        x = Activation('relu')(x)
    x = MaxPooling2D(2 if i < 3 else (2, 1))(x)

x = Permute((2, 1, 3))(x)
x = TimeDistributed(Flatten())(x)

rnn_size = 128
x = Bidirectional(CuDNNGRU(rnn_size, return_sequences=True))(x)
x = Bidirectional(CuDNNGRU(rnn_size, return_sequences=True))(x)
x = Dense(n_class, activation='softmax')(x)

base_model = Model(inputs=input_tensor, outputs=x)

首先模型输入一个 (height, width, 3) 维度的图片,然后经过一系列的层降维到了 (2, 16, 256),之后我们使用 Permute 把 width 轴调整到第一个维度以适配 RNN 的输入格式。调整以后的维度是 (16, 2, 256),然后使用 TimeDistributed(Flatten()) 把后两个维度压成一维,也就是 (16, 512),之后经过 2 层双向的 GRU 对序列横向建模,最后经过 Dense 分类器输出水平方向上每个字符的概率分布 (16, 37)