-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodels.py
214 lines (165 loc) · 6.55 KB
/
models.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
from typing import List
import numpy as np
from torch import nn
from torch.nn import functional as F
import torch
def build_mlp(layers_dims: List[int]):
layers = []
for i in range(len(layers_dims) - 2):
layers.append(nn.Linear(layers_dims[i], layers_dims[i + 1]))
layers.append(nn.BatchNorm1d(layers_dims[i + 1]))
layers.append(nn.ReLU(True))
layers.append(nn.Linear(layers_dims[-2], layers_dims[-1]))
return nn.Sequential(*layers)
class MockModel(torch.nn.Module):
"""
Does nothing. Just for testing.
"""
def __init__(self, device="cuda", bs=64, n_steps=17, output_dim=256):
super().__init__()
self.device = device
self.bs = bs
self.n_steps = n_steps
self.repr_dim = 256
def forward(self, states, actions):
"""
Args:
During training:
states: [B, T, Ch, H, W]
During inference:
states: [B, 1, Ch, H, W]
actions: [B, T-1, 2]
Output:
predictions: [B, T, D]
"""
return torch.randn((self.bs, self.n_steps, self.repr_dim)).to(self.device)
class PatchEmbedding(nn.Module):
def __init__(self, image_size, patch_size, in_channels, embed_dim):
super(PatchEmbedding, self).__init__()
self.patch_embedding = nn.Conv2d(
in_channels=in_channels, # first channel is agent, second is border and walls
out_channels=embed_dim, # image size is 64 x 64, and we want
kernel_size=patch_size, # 4x4 patches
stride=patch_size # non-overlapping patches
)
def forward(self, x):
x = self.patch_embedding(x)
x = x.flatten(2)
x = x.transpose(1, 2)
return x
class MultiHeadSelfAttention(nn.Module):
def __init__(self, embed_dim, num_heads):
super(MultiHeadSelfAttention, self).__init__()
assert embed_dim % num_heads == 0, "Embedding dimension must be divisible by number of heads"
self.num_heads = num_heads
self.embed_dim = embed_dim
self.head_dim = embed_dim // num_heads
self.q_proj = nn.Linear(embed_dim, embed_dim)
self.k_proj = nn.Linear(embed_dim, embed_dim)
self.v_proj = nn.Linear(embed_dim, embed_dim)
self.out_proj = nn.Linear(embed_dim, embed_dim)
self.scale = self.head_dim ** -0.5
def forward(self, x):
batch_size, seq_len, embed_dim = x.size()
Q = self.q_proj(x)
K = self.k_proj(x)
V = self.v_proj(x)
Q = Q.view(batch_size, seq_len, self.num_heads, self.head_dim).transpose(1, 2)
K = K.view(batch_size, seq_len, self.num_heads, self.head_dim).transpose(1, 2)
V = V.view(batch_size, seq_len, self.num_heads, self.head_dim).transpose(1, 2)
scores = torch.matmul(Q, K.transpose(-2, -1)) * self.scale
attention_weights = torch.softmax(scores, dim=-1)
attention_output = torch.matmul(attention_weights, V)
attention_output = attention_output.transpose(1, 2).contiguous()
attention_output = attention_output.view(batch_size, seq_len, embed_dim)
output = self.out_proj(attention_output)
return output
class TransformerBlock(nn.Module):
def __init__(self, embed_dim, num_heads, mlp_dim, dropout):
super(TransformerBlock, self).__init__()
self.attention = MultiHeadSelfAttention(embed_dim, num_heads)
self.norm1 = nn.LayerNorm(embed_dim)
self.mlp = nn.Sequential(
nn.Linear(embed_dim, mlp_dim),
nn.ReLU(),
nn.Linear(mlp_dim, embed_dim)
)
self.norm2 = nn.LayerNorm(embed_dim)
self.dropout = nn.Dropout(dropout)
def forward(self, x):
attention_output = self.attention(x)
x = self.norm1(x + self.dropout(attention_output))
mlp_output = self.mlp(x)
x = self.norm2(x + self.dropout(mlp_output))
return x
class VisionTransformer(nn.Module):
def __init__(self, image_size, patch_size, in_channels, embed_dim, num_heads, mlp_dim, num_layers, num_classes, dropout=0.1):
super(VisionTransformer, self).__init__()
self.patch_embedding = PatchEmbedding(image_size, patch_size, in_channels, embed_dim)
num_patches = (image_size // patch_size) ** 2
self.pos_embedding = nn.Parameter(torch.zeros(1, num_patches, embed_dim))
self.transformer_blocks = nn.ModuleList([
TransformerBlock(embed_dim, num_heads, mlp_dim, dropout)
for _ in range(num_layers)
])
self.dropout = nn.Dropout(dropout)
def forward(self, x):
x = self.patch_embedding(x)
x = x + self.pos_embedding
x = self.dropout(x)
for transformer_block in self.transformer_blocks:
x = transformer_block(x)
return x
class JEPAEncoder(torch.nn.Module):
def __init__(self, device="cuda", bs=64, n_steps=17, output_dim=256):
super().__init__()
self.device = device
self.bs = bs # batch size
self.n_steps = n_steps # number of forward passes
self.repr_dim = output_dim # encoder output dimension
# need to make sense later
self.encoder = VisionTransformer(
image_size=64,
patch_size=4,
in_channels=2,
embed_dim=256,
num_heads=8,
mlp_dim=512,
num_layers=6,
num_classes=256
)
def forward(self, states, actions):
"""
Args:
states: [B, T, Ch, H, W]
actions: [B, T-1, 2]
Output:
predictions: [B, T, D]
"""
B, T, C, H, W = states.size()
states = states.view(B * T, C, H, W)
embeddings = self.encoder(states)
embeddings = embeddings.view(B, T, -1)
return embeddings
class Prober(torch.nn.Module):
def __init__(
self,
embedding: int,
arch: str,
output_shape: List[int],
):
super().__init__()
self.output_dim = np.prod(output_shape)
self.output_shape = output_shape
self.arch = arch
arch_list = list(map(int, arch.split("-"))) if arch != "" else []
f = [embedding] + arch_list + [self.output_dim]
layers = []
for i in range(len(f) - 2):
layers.append(torch.nn.Linear(f[i], f[i + 1]))
layers.append(torch.nn.ReLU(True))
layers.append(torch.nn.Linear(f[-2], f[-1]))
self.prober = torch.nn.Sequential(*layers)
def forward(self, e):
output = self.prober(e)
return output