model_test.cc 9.0 KB
Newer Older
Y
Yan Chunwei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright (c) 2019 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.

15
#include <sstream>
Y
Yan Chunwei 已提交
16 17 18 19
#include <string>
#include <vector>
#include "lite/api/paddle_api.h"
#include "lite/api/test_helper.h"
20
#include "lite/core/device_info.h"
21
#include "lite/core/profile/timer.h"
Y
Yan Chunwei 已提交
22 23
#include "lite/utils/cp_logging.h"
#include "lite/utils/string.h"
24 25 26
#ifdef LITE_WITH_PROFILE
#include "lite/core/profile/basic_profiler.h"
#endif  // LITE_WITH_PROFILE
27
#include <gflags/gflags.h>
Y
Yan Chunwei 已提交
28

29
using paddle::lite::profile::Timer;
30

Y
Yan Chunwei 已提交
31 32 33
DEFINE_string(input_shape,
              "1,3,224,224",
              "input shapes, separated by colon and comma");
34 35 36
DEFINE_bool(use_optimize_nb,
            false,
            "optimized & naive buffer model for mobile devices");
37 38 39 40
DEFINE_string(backend,
              "arm_cpu",
              "choose backend for valid_places: arm_cpu | opencl. Compile "
              "OpenCL version if you choose opencl");
41
DEFINE_string(arg_name, "", "the arg name");
42

