Apollo 11.0
自动驾驶开放平台
inference_demo.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2019 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
17#include <gflags/gflags.h>
18
19#include "torch/script.h"
20#include "torch/torch.h"
21
23 model_file,
24 "/apollo/modules/planning/planning_base/tools/planning_demo_model.pt",
25 "pytorch model file.");
26
27int main(int argc, char** argv) {
28 google::ParseCommandLineFlags(&argc, &argv, true);
29
30 torch::jit::script::Module model;
31 std::cout << "is_optimized:" << model.is_optimized() << std::endl;
32 std::cout << "parameter size:" << model.parameters().size() << std::endl;
33
34 torch::Device device(torch::kCPU);
35
36 torch::set_num_threads(1);
37 try {
38 // Deserialize the ScriptModule from a file using torch::jit::load().
39 model = torch::jit::load(FLAGS_model_file, device);
40 } catch (const c10::Error& e) {
41 std::cerr << "error loading the model\n";
42 return -1;
43 }
44 std::cout << "is_optimized:" << model.is_optimized() << std::endl;
45 std::cout << "after loading parameter size:" << model.parameters().size()
46 << std::endl;
47
48 std::vector<torch::jit::IValue> torch_inputs;
49
50 int input_dim = 2 * 3 * 224 * 224 + 2 * 14;
51 std::vector<double> feature_values(input_dim, 0.5);
52
53 std::vector<torch::jit::IValue> inputs;
54 std::vector<torch::jit::IValue> tuple;
55 tuple.push_back(torch::zeros({2, 3, 224, 224}));
56 tuple.push_back(torch::zeros({2, 14}));
57 inputs.push_back(torch::ivalue::Tuple::create(tuple));
58
59 auto torch_output = model.forward(inputs);
60 std::cout << torch_output << std::endl;
61 std::cout << "isDoubleList:" << torch_output.isDoubleList() << std::endl;
62 std::cout << "isTensorList:" << torch_output.isTensorList() << std::endl;
63 std::cout << "isTensor:" << torch_output.isTensor() << std::endl;
64 auto torch_output_tensor = torch_output.toTensor();
65 std::cout << "tensor dim:" << torch_output_tensor.dim() << std::endl;
66 std::cout << "tensor sizes:" << torch_output_tensor.sizes() << std::endl;
67 std::cout << "tensor toString:" << torch_output_tensor.toString()
68 << std::endl;
69 std::cout << "tensor [0,0,0] element:" << torch_output_tensor[0][0][0]
70 << std::endl;
71 std::cout << "tensor [0,0,1] element:" << torch_output_tensor[0][0][1]
72 << std::endl;
73 std::cout << "tensor [0,0,0] element:"
74 << double(torch_output_tensor.accessor<float, 3>()[0][0][0])
75 << std::endl;
76 std::cout << "tensor [0,0,1] element:"
77 << double(torch_output_tensor.accessor<float, 3>()[0][0][1])
78 << std::endl;
79
80 return 0;
81}
first check imutoantoffset saved in device
Definition readme.txt:2
int main(int argc, char **argv)
DEFINE_string(model_file, "/apollo/modules/planning/planning_base/tools/planning_demo_model.pt", "pytorch model file.")