Spaces:
Runtime error
Runtime error
File size: 20,838 Bytes
1b2a9b1 |
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 |
"""
07-21
StyleLoss to encourage style statistics to be consistent within each cluster.
"""
import torch
import torchvision
import torch.nn as nn
import torch.nn.functional as F
# VGG architecter, used for the perceptual loss using a pretrained VGG network
class VGG19(torch.nn.Module):
def __init__(self, requires_grad=False, device = torch.device(f'cuda:0')):
super().__init__()
vgg_pretrained_features = torchvision.models.vgg19(pretrained=True).features
self.slice1 = torch.nn.Sequential()
self.slice2 = torch.nn.Sequential()
self.slice3 = torch.nn.Sequential()
self.slice4 = torch.nn.Sequential()
self.slice5 = torch.nn.Sequential()
for x in range(2):
self.slice1.add_module(str(x), vgg_pretrained_features[x])
for x in range(2, 7):
self.slice2.add_module(str(x), vgg_pretrained_features[x])
for x in range(7, 12):
self.slice3.add_module(str(x), vgg_pretrained_features[x])
for x in range(12, 21):
self.slice4.add_module(str(x), vgg_pretrained_features[x])
for x in range(21, 30):
self.slice5.add_module(str(x), vgg_pretrained_features[x])
if not requires_grad:
for param in self.parameters():
param.requires_grad = False
cnn_normalization_mean = torch.tensor([0.485, 0.456, 0.406]).to(device)
cnn_normalization_std = torch.tensor([0.229, 0.224, 0.225]).to(device)
self.normalization = Normalization(cnn_normalization_mean, cnn_normalization_std)
def forward(self, X):
#X = self.normalization(X)
h_relu1 = self.slice1(X)
h_relu2 = self.slice2(h_relu1)
h_relu3 = self.slice3(h_relu2)
h_relu4 = self.slice4(h_relu3)
h_relu5 = self.slice5(h_relu4)
out = [h_relu1, h_relu2, h_relu3, h_relu4, h_relu5]
return out
# create a module to normalize input image so we can easily put it in a
# nn.Sequential
class Normalization(nn.Module):
def __init__(self, mean, std):
super(Normalization, self).__init__()
# .view the mean and std to make them [C x 1 x 1] so that they can
# directly work with image Tensor of shape [B x C x H x W].
# B is batch size. C is number of channels. H is height and W is width.
self.mean = torch.tensor(mean).view(-1, 1, 1)
self.std = torch.tensor(std).view(-1, 1, 1)
def forward(self, img):
# normalize img
return (img - self.mean) / self.std
class GramMatrix(nn.Module):
def forward(self,input):
b, c, h, w = input.size()
f = input.view(b,c,h*w) # bxcx(hxw)
# torch.bmm(batch1, batch2, out=None) #
# batch1: bxmxp, batch2: bxpxn -> bxmxn #
G = torch.bmm(f,f.transpose(1,2)) # f: bxcx(hxw), f.transpose: bx(hxw)xc -> bxcxc
return G.div_(c*h*w)
class StyleLoss(nn.Module):
"""
Version 1. Compare mean and variance cluster-wise.
"""
def __init__(self, style_layers = 'relu3, relu4, relu5', device = torch.device(f'cuda:0'), style_mode = 'gram'):
super().__init__()
self.vgg = VGG19()
self.style_layers = []
for style_layer in style_layers.split(','):
self.style_layers.append(int(style_layer[-1]) - 1)
self.style_mode = style_mode
cnn_normalization_mean = torch.tensor([0.485, 0.456, 0.406]).to(device)
cnn_normalization_std = torch.tensor([0.229, 0.224, 0.225]).to(device)
self.normalization = Normalization(cnn_normalization_mean, cnn_normalization_std)
def forward(self, pred, gt):
"""
INPUTS:
- pred: (B, 3, H, W)
- gt: (B, 3, H, W)
- seg: (B, H, W)
"""
# extract features for images
B, _, H, W = pred.shape
pred = self.normalization(pred)
gt = self.normalization(gt)
pred_feats = self.vgg(pred)
gt_feats = self.vgg(gt)
loss = 0
for style_layer in self.style_layers:
pred_feat = pred_feats[style_layer]
gt_feat = gt_feats[style_layer]
pred_gram = GramMatrix()(pred_feat)
gt_gram = GramMatrix()(gt_feat)
loss += torch.sum((pred_gram - gt_gram) ** 2) / B
return loss
class styleLossMask(nn.Module):
"""
Version 1. Compare mean and variance cluster-wise.
"""
def __init__(self, style_layers = 'relu3, relu4, relu5', device = torch.device(f'cuda:0'), style_mode = 'gram'):
super().__init__()
self.vgg = VGG19()
self.style_layers = []
for style_layer in style_layers.split(','):
self.style_layers.append(int(style_layer[-1]) - 1)
self.style_mode = style_mode
#cnn_normalization_mean = torch.tensor([0.485, 0.456, 0.406]).to(device)
#cnn_normalization_std = torch.tensor([0.229, 0.224, 0.225]).to(device)
#self.normalization = Normalization(cnn_normalization_mean, cnn_normalization_std)
def forward(self, input, target, mask):
B, _, H, W = input.shape
#pred = self.normalization(input)
#target = self.normalization(target)
pred_feats = self.vgg(input)
gt_feats = self.vgg(target)
loss = 0
mb, mc, mh, mw = mask.shape
for style_layer in self.style_layers:
pred_feat = pred_feats[style_layer]
gt_feat = gt_feats[style_layer]
ib,ic,ih,iw = pred_feat.size()
iF = pred_feat.view(ib,ic,-1)
tb,tc,th,tw = gt_feat.size()
tF = gt_feat.view(tb,tc,-1)
for i in range(mb):
# resize mask to have the same size of the feature
maski = F.interpolate(mask[i:i+1], size = (ih, iw), mode = 'nearest')
mask_flat_i = maski.view(mc, -1)
maskt = F.interpolate(mask[i:i+1], size = (th, tw), mode = 'nearest')
mask_flat_t = maskt.view(mc, -1)
for j in range(mc):
# get features for each part
idx = torch.nonzero(mask_flat_i[j]).squeeze()
if len(idx.shape) == 0 or idx.shape[0] == 0:
continue
ipart = torch.index_select(iF, 2, idx)
idx = torch.nonzero(mask_flat_t[j]).squeeze()
if len(idx.shape) == 0 or idx.shape[0] == 0:
continue
tpart = torch.index_select(tF, 2, idx)
iMean = torch.mean(ipart,dim=2)
iGram = torch.bmm(ipart, ipart.transpose(1,2)).div_(ic*ih*iw) # f: bxcx(hxw), f.transpose: bx(hxw)xc -> bxcxc
tMean = torch.mean(tpart,dim=2)
tGram = torch.bmm(tpart, tpart.transpose(1,2)).div_(tc*th*tw) # f: bxcx(hxw), f.transpose: bx(hxw)xc -> bxcxc
loss_j = nn.MSELoss()(iMean,tMean) + nn.MSELoss()(iGram,tGram)
loss += loss_j
return loss/tb
# Perceptual loss that uses a pretrained VGG network
class VGGLoss(nn.Module):
def __init__(self, weights = [1.0 / 32, 1.0 / 16, 1.0 / 8, 1.0 / 4, 1.0], device = torch.device(f'cuda:0')):
super(VGGLoss, self).__init__()
self.vgg = VGG19(device = device)
self.criterion = nn.L1Loss()
self.weights = weights
def forward(self, x, y):
x_vgg, y_vgg = self.vgg(x), self.vgg(y)
loss = 0
for i in range(len(x_vgg)):
loss += self.weights[i] * self.criterion(x_vgg[i], y_vgg[i].detach())
return loss
class styleLossMaskv2(nn.Module):
def __init__(self, style_layers = 'relu3, relu4, relu5', device = torch.device(f'cuda:0')):
super().__init__()
self.vgg = VGG19(device = device)
self.style_layers = []
for style_layer in style_layers.split(','):
self.style_layers.append(int(style_layer[-1]) - 1)
cnn_normalization_mean = torch.tensor([0.485, 0.456, 0.406]).to(device)
cnn_normalization_std = torch.tensor([0.229, 0.224, 0.225]).to(device)
self.normalization = Normalization(cnn_normalization_mean, cnn_normalization_std)
def forward(self, input, target, mask_input, mask_target):
B, _, H, W = input.shape
input = self.normalization(input)
target = self.normalization(target)
pred_feats = self.vgg(input)
gt_feats = self.vgg(target)
loss = 0
mb, mc, mh, mw = mask_input.shape
for style_layer in self.style_layers:
pred_feat = pred_feats[style_layer]
gt_feat = gt_feats[style_layer]
ib,ic,ih,iw = pred_feat.size()
iF = pred_feat.view(ib,ic,-1)
tb,tc,th,tw = gt_feat.size()
tF = gt_feat.view(tb,tc,-1)
for i in range(mb):
# resize mask to have the same size of the feature
maski = F.interpolate(mask_input[i:i+1], size = (ih, iw), mode = 'nearest')
mask_flat_i = maski.view(mc, -1)
maskt = F.interpolate(mask_target[i:i+1], size = (th, tw), mode = 'nearest')
mask_flat_t = maskt.view(mc, -1)
for j in range(mc):
# get features for each part
idx = torch.nonzero(mask_flat_i[j]).squeeze()
if len(idx.shape) == 0 or idx.shape[0] == 0:
continue
ipart = torch.index_select(iF[i:i+1], 2, idx)
idx = torch.nonzero(mask_flat_t[j]).squeeze()
if len(idx.shape) == 0 or idx.shape[0] == 0:
continue
tpart = torch.index_select(tF[i:i+1], 2, idx)
iGram = torch.bmm(ipart, ipart.transpose(1,2)).div_(ipart.shape[1] * ipart.shape[2]) # f: bxcx(hxw), f.transpose: bx(hxw)xc -> bxcxc
tGram = torch.bmm(tpart, tpart.transpose(1,2)).div_(tpart.shape[1] * tpart.shape[2]) # f: bxcx(hxw), f.transpose: bx(hxw)xc -> bxcxc
loss += torch.sum((iGram - tGram) ** 2)
return loss/tb
class styleLossMaskv3(nn.Module):
def __init__(self, style_layers = 'relu1, relu2, relu3, relu4, relu5', device = torch.device(f'cuda:0')):
super().__init__()
self.vgg = VGG19(device = device)
self.style_layers = []
for style_layer in style_layers.split(','):
self.style_layers.append(int(style_layer[-1]) - 1)
cnn_normalization_mean = torch.tensor([0.485, 0.456, 0.406]).to(device)
cnn_normalization_std = torch.tensor([0.229, 0.224, 0.225]).to(device)
self.normalization = Normalization(cnn_normalization_mean, cnn_normalization_std)
def forward_img_img(self, input, target, mask_input, mask_target):
B, _, H, W = input.shape
input = self.normalization(input)
target = self.normalization(target)
pred_feats = self.vgg(input)
gt_feats = self.vgg(target)
loss = 0
mb, mc, mh, mw = mask_input.shape
for style_layer in self.style_layers:
pred_feat = pred_feats[style_layer]
gt_feat = gt_feats[style_layer]
ib,ic,ih,iw = pred_feat.size()
iF = pred_feat.view(ib,ic,-1)
tb,tc,th,tw = gt_feat.size()
tF = gt_feat.view(tb,tc,-1)
for i in range(mb):
# resize mask to have the same size of the feature
maski = F.interpolate(mask_input[i:i+1], size = (ih, iw), mode = 'nearest')
mask_flat_i = maski.view(mc, -1)
maskt = F.interpolate(mask_target[i:i+1], size = (th, tw), mode = 'nearest')
mask_flat_t = maskt.view(mc, -1)
for j in range(mc):
# get features for each part
idx = torch.nonzero(mask_flat_i[j]).squeeze()
if len(idx.shape) == 0 or idx.shape[0] == 0:
continue
ipart = torch.index_select(iF[i:i+1], 2, idx)
idx = torch.nonzero(mask_flat_t[j]).squeeze()
if len(idx.shape) == 0 or idx.shape[0] == 0:
continue
tpart = torch.index_select(tF[i:i+1], 2, idx)
iGram = torch.bmm(ipart, ipart.transpose(1,2)).div_(ipart.shape[1] * ipart.shape[2]) # f: bxcx(hxw), f.transpose: bx(hxw)xc -> bxcxc
tGram = torch.bmm(tpart, tpart.transpose(1,2)).div_(tpart.shape[1] * tpart.shape[2]) # f: bxcx(hxw), f.transpose: bx(hxw)xc -> bxcxc
#loss += torch.sum((iGram - tGram) ** 2)
loss += F.mse_loss(iGram, tGram)
#return loss/tb
return loss * 100000 / tb
def forward_patch_img(self, input, target, mask_target):
input = self.normalization(input)
target = self.normalization(target)
pred_feats = self.vgg(input)
gt_feats = self.vgg(target)
patch_num = input.shape[0] // target.shape[0]
loss = 0
for style_layer in self.style_layers:
pred_feat = pred_feats[style_layer]
gt_feat = gt_feats[style_layer]
ib,ic,ih,iw = pred_feat.size()
iF = pred_feat.view(ib,ic,-1)
tb,tc,th,tw = gt_feat.size()
tF = gt_feat.view(tb,tc,-1)
for i in range(tb):
# resize mask to have the same size of the feature
maskt = F.interpolate(mask_target[i:i+1], size = (th, tw), mode = 'nearest')
mask_flat_t = maskt.view(-1)
idx = torch.nonzero(mask_flat_t).squeeze()
if len(idx.shape) == 0 or idx.shape[0] == 0:
continue
tpart = torch.index_select(tF[i:i+1], 2, idx)
tGram = torch.bmm(tpart, tpart.transpose(1,2)).div_(tpart.shape[1] * tpart.shape[2]) # f: bxcx(hxw), f.transpose: bx(hxw)xc -> bxcxc
ipart = iF[i * patch_num: (i + 1) * patch_num]
iGram = torch.bmm(ipart, ipart.transpose(1,2)).div_(ipart.shape[1] * ipart.shape[2]) # f: bxcx(hxw), f.transpose: bx(hxw)xc -> bxcxc
#loss += torch.sum((iGram - tGram.repeat(patch_num, 1, 1)) ** 2)
loss += F.mse_loss(iGram, tGram.repeat(patch_num, 1, 1))
return loss/ib * 100000
class KLDLoss(nn.Module):
def forward(self, mu, logvar):
return -0.5 * torch.sum(1 + logvar - mu.pow(2) - logvar.exp())
class LPIPSorGramMatch(nn.Module):
"""
Version 1. Compare mean and variance cluster-wise.
"""
def __init__(self, style_layers = 'relu3, relu4, relu5', device = torch.device(f'cuda:0'),
weights = [1.0 / 32, 1.0 / 16, 1.0 / 8, 1.0 / 4, 1.0],):
super().__init__()
self.vgg = VGG19(device = device)
self.style_layers = []
for style_layer in style_layers.split(','):
self.style_layers.append(int(style_layer[-1]) - 1)
cnn_normalization_mean = torch.tensor([0.485, 0.456, 0.406]).to(device)
cnn_normalization_std = torch.tensor([0.229, 0.224, 0.225]).to(device)
self.normalization = Normalization(cnn_normalization_mean, cnn_normalization_std)
self.criterion = nn.L1Loss()
self.weights = weights
def forward(self, pred, gt, mode = 'lpips'):
"""
INPUTS:
- pred: (B, 3, H, W)
- gt: (B, 3, H, W)
- seg: (B, H, W)
"""
# extract features for images
B, _, H, W = pred.shape
pred = self.normalization(pred)
gt = self.normalization(gt)
pred_feats = self.vgg(pred)
gt_feats = self.vgg(gt)
if mode == 'lpips':
lpips_loss = 0
for i in range(len(pred_feats)):
lpips_loss += self.weights[i] * self.criterion(pred_feats[i], gt_feats[i].detach())
return lpips_loss
elif mode == 'gram_match':
gram_match_loss = 0
for style_layer in self.style_layers:
pred_feat = pred_feats[style_layer]
gt_feat = gt_feats[style_layer]
pred_gram = GramMatrix()(pred_feat)
gt_gram = GramMatrix()(gt_feat)
gram_match_loss += torch.sum((pred_gram - gt_gram) ** 2) / B
return gram_match_loss
else:
raise ValueError("Only computes lpips or gram match loss.")
class styleLossMaskv4(nn.Module):
def __init__(self, style_layers = 'relu3, relu4, relu5', device = torch.device(f'cuda:0')):
super().__init__()
self.vgg = VGG19(device = device)
self.style_layers = []
for style_layer in style_layers.split(','):
self.style_layers.append(int(style_layer[-1]) - 1)
cnn_normalization_mean = torch.tensor([0.485, 0.456, 0.406]).to(device)
cnn_normalization_std = torch.tensor([0.229, 0.224, 0.225]).to(device)
self.normalization = Normalization(cnn_normalization_mean, cnn_normalization_std)
def forward_img_img(self, input, target, mask_input, mask_target):
B, _, H, W = input.shape
input = self.normalization(input)
target = self.normalization(target)
pred_feats = self.vgg(input)
gt_feats = self.vgg(target)
loss = 0
mb, mc, mh, mw = mask_input.shape
for style_layer in self.style_layers:
pred_feat = pred_feats[style_layer]
gt_feat = gt_feats[style_layer]
ib,ic,ih,iw = pred_feat.size()
iF = pred_feat.view(ib,ic,-1)
tb,tc,th,tw = gt_feat.size()
tF = gt_feat.view(tb,tc,-1)
for i in range(mb):
# resize mask to have the same size of the feature
maski = F.interpolate(mask_input[i:i+1], size = (ih, iw), mode = 'nearest')
mask_flat_i = maski.view(mc, -1)
maskt = F.interpolate(mask_target[i:i+1], size = (th, tw), mode = 'nearest')
mask_flat_t = maskt.view(mc, -1)
for j in range(mc):
# get features for each part
idx = torch.nonzero(mask_flat_i[j]).squeeze()
if len(idx.shape) == 0 or idx.shape[0] == 0:
continue
ipart = torch.index_select(iF[i:i+1], 2, idx)
idx = torch.nonzero(mask_flat_t[j]).squeeze()
if len(idx.shape) == 0 or idx.shape[0] == 0:
continue
tpart = torch.index_select(tF[i:i+1], 2, idx)
iGram = torch.bmm(ipart, ipart.transpose(1,2)).div_(ipart.shape[1] * ipart.shape[2]) # f: bxcx(hxw), f.transpose: bx(hxw)xc -> bxcxc
iMean = torch.mean(ipart, dim=2)
tGram = torch.bmm(tpart, tpart.transpose(1,2)).div_(tpart.shape[1] * tpart.shape[2]) # f: bxcx(hxw), f.transpose: bx(hxw)xc -> bxcxc
tMean = torch.mean(tpart, dim=2)
loss += torch.sum((iGram - tGram) ** 2) + torch.sum((iMean - tMean) ** 2) * 0.01
return loss/tb
def forward_patch_img(self, input, target, mask_target):
input = self.normalization(input)
target = self.normalization(target)
pred_feats = self.vgg(input)
gt_feats = self.vgg(target)
patch_num = input.shape[0] // target.shape[0]
loss = 0
for style_layer in self.style_layers:
pred_feat = pred_feats[style_layer]
gt_feat = gt_feats[style_layer]
ib,ic,ih,iw = pred_feat.size()
iF = pred_feat.view(ib,ic,-1)
tb,tc,th,tw = gt_feat.size()
tF = gt_feat.view(tb,tc,-1)
for i in range(tb):
# resize mask to have the same size of the feature
maskt = F.interpolate(mask_target[i:i+1], size = (th, tw), mode = 'nearest')
mask_flat_t = maskt.view(-1)
idx = torch.nonzero(mask_flat_t).squeeze()
if len(idx.shape) == 0 or idx.shape[0] == 0:
continue
tpart = torch.index_select(tF[i:i+1], 2, idx)
tGram = torch.bmm(tpart, tpart.transpose(1,2)).div_(tpart.shape[1] * tpart.shape[2]) # f: bxcx(hxw), f.transpose: bx(hxw)xc -> bxcxc
tMean = torch.mean(tpart, dim=2)
ipart = iF[i * patch_num: (i + 1) * patch_num]
iMean = torch.mean(ipart, dim=2)
iGram = torch.bmm(ipart, ipart.transpose(1,2)).div_(ipart.shape[1] * ipart.shape[2]) # f: bxcx(hxw), f.transpose: bx(hxw)xc -> bxcxc
loss += torch.sum((iGram - tGram.repeat(patch_num, 1, 1)) ** 2)
loss += torch.sum((iMean - tMean.repeat(patch_num, 1)) ** 2) * 0.01
return loss/ib
|