按照数字识别教程跑代码,准确率高达98%,但实际检验效果特别差是怎么回事?
Created by: hacksman
代码如下所示:
from PIL import Image
import numpy as np
import paddle.v2 as paddle
# 设置是否用gpu,0为否,1为是
with_gpu = os.getenv('WITH_GPU', '0') != '0'
# 定义网络结构
def convolutional_neural_network_org(img):
# 第一层卷积层
conv_pool_1 = paddle.networks.simple_img_conv_pool(
input=img,
filter_size=5,
num_filters=20,
num_channel=1,
pool_size=2,
pool_stride=2,
act=paddle.activation.Relu())
# 第二层卷积层
conv_pool_2 = paddle.networks.simple_img_conv_pool(
input=conv_pool_1,
filter_size=5,
num_filters=50,
num_channel=20,
pool_size=2,
pool_stride=2,
act=paddle.activation.Relu())
# 全连接层
predict = paddle.layer.fc(
input=conv_pool_2, size=10, act=paddle.activation.Softmax())
return predict
def main():
# 初始化定义跑模型的设备
paddle.init(use_gpu=with_gpu, trainer_count=1)
# 读取数据
images = paddle.layer.data(
name='pixel', type=paddle.data_type.dense_vector(784))
label = paddle.layer.data(
name='label', type=paddle.data_type.integer_value(10))
# 调用之前定义的网络结构
predict = convolutional_neural_network_org(images)
# 定义损失函数
cost = paddle.layer.classification_cost(input=predict, label=label)
# 指定训练相关的参数
parameters = paddle.parameters.create(cost)
# 定义训练方法
optimizer = paddle.optimizer.Momentum(
learning_rate=0.1 / 128.0,
momentum=0.9,
regularization=paddle.optimizer.L2Regularization(rate=0.0005 * 128))
# 训练模型
trainer = paddle.trainer.SGD(
cost=cost, parameters=parameters, update_equation=optimizer)
lists = []
# 定义event_handler,输出训练过程中的结果
def event_handler(event):
if isinstance(event, paddle.event.EndIteration):
if event.batch_id % 100 == 0:
print "Pass %d, Batch %d, Cost %f, %s" % (
event.pass_id, event.batch_id, event.cost, event.metrics)
if isinstance(event, paddle.event.EndPass):
# 保存参数
with open('params_pass_%d.tar' % event.pass_id, 'w') as f:
parameters.to_tar(f)
result = trainer.test(reader=paddle.batch(
paddle.dataset.mnist.test(), batch_size=128))
print "Test with Pass %d, Cost %f, %s\n" % (
event.pass_id, result.cost, result.metrics)
lists.append((event.pass_id, result.cost,
result.metrics['classification_error_evaluator']))
trainer.train(
reader=paddle.batch(
paddle.reader.shuffle(paddle.dataset.mnist.train(), buf_size=8192),
batch_size=128),
event_handler=event_handler,
num_passes=10)
# 找到训练误差最小的一次结果
best = sorted(lists, key=lambda list: float(list[1]))[0]
print 'Best pass is %s, testing Avgcost is %s' % (best[0], best[1])
print 'The classification accuracy is %.2f%%' % (100 - float(best[2]) * 100)
# 加载数据
def load_image(file):
im = Image.open(file).convert('L')
im = im.resize((28, 28), Image.ANTIALIAS)
im = np.array(im).astype(np.float32).flatten()
print im
im = im / 255.0 * 2.0 - 1.0
print '-----'
print im
return im
# 测试结果
test_data = []
cur_dir = os.path.dirname(os.path.realpath(__file__))
test_data.append((load_image(cur_dir + '/image/infer_7.png'), ))
probs = paddle.infer(
output_layer=predict, parameters=parameters, input=test_data)
lab = np.argsort(-probs) # probs and lab are the results of one batch data
print lab
print "Label of image/infer_7.png is: %d" % lab[0][0]
if __name__ == '__main__':
from datetime import datetime
start_time = datetime.now()
main()
end_time = datetime.now()
escape = end_time-start_time
print escape
返回的结果如下:
Pass 0, Batch 0, Cost 3.637740, {'classification_error_evaluator': 0.875}
Pass 0, Batch 100, Cost 0.666954, {'classification_error_evaluator': 0.234375}
Pass 0, Batch 200, Cost 0.720760, {'classification_error_evaluator': 0.2265625}
Pass 0, Batch 300, Cost 0.423178, {'classification_error_evaluator': 0.125}
Pass 0, Batch 400, Cost 0.310293, {'classification_error_evaluator': 0.0625}
Test with Pass 0, Cost 0.278447, {'classification_error_evaluator': 0.0803999975323677}
# 为避免太长,中间的部分输出省略掉
Pass 9, Batch 0, Cost 0.069227, {'classification_error_evaluator': 0.015625}
Pass 9, Batch 100, Cost 0.009176, {'classification_error_evaluator': 0.0}
Pass 9, Batch 200, Cost 0.024691, {'classification_error_evaluator': 0.0078125}
Pass 9, Batch 300, Cost 0.025310, {'classification_error_evaluator': 0.015625}
Pass 9, Batch 400, Cost 0.124656, {'classification_error_evaluator': 0.03125}
Test with Pass 9, Cost 0.057880, {'classification_error_evaluator': 0.01759999990463257}
Best pass is 8, testing Avgcost is 0.0480473264802
The classification accuracy is 98.48%
[[0 2 1 7 9 4 3 8 6 5]]
Label of image/infer_7.png is: 0
0:05:38.941172
我原来的图片是我自己PS中画的 图片尺寸是28*28,分辨率是144dpi,图像模式是:RGB color 8bit
并且我尝试了不同的图片,都识别不出来,类似这些,就是猜的貌似也比它强,我怎么感觉我写出来的是人工智障呢?(懵逼.gif),期待解答。 数字0 数字3 数字6 数字9