iapytoo 0.0.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- iapytoo/__init__.py +0 -0
- iapytoo/dataset/__init__.py +68 -0
- iapytoo/dataset/scaling.py +98 -0
- iapytoo/metrics/__init__.py +82 -0
- iapytoo/metrics/distrib.py +19 -0
- iapytoo/metrics/metric.py +39 -0
- iapytoo/metrics/predefined.py +73 -0
- iapytoo/predictions/__init__.py +78 -0
- iapytoo/predictions/feature.py +16 -0
- iapytoo/predictions/plotters.py +146 -0
- iapytoo/predictions/predictors.py +15 -0
- iapytoo/predictions/types.py +9 -0
- iapytoo/train/__init__.py +0 -0
- iapytoo/train/checkpoint.py +44 -0
- iapytoo/train/context.py +35 -0
- iapytoo/train/factories.py +215 -0
- iapytoo/train/logger.py +190 -0
- iapytoo/train/loss.py +33 -0
- iapytoo/train/training.py +417 -0
- iapytoo/train/wgan.py +256 -0
- iapytoo/utils/__init__.py +0 -0
- iapytoo/utils/arguments.py +45 -0
- iapytoo/utils/config.py +123 -0
- iapytoo/utils/display.py +93 -0
- iapytoo/utils/iterative_mean.py +65 -0
- iapytoo/utils/meta_singleton.py +17 -0
- iapytoo/utils/time_utils.py +40 -0
- iapytoo/utils/timer.py +55 -0
- iapytoo-0.0.2.dist-info/LICENSE +201 -0
- iapytoo-0.0.2.dist-info/METADATA +22 -0
- iapytoo-0.0.2.dist-info/RECORD +33 -0
- iapytoo-0.0.2.dist-info/WHEEL +5 -0
- iapytoo-0.0.2.dist-info/top_level.txt +1 -0
iapytoo/__init__.py
ADDED
|
File without changes
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
from typing import Any, Tuple
|
|
3
|
+
from torchvision.datasets.vision import VisionDataset
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class DummyVisionDataset(VisionDataset):
|
|
7
|
+
def __init__(self):
|
|
8
|
+
super().__init__("./dummy")
|
|
9
|
+
|
|
10
|
+
self.length = 10
|
|
11
|
+
self.dim = 4
|
|
12
|
+
self.n_channels = 3
|
|
13
|
+
np.random.seed(12)
|
|
14
|
+
A = np.array(range(self.dim * self.dim)).reshape((self.dim, self.dim))
|
|
15
|
+
self.images = np.zeros(
|
|
16
|
+
shape=(self.length, self.n_channels,
|
|
17
|
+
self.dim, self.dim), dtype=np.float32
|
|
18
|
+
)
|
|
19
|
+
for idx in range(self.length):
|
|
20
|
+
for c in range(self.n_channels):
|
|
21
|
+
self.images[idx, c, :, :] = (
|
|
22
|
+
A + np.random.normal(size=(self.dim, self.dim)) - c * 3
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
self.targets = np.vstack(
|
|
26
|
+
[
|
|
27
|
+
np.array(range(self.length)) +
|
|
28
|
+
np.random.normal(size=(self.length)),
|
|
29
|
+
np.array(range(self.length)) +
|
|
30
|
+
np.random.normal(size=(self.length)),
|
|
31
|
+
]
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
def __len__(self) -> int:
|
|
35
|
+
return self.length
|
|
36
|
+
|
|
37
|
+
def __getitem__(self, index: int) -> Tuple[Any, Any]:
|
|
38
|
+
return self.images[index], self.targets[:, index]
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class DummyLabelDataset(VisionDataset):
|
|
42
|
+
def __init__(self):
|
|
43
|
+
super().__init__("./dummy")
|
|
44
|
+
|
|
45
|
+
self.length = 10
|
|
46
|
+
self.dim = 4
|
|
47
|
+
self.n_channels = 3
|
|
48
|
+
self.n_labels = 7
|
|
49
|
+
np.random.seed(12)
|
|
50
|
+
A = np.array(range(self.dim * self.dim)).reshape((self.dim, self.dim))
|
|
51
|
+
self.images = np.zeros(
|
|
52
|
+
shape=(self.length, self.n_channels,
|
|
53
|
+
self.dim, self.dim), dtype=np.float32
|
|
54
|
+
)
|
|
55
|
+
for idx in range(self.length):
|
|
56
|
+
for c in range(self.n_channels):
|
|
57
|
+
self.images[idx, c, :, :] = (
|
|
58
|
+
A + np.random.normal(size=(self.dim, self.dim)) - c * 3
|
|
59
|
+
)
|
|
60
|
+
self.targets = np.random.randint(
|
|
61
|
+
0, self.n_labels, dtype=np.int64, size=self.length
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
def __len__(self) -> int:
|
|
65
|
+
return self.length
|
|
66
|
+
|
|
67
|
+
def __getitem__(self, index: int) -> Tuple[Any, Any]:
|
|
68
|
+
return self.images[index], self.targets[index]
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
from enum import IntEnum
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class NormalizationType(IntEnum):
|
|
6
|
+
Standard = 1
|
|
7
|
+
LogMinMax = 2
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class OpType(IntEnum):
|
|
11
|
+
MIN = 0
|
|
12
|
+
MAX = 1
|
|
13
|
+
MEAN = 2
|
|
14
|
+
STD = 3
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class Scaling:
|
|
18
|
+
"""
|
|
19
|
+
Base class that uses statistics to normalize between [0, 1] or standardize
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
fields = ["min", "max", "mean", "std"]
|
|
23
|
+
|
|
24
|
+
@staticmethod
|
|
25
|
+
def create(kind: str, stats, columns):
|
|
26
|
+
scaling = None
|
|
27
|
+
if kind == "min_max":
|
|
28
|
+
scaling = MinMax(stats, columns=columns)
|
|
29
|
+
elif kind == "normal":
|
|
30
|
+
scaling = Normalize(stats, columns=columns)
|
|
31
|
+
else:
|
|
32
|
+
print(f"no way to normalize with {kind} kind -> not normalizing!")
|
|
33
|
+
|
|
34
|
+
return scaling
|
|
35
|
+
|
|
36
|
+
def __init__(self, stats, columns) -> None:
|
|
37
|
+
self.data = stats
|
|
38
|
+
self.weights = np.zeros(shape=(len(Normalize.fields), len(columns)))
|
|
39
|
+
for row, f in enumerate(self.fields):
|
|
40
|
+
for col, d in enumerate(columns):
|
|
41
|
+
self.weights[row, col] = self.data[d][f]
|
|
42
|
+
|
|
43
|
+
def value(self, name, field):
|
|
44
|
+
return self.data[name][field]
|
|
45
|
+
|
|
46
|
+
def __call__(self, x):
|
|
47
|
+
pass
|
|
48
|
+
|
|
49
|
+
def inv(self, x):
|
|
50
|
+
pass
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
class Normalize(Scaling):
|
|
54
|
+
"""
|
|
55
|
+
Base class that uses statistics to normalize between [0, 1] or standardize
|
|
56
|
+
"""
|
|
57
|
+
|
|
58
|
+
fields = ["min", "max", "mean", "std"]
|
|
59
|
+
|
|
60
|
+
def __init__(self, stats, columns) -> None:
|
|
61
|
+
super().__init__(stats, columns)
|
|
62
|
+
|
|
63
|
+
def __call__(self, x):
|
|
64
|
+
return (x - self.weights[OpType.MEAN]) / self.weights[OpType.STD]
|
|
65
|
+
|
|
66
|
+
def inv(self, x):
|
|
67
|
+
return x * self.weights[OpType.STD] + self.weights[OpType.MEAN]
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
class MinMax(Scaling):
|
|
71
|
+
def __init__(self, stats, columns) -> None:
|
|
72
|
+
super().__init__(stats, columns)
|
|
73
|
+
|
|
74
|
+
def __call__(self, x):
|
|
75
|
+
return (x - self.weights[OpType.MIN]) / (
|
|
76
|
+
self.weights[OpType.MAX] - self.weights[OpType.MIN]
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
def inv(self, x):
|
|
80
|
+
return (
|
|
81
|
+
x * (self.weights[OpType.MAX] - self.weights[OpType.MIN])
|
|
82
|
+
+ self.weights[OpType.MIN]
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
# TODO
|
|
87
|
+
# class LogNormalize(Normalize):
|
|
88
|
+
|
|
89
|
+
# def __init__(self, stats, x_columns, y_columns) -> None:
|
|
90
|
+
# super().__init__(stats)
|
|
91
|
+
|
|
92
|
+
# def __call__(self, X, Y):
|
|
93
|
+
# x_min = np.log(self.wx[0, :])
|
|
94
|
+
# x_max = np.log(self.wx[1, :])
|
|
95
|
+
# return (np.log(X) - x_min) / (x_max -x_min), (Y - self.wy[2, :]) / self.wy[3, :]
|
|
96
|
+
|
|
97
|
+
# def reverse_Y(self, Y, idx=0):
|
|
98
|
+
# return Y * self.wy[3, idx] + self.wy[2, idx]
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
|
|
3
|
+
from iapytoo.utils.meta_singleton import MetaSingleton
|
|
4
|
+
|
|
5
|
+
from .predefined import R2Metric, RMSMetric, MSMetric, AccuracyMetric
|
|
6
|
+
from .metric import Metric
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class MetricsCollection(Metric):
|
|
10
|
+
def __init__(self, tag: str, metric_names: list, config):
|
|
11
|
+
super().__init__(tag, config)
|
|
12
|
+
self.metrics = {}
|
|
13
|
+
factory = MetricFactory()
|
|
14
|
+
try:
|
|
15
|
+
for n in metric_names:
|
|
16
|
+
self.metrics[f"{tag}_{n}"] = factory.create_metric(n, config)
|
|
17
|
+
except MetricError as er:
|
|
18
|
+
print(f"Unable to create metric : {er}")
|
|
19
|
+
|
|
20
|
+
def to(self, device):
|
|
21
|
+
for m in self.metrics.values():
|
|
22
|
+
m.to(device)
|
|
23
|
+
|
|
24
|
+
# overwrite
|
|
25
|
+
def update(self, Y_hat, Y):
|
|
26
|
+
for m in self.metrics.values():
|
|
27
|
+
m.update(Y_hat, Y)
|
|
28
|
+
|
|
29
|
+
# overwrite
|
|
30
|
+
def compute(self):
|
|
31
|
+
self.results = {}
|
|
32
|
+
for k, m in self.metrics.items():
|
|
33
|
+
results = m.compute()
|
|
34
|
+
for name, result in results.items():
|
|
35
|
+
# prepend the name of the collection to all inner results : validation_accuracy for example
|
|
36
|
+
self.results[f'{self.name}_{name}'] = result
|
|
37
|
+
return self.results
|
|
38
|
+
|
|
39
|
+
# overwrite
|
|
40
|
+
def reset(self) -> None:
|
|
41
|
+
for m in self.metrics.values():
|
|
42
|
+
m.reset()
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class MetricError(Exception):
|
|
46
|
+
def __init__(self, *args: object) -> None:
|
|
47
|
+
super().__init__(*args)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
class MetricFactory(metaclass=MetaSingleton):
|
|
51
|
+
def __init__(self) -> None:
|
|
52
|
+
self.metrics_dict = {
|
|
53
|
+
"r2": R2Metric,
|
|
54
|
+
"rms": RMSMetric,
|
|
55
|
+
"ms": MSMetric,
|
|
56
|
+
"accuracy": AccuracyMetric,
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
def register_metric(self, key, metric_cls):
|
|
60
|
+
self.metrics_dict[key] = metric_cls
|
|
61
|
+
|
|
62
|
+
def create_metric(self, kind: str, config: dict, device="cpu") -> Metric:
|
|
63
|
+
"""Creates an architecture of NN
|
|
64
|
+
|
|
65
|
+
Args:
|
|
66
|
+
kind (str): kind of metric, key for the factory
|
|
67
|
+
config (dict): config dict to use to initialize metric
|
|
68
|
+
device (str, optional): Defaults to "cpu".
|
|
69
|
+
|
|
70
|
+
Raises:
|
|
71
|
+
MetricError: error raised if no architecture fit kind key
|
|
72
|
+
|
|
73
|
+
Returns:
|
|
74
|
+
iapytoo.metrics.Metric: metric
|
|
75
|
+
"""
|
|
76
|
+
try:
|
|
77
|
+
metric: Metric = self.metrics_dict[kind](config)
|
|
78
|
+
except KeyError:
|
|
79
|
+
raise MetricError(f"metric {kind} is not handled")
|
|
80
|
+
|
|
81
|
+
metric = metric.to(device=device)
|
|
82
|
+
return metric
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
from iapytoo.predictions import Predictions
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class MetricsDistrib:
|
|
6
|
+
def __init__(self, metric):
|
|
7
|
+
self.metric = metric
|
|
8
|
+
self.distrib = None
|
|
9
|
+
|
|
10
|
+
def compute(self, predictions: Predictions, columns=None):
|
|
11
|
+
Y = predictions.actual
|
|
12
|
+
y_hat = predictions.outputs
|
|
13
|
+
|
|
14
|
+
self.distrib = np.zeros(shape=(Y.shape[0], Y.shape[1]))
|
|
15
|
+
for i in range(Y.shape[0]):
|
|
16
|
+
for j in range(Y.shape[1]):
|
|
17
|
+
self.distrib[i, j] = self.metric(Y[i, j, :], y_hat[i, j, :])
|
|
18
|
+
|
|
19
|
+
return self.distrib
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class Metric:
|
|
5
|
+
def __init__(self, name, config, with_target=True):
|
|
6
|
+
self.with_target = with_target
|
|
7
|
+
self.name = name
|
|
8
|
+
self.config = config
|
|
9
|
+
|
|
10
|
+
# First dimension is for the concatenation.
|
|
11
|
+
self.predicted = torch.zeros(size=(0,))
|
|
12
|
+
if self.with_target:
|
|
13
|
+
self.target = torch.zeros(size=(0,))
|
|
14
|
+
self.results = {}
|
|
15
|
+
|
|
16
|
+
@property
|
|
17
|
+
def device(self):
|
|
18
|
+
return self.predicted.device
|
|
19
|
+
|
|
20
|
+
def to(self, device):
|
|
21
|
+
self.predicted = self.predicted.to(device)
|
|
22
|
+
if self.with_target:
|
|
23
|
+
self.target = self.target.to(device)
|
|
24
|
+
return self
|
|
25
|
+
|
|
26
|
+
def update(self, Y_hat, Y):
|
|
27
|
+
self.predicted = torch.cat((self.predicted, Y_hat), dim=0)
|
|
28
|
+
if self.with_target:
|
|
29
|
+
self.target = torch.cat((self.target, Y), dim=0)
|
|
30
|
+
|
|
31
|
+
def reset(self):
|
|
32
|
+
device = self.device
|
|
33
|
+
self.predicted = torch.zeros(size=self.output_size, device=device)
|
|
34
|
+
if self.with_target:
|
|
35
|
+
self.target = torch.zeros(
|
|
36
|
+
size=self.output_size, device=self.device)
|
|
37
|
+
|
|
38
|
+
def compute(self):
|
|
39
|
+
pass
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
|
|
2
|
+
import torch
|
|
3
|
+
from iapytoo.metrics.metric import Metric
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class AccuracyMetric(Metric):
|
|
7
|
+
def __init__(self, config):
|
|
8
|
+
super(AccuracyMetric, self).__init__("accuracy", config)
|
|
9
|
+
self.k = config['top_accuracy']
|
|
10
|
+
|
|
11
|
+
def compute(self):
|
|
12
|
+
|
|
13
|
+
# Take care, for this metrics predicted and target do not have the same shape.
|
|
14
|
+
# predicted : ouputs of the models - kind of probabilities
|
|
15
|
+
# Target : label of the class
|
|
16
|
+
_, top_pred = self.predicted.topk(self.k, 1)
|
|
17
|
+
top_pred = top_pred.t()
|
|
18
|
+
correct = top_pred.eq(self.target.view(1, -1).expand_as(top_pred))
|
|
19
|
+
correct_1 = correct[:1].reshape(-1).float().sum(0, keepdim=True)
|
|
20
|
+
correct_k = correct[:self.k].reshape(-1).float().sum(0, keepdim=True)
|
|
21
|
+
|
|
22
|
+
return {
|
|
23
|
+
'top-1': torch.round(
|
|
24
|
+
100.0 * correct_1 / self.target.shape[0], decimals=2),
|
|
25
|
+
f'top-{self.k}': torch.round(
|
|
26
|
+
100.0 * correct_k / self.target.shape[0], decimals=2)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class R2Metric(Metric):
|
|
31
|
+
def __init__(self, config) -> None:
|
|
32
|
+
super(R2Metric, self).__init__("r2", config)
|
|
33
|
+
|
|
34
|
+
def compute(self):
|
|
35
|
+
# Compute the mean of the target values
|
|
36
|
+
target_mean = torch.mean(self.target, dim=0)
|
|
37
|
+
|
|
38
|
+
# Compute the total sum of squares (SS_tot)
|
|
39
|
+
ss_tot = torch.sum((self.target - target_mean) ** 2, dim=0)
|
|
40
|
+
|
|
41
|
+
# Compute the residual sum of squares (SS_res)
|
|
42
|
+
ss_res = torch.sum((self.target - self.predicted) ** 2, dim=0)
|
|
43
|
+
|
|
44
|
+
# Compute the R² score
|
|
45
|
+
r2_score = 1 - ss_res / ss_tot
|
|
46
|
+
|
|
47
|
+
# Return the mean R² score across all output dimensions
|
|
48
|
+
return {self.name: r2_score}
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
class MSMetric(Metric):
|
|
52
|
+
def __init__(self, config) -> None:
|
|
53
|
+
super(MSMetric, self).__init__("mean_square", config)
|
|
54
|
+
|
|
55
|
+
def _compute(self):
|
|
56
|
+
diff = self.predicted - self.target
|
|
57
|
+
return torch.mean(diff * diff, dim=0)
|
|
58
|
+
|
|
59
|
+
def compute(self):
|
|
60
|
+
ms = self.compute()
|
|
61
|
+
self.results = {self.name: ms}
|
|
62
|
+
return self.results
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
class RMSMetric(MSMetric):
|
|
66
|
+
def __init__(self, config) -> None:
|
|
67
|
+
Metric.__init__(self, "root_mean_square", config)
|
|
68
|
+
self.name = "rms"
|
|
69
|
+
|
|
70
|
+
def compute(self):
|
|
71
|
+
mean_squared_error = super()._compute()
|
|
72
|
+
self.results = {self.name: torch.sqrt(mean_squared_error)}
|
|
73
|
+
return self.results
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
import numpy as np
|
|
3
|
+
|
|
4
|
+
from .types import PredictionType
|
|
5
|
+
from .plotters import CollectionPlotters
|
|
6
|
+
from .predictors import Predictor
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class Predictions:
|
|
10
|
+
def __init__(self, predictor: Predictor, norm=False):
|
|
11
|
+
self.norm = norm
|
|
12
|
+
self.outputs = None # dimension is assigned in compute
|
|
13
|
+
self.actual = None
|
|
14
|
+
self.predictor = predictor
|
|
15
|
+
self.prediction_plotter: CollectionPlotters = CollectionPlotters()
|
|
16
|
+
self.prediction_plotter.connect(self)
|
|
17
|
+
|
|
18
|
+
def __len__(self):
|
|
19
|
+
"""return the number of plotters used by the predictions"""
|
|
20
|
+
return len(self.prediction_plotter)
|
|
21
|
+
|
|
22
|
+
def add_plotter(self, prediction_plotter):
|
|
23
|
+
self.prediction_plotter.add(prediction_plotter)
|
|
24
|
+
|
|
25
|
+
def compute(self, training, loader):
|
|
26
|
+
device = training.device
|
|
27
|
+
y_scaling = training.y_scaling
|
|
28
|
+
self.outputs = torch.zeros(size=(0,), device=device)
|
|
29
|
+
self.actual = torch.zeros(size=(0,), device=device)
|
|
30
|
+
|
|
31
|
+
model = training.model
|
|
32
|
+
model.eval()
|
|
33
|
+
with torch.no_grad():
|
|
34
|
+
for X, Y in loader:
|
|
35
|
+
X = X.to(device)
|
|
36
|
+
Y = Y.to(device)
|
|
37
|
+
model_output = model(X)
|
|
38
|
+
|
|
39
|
+
self.outputs = torch.cat((self.outputs, model_output), dim=0)
|
|
40
|
+
self.actual = torch.cat((self.actual, Y), dim=0)
|
|
41
|
+
|
|
42
|
+
if not self.norm and y_scaling is not None:
|
|
43
|
+
self.outputs = y_scaling.inv(self.outputs)
|
|
44
|
+
self.actual = y_scaling.inv(self.actual)
|
|
45
|
+
|
|
46
|
+
def tensor(self, type: PredictionType = PredictionType.PREDICTED):
|
|
47
|
+
if type == PredictionType.PREDICTED:
|
|
48
|
+
predicted = self.predictor(self.outputs)
|
|
49
|
+
return predicted.detach().cpu()
|
|
50
|
+
elif type == PredictionType.ACTUAL:
|
|
51
|
+
return self.actual.detach().cpu()
|
|
52
|
+
else:
|
|
53
|
+
return self.outputs.detach().cpu()
|
|
54
|
+
|
|
55
|
+
def numpy(self, type: PredictionType = PredictionType.PREDICTED):
|
|
56
|
+
return self.tensor(type).numpy()
|
|
57
|
+
|
|
58
|
+
def plot(self, epoch):
|
|
59
|
+
if self.prediction_plotter is not None:
|
|
60
|
+
return self.prediction_plotter.plot(epoch)
|
|
61
|
+
else:
|
|
62
|
+
return {} # nothing to plot
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
class GenerativePredictions(Predictions):
|
|
66
|
+
def __init__(self, norm=False):
|
|
67
|
+
super().__init__(norm)
|
|
68
|
+
|
|
69
|
+
def compute(self, training, loader):
|
|
70
|
+
device = training.device
|
|
71
|
+
self.outputs = torch.zeros(size=(0,), device=device)
|
|
72
|
+
generator = training.generator
|
|
73
|
+
generator.eval()
|
|
74
|
+
with torch.no_grad():
|
|
75
|
+
for X in loader:
|
|
76
|
+
X = X.to(device)
|
|
77
|
+
gen_output = generator(X)
|
|
78
|
+
self.outputs = torch.cat((self.outputs, gen_output), dim=0)
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
from iapytoo.predictions import Predictions
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class Feature:
|
|
5
|
+
def __init__(self, computer) -> None:
|
|
6
|
+
self.actual = []
|
|
7
|
+
self.predicted = []
|
|
8
|
+
self.computer = computer
|
|
9
|
+
|
|
10
|
+
def compute(self, predictions: Predictions, **kwargs):
|
|
11
|
+
for i in range(predictions.actual.shape[0]):
|
|
12
|
+
actual_del = self.computer(predictions.actual[i, 0, :], **kwargs)
|
|
13
|
+
self.actual.append(actual_del)
|
|
14
|
+
predicted_del = self.computer(
|
|
15
|
+
predictions.outputs[i, 0, :], **kwargs)
|
|
16
|
+
self.predicted.append(predicted_del)
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import torchvision
|
|
2
|
+
import matplotlib.pyplot as plt
|
|
3
|
+
from sklearn.metrics import confusion_matrix
|
|
4
|
+
from sklearn.manifold import TSNE
|
|
5
|
+
import seaborn as sns
|
|
6
|
+
from scipy.signal import welch
|
|
7
|
+
import numpy as np
|
|
8
|
+
|
|
9
|
+
from iapytoo.predictions.types import PredictionType
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class PredictionPlotter:
|
|
13
|
+
def __init__(self, title=None):
|
|
14
|
+
self.predictions = None
|
|
15
|
+
self.title = title
|
|
16
|
+
|
|
17
|
+
def connect(self, predictions):
|
|
18
|
+
self.predictions = predictions
|
|
19
|
+
|
|
20
|
+
def plot(self, epoch):
|
|
21
|
+
return None
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class CollectionPlotters(PredictionPlotter):
|
|
25
|
+
|
|
26
|
+
def __init__(self, title=None):
|
|
27
|
+
super(CollectionPlotters, self).__init__(title)
|
|
28
|
+
self.plotters = []
|
|
29
|
+
|
|
30
|
+
def __len__(self):
|
|
31
|
+
return len(self.plotters)
|
|
32
|
+
|
|
33
|
+
def add(self, prediction_plotter):
|
|
34
|
+
prediction_plotter.connect(self.predictions)
|
|
35
|
+
self.plotters.append(prediction_plotter)
|
|
36
|
+
|
|
37
|
+
def plot(self, epoch):
|
|
38
|
+
plots = {}
|
|
39
|
+
for p in self.plotters:
|
|
40
|
+
plots.update(p.plot(epoch))
|
|
41
|
+
|
|
42
|
+
return plots
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class ScatterPlotter(PredictionPlotter):
|
|
46
|
+
def __init__(self):
|
|
47
|
+
super().__init__(title="actual_versus_predicted")
|
|
48
|
+
|
|
49
|
+
def plot(self, epoch):
|
|
50
|
+
predicted = self.predictions.numpy(PredictionType.PREDICTED)
|
|
51
|
+
actual = self.predictions.numpy(PredictionType.ACTUAL)
|
|
52
|
+
fig, ax = plt.subplots(figsize=(10, 5))
|
|
53
|
+
ax.scatter(predicted, actual)
|
|
54
|
+
|
|
55
|
+
return {self.title: fig}
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
class ConfusionPlotter(PredictionPlotter):
|
|
59
|
+
def __init__(self):
|
|
60
|
+
super().__init__(title="confusion_matrix")
|
|
61
|
+
|
|
62
|
+
def plot(self, epoch):
|
|
63
|
+
# Calcul de la matrice de confusion
|
|
64
|
+
predicted = self.predictions.numpy(PredictionType.PREDICTED)
|
|
65
|
+
actual = self.predictions.numpy(PredictionType.ACTUAL)
|
|
66
|
+
cm = confusion_matrix(predicted, actual)
|
|
67
|
+
fig, ax = plt.subplots(figsize=(10, 5))
|
|
68
|
+
sns.heatmap(cm, annot=True, fmt="d", cmap="Blues", cbar=False)
|
|
69
|
+
|
|
70
|
+
return {self.title: fig}
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
class TSNEPlotter(PredictionPlotter):
|
|
74
|
+
|
|
75
|
+
def __init__(self):
|
|
76
|
+
super().__init__(title="tsne")
|
|
77
|
+
|
|
78
|
+
def plot(self, epoch):
|
|
79
|
+
|
|
80
|
+
outputs = self.predictions.numpy(PredictionType.OUTPUTS)
|
|
81
|
+
actual = self.predictions.numpy(PredictionType.ACTUAL).astype(np.int32)
|
|
82
|
+
tsne = TSNE(n_components=2, random_state=42)
|
|
83
|
+
predicted_tsne = tsne.fit_transform(outputs)
|
|
84
|
+
|
|
85
|
+
fig, ax = plt.subplots(figsize=(10, 5))
|
|
86
|
+
# Utilisons les classes pour colorer les points
|
|
87
|
+
for i in np.unique(actual):
|
|
88
|
+
ax.scatter(
|
|
89
|
+
predicted_tsne[actual == i, 0],
|
|
90
|
+
predicted_tsne[actual == i, 1],
|
|
91
|
+
label=f"{i}",
|
|
92
|
+
alpha=0.6,
|
|
93
|
+
)
|
|
94
|
+
ax.legend()
|
|
95
|
+
ax.set_title("t-SNE")
|
|
96
|
+
|
|
97
|
+
return {self.title: fig}
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
class Fake1DPlotter(PredictionPlotter):
|
|
101
|
+
def __init__(self, n_plot=4):
|
|
102
|
+
super().__init__(title="generated_fake1d")
|
|
103
|
+
self.n_plot = n_plot
|
|
104
|
+
|
|
105
|
+
def plot(self, epoch):
|
|
106
|
+
fake = self.predictions.numpy(PredictionType.PREDICTED)
|
|
107
|
+
f, a = plt.subplots(self.n_plot, self.n_plot, figsize=(8, 8))
|
|
108
|
+
for i in range(self.n_plot):
|
|
109
|
+
for j in range(self.n_plot):
|
|
110
|
+
a[i][j].plot(fake[i * self.n_plot + j].view(-1))
|
|
111
|
+
a[i][j].set_xticks(())
|
|
112
|
+
a[i][j].set_yticks(())
|
|
113
|
+
|
|
114
|
+
return {self.title: f}
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
class DSPPlotter(PredictionPlotter):
|
|
118
|
+
def __init__(self, f_max=4, nperseg=200):
|
|
119
|
+
super().__init__(title="dsp")
|
|
120
|
+
self.f_max = f_max
|
|
121
|
+
self.nperseg = nperseg
|
|
122
|
+
|
|
123
|
+
def plot(self, epoch):
|
|
124
|
+
fake = self.predictions.predicted[0].view(-1)
|
|
125
|
+
f, ax = plt.subplots()
|
|
126
|
+
|
|
127
|
+
frequencies, power_spectrum = welch(fake, fs=self.f_max, nperseg=self.nperseg)
|
|
128
|
+
ax.plot(frequencies, power_spectrum)
|
|
129
|
+
|
|
130
|
+
return {self.title: f}
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
class Fake2DPlotter(PredictionPlotter):
|
|
134
|
+
def __init__(self, n_plot=4):
|
|
135
|
+
super().__init__("generated_fake2d")
|
|
136
|
+
self.n_plot = n_plot
|
|
137
|
+
|
|
138
|
+
def plot(self, epoch):
|
|
139
|
+
outputs = self.predictions.tensor(PredictionType.OUTPUTS)
|
|
140
|
+
fake_list = [outputs[i] for i in range(outputs.shape[0])]
|
|
141
|
+
grid_img = torchvision.utils.make_grid(fake_list, nrow=self.n_plot)
|
|
142
|
+
|
|
143
|
+
f, ax = plt.subplots()
|
|
144
|
+
ax.imshow(grid_img.permute(1, 2, 0))
|
|
145
|
+
|
|
146
|
+
return {self.title: f}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
|
|
2
|
+
import torch
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class Predictor:
|
|
6
|
+
def __call__(self, outputs: torch.Tensor) -> torch.Tensor:
|
|
7
|
+
# By default, do nothing outputs of the model are predictions.
|
|
8
|
+
return outputs
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class MaxPredictor(Predictor):
|
|
12
|
+
|
|
13
|
+
def __call__(self, outputs: torch.Tensor) -> torch.Tensor:
|
|
14
|
+
_, preds = torch.max(outputs.data, 1)
|
|
15
|
+
return preds
|
|
File without changes
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import torch
|
|
3
|
+
import mlflow
|
|
4
|
+
import tempfile
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class CheckPoint:
|
|
8
|
+
def __init__(self, run_id=None) -> None:
|
|
9
|
+
self.params = {"run_id": run_id}
|
|
10
|
+
if run_id is not None:
|
|
11
|
+
run = mlflow.get_run(self.run_id)
|
|
12
|
+
assert run is not None, f"unable to find run {self.run_id}"
|
|
13
|
+
with tempfile.TemporaryDirectory() as tmpdir:
|
|
14
|
+
local_artifact = mlflow.artifacts.download_artifacts(
|
|
15
|
+
run.info.artifact_uri, dst_path=tmpdir
|
|
16
|
+
)
|
|
17
|
+
ck_path = os.path.join(local_artifact, "checkpoints")
|
|
18
|
+
files = []
|
|
19
|
+
for f in os.listdir(ck_path):
|
|
20
|
+
ck_name = os.path.join(ck_path, f)
|
|
21
|
+
if os.path.isfile(ck_name):
|
|
22
|
+
files.append(ck_name)
|
|
23
|
+
files.sort(key=lambda x: os.path.getmtime(x))
|
|
24
|
+
ck_name = files[-1]
|
|
25
|
+
self.params = torch.load(ck_name)
|
|
26
|
+
else:
|
|
27
|
+
self.params["epoch"] = -1
|
|
28
|
+
|
|
29
|
+
@property
|
|
30
|
+
def run_id(self):
|
|
31
|
+
return self.params["run_id"]
|
|
32
|
+
|
|
33
|
+
@property
|
|
34
|
+
def epoch(self):
|
|
35
|
+
return self.params["epoch"]
|
|
36
|
+
|
|
37
|
+
def update(self, run_id, epoch, training):
|
|
38
|
+
self.params["run_id"] = run_id
|
|
39
|
+
self.params["epoch"] = epoch
|
|
40
|
+
self.params["training"] = training.state_dict()
|
|
41
|
+
|
|
42
|
+
def init(self, training, only_model=False):
|
|
43
|
+
if self.run_id is not None:
|
|
44
|
+
training.load_state_dict(self.params["training"], only_model=only_model)
|