utils.py 9.2 KB
Newer Older
G
Guanghua Yu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import time
import os
import ast
import argparse


def argsparser():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument(
        "--model_dir",
        type=str,
        default=None,
        help=("Directory include:'model.pdiparams', 'model.pdmodel', "
              "'infer_cfg.yml', created by tools/export_model.py."),
        required=True)
    parser.add_argument(
        "--image_file", type=str, default=None, help="Path of image file.")
    parser.add_argument(
        "--image_dir",
        type=str,
        default=None,
        help="Dir of image file, `image_file` has a higher priority.")
    parser.add_argument(
        "--video_file",
        type=str,
        default=None,
        help="Path of video file, `video_file` or `camera_id` has a highest priority."
    )
    parser.add_argument(
        "--camera_id",
        type=int,
        default=-1,
        help="device id of camera to predict.")
    parser.add_argument(
        "--threshold", type=float, default=0.5, help="Threshold of score.")
    parser.add_argument(
        "--output_dir",
        type=str,
        default="output",
        help="Directory of output visualization files.")
    parser.add_argument(
        "--run_mode",
        type=str,
        default='fluid',
        help="mode of running(fluid/trt_fp32/trt_fp16/trt_int8)")
    parser.add_argument(
        "--use_gpu",
        type=ast.literal_eval,
        default=False,
        help="Whether to predict with GPU.")
    parser.add_argument(
        "--run_benchmark",
        type=ast.literal_eval,
        default=False,
        help="Whether to predict a image_file repeatedly for benchmark")
    parser.add_argument(
        "--enable_mkldnn",
        type=ast.literal_eval,
        default=False,
        help="Whether use mkldnn with CPU.")
    parser.add_argument(
        "--cpu_threads", type=int, default=1, help="Num of threads with CPU.")
    parser.add_argument(
        "--use_dynamic_shape",
        type=ast.literal_eval,
        default=False,
        help="Dynamic_shape for TensorRT.")
    parser.add_argument(
        "--trt_min_shape", type=int, default=1, help="min_shape for TensorRT.")
    parser.add_argument(
        "--trt_max_shape",
        type=int,
        default=1280,
        help="max_shape for TensorRT.")
    parser.add_argument(
        "--trt_opt_shape",
        type=int,
        default=640,
        help="opt_shape for TensorRT.")
    parser.add_argument(
        "--trt_calib_mode",
        type=bool,
        default=False,
        help="If the model is produced by TRT offline quantitative "
        "calibration, trt_calib_mode need to set True.")

    return parser


class Times(object):
    def __init__(self):
        self.time = 0.
        # start time
        self.st = 0.
        # end time
        self.et = 0.

    def start(self):
        self.st = time.time()

    def end(self, repeats=1, accumulative=True):
        self.et = time.time()
        if accumulative:
            self.time += (self.et - self.st) / repeats
        else:
            self.time = (self.et - self.st) / repeats

    def reset(self):
        self.time = 0.
        self.st = 0.
        self.et = 0.

    def value(self):
        return round(self.time, 4)


class Timer(Times):
    def __init__(self):
        super(Timer, self).__init__()
        self.preprocess_time = Times()
        self.inference_time = Times()
        self.postprocess_time = Times()
        self.img_num = 0

    def info(self, average=False):
        total_time = self.preprocess_time.value() + self.inference_time.value(
        ) + self.postprocess_time.value()
        total_time = round(total_time, 4)
        print("------------------ Inference Time Info ----------------------")
        print("total_time(ms): {}, img_num: {}".format(total_time * 1000,
                                                       self.img_num))
        preprocess_time = round(self.preprocess_time.value() / self.img_num,
                                4) if average else self.preprocess_time.value()
        postprocess_time = round(
            self.postprocess_time.value() / self.img_num,
            4) if average else self.postprocess_time.value()
        inference_time = round(self.inference_time.value() / self.img_num,
                               4) if average else self.inference_time.value()

        average_latency = total_time / self.img_num
        print("average latency time(ms): {:.2f}, QPS: {:2f}".format(
            average_latency * 1000, 1 / average_latency))
        print(
            "preprocess_time(ms): {:.2f}, inference_time(ms): {:.2f}, postprocess_time(ms): {:.2f}".
            format(preprocess_time * 1000, inference_time * 1000,
                   postprocess_time * 1000))

    def report(self, average=False):
        dic = {}
        dic['preprocess_time'] = round(
            self.preprocess_time.value() / self.img_num,
            4) if average else self.preprocess_time.value()
        dic['postprocess_time'] = round(
            self.postprocess_time.value() / self.img_num,
            4) if average else self.postprocess_time.value()
        dic['inference_time'] = round(
            self.inference_time.value() / self.img_num,
            4) if average else self.inference_time.value()
        dic['img_num'] = self.img_num
        total_time = self.preprocess_time.value() + self.inference_time.value(
        ) + self.postprocess_time.value()
        dic['total_time'] = round(total_time, 4)
        return dic


