-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscriminator.py
63 lines (47 loc) · 1.58 KB
/
discriminator.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""ANIMESH BALA ANI"""
# Import Modules
import torch
import torch.nn as nn
# CNN Block Class
class CNNBlock(nn.Module):
def __init__(self, in_channels, out_channels, stride=2):
super().__init__()
self.conv = nn.Sequential(
nn.Conv2d(in_channels, out_channels, 4, stride, bias=False, padding_mode='reflect'),
nn.BatchNorm2d(out_channels),
nn.LeakyReLU(0.2),
)
def forward(self, x):
return self.conv(x)
# Discriminator Model Class
class Discriminator(nn.Module):
def __init__(self, in_channels=3, features=[64, 128, 258, 512]):
super().__init__()
self.initial = nn.Sequential(
nn.Conv2d(in_channels*2, features[0], 4, 2, 1, padding_mode='reflect'),
nn.LeakyReLU(0.2),
)
layers = []
in_channels = features[0]
for feature in features[1:]:
layers.append(
CNNBlock(in_channels, feature, stride=1 if feature == features[-1] else 2),
)
in_channels = feature
layers.append(nn.Conv2d(in_channels, 1, 4, 1, 1, padding_mode='reflect'))
self.model = nn.Sequential(*layers)
def forward(self, x, y):
x = torch.cat([x, y], dim=1)
x = self.initial(x)
return self.model(x)
# Test Block
def test():
x = torch.randn((1, 3, 1920, 1080))
y = torch.randn((1, 3, 1920, 1080))
model = Discriminator()
preds = model(x, y)
print(preds.shape)
if __name__ == "__main__":
test()