验证码识别的准确度低

时间:2017-05-13 15:14:12

标签: python machine-learning tensorflow keras captcha

我使用以下方法生成最多4位数的验证码:

def genData(n=30000, max_digs=4, width=150):
    capgen = ImageCaptcha()
    data = []
    target = []
    for i in range(n):
        x = np.random.randint(0, 10 ** max_digs)
        img = misc.imread(capgen.generate(str(x)))
        img = np.mean(img, axis=2)[:, :width]
        data.append(img.flatten())
        target.append(x)
    return np.array(data), np.array(target)

然后我正在处理如下的数据

train_data, train_target = genData()
test_data, test_target = genData(1000)

train_data = train_data.reshape(train_data.shape[0], 1, 150, 60)
test_data = test_data.reshape(test_data.shape[0], 1, 150, 60)
train_data = train_data.astype('float32')
test_data = test_data.astype('float32')
train_data /= 255
test_data /= 255

我的模型结构如下:

def get_model():
    # create model
    model = Sequential()
    model.add(Conv2D(30, (5, 5), input_shape=(1, 150, 60), activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Conv2D(15, (3, 3), activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.2))
    model.add(Flatten())
    model.add(Dense(128, activation='relu'))
    model.add(Dense(50, activation='relu'))
    model.add(Dense(10 ** 4, activation='softmax'))
    # Compile model
    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
    return model

然后我正在训练模型

model = get_model()
# Fit the model
model.fit(train_data, train_target, validation_data=(test_data, test_target), epochs=10, batch_size=200)
# Final evaluation of the model
scores = model.evaluate(test_data, test_target, verbose=0)
print("Large CNN Error: %.2f%%" % (100 - scores[1] * 100))

我不知道哪个部分我做错了,但我的准确度甚至达不到%1。

1 个答案:

答案 0 :(得分:2)

你有10000(!)个班级。你训练多久了?你每节课有多少训练数据?

你的方法几乎肯定是问题所在。虽然你可以解决问题"蛮力"像这样,这是一个非常糟糕的方式。您应首先尝试检测单个数字,然后使用10级分类器对每个数字进行分类。