test_inference_recognize_digits.cc 2.9 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
#include "paddle/framework/init.h"
20 21
#include "paddle/inference/inference.h"

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

24
TEST(recognize_digits, CPU) {
25
  if (FLAGS_dirname.empty()) {
26
    LOG(FATAL) << "Usage: ./example --dirname=path/to/your/model";
27 28 29 30 31
  }

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

32 33 34 35 36 37 38 39 40
  // 0. Initialize all the devices
  paddle::framework::InitDevices();

  // 1. Define place, executor and scope
  auto place = paddle::platform::CPUPlace();
  auto executor = paddle::framework::Executor(place);
  auto* scope = new paddle::framework::Scope();

  // 2. Initialize the inference_program and load all parameters from file
41
  paddle::InferenceEngine* engine = new paddle::InferenceEngine();
42 43 44 45 46 47 48
  paddle::framework::ProgramDesc* inference_program =
      engine->LoadInferenceModel(executor, scope, dirname);

  // 3. Get the feed_var_names and fetch_var_names
  const std::vector<std::string>& feed_target_names = engine->GetFeedVarNames();
  const std::vector<std::string>& fetch_target_names =
      engine->GetFetchVarNames();
49

50 51
  // 4. Prepare inputs
  std::map<std::string, const paddle::framework::LoDTensor*> feed_targets;
52 53 54
  paddle::framework::LoDTensor input;
  srand(time(0));
  float* input_ptr =
55
      input.mutable_data<float>({1, 28, 28}, paddle::platform::CPUPlace());
56 57 58
  for (int i = 0; i < 784; ++i) {
    input_ptr[i] = rand() / (static_cast<float>(RAND_MAX));
  }
59 60 61 62 63 64 65 66 67
  feed_targets[feed_target_names[0]] = &input;

  // 5. Define Tensor to get the outputs
  std::map<std::string, paddle::framework::LoDTensor*> fetch_targets;
  paddle::framework::LoDTensor output;
  fetch_targets[fetch_target_names[0]] = &output;

  // 6. Run the inference program
  executor.Run(*inference_program, scope, feed_targets, fetch_targets);
68

69 70 71 72 73 74 75
  // 7. Use the output as your expect.
  LOG(INFO) << output.dims();
  std::stringstream ss;
  ss << "result:";
  float* output_ptr = output.data<float>();
  for (int j = 0; j < output.numel(); ++j) {
    ss << " " << output_ptr[j];
76
  }
77
  LOG(INFO) << ss.str();
78

79
  delete scope;
80
  delete engine;
81 82 83 84 85 86
}

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