-
Notifications
You must be signed in to change notification settings - Fork 13
/
utils.py
234 lines (178 loc) · 7.55 KB
/
utils.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
from importlib import metadata
import json
import os
import re
from abc import ABC
from typing import List, Tuple, Optional
import numpy as np
import torch
import torchaudio
import torchvision
from einops import rearrange
from pytorch_lightning import Callback, Trainer, LightningModule
from torch import Tensor
from torch.nn import functional as F, Module
def read_json(path: str, object_hook=None):
with open(path, 'r') as f:
return json.load(f, object_hook=object_hook)
def read_video(path: str):
video, audio, info = torchvision.io.read_video(path, pts_unit="sec")
video = video.permute(0, 3, 1, 2) / 255
audio = audio.permute(1, 0)
return video, audio, info
def read_audio(path: str):
return torchaudio.load(path)
def read_image(path: str):
return torchvision.io.read_image(path).float() / 255.0
def padding_video(tensor: Tensor, target: int, padding_method: str = "zero", padding_position: str = "tail") -> Tensor:
t, c, h, w = tensor.shape
padding_size = target - t
pad = _get_padding_pair(padding_size, padding_position)
if padding_method == "zero":
return F.pad(tensor, pad=[0, 0, 0, 0, 0, 0] + pad)
elif padding_method == "same":
tensor = rearrange(tensor, "t c h w -> c h w t")
tensor = F.pad(tensor, pad=pad + [0, 0], mode="replicate")
return rearrange(tensor, "c h w t -> t c h w")
else:
raise ValueError("Wrong padding method. It should be zero or tail or average.")
def padding_audio(tensor: Tensor, target: int,
padding_method: str = "zero",
padding_position: str = "tail"
) -> Tensor:
t, c = tensor.shape
padding_size = target - t
pad = _get_padding_pair(padding_size, padding_position)
if padding_method == "zero":
return F.pad(tensor, pad=[0, 0] + pad)
elif padding_method == "same":
tensor = rearrange(tensor, "t c -> 1 c t")
tensor = F.pad(tensor, pad=pad, mode="replicate")
return rearrange(tensor, "1 c t -> t c")
else:
raise ValueError("Wrong padding method. It should be zero or tail or average.")
def _get_padding_pair(padding_size: int, padding_position: str) -> List[int]:
if padding_position == "tail":
pad = [0, padding_size]
elif padding_position == "head":
pad = [padding_size, 0]
elif padding_position == "average":
padding_head = padding_size // 2
padding_tail = padding_size - padding_head
pad = [padding_head, padding_tail]
else:
raise ValueError("Wrong padding position. It should be zero or tail or average.")
return pad
def resize_video(tensor: Tensor, size: Tuple[int, int], resize_method: str = "bicubic") -> Tensor:
return F.interpolate(tensor, size=size, mode=resize_method)
class _ConvNd(Module, ABC):
def __init__(self, in_channels: int, out_channels: int, kernel_size: int, stride: int = 1, padding: int = 0,
build_activation: Optional[callable] = None
):
super().__init__()
self.conv = self.PtConv(
in_channels, out_channels, kernel_size, stride=stride, padding=padding
)
if build_activation is not None:
self.activation = build_activation()
else:
self.activation = None
def forward(self, x: Tensor) -> Tensor:
x = self.conv(x)
if self.activation is not None:
x = self.activation(x)
return x
class Conv1d(_ConvNd):
PtConv = torch.nn.Conv1d
class Conv2d(_ConvNd):
PtConv = torch.nn.Conv2d
class Conv3d(_ConvNd):
PtConv = torch.nn.Conv3d
def iou_with_anchors(anchors_min, anchors_max, box_min, box_max):
"""Compute jaccard score between a box and the anchors."""
len_anchors = anchors_max - anchors_min
int_xmin = np.maximum(anchors_min, box_min)
int_xmax = np.minimum(anchors_max, box_max)
inter_len = np.maximum(int_xmax - int_xmin, 0.)
union_len = len_anchors - inter_len + box_max - box_min
iou = inter_len / union_len
return iou
def ioa_with_anchors(anchors_min, anchors_max, box_min, box_max):
# calculate the overlap proportion between the anchor and all bbox for supervise signal,
# the length of the anchor is 0.01
len_anchors = anchors_max - anchors_min
int_xmin = np.maximum(anchors_min, box_min)
int_xmax = np.minimum(anchors_max, box_max)
inter_len = np.maximum(int_xmax - int_xmin, 0.)
scores = np.divide(inter_len, len_anchors)
return scores
def iou_1d(proposal, target) -> Tensor:
"""
Calculate 1D IOU for N proposals with L labels.
Args:
proposal (:class:`~torch.Tensor` | :class:`~numpy.ndarray`): The predicted array with [M, 2]. First column is
beginning, second column is end.
target (:class:`~torch.Tensor` | :class:`~numpy.ndarray`): The label array with [N, 2]. First column is
beginning, second column is end.
Returns:
:class:`~torch.Tensor`: The iou result with [M, N].
"""
if type(proposal) is np.ndarray:
proposal = torch.from_numpy(proposal)
if type(target) is np.ndarray:
target = torch.from_numpy(target)
proposal_begin = proposal[:, 0].unsqueeze(0).T
proposal_end = proposal[:, 1].unsqueeze(0).T
target_begin = target[:, 0]
target_end = target[:, 1]
inner_begin = torch.maximum(proposal_begin, target_begin)
inner_end = torch.minimum(proposal_end, target_end)
outer_begin = torch.minimum(proposal_begin, target_begin)
outer_end = torch.maximum(proposal_end, target_end)
inter = torch.clamp(inner_end - inner_begin, min=0.)
union = outer_end - outer_begin
return inter / union
class LrLogger(Callback):
"""Log learning rate in each epoch start."""
def on_train_epoch_start(self, trainer: Trainer, pl_module: LightningModule) -> None:
for i, optimizer in enumerate(trainer.optimizers):
for j, params in enumerate(optimizer.param_groups):
key = f"opt{i}_lr{j}"
value = params["lr"]
pl_module.logger.log_metrics({key: value}, step=trainer.global_step)
pl_module.log(key, value, logger=False, sync_dist=pl_module.distributed)
class EarlyStoppingLR(Callback):
"""Early stop model training when the LR is lower than threshold."""
def __init__(self, lr_threshold: float, mode="all"):
self.lr_threshold = lr_threshold
if mode in ("any", "all"):
self.mode = mode
else:
raise ValueError(f"mode must be one of ('any', 'all')")
def on_train_epoch_start(self, trainer: Trainer, pl_module: LightningModule) -> None:
self._run_early_stop_checking(trainer)
def _run_early_stop_checking(self, trainer: Trainer) -> None:
metrics = trainer._logger_connector.callback_metrics
if len(metrics) == 0:
return
all_lr = []
for key, value in metrics.items():
if re.match(r"opt\d+_lr\d+", key):
all_lr.append(value)
if len(all_lr) == 0:
return
if self.mode == "all":
if all(lr <= self.lr_threshold for lr in all_lr):
trainer.should_stop = True
elif self.mode == "any":
if any(lr <= self.lr_threshold for lr in all_lr):
trainer.should_stop = True
def generate_metadata_min(data_root: str):
metadata_full = read_json(os.path.join(data_root, "metadata.json"))
metadata_min = []
for meta in metadata_full:
del meta["timestamps"]
del meta["transcript"]
metadata_min.append(meta)
with open(os.path.join(data_root, "metadata.min.json"), "w") as f:
json.dump(metadata_min, f)