-
Notifications
You must be signed in to change notification settings - Fork 1
/
adversarial_trainer.py
99 lines (67 loc) · 2.61 KB
/
adversarial_trainer.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
import torch
import copy
class GANFactory:
factories = {}
def __init__(self):
pass
def add_factory(gan_id, model_factory):
GANFactory.factories.put[gan_id] = model_factory
add_factory = staticmethod(add_factory)
# A Template Method:
def create_model(gan_id, net_d=None, criterion=None):
if gan_id not in GANFactory.factories:
GANFactory.factories[gan_id] = \
eval(gan_id + '.Factory()')
return GANFactory.factories[gan_id].create(net_d, criterion)
create_model = staticmethod(create_model)
class GANTrainer(object):
def __init__(self, net_d, criterion):
self.net_d = net_d
self.criterion = criterion
def loss_d(self, pred, gt):
pass
def loss_g(self, pred, gt):
pass
def get_params(self):
pass
class NoGAN(GANTrainer):
def __init__(self, net_d, criterion):
GANTrainer.__init__(self, net_d, criterion)
def loss_d(self, pred, gt):
return [0]
def loss_g(self, pred, gt):
return 0
def get_params(self):
return [torch.nn.Parameter(torch.Tensor(1))]
class Factory:
@staticmethod
def create(net_d, criterion): return NoGAN(net_d, criterion)
class SingleGAN(GANTrainer):
def __init__(self, net_d, criterion):
GANTrainer.__init__(self, net_d, criterion)
self.net_d = self.net_d.cuda()
def loss_d(self, pred, gt):
return self.criterion(self.net_d, pred, gt)
def loss_g(self, pred, gt):
return self.criterion.get_g_loss(self.net_d, pred, gt)
def get_params(self):
return self.net_d.parameters()
class Factory:
@staticmethod
def create(net_d, criterion): return SingleGAN(net_d, criterion)
class DoubleGAN(GANTrainer):
def __init__(self, net_d, criterion):
GANTrainer.__init__(self, net_d, criterion)
self.patch_d = net_d['patch'].cuda()
self.full_d = net_d['full'].cuda()
self.full_criterion = copy.deepcopy(criterion)
def loss_d(self, pred, gt):
return (self.criterion(self.patch_d, pred, gt) + self.full_criterion(self.full_d, pred, gt)) / 2
def loss_g(self, pred, gt):
return (self.criterion.get_g_loss(self.patch_d, pred, gt) + self.full_criterion.get_g_loss(self.full_d, pred,
gt)) / 2
def get_params(self):
return list(self.patch_d.parameters()) + list(self.full_d.parameters())
class Factory:
@staticmethod
def create(net_d, criterion): return DoubleGAN(net_d, criterion)