def get_current_memory_mb():
    """
    It is used to Obtain the memory usage of the CPU and GPU during the running of the program.
    And this function Current program is time-consuming.
    """
    import pynvml
    import psutil
    import GPUtil
    gpu_id = os.environ.get('CUDA_VISIBLE_DEVICES', 0)

    pid = os.getpid()
    p = psutil.Process(pid)
    info = p.memory_full_info()
    cpu_mem = info.uss / 1024. / 1024.
    gpu_mem = 0
    gpu_percent = 0
    gpus = GPUtil.getGPUs()
    if gpu_id is not None and len(gpus) > 0:
        gpu_percent = gpus[gpu_id].load
        pynvml.nvmlInit()
        handle = pynvml.nvmlDeviceGetHandleByIndex(0)
        meminfo = pynvml.nvmlDeviceGetMemoryInfo(handle)
        gpu_mem = meminfo.used / 1024. / 1024.
    return round(cpu_mem, 4), round(gpu_mem, 4), round(gpu_percent, 4)


class LoggerHelper(object):
    def __init__(self, args, times, mem_info=None):
        """
        args: utility.parse_args()
        times: The Timer class
        """
        self.args = args
        self.times = times
        self.model_name = args.model_dir.strip('/').split('/')[-1]
        self.batch_size = 1
        self.shape = "dynamic shape"
        if args.run_mode == 'fluid':
            self.precision = "fp32"
            self.use_tensorrt = False
        else:
            self.precision = args.run_mode.split('_')[-1]
            self.use_tensorrt = True

        self.device = "gpu" if args.use_gpu else "cpu"
        self.preprocess_time = round(times['preprocess_time'], 4)
        self.inference_time = round(times['inference_time'], 4)
        self.postprocess_time = round(times['postprocess_time'], 4)
        self.data_num = times['img_num']
        self.total_time = round(times['total_time'], 4)
        self.mem_info = {"cpu_rss": 0, "gpu_rss": 0, "gpu_util": 0}
        if mem_info is not None:
            self.mem_info = mem_info

    def report(self):
        print("\n")
        print("----------------------- Config info -----------------------")
        print("runtime_device:", self.device)
        print("ir_optim:", True)
        print("enable_memory_optim:", True)
        print("enable_tensorrt:", self.use_tensorrt)
        print("precision:", self.precision)
        print("enable_mkldnn:", self.args.enable_mkldnn)
        print("cpu_math_library_num_threads:", self.args.cpu_threads)

        print("----------------------- Model info ----------------------")
        print("model_name:", self.model_name)

        print("------------------------ Data info ----------------------")
        print("batch_size:", self.batch_size)
        print("input_shape:", self.shape)

        print("----------------------- Perf info -----------------------")
        print("[cpu_rss(MB): {} gpu_rss(MB): {}, gpu_util: {}%".format(
            round(self.mem_info['cpu_rss'], 4),
            round(self.mem_info['gpu_rss'], 4),
            round(self.mem_info['gpu_util'], 2)))
        print("total number of predicted data: {} and total time spent(s): {}".
              format(self.data_num, self.total_time))
        print(
            "preproce_time(ms): {}, inference_time(ms): {}, postprocess_time(ms): {}".
            format(self.preprocess_time * 1000, self.inference_time * 1000,
                   self.postprocess_time * 1000))