Skip to content

Commit dadfe55

Browse files
committed
Start testing image classification with high level api
1 parent 0fed3db commit dadfe55

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

python/paddle/fluid/tests/book/high-level-api/image_classification/notest_image_classification_resnet.py renamed to python/paddle/fluid/tests/book/high-level-api/image_classification/test_image_classification_resnet.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,9 @@ def train_network():
8080
label = fluid.layers.data(name='label', shape=[1], dtype='int64')
8181
cost = fluid.layers.cross_entropy(input=predict, label=label)
8282
avg_cost = fluid.layers.mean(cost)
83-
accuracy = fluid.layers.accuracy(input=predict, label=label)
84-
return avg_cost, accuracy
83+
# accuracy = fluid.layers.accuracy(input=predict, label=label)
84+
# return avg_cost, accuracy
85+
return avg_cost
8586

8687

8788
def train(use_cuda, save_path):
@@ -97,12 +98,12 @@ def train(use_cuda, save_path):
9798
paddle.dataset.cifar.test10(), batch_size=BATCH_SIZE)
9899

99100
def event_handler(event):
100-
if isinstance(event, fluid.EndIteration):
101-
if (event.batch_id % 10) == 0:
101+
if isinstance(event, fluid.EndEpochEvent):
102+
if (event.epoch % 10) == 0:
102103
avg_cost, accuracy = trainer.test(reader=test_reader)
103104

104105
print('BatchID {1:04}, Loss {2:2.2}, Acc {3:2.2}'.format(
105-
event.batch_id + 1, avg_cost, accuracy))
106+
event.epoch + 1, avg_cost, accuracy))
106107

107108
if accuracy > 0.01: # Low threshold for speeding up CI
108109
trainer.params.save(save_path)
@@ -114,13 +115,18 @@ def event_handler(event):
114115
optimizer=fluid.optimizer.Adam(learning_rate=0.001),
115116
place=place,
116117
event_handler=event_handler)
117-
trainer.train(train_reader, EPOCH_NUM, event_handler=event_handler)
118+
119+
trainer.train(
120+
num_epochs=EPOCH_NUM,
121+
event_handler=event_handler,
122+
reader=train_reader,
123+
feed_order=['pixel', 'label'])
118124

119125

120126
def infer(use_cuda, save_path):
121-
params = fluid.Params(save_path)
122127
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
123-
inferencer = fluid.Inferencer(inference_network, params, place=place)
128+
inferencer = fluid.Inferencer(
129+
inference_network, save_path=save_path, place=place)
124130

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

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,9 @@ def train_network():
5959
label = fluid.layers.data(name='label', shape=[1], dtype='int64')
6060
cost = fluid.layers.cross_entropy(input=predict, label=label)
6161
avg_cost = fluid.layers.mean(cost)
62-
accuracy = fluid.layers.accuracy(input=predict, label=label)
63-
return avg_cost, accuracy
62+
# accuracy = fluid.layers.accuracy(input=predict, label=label)
63+
# return avg_cost, accuracy
64+
return avg_cost
6465

6566

6667
def train(use_cuda, save_path):
@@ -76,12 +77,12 @@ def train(use_cuda, save_path):
7677
paddle.dataset.cifar.test10(), batch_size=BATCH_SIZE)
7778

7879
def event_handler(event):
79-
if isinstance(event, fluid.EndIteration):
80-
if (event.batch_id % 10) == 0:
80+
if isinstance(event, fluid.EndEpochEvent):
81+
if (event.epoch % 10) == 0:
8182
avg_cost, accuracy = trainer.test(reader=test_reader)
8283

8384
print('BatchID {1:04}, Loss {2:2.2}, Acc {3:2.2}'.format(
84-
event.batch_id + 1, avg_cost, accuracy))
85+
event.epoch + 1, avg_cost, accuracy))
8586

8687
if accuracy > 0.01: # Low threshold for speeding up CI
8788
trainer.params.save(save_path)

0 commit comments

Comments
 (0)