test_inference_pre_post.cpp 2.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
/* 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 "../test_helper.h"
#include "io/paddle_inference_api.h"

using namespace paddle_mobile;  // NOLINT

PaddleMobileConfig GetConfig() {
  PaddleMobileConfig config;
  config.precision = PaddleMobileConfig::FP32;
  config.device = PaddleMobileConfig::kGPU_CL;
  config.pre_post_type = PaddleMobileConfig::UINT8_255;

  config.prog_file = "../models/superv2/model";
  config.param_file = "../models/superv2/params";
  config.lod_mode = false;
  config.load_when_predict = true;
  config.cl_path = "/data/local/tmp/bin";
  return config;
}

int main() {
  PaddleMobileConfig config = GetConfig();
  auto predictor =
      CreatePaddlePredictor<PaddleMobileConfig,
                            PaddleEngineKind::kPaddleMobile>(config);

  int input_length = 1 * 1 * 300 * 300;
  int output_length = input_length;

  uint8_t data_ui[300 * 300];
  for (int i = 0; i < input_length; ++i) {
    data_ui[i] = i % 256;
  }

  PaddleTensor input;
  input.shape = std::vector<int>({1, 1, 300, 300});
  input.data = PaddleBuf(data_ui, sizeof(data_ui));
  input.dtype = PaddleDType::UINT8;
  input.layout = LayoutType::LAYOUT_CHW;
  std::vector<PaddleTensor> inputs(1, input);

  PaddleTensor output;
  output.shape = std::vector<int>({});
  output.data = PaddleBuf();
  output.dtype = PaddleDType::UINT8;
  output.layout = LayoutType::LAYOUT_CHW;
  std::vector<PaddleTensor> outputs(1, output);

  std::cout << " print input : " << std::endl;
  int stride = input_length / 20;
  stride = stride > 0 ? stride : 1;
  for (size_t j = 0; j < input_length; j += stride) {
    std::cout << (unsigned)data_ui[j] << " ";
  }
  std::cout << std::endl;

  predictor->Run(inputs, &outputs);

  std::cout << " print output : " << std::endl;
  uint8_t *data_o = static_cast<uint8_t *>(outputs[0].data.data());
  int numel = outputs[0].data.length() / sizeof(uint8_t);
  stride = numel / 20;
  stride = stride > 0 ? stride : 1;
  for (size_t j = 0; j < numel; j += stride) {
    std::cout << (unsigned)data_o[j] << " ";
  }
  std::cout << std::endl;

  return 0;
}