-
Notifications
You must be signed in to change notification settings - Fork 2
/
model_defs.py
166 lines (131 loc) · 4.84 KB
/
model_defs.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torchvision import datasets, transforms
from torch.utils.data import DataLoader, Dataset
from torchvision.utils import save_image
import pandas as pd
import random
from tqdm.notebook import tqdm
import matplotlib.pyplot as plt
from matplotlib.pyplot import imshow
#helper function
def to_img(x):
x = 0.5 * (x + 1)
x = x.clamp(0, 1)
x = x.view(x.size(0), 1, 28, 28)
return x
#dataset
class Char_img(Dataset):
def __init__(self, targ_data, ref_data):
self.targ_img = targ_data.img
self.targ_l = targ_data.label
self.ref_img = ref_data.img
self.ref_l = ref_data.label
def __len__(self):
return len(self.targ_l)
def __getitem__(self, idx):
n = min(len(self.targ_l)-1, idx)
t_img = self.targ_img[n]
t_lab = self.targ_l[n] #== 0
r_img = self.ref_img[idx]
r_lab = torch.tensor(self.ref_l[idx])
return t_img, t_lab, r_img, r_lab
def get_img(self, i):
t_img, _,_,_ = self.__getitem__(i)
return t_img
#model definitions
class One_class_net(nn.Module):
def __init__(self, ae = None, num_classes = 26):
super(One_class_net, self).__init__()
if ae:
self.features = ae.encoder
else:
self.features = nn.Sequential(
nn.Conv2d(1, 32, 3, stride=3, padding=1), # b, 16, 10, 10
nn.ReLU(True),
nn.MaxPool2d(2, stride=2), # b, 16, 5, 5
nn.Conv2d(32, 16, 3, stride=2, padding=1), # b, 8, 3, 3
nn.ReLU(True),
nn.MaxPool2d(2, stride=1) # b, 8, 2, 2
)
self.conv_out = nn.Conv2d(16, num_classes, 2)
self.soft = nn.Softmax()
def forward(self, x):
num_sam = x.shape[0]
if len(x.shape) != 4:
x = x.view(-1,1,28,28)
feat = self.features(x)
h = self.conv_out(feat)
output = h.view(num_sam,-1)#self.soft(h).view(num_sam,-1)#F.log_softmax(h).view(num_sam,-1)
return output, feat
class autoencoder(nn.Module):
def __init__(self):
super(autoencoder, self).__init__()
self.encoder = nn.Sequential(
nn.Conv2d(1, 16, 3, stride=3, padding=1), # b, 16, 10, 10
nn.ReLU(True),
nn.MaxPool2d(2, stride=2), # b, 16, 5, 5
nn.Conv2d(16, 8, 3, stride=2, padding=1), # b, 8, 3, 3
nn.ReLU(True),
nn.MaxPool2d(2, stride=1) # b, 8, 2, 2
)
self.decoder = nn.Sequential(
nn.ConvTranspose2d(8, 16, 3, stride=2), # b, 16, 5, 5
nn.ReLU(True),
nn.ConvTranspose2d(16, 8, 5, stride=3, padding=1), # b, 8, 15, 15
nn.ReLU(True),
nn.ConvTranspose2d(8, 1, 2, stride=2, padding=1), # b, 1, 28, 28
nn.Tanh()
)
def forward(self, x):
x = self.encoder(x)
x = self.decoder(x)
return x
class autoencoder_v2(nn.Module):
def __init__(self):
super(autoencoder_v2, self).__init__()
self.encoder = nn.Sequential(
nn.Conv2d(1, 32, 3, stride=3, padding=1), # b, 16, 10, 10
nn.ReLU(True),
nn.MaxPool2d(2, stride=2), # b, 16, 5, 5
nn.Conv2d(32, 16, 3, stride=2, padding=1), # b, 8, 3, 3
nn.ReLU(True),
nn.MaxPool2d(2, stride=1) # b, 8, 2, 2
)
self.decoder = nn.Sequential(
nn.ConvTranspose2d(16, 32, 3, stride=2), # b, 16, 5, 5
nn.ReLU(True),
nn.ConvTranspose2d(32, 16, 5, stride=3, padding=1), # b, 8, 15, 15
nn.ReLU(True),
nn.ConvTranspose2d(16, 1, 2, stride=2, padding=1), # b, 1, 28, 28
nn.Tanh()
)
def forward(self, x):
x = self.encoder(x)
x = self.decoder(x)
return x
class autoencoder_v3(nn.Module):
def __init__(self):
super(autoencoder_v3, self).__init__()
self.encoder = nn.Sequential(
nn.Conv2d(1, 64, 3, stride=3, padding=1), # b, 16, 10, 10
nn.ReLU(True),
nn.MaxPool2d(2, stride=2), # b, 16, 5, 5
nn.Conv2d(64, 32, 3, stride=2, padding=1), # b, 8, 3, 3
nn.ReLU(True),
nn.MaxPool2d(2, stride=1) # b, 8, 2, 2
)
self.decoder = nn.Sequential(
nn.ConvTranspose2d(32, 64, 3, stride=2), # b, 16, 5, 5
nn.ReLU(True),
nn.ConvTranspose2d(64, 32, 5, stride=3, padding=1), # b, 8, 15, 15
nn.ReLU(True),
nn.ConvTranspose2d(32, 1, 2, stride=2, padding=1), # b, 1, 28, 28
nn.Tanh()
)
def forward(self, x):
x = self.encoder(x)
x = self.decoder(x)
return x