test_marker.cpp 5.7 KB
Newer Older
J
jameswu2014 已提交
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
/* 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 "../test_include.h"

#ifdef PADDLE_MOBILE_FPGA_V1
#include "fpga/V1/api.h"
#endif
#ifdef PADDLE_MOBILE_FPGA_V2
#include "fpga/V2/api.h"
#endif
#include <string>

void readStream(std::string filename, char *buf) {
  std::ifstream in;
J
jameswu2014 已提交
29
  in.open(filename, std::ios::in | std::ios::binary);
J
jameswu2014 已提交
30 31 32 33 34
  if (!in.is_open()) {
    std::cout << "open File Failed." << std::endl;
    return;
  }

J
jameswu2014 已提交
35 36 37
  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
J
jameswu2014 已提交
38 39 40 41 42
  in.read(buf, length);
  DLOG << length;
  in.close();
}

J
jameswu2014 已提交
43 44
void convert_to_chw(int16_t **data_in, int channel, int height, int width,
                    int num, int16_t *data_tmp) {
J
jameswu2014 已提交
45
  int64_t amount_per_side = width * height;
J
jameswu2014 已提交
46 47 48 49 50 51 52
  for (int n = 0; n < num; n++) {
    for (int h = 0; h < height; h++) {
      for (int w = 0; w < width; w++) {
        for (int c = 0; c < channel; c++) {
          *(data_tmp + n * amount_per_side * channel + c * amount_per_side +
            width * h + w) = *((*data_in)++);
        }
J
jameswu2014 已提交
53 54 55 56 57 58 59
      }
    }
  }
}

void dump_stride_half(std::string filename, Tensor input_tensor,
                      const int dumpnum, bool use_chw) {
J
jameswu2014 已提交
60 61
  // bool use_chw = true;
  if (input_tensor.dims().size() != 4) return;
J
jameswu2014 已提交
62 63 64 65 66 67 68
  int c = (input_tensor.dims())[1];
  int h = (input_tensor.dims())[2];
  int w = (input_tensor.dims())[3];
  int n = (input_tensor.dims())[0];
  auto data_ptr = input_tensor.get_data();
  auto *data_ptr_16 = reinterpret_cast<half *>(data_ptr);
  auto data_tmp = data_ptr_16;
J
jameswu2014 已提交
69 70 71 72
  if (use_chw) {
    data_tmp =
        reinterpret_cast<half *>(malloc(n * c * h * w * sizeof(int16_t)));
    convert_to_chw(&data_ptr_16, c, h, w, n, data_tmp);
J
jameswu2014 已提交
73 74 75 76 77 78 79 80 81 82
  }
  std::ofstream out(filename.c_str());
  float result = 0;
  int stride = input_tensor.numel() / dumpnum;
  stride = stride > 0 ? stride : 1;
  for (int i = 0; i < input_tensor.numel(); i += stride) {
    result = paddle_mobile::fpga::fp16_2_fp32(data_tmp[i]);
    out << result << std::endl;
  }
  out.close();
J
jameswu2014 已提交
83 84 85
  if (data_tmp != data_ptr_16) {
    free(data_tmp);
  }
J
jameswu2014 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
}

void dump_stride_float(std::string filename, Tensor input_tensor,
                       const int dumpnum) {
  auto data_ptr = reinterpret_cast<float *>(input_tensor.get_data());
  std::ofstream out(filename.c_str());
  float result = 0;
  int stride = input_tensor.numel() / dumpnum;
  stride = stride > 0 ? stride : 1;
  for (int i = 0; i < input_tensor.numel(); i += stride) {
    result = data_ptr[i];
    out << result << std::endl;
  }
  out.close();
}

J
jameswu2014 已提交
102 103 104
void dump_stride(std::string filename, Tensor input_tensor, const int dumpnum,
                 bool use_chw) {
  static int i = 0;
J
jameswu2014 已提交
105 106 107
  if (input_tensor.numel() == 0) {
    return;
  }
J
jameswu2014 已提交
108 109 110 111 112 113
  if (input_tensor.type() == typeid(float)) {
    DLOG << "op: " << i++ << ", float data  " << input_tensor.numel();
    dump_stride_float(filename, input_tensor, dumpnum);
  } else {
    DLOG << "op: " << i++ << ", half data  " << input_tensor.numel();
    dump_stride_half(filename, input_tensor, dumpnum, use_chw);
J
jameswu2014 已提交
114 115 116 117 118 119 120 121 122 123
  }
  DLOG << "dump input address: " << input_tensor.get_data();
}

static const char *g_marker_combine = "../models/marker/model";
static const char *g_image_src_float = "../models/marker/model/input_0.bin";
int main() {
  paddle_mobile::fpga::open_device();
  paddle_mobile::PaddleMobile<paddle_mobile::FPGA> paddle_mobile;

J
jameswu2014 已提交
124
  // if (paddle_mobile.Load(std::string(g_rfcn_combine) + "/model",
J
jameswu2014 已提交
125
  //                       std::string(g_rfcn_combine) + "/params", true, false,
J
jameswu2014 已提交
126 127
  //                     1, true)) {
  if (paddle_mobile.Load(std::string(g_marker_combine), true)) {
J
jameswu2014 已提交
128
    float img_info[3] = {720, 1280, 800.0f / 960.0f};
J
jameswu2014 已提交
129 130
    auto img = reinterpret_cast<float *>(
        fpga::fpga_malloc(720 * 1280 * 3 * sizeof(float)));
J
jameswu2014 已提交
131 132 133
    readStream(g_image_src_float, reinterpret_cast<char *>(img));

    std::vector<void *> v(3, nullptr);
J
jameswu2014 已提交
134
    paddle_mobile.FeedData({img});
J
jameswu2014 已提交
135 136 137 138 139
    paddle_mobile.Predict_To(-1);

    for (int i = 47; i < 52; i++) {
      auto tensor_ptr = paddle_mobile.FetchResult(i);
      std::string saveName = "marker_" + std::to_string(i);
J
jameswu2014 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
      // if(i != 58)
      paddle_mobile::fpga::fpga_invalidate((*tensor_ptr).get_data(),
                                           tensor_ptr->numel() * sizeof(float));
      //                                   tensor_ptr->numel() * sizeof(float));

      dump_stride(saveName, (*tensor_ptr), tensor_ptr->numel(),
                  true);  // 20);//tensor_ptr->numel());

      /*    float result = 0;
          std::string str = "softmax_input_data";
          float* data =
         static_cast<float*>(fpga::fpga_malloc(tensor_ptr->numel() *
         sizeof(float))); str = "softmax_output_data"; auto output_ptr =
         static_cast<half*>((*tensor_ptr).get_data()); for (int idx = 0; idx <
         tensor_ptr->numel(); ++idx)
          {
              data[idx] = fpga::fp16_2_fp32(output_ptr[idx]);
          }
          fpga::savefile<float>(str,data, tensor_ptr->numel(), result );   */
    }

    //   paddle_mobile.GetResults(&v);
J
jameswu2014 已提交
162 163 164 165 166 167
    DLOG << "Computation done";
    fpga::fpga_free(img);
  }

  return 0;
}