-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelper.py
More file actions
64 lines (53 loc) · 1.91 KB
/
helper.py
File metadata and controls
64 lines (53 loc) · 1.91 KB
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
from turtle import forward
from sqlalchemy import true
import torch
import torch.nn as nn
import torch.nn.functional as F
class GroupNorm(nn.Module):
def __init__(self, channels):
super(GroupNorm, self).__init__()
self.gn = nn,GroupNorm(num_groups=32, num_channels=channels, eps=1e-6, affine=True)
def forward(self, x):
return self.gn(x)
class Swish(nn.Module):
def forward(self, x):
return x* torch.sigmoid(x)
class ResidualBlock(nn.Module):
def __init__(self, in_channels, out_channels):
super(ResidualBlock, self).__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.block - nn.Sequential(
GroupNorm(in_channels),
Swish(),
nn.Conv2d(in_channels, out_channels, 3, 1, 1),
GroupNorm(out_channels),
Swish(),
nn.Conv2d(out_channels, out_channels, 3, 1, 1)
)
if in_channels != out_channels:
self.channel_up = nn.Conv2d(in_channels, out_channels, 1, 1, 0)
def forward(self, x):
if self.in_channels != self.out_channels:
return self.channel_up(x) + self.block(x)
else:
return x + self.block(x)
class UpSampleBlock(nn.Module):
def __init__(self, channels):
super(UpSampleBlock, self).__init__()
self.conv = nn.Conv2d(channels, channels, 3, 1, 1)
def forward(self, x):
x = F.interpolate(x, scale_factor=2.0)
return self.conv(x)
class DownSampleBlock(nn.Module):
def __init__(self, channels):
super(DownSampleBlock, self).__init__()
self.conv = nn.Conv2d(channels, channels, 3, 2, 0)
def forward(self, x):
pad = (0 , 1, 0, 1)
x = F.pad(x, pad, mode="constant", value=0)
return self.conv(x)
class NonLocalBlock(nn.Module):
def __init__(self):
super().__init__()
self.in_channels = channels