Skip to content

Commit 52d3d43

Browse files
committed
Update the image classification tests to match the new syntax
1 parent 771597d commit 52d3d43

File tree

4 files changed

+40
-27
lines changed

4 files changed

+40
-27
lines changed

python/paddle/fluid/tests/book/high-level-api/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ foreach(src ${TEST_OPS})
66
py_test(${src} SRCS ${src}.py)
77
endforeach()
88

9+
add_subdirectory(image_classification)
910
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/image_classification/test_image_classification_resnet.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -99,23 +99,23 @@ def train(use_cuda, save_path):
9999

100100
def event_handler(event):
101101
if isinstance(event, fluid.EndEpochEvent):
102-
if (event.epoch % 10) == 0:
103-
avg_cost, accuracy = trainer.test(reader=test_reader)
104-
105-
print('BatchID {1:04}, Loss {2:2.2}, Acc {3:2.2}'.format(
106-
event.epoch + 1, avg_cost, accuracy))
107-
108-
if accuracy > 0.01: # Low threshold for speeding up CI
109-
trainer.params.save(save_path)
110-
return
102+
trainer.save_inference_model(save_path)
103+
# if (event.batch_id % 10) == 0:
104+
# avg_cost, accuracy = trainer.test(reader=test_reader)
105+
#
106+
# print('BatchID {1:04}, Loss {2:2.2}, Acc {3:2.2}'.format(
107+
# event.batch_id + 1, avg_cost, accuracy))
108+
#
109+
# if accuracy > 0.01: # Low threshold for speeding up CI
110+
# trainer.params.save(save_path)
111+
# return
111112

112113
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
113114
trainer = fluid.Trainer(
114115
train_network,
116+
inference_network,
115117
optimizer=fluid.optimizer.Adam(learning_rate=0.001),
116-
place=place,
117-
event_handler=event_handler)
118-
118+
place=place)
119119
trainer.train(
120120
num_epochs=EPOCH_NUM,
121121
event_handler=event_handler,
@@ -125,8 +125,8 @@ def event_handler(event):
125125

126126
def infer(use_cuda, save_path):
127127
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
128-
inferencer = fluid.Inferencer(
129-
inference_network, save_path=save_path, place=place)
128+
129+
inferencer = fluid.Inferencer(param_path=save_path, place=place)
130130

131131
# The input's dimension of conv should be 4-D or 5-D.
132132
# Use normilized image pixels as input data, which should be in the range

python/paddle/fluid/tests/book/high-level-api/image_classification/test_image_classification_vgg.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -78,29 +78,34 @@ def train(use_cuda, save_path):
7878

7979
def event_handler(event):
8080
if isinstance(event, fluid.EndEpochEvent):
81-
if (event.epoch % 10) == 0:
82-
avg_cost, accuracy = trainer.test(reader=test_reader)
83-
84-
print('BatchID {1:04}, Loss {2:2.2}, Acc {3:2.2}'.format(
85-
event.epoch + 1, avg_cost, accuracy))
86-
87-
if accuracy > 0.01: # Low threshold for speeding up CI
88-
trainer.params.save(save_path)
89-
return
81+
trainer.save_inference_model(save_path)
82+
# if (event.epoch % 10) == 0:
83+
# avg_cost, accuracy = trainer.test(reader=test_reader)
84+
#
85+
# print('BatchID {1:04}, Loss {2:2.2}, Acc {3:2.2}'.format(
86+
# event.epoch + 1, avg_cost, accuracy))
87+
#
88+
# if accuracy > 0.01: # Low threshold for speeding up CI
89+
# trainer.params.save(save_path)
90+
# return
9091

9192
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
9293
trainer = fluid.Trainer(
9394
train_network,
95+
inference_network,
9496
optimizer=fluid.optimizer.Adam(learning_rate=0.001),
95-
place=place,
96-
event_handler=event_handler)
97-
trainer.train(train_reader, EPOCH_NUM, event_handler=event_handler)
97+
place=place)
98+
trainer.train(
99+
num_epochs=EPOCH_NUM,
100+
event_handler=event_handler,
101+
reader=train_reader,
102+
feed_order=['pixel', 'label'])
98103

99104

100105
def infer(use_cuda, save_path):
101106
params = fluid.Params(save_path)
102107
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
103-
inferencer = fluid.Inferencer(inference_network, params, place=place)
108+
inferencer = fluid.Inferencer(param_path=save_path, place=place)
104109

105110
# The input's dimension of conv should be 4-D or 5-D.
106111
# Use normilized image pixels as input data, which should be in the range

0 commit comments

Comments
 (0)