# 百度魅族深度学习大赛初赛冠军作品(图像识别.源码)

3个运算数：3个0到9的整型数字； 2个运算符：可以是+、-、*，分别代表加法、减法、乘法 0或1对括号：括号可能是0对或者1对

```(3-7)+5 1
5-6+2 1
(6+7)*2 26
(4+2)+7 13
(6*4)*4 96```

## 使用 captcha 进行数据增强

### 生成器

```import stringimport random

digits = string.digits
operators = '+-*'characters = digits + operators + '() '

def generate():
seq = ''
k = random.randint(0, 2)
if k == 1:
seq += '('
seq += random.choice(digits)
seq += random.choice(operators)    if k == 2:
seq += '('
seq += random.choice(digits)    if k == 1:
seq += ')'
seq += random.choice(operators)
seq += random.choice(digits)    if k == 2:
seq += ')'

return seq```

```import random
def generate():
ts = [u'{}{}{}{}{}', '({}{}{}){}{}', '{}{}({}{}{})']
ds = u'0123456789'
os = u'+-*'
cs = [random.choice(ds)
if x%2 == 0
else
random.choice(os)
for x in range(5)]
return random.choice(ts).format(*cs)```

```# line 191-194if c != '-':
im = im.resize((w2, h2))
im = im.transform((w, h), Image.QUAD, data)```

```import stringimport os

digits = string.digits
operators = '+-*'characters = digits + operators + '() '

width, height, n_len, n_class = 180, 60, 7, len(characters) + 1

generator = ImageCaptcha(width=width, height=height,
font_sizes=range(35, 56),
fonts=['fonts/%s'%x for x in os.listdir('fonts')
if '.tt' in x]
)
generator.generate_image('(1-2)-3')```

## 模型结构

```from keras.layers import *from keras.models
import *from make_parallel import make_parallel
rnn_size = 128input_tensor = Input((width, height, 3))
x = input_tensorfor i in range(3):
x = Conv2D(32*2**i, (3, 3),
kernel_initializer='he_normal')(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Conv2D(32*2**i, (3, 3),
kernel_initializer='he_normal')(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = MaxPooling2D(pool_size=(2, 2))(x)

conv_shape = x.get_shape()
x = Reshape(target_shape=(int(conv_shape[1]),
int(conv_shape[2]*conv_shape[3])))(x)

x = Dense(128, kernel_initializer='he_normal')(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)

gru_1 = GRU(rnn_size, return_sequences=True,
kernel_initializer='he_normal', name='gru1')(x)
gru_1b = GRU(rnn_size, return_sequences=True,
go_backwards=True, kernel_initializer='he_normal',
name='gru1_b')(x)
gru1_merged = add([gru_1, gru_1b])

gru_2 = GRU(rnn_size, return_sequences=True,
kernel_initializer='he_normal', name='gru2')(gru1_merged)
gru_2b = GRU(rnn_size, return_sequences=True,
go_backwards=True, kernel_initializer='he_normal',
name='gru2_b')(gru1_merged)
x = concatenate([gru_2, gru_2b])
x = Dropout(0.25)(x)
x = Dense(n_class, kernel_initializer='he_normal',
activation='softmax')(x)
base_model = Model(input=input_tensor, output=x)

base_model2 = make_parallel(base_model, 4)

labels = Input(name='the_labels', shape=[n_len], dtype='float32')
input_length = Input(name='input_length', shape=(1,), dtype='int64')
label_length = Input(name='label_length', shape=(1,), dtype='int64')
loss_out = Lambda(ctc_lambda_func, name='ctc')([base_model2.output, labels, input_length, label_length])

model = Model(inputs=(input_tensor, labels, input_length,
label_length), outputs=loss_out)
model.compile(loss={'ctc': lambda y_true, y_pred: y_pred},

BN 层主要是为了训练加速，实验结果非常好，模型收敛快了很多。

base_model 的可视化：

model 的可视化：

## 总结

### 官方扩充测试集的难点

```IMAGE_DIR = 'image_contest_level_1_validate'index = 117422img = cv2.imread('%s/%d.png' % (IMAGE_DIR, index))
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
h = cv2.equalizeHist(gray)```

822 篇文章233 人订阅

0 条评论

## 相关文章

12500

19120

### 一文教你如何用神经网络识别验证码！

AI 研习社按：本文作者 Slyne_D，原载于作者个人博客，雷锋网 AI 研习社已获授权。文中相关链接详见文末“阅读原文”。 这是去年博主心血来潮实现的一个小...

31030

35680

1.6K40

14240

28020

16220

27550

54870