File size: 14,658 Bytes
c99d198 | 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 |
"""Self supervised models."""
import abc
import math
from typing import List, Union
import dataclasses
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torchvision import models
from torchvision.models.resnet import BasicBlock
from torchvision.models.resnet import ResNet
from torch.utils.model_zoo import load_url as load_state_dict_from_url
from transformers import CLIPTextModel, CLIPTokenizer
from .imagen import PerceiverResampler
@dataclasses.dataclass
class SelfSupervisedOutput:
"""The output of a self-supervised model."""
frames: Union[np.ndarray, torch.FloatTensor]
feats: Union[np.ndarray, torch.FloatTensor]
embs: Union[np.ndarray, torch.FloatTensor]
def squeeze(self, dim):
kwargs = {}
for k, v in dataclasses.asdict(self).items():
kwargs[k] = v.squeeze(dim)
return self.__class__(**kwargs)
def cpu(self):
kwargs = {}
for k, v in dataclasses.asdict(self).items():
kwargs[k] = v.cpu()
return self.__class__(**kwargs)
def numpy(self):
kwargs = {}
for k, v in dataclasses.asdict(self).items():
if k != "frames":
kwargs[k] = v.cpu().detach().numpy()
kwargs["frames"] = self.frames.permute(0, 2, 3, 1).cpu().detach().numpy()
return self.__class__(**kwargs)
@classmethod
def merge(
cls,
output_list,
):
kwargs = {}
for k in dataclasses.asdict(output_list[0]).keys():
kwargs[k] = torch.cat([getattr(o, k) for o in output_list], dim=1)
return cls(**kwargs)
class SelfSupervisedModel(nn.Module, abc.ABC):
"""A self-supervised model trained on video data."""
@abc.abstractmethod
def __init__(
self,
num_ctx_frames,
normalize_embeddings,
learnable_temp,
):
super().__init__()
self.num_ctx_frames = num_ctx_frames
self.normalize_embeddings = normalize_embeddings
self.learnable_temp = learnable_temp
# Log-parameterized multiplicative softmax temperature param.
if learnable_temp:
self.logit_scale = nn.Parameter(torch.ones([]))
def forward(self, x):
"""Forward the video frames through the network.
Args:
x: The video frames of shape (B, T, C, H, W). If there are S video frames
and we are using X context frames, then T = S * X.
Returns:
An instance of SelfSupervisedOutput.
"""
batch_size, t, c, h, w = x.shape
x_flat = x.view((batch_size * t, c, h, w))
feats = self.backbone(x_flat)
feats_flat = torch.flatten(feats, 1)
embs = self.encoder(feats_flat)
if self.normalize_embeddings:
embs = embs / (embs.norm(dim=-1, keepdim=True) + 1e-7)
if self.learnable_temp:
logit_scale = self.logit_scale.exp()
embs = logit_scale * embs
embs = embs.view((batch_size, t, -1))
feats = feats.view((batch_size, t, -1))
return SelfSupervisedOutput(frames=x, feats=feats, embs=embs)
@torch.no_grad()
def infer(
self,
x,
max_batch_size = 128,
):
"""Forward at inference with possible very large batch sizes."""
# Figure out a max batch size that's a multiple of the number of context
# frames. This is so we can support large videos with many frames.
lcm = self.num_ctx_frames
effective_bs = math.floor(max_batch_size / lcm) * lcm
if x.shape[1] > effective_bs:
out = []
for i in range(math.ceil(x.shape[1] / effective_bs)):
sub_frames = x[:, i * effective_bs:(i + 1) * effective_bs]
out.append(self.forward(sub_frames).cpu())
out = SelfSupervisedOutput.merge(out)
else:
out = self.forward(x).cpu()
return out.squeeze(0)
class Resnet18LinearEncoderNet(SelfSupervisedModel):
"""A resnet18 backbone with a linear encoder head."""
def __init__(self, embedding_size, *args, **kwargs):
super().__init__(*args, **kwargs)
# Visual backbone.
resnet = models.resnet18(pretrained=True)
num_ftrs = resnet.fc.in_features
layers_ = list(resnet.children())[:-1]
self.backbone = nn.Sequential(*layers_)
# Encoder.
self.encoder = nn.Linear(num_ftrs, embedding_size)
class GoalClassifier(SelfSupervisedModel):
"""A resnet18 backbone with a binary classification head."""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Visual backbone.
resnet = models.resnet18(pretrained=True)
num_ftrs = resnet.fc.in_features
layers_ = list(resnet.children())[:-1]
self.backbone = nn.Sequential(*layers_)
# Classification head.
self.encoder = nn.Linear(num_ftrs, 1)
class Resnet18RawImageNetFeaturesNet(SelfSupervisedModel):
"""A resnet18 backbone with an identity encoder head."""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Visual backbone.
resnet = models.resnet18(pretrained=True)
layers_ = list(resnet.children())[:-1]
self.backbone = nn.Sequential(*layers_)
# Identity encoder.
self.encoder = nn.Identity()
class Upsampling(nn.Module):
"""Unet upsampling adapted from [1].
References:
[1]: https://github.com/milesial/Pytorch-UNet
"""
def __init__(self, in_channels, out_channels):
super().__init__()
self.up = nn.Upsample(scale_factor=2, mode="bilinear", align_corners=True)
self.conv = nn.Sequential(
nn.Conv2d(in_channels, in_channels // 2, kernel_size=3, padding=1),
nn.BatchNorm2d(in_channels // 2),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels // 2, out_channels, kernel_size=3, padding=1),
nn.BatchNorm2d(out_channels),
nn.ReLU(inplace=True),
)
def forward(self, x1, x2):
x1 = self.up(x1)
diffy = x2.size()[2] - x1.size()[2]
diffx = x2.size()[3] - x1.size()[3]
x1 = F.pad(x1,
[diffx // 2, diffx - diffx // 2, diffy // 2, diffy - diffy // 2])
x = torch.cat([x2, x1], dim=1)
return self.conv(x)
@dataclasses.dataclass
class SelfSupervisedReconOutput(SelfSupervisedOutput):
"""Self-supervised output with a reconstruction tensor."""
reconstruction: Union[np.ndarray, torch.FloatTensor]
def numpy(self):
kwargs = {}
for k, v in dataclasses.asdict(self).items():
if k != "frames" or k != "reconstruction":
kwargs[k] = v.cpu().detach().numpy()
kwargs["frames"] = self.frames.permute(0, 2, 3, 1).cpu().detach().numpy()
kwargs["reconstruction"] = self.reconstruction.permute(
0, 2, 3, 1).cpu().detach().numpy()
return self.__class__(**kwargs)
class Resnet18LinearEncoderAutoEncoderNet(ResNet):
"""Resnet18LinearEncoder with an auxiliary autoencoding path."""
def __init__(
self,
embedding_size,
num_ctx_frames,
normalize_embeddings,
learnable_temp,
):
super().__init__(BasicBlock, [2, 2, 2, 2])
self.num_ctx_frames = num_ctx_frames
self.normalize_embeddings = normalize_embeddings
self.learnable_temp = learnable_temp
# Load pretrained weights.
state_dict = load_state_dict_from_url(
"https://download.pytorch.org/models/resnet18-5c106cde.pth",
progress=True,
)
self.load_state_dict(state_dict)
# Embedding head.
self.fc = nn.Linear(self.fc.in_features, embedding_size)
# Upsampling path.
self.up1 = Upsampling(1024, 512 // 2)
self.up2 = Upsampling(512, 256 // 2)
self.up3 = Upsampling(256, 128 // 2)
self.up4 = Upsampling(128, 64)
self.out_conv = nn.Conv2d(64, 3, kernel_size=1)
# Log-parameterized multiplicative softmax temperature param.
if learnable_temp:
self.logit_scale = nn.Parameter(torch.ones([]))
def encode(self, x):
# Compute embeddings.
batch_size, t, c, h, w = x.shape
x = x.view((batch_size * t, c, h, w))
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x = self.maxpool(x)
x1 = self.layer1(x) # B, 64, 56, 56
x2 = self.layer2(x1) # B, 128, 28, 28
x3 = self.layer3(x2) # B, 256, 14, 14
x4 = self.layer4(x3) # B, 512, 7, 7
# Compute embeddings.
feats = self.avgpool(x4) # B, 512, 1, 1
flat_feats = torch.flatten(feats, 1)
embs = self.fc(flat_feats)
if self.normalize_embeddings:
embs = embs / (embs.norm(dim=-1, keepdim=True) + 1e-7)
if self.learnable_temp:
logit_scale = self.logit_scale.exp()
embs = logit_scale * embs
embs = embs.view((batch_size, t, -1))
return embs, [x1, x2, x3, x4, feats]
def decode_all_res(self, feature_maps):
"""Decode using all spatial resolutions, a la u-net."""
x1, x2, x3, x4, feats = feature_maps
x = self.up1(feats, x4)
x = self.up2(x, x3)
x = self.up3(x, x2)
x = self.up4(x, x1)
recon = self.out_conv(x)
return recon
def decode_lowest_res(self, feature_maps):
_, _, _, x, _ = feature_maps
for up_conv in self.up_convs:
x = F.relu(up_conv(x))
x = F.interpolate(
x,
scale_factor=2,
mode="bilinear",
recompute_scale_factor=False,
align_corners=True,
)
x = self.out_conv(x)
return X
def forward(self, x):
embs, feature_maps = self.encode(x)
recon = self.decode_all_res(feature_maps)
feats = feature_maps[-1]
feats = feats.view((embs.shape[0], embs.shape[1], *feats.shape[1:]))
recon = recon.view((embs.shape[0], embs.shape[1], *recon.shape[1:]))
return SelfSupervisedReconOutput(
frames=x,
feats=feats,
embs=embs,
reconstruction=recon,
)
@torch.no_grad()
def infer(
self,
x,
max_batch_size=128
):
"""Forward at inference with possible very large batch sizes."""
# Figure out a max batch size that's a multiple of the number of context
# frames. This is so we can support large videos with many frames.
lcm = self.num_ctx_frames
effective_bs = math.floor(max_batch_size / lcm) * lcm
if x.shape[1] > effective_bs:
out = []
for i in range(math.ceil(x.shape[1] / effective_bs)):
sub_frames = x[:, i * effective_bs:(i + 1) * effective_bs]
out.append(self.forward(sub_frames).cpu())
out = SelfSupervisedReconOutput.merge(out)
else:
out = self.forward(x).cpu()
return out.squeeze(0)
class Resnet18LinearEncoderAndTextEncoderNet(SelfSupervisedModel):
"""A resnet18 fused with text encoder backbone with a linear encoder head."""
def __init__(self, embedding_size, *args, **kwargs):
super().__init__(*args, **kwargs)
# Visual backbone.
resnet = models.resnet18(weights="ResNet18_Weights.DEFAULT")
num_ftrs = resnet.fc.in_features
layers_ = list(resnet.children())[:-1]
self.backbone = nn.Sequential(*layers_)
# Encoder.
self.encoder = nn.Linear(num_ftrs, embedding_size)
# Text encoder.
pretrained_model = "openai/clip-vit-base-patch32"
self.tokenizer = CLIPTokenizer.from_pretrained(pretrained_model)
self.text_encoder = CLIPTextModel.from_pretrained(pretrained_model)
self.text_encoder.requires_grad_(False)
self.text_encoder.eval()
self.task_attnpool = nn.Sequential(
PerceiverResampler(dim=num_ftrs, depth=2),
nn.Linear(num_ftrs, num_ftrs),
nn.ReLU(inplace=True),
)
def encode_batch_text(self, batch_text):
batch_text_ids = self.tokenizer(batch_text, return_tensors = 'pt', padding = True, truncation = True, max_length = 128).to('cuda')
batch_text_embed = self.text_encoder(**batch_text_ids).last_hidden_state
return batch_text_embed
def forward(self, x, task_txts):
"""Forward the video frames through the network.
Args:
x: The video frames of shape (B, T, C, H, W). If there are S video frames
and we are using X context frames, then T = S * X.
Returns:
An instance of SelfSupervisedOutput.
"""
batch_size, t, c, h, w = x.shape
x_flat = x.view((batch_size * t, c, h, w))
feats = self.backbone(x_flat)
visual_feats_flat = torch.flatten(feats, 1)
# txt_fts = self.encode_batch_text(task_txts)
# label_embs = self.task_attnpool(txt_fts).mean(dim=1)
# label_embs = torch.repeat_interleave(label_embs, visual_feats_flat.shape[0]//label_embs.shape[0], dim=0)
feats_flat = visual_feats_flat #+ label_embs
embs = self.encoder(feats_flat)
if self.normalize_embeddings:
embs = embs / (embs.norm(dim=-1, keepdim=True) + 1e-7)
if self.learnable_temp:
logit_scale = self.logit_scale.exp()
embs = logit_scale * embs
embs = embs.view((batch_size, t, -1))
feats = feats.view((batch_size, t, -1))
return SelfSupervisedOutput(frames=x, feats=feats, embs=embs)
@torch.no_grad()
def infer(
self,
x,
task_txts,
max_batch_size = 128,
):
"""Forward at inference with possible very large batch sizes."""
# Figure out a max batch size that's a multiple of the number of context
# frames. This is so we can support large videos with many frames.
lcm = self.num_ctx_frames
effective_bs = math.floor(max_batch_size / lcm) * lcm
if x.shape[1] > effective_bs:
out = []
for i in range(math.ceil(x.shape[1] / effective_bs)):
sub_frames = x[:, i * effective_bs:(i + 1) * effective_bs]
out.append(self.forward(sub_frames, task_txts).cpu())
out = SelfSupervisedOutput.merge(out)
else:
out = self.forward(x, task_txts).cpu()
return out.squeeze(0) |