test_net.cpp 8.8 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

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++;
34 35 36 37
  bool quantification = std::stoi(argv[arg_index]) == 1;
  arg_index++;
  int quantification_fold = std::stoi(argv[arg_index]);
  arg_index++;
Y
Yanzhan Yang 已提交
38
  paddle_mobile::PaddleMobileConfigInternal config;
39 40 41
  config.memory_optimization_level = enable_memory_optimization
                                         ? MemoryOptimizationWithoutFeeds
                                         : NoMemoryOptimization;
42 43 44 45 46 47 48 49 50

  // 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 已提交
51 52 53 54
#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");
55
  std::cout << "testing opencl yyz " << std::endl;
Z
zp7 已提交
56
#else
Y
Yanzhan Yang 已提交
57
  paddle_mobile::PaddleMobile<paddle_mobile::CPU> paddle_mobile(config);
Y
Yanzhan Yang 已提交
58
  paddle_mobile.SetThreadNum(1);
59
  std::cout << "testing cpu yyz " << std::endl;
Z
zp7 已提交
60
#endif
Y
Yanzhan Yang 已提交
61

Y
Yanzhan Yang 已提交
62 63
  int dim_count = std::stoi(argv[arg_index]);
  arg_index++;
Y
Yanzhan Yang 已提交
64 65 66
  int size = 1;
  std::vector<int64_t> dims;
  for (int i = 0; i < dim_count; i++) {
Y
Yanzhan Yang 已提交
67
    int64_t dim = std::stoi(argv[arg_index + i]);
Y
Yanzhan Yang 已提交
68 69 70
    size *= dim;
    dims.push_back(dim);
  }
Y
Yanzhan Yang 已提交
71
  arg_index += dim_count;
Y
Yanzhan Yang 已提交
72

73 74 75 76 77 78 79 80 81 82 83 84 85
  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 已提交
86 87
  int var_count = std::stoi(argv[arg_index]);
  arg_index++;
88 89 90 91 92
  bool is_sample_step = std::stoi(argv[arg_index]) == 1;
  arg_index++;
  int sample_arg = std::stoi(argv[arg_index]);
  int sample_step = sample_arg;
  int sample_num = sample_arg;
Y
Yanzhan Yang 已提交
93
  arg_index++;
Y
Yanzhan Yang 已提交
94 95
  std::vector<std::string> var_names;
  for (int i = 0; i < var_count; i++) {
Y
Yanzhan Yang 已提交
96
    std::string var_name = argv[arg_index + i];
Y
Yanzhan Yang 已提交
97 98
    var_names.push_back(var_name);
  }
Y
Yanzhan Yang 已提交
99
  arg_index += var_count;
100 101
  bool check_shape = std::stoi(argv[arg_index]) == 1;
  arg_index++;
