-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetricsClassifier.py
More file actions
126 lines (108 loc) · 4.39 KB
/
Copy pathmetricsClassifier.py
File metadata and controls
126 lines (108 loc) · 4.39 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
"""
Classifiers implementation for evaluation process
"""
import torch
import torch.nn as nn
from models.inceptionTime import InceptionBlock
class Flatten(nn.Module):
def __init__(self, out_features):
super(Flatten, self).__init__()
self.output_dim = out_features
def forward(self, x):
return x.view(-1, self.output_dim)
class Reshape(nn.Module):
def __init__(self, out_shape):
super(Reshape, self).__init__()
self.out_shape = out_shape
def forward(self, x):
return x.view(-1, *self.out_shape)
class Classifier(nn.Module):
def __init__(self, dataset_name, label_dim, time_step=None):
"""
Classifiers implementation for evaluation process
Args:
dataset_name (str): Name of the dataset.
label_dim (int): Number of classes in the dataset.
time_step (int): Number of time steps in the dataset.
"""
super(Classifier, self).__init__()
self.dataset_name = dataset_name
self.label_dim = label_dim
self.time_step = time_step
if self.dataset_name == "MNIST":
self.ClassifierModel = nn.Sequential(
nn.Conv2d(1, 32, kernel_size=5, stride=1),
nn.ReLU(),
nn.Conv2d(32, 64, kernel_size=5, stride=1),
nn.ReLU(),
nn.Conv2d(64, 128, kernel_size=5, stride=1),
nn.ReLU(),
nn.AdaptiveAvgPool2d(output_size=1),
Flatten(out_features=128),
nn.Linear(128, self.label_dim),
)
else:
self.ClassifierModel = nn.Sequential(
Reshape(out_shape=(1, self.time_step)),
InceptionBlock(
in_channels=1,
n_filters=32,
kernel_sizes=[5, 11, 23],
bottleneck_channels=32,
use_residual=True,
activation=nn.ReLU()
),
InceptionBlock(
in_channels=32 * 4,
n_filters=32,
kernel_sizes=[5, 11, 23],
bottleneck_channels=32,
use_residual=True,
activation=nn.ReLU()
),
InceptionBlock(
in_channels=32 * 4,
n_filters=32,
kernel_sizes=[5, 11, 23],
bottleneck_channels=32,
use_residual=True,
activation=nn.ReLU()
),
InceptionBlock(
in_channels=32 * 4,
n_filters=32,
kernel_sizes=[5, 11, 23],
bottleneck_channels=32,
use_residual=True,
activation=nn.ReLU()
),
InceptionBlock(
in_channels=32 * 4,
n_filters=32,
kernel_sizes=[5, 11, 23],
bottleneck_channels=32,
use_residual=True,
activation=nn.ReLU()
),
nn.AdaptiveAvgPool1d(output_size=1),
Flatten(out_features=32 * 4 * 1),
nn.Linear(in_features=4 * 32 * 1, out_features=self.label_dim)
)
if torch.cuda.is_available():
self.ClassifierModel.cuda()
def load(self, metric, domain):
"""
Loading best trained classifier based on IS and FID metrics
Args:
metric: Name of the evaluation metric: IS/FID.
domain: Name of the domain: image/time-series.
"""
model_path = "./classifiers/best_classifier_{}.torch".format(self.dataset_name)
state = 'best_state_' if domain == "image" else 'best_inception_state_'
if metric == "IS":
self.ClassifierModel.load_state_dict(torch.load(model_path, map_location=torch.device('cpu'))
[state + 'is'], strict=False)
elif metric == "FID":
self.ClassifierModel.load_state_dict(torch.load(model_path, map_location=torch.device('cpu'))
[state + 'fid'], strict=False)
return self.ClassifierModel