-
Notifications
You must be signed in to change notification settings - Fork 6
/
Model_WGAN.py
78 lines (59 loc) · 2.64 KB
/
Model_WGAN.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import numpy as np
import torch
import os
from torch import nn
from torch import optim
from torch.nn import functional as F
class Discriminator(nn.Module):
def __init__(self, channel=512):
super(Discriminator, self).__init__()
self.channel = channel
n_class = 1
self.conv1 = nn.Conv3d(1, channel//8, kernel_size=4, stride=2, padding=1)
self.conv2 = nn.Conv3d(channel//8, channel//4, kernel_size=4, stride=2, padding=1)
self.bn2 = nn.BatchNorm3d(channel//4)
self.conv3 = nn.Conv3d(channel//4, channel//2, kernel_size=4, stride=2, padding=1)
self.bn3 = nn.BatchNorm3d(channel//2)
self.conv4 = nn.Conv3d(channel//2, channel, kernel_size=4, stride=2, padding=1)
self.bn4 = nn.BatchNorm3d(channel)
self.conv5 = nn.Conv3d(channel, n_class, kernel_size=4, stride=2, padding=1)
def forward(self, x, _return_activations=False):
h1 = F.leaky_relu(self.conv1(x), negative_slope=0.2)
h2 = F.leaky_relu(self.bn2(self.conv2(h1)), negative_slope=0.2)
h3 = F.leaky_relu(self.bn3(self.conv3(h2)), negative_slope=0.2)
h4 = F.leaky_relu(self.bn4(self.conv4(h3)), negative_slope=0.2)
h5 = self.conv5(h4)
output = h5
return output
class Generator(nn.Module):
def __init__(self, noise:int=1000, channel:int=64):
super(Generator, self).__init__()
_c = channel
self.noise = noise
self.fc = nn.Linear(1000,512*4*4*4)
self.bn1 = nn.BatchNorm3d(_c*8)
self.tp_conv2 = nn.Conv3d(_c*8, _c*4, kernel_size=3, stride=1, padding=1, bias=False)
self.bn2 = nn.BatchNorm3d(_c*4)
self.tp_conv3 = nn.Conv3d(_c*4, _c*2, kernel_size=3, stride=1, padding=1, bias=False)
self.bn3 = nn.BatchNorm3d(_c*2)
self.tp_conv4 = nn.Conv3d(_c*2, _c, kernel_size=3, stride=1, padding=1, bias=False)
self.bn4 = nn.BatchNorm3d(_c)
self.tp_conv5 = nn.Conv3d(_c, 1, kernel_size=3, stride=1, padding=1, bias=False)
def forward(self, noise):
noise = noise.view(-1, 1000)
h = self.fc(noise)
h = h.view(-1,512,4,4,4)
h = F.relu(self.bn1(h))
h = F.upsample(h,scale_factor = 2)
h = self.tp_conv2(h)
h = F.relu(self.bn2(h))
h = F.upsample(h,scale_factor = 2)
h = self.tp_conv3(h)
h = F.relu(self.bn3(h))
h = F.upsample(h,scale_factor = 2)
h = self.tp_conv4(h)
h = F.relu(self.bn4(h))
h = F.upsample(h,scale_factor = 2)
h = self.tp_conv5(h)
h = F.tanh(h)
return h