-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCNN_Models.py
102 lines (81 loc) · 3.26 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
import torch
import torch.nn as nn
import torch.nn.functional as F
class MalCnnOne(nn.Module):
CNN_name = "Model One"
def __init__(self, dropout=0.5, image_size=256):
super(MalCnnOne, self).__init__()
self.dropout=dropout
self.image_size=image_size
#Convolutions
self.conv1 = nn.Conv2d(1, 16, 3, padding=1)
self.norm1 = nn.BatchNorm2d(16)
self.conv2 = nn.Conv2d(16, 32, 3, padding=1)
self.norm2 = nn.BatchNorm2d(32)
self.conv3 = nn.Conv2d(32, 64, 3, padding=1)
self.norm3 = nn.BatchNorm2d(64)
#Pooling layer
self.pool = nn.MaxPool2d(2, 2)
#Fully connected layers
self.fc1 = nn.Linear(self.image_size*self.image_size,512)
self.norm4 = nn.BatchNorm1d(512)
self.fc2 = nn.Linear(512, 256)
self.norm5 = nn.BatchNorm1d(256)
self.fc3 = nn.Linear(256, 2)
#flattener
self.flattening = torch.nn.Flatten()
#Dropout
self.dropout = nn.Dropout(self.dropout)
def forward(self, x):
x = self.pool(F.relu(self.norm1(self.conv1(x))))
x = self.pool(F.relu(self.norm2(self.conv2(x))))
x = self.pool(F.relu(self.norm3(self.conv3(x))))
x = self.flattening(x)
x = self.dropout(F.relu(self.norm4(self.fc1(x))))
x = self.dropout(F.relu(self.norm5(self.fc2(x))))
x = F.log_softmax(self.fc3(x),dim=1)
return x
class MalCnnTwo(nn.Module):
CNN_name = "Model Two"
def __init__(self, dropout=0.5, image_dim=64):
super(MalCnnTwo, self).__init__()
self.dropout=dropout
self.image_dim=image_dim
#Convolutions
self.conv1 = nn.Conv2d(1, 16, 3, padding=1)
self.norm1 = nn.BatchNorm2d(16)
self.conv2 = nn.Conv2d(16, 32, 3, padding=1)
self.norm2 = nn.BatchNorm2d(32)
self.conv3 = nn.Conv2d(32, 64, 3, padding=1)
self.norm3 = nn.BatchNorm2d(64)
self.conv4 = nn.Conv2d(64, 128, 3, padding=1)
self.norm4 = nn.BatchNorm2d(128)
self.conv5 = nn.Conv2d(128, 256, 3, padding=1)
self.norm5 = nn.BatchNorm2d(256)
#Pooling layer
self.pool = nn.MaxPool2d(2, 2)
#Fully connected layers
self.temp = int((self.image_dim*self.image_dim)/4)
self.fc1 = nn.Linear(self.temp,1024)
self.norm6 = nn.BatchNorm1d(1024)
self.fc2 = nn.Linear(1024, 512)
self.norm7 = nn.BatchNorm1d(512)
self.fc3 = nn.Linear(512, 256)
self.norm8 = nn.BatchNorm1d(256)
self.fc4 = nn.Linear(256, 2)
#Flattening
self.flattener = torch.nn.Flatten()
#Dropout
self.dropout = nn.Dropout(self.dropout)
def forward(self, x):
x = self.pool(F.relu(self.norm1(self.conv1(x))))
x = self.pool(F.relu(self.norm2(self.conv2(x))))
x = self.pool(F.relu(self.norm3(self.conv3(x))))
x = self.pool(F.relu(self.norm4(self.conv4(x))))
x = self.pool(F.relu(self.norm5(self.conv5(x))))
x=self.flattener(x)
x = self.dropout(F.relu(self.norm6(self.fc1(x))))
x = self.dropout(F.relu(self.norm7(self.fc2(x))))
x = self.dropout(F.relu(self.norm8(self.fc3(x))))
x = F.log_softmax(self.fc4(x),dim=1)
return x