-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsurprise_estimation.py
65 lines (51 loc) · 2.22 KB
/
surprise_estimation.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
"""
Contains the model definition of the finetuned MobilenetV3 model to detect the source of a house music album cover (human-generated/ai-generated)
"""
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision.models as models
class CreativeNet(nn.Module):
def __init__(self, train_baseline_classifier = False, num_output_classes = 2, dropout_rate = 0.20):
super().__init__()
# Set instance variables
self.train_baseline_classifier = train_baseline_classifier
self.num_outuput_classes = num_output_classes
self.dropout_rate = dropout_rate
# Set the current device for tensor calculations
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# Baseline: MobileNet V3 small
self.baseline = models.mobilenet_v3_small(weights = models.MobileNet_V3_Small_Weights.IMAGENET1K_V1)
# Freeze the parameters of the base model (including but not limited to the last layers)
for param in self.baseline.parameters():
param.requires_grad = False
if self.train_baseline_classifier:
for param in self.baseline.classifier.parameters():
param.requires_grad = True
# Fully-connected block
self.fc1 = nn.Linear(1000, 128)
self.dropout1 = nn.Dropout(self.dropout_rate)
self.fc2 = nn.Linear(128, 32)
self.dropout2 = nn.Dropout(self.dropout_rate)
self.fc3 = nn.Linear(32, self.num_outuput_classes)
def forward(self, x):
# Baseline
x = x.to(self.device)
x = self.baseline(x)
# FC Block
x = F.leaky_relu(self.fc1(x))
x = self.dropout1(x)
x = F.leaky_relu(self.fc2(x))
x = self.dropout2(x)
x = F.leaky_relu(self.fc3(x))
x = torch.sigmoid(x)
return x
if __name__ == "__main__":
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(f"Using device {device}")
MODEL_ARGS = {
"train_baseline_classifier" : False,
"num_output_classes" : 2,
"dropout_rate" : 0.35
}
model = CreativeNet(**MODEL_ARGS).to(device)