forked from Kabongosalomon/Cat-vs-Dog-Classifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
75 lines (63 loc) · 2.77 KB
/
model.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
# Attempt to build a network to classify cats vs dogs from this dataset
from torch import nn, optim
import torch.nn.functional as F
class CNN_Classifier_1(nn.Module):
def __init__(self, n_feature, output_size):
super(CNN_Classifier_1, self).__init__()
self.n_feature = n_feature
## Define layers of a CNN
self.conv1 = nn.Conv2d(3, n_feature*2**1, 3, padding=1, stride=2)
self.conv2 = nn.Conv2d(n_feature*2**1, n_feature*2**2, 3, padding=1, stride=2)
self.conv3 = nn.Conv2d(n_feature*2**2, n_feature*2**3, 3, padding=1)
# max pooling layer
self.pool = nn.MaxPool2d(2, 2)
self.fc1 = nn.Linear(128*7*7, 500)
self.fc2 = nn.Linear(500, output_size)
self.dropout = nn.Dropout(0.3)
def forward(self, x, verbose=False):
## Define forward behavior~
x = self.pool(F.relu(self.conv1(x)))
x = self.pool(F.relu(self.conv2(x)))
x = self.pool(F.relu(self.conv3(x)))
# flatten image input
x = x.view(-1, 128*7*7)
# add dropout layer
x = self.dropout(x)
# add 1st hidden layer, with relu activation function
x = F.relu(self.fc1(x))
# add dropout layer
x = self.dropout(x)
# add 2nd hidden layer, with relu activation function
x = self.fc2(x)
return x
class CNN_Classifier_2(nn.Module):
def __init__(self, n_feature, output_size):
super(CNN_Classifier_2, self).__init__()
self.n_feature = n_feature
## Define layers of a CNN
self.conv1 = nn.Conv2d(3, n_feature*2**1, 3, padding=1, stride=2)
self.conv2 = nn.Conv2d(n_feature*2**1, n_feature*2**2, 3, padding=1, stride=2)
self.conv3 = nn.Conv2d(n_feature*2**2, n_feature*2**3, 3, padding=1)
self.conv4 = nn.Conv2d(n_feature*2**3, n_feature*2**4, 3, padding=1, stride=2)
# max pooling layer
self.pool = nn.MaxPool2d(2, 2)
self.fc1 = nn.Linear(256*2*2, 500) # Computer this automatically using scrept in the dog_breed classifier notebook
self.fc2 = nn.Linear(500, output_size)
self.dropout = nn.Dropout(0.3)
def forward(self, x, verbose=False):
## Define forward behavior~
x = self.pool(F.relu(self.conv1(x)))
x = self.pool(F.relu(self.conv2(x)))
x = self.pool(F.relu(self.conv3(x)))
x = self.pool(F.relu(self.conv4(x)))
# flatten image input
x = x.view(-1, 256*2*2)
# add dropout layer
x = self.dropout(x)
# add 1st hidden layer, with relu activation function
x = F.relu(self.fc1(x))
# add dropout layer
x = self.dropout(x)
# add 2nd hidden layer, with relu activation function
x = self.fc2(x)
return x