-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmodel_utils.py
53 lines (40 loc) · 1.52 KB
/
model_utils.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
'''
Animal Model Follows:
https://github.com/Aggarwal-Abhishek/BasicCNN_Pytorch
Weights can be downloaded from the above link and change the name to 'animal_model_50.pth' and 'animal_model_100.pth'
'''
import torch
import torch.nn as nn
torch_device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
class MyModel(nn.Module):
def __init__(self):
super(MyModel, self).__init__()
self.model = nn.Sequential(
nn.Conv2d(3, 16, kernel_size=3), nn.ReLU(),
nn.Conv2d(16, 16, kernel_size=3), nn.ReLU(),
nn.MaxPool2d(2, 2),
nn.Conv2d(16, 32, kernel_size=3), nn.ReLU(),
nn.Conv2d(32, 32, kernel_size=3), nn.ReLU(),
nn.MaxPool2d(2, 2),
nn.Conv2d(32, 64, kernel_size=3), nn.ReLU(),
nn.Conv2d(64, 64, kernel_size=3), nn.ReLU(),
nn.MaxPool2d(2, 2),
nn.Conv2d(64, 128, kernel_size=3), nn.ReLU(),
nn.Conv2d(128, 128, kernel_size=3), nn.ReLU(),
nn.MaxPool2d(2, 2),
nn.Conv2d(128, 256, kernel_size=3), nn.ReLU(),
nn.Conv2d(256, 256, kernel_size=3), nn.ReLU(),
nn.MaxPool2d(2, 2),
).to(torch_device)
self.classifier = nn.Sequential(
nn.Flatten(),
nn.Dropout(0.25),
nn.Linear(4096, 256),
nn.ReLU(),
nn.Dropout(0.5),
nn.Linear(256, 10)
).to(torch_device)
def forward(self, x):
x = self.model(x)
x = self.classifier(x)
return x