crnn_ctc_model.py 7.0 KB
Newer Older
W
wanghaoshuang 已提交
1
import paddle.fluid as fluid
W
wanghaoshuang 已提交
2 3 4 5 6 7 8 9


def conv_bn_pool(input,
                 group,
                 out_ch,
                 act="relu",
                 param=None,
                 bias=None,
W
wanghaoshuang 已提交
10 11
                 param_0=None,
                 is_test=False):
W
wanghaoshuang 已提交
12 13 14 15 16 17 18 19 20 21
    tmp = input
    for i in xrange(group):
        tmp = fluid.layers.conv2d(
            input=tmp,
            num_filters=out_ch[i],
            filter_size=3,
            padding=1,
            param_attr=param if param_0 is None else param_0,
            act=None,  # LinearActivation
            use_cudnn=True)
W
wanghaoshuang 已提交
22
        tmp = fluid.layers.batch_norm(
W
wanghaoshuang 已提交
23 24 25 26 27
            input=tmp,
            act=act,
            param_attr=param,
            bias_attr=bias,
            is_test=is_test)
W
wanghaoshuang 已提交
28
    tmp = fluid.layers.pool2d(
W
wanghaoshuang 已提交
29 30 31 32 33 34
        input=tmp,
        pool_size=2,
        pool_type='max',
        pool_stride=2,
        use_cudnn=True,
        ceil_mode=True)
W
wanghaoshuang 已提交
35 36 37 38

    return tmp


W
wanghaoshuang 已提交
39 40 41 42 43 44
def ocr_convs(input,
              num,
              with_bn,
              regularizer=None,
              gradient_clip=None,
              is_test=False):
W
wanghaoshuang 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
    assert (num % 4 == 0)

    b = fluid.ParamAttr(
        regularizer=regularizer,
        gradient_clip=gradient_clip,
        initializer=fluid.initializer.Normal(0.0, 0.0))
    w0 = fluid.ParamAttr(
        regularizer=regularizer,
        gradient_clip=gradient_clip,
        initializer=fluid.initializer.Normal(0.0, 0.0005))
    w1 = fluid.ParamAttr(
        regularizer=regularizer,
        gradient_clip=gradient_clip,
        initializer=fluid.initializer.Normal(0.0, 0.01))
    tmp = input
W
wanghaoshuang 已提交
60 61
    tmp = conv_bn_pool(
        tmp, 2, [16, 16], param=w1, bias=b, param_0=w0, is_test=is_test)
62

W
wanghaoshuang 已提交
63 64 65
    tmp = conv_bn_pool(tmp, 2, [32, 32], param=w1, bias=b, is_test=is_test)
    tmp = conv_bn_pool(tmp, 2, [64, 64], param=w1, bias=b, is_test=is_test)
    tmp = conv_bn_pool(tmp, 2, [128, 128], param=w1, bias=b, is_test=is_test)
W
wanghaoshuang 已提交
66 67 68 69 70 71 72
    return tmp


def encoder_net(images,
                num_classes,
                rnn_hidden_size=200,
                regularizer=None,
W
wanghaoshuang 已提交
73 74
                gradient_clip=None,
                is_test=False):
W
wanghaoshuang 已提交
75
    conv_features = ocr_convs(
W
wanghaoshuang 已提交
76 77 78 79 80 81
        images,
        8,
        True,
        regularizer=regularizer,
        gradient_clip=gradient_clip,
        is_test=is_test)
W
wanghaoshuang 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
    sliced_feature = fluid.layers.im2sequence(
        input=conv_features,
        stride=[1, 1],
        filter_size=[conv_features.shape[2], 1])

    para_attr = fluid.ParamAttr(
        regularizer=regularizer,
        gradient_clip=gradient_clip,
        initializer=fluid.initializer.Normal(0.0, 0.02))
    bias_attr = fluid.ParamAttr(
        regularizer=regularizer,
        gradient_clip=gradient_clip,
        initializer=fluid.initializer.Normal(0.0, 0.02),
        learning_rate=2.0)
    bias_attr_nobias = fluid.ParamAttr(
        regularizer=regularizer,
        gradient_clip=gradient_clip,
        initializer=fluid.initializer.Normal(0.0, 0.02))

    fc_1 = fluid.layers.fc(input=sliced_feature,
                           size=rnn_hidden_size * 3,
                           param_attr=para_attr,
                           bias_attr=bias_attr_nobias)
    fc_2 = fluid.layers.fc(input=sliced_feature,
                           size=rnn_hidden_size * 3,
                           param_attr=para_attr,
                           bias_attr=bias_attr_nobias)

    gru_forward = fluid.layers.dynamic_gru(
        input=fc_1,
        size=rnn_hidden_size,
        param_attr=para_attr,
        bias_attr=bias_attr,
        candidate_activation='relu')
    gru_backward = fluid.layers.dynamic_gru(
        input=fc_2,
        size=rnn_hidden_size,
        is_reverse=True,
        param_attr=para_attr,
        bias_attr=bias_attr,
        candidate_activation='relu')

    w_attr = fluid.ParamAttr(
        regularizer=regularizer,
        gradient_clip=gradient_clip,
        initializer=fluid.initializer.Normal(0.0, 0.02))
    b_attr = fluid.ParamAttr(
        regularizer=regularizer,
        gradient_clip=gradient_clip,
        initializer=fluid.initializer.Normal(0.0, 0.0))

    fc_out = fluid.layers.fc(input=[gru_forward, gru_backward],
                             size=num_classes + 1,
                             param_attr=w_attr,
                             bias_attr=b_attr)
