test_net.cpp 4.2 KB
Newer Older
Y
Yanzhan Yang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* 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>
#include <string>
#include "../test_helper.h"
#include "../test_include.h"

Y
Yanzhan Yang 已提交
20
void test(int argc, char *argv[]);
Y
Yanzhan Yang 已提交
21 22

int main(int argc, char *argv[]) {
Y
Yanzhan Yang 已提交
23
  test(argc, argv);
Y
Yanzhan Yang 已提交
24 25
  return 0;
}
Y
Yanzhan Yang 已提交
26 27 28 29 30 31 32 33 34 35

void test(int argc, char *argv[]) {
  int arg_index = 1;
  bool fuse = std::stoi(argv[arg_index]) == 1;
  arg_index++;
  bool enable_memory_optimization = std::stoi(argv[arg_index]) == 1;
  arg_index++;
  paddle_mobile::PaddleMobileConfigInternal config;
  config.enable_memory_optimization = enable_memory_optimization;
  paddle_mobile::PaddleMobile<paddle_mobile::CPU> paddle_mobile(config);
Y
Yanzhan Yang 已提交
36 37
  paddle_mobile.SetThreadNum(1);

Y
Yanzhan Yang 已提交
38 39
  int dim_count = std::stoi(argv[arg_index]);
  arg_index++;
Y
Yanzhan Yang 已提交
40 41 42
  int size = 1;
  std::vector<int64_t> dims;
  for (int i = 0; i < dim_count; i++) {
Y
Yanzhan Yang 已提交
43
    int64_t dim = std::stoi(argv[arg_index + i]);
Y
Yanzhan Yang 已提交
44 45 46
    size *= dim;
    dims.push_back(dim);
  }
Y
Yanzhan Yang 已提交
47
  arg_index += dim_count;
Y
Yanzhan Yang 已提交
48

49 50 51 52 53 54 55 56 57 58 59 60 61
  bool is_lod = std::stoi(argv[arg_index]) == 1;
  arg_index++;
  paddle_mobile::framework::LoD lod{{}};
  if (is_lod) {
    int lod_count = std::stoi(argv[arg_index]);
    arg_index++;
    for (int i = 0; i < lod_count; i++) {
      int dim = std::stoi(argv[arg_index + i]);
      lod[0].push_back(dim);
    }
    arg_index += lod_count;
  }

Y
Yanzhan Yang 已提交
62 63 64 65
  int var_count = std::stoi(argv[arg_index]);
  arg_index++;
  int sample_step = std::stoi(argv[arg_index]);
  arg_index++;
Y
Yanzhan Yang 已提交
66 67
  std::vector<std::string> var_names;
  for (int i = 0; i < var_count; i++) {
Y
Yanzhan Yang 已提交
68
    std::string var_name = argv[arg_index + i];
Y
Yanzhan Yang 已提交
69 70
    var_names.push_back(var_name);
  }
Y
Yanzhan Yang 已提交
71
  arg_index += var_count;
Y
Yanzhan Yang 已提交
72 73 74 75 76

  auto time1 = time();
  if (paddle_mobile.Load("./checked_model/model", "./checked_model/params",
                         fuse, false, 1, true)) {
    auto time2 = time();
Y
Yanzhan Yang 已提交
77
    std::cout << "auto-test"
Y
Yanzhan Yang 已提交
78 79 80 81 82 83 84 85 86 87 88 89
              << " load-time-cost :" << time_diff(time1, time1) << "ms"
              << std::endl;

    std::vector<float> input_data;
    std::ifstream in("input.txt", std::ios::in);
    for (int i = 0; i < size; i++) {
      float num;
      in >> num;
      input_data.push_back(num);
    }
    in.close();

90 91 92 93 94 95 96 97 98 99
    paddle_mobile::framework::LoDTensor input_tensor;
    if (is_lod) {
      input_tensor.Resize(paddle_mobile::framework::make_ddim(dims));
      input_tensor.set_lod(lod);
      auto *tensor_data = input_tensor.mutable_data<float>();
      for (int i = 0; i < size; i++) {
        tensor_data[i] = input_data[i];
      }
    }

Y
Yanzhan Yang 已提交
100 101
    // 预热10次
    for (int i = 0; i < 10; i++) {
102 103 104 105 106
      if (is_lod) {
        auto out = paddle_mobile.Predict(input_tensor);
      } else {
        auto out = paddle_mobile.Predict(input_data, dims);
      }
Y
Yanzhan Yang 已提交
107 108 109 110 111
    }

    // 测速
    auto time3 = time();
    for (int i = 0; i < 50; i++) {
112 113 114 115 116
      if (is_lod) {
        auto out = paddle_mobile.Predict(input_tensor);
      } else {
        auto out = paddle_mobile.Predict(input_data, dims);
      }
Y
Yanzhan Yang 已提交
117 118
    }
    auto time4 = time();
Y
Yanzhan Yang 已提交
119 120 121
    std::cout << "auto-test"
              << " predict-time-cost " << time_diff(time3, time4) / 50 << "ms"
              << std::endl;
Y
Yanzhan Yang 已提交
122 123

    // 测试正确性
124 125 126 127 128
    if (is_lod) {
      auto out = paddle_mobile.Predict(input_tensor);
    } else {
      auto out = paddle_mobile.Predict(input_data, dims);
    }
Y
Yanzhan Yang 已提交
129 130 131 132 133 134 135 136 137 138 139
    for (auto var_name : var_names) {
      auto out = paddle_mobile.Fetch(var_name);
      auto len = out->numel();
      if (len == 0) {
        continue;
      }
      if (out->memory_size() == 0) {
        continue;
      }
      auto data = out->data<float>();
      std::string sample = "";
Y
Yanzhan Yang 已提交
140
      for (int i = 0; i < len; i += sample_step) {
Y
Yanzhan Yang 已提交
141 142
        sample += " " + std::to_string(data[i]);
      }
Y
Yanzhan Yang 已提交
143 144
      std::cout << "auto-test"
                << " var " << var_name << sample << std::endl;
Y
Yanzhan Yang 已提交
145 146 147 148
    }
    std::cout << std::endl;
  }
}