forked from nelson1425/EfficientAD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.py
120 lines (114 loc) · 4.7 KB
/
common.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
from torch import nn
from torchvision.datasets import ImageFolder
def get_autoencoder(out_channels=384):
return nn.Sequential(
# encoder
nn.Conv2d(in_channels=3, out_channels=32, kernel_size=4, stride=2,
padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=32, out_channels=32, kernel_size=4, stride=2,
padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=32, out_channels=64, kernel_size=4, stride=2,
padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=64, out_channels=64, kernel_size=4, stride=2,
padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=64, out_channels=64, kernel_size=4, stride=2,
padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=64, out_channels=64, kernel_size=8),
# decoder
nn.Upsample(size=3, mode='bilinear'),
nn.Conv2d(in_channels=64, out_channels=64, kernel_size=4, stride=1,
padding=2),
nn.ReLU(inplace=True),
nn.Dropout(0.2),
nn.Upsample(size=8, mode='bilinear'),
nn.Conv2d(in_channels=64, out_channels=64, kernel_size=4, stride=1,
padding=2),
nn.ReLU(inplace=True),
nn.Dropout(0.2),
nn.Upsample(size=15, mode='bilinear'),
nn.Conv2d(in_channels=64, out_channels=64, kernel_size=4, stride=1,
padding=2),
nn.ReLU(inplace=True),
nn.Dropout(0.2),
nn.Upsample(size=32, mode='bilinear'),
nn.Conv2d(in_channels=64, out_channels=64, kernel_size=4, stride=1,
padding=2),
nn.ReLU(inplace=True),
nn.Dropout(0.2),
nn.Upsample(size=63, mode='bilinear'),
nn.Conv2d(in_channels=64, out_channels=64, kernel_size=4, stride=1,
padding=2),
nn.ReLU(inplace=True),
nn.Dropout(0.2),
nn.Upsample(size=127, mode='bilinear'),
nn.Conv2d(in_channels=64, out_channels=64, kernel_size=4, stride=1,
padding=2),
nn.ReLU(inplace=True),
nn.Dropout(0.2),
nn.Upsample(size=56, mode='bilinear'),
nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, stride=1,
padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=64, out_channels=out_channels, kernel_size=3,
stride=1, padding=1)
)
def get_pdn_small(out_channels=384, padding=False):
pad_mult = 1 if padding else 0
return nn.Sequential(
nn.Conv2d(in_channels=3, out_channels=128, kernel_size=4,
padding=3 * pad_mult),
nn.ReLU(inplace=True),
nn.AvgPool2d(kernel_size=2, stride=2, padding=1 * pad_mult),
nn.Conv2d(in_channels=128, out_channels=256, kernel_size=4,
padding=3 * pad_mult),
nn.ReLU(inplace=True),
nn.AvgPool2d(kernel_size=2, stride=2, padding=1 * pad_mult),
nn.Conv2d(in_channels=256, out_channels=256, kernel_size=3,
padding=1 * pad_mult),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=256, out_channels=out_channels, kernel_size=4)
)
def get_pdn_medium(out_channels=384, padding=False):
pad_mult = 1 if padding else 0
return nn.Sequential(
nn.Conv2d(in_channels=3, out_channels=256, kernel_size=4,
padding=3 * pad_mult),
nn.ReLU(inplace=True),
nn.AvgPool2d(kernel_size=2, stride=2, padding=1 * pad_mult),
nn.Conv2d(in_channels=256, out_channels=512, kernel_size=4,
padding=3 * pad_mult),
nn.ReLU(inplace=True),
nn.AvgPool2d(kernel_size=2, stride=2, padding=1 * pad_mult),
nn.Conv2d(in_channels=512, out_channels=512, kernel_size=1),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3,
padding=1 * pad_mult),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=512, out_channels=out_channels, kernel_size=4),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=out_channels, out_channels=out_channels,
kernel_size=1)
)
class ImageFolderWithoutTarget(ImageFolder):
def __getitem__(self, index):
sample, target = super().__getitem__(index)
return sample
class ImageFolderWithPath(ImageFolder):
def __getitem__(self, index):
path, target = self.samples[index]
sample, target = super().__getitem__(index)
return sample, target, path
def InfiniteDataloader(loader):
iterator = iter(loader)
while True:
try:
yield next(iterator)
except StopIteration:
iterator = iter(loader)