W
wanghaoshuang 已提交
137

W
wanghaoshuang 已提交
138 139 140 141 142 143
    return fc_out


def ctc_train_net(images, label, args, num_classes):
    regularizer = fluid.regularizer.L2Decay(args.l2)
    gradient_clip = None
W
wanghaoshuang 已提交
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
    if args.parallel:
        places = fluid.layers.get_places()
        pd = fluid.layers.ParallelDo(places)
        with pd.do():
            images_ = pd.read_input(images)
            label_ = pd.read_input(label)

            fc_out = encoder_net(
                images_,
                num_classes,
                regularizer=regularizer,
                gradient_clip=gradient_clip)

            cost = fluid.layers.warpctc(
                input=fc_out,
                label=label_,
                blank=num_classes,
                norm_by_times=True)
            sum_cost = fluid.layers.reduce_sum(cost)

            decoded_out = fluid.layers.ctc_greedy_decoder(
                input=fc_out, blank=num_classes)

            pd.write_output(sum_cost)
            pd.write_output(decoded_out)

        sum_cost, decoded_out = pd()
        sum_cost = fluid.layers.reduce_sum(sum_cost)

    else:
        fc_out = encoder_net(
            images,
            num_classes,
            regularizer=regularizer,
            gradient_clip=gradient_clip)

        cost = fluid.layers.warpctc(
            input=fc_out, label=label, blank=num_classes, norm_by_times=True)
        sum_cost = fluid.layers.reduce_sum(cost)
        decoded_out = fluid.layers.ctc_greedy_decoder(
            input=fc_out, blank=num_classes)
W
wanghaoshuang 已提交
185

W
wanghaoshuang 已提交
186 187 188 189
    casted_label = fluid.layers.cast(x=label, dtype='int64')
    error_evaluator = fluid.evaluator.EditDistance(
        input=decoded_out, label=casted_label)

W
wanghaoshuang 已提交
190
    inference_program = fluid.default_main_program().clone(for_test=True)
W
wanghaoshuang 已提交
191 192 193

    optimizer = fluid.optimizer.Momentum(
        learning_rate=args.learning_rate, momentum=args.momentum)
W
wanghaoshuang 已提交
194
    _, params_grads = optimizer.minimize(sum_cost)
W
wanghaoshuang 已提交
195 196
    model_average = fluid.optimizer.ModelAverage(
        args.average_window,
197
        params_grads,
W
wanghaoshuang 已提交
198 199 200
        min_average_window=args.min_average_window,
        max_average_window=args.max_average_window)

201
    return sum_cost, error_evaluator, inference_program, model_average
W
wanghaoshuang 已提交
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218


def ctc_infer(images, num_classes):
    fc_out = encoder_net(images, num_classes, is_test=True)
    return fluid.layers.ctc_greedy_decoder(input=fc_out, blank=num_classes)


def ctc_eval(images, label, num_classes):
    fc_out = encoder_net(images, num_classes, is_test=True)
    decoded_out = fluid.layers.ctc_greedy_decoder(
        input=fc_out, blank=num_classes)

    casted_label = fluid.layers.cast(x=label, dtype='int64')
    error_evaluator = fluid.evaluator.EditDistance(
        input=decoded_out, label=casted_label)

    cost = fluid.layers.warpctc(
219
        input=fc_out, label=label, blank=num_classes, norm_by_times=True)
W
wanghaoshuang 已提交
220 221

    return error_evaluator, cost