-
Notifications
You must be signed in to change notification settings - Fork 0
/
conv_modules.py
99 lines (86 loc) · 2.57 KB
/
conv_modules.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
from tinygrad import nn, Tensor
class conv:
def __init__(
self,
batchNorm,
in_channels,
out_channels,
kernel_size=3,
padding=None,
stride=1,
):
if padding:
pad = padding
else:
pad = (kernel_size - 1) // 2
if batchNorm:
self.layers = [
nn.Conv2d(
in_channels,
out_channels,
kernel_size=kernel_size,
stride=stride,
padding=pad,
bias=False,
),
nn.BatchNorm2d(out_channels),
Tensor.leakyrelu,
]
else:
self.layers = [
nn.Conv2d(
in_channels,
out_channels,
kernel_size=kernel_size,
stride=stride,
padding=pad,
bias=True,
),
Tensor.leakyrelu,
]
def __call__(self, x: Tensor) -> Tensor:
return x.sequential(self.layers)
def predict_flow(in_planes):
return nn.Conv2d(in_planes, 2, kernel_size=3, stride=1, padding=1, bias=True)
class i_conv:
def __init__(self, batchNorm, in_planes, out_planes, kernel_size=3, stride=1, bias=True) -> None:
if batchNorm:
self.layers = [
nn.Conv2d(
in_planes,
out_planes,
kernel_size=kernel_size,
stride=stride,
padding=(kernel_size - 1) // 2,
bias=bias,
),
nn.BatchNorm2d(out_planes),
]
else:
self.layers = [
nn.Conv2d(
in_planes,
out_planes,
kernel_size=kernel_size,
stride=stride,
padding=(kernel_size - 1) // 2,
bias=bias,
),
]
def __call__(self, x: Tensor) -> Tensor:
return x.sequential(self.layers)
class deconv:
def __init__(self, in_planes, out_planes, stride=2, padding=2):
self.layers = [
nn.ConvTranspose2d(
in_planes,
out_planes,
kernel_size=5,
stride=stride,
padding=padding,
bias=True,
),
Tensor.leakyrelu,
]
def __call__(self, x: Tensor) -> Tensor:
return x.sequential(self.layers)