test_recognize_digits_conv.py 2.5 KB
Newer Older
F
fengjiayi 已提交
1
import paddle.v2 as paddle
Q
Qiao Longfei 已提交
2 3 4 5
import paddle.v2.fluid.layers as layers
import paddle.v2.fluid.nets as nets
import paddle.v2.fluid.core as core
import paddle.v2.fluid.optimizer as optimizer
D
Dong Zhihong 已提交
6
import paddle.v2.fluid.evaluator as evaluator
H
Helin Wang 已提交
7
import paddle.v2.fluid.framework as framework
Q
Qiao Longfei 已提交
8
from paddle.v2.fluid.executor import Executor
F
fengjiayi 已提交
9 10 11 12 13 14

import numpy as np

images = layers.data(
    name='pixel',
    shape=[1, 28, 28],
15
    data_type='float32')
F
fengjiayi 已提交
16 17 18
label = layers.data(
    name='label',
    shape=[1],
19
    data_type='int64')
F
fengjiayi 已提交
20 21 22 23 24 25
conv_pool_1 = nets.simple_img_conv_pool(
    input=images,
    filter_size=5,
    num_filters=20,
    pool_size=2,
    pool_stride=2,
26
    act="relu")
F
fengjiayi 已提交
27 28 29 30 31 32
conv_pool_2 = nets.simple_img_conv_pool(
    input=conv_pool_1,
    filter_size=5,
    num_filters=50,
    pool_size=2,
    pool_stride=2,
33
    act="relu")
F
fengjiayi 已提交
34 35 36

predict = layers.fc(input=conv_pool_2,
                    size=10,
37
                    act="softmax")
H
helinwang 已提交
38
cost = layers.cross_entropy(input=predict, label=label)
39
avg_cost = layers.mean(x=cost)
Q
Qiao Longfei 已提交
40
optimizer = optimizer.AdamOptimizer(learning_rate=0.01, beta1=0.9, beta2=0.999)
41
opts = optimizer.minimize(avg_cost)
F
fengjiayi 已提交
42

D
Dong Zhihong 已提交
43
accuracy, acc_out = evaluator.accuracy(
D
Dong Zhihong 已提交
44
    input=predict,
H
helinwang 已提交
45
    label=label)
D
Dong Zhihong 已提交
46

F
fengjiayi 已提交
47
BATCH_SIZE = 50
F
fengjiayi 已提交
48
PASS_NUM = 3
F
fengjiayi 已提交
49 50 51 52 53 54 55 56
train_reader = paddle.batch(
    paddle.reader.shuffle(
        paddle.dataset.mnist.train(), buf_size=500),
    batch_size=BATCH_SIZE)

place = core.CPUPlace()
exe = Executor(place)

57
exe.run(framework.default_startup_program())
F
fengjiayi 已提交
58 59 60

for pass_id in range(PASS_NUM):
    count = 0
D
Dong Zhihong 已提交
61
    accuracy.reset(exe)
F
fengjiayi 已提交
62 63 64
    for data in train_reader():
        img_data = np.array(map(lambda x: x[0].reshape([1, 28, 28]),
                                data)).astype("float32")
65
        y_data = np.array(map(lambda x: x[1], data)).astype("int64")
F
fengjiayi 已提交
66 67 68 69 70 71 72
        y_data = y_data.reshape([BATCH_SIZE, 1])

        tensor_img = core.LoDTensor()
        tensor_y = core.LoDTensor()
        tensor_img.set(img_data, place)
        tensor_y.set(y_data, place)

73
        outs = exe.run(framework.default_main_program(),
F
fengjiayi 已提交
74 75
                       feed={"pixel": tensor_img,
                             "label": tensor_y},
D
Dong Zhihong 已提交
76
                       fetch_list=[avg_cost, acc_out])
F
fengjiayi 已提交
77
        loss = np.array(outs[0])
F
fengjiayi 已提交
78
        acc = np.array(outs[1])
D
Dong Zhihong 已提交
79 80 81
        pass_acc = accuracy.eval(exe)
        print "pass id : ", pass_id, pass_acc
        # print loss, acc
F
fengjiayi 已提交
82 83 84
        if loss < 10.0 and acc > 0.9:
            # if avg cost less than 10.0 and accuracy is larger than 0.9, we think our code is good.
            exit(0)
D
Dong Zhihong 已提交
85 86 87

    pass_acc = accuracy.eval(exe)
    print "pass id : ", pass_id, pass_acc
F
fengjiayi 已提交
88 89

exit(1)