test_rfcn_api.cpp 5.7 KB
Newer Older
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. */

15 16 17
#ifndef PADDLE_MOBILE_FPGA
#define PADDLE_MOBILE_FPGA
#endif
18
#include <fstream>
Z
zhangyang0701 已提交
19
#include <iostream>
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
#include "../../src/io/paddle_inference_api.h"

using namespace paddle_mobile;
using namespace paddle_mobile::fpga;

static const char *g_image = "../models/rfcn/data.bin";
static const char *g_model = "../models/rfcn/model";
static const char *g_param = "../models/rfcn/params";

void readStream(std::string filename, char *buf) {
  std::ifstream in;
  in.open(filename, std::ios::in | std::ios::binary);
  if (!in.is_open()) {
    std::cout << "open File Failed." << std::endl;
    return;
  }

  in.seekg(0, std::ios::end);  // go to the end
  auto length = in.tellg();    // report location (this is the length)
  in.seekg(0, std::ios::beg);  // go back to the beginning
  in.read(buf, length);
  in.close();
}

PaddleMobileConfig GetConfig() {
Z
zhangyang0701 已提交
45 46 47 48 49 50 51 52 53 54 55
  PaddleMobileConfig config;
  config.precision = PaddleMobileConfig::FP32;
  config.device = PaddleMobileConfig::kFPGA;
  config.prog_file = g_model;
  config.param_file = g_param;
  config.thread_num = 1;
  config.batch_size = 1;
  config.optimize = true;
  config.lod_mode = true;
  config.quantification = false;
  return config;
56 57
}

Z
zhangyang0701 已提交
58 59 60 61 62 63 64 65 66 67 68 69
PaddleMobileConfig GetConfig1() {
  PaddleMobileConfig config;
  config.precision = PaddleMobileConfig::FP32;
  config.device = PaddleMobileConfig::kFPGA;
  config.model_dir = "../models/resnet50";
  config.thread_num = 1;
  config.batch_size = 1;
  config.optimize = true;
  config.quantification = false;
  return config;
}

70
int main() {
Z
zhangyang0701 已提交
71
  open_device();
Z
zhangyang0701 已提交
72

Z
zhangyang0701 已提交
73 74 75 76 77
  PaddleMobileConfig config = GetConfig();
  auto predictor =
      CreatePaddlePredictor<PaddleMobileConfig,
                            PaddleEngineKind::kPaddleMobile>(config);

78
  std::cout << "Finishing loading model" << std::endl;
Z
zhangyang0701 已提交
79

Z
zhangyang0701 已提交
80 81
  float img_info[3] = {432, 1280, 1.0f};
  int img_length = 432 * 1280 * 3;
Z
zhangyang0701 已提交
82 83 84
  auto img = reinterpret_cast<float *>(fpga_malloc(img_length * sizeof(float)));
  readStream(g_image, reinterpret_cast<char *>(img));

85
  std::cout << "Finishing initializing data" << std::endl;
86
  struct PaddleTensor t_img_info, t_img;
Z
zhangyang0701 已提交
87
  t_img.dtypeid = typeid(float);
88
  t_img_info.layout = LAYOUT_HWC;
Z
zhangyang0701 已提交
89
  t_img_info.shape = std::vector<int>({1, 3});
90 91 92
  t_img_info.name = "Image information";
  t_img_info.data.Reset(img_info, 3 * sizeof(float));

Z
zhangyang0701 已提交
93
  t_img.dtypeid = typeid(float);
94
  t_img.layout = LAYOUT_HWC;
Z
zhangyang0701 已提交
95
  t_img.shape = std::vector<int>({1, 432, 1280, 3});
96 97 98 99 100 101 102 103 104
  t_img.name = "Image information";
  t_img.data.Reset(img, img_length * sizeof(float));
  predictor->FeedPaddleTensors({t_img_info, t_img});

  std::cout << "Finishing feeding data " << std::endl;

  predictor->Predict_From_To(0, -1);
  std::cout << "Finishing predicting " << std::endl;

105 106 107
  std::vector<PaddleTensor> v;        // No need to initialize v
  predictor->FetchPaddleTensors(&v);  // Old data in v will be cleared
  std::cout << "Output number is " << v.size() << std::endl;
Z
zhangyang0701 已提交
108 109 110
  std::cout << "out[0] length " << v[0].data.length() << std::endl;
  std::cout << "out[1] length " << v[1].data.length() << std::endl;
  std::cout << "out[2] length " << v[2].data.length() << std::endl;
111

Z
zhangyang0701 已提交
112 113 114 115 116
  auto post_nms = v[0].data.length() / sizeof(float) / 8;
  for (int num = 0; num < post_nms; num++) {
    for (int i = 0; i < 8; i++) {
      auto p = reinterpret_cast<float *>(v[0].data.data());
      std::cout << p[num * 8 + i] << std::endl;
117 118
    }
  }
Z
zhangyang0701 已提交
119 120 121 122
  for (int num = 0; num < post_nms; num++) {
    for (int i = 0; i < 8; i++) {
      auto p = reinterpret_cast<float *>(v[1].data.data());
      std::cout << p[num * 8 + i] << std::endl;
123 124
    }
  }
Z
zhangyang0701 已提交
125 126 127 128
  for (int num = 0; num < post_nms; num++) {
    for (int i = 0; i < 4; i++) {
      auto p = reinterpret_cast<float *>(v[2].data.data());
      std::cout << p[num * 4 + i] << std::endl;
129 130
    }
  }
131 132
  std::cout << "Finish getting vector values" << std::endl;

Z
zhangyang0701 已提交
133 134
  ////////////////////////////////////////////////////

135 136 137 138 139 140 141
  PaddleTensor tensor;
  predictor->GetPaddleTensor("fetch2", &tensor);
  for (int i = 0; i < post_nms; i++) {
    auto p = reinterpret_cast<float *>(tensor.data.data());
    std::cout << p[+i] << std::endl;
  }

Z
zhangyang0701 已提交
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 167 168 169 170 171 172
  //////////////////////////////////////////////////////

  PaddleMobileConfig config1 = GetConfig1();
  auto predictor1 =
      CreatePaddlePredictor<PaddleMobileConfig,
                            PaddleEngineKind::kPaddleMobile>(config1);

  std::cout << "Finishing loading model" << std::endl;

  int img_length1 = 224 * 224 * 3;
  auto img1 =
      reinterpret_cast<float *>(fpga_malloc(img_length1 * sizeof(float)));

  std::cout << "Finishing initializing data" << std::endl;

  struct PaddleTensor t_img1;

  t_img1.dtypeid = typeid(float);
  t_img1.layout = LAYOUT_HWC;
  t_img1.shape = std::vector<int>({1, 224, 224, 3});
  t_img1.name = "Image information";
  t_img1.data.Reset(img1, img_length1 * sizeof(float));
  predictor1->FeedPaddleTensors({t_img1});
  predictor1->Predict_From_To(0, -1);
  std::cout << "Finishing predicting " << std::endl;

  std::vector<PaddleTensor> v1;         // No need to initialize v
  predictor1->FetchPaddleTensors(&v1);  // Old data in v will be cleared
  std::cout << "Output number is " << v1.size() << std::endl;
  std::cout << "out[0] length " << v1[0].data.length() << std::endl;

Z
zhangyang0701 已提交
173
  return 0;
174
}