|
| 1 | +from __future__ import absolute_import |
| 2 | +from __future__ import division |
| 3 | +from __future__ import print_function |
| 4 | + |
| 5 | +import os |
| 6 | +import numpy as np |
| 7 | +import time |
| 8 | +import sys |
| 9 | +import argparse |
| 10 | +import functools |
| 11 | +import math |
| 12 | + |
| 13 | +import paddle |
| 14 | +import paddle.inference as paddle_infer |
| 15 | +from utility import add_arguments, print_arguments |
| 16 | +import imagenet_dataset as dataset |
| 17 | + |
| 18 | + |
| 19 | +def eval(args): |
| 20 | + model_file = os.path.join(args.model_path, args.model_filename) |
| 21 | + params_file = os.path.join(args.model_path, args.params_filename) |
| 22 | + config = paddle_infer.Config(model_file, params_file) |
| 23 | + config.enable_mkldnn() |
| 24 | + |
| 25 | + predictor = paddle_infer.create_predictor(config) |
| 26 | + |
| 27 | + input_names = predictor.get_input_names() |
| 28 | + input_handle = predictor.get_input_handle(input_names[0]) |
| 29 | + output_names = predictor.get_output_names() |
| 30 | + output_handle = predictor.get_output_handle(output_names[0]) |
| 31 | + |
| 32 | + val_dataset = dataset.ImageNetDataset(data_dir=args.data_dir, mode='val') |
| 33 | + eval_loader = paddle.io.DataLoader( |
| 34 | + val_dataset, batch_size=args.batch_size, drop_last=True) |
| 35 | + |
| 36 | + cost_time = 0. |
| 37 | + total_num = 0. |
| 38 | + correct_1_num = 0 |
| 39 | + correct_5_num = 0 |
| 40 | + for batch_id, data in enumerate(eval_loader()): |
| 41 | + img_np = np.array([tensor.numpy() for tensor in data[0]]) |
| 42 | + label_np = np.array([tensor.numpy() for tensor in data[1]]) |
| 43 | + |
| 44 | + input_handle.reshape(img_np.shape) |
| 45 | + input_handle.copy_from_cpu(img_np) |
| 46 | + |
| 47 | + t1 = time.time() |
| 48 | + predictor.run() |
| 49 | + t2 = time.time() |
| 50 | + cost_time += (t2 - t1) |
| 51 | + |
| 52 | + output_data = output_handle.copy_to_cpu() |
| 53 | + |
| 54 | + for i in range(len(label_np)): |
| 55 | + label = label_np[i][0] |
| 56 | + result = output_data[i, :] |
| 57 | + index = result.argsort() |
| 58 | + total_num += 1 |
| 59 | + if index[-1] == label: |
| 60 | + correct_1_num += 1 |
| 61 | + if label in index[-5:]: |
| 62 | + correct_5_num += 1 |
| 63 | + |
| 64 | + if batch_id % 10 == 0: |
| 65 | + acc1 = correct_1_num / total_num |
| 66 | + acc5 = correct_5_num / total_num |
| 67 | + avg_time = cost_time / total_num |
| 68 | + print( |
| 69 | + "batch_id {}, acc1 {:.3f}, acc5 {:.3f}, avg time {:.5f} sec/img". |
| 70 | + format(batch_id, acc1, acc5, avg_time)) |
| 71 | + |
| 72 | + if args.test_samples > 0 and \ |
| 73 | + (batch_id + 1)* args.batch_size >= args.test_samples: |
| 74 | + break |
| 75 | + |
| 76 | + acc1 = correct_1_num / total_num |
| 77 | + acc5 = correct_5_num / total_num |
| 78 | + print("End test: test_acc1 {:.3f}, test_acc5 {:.5f}".format(acc1, acc5)) |
| 79 | + |
| 80 | + |
| 81 | +def main(): |
| 82 | + parser = argparse.ArgumentParser(description=__doc__) |
| 83 | + add_arg = functools.partial(add_arguments, argparser=parser) |
| 84 | + add_arg('model_path', str, "", "The inference model path.") |
| 85 | + add_arg('model_filename', str, "int8_infer.pdmodel", "model filename") |
| 86 | + add_arg('params_filename', str, "int8_infer.pdiparams", "params filename") |
| 87 | + add_arg('data_dir', str, "/dataset/ILSVRC2012/", |
| 88 | + "The ImageNet dataset root dir.") |
| 89 | + add_arg('test_samples', int, -1, |
| 90 | + "Test samples. If set -1, use all test samples") |
| 91 | + add_arg('batch_size', int, 16, "Batch size.") |
| 92 | + |
| 93 | + args = parser.parse_args() |
| 94 | + print_arguments(args) |
| 95 | + |
| 96 | + eval(args) |
| 97 | + |
| 98 | + |
| 99 | +if __name__ == '__main__': |
| 100 | + main() |
0 commit comments