test_mobilenet_GPU.cpp 4.8 KB
Newer Older
Y
yangfei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* Copyright (c) 2018 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. */

#include <iostream>
Y
yangfei 已提交
16
#include "../../src/common/types.h"
Y
yangfei 已提交
17 18 19
#include "../test_helper.h"
#include "../test_include.h"

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
int main(int argc, char **argv) {
  // init input args
  string model_dir = g_mobilenet;
  int64_t N = 1;
  int64_t C = 3;
  int64_t H = 224;
  int64_t W = 224;
  int repeats = 10;
  int warmup = 10;
  int print_output_elem = 0;

  std::cout << "argc:" << argc << std::endl;
  if (argc > 1 && argc < 9) {
    std::cout << "usage:" << argv[0] << "\n"
              << " <model_dir>\n"
              << " <input_n>\n"
              << " <input_c>\n"
              << " <input_h>\n"
              << " <input_w>\n"
              << " <repeats>\n"
              << " <warmup>\n"
              << " <print_output>"
              << std::endl;
    return 0;
  }

  if (argc >= 9) {
    model_dir = argv[1];
    N = atoi(argv[2]);
    C = atoi(argv[3]);
    H = atoi(argv[4]);
    W = atoi(argv[5]);
    repeats = atoi(argv[6]);
    warmup = atoi(argv[7]);
    print_output_elem = atoi(argv[8]);
  }

  std::cout << "input shape(NCHW):"
            << N << " "
            << C << " "
            << H << " "
            << W << std::endl;
  std::cout << "repeats:" << repeats << std::endl;
  std::cout << "model_dir:" << model_dir << std::endl;

Y
yangfei 已提交
65
  paddle_mobile::PaddleMobile<paddle_mobile::GPU_CL> paddle_mobile;
66
  //    paddle_mobile.SetThreadNum(4);
67
  auto load_start = paddle_mobile::time();
Y
yangfei 已提交
68
#ifdef PADDLE_MOBILE_CL
Y
yangfei 已提交
69
  paddle_mobile.SetCLPath("/data/local/tmp/bin");
Y
yangfei 已提交
70 71
#endif

72 73 74 75 76
  auto load_model_status = paddle_mobile.Load(std::string(model_dir), true);
  if (!load_model_status) {
    std::cout << "failed to load model from:" << model_dir << std::endl;
    return 0;
  }
Y
yangfei 已提交
77

78 79 80 81
  auto load_end = paddle_mobile::time();
  std::cout << "load cost:"
            << paddle_mobile::time_diff(load_start, load_end)
            << " ms"  << std::endl;
82

83 84 85 86
  // input tensor
  std::vector<float> input;
  std::vector<int64_t> dims{N, C, H, W};
  GetInput<float>(g_test_image_1x3x224x224_banana, &input, dims);
87

88 89 90 91 92
  // warmup
  std::vector<float> vec_result = paddle_mobile.Predict(input, dims);
  for (int widx = 0; widx < warmup; ++widx) {
    paddle_mobile.Predict(input, dims);
  }
L
liuruilong 已提交
93

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
  // benchmark
  float sum_duration = 0.0f;
  float min_duration = 1e5f;
  float max_duration = 1e-5f;
  float ave_duration = -1;
  for (int ridx = 0; ridx < repeats; ++ridx) {
    auto start = paddle_mobile::time();
    vec_result = paddle_mobile.Predict(input, dims);
    auto end = paddle_mobile::time();
    auto duration = paddle_mobile::time_diff(start, end);
    sum_duration += duration;
    min_duration = (duration > min_duration) ? min_duration : duration;
    max_duration = (duration < max_duration) ? max_duration : duration;
    std::cout << "ridx:" << ridx + 1 << "/" << repeats
              << " " << duration << " ms" << std::endl;
  }
L
liuruilong 已提交
110

111 112 113 114 115 116 117 118 119 120 121
  // benchmark result
  ave_duration = sum_duration / static_cast<float>(repeats);

  // output result
  float output_sum = 0;
  float output_ave = -1;
  for (size_t oidx = 0; oidx < vec_result.size(); ++oidx) {
    output_sum += vec_result[oidx];
    if (print_output_elem) {
      std::cout << "out_idx:" << oidx << " " << vec_result[oidx] << std::endl;
    }
122
  }
123 124 125
  output_ave = output_sum / static_cast<float>(vec_result.size());
  std::vector<float>::iterator biggest =
      std::max_element(std::begin(vec_result), std::end(vec_result));
126

127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
  // summary
  std::cout << "===== predict benchmark ====" << std::endl
            << "run repeats:" << repeats << std::endl
            << "sum_duration:" << sum_duration << " ms" << std::endl
            << "ave_duration:" << ave_duration << " ms" << std::endl
            << "max_duration:" << max_duration << " ms" << std::endl
            << "min_duration:" << min_duration << " ms" << std::endl
            << "\n===== predict result ====" << std::endl
            << "output_sum:" << output_sum << std::endl
            << "output_ave:" << output_ave << std::endl
            << "output_size:" << vec_result.size() << std::endl
            << "Max element is " << *biggest << " at position "
            << std::distance(std::begin(vec_result), biggest) << std::endl
            << "Note: 如果结果Nan请查看:"
               " test/images/g_test_image_1x3x224x224_banana "
142 143 144
               "是否存在?"
            << std::endl;
  return 0;
Y
yangfei 已提交
145
}