Y
Yan Chunwei 已提交
43 44 45 46 47 48 49 50
namespace paddle {
namespace lite_api {

void OutputOptModel(const std::string& load_model_dir,
                    const std::string& save_optimized_model_dir,
                    const std::vector<std::vector<int64_t>>& input_shapes) {
  lite_api::CxxConfig config;
  config.set_model_dir(load_model_dir);
51 52 53 54 55
#ifdef LITE_WITH_X86
  config.set_valid_places({Place{TARGET(kX86), PRECISION(kFloat)},
                           Place{TARGET(kX86), PRECISION(kInt64)},
                           Place{TARGET(kHost), PRECISION(kFloat)}});
#else
56 57 58 59 60 61 62 63 64 65 66 67 68
  if (FLAGS_backend == "opencl") {
    config.set_valid_places({
        Place{TARGET(kOpenCL), PRECISION(kFP16), DATALAYOUT(kImageDefault)},
        Place{TARGET(kOpenCL), PRECISION(kFloat), DATALAYOUT(kNCHW)},
        Place{TARGET(kOpenCL), PRECISION(kAny), DATALAYOUT(kImageDefault)},
        Place{TARGET(kOpenCL), PRECISION(kAny), DATALAYOUT(kNCHW)},
        TARGET(kARM),  // enable kARM CPU kernel when no opencl kernel
    });
  } else {  // arm_cpu
    config.set_valid_places({
        Place{TARGET(kARM), PRECISION(kFloat)},
    });
  }
69
#endif
Y
Yan Chunwei 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
  auto predictor = lite_api::CreatePaddlePredictor(config);

  // delete old optimized model
  int ret = system(
      paddle::lite::string_format("rm -rf %s", save_optimized_model_dir.c_str())
          .c_str());
  if (ret == 0) {
    LOG(INFO) << "delete old optimized model " << save_optimized_model_dir;
  }
  predictor->SaveOptimizedModel(save_optimized_model_dir,
                                LiteModelType::kNaiveBuffer);
  LOG(INFO) << "Load model from " << load_model_dir;
  LOG(INFO) << "Save optimized model to " << save_optimized_model_dir;
}

#ifdef LITE_WITH_LIGHT_WEIGHT_FRAMEWORK
void Run(const std::vector<std::vector<int64_t>>& input_shapes,
         const std::string& model_dir,
88
         const PowerMode power_mode,
Y
Yan Chunwei 已提交
89
         const int thread_num,
90
         const int repeat,
Y
Yan Chunwei 已提交
91 92
         const int warmup_times = 0) {
  lite_api::MobileConfig config;
93
  config.set_model_from_file(model_dir + ".nb");
94 95
  config.set_power_mode(power_mode);
  config.set_threads(thread_num);
Y
Yan Chunwei 已提交
96 97 98 99 100 101 102 103 104 105 106

  auto predictor = lite_api::CreatePaddlePredictor(config);

  for (int j = 0; j < input_shapes.size(); ++j) {
    auto input_tensor = predictor->GetInput(j);
    input_tensor->Resize(input_shapes[j]);
    auto input_data = input_tensor->mutable_data<float>();
    int input_num = 1;
    for (int i = 0; i < input_shapes[j].size(); ++i) {
      input_num *= input_shapes[j][i];
    }
H
HappyAngel 已提交
107

Y
Yan Chunwei 已提交
108 109 110 111 112 113 114 115 116
    for (int i = 0; i < input_num; ++i) {
      input_data[i] = 1.f;
    }
  }

  for (int i = 0; i < warmup_times; ++i) {
    predictor->Run();
  }

117 118
  Timer ti;
  for (int j = 0; j < repeat; ++j) {
119
    ti.Start();
Y
Yan Chunwei 已提交
120
    predictor->Run();
121 122
    float t = ti.Stop();
    LOG(INFO) << "iter: " << j << ", time: " << t << " ms";
Y
Yan Chunwei 已提交
123 124 125
  }

  LOG(INFO) << "================== Speed Report ===================";
126 127 128
  LOG(INFO) << "Model: " << model_dir
            << ", power_mode: " << static_cast<int>(power_mode)
            << ", threads num " << thread_num << ", warmup: " << warmup_times
129
            << ", repeats: " << repeat << ", avg time: " << ti.LapTimes().Avg()
130
            << " ms"
131 132
            << ", min time: " << ti.LapTimes().Min() << " ms"
            << ", max time: " << ti.LapTimes().Max() << " ms.";
Y
Yan Chunwei 已提交
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
  // output summary
  size_t output_tensor_num = predictor->GetOutputNames().size();
  LOG(INFO) << "output tensor num:" << output_tensor_num;

  for (size_t tidx = 0; tidx < output_tensor_num; ++tidx) {
    auto output_tensor = predictor->GetOutput(tidx);
    LOG(INFO) << "============= output tensor " << tidx << " =============";
    auto tensor_shape = output_tensor->shape();
    std::string tensor_shape_str{""};
    int output_tensor_numel = 1;
    for (int i = 0; i < tensor_shape.size(); ++i) {
      output_tensor_numel *= tensor_shape[i];
      tensor_shape_str += std::to_string(tensor_shape[i]);
      tensor_shape_str += (i < tensor_shape.size() - 1) ? "x" : "";
    }
    auto out_data = output_tensor->data<float>();
    auto out_mean =
        paddle::lite::compute_mean<float>(out_data, output_tensor_numel);
    auto out_std_dev = paddle::lite::compute_standard_deviation<float>(
        out_data, output_tensor_numel, true, out_mean);

    LOG(INFO) << "output tensor " << tidx << " dims:" << tensor_shape_str;
    LOG(INFO) << "output tensor " << tidx
              << " elements num:" << output_tensor_numel;
    LOG(INFO) << "output tensor " << tidx
              << " standard deviation:" << out_std_dev;
    LOG(INFO) << "output tensor " << tidx << " mean value:" << out_mean << "\n";

    // print result
    for (int i = 0; i < output_tensor_numel; ++i) {
      VLOG(2) << "output_tensor->data<float>()[" << i
              << "]:" << output_tensor->data<float>()[i];
    }
Y
Yan Chunwei 已提交
167
  }
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184

  // please turn off memory_optimize_pass to use this feature.
  if (FLAGS_arg_name != "") {
    auto arg_tensor = predictor->GetTensor(FLAGS_arg_name);
    auto arg_shape = arg_tensor->shape();
    int arg_num = 1;
    std::ostringstream os;
    os << "{";
    for (int i = 0; i < arg_shape.size(); ++i) {
      arg_num *= arg_shape[i];
      os << arg_shape[i] << ",";
    }
    os << "}";
    float sum = 0.;
    std::ofstream out(FLAGS_arg_name + ".txt");
    for (size_t i = 0; i < arg_num; ++i) {
      sum += arg_tensor->data<float>()[i];
185
      out << paddle::lite::to_string(arg_tensor->data<float>()[i]) << "\n";
186 187 188 189
    }
    LOG(INFO) << FLAGS_arg_name << " shape is " << os.str()
              << ", mean value is " << sum * 1. / arg_num;
  }
Y
Yan Chunwei 已提交
190 191 192 193 194 195 196 197 198 199 200 201 202
}
#endif

}  // namespace lite_api
}  // namespace paddle

