test_inference_recognize_digits.cc 2.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

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
#include <gtest/gtest.h>
16
#include <time.h>
17
#include <sstream>
18
#include "gflags/gflags.h"
19 20
#include "paddle/inference/inference.h"

21 22
DEFINE_string(dirname, "", "Directory of the inference model.");

L
Liu Yiqun 已提交
23
TEST(inference, recognize_digits) {
24
  if (FLAGS_dirname.empty()) {
25
    LOG(FATAL) << "Usage: ./example --dirname=path/to/your/model";
26 27 28 29 30 31
  }

  std::cout << "FLAGS_dirname: " << FLAGS_dirname << std::endl;
  std::string dirname = FLAGS_dirname;

  paddle::InferenceEngine* engine = new paddle::InferenceEngine();
32
  engine->LoadInferenceModel(dirname);
33 34 35 36 37 38 39 40 41 42 43 44

  paddle::framework::LoDTensor input;
  srand(time(0));
  float* input_ptr =
      input.mutable_data<float>({1, 784}, paddle::platform::CPUPlace());
  for (int i = 0; i < 784; ++i) {
    input_ptr[i] = rand() / (static_cast<float>(RAND_MAX));
  }

  std::vector<paddle::framework::LoDTensor> feeds;
  feeds.push_back(input);
  std::vector<paddle::framework::LoDTensor> fetchs;
45
  engine->Execute(feeds, fetchs);
46 47

  for (size_t i = 0; i < fetchs.size(); ++i) {
48 49 50
    LOG(INFO) << fetchs[i].dims();
    std::stringstream ss;
    ss << "result:";
51
    float* output_ptr = fetchs[i].data<float>();
52 53
    for (int j = 0; j < fetchs[i].numel(); ++j) {
      ss << " " << output_ptr[j];
54
    }
55
    LOG(INFO) << ss.str();
56
  }
57 58

  delete engine;
59 60 61 62 63 64
}

int main(int argc, char** argv) {
  google::ParseCommandLineFlags(&argc, &argv, false);
  testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
65
}