Y
Yanzhan Yang 已提交
102 103 104

  auto time1 = time();
  if (paddle_mobile.Load("./checked_model/model", "./checked_model/params",
105
                         fuse, quantification, 1, true, quantification_fold)) {
Y
Yanzhan Yang 已提交
106
    auto time2 = time();
Y
Yanzhan Yang 已提交
107
    std::cout << "auto-test"
108
              << " load-time-cost :" << time_diff(time1, time2) << "ms"
Y
Yanzhan Yang 已提交
109 110
              << std::endl;

111
    float *input_data_array = new float[size];
Y
Yanzhan Yang 已提交
112 113 114 115
    std::ifstream in("input.txt", std::ios::in);
    for (int i = 0; i < size; i++) {
      float num;
      in >> num;
116
      input_data_array[i] = num;
Y
Yanzhan Yang 已提交
117 118 119
    }
    in.close();

120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
    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;
136
    if (is_lod) {
137 138 139
      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>();
140
      for (int i = 0; i < size; i++) {
141
        tensor_data[i] = input_data_array[i];
142 143 144
      }
    }

145 146 147 148 149 150 151 152 153
    // // 预热10次
    // for (int i = 0; i < 10; i++) {
    //   if (is_lod) {
    //     auto out = paddle_mobile.Predict(input_lod_tensor);
    //   } else {
    //     paddle_mobile.Feed(var_names[0], input_tensor);
    //     paddle_mobile.Predict();
    //   }
    // }
Y
Yanzhan Yang 已提交
154

155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
    // // 测速
    // auto time5 = time();
    // for (int i = 0; i < 50; i++) {
    //   if (is_lod) {
    //     auto out = paddle_mobile.Predict(input_lod_tensor);
    //   } else {
    //     paddle_mobile.Feed(var_names[0], input_tensor);
    //     paddle_mobile.Predict();
    //   }
    // }
    // auto time6 = time();
    // std::cout << "auto-test"
    //           << " predict-time-cost " << time_diff(time5, time6) / 50 <<
    //           "ms"
    //           << std::endl;
Y
Yanzhan Yang 已提交
170 171

    // 测试正确性
172
    if (is_lod) {
173
      auto out = paddle_mobile.Predict(input_lod_tensor);
174
    } else {
175 176
      paddle_mobile.Feed(var_names[0], input_tensor);
      paddle_mobile.Predict();
177
    }
178 179 180
#ifdef PADDLE_MOBILE_CL
    for (auto var_name : var_names) {
      auto cl_image = paddle_mobile.FetchImage(var_name);
181 182 183
      if (cl_image == nullptr || cl_image->GetCLImage() == nullptr) {
        continue;
      }
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
      auto len = cl_image->numel();
      if (len == 0) {
        continue;
      }
      int width = cl_image->ImageDims()[0];
      int height = cl_image->ImageDims()[1];
      paddle_mobile::framework::half_t *image_data =
          new paddle_mobile::framework::half_t[height * width * 4];
      cl_int err;
      cl_mem image = cl_image->GetCLImage();
      size_t origin[3] = {0, 0, 0};
      size_t region[3] = {width, height, 1};
      err = clEnqueueReadImage(cl_image->CommandQueue(), image, CL_TRUE, origin,
                               region, 0, 0, image_data, 0, NULL, NULL);
      CL_CHECK_ERRORS(err);
      float *tensor_data = new float[cl_image->numel()];
      auto converter = cl_image->Converter();
      converter->ImageToNCHW(image_data, tensor_data, cl_image->ImageDims(),
                             cl_image->dims());

      auto data = tensor_data;
      std::string sample = "";
206 207 208 209 210
      if (check_shape) {
        for (int i = 0; i < cl_image->dims().size(); i++) {
          sample += " " + std::to_string(cl_image->dims()[i]);
        }
      }
211 212 213 214 215 216 217 218 219 220 221 222 223
      if (!is_sample_step) {
        sample_step = len / sample_num;
      }
      if (sample_step <= 0) {
        sample_step = 1;
      }
      for (int i = 0; i < len; i += sample_step) {
        sample += " " + std::to_string(data[i]);
      }
      std::cout << "auto-test"
                << " var " << var_name << sample << std::endl;
    }
#else
Y
Yanzhan Yang 已提交
224 225 226 227 228 229 230 231 232
    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;
      }
233 234 235
      if (out->type() == type_id<int>()) {
        auto data = out->data<int>();
        std::string sample = "";
236 237 238 239 240
        if (check_shape) {
          for (int i = 0; i < out->dims().size(); i++) {
            sample += " " + std::to_string(out->dims()[i]);
          }
        }
241 242 243 244 245 246 247 248 249 250 251 252 253 254
        if (!is_sample_step) {
          sample_step = len / sample_num;
        }
        if (sample_step <= 0) {
          sample_step = 1;
        }
        for (int i = 0; i < len; i += sample_step) {
          sample += " " + std::to_string(data[i]);
        }
        std::cout << "auto-test"
                  << " var " << var_name << sample << std::endl;
      } else if (out->type() == type_id<float>()) {
        auto data = out->data<float>();
        std::string sample = "";
255 256 257 258 259
        if (check_shape) {
          for (int i = 0; i < out->dims().size(); i++) {
            sample += " " + std::to_string(out->dims()[i]);
          }
        }
260 261 262 263 264 265 266 267 268 269 270
        if (!is_sample_step) {
          sample_step = len / sample_num;
        }
        if (sample_step <= 0) {
          sample_step = 1;
        }
        for (int i = 0; i < len; i += sample_step) {
          sample += " " + std::to_string(data[i]);
        }
        std::cout << "auto-test"
                  << " var " << var_name << sample << std::endl;
271
      }
Y
Yanzhan Yang 已提交
272
    }
273
#endif
Y
Yanzhan Yang 已提交
274 275 276
    std::cout << std::endl;
  }
}