test_net.cpp 5.6 KB
Newer Older
Y
Yanzhan Yang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* 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. */

Z
zp7 已提交
15
#include <fstream>
Y
Yanzhan Yang 已提交
16 17 18 19 20
#include <iostream>
#include <string>
#include "../test_helper.h"
#include "../test_include.h"

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

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

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;
35 36 37
  config.memory_optimization_level = enable_memory_optimization
                                         ? MemoryOptimizationWithoutFeeds
                                         : NoMemoryOptimization;
38 39 40 41 42 43 44 45 46

  // save obfuscated model
  // config.model_obfuscate_key = "asdf";
  // std::ofstream out_file("new-params", std::ofstream::binary);
  // char *out_data = ReadFileToBuff("./checked_model/params");
  // int len = GetFileLength("./checked_model/params");
  // out_file.write(out_data, len);
  // out_file.close();

Z
zp7 已提交
47 48 49 50 51
#ifdef PADDLE_MOBILE_CL
  //  config.load_when_predict = true;
  paddle_mobile::PaddleMobile<paddle_mobile::GPU_CL> paddle_mobile(config);
  paddle_mobile.SetCLPath("/data/local/tmp/bin");
#else
Y
Yanzhan Yang 已提交
52
  paddle_mobile::PaddleMobile<paddle_mobile::CPU> paddle_mobile(config);
Y
Yanzhan Yang 已提交
53
  paddle_mobile.SetThreadNum(1);
Z
zp7 已提交
54
#endif
Y
Yanzhan Yang 已提交
55

Y
Yanzhan Yang 已提交
56 57
  int dim_count = std::stoi(argv[arg_index]);
  arg_index++;
Y
Yanzhan Yang 已提交
58 59 60
  int size = 1;
  std::vector<int64_t> dims;
  for (int i = 0; i < dim_count; i++) {
Y
Yanzhan Yang 已提交
61
    int64_t dim = std::stoi(argv[arg_index + i]);
Y
Yanzhan Yang 已提交
62 63 64
    size *= dim;
    dims.push_back(dim);
  }
Y
Yanzhan Yang 已提交
65
  arg_index += dim_count;
Y
Yanzhan Yang 已提交
66

67 68 69 70 71 72 73 74 75 76 77 78 79
  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 已提交
80 81 82 83
  int var_count = std::stoi(argv[arg_index]);
  arg_index++;
  int sample_step = std::stoi(argv[arg_index]);
  arg_index++;
Y
Yanzhan Yang 已提交
84 85
  std::vector<std::string> var_names;
  for (int i = 0; i < var_count; i++) {
Y
Yanzhan Yang 已提交
86
    std::string var_name = argv[arg_index + i];
Y
Yanzhan Yang 已提交
87 88
    var_names.push_back(var_name);
  }
Y
Yanzhan Yang 已提交
89
  arg_index += var_count;
Y
Yanzhan Yang 已提交
90 91 92 93 94

  auto time1 = time();
  if (paddle_mobile.Load("./checked_model/model", "./checked_model/params",
                         fuse, false, 1, true)) {
    auto time2 = time();
Y
Yanzhan Yang 已提交
95
    std::cout << "auto-test"
96
              << " load-time-cost :" << time_diff(time1, time2) << "ms"
Y
Yanzhan Yang 已提交
97 98
              << std::endl;

99
    float input_data_array[size];
Y
Yanzhan Yang 已提交
100 101 102 103
    std::ifstream in("input.txt", std::ios::in);
    for (int i = 0; i < size; i++) {
      float num;
      in >> num;
104
      input_data_array[i] = num;
Y
Yanzhan Yang 已提交
105 106 107
    }
    in.close();

108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
    auto time3 = time();
    // std::vector<float> input_data;
    // for (int i = 0; i < size; i++) {
    //   float num = input_data_array[i];
    //   input_data.push_back(num);
    // }
    // paddle_mobile::framework::Tensor input_tensor(input_data,
    // paddle_mobile::framework::make_ddim(dims));
    paddle_mobile::framework::Tensor input_tensor(
        input_data_array, paddle_mobile::framework::make_ddim(dims));
    auto time4 = time();
    std::cout << "auto-test"
              << " preprocess-time-cost :" << time_diff(time3, time4) << "ms"
              << std::endl;

    paddle_mobile::framework::LoDTensor input_lod_tensor;
124
    if (is_lod) {
125 126 127
      input_lod_tensor.Resize(paddle_mobile::framework::make_ddim(dims));
      input_lod_tensor.set_lod(lod);
      auto *tensor_data = input_lod_tensor.mutable_data<float>();
128
      for (int i = 0; i < size; i++) {
129
        tensor_data[i] = input_data_array[i];
130 131 132
      }
    }

Y
Yanzhan Yang 已提交
133 134
    // 预热10次
    for (int i = 0; i < 10; i++) {
135
      if (is_lod) {
136
        auto out = paddle_mobile.Predict(input_lod_tensor);
137
      } else {
138 139
        paddle_mobile.Feed(var_names[0], input_tensor);
        paddle_mobile.Predict();
140
      }
Y
Yanzhan Yang 已提交
141 142 143
    }

    // 测速
144
    auto time5 = time();
Y
Yanzhan Yang 已提交
145
    for (int i = 0; i < 50; i++) {
146
      if (is_lod) {
147
        auto out = paddle_mobile.Predict(input_lod_tensor);
148
      } else {
149 150
        paddle_mobile.Feed(var_names[0], input_tensor);
        paddle_mobile.Predict();
151
      }
Y
Yanzhan Yang 已提交
152
    }
153
    auto time6 = time();
Y
Yanzhan Yang 已提交
154
    std::cout << "auto-test"
155
              << " predict-time-cost " << time_diff(time5, time6) / 50 << "ms"
Y
Yanzhan Yang 已提交
156
              << std::endl;
Y
Yanzhan Yang 已提交
157 158

    // 测试正确性
159
    if (is_lod) {
160
      auto out = paddle_mobile.Predict(input_lod_tensor);
161
    } else {
162 163
      paddle_mobile.Feed(var_names[0], input_tensor);
      paddle_mobile.Predict();
164
    }
Y
Yanzhan Yang 已提交
165 166 167 168 169 170 171 172 173 174 175
    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 已提交
176
      for (int i = 0; i < len; i += sample_step) {
Y
Yanzhan Yang 已提交
177 178
        sample += " " + std::to_string(data[i]);
      }
Y
Yanzhan Yang 已提交
179 180
      std::cout << "auto-test"
                << " var " << var_name << sample << std::endl;
Y
Yanzhan Yang 已提交
181 182 183 184
    }
    std::cout << std::endl;
  }
}