未验证 提交 a50b43ce 编写于 作者: C Chen Weihang 提交者: GitHub

Merge pull request #593 from JiabinYang/book02_refine

fix print() function to fit python3
from __future__ import print_function
import os import os
from PIL import Image from PIL import Image
import numpy as np import numpy as np
...@@ -89,8 +90,8 @@ def main(): ...@@ -89,8 +90,8 @@ def main():
if event.step % 100 == 0: if event.step % 100 == 0:
# event.metrics maps with train program return arguments. # event.metrics maps with train program return arguments.
# event.metrics[0] will yeild avg_cost and event.metrics[1] will yeild acc in this example. # event.metrics[0] will yeild avg_cost and event.metrics[1] will yeild acc in this example.
print "Pass %d, Batch %d, Cost %f" % (event.step, event.epoch, print("Pass %d, Batch %d, Cost %f" % (event.step, event.epoch,
event.metrics[0]) event.metrics[0]))
if isinstance(event, fluid.EndEpochEvent): if isinstance(event, fluid.EndEpochEvent):
avg_cost, acc = trainer.test( avg_cost, acc = trainer.test(
...@@ -112,8 +113,8 @@ def main(): ...@@ -112,8 +113,8 @@ def main():
# find the best pass # find the best pass
best = sorted(lists, key=lambda list: float(list[1]))[0] best = sorted(lists, key=lambda list: float(list[1]))[0]
print 'Best pass is %s, testing Avgcost is %s' % (best[0], best[1]) print('Best pass is %s, testing Avgcost is %s' % (best[0], best[1]))
print 'The classification accuracy is %.2f%%' % (float(best[2]) * 100) print('The classification accuracy is %.2f%%' % (float(best[2]) * 100))
def load_image(file): def load_image(file):
im = Image.open(file).convert('L') im = Image.open(file).convert('L')
...@@ -133,7 +134,7 @@ def main(): ...@@ -133,7 +134,7 @@ def main():
results = inferencer.infer({'img': img}) results = inferencer.infer({'img': img})
lab = np.argsort(results) # probs and lab are the results of one batch data lab = np.argsort(results) # probs and lab are the results of one batch data
print "Label of image/infer_3.png is: %d" % lab[0][0][-1] print("Inference result of image/infer_3.png is: %d" % lab[0][0][-1])
if __name__ == '__main__': if __name__ == '__main__':
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册