-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmodules.py
50 lines (38 loc) · 1.5 KB
/
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
import torch
import torch.nn as nn
import numpy as np
import torch.nn.functional as F
class Downsample(nn.Module):
def __init__(self, n_channels, ratio):
super(Downsample, self).__init__()
self.ratio = ratio
dconvs = []
for i in range(int(np.log2(ratio))):
dconvs.append(nn.Conv2d(n_channels, n_channels, 3, stride=2, padding=1, dilation=1, groups=n_channels, bias=True))
self.downsample = nn.Sequential(*dconvs)
def forward(self,x):
h = self.downsample(x)
return h
class Upsample(nn.Module):
def __init__(self, n_channels, ratio):
super(Upsample, self).__init__()
uconvs = []
for i in range(int(np.log2(ratio))):
uconvs.append(nn.ConvTranspose2d(in_channels=n_channels, out_channels=n_channels, kernel_size=3, stride=2, padding=1, output_padding=1))
self.upsample = nn.Sequential(*uconvs)
def forward(self,x):
h = self.upsample(x)
return h
class resblock(nn.Module):
def __init__(self, n_channels, kernel_size):
super(resblock, self).__init__()
self.body = nn.Sequential(
nn.Conv2d(n_channels, n_channels, kernel_size, stride=1, padding=kernel_size //2, bias=True),
nn.ReLU(inplace=True),
nn.Conv2d(n_channels, n_channels, kernel_size, stride=1, padding=kernel_size //2, bias=True),
)
self.relu = nn.ReLU()
def forward(self,x):
res = self.body(x)
x = res + x
return self.relu(x)