-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUpBlock.py
42 lines (36 loc) · 1.35 KB
/
UpBlock.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
import torch
from torch import nn
class UpBlock(nn.Module):
def __init__(self, input_channels, output_channels):
super(UpBlock, self).__init__()
internal_channels = int((input_channels+output_channels)/2)
kernel_size = 7
self.conv1 = nn.Conv2d(input_channels, internal_channels, 3, 1, 1)
# ConvTranspose2d replaced with Upsample since noise reduction
self.upsample = nn.Upsample(
scale_factor=2, mode='bilinear', align_corners=True)
# Convolution layer to compensate for the change in number of channels
self.conv_compensate = nn.Conv2d(
internal_channels, output_channels, 3, 1, 1)
self.conv2 = nn.Conv2d(output_channels, output_channels, 3, 1, 1)
self.batch_norm = nn.BatchNorm2d(num_features=output_channels)
self.relu = nn.ReLU()
def forward(self, x):
x = self.first_conv_block(x)
x = self.upsampling(x)
x = self.second_conv_block(x)
return x
def first_conv_block(self, x):
x = self.conv1(x)
x = self.relu(x)
return x
def second_conv_block(self, x):
x = self.conv2(x)
x = self.batch_norm(x)
x = self.relu(x)
return x
def upsampling(self, x):
x = self.upsample(x)
x = self.conv_compensate(x)
x = self.relu(x)
return x