analyzer_text_classification_tester.cc 3.1 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.

L
luotao1 已提交
15
#include "paddle/fluid/inference/tests/api/tester_helper.h"
16 17

namespace paddle {
L
luotao1 已提交
18
namespace inference {
19

20
struct DataReader {
L
luotao1 已提交
21 22
  explicit DataReader(const std::string &path)
      : file(new std::ifstream(path)) {}
23

L
luotao1 已提交
24
  bool NextBatch(std::vector<PaddleTensor> *input, int batch_size) {
25 26
    PADDLE_ENFORCE_EQ(batch_size, 1);
    std::string line;
L
luotao1 已提交
27 28 29
    PaddleTensor tensor;
    tensor.dtype = PaddleDType::INT64;
    tensor.lod.emplace_back(std::vector<size_t>({0}));
30 31 32 33 34 35
    std::vector<int64_t> data;

    for (int i = 0; i < batch_size; i++) {
      if (!std::getline(*file, line)) return false;
      inference::split_to_int64(line, ' ', &data);
    }
L
luotao1 已提交
36
    tensor.lod.front().push_back(data.size());
37

L
luotao1 已提交
38 39 40 41 42
    tensor.data.Resize(data.size() * sizeof(int64_t));
    memcpy(tensor.data.data(), data.data(), data.size() * sizeof(int64_t));
    tensor.shape.push_back(data.size());
    tensor.shape.push_back(1);
    input->assign({tensor});
43
    return true;
44 45
  }

46 47 48 49
  std::unique_ptr<std::ifstream> file;
};

void Main(int batch_size) {
50 51 52 53 54 55
  // shape --
  // Create Predictor --
  AnalysisConfig config;
  config.model_dir = FLAGS_infer_model;
  config.use_gpu = false;
  config.enable_ir_optim = true;
56

L
luotao1 已提交
57 58 59
  std::vector<PaddleTensor> input_slots, output_slots;
  DataReader reader(FLAGS_infer_data);
  std::vector<std::vector<PaddleTensor>> input_slots_all;
60

L
luotao1 已提交
61 62 63 64 65
  if (FLAGS_test_all_data) {
    LOG(INFO) << "test all data";
    int num_batches = 0;
    while (reader.NextBatch(&input_slots, FLAGS_batch_size)) {
      input_slots_all.emplace_back(input_slots);
66 67
      ++num_batches;
    }
L
luotao1 已提交
68 69 70
    LOG(INFO) << "total number of samples: " << num_batches * FLAGS_batch_size;
    TestPrediction(config, input_slots_all, &output_slots, FLAGS_num_threads);
    return;
71
  }
L
luotao1 已提交
72 73 74 75 76 77

  // one batch starts
  // data --
  reader.NextBatch(&input_slots, FLAGS_batch_size);
  input_slots_all.emplace_back(input_slots);
  TestPrediction(config, input_slots_all, &output_slots, FLAGS_num_threads);
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97

  // Get output
  LOG(INFO) << "get outputs " << output_slots.size();

  for (auto &output : output_slots) {
    LOG(INFO) << "output.shape: " << to_string(output.shape);
    // no lod ?
    CHECK_EQ(output.lod.size(), 0UL);
    LOG(INFO) << "output.dtype: " << output.dtype;
    std::stringstream ss;
    for (int i = 0; i < 5; i++) {
      ss << static_cast<float *>(output.data.data())[i] << " ";
    }
    LOG(INFO) << "output.data summary: " << ss.str();
    // one batch ends
  }
}

TEST(text_classification, basic) { Main(FLAGS_batch_size); }

L
luotao1 已提交
98
}  // namespace inference
99
}  // namespace paddle