-
Notifications
You must be signed in to change notification settings - Fork 1
/
cnn_models.py
128 lines (109 loc) · 5.19 KB
/
cnn_models.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
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
import torch
from torch import nn as nn
from torch.nn import functional as F
from batchbald_redux import consistent_mc_dropout
class CNN_MC_RMNIST(consistent_mc_dropout.BayesianModule):
def __init__(self, num_classes=10):
super().__init__()
self.conv1 = nn.Conv2d(1, 32, kernel_size=5)
self.conv1_drop = consistent_mc_dropout.ConsistentMCDropout2d()
self.conv2 = nn.Conv2d(32, 64, kernel_size=5)
self.conv2_drop = consistent_mc_dropout.ConsistentMCDropout2d()
self.fc1 = nn.Linear(1024, 128)
self.fc1_drop = consistent_mc_dropout.ConsistentMCDropout()
self.fc2 = nn.Linear(128, num_classes)
def mc_forward_impl(self, input: torch.Tensor):
input = F.relu(F.max_pool2d(self.conv1_drop(self.conv1(input)), 2))
input = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(input)), 2))
input = input.view(-1, 1024)
input = F.relu(self.fc1_drop(self.fc1(input)))
input = self.fc2(input)
input = F.log_softmax(input, dim=1)
return input
class CNN_ENS_RMNIST(nn.Module):
def __init__(self, num_classes=10):
super().__init__()
self.conv1 = nn.Conv2d(1, 32, kernel_size=5)
self.conv1_drop = nn.Dropout(p=0.25)
self.conv2 = nn.Conv2d(32, 64, kernel_size=5)
self.conv2_drop = nn.Dropout(p=0.25)
self.fc1 = nn.Linear(1024, 128)
self.fc1_drop = nn.Dropout(p=0.5)
self.fc2 = nn.Linear(128, num_classes)
def forward(self, input: torch.Tensor):
input = F.relu(F.max_pool2d(self.conv1_drop(self.conv1(input)), 2))
input = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(input)), 2))
input = input.view(-1, 1024)
input = F.relu(self.fc1_drop(self.fc1(input)))
input = self.fc2(input)
input = F.log_softmax(input, dim=1)
return input
class CNN_MC_EMNIST(consistent_mc_dropout.BayesianModule):
def __init__(self, num_classes=47):
super().__init__()
self.conv1 = nn.Conv2d(1, 32, kernel_size=3)
self.conv1_drop = consistent_mc_dropout.ConsistentMCDropout2d()
self.conv2 = nn.Conv2d(32, 64, kernel_size=3)
self.conv2_drop = consistent_mc_dropout.ConsistentMCDropout2d()
self.conv3 = nn.Conv2d(64, 128, kernel_size=3)
self.conv3_drop = consistent_mc_dropout.ConsistentMCDropout2d()
self.fc1 = nn.Linear(128 * 4 * 4, 512)
self.fc1_drop = consistent_mc_dropout.ConsistentMCDropout()
self.fc2 = nn.Linear(512, num_classes)
def mc_forward_impl(self, input: torch.Tensor):
input = F.relu(F.max_pool2d(self.conv1_drop(self.conv1(input)), 2))
input = F.relu(self.conv2_drop(self.conv2(input)))
input = F.relu(F.max_pool2d(self.conv3_drop(self.conv3(input)), 2))
input = input.view(-1, 128 * 4 * 4)
input = F.relu(self.fc1_drop(self.fc1(input)))
input = self.fc2(input)
input = F.log_softmax(input, dim=1)
return input
class CNN_ENS_EMNIST(nn.Module):
def __init__(self, num_classes=47):
super().__init__()
self.conv1 = nn.Conv2d(1, 32, kernel_size=3)
self.conv1_drop = nn.Dropout(p=0.25)
self.conv2 = nn.Conv2d(32, 64, kernel_size=3)
self.conv2_drop = nn.Dropout(p=0.25)
self.conv3 = nn.Conv2d(64, 128, kernel_size=3)
self.conv3_drop = nn.Dropout(p=0.25)
self.fc1 = nn.Linear(128 * 4 * 4, 512)
self.fc1_drop = nn.Dropout(p=0.5)
self.fc2 = nn.Linear(512, num_classes)
self.relu = nn.ReLU(inplace=True)
self.max_pool2d = nn.MaxPool2d(kernel_size=2)
def forward(self, input: torch.Tensor):
input = F.relu(self.max_pool2d(self.conv1_drop(self.conv1(input))))
input = F.relu(self.conv2_drop(self.conv2(input)))
input = F.relu(self.max_pool2d(self.conv3_drop(self.conv3(input))))
input = input.view(-1, 128 * 4 * 4)
input = F.relu(self.fc1_drop(self.fc1(input)))
input = self.fc2(input)
input = F.log_softmax(input, dim=1)
return input
class CNN_ENS_CIFAR10(nn.Module):
def __init__(self, num_classes=10):
super().__init__()
self.conv1 = nn.Conv2d(3, 32, kernel_size=3)
self.conv1_drop = nn.Dropout(p=0.25)
self.conv2 = nn.Conv2d(32, 32, kernel_size=3)
self.conv2_drop = nn.Dropout(p=0.25)
self.conv3 = nn.Conv2d(32, 64, kernel_size=3)
self.conv3_drop = nn.Dropout(p=0.25)
self.conv4 = nn.Conv2d(64, 64, kernel_size=3)
self.conv4_drop = nn.Dropout(p=0.25)
self.fc1_drop = nn.Dropout(p=0.5)
self.fc2 = nn.Linear(64*5*5, num_classes)
self.relu = nn.ReLU(inplace=True)
self.max_pool2d = nn.MaxPool2d(kernel_size=2)
def forward(self, input: torch.Tensor):
input = self.conv1_drop(self.relu(self.conv1(input)))
input = self.max_pool2d(self.conv2_drop(self.relu(self.conv2(input))))
input = self.conv3_drop(self.relu(self.conv3(input)))
input = self.max_pool2d(self.conv4_drop(self.relu(self.conv4(input))))
input = input.view(-1, 64 * 5 * 5)
input = self.fc1_drop(self.relu(input))
input = self.fc2(input)
input = F.log_softmax(input, dim=1)
return input