main.c 4.3 KB
Newer Older
1
//   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
D
dzhwinter 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14
//
// 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.

Y
Yu Yang 已提交
15 16
#include <paddle/capi.h>
#include <time.h>
P
peterzhang2029 已提交
17

Y
Yu Yang 已提交
18 19
#include "../common/common.h"

Y
ying 已提交
20
// Modify this path as needed.
Y
Yu Yang 已提交
21
#define CONFIG_BIN "./trainer_config.bin"
Y
ying 已提交
22 23 24 25 26 27
// Modify this path as needed.
// This demo assumes that merged model is not used, then this path is the
// directory storing all the trained parameters.
// If the model is trained by PaddlePaddle V2 API, the model is saved as
// a compressed file. You need to uncompress the compressed file first.
#define MODEL_PATH "models/pass_4"
Y
Yu Yang 已提交
28 29

int main() {
Y
ying 已提交
30
  // Initalize the PaddlePaddle runtime environment.
P
peterzhang2029 已提交
31 32
  char* argv[] = {"--use_gpu=False"};
  CHECK(paddle_init(1, (char**)argv));
Y
Yu Yang 已提交
33

Y
ying 已提交
34
  // Read the binary configuration file generated by `convert_protobin.sh`
Y
Yu Yang 已提交
35 36 37
  long size;
  void* buf = read_config(CONFIG_BIN, &size);

Y
ying 已提交
38
  // Create the gradient machine for inference.
Y
Yu Yang 已提交
39 40 41
  paddle_gradient_machine machine;
  CHECK(paddle_gradient_machine_create_for_inference(&machine, buf, (int)size));

Y
ying 已提交
42 43 44 45 46 47 48
  // Load the trained model. Modify the parameter MODEL_PATH to set the correct
  // path of the trained model.
  CHECK(paddle_gradient_machine_load_parameter_from_disk(machine, MODEL_PATH));

  // Inputs and outputs of the network are organized as paddle_arguments object
  // in C-API. In the comments below, "argument" specifically means one input of
  // the neural network in PaddlePaddle C-API.
Y
Yu Yang 已提交
49 50
  paddle_arguments in_args = paddle_arguments_create_none();

Y
ying 已提交
51 52
  // There is only one data layer in this demo MNIST network, invoke this
  // function to create one argument.
Y
Yu Yang 已提交
53 54
  CHECK(paddle_arguments_resize(in_args, 1));

Y
ying 已提交
55 56 57 58 59 60 61 62 63
  // Each argument needs one matrix or one ivector (integer vector, for sparse
  // index input, usually used in NLP task) to holds the real input data.
  // In the comments below, "matrix" specifically means the object needed by
  // argument to hold the data. Here we create the matrix for the above created
  // agument to store the testing samples.
  paddle_matrix mat =
      paddle_matrix_create(/* height = batch size */ 1,
                           /* width = dimensionality of the data layer */ 784,
                           /* whether to use GPU */ false);
Y
Yu Yang 已提交
64

P
peterzhang2029 已提交
65
  paddle_real* array;
Y
ying 已提交
66 67
  // Get the pointer pointing to the start address of the first row of the
  // created matrix.
P
peterzhang2029 已提交
68
  CHECK(paddle_matrix_get_row(mat, 0, &array));
P
peterzhang2029 已提交
69

Y
ying 已提交
70 71
  // Fill the matrix with a randomly generated test sample.
  srand(time(0));
P
peterzhang2029 已提交
72 73 74
  for (int i = 0; i < 784; ++i) {
    array[i] = rand() / ((float)RAND_MAX);
  }
Y
Yu Yang 已提交
75

Y
ying 已提交
76
  // Assign the matrix to the argument.
Y
Yu Yang 已提交
77 78
  CHECK(paddle_arguments_set_value(in_args, 0, mat));

Y
ying 已提交
79
  // Create the output argument.
Y
Yu Yang 已提交
80
  paddle_arguments out_args = paddle_arguments_create_none();
Y
ying 已提交
81 82

  // Invoke the forward computation.
Y
Yu Yang 已提交
83 84 85
  CHECK(paddle_gradient_machine_forward(machine,
                                        in_args,
                                        out_args,
Y
ying 已提交
86
                                        /* is train taks or not */ false));
Y
Yu Yang 已提交
87

Y
ying 已提交
88 89 90 91
  // Create the matrix to hold the forward result of the neural network.
  paddle_matrix prob = paddle_matrix_create_none();
  // Access the matrix of the output argument, the predicted result is stored in
  // which.
Y
Yu Yang 已提交
92
  CHECK(paddle_arguments_get_value(out_args, 0, prob));
Y
Yu Yang 已提交
93

P
peterzhang2029 已提交
94 95 96
  uint64_t height;
  uint64_t width;
  CHECK(paddle_matrix_get_shape(prob, &height, &width));
P
peterzhang2029 已提交
97
  CHECK(paddle_matrix_get_row(prob, 0, &array));
Y
Yu Yang 已提交
98

P
peterzhang2029 已提交
99
  printf("Prob: \n");
100
  for (int i = 0; i < height * width; ++i) {
P
peterzhang2029 已提交
101
    printf("%.4f ", array[i]);
P
peterzhang2029 已提交
102
    if ((i + 1) % width == 0) {
P
peterzhang2029 已提交
103 104
      printf("\n");
    }
Y
Yu Yang 已提交
105 106 107
  }
  printf("\n");

Y
ying 已提交
108
  // The cleaning up.
Y
Yu Yang 已提交
109 110 111 112 113 114
  CHECK(paddle_matrix_destroy(prob));
  CHECK(paddle_arguments_destroy(out_args));
  CHECK(paddle_matrix_destroy(mat));
  CHECK(paddle_arguments_destroy(in_args));
  CHECK(paddle_gradient_machine_destroy(machine));

Y
Yu Yang 已提交
115 116
  return 0;
}