test_recognize_digits_conv.py 2.5 KB
Newer Older
D
dzhwinter 已提交
1
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
D
dzhwinter 已提交
2
#
D
dzhwinter 已提交
3 4 5
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
D
dzhwinter 已提交
6
#
D
dzhwinter 已提交
7
#     http://www.apache.org/licenses/LICENSE-2.0
D
dzhwinter 已提交
8
#
D
dzhwinter 已提交
9 10 11 12 13 14
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

15
from __future__ import print_function
Q
Qiao Longfei 已提交
16
import numpy as np
F
fengjiayi 已提交
17
import paddle.v2 as paddle
18
import paddle.v2.fluid as fluid
F
fengjiayi 已提交
19

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

37 38 39 40 41
predict = fluid.layers.fc(input=conv_pool_2, size=10, act="softmax")
cost = fluid.layers.cross_entropy(input=predict, label=label)
avg_cost = fluid.layers.mean(x=cost)
optimizer = fluid.optimizer.Adam(learning_rate=0.01)
optimizer.minimize(avg_cost)
F
fengjiayi 已提交
42

43
accuracy = fluid.evaluator.Accuracy(input=predict, label=label)
D
Dong Zhihong 已提交
44

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

52 53
place = fluid.CPUPlace()
exe = fluid.Executor(place)
Y
Yu Yang 已提交
54
feeder = fluid.DataFeeder(feed_list=[images, label], place=place)
55
exe.run(fluid.default_startup_program())
F
fengjiayi 已提交
56 57

for pass_id in range(PASS_NUM):
D
Dong Zhihong 已提交
58
    accuracy.reset(exe)
F
fengjiayi 已提交
59
    for data in train_reader():
60
        loss, acc = exe.run(fluid.default_main_program(),
Y
Yu Yang 已提交
61
                            feed=feeder.feed(data),
62
                            fetch_list=[avg_cost] + accuracy.metrics)
D
Dong Zhihong 已提交
63
        pass_acc = accuracy.eval(exe)
F
fengjiayi 已提交
64 65
        print("pass_id=" + str(pass_id) + " acc=" + str(acc) + " pass_acc=" +
              str(pass_acc))
D
Dong Zhihong 已提交
66
        # print loss, acc
F
fengjiayi 已提交
67
        if loss < 10.0 and pass_acc > 0.9:
F
fengjiayi 已提交
68 69
            # 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 已提交
70 71

    pass_acc = accuracy.eval(exe)
F
fengjiayi 已提交
72
    print("pass_id=" + str(pass_id) + " pass_acc=" + str(pass_acc))
F
fengjiayi 已提交
73 74

exit(1)