-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathresidual_block.py
More file actions
36 lines (28 loc) · 1.08 KB
/
residual_block.py
File metadata and controls
36 lines (28 loc) · 1.08 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Aug 3 11:30:48 2022
@author: ahmedemam576
one of the building blocks to the Gen and Disc
"""
import torch
from torch import nn
class Residual_Block(nn.Module):
def __init__ (self, input_channels, kernel_size = 3 , use_inorm = True, activation= 'relu'):
super(Residual_Block,self).__init__()
self.conv1= nn.Conv2d(input_channels, input_channels, kernel_size,padding=1, padding_mode='reflect')
self.conv2= nn.Conv2d(input_channels, input_channels, kernel_size,padding=1, padding_mode='reflect')
if use_inorm:
self.innorm = nn.InstanceNorm2d(input_channels)
self.activation = activation
def forward(self, x):
x_original = x.clone()
x= self.conv1(x)
x= self.innorm(x)
if self.activation == 'relu':
x= nn.functional.relu(x)
elif self.activation == 'leakyrelu':
x= nn.functional.leakyrelu(x,0.2)
x= self.conv2(x)
x= self.innorm(x)
return x_original + x