-
Notifications
You must be signed in to change notification settings - Fork 2
/
neural_network_vision_conv_lstm_model.py
297 lines (230 loc) · 13 KB
/
neural_network_vision_conv_lstm_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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import torch
import torch.nn as nn
# # # # https://www.researchgate.net/figure/A-comparison-between-ResNet-v1-and-ResNet-v2-on-residual-blocks-23_fig2_342334669
# class Residual_block(nn.Module): # RESNET V1
# def __init__(self, num_channels, stride=1):
# super().__init__()
# activation= nn.LeakyReLU() #, nn.LeakyReLU(), nn.GELU, nn.ReLU(), nn.ELU
# convolution = torch.nn.Conv2d(num_channels, num_channels,
# kernel_size=3, stride=stride,
# padding=1, bias=False)
# batch_norm = torch.nn.BatchNorm2d(num_channels)
# self.activation = nn.ReLU()
# first_layer_sequence = [
# convolution,
# batch_norm,
# self.activation
# ]
# recursive_layer_sequence = [
# convolution,
# batch_norm
# ]
# sequence = first_layer_sequence + (recursive_layer_sequence*1)
# self.sequential_container = nn.Sequential(*tuple(sequence)) # # # combine layers
# self.last_layer = activation # # # last layer
# def forward(self, state):
# x = self.sequential_container(state)
# x = x + state
# return self.last_layer(x)
# # # https://www.researchgate.net/figure/A-comparison-between-ResNet-v1-and-ResNet-v2-on-residual-blocks-23_fig2_342334669
class Residual_block(nn.Module): #RESNET V2
def __init__(self, num_channels, stride=1):
super().__init__()
activation= nn.LeakyReLU() #, nn.LeakyReLU(), nn.GELU, nn.ReLU(), nn.ELU
convolution_3 = torch.nn.Conv2d(num_channels, num_channels,
kernel_size=3, stride=stride,
padding=1, bias=False)
convolution_1 = torch.nn.Conv2d(num_channels, num_channels,
kernel_size=3, stride=stride,
padding=1, bias=False)
batch_norm = torch.nn.BatchNorm2d(num_channels)
activation = nn.ReLU()
dropout = nn.Dropout2d(p=0.5)
first_layer_sequence = [
batch_norm,
activation,
dropout,
convolution_1
]
recursive_layer_sequence = [
batch_norm,
activation,
dropout,
convolution_3,
batch_norm,
activation,
dropout,
convolution_1
]
sequence = first_layer_sequence + (recursive_layer_sequence*1)
self.sequential_container = nn.Sequential(*tuple(sequence)) # # # combine layers
self.last_layer = activation # # # last layer
def forward(self, state):
x = self.sequential_container(state)
x = x + state
return x
class Down_sample(torch.nn.Module):
def __init__(self, in_channels, out_channels):
super().__init__()
convolution_in = torch.nn.Conv2d(in_channels, out_channels // 2,
kernel_size=3, stride=2,
padding=1, bias=False,)
convolution_out = torch.nn.Conv2d(out_channels // 2, out_channels,
kernel_size=3, stride=2,
padding=1, bias=False,)
res_blocks_in = Residual_block(out_channels // 2)
pooling = torch.nn.AvgPool2d(kernel_size=3, stride=2, padding=1)
res_blocks_out = Residual_block(out_channels)
sequence = [
convolution_in,
res_blocks_in,
res_blocks_in,
convolution_out,
res_blocks_out,
res_blocks_out,
pooling,
res_blocks_out,
res_blocks_out,
res_blocks_out,
pooling
]
self.sequential_container = nn.Sequential(*tuple(sequence)) # # # combine layers
def forward(self, x):
x = self.sequential_container(x)
return x
class Representation_function(torch.nn.Module):
def __init__(self,
observation_space_dimensions,
state_dimension,
action_dimension,
hidden_layer_dimensions,
number_of_hidden_layer,
num_channels = 3 ,
stacked_observations = 1,
down_sampling = True ):
super().__init__()
self.action_space = action_dimension
self.down_sampling = down_sampling
stack_observation = observation_space_dimensions[-1]
downsample_net = Down_sample( stack_observation, num_channels)
convolution = torch.nn.Conv2d(stack_observation, num_channels, kernel_size=3, stride=1, padding=1, bias=False)#3x3
batchnorm = torch.nn.BatchNorm2d(num_channels)
activation = torch.nn.ReLU()
resblock = Residual_block(num_channels)
sequence_down_samp = [
downsample_net
] + [resblock]
sequence_conv_norm = [
convolution,
batchnorm,
activation,
] + [resblock]
self.sequential_downsampler = nn.Sequential(*tuple(sequence_down_samp))
self.sequential_convolution_activation = nn.Sequential(*tuple(sequence_conv_norm))
def forward(self, state):
state_normalize = self.sequential_downsampler(state) if self.down_sampling else self.sequential_convolution_activation(state)
return state_normalize
class Dynamics_function(torch.nn.Module):
def __init__(self,
state_dimension,
action_dimension,
observation_space_dimensions,
hidden_layer_dimensions,
number_of_hidden_layer,
num_channels = 3,
reduced_channels_reward = 1,
down_sampling=True):
super().__init__()
block_output_size_reward = ( (reduced_channels_reward * \
int(observation_space_dimensions[0]/14) * \
int(observation_space_dimensions[1]/14) * \
observation_space_dimensions[2])
if down_sampling
else (reduced_channels_reward * \
observation_space_dimensions[0] * \
observation_space_dimensions[1] * \
observation_space_dimensions[2])
)
self.action_space = action_dimension
dropout1d = nn.Dropout(p=0.5)
convolution = torch.nn.Conv2d(num_channels + 1, num_channels , kernel_size=3, stride=1, padding=1, bias=False) # 3x3
batchnorm = torch.nn.BatchNorm2d(num_channels )
resblock = Residual_block(num_channels )
convolution_reward = torch.nn.Conv2d(num_channels + 1 , num_channels, 1)#1x1 # reduced_channels_reward for second arg
activation=torch.nn.ReLU()
flatten = torch.nn.Flatten(1,-1) # or x.view(-1, self.block_output_size_reward)
sequence = [nn.LSTM(hidden_layer_dimensions, hidden_layer_dimensions,number_of_hidden_layer),
convolution,
batchnorm,
activation,
] + ([resblock] * number_of_hidden_layer)+ \
[activation]
sequence_reward = [
convolution_reward,
flatten,
nn.Linear(block_output_size_reward, hidden_layer_dimensions),
activation,
nn.LSTM(hidden_layer_dimensions, hidden_layer_dimensions),
nn.Linear(hidden_layer_dimensions, state_dimension,number_of_hidden_layer)
]
self.sequential_container = nn.Sequential(*tuple(sequence))
self.sequential_reward = nn.Sequential(*tuple(sequence_reward))
def forward(self, state_normalized, action):
x = torch.cat([state_normalized, action],dim=1)
next_state_normalize = self.sequential_container(x)
reward = self.sequential_reward(x)
return reward , next_state_normalize
class Prediction_function(torch.nn.Module):
def __init__(self,
state_dimension,
action_dimension,
observation_space_dimensions,
hidden_layer_dimensions,
number_of_hidden_layer,
down_sampling = True,
reduced_channels_value = 1,
reduced_channels_policy = 1,
num_channels = 3):
super().__init__()
block_output_size_value = ((reduced_channels_value * observation_space_dimensions[2] * int(observation_space_dimensions[1]/14) * int(observation_space_dimensions[0]/14))
if down_sampling
else (reduced_channels_value * observation_space_dimensions[2] * observation_space_dimensions[1] * observation_space_dimensions[0]) )
block_output_size_policy = ((reduced_channels_policy * observation_space_dimensions[2] * int(observation_space_dimensions[1]/14) * int(observation_space_dimensions[0]/14))
if down_sampling
else (reduced_channels_policy * observation_space_dimensions[2] * observation_space_dimensions[1] * observation_space_dimensions[0]))
resblock = Residual_block(num_channels)
convolution_value = torch.nn.Conv2d(num_channels, num_channels, 1) #1x1 # reduced_channels_value for second arg
convolution_policy = torch.nn.Conv2d(num_channels, num_channels, 1) #1x1 # reduced_channels_policy for second arg
flatten = torch.nn.Flatten(1,-1)
activation = torch.nn.ReLU()
dropout1d = nn.Dropout(p=0.5)
sequence_layer_init = [nn.Linear(block_output_size_value, hidden_layer_dimensions),
activation]
sequence_layer_recursive = [nn.LSTM(hidden_layer_dimensions, hidden_layer_dimensions,number_of_hidden_layer)]
sequence_layer_out = [nn.Linear(hidden_layer_dimensions, state_dimension)]
lstm_value = nn.Sequential(*tuple(sequence_layer_init + \
sequence_layer_recursive + \
sequence_layer_out) )
# self.fc_policy = mlp(self.block_output_size_policy,fc_policy_layers,action_space_size,)
sequence_layer_init = [nn.Linear(block_output_size_policy, hidden_layer_dimensions),
activation]
sequence_layer_recursive = [nn.LSTM(hidden_layer_dimensions, hidden_layer_dimensions,number_of_hidden_layer)]
sequence_layer_out = [nn.Linear(hidden_layer_dimensions, action_dimension)]
lstm_policy = nn.Sequential(*tuple(sequence_layer_init + \
sequence_layer_recursive + \
sequence_layer_out) )
sequence_1 = [ resblock ] * number_of_hidden_layer
sequence_2 = [convolution_value,
flatten,
lstm_value]
sequence_3 = [convolution_policy,
flatten,
lstm_policy]
self.resnet = nn.Sequential(*tuple(sequence_1))
self.nn_value = nn.Sequential(*tuple(sequence_2))
self.nn_policy = nn.Sequential(*tuple(sequence_3))
def forward(self, state_normalize):
resnet_state_normalize = self.resnet(state_normalize)
value = self.nn_value(resnet_state_normalize)
policy = self.nn_policy(resnet_state_normalize)
return policy , value