Newer
Older
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# ------------------------------------------------------------------
# _____ _ _ _
# | ___(_) __| | | ___
# | |_ | |/ _` | |/ _ \
# | _| | | (_| | | __/
# |_| |_|\__,_|_|\___| GAN / Generators
# ------------------------------------------------------------------
# Formation Introduction au Deep Learning (FIDLE)
# CNRS/MIAI - https://fidle.cnrs.fr
# ------------------------------------------------------------------
# JL Parouty (Mars 2024)
import numpy as np
import torch.nn as nn
# -----------------------------------------------------------------------------
# -- Discriminator n°1
# -----------------------------------------------------------------------------
#
class Discriminator_1(nn.Module):
'''
A basic DNN discriminator, usable with classic GAN
'''
def __init__(self, latent_dim=None, data_shape=None):
super().__init__()
self.img_shape = data_shape
print('init discriminator 1 : ',data_shape,' to sigmoid')
self.model = nn.Sequential(
nn.Flatten(),
nn.Linear(int(np.prod(data_shape)), 512),
nn.ReLU(),
nn.Linear(512, 256),
nn.ReLU(),
nn.Linear(256, 1),
nn.Sigmoid(),
)
def forward(self, img):
validity = self.model(img)
return validity
# -----------------------------------------------------------------------------
# -- Discriminator n°2
# -----------------------------------------------------------------------------
#
class Discriminator_2(nn.Module):
'''
A more efficient discriminator,based on CNN, usable with classic GAN
'''
def __init__(self, latent_dim=None, data_shape=None):
super().__init__()
self.img_shape = data_shape
print('init discriminator 2 : ',data_shape,' to sigmoid')
self.model = nn.Sequential(
nn.Conv2d(1, 32, kernel_size = 3, stride = 2, padding = 1),
nn.ReLU(),
nn.BatchNorm2d(32),
nn.Dropout2d(0.25),
nn.Conv2d(32, 64, kernel_size = 3, stride = 1, padding = 1),
nn.ReLU(),
nn.BatchNorm2d(64),
nn.Dropout2d(0.25),
nn.Conv2d(64, 128, kernel_size = 3, stride = 1, padding = 1),
nn.ReLU(),
nn.BatchNorm2d(128),
nn.Dropout2d(0.25),
nn.Conv2d(128, 256, kernel_size = 3, stride = 2, padding = 1),
nn.ReLU(),
nn.BatchNorm2d(256),
nn.Dropout2d(0.25),
nn.Flatten(),
nn.Linear(12544, 1),
nn.Sigmoid(),
)
def forward(self, img):
img_nchw = img.permute(0, 3, 1, 2) # reformat from NHWC to NCHW
validity = self.model(img_nchw)
return validity
# -----------------------------------------------------------------------------
# -- Discriminator n°3
# -----------------------------------------------------------------------------
#
class Discriminator_3(nn.Module):
'''
A CNN discriminator, usable with a WGANGP.
This discriminator has no sigmoid and returns a critical and not a probability
'''
def __init__(self, latent_dim=None, data_shape=None):
super().__init__()
self.img_shape = data_shape
print('init discriminator 3 : ',data_shape,' to sigmoid')
self.model = nn.Sequential(
nn.Conv2d(1, 32, kernel_size = 3, stride = 2, padding = 1),
nn.ReLU(),
nn.BatchNorm2d(32),
nn.Dropout2d(0.25),
nn.Conv2d(32, 64, kernel_size = 3, stride = 1, padding = 1),
nn.ReLU(),
nn.BatchNorm2d(64),
nn.Dropout2d(0.25),
nn.Conv2d(64, 128, kernel_size = 3, stride = 1, padding = 1),
nn.ReLU(),
nn.BatchNorm2d(128),
nn.Dropout2d(0.25),
nn.Conv2d(128, 256, kernel_size = 3, stride = 2, padding = 1),
nn.ReLU(),
nn.BatchNorm2d(256),
nn.Dropout2d(0.25),
nn.Flatten(),
nn.Linear(12544, 1),
nn.Sigmoid(),
)
def forward(self, img):
img_nchw = img.permute(0, 3, 1, 2) # reformat from NHWC to NCHW
validity = self.model(img_nchw)
return validity