-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
79 lines (61 loc) · 1.88 KB
/
models.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
"""
This file contains three models needed to build ADDA:
- LeNetEncoder: to extract features from images.
- LeNetClassifier: to perform image classification.
- Discriminator model: to perform adversarial adaptation that if sees encoded source and target examples cannot reliably predict their domain label.
Tzeng, E., Hoffman, J., Saenko, K., & Darrell, T. (2017). Adversarial discriminative domain adaptation.
In Proceedings of the IEEE conference on computer vision and pattern recognition (pp. 7167-7176).
"""
import torch.nn.functional as F
from torch import nn
class Encoder(nn.Module):
"""
encoder for ADDA.
"""
def __init__(self):
super(Encoder, self).__init__()
self.restored = False
self.layer = nn.Sequential(
nn.Linear(2, 16),
nn.ReLU(),
nn.Linear(16, 16),
nn.Dropout(0.2),
nn.ReLU(),
nn.Linear(16, 2)
)
def forward(self, input):
out = self.layer(input)
return out
class Classifier(nn.Module):
"""
classifier for ADDA.
"""
def __init__(self):
super(Classifier, self).__init__()
self.restored = False
self.layer = nn.Sequential(
nn.Softmax()
)
def forward(self, input):
out = self.layer(input)
return out
class Discriminator(nn.Module):
"""
Discriminator model for source domain.
"""
def __init__(self):
"""Init discriminator."""
super(Discriminator, self).__init__()
self.restored = False
self.layer = nn.Sequential(
nn.Linear(2, 20),
nn.ReLU(),
nn.Linear(20, 50),
nn.ReLU(),
nn.Linear(50, 2),
nn.Softmax()
)
def forward(self, input):
"""Forward the discriminator."""
out = self.layer(input)
return out