Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Pinoxchio
apollo
提交
533e42b1
A
apollo
项目概览
Pinoxchio
/
apollo
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
A
apollo
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
533e42b1
编写于
11月 06, 2018
作者:
P
panjiacheng
提交者:
Jiangtao Hu
12月 13, 2018
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Prediction: porting the trained cruise pytorch model to protobuf.
上级
8fa11f91
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
217 addition
and
4 deletion
+217
-4
modules/tools/prediction/mlp_train/cruiseMLP_train.py
modules/tools/prediction/mlp_train/cruiseMLP_train.py
+116
-4
modules/tools/prediction/mlp_train/proto/cruise_model.proto
modules/tools/prediction/mlp_train/proto/cruise_model.proto
+101
-0
未找到文件。
modules/tools/prediction/mlp_train/cruiseMLP_train.py
浏览文件 @
533e42b1
...
...
@@ -24,6 +24,12 @@ import numpy as np
import
logging
import
argparse
import
proto.cruise_model_pb2
from
proto.cruise_model_pb2
import
TensorParameter
,
InputParameter
,
\
Conv1dParameter
,
DenseParameter
,
ActivationParameter
,
MaxPool1d
,
\
AvgPool1d
,
LaneFeatureConv
,
ObsFeatureFC
,
Classify
,
Regress
,
\
CruiseModel
import
torch
import
torch.nn
as
nn
import
torch.optim
as
optim
...
...
@@ -105,13 +111,13 @@ class FCNN_CNN1D(torch.nn.Module):
super
(
FCNN_CNN1D
,
self
).
__init__
()
self
.
lane_feature_conv
=
torch
.
nn
.
Sequential
(
\
nn
.
Conv1d
(
5
,
10
,
3
,
stride
=
1
),
\
nn
.
BatchNorm1d
(
10
),
\
#
nn.BatchNorm1d(10),\
nn
.
ReLU
(),
\
nn
.
Conv1d
(
10
,
16
,
3
,
stride
=
2
),
\
nn
.
BatchNorm1d
(
16
),
\
#
nn.BatchNorm1d(16),\
nn
.
ReLU
(),
\
nn
.
Conv1d
(
16
,
25
,
3
,
stride
=
2
),
\
nn
.
BatchNorm1d
(
25
)
#
nn.BatchNorm1d(25)
)
self
.
lane_feature_maxpool
=
nn
.
MaxPool1d
(
3
)
self
.
lane_feature_avgpool
=
nn
.
AvgPool1d
(
3
)
...
...
@@ -181,6 +187,108 @@ class FCNN_CNN1D(torch.nn.Module):
return
out_c
,
out_r
def
load_Conv1dParameter
(
model
,
key
,
stride
=
1
):
model_pb
=
Conv1dParameter
()
model_pb
.
shape
.
extend
(
list
(
model
.
state_dict
()[
key
+
'.weight'
].
shape
))
model_pb
.
use_bias
=
True
kernel_param
=
TensorParameter
()
kernel_param
.
shape
.
extend
(
list
(
model
.
state_dict
()[
key
+
'.weight'
].
shape
))
kernel_param
.
data
.
extend
(
list
(
model
.
state_dict
()[
key
+
'.weight'
].
numpy
().
reshape
(
-
1
)))
model_pb
.
kernel
.
CopyFrom
(
kernel_param
)
bias_param
=
TensorParameter
()
bias_param
.
shape
.
extend
(
list
(
model
.
state_dict
()[
key
+
'.bias'
].
shape
))
bias_param
.
data
.
extend
(
list
(
model
.
state_dict
()[
key
+
'.bias'
].
numpy
().
reshape
(
-
1
)))
model_pb
.
bias
.
CopyFrom
(
bias_param
)
model_pb
.
stride
=
stride
return
model_pb
def
load_DenseParameter
(
model
,
key
):
model_pb
=
DenseParameter
()
model_pb
.
use_bias
=
True
weights_param
=
TensorParameter
()
weights_param
.
shape
.
extend
(
list
(
model
.
state_dict
()[
key
+
'.weight'
].
numpy
().
T
.
shape
))
weights_param
.
data
.
extend
(
list
(
model
.
state_dict
()[
key
+
'.weight'
].
numpy
().
T
.
reshape
(
-
1
)))
model_pb
.
weights
.
CopyFrom
(
weights_param
)
bias_param
=
TensorParameter
()
bias_param
.
shape
.
extend
(
list
(
model
.
state_dict
()[
key
+
'.bias'
].
numpy
().
shape
))
bias_param
.
data
.
extend
(
list
(
model
.
state_dict
()[
key
+
'.bias'
].
numpy
()))
model_pb
.
bias
.
CopyFrom
(
bias_param
)
model_pb
.
units
=
model_pb
.
bias
.
shape
[
0
]
return
model_pb
def
save_FCNN_CNN1D
(
model
,
filename
):
model_pb
=
CruiseModel
()
lane_feature_conv
=
LaneFeatureConv
()
lane_feature_conv
.
conv1d_0
.
CopyFrom
(
load_Conv1dParameter
(
model
,
'lane_feature_conv.0'
,
stride
=
1
))
lane_feature_conv
.
activation_1
.
activation
=
'relu'
lane_feature_conv
.
conv1d_2
.
CopyFrom
(
load_Conv1dParameter
(
model
,
'lane_feature_conv.2'
,
stride
=
2
))
lane_feature_conv
.
activation_3
.
activation
=
'relu'
lane_feature_conv
.
conv1d_4
.
CopyFrom
(
load_Conv1dParameter
(
model
,
'lane_feature_conv.4'
,
stride
=
2
))
lane_feature_maxpool
=
MaxPool1d
()
lane_feature_maxpool
.
kernel_size
=
3
lane_feature_maxpool
.
stride
=
3
lane_feature_avgpool
=
AvgPool1d
()
lane_feature_avgpool
.
kernel_size
=
3
lane_feature_avgpool
.
stride
=
3
obs_feature_fc
=
ObsFeatureFC
()
obs_feature_fc
.
linear_0
.
CopyFrom
(
load_DenseParameter
(
model
,
'obs_feature_fc.0'
))
obs_feature_fc
.
activation_1
.
activation
=
'sigmoid'
obs_feature_fc
.
linear_3
.
CopyFrom
(
load_DenseParameter
(
model
,
'obs_feature_fc.3'
))
obs_feature_fc
.
activation_4
.
activation
=
'sigmoid'
classify
=
Classify
()
classify
.
linear_0
.
CopyFrom
(
load_DenseParameter
(
model
,
'classify.0'
))
classify
.
activation_1
.
activation
=
'sigmoid'
classify
.
linear_3
.
CopyFrom
(
load_DenseParameter
(
model
,
'classify.3'
))
classify
.
activation_4
.
activation
=
'sigmoid'
classify
.
linear_6
.
CopyFrom
(
load_DenseParameter
(
model
,
'classify.6'
))
classify
.
activation_7
.
activation
=
'sigmoid'
classify
.
linear_9
.
CopyFrom
(
load_DenseParameter
(
model
,
'classify.9'
))
classify
.
activation_10
.
activation
=
'sigmoid'
regress
=
Regress
()
regress
.
linear_0
.
CopyFrom
(
load_DenseParameter
(
model
,
'regress.0'
))
regress
.
activation_1
.
activation
=
'relu'
regress
.
linear_3
.
CopyFrom
(
load_DenseParameter
(
model
,
'regress.3'
))
regress
.
activation_4
.
activation
=
'relu'
regress
.
linear_6
.
CopyFrom
(
load_DenseParameter
(
model
,
'regress.6'
))
regress
.
activation_7
.
activation
=
'relu'
regress
.
linear_9
.
CopyFrom
(
load_DenseParameter
(
model
,
'regress.9'
))
regress
.
activation_10
.
activation
=
'relu'
model_pb
.
lane_feature_conv
.
CopyFrom
(
lane_feature_conv
)
model_pb
.
lane_feature_maxpool
.
CopyFrom
(
lane_feature_maxpool
)
model_pb
.
lane_feature_avgpool
.
CopyFrom
(
lane_feature_avgpool
)
model_pb
.
obs_feature_fc
.
CopyFrom
(
obs_feature_fc
)
model_pb
.
classify
.
CopyFrom
(
classify
)
model_pb
.
regress
.
CopyFrom
(
regress
)
with
open
(
filename
,
'wb'
)
as
params_file
:
params_file
.
write
(
model_pb
.
SerializeToString
())
'''
Custom defined loss function that lumps the loss of classification and
of regression together.
...
...
@@ -228,6 +336,7 @@ def print_dist(label):
for
l
in
unique_labels
:
print
(
'Label = {}: {}%'
.
format
(
l
,
np
.
sum
(
label
==
l
)
/
len
(
label
)
*
100
))
# ========================================================================
...
...
@@ -471,7 +580,7 @@ def validate_vanilla(valid_X, valid_y, model, batch_size=2048):
.
format
(
np
.
mean
(
loss_history
),
valid_accuracy
,
valid_precision
,
\
valid_recall
,
valid_auc
))
return
valid_loss
return
np
.
mean
(
loss_history
)
'''
...
...
@@ -562,10 +671,13 @@ if __name__ == "__main__":
y_valid
=
Variable
(
torch
.
FloatTensor
(
y_valid
).
cuda
())
# Model training:
best_valid_loss
=
float
(
'+inf'
)
for
epoch
in
range
(
100
):
train_vanilla
(
X_train
,
y_train
,
model
,
optimizer
,
epoch
)
valid_loss
=
validate_vanilla
(
X_valid
,
y_valid
,
model
)
scheduler
.
step
(
valid_loss
)
if
valid_loss
<
best_valid_loss
:
torch
.
#torch.save(model.state_dict(), './cruiseMLP_saved_model.pt')
else
:
...
...
modules/tools/prediction/mlp_train/proto/cruise_model.proto
0 → 100644
浏览文件 @
533e42b1
syntax
=
"proto2"
;
// Helpers:
message
TensorParameter
{
repeated
float
data
=
1
[
packed
=
true
];
repeated
int32
shape
=
2
;
}
message
InputParameter
{
repeated
int32
input_shape
=
1
;
optional
string
dtype
=
2
;
// data type of the input
optional
bool
sparse
=
3
;
}
// Basic layers
message
Conv1dParameter
{
repeated
int32
shape
=
1
;
optional
bool
use_bias
=
2
;
optional
TensorParameter
kernel
=
3
;
optional
TensorParameter
bias
=
4
;
optional
int32
stride
=
5
;
}
message
DenseParameter
{
optional
int32
units
=
1
;
optional
string
activation
=
2
;
optional
bool
use_bias
=
3
;
optional
TensorParameter
weights
=
4
;
optional
TensorParameter
bias
=
5
;
}
message
ActivationParameter
{
optional
string
activation
=
1
;
}
message
MaxPool1d
{
optional
int32
kernel_size
=
1
;
optional
int32
stride
=
2
;
}
message
AvgPool1d
{
optional
int32
kernel_size
=
1
;
optional
int32
stride
=
2
;
}
// Intermediate building blocks:
message
LaneFeatureConv
{
optional
Conv1dParameter
conv1d_0
=
1
;
optional
ActivationParameter
activation_1
=
2
;
optional
Conv1dParameter
conv1d_2
=
3
;
optional
ActivationParameter
activation_3
=
4
;
optional
Conv1dParameter
conv1d_4
=
5
;
}
message
ObsFeatureFC
{
optional
DenseParameter
linear_0
=
1
;
optional
ActivationParameter
activation_1
=
2
;
optional
DenseParameter
linear_3
=
3
;
optional
ActivationParameter
activation_4
=
4
;
}
message
Classify
{
optional
DenseParameter
linear_0
=
1
;
optional
ActivationParameter
activation_1
=
2
;
optional
DenseParameter
linear_3
=
3
;
optional
ActivationParameter
activation_4
=
4
;
optional
DenseParameter
linear_6
=
5
;
optional
ActivationParameter
activation_7
=
6
;
optional
DenseParameter
linear_9
=
7
;
optional
ActivationParameter
activation_10
=
8
;
}
message
Regress
{
optional
DenseParameter
linear_0
=
1
;
optional
ActivationParameter
activation_1
=
2
;
optional
DenseParameter
linear_3
=
3
;
optional
ActivationParameter
activation_4
=
4
;
optional
DenseParameter
linear_6
=
5
;
optional
ActivationParameter
activation_7
=
6
;
optional
DenseParameter
linear_9
=
7
;
optional
ActivationParameter
activation_10
=
8
;
}
// Final model
// next id =
message
CruiseModel
{
optional
LaneFeatureConv
lane_feature_conv
=
1
;
optional
MaxPool1d
lane_feature_maxpool
=
2
;
optional
AvgPool1d
lane_feature_avgpool
=
3
;
optional
ObsFeatureFC
obs_feature_fc
=
5
;
optional
Classify
classify
=
6
;
optional
Regress
regress
=
7
;
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录