-
Notifications
You must be signed in to change notification settings - Fork 0
/
VisionModels.py
91 lines (78 loc) · 2.53 KB
/
VisionModels.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
import os
import numpy as np
import random
import torch
import torch.nn as nn
from typing import Optional, List
from torchvision.models import efficientnet_b3
class CustomCNN(nn.Module):
def __init__(self,
random_seed: Optional[int] = None) -> None:
super(CustomCNN, self).__init__()
self.linear_relu_stack = nn.Sequential(
nn.Conv2d(3, 8, 3, stride=2),
nn.ReLU(),
nn.Conv2d(8, 16, 3),
nn.ReLU(),
nn.Conv2d(16, 32, 3),
nn.ReLU(),
nn.AvgPool2d(3, stride=2),
nn.ReLU(),
nn.Conv2d(32, 16, 3),
nn.ReLU(),
nn.Conv2d(16, 8, 3),
nn.ReLU(),
nn.Conv2d(8, 4, 3),
nn.ReLU(),
nn.AvgPool2d(5, stride=2),
nn.ReLU(),
nn.Flatten(start_dim=1, end_dim=-1),
nn.Linear(2116, 100),
nn.ReLU(),
nn.Linear(100, 2),
)
self.metrics = {
'train_loss': [],
'train_acc': [],
'val_loss': [],
'val_acc': []
}
self.states = []
if random_seed:
os.environ['PYTHONHASHSEED'] = str(random_seed)
torch.manual_seed(random_seed)
torch.cuda.manual_seed(random_seed)
np.random.seed(random_seed)
random.seed(random_seed)
def forward(self, X):
return self.linear_relu_stack(X)
class CustomEfficientNetB3(nn.Module):
def __init__(self,
num_classes: int = 2,
pretrained: bool = True,
random_seed: Optional[int] = None):
super(CustomEfficientNetB3, self).__init__()
self.base_model = efficientnet_b3(pretrained=pretrained)
self.dropout = nn.Dropout(p=0.3, inplace=True)
self.fc = nn.Linear(
self.base_model.classifier[1].in_features, num_classes)
# Replace the classifier of the base model
self.base_model.classifier = nn.Sequential(
self.dropout,
self.fc
)
self.metrics = {
'train_loss': [],
'train_acc': [],
'val_loss': [],
'val_acc': []
}
self.states = []
if random_seed:
os.environ['PYTHONHASHSEED'] = str(random_seed)
torch.manual_seed(random_seed)
torch.cuda.manual_seed(random_seed)
np.random.seed(random_seed)
random.seed(random_seed)
def forward(self, x):
return self.base_model(x)