Spaces:
Running on Zero
Running on Zero
File size: 1,572 Bytes
d2c5d46 | 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 | """
Copyright (C) 2019 NVIDIA Corporation. All rights reserved.
Licensed under the CC BY-NC-SA 4.0 license (https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode).
"""
import re
import torch
import torch.nn as nn
from diffcve.modules.diffusion.utils import normalization
class SPADE(nn.Module):
def __init__(self, norm_nc, label_nc, config_text='spadeinstance3x3'):
super().__init__()
assert config_text.startswith('spade')
parsed = re.search(r'spade(\D+)(\d)x\d', config_text)
param_free_norm_type = str(parsed.group(1))
ks = int(parsed.group(2))
self.param_free_norm = normalization(norm_nc)
nhidden = 128
pw = ks // 2
self.mlp_shared = nn.Sequential(
nn.Conv2d(label_nc, nhidden, kernel_size=ks, padding=pw),
nn.ReLU()
)
self.mlp_gamma = nn.Conv2d(nhidden, norm_nc, kernel_size=ks, padding=pw)
self.mlp_beta = nn.Conv2d(nhidden, norm_nc, kernel_size=ks, padding=pw)
def forward(self, x_dic, segmap_dic, size=None):
if size is None:
key = str(int(x_dic.shape[-1])) if torch.onnx.is_in_onnx_export() else str(x_dic.size(-1))
segmap = segmap_dic[key]
x = x_dic
else:
x = x_dic[str(size)]
segmap = segmap_dic[str(size)]
normalized = self.param_free_norm(x)
actv = self.mlp_shared(segmap)
gamma = self.mlp_gamma(actv)
beta = self.mlp_beta(actv)
out = normalized * (1 + gamma) + beta
return out
|