-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.h
161 lines (138 loc) · 4.16 KB
/
model.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#ifndef FFCC_MODEL_H
#define FFCC_MODEL_H
#include <torch/script.h>
template <int nIn>
class PFNN_net {
public:
torch::jit::script::Module module;
public:
PFNN_net(const std::string& path) {
try {
module = torch::jit::load(path);
}
catch (const c10::Error& e) {
std::cerr << "error load model " << e.msg() << "\n";
return;
}
module.eval();
}
template <typename T0>
void predict(
std::vector<T0>& last_vector,
const std::vector<float>& aIn)
{
assert(aIn.size() == nIn);
std::vector<float> aInFF;
aInFF.assign(aIn.begin(), aIn.end());
constexpr int nInFF = nIn;
assert(aInFF.size() == nInFF);
std::vector<torch::jit::IValue> inputs;
inputs.emplace_back(
torch::from_blob(const_cast<float*>(aInFF.data()), { 1,nInFF })); // batch_size, input_size
at::Tensor output_para = module.forward(inputs).toTensor();
last_vector.clear();
for (int i = 0; i < output_para.sizes()[1]; i++) {
last_vector.push_back(output_para[0][i].item<T0>());
}
}
};
/**
* MLP_Fourier model
* @param aIn : Input of the model, phase and trajectory in local frame
* @param nOctave : Fourier feature hyperparameter N, mutiply the positive interger up to N
*/
template <int nIn, int nOctave>
class MLP_Fourier{
public:
torch::jit::script::Module module;
public:
MLP_Fourier(const std::string& path) {
try {
module = torch::jit::load(path);
}
catch (const c10::Error &e) {
std::cerr << "error load model " << e.msg() << "\n";
return;
}
module.eval();
}
template <typename T0>
void predict(
std::vector<T0>& last_vector,
const std::vector<float>& aIn)
{
assert(aIn.size() == nIn);
std::vector<float> aInFF;
if(nOctave==0){
aInFF.assign(aIn.begin(),aIn.end());
}
else{
float phase = aIn[0];
for(unsigned int ioct=0;ioct<nOctave;++ioct){
aInFF.push_back(sin(2*M_PI*phase*(1+ioct)));
aInFF.push_back(cos(2*M_PI*phase*(1+ioct)));
}
aInFF.insert(aInFF.end(),aIn.begin()+1,aIn.end());
}
constexpr int nInFF = (nOctave==0) ? nIn : nIn-1+nOctave*2;
assert(aInFF.size()==nInFF);
std::vector<torch::jit::IValue> inputs;
inputs.emplace_back(
torch::from_blob(const_cast<float *>(aInFF.data()), {1,nInFF}));
at::Tensor output_para = module.forward(inputs).toTensor();
last_vector.clear();
for (int i = 0; i < output_para.sizes()[1]; i++) {
last_vector.push_back(output_para[0][i].item<T0>());
}
}
};
template <int nIn, int nLayer, int nHidden>
class RNN_Phase{
public:
torch::jit::script::Module module;
std::vector<float> vector_hidden;
public:
RNN_Phase(const std::string& path) {
try {
module = torch::jit::load(path);
}
catch (const c10::Error &e) {
std::cerr << "error load model " << e.msg() << "\n";
return;
}
module.eval();
vector_hidden.assign(nLayer*nHidden, 0.0);
}
void predict(
std::vector<float>& vector_out,
const std::vector<float>& aIn)
{
assert(aIn.size() == nIn);
std::vector<torch::jit::IValue> inputs;
inputs.emplace_back(
torch::from_blob(
const_cast<float *>(aIn.data()),
{1,1,nIn})); // seqlength, batch_size, input_size
inputs.emplace_back(
torch::from_blob(
vector_hidden.data(),
{nLayer,1,nHidden})); // num_layer, batch_size, hidden_size
torch::jit::IValue out_hidden = module.forward(inputs);
c10::intrusive_ptr<c10::ivalue::Tuple> hoge = out_hidden.toTuple();
at::Tensor out0 = hoge->elements()[0].toTensor();
at::Tensor hid0 = hoge->elements()[1].toTensor();
vector_out.assign(
out0.template data_ptr<float>(),
out0.template data_ptr<float>()+out0.numel() );
vector_hidden.assign(
hid0.template data_ptr<float>(),
hid0.template data_ptr<float>()+hid0.numel() );
/*
std::cout << "hoge" << std::endl;
for(auto f: vector_hidden ){
std::cout << f << std::endl;
}
*/
}
};
#endif //INC_2_RUNTIME_MODEL_H