Skip to content

Commit 177324b

Browse files
authored
[Test-driven] Recognize Digit: update mnist test cases with the new API syntax. (#10507)
* Update the mnist test cases with the new API syntax. * Turn on the tests for MNIST * delete the test files got merged accidently. * Enable the mnist tests. ready for test driven development. * Comment out the infer first This is to confirm that the Trainer.train is working * Add CMake file to include the tests * Make the train program only return avg_cost for now * Update the tests to use the latest syntax
1 parent 236dc7b commit 177324b

File tree

5 files changed

+111
-60
lines changed

5 files changed

+111
-60
lines changed

python/paddle/fluid/tests/book/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ string(REPLACE ".py" "" TEST_OPS "${TEST_OPS}")
55
foreach(src ${TEST_OPS})
66
py_test(${src} SRCS ${src}.py)
77
endforeach()
8+
9+
add_subdirectory(high-level-api)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
file(GLOB TEST_OPS RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "test_*.py")
2+
string(REPLACE ".py" "" TEST_OPS "${TEST_OPS}")
3+
4+
# default test
5+
foreach(src ${TEST_OPS})
6+
py_test(${src} SRCS ${src}.py)
7+
endforeach()
8+
9+
add_subdirectory(recognize_digits)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
file(GLOB TEST_OPS RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "test_*.py")
2+
string(REPLACE ".py" "" TEST_OPS "${TEST_OPS}")
3+
4+
# default test
5+
foreach(src ${TEST_OPS})
6+
py_test(${src} SRCS ${src}.py)
7+
endforeach()

python/paddle/fluid/tests/book/high-level-api/recognize_digits/notest_recognize_digits_conv.py renamed to python/paddle/fluid/tests/book/high-level-api/recognize_digits/test_recognize_digits_conv.py

Lines changed: 47 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import math
2222
import sys
2323
import os
24-
import paddle.v2.dataset as dataset
2524

2625
BATCH_SIZE = 64
2726

@@ -54,47 +53,65 @@ def train_program():
5453
predict = inference_program()
5554
cost = fluid.layers.cross_entropy(input=predict, label=label)
5655
avg_cost = fluid.layers.mean(cost)
57-
acc = fluid.layers.accuracy(input=predict, label=label)
58-
return avg_cost, acc
56+
# acc = fluid.layers.accuracy(input=predict, label=label)
57+
# return avg_cost, acc
58+
return avg_cost
5959

6060

6161
def train(use_cuda, save_dirname):
6262
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
63-
6463
optimizer = fluid.optimizer.Adam(learning_rate=0.001)
65-
trainer = fluid.Trainer(train_program, place=place, optimizer=optimizer)
64+
65+
trainer = fluid.Trainer(
66+
train_func=train_program,
67+
infer_func=inference_program,
68+
place=place,
69+
optimizer=optimizer)
6670

6771
def event_handler(event):
68-
if isinstance(event, fluid.EndIteration):
69-
avg_cost, acc = event.values
70-
print("avg_cost: %s" % avg_cost)
71-
print("acc : %s" % acc)
72-
73-
if (event.batch_id + 1) % 10 == 0:
74-
test_metrics = trainer.test(reader=dataset.mnist.test())
75-
avg_cost_set = test_metrics[0]
76-
acc_set = test_metrics[1]
77-
78-
# get test acc and loss
79-
acc = numpy.array(acc_set).mean()
80-
avg_cost = numpy.array(avg_cost_set).mean()
81-
if float(acc) > 0.2: # Smaller value to increase CI speed
82-
trainer.save_params(save_dirname)
83-
else:
84-
print('BatchID {0}, Test Loss {1:0.2}, Acc {2:0.2}'.format(
85-
event.batch_id + 1, float(avg_cost), float(acc)))
86-
if math.isnan(float(avg_cost)):
87-
sys.exit("got NaN loss, training failed.")
72+
if isinstance(event, fluid.EndEpochEvent):
73+
# if (event.epoch + 1) % 10 == 0:
74+
# trainer.save_params(save_dirname)
75+
trainer.save_inference_model(save_dirname)
76+
77+
# TODO: Uncomment this part once we are sure that .train is working
78+
# test_reader = paddle.batch(
79+
# paddle.dataset.mnist.test(), batch_size=BATCH_SIZE)
80+
# test_metrics = trainer.test(reader=test_reader)
81+
# avg_cost_set = test_metrics[0]
82+
# acc_set = test_metrics[1]
83+
#
84+
# # get test acc and loss
85+
# acc = numpy.array(acc_set).mean()
86+
# avg_cost = numpy.array(avg_cost_set).mean()
87+
#
88+
# print("avg_cost: %s" % avg_cost)
89+
# print("acc : %s" % acc)
90+
#
91+
# if float(acc) > 0.2: # Smaller value to increase CI speed
92+
# trainer.save_params(save_dirname)
93+
# else:
94+
# print('BatchID {0}, Test Loss {1:0.2}, Acc {2:0.2}'.format(
95+
# event.epoch + 1, float(avg_cost), float(acc)))
96+
# if math.isnan(float(avg_cost)):
97+
# sys.exit("got NaN loss, training failed.")
98+
99+
train_reader = paddle.batch(
100+
paddle.reader.shuffle(
101+
paddle.dataset.mnist.train(), buf_size=500),
102+
batch_size=BATCH_SIZE)
88103

89104
trainer.train(
90-
reader=dataset.mnist.train(), num_pass=100, event_handler=event_handler)
105+
num_epochs=1,
106+
event_handler=event_handler,
107+
reader=train_reader,
108+
feed_order=['img', 'label'])
91109

