-
Notifications
You must be signed in to change notification settings - Fork 5
/
renderer.py
32 lines (27 loc) · 880 Bytes
/
renderer.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
import torch
import torch.nn as nn
import torch.nn.functional as F
class LayeredRenderer:
def __init__(self):
pass
def __call__(self, bg, fg_mask):
out = bg
fg, mask = fg_mask
out = out * (1. - mask) + fg * mask
return out
class ModelWrapper(nn.Module):
def __init__(self, generator, perturber, renderer):
super().__init__()
self.generator = generator
if type(perturber) in (list, tuple) and len(perturber) == 0:
perturber = None
self.perturber = perturber
self.renderer = renderer
def forward(self, *args, **kwargs):
X = self.generator(*args, **kwargs)
if self.perturber is not None:
perturbed = self.perturber(*X)
else:
perturbed = X
rendered = self.renderer(*perturbed)
return rendered, perturbed, X