utils.h 3.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// 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.

#pragma once
16
#include <math.h>
17
#include <algorithm>
N
nhzlx 已提交
18 19
#include <fstream>
#include <iostream>
20 21
#include <string>
#include <vector>
22
#include "paddle/include/paddle_inference_api.h"
23 24 25 26

namespace paddle {
namespace demo {

N
nhzlx 已提交
27 28 29 30 31
struct Record {
  std::vector<float> data;
  std::vector<int32_t> shape;
};

32
static void split(const std::string& str, char sep,
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
                  std::vector<std::string>* pieces) {
  pieces->clear();
  if (str.empty()) {
    return;
  }
  size_t pos = 0;
  size_t next = str.find(sep, pos);
  while (next != std::string::npos) {
    pieces->push_back(str.substr(pos, next - pos));
    pos = next + 1;
    next = str.find(sep, pos);
  }
  if (!str.substr(pos).empty()) {
    pieces->push_back(str.substr(pos));
  }
}

N
nhzlx 已提交
50
Record ProcessALine(const std::string& line) {
M
minqiyang 已提交
51
  VLOG(3) << "process a line";
N
nhzlx 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
  std::vector<std::string> columns;
  split(line, '\t', &columns);
  CHECK_EQ(columns.size(), 2UL)
      << "data format error, should be <data>\t<shape>";

  Record record;
  std::vector<std::string> data_strs;
  split(columns[0], ' ', &data_strs);
  for (auto& d : data_strs) {
    record.data.push_back(std::stof(d));
  }

  std::vector<std::string> shape_strs;
  split(columns[1], ' ', &shape_strs);
  for (auto& s : shape_strs) {
    record.shape.push_back(std::stoi(s));
  }
M
minqiyang 已提交
69 70
  VLOG(3) << "data size " << record.data.size();
  VLOG(3) << "data shape size " << record.shape.size();
N
nhzlx 已提交
71 72 73 74 75 76 77 78 79 80 81
  return record;
}

void CheckOutput(const std::string& referfile, const PaddleTensor& output) {
  std::string line;
  std::ifstream file(referfile);
  std::getline(file, line);
  auto refer = ProcessALine(line);
  file.close();

  size_t numel = output.data.length() / PaddleDtypeSize(output.dtype);
M
minqiyang 已提交
82 83
  VLOG(3) << "predictor output numel " << numel;
  VLOG(3) << "reference output numel " << refer.data.size();
N
nhzlx 已提交
84 85 86 87 88 89 90 91
  CHECK_EQ(numel, refer.data.size());
  switch (output.dtype) {
    case PaddleDType::INT64: {
      for (size_t i = 0; i < numel; ++i) {
        CHECK_EQ(static_cast<int64_t*>(output.data.data())[i], refer.data[i]);
      }
      break;
    }
92
    case PaddleDType::FLOAT32: {
N
nhzlx 已提交
93 94 95 96 97 98
      for (size_t i = 0; i < numel; ++i) {
        CHECK_LT(
            fabs(static_cast<float*>(output.data.data())[i] - refer.data[i]),
            1e-5);
      }
      break;
99 100 101 102 103 104 105
    }
    case PaddleDType::INT32: {
      for (size_t i = 0; i < numel; ++i) {
        CHECK_EQ(static_cast<int32_t*>(output.data.data())[i], refer.data[i]);
      }
      break;
    }
N
nhzlx 已提交
106 107 108
  }
}

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
/*
 * Get a summary of a PaddleTensor content.
 */
static std::string SummaryTensor(const PaddleTensor& tensor) {
  std::stringstream ss;
  int num_elems = tensor.data.length() / PaddleDtypeSize(tensor.dtype);

  ss << "data[:10]\t";
  switch (tensor.dtype) {
    case PaddleDType::INT64: {
      for (int i = 0; i < std::min(num_elems, 10); i++) {
        ss << static_cast<int64_t*>(tensor.data.data())[i] << " ";
      }
      break;
    }
124
    case PaddleDType::FLOAT32: {
125 126 127 128
      for (int i = 0; i < std::min(num_elems, 10); i++) {
        ss << static_cast<float*>(tensor.data.data())[i] << " ";
      }
      break;
129 130 131 132 133 134 135
    }
    case PaddleDType::INT32: {
      for (int i = 0; i < std::min(num_elems, 10); i++) {
        ss << static_cast<int32_t*>(tensor.data.data())[i] << " ";
      }
      break;
    }
136 137 138 139 140 141
  }
  return ss.str();
}

}  // namespace demo
}  // namespace paddle