-
Notifications
You must be signed in to change notification settings - Fork 1
/
model.py
135 lines (106 loc) · 4.17 KB
/
model.py
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
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from config import Config
class Flatten(nn.Module):
def forward(self, x):
return x.view(x.size(0), -1)
class ConvBlock(nn.Module):
"""
A convolutional unit with bn and activation
"""
def __init__(self, input_size, output_size, kernel_size, stride, activation):
super(ConvBlock, self).__init__()
self.kernel_size = kernel_size
self.stride = stride
self.padding = kernel_size // 2
self.pad = nn.ReplicationPad2d(self.padding)
self.conv = nn.Conv2d(
input_size, output_size, kernel_size=kernel_size, stride=stride)
# self.bn = nn.BatchNorm2d(output_size)
self.activation = activation
def forward(self, x):
x = self.pad(x)
x = self.conv(x)
# x = self.bn(x)
return self.activation(x)
def size_out(self, size):
return (size - self.kernel_size + self.padding * 2) // self.stride + 1
class Model(nn.Module):
def __init__(self, cfg: Config):
super(Model, self).__init__()
self.cfg = cfg
# global branch
conv_blocks = []
size = np.array(cfg.stage_size)
for s in cfg.global_conv_setting:
block = ConvBlock(s[0], s[1], s[2], s[3],
activation=cfg.activation)
conv_blocks.append(block)
size = block.size_out(size)
self.global_conv = nn.Sequential(*conv_blocks)
size_out = size[0] * size[1] * \
cfg.global_conv_setting[-1][1]
self.global_hidden = nn.Sequential(
Flatten(),
nn.Linear(size_out, cfg.hidden_size // 2),
)
# local branch
conv_blocks = []
size = np.array((cfg.local_map_size, cfg.local_map_size))
for s in cfg.local_conv_setting:
block = ConvBlock(s[0], s[1], s[2], s[3],
activation=cfg.activation)
conv_blocks.append(block)
size = block.size_out(size)
self.local_conv = nn.Sequential(*conv_blocks)
size_out = size[0] * size[1] * \
cfg.local_conv_setting[-1][1]
self.local_hidden = nn.Sequential(
Flatten(),
# nn.Linear(cfg.local_map_size * cfg.local_map_size * cfg.local_input_channel, cfg.hidden_size // 2),
nn.Linear(size_out, cfg.hidden_size // 2),
)
# rnn step
if cfg.rnn_type == "LSTM":
rnn = nn.LSTM
elif cfg.rnn_type == "GRU":
rnn = nn.GRU
else:
raise NotImplementedError
self.rnn = rnn(
input_size=cfg.hidden_size,
hidden_size=cfg.rnn_hidden_size,
num_layers=cfg.rnn_layer_size,
dropout=cfg.dropout
)
# output step
self.hidden_a = nn.Linear(cfg.rnn_hidden_size, cfg.hidden_size)
self.hidden_c = nn.Linear(cfg.rnn_hidden_size, cfg.hidden_size)
self.mu = nn.Linear(cfg.hidden_size, 2)
self.sigma = nn.Linear(cfg.hidden_size, 2)
self.val = nn.Linear(cfg.hidden_size, 1)
def forward(self, s, h):
"""
Input:
s: state batch. shape (N, C, H, W)
h: previous hidden state. shape (rnn_layer_size, N, rnn_hidden_size) or a tuple of two
Output:
mu: mu for the policy distribution. shape (N, 2)
sigma: sigma for the policy distribution. shape (N, 2)
val: state value of the input state. shape (N, 1)
next_h: next hidden state. shape (rnn_layer_size, N, rnn_hidden_size) or a tuple of two
"""
x_global = self.global_hidden(self.global_conv(s[0]))
x_local = self.local_hidden(self.local_conv(s[1]))
x = torch.cat((x_global, x_local), 1)
x = self.cfg.activation(x)
x, next_h = self.rnn(x.unsqueeze(0), h)
x = x.squeeze(0)
hidden_a = self.cfg.activation(self.hidden_a(x))
hidden_c = self.cfg.activation(self.hidden_c(x))
mu = self.mu(hidden_a)
sigma = torch.sigmoid(self.sigma(hidden_a)) + 1e-5
val = self.val(hidden_c)
return mu, sigma, val, next_h