-
Notifications
You must be signed in to change notification settings - Fork 2
/
model.py
724 lines (620 loc) · 29 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
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
import matplotlib.pyplot as plt
import numpy as np
import pytorch_lightning as pl
import torch
import wandb
import pydantic
from dataclasses import dataclass
from einops import rearrange
from torchmetrics import Accuracy, MeanMetric
from typing import Optional, List, Union
from collections import namedtuple
from enum import Enum
class ActionType(Enum):
LEFT_CLICK = "left_click"
RIGHT_CLICK = "right_click"
DOUBLE_CLICK = "double_click"
END = "end"
CursorPosition = namedtuple("CursorPosition", ("x", "y"))
Resolution = namedtuple("Resolution", ("width", "height"))
@pydantic.dataclasses.dataclass
class UIActModelConfig:
# TextEncoder
text_conditioned: bool
text_num_embeddings: int
text_max_length: int
text_hidden_dim: int
text_num_heads: int
text_ffn_dim: int
text_num_layers: int
text_dropout_p: float
# FrameEncoder
frame_input_channels: int
frame_resolution: Resolution
frame_base_feature_maps: int
frame_residual_layers: int
frame_feature_maps: int
# ActionDecoder
action_max_length: int
action_hidden_dim: int
action_num_heads: int
action_ffn_dim: int
action_num_layers: int
action_dropout_p: float
# Heads
event_classes: list
@dataclass
class UIActModelInput:
frames: torch.Tensor
frames_attention_mask: torch.Tensor
text: torch.Tensor = None
text_attention_mask: torch.Tensor = None
target_events: torch.Tensor = None
target_cursor_positions: torch.Tensor = None
@dataclass
class UIActModelOutput:
event_logits: torch.Tensor
cursor_position_logits: torch.Tensor
hidden_states: Union[torch.Tensor, List[torch.Tensor]]
text_hidden_states: Union[torch.Tensor, List[torch.Tensor]]
loss: Optional[torch.Tensor]
event_loss: Optional[torch.Tensor]
cursor_position_loss: Optional[torch.Tensor]
# Credit: https://github.com/bzhangGo/rmsnorm/blob/master/rmsnorm_torch.py
class RMSNorm(torch.nn.Module):
def __init__(self, d, p=-1., eps=1e-8, bias=False):
"""
Root Mean Square Layer Normalization
:param d: model size
:param p: partial RMSNorm, valid value [0, 1], default -1.0 (disabled)
:param eps: epsilon value, default 1e-8
:param bias: whether use bias term for RMSNorm, disabled by
default because RMSNorm doesn't enforce re-centering invariance.
"""
super(RMSNorm, self).__init__()
self.eps = eps
self.d = d
self.p = p
self.bias = bias
self.scale = torch.nn.Parameter(torch.ones(d))
self.register_parameter("scale", self.scale)
if self.bias:
self.offset = torch.nn.Parameter(torch.zeros(d))
self.register_parameter("offset", self.offset)
def forward(self, x):
if self.p < 0. or self.p > 1.:
norm_x = x.norm(2, dim=-1, keepdim=True)
d_x = self.d
else:
partial_size = int(self.d * self.p)
partial_x, _ = torch.split(x, [partial_size, self.d - partial_size], dim=-1)
norm_x = partial_x.norm(2, dim=-1, keepdim=True)
d_x = partial_size
rms_x = norm_x * d_x ** (-1. / 2)
x_normed = x / (rms_x + self.eps)
if self.bias:
return self.scale * x_normed + self.offset
return self.scale * x_normed
class MultiHeadAttention(torch.nn.Module):
def __init__(
self,
v_tot_dim: int, # the dim of v, total for all heads
hidden_dim: int,
dropout_p: float
):
super().__init__()
self.attn_dropout = torch.nn.Dropout(dropout_p)
self.resid_dropout = torch.nn.Dropout(dropout_p)
self.final_proj = torch.nn.Linear(in_features=v_tot_dim, out_features=hidden_dim, bias=False)
def forward(self, q, k, v, attention_mask):
"""
Args:
q: [..., heads, q_len, qk_dim]
k: [..., heads, kv_len, qk_dim]
v: [..., heads, kv_len, v_dim]
attention_mask: [..., heads, q_len, kv_len]
"""
attention_scores = q @ k.swapdims(-1, -2) # - [..., heads, q_len, kv_len]
attention_scores = attention_scores + attention_mask
attention_scores = torch.nn.functional.softmax(attention_scores, dim=-1)
attention_scores = self.attn_dropout(attention_scores)
output = attention_scores @ v # [..., heads, q_len, v_dim]
# Concat heads
output = rearrange(output, "... h l d -> ... l (h d)")
# Final linear projection
output = self.final_proj(output) # [..., q_len, hidden_dim]
# Final dropout
output = self.resid_dropout(output)
return output, attention_scores
class TextEncoderSelfAttention(torch.nn.Module):
def __init__(self, config: UIActModelConfig):
super().__init__()
self.config = config
self.pre_norm = RMSNorm(config.text_hidden_dim)
self.qkv_proj = torch.nn.Linear(config.text_hidden_dim, config.text_hidden_dim * 3, bias=False)
self.attention = MultiHeadAttention(config.text_hidden_dim, config.text_hidden_dim, config.text_dropout_p)
def forward(
self,
hidden_states,
attention_mask
):
"""
Args:
hidden_states: [batch, seq_len, hidden_dim]
attention_mask: [batch, seq_len]
"""
hidden_states = self.pre_norm(hidden_states)
q, k, v = rearrange(self.qkv_proj(hidden_states), "b l (i h d) -> i b h l d", i=3, h=self.config.text_num_heads)
attention_mask = rearrange(attention_mask, "b l -> b 1 1 l") # Add broadcast dimensions for heads and queries
attention_mask = (1.0 - attention_mask) * -10000.0 # Add -10000 on masked positions in score matrix
hidden_states, attention_scores = self.attention(q, k, v, attention_mask)
return hidden_states
class TextEncoderBlock(torch.nn.Module):
def __init__(self, config: UIActModelConfig):
super().__init__()
self.self_attention = TextEncoderSelfAttention(config)
self.ffn = torch.nn.Sequential(
RMSNorm(config.text_hidden_dim),
torch.nn.Linear(config.text_hidden_dim, config.text_ffn_dim),
torch.nn.GELU(),
torch.nn.Linear(config.text_ffn_dim, config.text_hidden_dim)
)
def forward(
self,
hidden_states,
attention_mask
):
"""
Args:
hidden_states: [batch, seq_len, hidden_dim]
attention_mask: [batch, seq_len]
"""
# Self attention
hidden_states = hidden_states + self.self_attention(
hidden_states,
attention_mask
)
# Feed-forward network
hidden_states = hidden_states + self.ffn(
hidden_states
)
return hidden_states
class TextEncoder(torch.nn.Module):
def __init__(self, config: UIActModelConfig):
super().__init__()
self.token_embeddings = torch.nn.Embedding(config.text_num_embeddings, config.text_hidden_dim)
self.pos_embeddings = torch.nn.Embedding(config.text_max_length, config.text_hidden_dim)
self.blocks = torch.nn.ModuleList([
TextEncoderBlock(config)
for _ in range(config.text_num_layers)
])
def forward(
self,
input_ids,
attention_mask,
return_all_hidden_states: bool=False
):
"""
Args:
input_ids: [batch, seq_len]
attention_mask: [batch, seq_len]
"""
hidden_states = self.token_embeddings(input_ids) # [batch, seq_len, hidden_dim]
hidden_states += self.pos_embeddings(
torch.arange(0, input_ids.shape[1], device=hidden_states.device)
)
if return_all_hidden_states:
hidden_states = [hidden_states]
for block in self.blocks:
out = block(
hidden_states if not return_all_hidden_states else hidden_states[-1],
attention_mask
)
if return_all_hidden_states:
hidden_states.append(out)
else:
hidden_states = out
return hidden_states
class FrameEncoderResidualBlock(torch.nn.Module):
def __init__(self, channels):
super().__init__()
self.conv1 = torch.nn.Conv3d(channels, channels, (1, 3, 3), stride=(1, 1, 1), padding=(0, 2, 2), dilation=(1, 2, 2), bias=False)
self.bn1 = torch.nn.BatchNorm3d(channels)
self.relu = torch.nn.ReLU(inplace=True)
self.conv2 = torch.nn.Conv3d(channels, channels, (1, 3, 3), stride=(1, 1, 1), padding=(0, 1, 1), bias=False)
self.bn2 = torch.nn.BatchNorm3d(channels)
def forward(self, x):
identity = x
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x = self.conv2(x)
x = self.bn2(x)
x = x + identity # Residual connection
x = self.relu(x)
return x
class FrameEncoder(torch.nn.Module):
def __init__(self, config: UIActModelConfig):
super().__init__()
num_downsample_layers = 4
self.base = torch.nn.Sequential(
torch.nn.Conv3d(config.frame_input_channels, config.frame_base_feature_maps, (1, 7, 7), stride=(1, 2, 2), padding=(0, 3, 3), bias=False),
torch.nn.BatchNorm3d(config.frame_base_feature_maps),
torch.nn.ReLU(inplace=True),
torch.nn.MaxPool3d((1, 3, 3), stride=(1, 2, 2), padding=(0, 1, 1))
)
self.residual_blocks = torch.nn.Sequential(*(
FrameEncoderResidualBlock(config.frame_base_feature_maps)
for _ in range(config.frame_residual_layers)
))
self.downsample_layers = torch.nn.ModuleList([
torch.nn.Sequential(
torch.nn.Conv3d(
config.frame_base_feature_maps if layer_idx == 0 else config.frame_feature_maps,
config.frame_feature_maps,
kernel_size=(1, 3, 3),
stride=(1, 2, 2),
padding=(0, 1, 1),
bias=False
),
torch.nn.BatchNorm3d(config.frame_feature_maps),
torch.nn.ReLU(inplace=True)
)
for layer_idx in range(num_downsample_layers)
])
self.avgpool = torch.nn.AdaptiveAvgPool2d(output_size=(1, 1))
def forward(self, frames):
"""
Args:
frames: [batch, num_frames, height, width, input_channels]
Returns:
output: [batch, num_frames, feature_maps]
base_output: [batch, num_frames, height/4, width/4, feature_maps]
"""
x = frames.type(torch.float32) / 255.0 - 0.5
x = rearrange(x, "b t h w c -> b c t h w")
x = self.base(x)
residual_output = self.residual_blocks(x)
x = residual_output
for layer in self.downsample_layers:
x = layer(x)
x = self.avgpool(x)
x = rearrange(x, "b c t 1 1 -> b t c")
return x, rearrange(residual_output, "b c t h w -> b t h w c")
class ActionDecoderSelfAttention(torch.nn.Module):
def __init__(self, config: UIActModelConfig):
super().__init__()
self.config = config
self.pre_norm = RMSNorm(config.action_hidden_dim)
self.qkv_proj = torch.nn.Linear(config.action_hidden_dim, config.action_hidden_dim * 3, bias=False)
self.attention = MultiHeadAttention(config.action_hidden_dim, config.action_hidden_dim, config.action_dropout_p)
def forward(self, hidden_states, attention_mask):
"""
Args:
hidden_states: [batch, seq_len, hidden_dim]
attention_mask: [batch, seq_len]
"""
hidden_states = self.pre_norm(hidden_states)
q, k, v = rearrange(self.qkv_proj(hidden_states), "b l (i h d) -> i b h l d", i=3, h=self.config.action_num_heads)
attention_mask = rearrange(attention_mask, "b l -> b 1 1 l") # Add broadcast dimensions for heads and queries
attention_mask = (1.0 - attention_mask) * -10000.0 # Add -10000 on masked positions in score matrix
# Apply causal mask
length = q.size(-2)
causal_mask = (1.0 - torch.tril(torch.ones((length, length), dtype=torch.uint8, device=attention_mask.device)).view(1, 1, length, length)) * -10000.0
attention_mask = attention_mask + causal_mask
hidden_states, attention_scores = self.attention(q, k, v, attention_mask)
return hidden_states
class ActionDecoderCrossAttention(torch.nn.Module):
def __init__(self, hidden_dim: int, ca_hidden_dim: int, num_heads: int, dropout_p: float):
super().__init__()
self.num_heads = num_heads
self.pre_norm = RMSNorm(hidden_dim)
self.q_proj = torch.nn.Linear(hidden_dim, ca_hidden_dim, bias=False)
self.kv_proj = torch.nn.Linear(ca_hidden_dim, ca_hidden_dim * 2, bias=False)
self.attention = MultiHeadAttention(ca_hidden_dim, hidden_dim, dropout_p)
def forward(
self,
hidden_states,
ca_hidden_states,
ca_attention_mask
):
"""
Args:
hidden_states: [..., seq_len, hidden_dim]
ca_hidden_states: [..., ca_seq_len, ca_hidden_dim]
ca_attention_mask: [..., ca_seq_len]
"""
hidden_states = self.pre_norm(hidden_states)
q = rearrange(self.q_proj(hidden_states), "... l (h d) -> ... h l d", h=self.num_heads)
k, v = rearrange(self.kv_proj(ca_hidden_states), "... l (i h d) -> i ... h l d", i=2, h=self.num_heads)
ca_attention_mask = rearrange(ca_attention_mask, "... l -> ... 1 1 l") # Add broadcast dimensions for heads and queries
ca_attention_mask = (1.0 - ca_attention_mask) * -10000.0 # Add -10000 on masked positions in score matrix
hidden_states, attention_scores = self.attention(q, k, v, ca_attention_mask)
return hidden_states
class ActionDecoderBlock(torch.nn.Module):
def __init__(self, config: UIActModelConfig):
super().__init__()
self.config = config
self.self_attention = ActionDecoderSelfAttention(config)
if config.text_conditioned:
self.text_cross_attention = ActionDecoderCrossAttention(
hidden_dim=config.action_hidden_dim,
ca_hidden_dim=config.text_hidden_dim,
num_heads=config.action_num_heads,
dropout_p=config.action_dropout_p
)
self.frame_cross_attention = ActionDecoderCrossAttention(
hidden_dim=config.action_hidden_dim,
ca_hidden_dim=config.frame_base_feature_maps,
num_heads=config.action_num_heads,
dropout_p=config.action_dropout_p
)
self.ffn = torch.nn.Sequential(
RMSNorm(config.action_hidden_dim),
torch.nn.Linear(config.action_hidden_dim, config.action_ffn_dim),
torch.nn.GELU(),
torch.nn.Linear(config.action_ffn_dim, config.action_hidden_dim)
)
def forward(
self,
hidden_states,
attention_mask,
encoded_text,
text_attention_mask,
frame_feature_maps,
):
"""
Args:
hidden_states: [batch, num_frames, frame_dim]
attention_mask: [batch, num_frames]
encoded_text: [batch, text_len, text_dim]
text_attention_mask: [batch, text_len]
frame_feature_maps: [batch, num_frames, h*w, feature_maps]
"""
hidden_states = hidden_states + self.self_attention(
hidden_states,
attention_mask
)
if self.config.text_conditioned:
hidden_states = hidden_states + self.text_cross_attention(
hidden_states,
encoded_text,
text_attention_mask
)
hidden_states = hidden_states + self.frame_cross_attention(
hidden_states=rearrange(hidden_states, "b l d -> b l 1 d"),
ca_hidden_states=frame_feature_maps,
ca_attention_mask=torch.ones(frame_feature_maps.shape[:3], device=hidden_states.device)
).squeeze(2)
hidden_states = hidden_states + self.ffn(
hidden_states
)
return hidden_states
class UIActModel(torch.nn.Module):
def __init__(self, config: UIActModelConfig):
super().__init__()
self.config = config
self.text_encoder = TextEncoder(config) if config.text_conditioned else None
self.frame_encoder = FrameEncoder(config)
#self.frame_feature_maps_abs_pos_encodings = torch.nn.Parameter(
# torch.randn((1, 1, config.frame_resolution.height//4, config.frame_resolution.width//4, config.frame_base_feature_maps)) * 0.01
#)
self.frame_feature_maps_pos_encodings = torch.nn.Parameter(
torch.randn((1, 1, config.frame_resolution.height//4, config.frame_resolution.width//4, config.frame_base_feature_maps)) * 0.01
)
#self.frame_proj = torch.nn.Linear(config.frame_feature_maps + config.frame_base_feature_maps + len(config.event_classes), config.action_hidden_dim, bias=False)
self.frame_proj = torch.nn.Linear(config.frame_feature_maps, config.action_hidden_dim, bias=False)
self.action_pos_embeddings = torch.nn.Parameter(torch.randn(1, config.action_max_length, config.action_hidden_dim) * 0.01)
self.action_decoder = torch.nn.ModuleList([
ActionDecoderBlock(config)
for _ in range(config.action_num_layers)
])
self.event_head = torch.nn.Linear(config.action_hidden_dim, len(config.event_classes), bias=False)
self.cursor_position_head = torch.nn.Linear(config.action_hidden_dim, config.frame_base_feature_maps, bias=False)
self._left_click_idx = self.config.event_classes.index(ActionType.LEFT_CLICK.value) if ActionType.LEFT_CLICK.value in self.config.event_classes else -1
self._right_click_idx = self.config.event_classes.index(ActionType.RIGHT_CLICK.value) if ActionType.RIGHT_CLICK.value in self.config.event_classes else -1
def _get_cursor_position_target_indices(self, target_cursor_positions, frames_width, target_events, frames_attention_mask) -> torch.Tensor:
target_cursor_positions = (target_cursor_positions / 4).round().long() # get coordinates in feature maps
feature_maps_width = int(frames_width / 4)
targets = target_cursor_positions[:, :, 1] * feature_maps_width + target_cursor_positions[:, :, 0] # get flat height x width index
targets[((target_events != self._left_click_idx) & (target_events != self._right_click_idx)) | (frames_attention_mask == 0)] = -100 # Only apply for click events
return targets
def forward(
self,
frames,
frames_attention_mask,
text=None,
text_attention_mask=None,
target_events=None,
target_cursor_positions=None,
return_all_hidden_states: bool=False,
return_all_text_hidden_states: bool=False
):
"""
Args:
frames: [batch, num_frames, height, width, input_channels]
frames_attention_mask: [batch, num_frames]
text: [batch, text_len]
text_attention_mask: [batch, text_len]
target_events: [batch, num_frames]
target_cursor_positions: [batch, num_frames, 2]
"""
# Encode text (optionally)
if self.config.text_conditioned:
encoded_text = self.text_encoder(
text,
text_attention_mask,
return_all_hidden_states=return_all_text_hidden_states
)
# encoded_text: [batch, text_len, text_dim] (potentially list of tensors)
else:
encoded_text = None
# Encode frames
encoded_frames, frame_feature_maps = self.frame_encoder(frames)
# encoded_frames [batch, num_frames, frame_dim]
# frame_feature_maps [batch, num_frames, height/4, width/4, frame_base_feature_maps]
# Add absolute positional embeddings
frame_feature_maps = frame_feature_maps + self.frame_feature_maps_pos_encodings
frame_feature_maps = rearrange(frame_feature_maps, "b l h w d -> b l (h w) d")
frame_reps = encoded_frames
# Action decoder
hidden_states = self.frame_proj(frame_reps)
hidden_states = hidden_states + self.action_pos_embeddings[:, :hidden_states.shape[1], :]
if return_all_hidden_states:
hidden_states = [hidden_states]
for layer in self.action_decoder:
out = layer(
hidden_states if not return_all_hidden_states else hidden_states[-1],
frames_attention_mask,
encoded_text if not return_all_text_hidden_states else encoded_text[-1],
text_attention_mask,
frame_feature_maps
)
if return_all_hidden_states:
hidden_states.append(out)
else:
hidden_states = out
event_logits = self.event_head(hidden_states if not return_all_hidden_states else hidden_states[-1])
# event_logits: [batch, num_frames, num_events]
cursor_pos_logits = self.cursor_position_head(hidden_states if not return_all_hidden_states else hidden_states[-1])
cursor_pos_logits = frame_feature_maps @ cursor_pos_logits[:, :, :, None]
cursor_pos_logits = rearrange(cursor_pos_logits, "b l hw 1 -> b l hw")
# cursor_pos_logits [batch, num_frames, frame_base_feature_maps]
event_loss = None
cursor_position_loss = None
# Event classification head
if target_events is not None:
event_loss = torch.nn.functional.cross_entropy(
event_logits.view(-1, event_logits.shape[-1]),
target_events.view(-1)
)
# Cursor position head
if target_cursor_positions is not None:
targets = self._get_cursor_position_target_indices(
target_cursor_positions=target_cursor_positions,
frames_width=frames.shape[3],
target_events=target_events,
frames_attention_mask=frames_attention_mask
)
cursor_position_loss = torch.nn.functional.cross_entropy(
cursor_pos_logits.view(-1, cursor_pos_logits.shape[-1]),
targets.view(-1)
)
return UIActModelOutput(
event_logits=event_logits,
cursor_position_logits=cursor_pos_logits,
hidden_states=hidden_states,
text_hidden_states=encoded_text,
event_loss=event_loss,
cursor_position_loss=cursor_position_loss,
loss=((event_loss or 0) + (cursor_position_loss or 0)) or None,
)
class UIActLightningModel(UIActModel, pl.LightningModule):
def __init__(self, config: UIActModelConfig):
super().__init__(config)
self.save_hyperparameters()
num_cursor_pos = config.frame_resolution.width * config.frame_resolution.height // (4 * 4)
self.train_event_accuracy = Accuracy("multiclass", num_classes=2, ignore_index=-100)
self.train_cursor_pos_accuracy = Accuracy("multiclass", num_classes=num_cursor_pos, ignore_index=-100)
self.val_event_accuracy = Accuracy("multiclass", num_classes=2, ignore_index=-100)
self.val_cursor_pos_accuracy = Accuracy("multiclass", num_classes=num_cursor_pos, ignore_index=-100)
self.val_l2_error = MeanMetric()
self.validation_step_outputs = []
def configure_optimizers(self):
return torch.optim.Adam(self.parameters(), lr=1e-4)
def training_step(self, batch: UIActModelInput, _):
output = self.forward(
frames=batch.frames,
frames_attention_mask=batch.frames_attention_mask,
text=batch.text,
text_attention_mask=batch.text_attention_mask,
target_events=batch.target_events,
target_cursor_positions=batch.target_cursor_positions
)
self.train_event_accuracy(
output.event_logits.view(-1, output.event_logits.shape[-1]),
batch.target_events.view(-1)
)
self.log("train/event_accuracy", self.train_event_accuracy, on_epoch=True, on_step=False, sync_dist=True)
self.train_cursor_pos_accuracy(
output.cursor_position_logits.view(-1, output.cursor_position_logits.shape[-1]),
self._get_cursor_position_target_indices(batch.target_cursor_positions, batch.frames.shape[3], batch.target_events, batch.frames_attention_mask).view(-1)
)
self.log("train/cursor_position_accuracy", self.train_cursor_pos_accuracy, on_epoch=True, on_step=False, sync_dist=True)
self.log("train/event_loss", output.event_loss)
self.log("train/cursor_position_loss", output.cursor_position_loss)
self.log("train/loss", output.loss)
return output.loss
def validation_step(self, batch: UIActModelInput, batch_idx):
output = self.forward(
frames=batch.frames,
frames_attention_mask=batch.frames_attention_mask,
text=batch.text,
text_attention_mask=batch.text_attention_mask,
target_events=batch.target_events,
target_cursor_positions=batch.target_cursor_positions
)
# log predicted cursor_position
if batch_idx == 0 and self.logger is not None:
self.log_cursor_position_predictions(batch, output)
# l2_error
pred_pos_flat = output.cursor_position_logits.argmax(dim=-1) # [batch, num_frames]
pred_pos = torch.zeros(
(pred_pos_flat.shape[0], pred_pos_flat.shape[1], 2),
dtype=pred_pos_flat.dtype,
device=pred_pos_flat.device
)
width = self.config.frame_resolution.width // 4
pred_pos[..., 0] = pred_pos_flat % width
pred_pos[..., 1] = pred_pos_flat // width
l2_error = torch.sqrt(torch.sum((batch.target_cursor_positions/4 - pred_pos) ** 2, dim=-1))
# we only want to compute l2_error on click frames
click_frames = (((batch.target_events == self._left_click_idx) | (batch.target_events == self._right_click_idx)) & batch.frames_attention_mask).type(torch.bool)
non_click_frames = torch.bitwise_not(click_frames)
# set l2_error = -1 for frames that are not click_frames
l2_error[non_click_frames] = -1
self.val_l2_error.update(l2_error[click_frames])
self.log("val/l2_error", self.val_l2_error, on_epoch=True)
self.val_event_accuracy(
output.event_logits.view(-1, output.event_logits.shape[-1]),
batch.target_events.view(-1)
)
self.log("val/event_accuracy", self.val_event_accuracy)
self.val_cursor_pos_accuracy(
output.cursor_position_logits.view(-1, output.cursor_position_logits.shape[-1]),
self._get_cursor_position_target_indices(batch.target_cursor_positions, batch.frames.shape[3], batch.target_events, batch.frames_attention_mask).view(-1)
)
self.log("val/cursor_position_accuracy", self.val_cursor_pos_accuracy)
self.log("val/event_loss", output.event_loss, sync_dist=True)
self.log("val/cursor_position_loss", output.cursor_position_loss, sync_dist=True)
self.log("val/loss", output.loss, sync_dist=True)
self.validation_step_outputs.append((output, l2_error))
return output, l2_error
def on_validation_epoch_end(self):
if self.logger is not None:
all_l2_errors = []
for _, l2_error in self.validation_step_outputs:
all_l2_errors.append(l2_error[l2_error >= 0.0])
all_l2_errors = torch.concatenate(all_l2_errors).detach().cpu().numpy()
self.logger.experiment.log({"val/l2_error_dist": wandb.Histogram(all_l2_errors)}, commit=False)
self.validation_step_outputs.clear()
def log_cursor_position_predictions(self, batch: UIActModelInput, output: UIActModelOutput):
click_pos = torch.nn.functional.softmax(output.cursor_position_logits[0], dim=-1).cpu().detach().numpy()
click_pos = click_pos.reshape(click_pos.shape[0], self.config.frame_resolution.height//4, self.config.frame_resolution.width//4)
figs = []
for i in range(batch.frames.shape[1]):
fig = plt.figure(figsize=(10,6))
ax = fig.add_axes([0, 0, 1, 1])
ax.axis('off')
alpha = np.kron(click_pos[i], np.ones((4, 4)))[:, :, None]
alpha /= alpha.max()
alpha = alpha * 0.8 + 0.2
frame = batch.frames[0, i, :, :, :3].cpu().detach().numpy() / 255.
frame = np.concatenate([frame, alpha], axis=-1)
ax.imshow(np.zeros((batch.frames.shape[2], batch.frames.shape[3], 3), dtype=np.byte))
ax.imshow(frame)
ax.scatter(batch.target_cursor_positions[0, i, 0].item(), batch.target_cursor_positions[0, i, 1].item(), s=10, c='red', marker='o')
figs.append(fig)
self.logger.experiment.log({f"val/frame_{i}": fig for i, fig in enumerate(figs)}, commit=False)
for fig in figs:
plt.close(fig)