-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcritic.py
More file actions
55 lines (45 loc) · 1.54 KB
/
Copy pathcritic.py
File metadata and controls
55 lines (45 loc) · 1.54 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import torch
from torch import nn
class BasicCritic(nn.Module):
"""
The BasicCritic module takes an image and predicts whether it is a cover
image or a steganographic image (N, 1).
Input: (N, 3, H, W)
Output: (N, 1)
"""
def _conv2d(self, in_channels, out_channels):
return nn.Conv2d(
in_channels=in_channels,
out_channels=out_channels,
kernel_size=3
)
def _build_models(self):
self.conv1 = nn.Sequential(
self._conv2d(3, self.hidden_size),
nn.LeakyReLU(inplace=True),
nn.BatchNorm2d(self.hidden_size),
)
self.conv2 = nn.Sequential(
self._conv2d(self.hidden_size, self.hidden_size),
nn.LeakyReLU(inplace=True),
nn.BatchNorm2d(self.hidden_size),
)
self.conv3 = nn.Sequential(
self._conv2d(self.hidden_size, self.hidden_size),
nn.LeakyReLU(inplace=True),
nn.BatchNorm2d(self.hidden_size),
)
self.conv4 = nn.Sequential(
self._conv2d(self.hidden_size, 1)
)
return self.conv1,self.conv2,self.conv3,self.conv4
def __init__(self, hidden_size):
super().__init__()
self.hidden_size = hidden_size
self._models = self._build_models()
def forward(self, image):
x = self._models[0](image)
x_1 = self._models[1](x)
x_2 = self._models[2](x_1)
x_3 = self._models[3](x_2)
return torch.mean(x_3.view(x_3.size(0), -1), dim=1)