92110

93111
def infer(use_cuda, save_dirname=None):
94112
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
95113

96-
inferencer = fluid.Inferencer(
97-
inference_program, param_path=save_dirname, place=place)
114+
inferencer = fluid.Inferencer(param_path=save_dirname, place=place)
98115

99116
batch_size = 1
100117
tensor_img = numpy.random.uniform(-1.0, 1.0,
@@ -114,5 +131,5 @@ def main(use_cuda):
114131

115132

116133
if __name__ == '__main__':
117-
for use_cuda in (False, True):
118-
main(use_cuda=use_cuda)
134+
# for use_cuda in (False, True):
135+
main(use_cuda=False)

python/paddle/fluid/tests/book/high-level-api/recognize_digits/notest_recognize_digits_mlp.py renamed to python/paddle/fluid/tests/book/high-level-api/recognize_digits/test_recognize_digits_mlp.py

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import math
2222
import sys
2323
import os
24-
import paddle.v2.dataset as dataset
2524

2625
BATCH_SIZE = 64
2726

@@ -41,47 +40,64 @@ def train_program():
4140
predict = inference_program()
4241
cost = fluid.layers.cross_entropy(input=predict, label=label)
4342
avg_cost = fluid.layers.mean(cost)
44-
acc = fluid.layers.accuracy(input=predict, label=label)
45-
return avg_cost, acc
43+
# acc = fluid.layers.accuracy(input=predict, label=label)
44+
# return avg_cost, acc
45+
return avg_cost
4646

4747

4848
def train(use_cuda, save_dirname):
4949
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
50-
5150
optimizer = fluid.optimizer.Adam(learning_rate=0.001)
52-
trainer = fluid.Trainer(train_program, place=place, optimizer=optimizer)
51+
52+
trainer = fluid.Trainer(
53+
train_func=train_program,
54+
infer_func=inference_program,
55+
place=place,
56+
optimizer=optimizer)
5357

5458
def event_handler(event):
55-
if isinstance(event, fluid.EndIteration):
56-
avg_cost, acc = event.values
57-
print("avg_cost: %s" % avg_cost)
58-
print("acc : %s" % acc)
59-
60-
if (event.batch_id + 1) % 10 == 0:
61-
test_metrics = trainer.test(reader=dataset.mnist.test())
62-
avg_cost_set = test_metrics[0]
63-
acc_set = test_metrics[1]
64-
65-
# get test acc and loss
66-
acc = numpy.array(acc_set).mean()
67-
avg_cost = numpy.array(avg_cost_set).mean()
68-
if float(acc) > 0.2: # Smaller value to increase CI speed
69-
trainer.save_params(save_dirname)
70-
else:
71-
print('BatchID {0}, Test Loss {1:0.2}, Acc {2:0.2}'.format(
72-
event.batch_id + 1, float(avg_cost), float(acc)))
73-
if math.isnan(float(avg_cost)):
74-
sys.exit("got NaN loss, training failed.")
59+
if isinstance(event, fluid.EndEpochEvent):
60+
# if (event.epoch + 1) % 10 == 0:
61+
trainer.save_inference_model(save_dirname)
62+
63+
# TODO: Uncomment this part once we are sure that .train is working
64+
# test_reader = paddle.batch(
65+
# paddle.dataset.mnist.test(), batch_size=BATCH_SIZE)
66+
# test_metrics = trainer.test(reader=test_reader)
67+
# avg_cost_set = test_metrics[0]
68+
# acc_set = test_metrics[1]
69+
#
70+
# # get test acc and loss
71+
# acc = numpy.array(acc_set).mean()
72+
# avg_cost = numpy.array(avg_cost_set).mean()
73+
#
74+
# print("avg_cost: %s" % avg_cost)
75+
# print("acc : %s" % acc)
76+
#
77+
# if float(acc) > 0.2: # Smaller value to increase CI speed
78+
# trainer.save_params(save_dirname)
79+
# else:
80+
# print('BatchID {0}, Test Loss {1:0.2}, Acc {2:0.2}'.format(
81+
# event.epoch + 1, float(avg_cost), float(acc)))
82+
# if math.isnan(float(avg_cost)):
83+
# sys.exit("got NaN loss, training failed.")
84+
85+
train_reader = paddle.batch(
86+
paddle.reader.shuffle(
87+
paddle.dataset.mnist.train(), buf_size=500),
88+
batch_size=BATCH_SIZE)
7589

7690
trainer.train(
77-
reader=dataset.mnist.train(), num_pass=100, event_handler=event_handler)
91+
num_epochs=1,
92+
event_handler=event_handler,
93+
reader=train_reader,
94+
feed_order=['img', 'label'])
7895

7996

8097
def infer(use_cuda, save_dirname=None):
8198
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
8299

83-
inferencer = fluid.Inferencer(
84-
inference_program, param_path=save_dirname, place=place)
100+
inferencer = fluid.Inferencer(param_path=save_dirname, place=place)
85101

86102
batch_size = 1
87103
tensor_img = numpy.random.uniform(-1.0, 1.0,
@@ -101,5 +117,5 @@ def main(use_cuda):
101117

102118

103119
if __name__ == '__main__':
104-
for use_cuda in (False, True):
105-
main(use_cuda=use_cuda)
120+
# for use_cuda in (False, True):
121+
main(use_cuda=False)

0 commit comments

Comments
 (0)