int main(int argc, char** argv) {
  gflags::ParseCommandLineFlags(&argc, &argv, true);
  if (FLAGS_model_dir == "") {
    LOG(INFO) << "usage: "
              << "--model_dir /path/to/your/model";
    exit(0);
  }
203

204 205 206 207 208 209
  std::string save_optimized_model_dir = "";
  if (FLAGS_use_optimize_nb) {
    save_optimized_model_dir = FLAGS_model_dir;
  } else {
    save_optimized_model_dir = FLAGS_model_dir + "opt2";
  }
Y
Yan Chunwei 已提交
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

  auto split_string =
      [](const std::string& str_in) -> std::vector<std::string> {
    std::vector<std::string> str_out;
    std::string tmp_str = str_in;
    while (!tmp_str.empty()) {
      size_t next_offset = tmp_str.find(":");
      str_out.push_back(tmp_str.substr(0, next_offset));
      if (next_offset == std::string::npos) {
        break;
      } else {
        tmp_str = tmp_str.substr(next_offset + 1);
      }
    }
    return str_out;
  };

  auto get_shape = [](const std::string& str_shape) -> std::vector<int64_t> {
    std::vector<int64_t> shape;
    std::string tmp_str = str_shape;
    while (!tmp_str.empty()) {
      int dim = atoi(tmp_str.data());
      shape.push_back(dim);
      size_t next_offset = tmp_str.find(",");
      if (next_offset == std::string::npos) {
        break;
      } else {
        tmp_str = tmp_str.substr(next_offset + 1);
      }
    }
    return shape;
  };

  LOG(INFO) << "input shapes: " << FLAGS_input_shape;
  std::vector<std::string> str_input_shapes = split_string(FLAGS_input_shape);
  std::vector<std::vector<int64_t>> input_shapes;
246
  for (size_t i = 0; i < str_input_shapes.size(); ++i) {
Y
Yan Chunwei 已提交
247 248 249 250
    LOG(INFO) << "input shape: " << str_input_shapes[i];
    input_shapes.push_back(get_shape(str_input_shapes[i]));
  }

251 252 253 254 255
  if (!FLAGS_use_optimize_nb) {
    // Output optimized model
    paddle::lite_api::OutputOptModel(
        FLAGS_model_dir, save_optimized_model_dir, input_shapes);
  }
Y
Yan Chunwei 已提交
256 257 258

#ifdef LITE_WITH_LIGHT_WEIGHT_FRAMEWORK
  // Run inference using optimized model
259 260 261 262 263 264 265
  paddle::lite_api::Run(
      input_shapes,
      save_optimized_model_dir,
      static_cast<paddle::lite_api::PowerMode>(FLAGS_power_mode),
      FLAGS_threads,
      FLAGS_repeats,
      FLAGS_warmup);
Y
Yan Chunwei 已提交
266 267 268
#endif
  return 0;
}