forked from VITA-Group/DeblurGANv2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadversarial_trainer.py
91 lines (63 loc) · 2.64 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
import torch
import copy
class AdversarialTrainerFactory:
factories = {}
def addFactory(id, modelFactory):
AdversarialTrainerFactory.factories.put[id] = modelFactory
addFactory = staticmethod(addFactory)
# A Template Method:
def createModel(id, netD=None, criterion=None):
if id not in AdversarialTrainerFactory.factories:
AdversarialTrainerFactory.factories[id] = \
eval(id + '.Factory()')
return AdversarialTrainerFactory.factories[id].create(netD, criterion)
createModel = staticmethod(createModel)
class AdversarialTrainer(object):
def __init__(self, netD, criterion):
self.netD = netD
self.criterion = criterion
def lossD(self, pred, gt):
pass
def lossG(self, pred, gt):
pass
def get_params(self):
pass
class NoAdversarialTrainer(AdversarialTrainer):
def __init__(self, netD, criterion):
AdversarialTrainer.__init__(self, netD, criterion)
def lossD(self, pred, gt):
return [0]
def lossG(self, pred, gt):
return 0
def get_params(self):
return [torch.nn.Parameter(torch.Tensor(1))]
class Factory:
def create(self, netD, criterion): return NoAdversarialTrainer(netD, criterion)
class SingleAdversarialTrainer(AdversarialTrainer):
def __init__(self, netD, criterion):
AdversarialTrainer.__init__(self, netD, criterion)
self.netD = self.netD.cuda()
def lossD(self, pred, gt):
return self.criterion(self.netD, pred, gt)
def lossG(self, pred, gt):
return self.criterion.get_g_loss(self.netD, pred, gt)
def get_params(self):
return self.netD.parameters()
class Factory:
def create(self, netD, criterion): return SingleAdversarialTrainer(netD, criterion)
class DoubleAdversarialTrainer(AdversarialTrainer):
def __init__(self, netD, criterion):
AdversarialTrainer.__init__(self, netD, criterion)
self.patchD = netD['patch']
self.fullD = netD['full']
self.patchD = self.patchD.cuda()
self.fullD = self.fullD.cuda()
self.full_criterion = copy.deepcopy(criterion)
def lossD(self, pred, gt):
return (self.criterion(self.patchD, pred, gt) + self.full_criterion(self.fullD, pred, gt)) / 2
def lossG(self, pred, gt):
return (self.criterion.get_g_loss(self.patchD, pred, gt) + self.full_criterion.get_g_loss(self.fullD, pred, gt)) / 2
def get_params(self):
return list(self.patchD.parameters()) + list(self.fullD.parameters())
class Factory:
def create(self, netD, criterion): return DoubleAdversarialTrainer(netD, criterion)