test_resnet50.cpp 4.3 KB
Newer Older
Z
zhangyang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/* 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
zhangyang 已提交
14
#include <fstream>
Z
zhangyang 已提交
15 16
#include <iomanip>
#include <iostream>
Z
zhangyang 已提交
17
#include "../test_include.h"
Z
zhangyang 已提交
18 19 20 21 22 23 24 25

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

Z
zhangyang 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
void readStream(std::string filename, float *buf) {
  std::ifstream in;
  in.open(filename, std::ios::in);
  if (!in.is_open()) {
    std::cout << "open File Failed." << std::endl;
    return;
  }
  string strOne;
  int i = 0;
  while (!in.eof()) {
    in >> buf[i];
    i++;
  }
  in.close();
}
Z
zhangyang 已提交
41

Z
zhangyang 已提交
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 85 86
void convert_to_chw(int16_t **data_in, int channel, int height, int width,
                    int16_t *data_tmp) {
  int64_t amount_per_side = width * height;
  for (int h = 0; h < height; h++) {
    for (int w = 0; w < width; w++) {
      for (int c = 0; c < channel; c++) {
        *(data_tmp + c * amount_per_side + width * h + w) = *((*data_in)++);
      }
    }
  }
}

void dump(std::string filename, const Tensor input_tensor) {
  auto dataptr = input_tensor.data<float>();
  std::ofstream out(filename.c_str());
  float result = 0;
  for (int i = 0; i < input_tensor.numel(); ++i) {
    result = paddle_mobile::fpga::fp16_2_fp32(dataptr[i]);
    out << result << std::endl;
  }
  out.close();
}
void dump_stride(std::string filename, const Tensor input_tensor,
                 const int dumpnum) {
  int c = (input_tensor.dims())[1];
  int h = (input_tensor.dims())[2];
  int w = (input_tensor.dims())[3];
  auto data_ptr = input_tensor.data<float>();
  int16_t *data_tmp = (int16_t *)malloc(c * h * w * sizeof(int16_t));
  int16_t *data_ptr_16 = (int16_t *)data_ptr;
  convert_to_chw(&data_ptr_16, c, h, w, data_tmp);
  // const int16_t *dataptr = input_tensor.data<int16_t>();
  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();
  free(data_tmp);
}
static const char *g_resnet50 = "../models/resnet50";
const std::string g_image_src_float = "../images/image_src_float";
Z
zhangyang 已提交
87
int main() {
Z
zhangyang 已提交
88
  paddle_mobile::fpga::open_device();
Z
zhangyang 已提交
89
  paddle_mobile::PaddleMobile<paddle_mobile::FPGA> paddle_mobile;
Z
zhangyang 已提交
90
  if (paddle_mobile.Load(std::string(g_resnet50), true)) {
Z
zhangyang 已提交
91
    Tensor input_tensor;
Z
zhangyang 已提交
92 93
    SetupTensor<float>(&input_tensor, {1, 3, 224, 224}, static_cast<float>(2),
                       static_cast<float>(2));
Z
zhangyang 已提交
94 95
    readStream(g_image_src_float,
               input_tensor.mutable_data<float>({1, 3, 224, 224}));
Z
zhangyang 已提交
96
    paddle_mobile.FeedData(input_tensor);
Z
zhangyang 已提交
97
    paddle_mobile.Predict_To(-1);
Z
zhangyang 已提交
98
    for (int i = 0; i < 73; i++) {
Z
zhangyang 已提交
99
      auto tensor_ptr = paddle_mobile.FetchResult(i);
Z
zhangyang 已提交
100
      std::string saveName = "resnet50_result_" + std::to_string(i);
Z
zhangyang 已提交
101
      paddle_mobile::fpga::fpga_invalidate((*tensor_ptr).data<float>(),
Z
zhangyang 已提交
102 103 104 105
                                           tensor_ptr->numel() * sizeof(half));
      dump_stride(saveName, (*tensor_ptr), 20);
      // dump(saveName, (*tensor_ptr));
    }
Z
zhangyang 已提交
106

Z
zhangyang 已提交
107 108
    std::shared_ptr<Tensor> output_tensor = paddle_mobile.FetchResult(73);
    //(*output_tensor).dump<float>("resnet50_result_73");
Z
zhangyang 已提交
109
    output_tensor = paddle_mobile.FetchResult(74);
Z
zhangyang 已提交
110 111 112 113 114
    //(*output_tensor).dump<float>("resnet50_result_74");
    // std::shared_ptr<Tensor> output_tensor = paddle_mobile.FetchResult(74);

    // output_tensor = paddle_mobile.FetchResult(74);

Z
zhangyang 已提交
115 116 117 118 119 120 121 122 123
    float max = 0;
    auto data_ptr = output_tensor->data<float>();
    int maximumIdx = 0;
    for (int i = 0; i < (*output_tensor).numel(); i++) {
      if (data_ptr[i] > max) {
        maximumIdx = i;
        max = data_ptr[i];
      }
    }
Z
zhangyang 已提交
124
    std::cout << "index : " << std::dec << maximumIdx << ",    value : " << max
Z
zhangyang 已提交
125 126
              << std::endl;
    std::cout << "Computation done" << std::endl;
Z
zhangyang 已提交
127 128 129
    return 0;
  }
}