iapytoo 0.0.6.dev0__py3-none-any.whl → 0.0.6.dev2__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.
Potentially problematic release.
This version of iapytoo might be problematic. Click here for more details.
- iapytoo/_version.py +2 -2
- iapytoo/metrics/__init__.py +0 -82
- iapytoo/metrics/collection.py +39 -0
- iapytoo/metrics/metric.py +3 -3
- iapytoo/predictions/__init__.py +24 -8
- iapytoo/predictions/predictors.py +0 -20
- iapytoo/train/factories.py +170 -100
- iapytoo/train/inference.py +99 -17
- iapytoo/train/logger.py +2 -15
- iapytoo/train/mlflow_model.py +51 -43
- iapytoo/train/training.py +14 -35
- iapytoo/train/valuator.py +18 -0
- iapytoo/train/wgan.py +14 -31
- iapytoo/utils/model_config.py +3 -1
- {iapytoo-0.0.6.dev0.dist-info → iapytoo-0.0.6.dev2.dist-info}/METADATA +1 -1
- {iapytoo-0.0.6.dev0.dist-info → iapytoo-0.0.6.dev2.dist-info}/RECORD +19 -18
- {iapytoo-0.0.6.dev0.dist-info → iapytoo-0.0.6.dev2.dist-info}/WHEEL +0 -0
- {iapytoo-0.0.6.dev0.dist-info → iapytoo-0.0.6.dev2.dist-info}/licenses/LICENSE +0 -0
- {iapytoo-0.0.6.dev0.dist-info → iapytoo-0.0.6.dev2.dist-info}/top_level.txt +0 -0
iapytoo/_version.py
CHANGED
|
@@ -17,5 +17,5 @@ __version__: str
|
|
|
17
17
|
__version_tuple__: VERSION_TUPLE
|
|
18
18
|
version_tuple: VERSION_TUPLE
|
|
19
19
|
|
|
20
|
-
__version__ = version = '0.0.6.
|
|
21
|
-
__version_tuple__ = version_tuple = (0, 0, 6, '
|
|
20
|
+
__version__ = version = '0.0.6.dev2'
|
|
21
|
+
__version_tuple__ = version_tuple = (0, 0, 6, 'dev2')
|
iapytoo/metrics/__init__.py
CHANGED
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
from iapytoo.utils.config import Config
|
|
3
|
-
from iapytoo.utils.singleton import singleton
|
|
4
|
-
from .predefined import R2Metric, RMSMetric, MSMetric, AccuracyMetric
|
|
5
|
-
from .metric import Metric
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
class MetricsCollection(Metric):
|
|
9
|
-
def __init__(self, tag: str, metric_names: list[str], config: Config):
|
|
10
|
-
super().__init__(tag, config)
|
|
11
|
-
self.metrics = {}
|
|
12
|
-
factory = MetricFactory()
|
|
13
|
-
try:
|
|
14
|
-
for n in metric_names:
|
|
15
|
-
self.metrics[f"{tag}_{n}"] = factory.create_metric(n, config)
|
|
16
|
-
except MetricError as er:
|
|
17
|
-
print(f"Unable to create metric : {er}")
|
|
18
|
-
|
|
19
|
-
def to(self, device):
|
|
20
|
-
for m in self.metrics.values():
|
|
21
|
-
m.to(device)
|
|
22
|
-
|
|
23
|
-
# overwrite
|
|
24
|
-
def update(self, Y_hat, Y):
|
|
25
|
-
for m in self.metrics.values():
|
|
26
|
-
m.update(Y_hat, Y)
|
|
27
|
-
|
|
28
|
-
# overwrite
|
|
29
|
-
def compute(self):
|
|
30
|
-
self.results = {}
|
|
31
|
-
for k, m in self.metrics.items():
|
|
32
|
-
results = m.compute()
|
|
33
|
-
for name, result in results.items():
|
|
34
|
-
# prepend the name of the collection to all inner results : validation_accuracy for example
|
|
35
|
-
self.results[f"{self.name}_{name}"] = result
|
|
36
|
-
return self.results
|
|
37
|
-
|
|
38
|
-
# overwrite
|
|
39
|
-
def reset(self) -> None:
|
|
40
|
-
for m in self.metrics.values():
|
|
41
|
-
m.reset()
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
class MetricError(Exception):
|
|
45
|
-
def __init__(self, *args: object) -> None:
|
|
46
|
-
super().__init__(*args)
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
@singleton
|
|
50
|
-
class MetricFactory:
|
|
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: Config, 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,39 @@
|
|
|
1
|
+
from iapytoo.utils.config import Config
|
|
2
|
+
from iapytoo.metrics.metric import Metric
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class MetricsCollection(Metric):
|
|
6
|
+
def __init__(self, tag: str, metric_names: list[str], config: Config):
|
|
7
|
+
from iapytoo.train.factories import Factory, MetricError
|
|
8
|
+
super().__init__(tag, config)
|
|
9
|
+
self.metrics = {}
|
|
10
|
+
try:
|
|
11
|
+
factory = Factory()
|
|
12
|
+
for n in metric_names:
|
|
13
|
+
self.metrics[f"{tag}_{n}"] = factory.create_metric(n, config)
|
|
14
|
+
except MetricError as er:
|
|
15
|
+
print(f"Unable to create metric : {er}")
|
|
16
|
+
|
|
17
|
+
def to(self, device):
|
|
18
|
+
for m in self.metrics.values():
|
|
19
|
+
m.to(device)
|
|
20
|
+
|
|
21
|
+
# overwrite
|
|
22
|
+
def update(self, Y_hat, Y):
|
|
23
|
+
for m in self.metrics.values():
|
|
24
|
+
m.update(Y_hat, Y)
|
|
25
|
+
|
|
26
|
+
# overwrite
|
|
27
|
+
def compute(self):
|
|
28
|
+
self.results = {}
|
|
29
|
+
for k, m in self.metrics.items():
|
|
30
|
+
results = m.compute()
|
|
31
|
+
for name, result in results.items():
|
|
32
|
+
# prepend the name of the collection to all inner results : validation_accuracy for example
|
|
33
|
+
self.results[f"{self.name}_{name}"] = result
|
|
34
|
+
return self.results
|
|
35
|
+
|
|
36
|
+
# overwrite
|
|
37
|
+
def reset(self) -> None:
|
|
38
|
+
for m in self.metrics.values():
|
|
39
|
+
m.reset()
|
iapytoo/metrics/metric.py
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import torch
|
|
2
2
|
|
|
3
3
|
from iapytoo.utils.config import Config
|
|
4
|
-
from iapytoo.predictions.predictors import PredictorFactory
|
|
5
4
|
|
|
6
5
|
|
|
7
6
|
class Metric:
|
|
8
7
|
def __init__(self, name, config: Config, with_target=True):
|
|
8
|
+
from iapytoo.train.factories import Factory
|
|
9
9
|
self.with_target = with_target
|
|
10
10
|
self.name = name
|
|
11
11
|
self.config = config
|
|
12
|
-
self.predictor =
|
|
12
|
+
self.predictor = Factory().create_predictor(config)
|
|
13
13
|
|
|
14
14
|
# First dimension is for the concatenation.
|
|
15
15
|
self.outputs = torch.zeros(size=(0,))
|
|
@@ -23,7 +23,7 @@ class Metric:
|
|
|
23
23
|
|
|
24
24
|
@property
|
|
25
25
|
def predicted(self):
|
|
26
|
-
return self.predictor(self.outputs)
|
|
26
|
+
return self.predictor(self.outputs)
|
|
27
27
|
|
|
28
28
|
def to(self, device):
|
|
29
29
|
self.outputs = self.outputs.to(device)
|
iapytoo/predictions/__init__.py
CHANGED
|
@@ -1,22 +1,35 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
1
2
|
import torch
|
|
2
3
|
|
|
3
|
-
from iapytoo.utils.config import Config
|
|
4
4
|
from .types import PredictionType
|
|
5
5
|
from .plotters import CollectionPlotters
|
|
6
|
-
|
|
7
|
-
from
|
|
6
|
+
|
|
7
|
+
from typing import TYPE_CHECKING
|
|
8
|
+
|
|
9
|
+
if TYPE_CHECKING:
|
|
10
|
+
from iapytoo.train.inference import Inference
|
|
8
11
|
|
|
9
12
|
|
|
10
13
|
class Predictions:
|
|
11
|
-
def __init__(self,
|
|
14
|
+
def __init__(self, inference: Inference):
|
|
12
15
|
self.outputs = None # dimension is assigned in compute
|
|
13
16
|
self.actual = None
|
|
14
17
|
|
|
15
|
-
self.
|
|
18
|
+
self.inference = inference
|
|
16
19
|
|
|
17
20
|
self.prediction_plotter: CollectionPlotters = CollectionPlotters()
|
|
18
21
|
self.prediction_plotter.connect(self)
|
|
19
22
|
|
|
23
|
+
@property
|
|
24
|
+
def valuator(self):
|
|
25
|
+
assert self.inference is not None, "no inference for this prediction"
|
|
26
|
+
return self.inference.valuator
|
|
27
|
+
|
|
28
|
+
@property
|
|
29
|
+
def predictor(self):
|
|
30
|
+
assert self.inference is not None, "no inference for this prediction"
|
|
31
|
+
return self.inference.predictor
|
|
32
|
+
|
|
20
33
|
def __len__(self):
|
|
21
34
|
"""return the number of plotters used by the predictions"""
|
|
22
35
|
return len(self.prediction_plotter)
|
|
@@ -24,12 +37,15 @@ class Predictions:
|
|
|
24
37
|
def add_plotter(self, prediction_plotter):
|
|
25
38
|
self.prediction_plotter.add(prediction_plotter)
|
|
26
39
|
|
|
27
|
-
def compute(self, loader
|
|
40
|
+
def compute(self, loader):
|
|
41
|
+
|
|
42
|
+
assert self.valuator is not None, "no valuator"
|
|
43
|
+
assert self.predictor is not None, "no predictor"
|
|
28
44
|
|
|
29
45
|
self.outputs = torch.zeros(size=(0,))
|
|
30
46
|
self.actual = torch.zeros(size=(0,))
|
|
31
47
|
|
|
32
|
-
for outputs, actual in valuator.evaluate_loader(loader):
|
|
48
|
+
for outputs, actual in self.valuator.evaluate_loader(loader):
|
|
33
49
|
self.outputs = torch.cat((self.outputs, outputs), dim=0)
|
|
34
50
|
|
|
35
51
|
if actual is not None:
|
|
@@ -37,7 +53,7 @@ class Predictions:
|
|
|
37
53
|
|
|
38
54
|
def tensor(self, type: PredictionType = PredictionType.PREDICTED):
|
|
39
55
|
if type == PredictionType.PREDICTED:
|
|
40
|
-
return self.predictor(self.outputs)
|
|
56
|
+
return self.predictor(self.outputs)
|
|
41
57
|
elif type == PredictionType.ACTUAL:
|
|
42
58
|
return self.actual
|
|
43
59
|
else:
|
|
@@ -1,8 +1,5 @@
|
|
|
1
1
|
import torch
|
|
2
2
|
|
|
3
|
-
from iapytoo.utils.singleton import singleton
|
|
4
|
-
from iapytoo.utils.config import Config
|
|
5
|
-
|
|
6
3
|
|
|
7
4
|
class Predictor:
|
|
8
5
|
def __call__(self, outputs: torch.Tensor) -> torch.Tensor:
|
|
@@ -14,20 +11,3 @@ class MaxPredictor(Predictor):
|
|
|
14
11
|
def __call__(self, outputs: torch.Tensor) -> torch.Tensor:
|
|
15
12
|
_, preds = torch.max(outputs.data, 1)
|
|
16
13
|
return preds
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
@singleton
|
|
20
|
-
class PredictorFactory:
|
|
21
|
-
def __init__(self) -> None:
|
|
22
|
-
self.predictor_dict = {"max": MaxPredictor}
|
|
23
|
-
|
|
24
|
-
def create_predictor(self, key: Config | str):
|
|
25
|
-
if isinstance(key, Config):
|
|
26
|
-
config: Config = key
|
|
27
|
-
kind = config.model.predictor
|
|
28
|
-
else:
|
|
29
|
-
kind = key
|
|
30
|
-
if kind is not None:
|
|
31
|
-
return self.predictor_dict[kind]()
|
|
32
|
-
|
|
33
|
-
return None
|
iapytoo/train/factories.py
CHANGED
|
@@ -2,14 +2,15 @@ import torch.nn as nn
|
|
|
2
2
|
from iapytoo.utils.singleton import singleton
|
|
3
3
|
import torch.optim as to
|
|
4
4
|
from torch.optim.lr_scheduler import StepLR
|
|
5
|
+
from iapytoo.predictions.predictors import Predictor, MaxPredictor
|
|
6
|
+
from iapytoo.train.valuator import ModelValuator, WGANValuator
|
|
7
|
+
from iapytoo.metrics.metric import Metric
|
|
8
|
+
from iapytoo.metrics.predefined import R2Metric, RMSMetric, AccuracyMetric, MSMetric
|
|
5
9
|
|
|
6
10
|
from iapytoo.utils.config import Config
|
|
7
11
|
|
|
8
12
|
|
|
9
|
-
|
|
10
|
-
def __init__(self, *args: object) -> None:
|
|
11
|
-
super().__init__(*args)
|
|
12
|
-
|
|
13
|
+
# Model
|
|
13
14
|
|
|
14
15
|
class WeightInitiator:
|
|
15
16
|
def __call__(self, m):
|
|
@@ -32,46 +33,7 @@ class Model(nn.Module):
|
|
|
32
33
|
return model_output
|
|
33
34
|
|
|
34
35
|
|
|
35
|
-
|
|
36
|
-
class ModelFactory:
|
|
37
|
-
def __init__(self) -> None:
|
|
38
|
-
self.models_dict = {}
|
|
39
|
-
|
|
40
|
-
def register_model(self, key, model_cls):
|
|
41
|
-
self.models_dict[key] = model_cls
|
|
42
|
-
|
|
43
|
-
def create_model(self, kind: str, config: Config, loader, device="cpu"):
|
|
44
|
-
"""Creates an architecture of NN
|
|
45
|
-
|
|
46
|
-
Args:
|
|
47
|
-
kind (str): kind of NN, key for the factory
|
|
48
|
-
config (dict): config dict to use to initialize model
|
|
49
|
-
loader (DataLoader): a dataloader to have input/output dimensions
|
|
50
|
-
device (str, optional): Defaults to "cpu".
|
|
51
|
-
|
|
52
|
-
Raises:
|
|
53
|
-
ModelError: error raised if no architecture fit kind key
|
|
54
|
-
|
|
55
|
-
Returns:
|
|
56
|
-
nn.Module: pytorch model
|
|
57
|
-
"""
|
|
58
|
-
try:
|
|
59
|
-
model: Model = self.models_dict[kind](loader, config)
|
|
60
|
-
except KeyError:
|
|
61
|
-
raise ModelError(f"model {kind} is not handled")
|
|
62
|
-
|
|
63
|
-
initiator = model.weight_initiator()
|
|
64
|
-
if initiator is not None:
|
|
65
|
-
model.apply(initiator)
|
|
66
|
-
|
|
67
|
-
model = model.to(device=device)
|
|
68
|
-
return model
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
class OptimizerError(Exception):
|
|
72
|
-
def __init__(self, *args: object) -> None:
|
|
73
|
-
super().__init__(*args)
|
|
74
|
-
|
|
36
|
+
# Optimizer
|
|
75
37
|
|
|
76
38
|
class Optimizer:
|
|
77
39
|
def __init__(self, model, config: Config) -> None:
|
|
@@ -109,43 +71,7 @@ class SGDOptimizer(Optimizer):
|
|
|
109
71
|
kwargs["momentum"] = config.training.momentum
|
|
110
72
|
self.torch_optimizer = to.SGD(model.parameters(), **kwargs)
|
|
111
73
|
|
|
112
|
-
|
|
113
|
-
@singleton
|
|
114
|
-
class OptimizerFactory:
|
|
115
|
-
def __init__(self) -> None:
|
|
116
|
-
self.optimizers_dict = {
|
|
117
|
-
"adam": AdamOptimizer,
|
|
118
|
-
"sgd": SGDOptimizer,
|
|
119
|
-
"rmsprop": RMSpropOptimizer,
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
def register_optimizer(self, key, optimizer_cls):
|
|
123
|
-
self.optimizers_dict[key] = optimizer_cls
|
|
124
|
-
|
|
125
|
-
def create_optimizer(self, kind: str, model, config: Config):
|
|
126
|
-
"""Creates an optimizer for the NN
|
|
127
|
-
|
|
128
|
-
Args:
|
|
129
|
-
kind (str): kind of optimizer, key for the factory
|
|
130
|
-
config (dict): config dict to use to initialize optimizer
|
|
131
|
-
|
|
132
|
-
Raises:
|
|
133
|
-
OptimizerError: error raised if no architecture fit kind key
|
|
134
|
-
|
|
135
|
-
Returns:
|
|
136
|
-
nn.Module: pytorch optimizer
|
|
137
|
-
"""
|
|
138
|
-
try:
|
|
139
|
-
optimizer = self.optimizers_dict[kind](model, config)
|
|
140
|
-
except KeyError:
|
|
141
|
-
raise OptimizerError(f"optimizer {kind} is not handled")
|
|
142
|
-
|
|
143
|
-
return optimizer
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
class SchedulerError(Exception):
|
|
147
|
-
def __init__(self, *args: object) -> None:
|
|
148
|
-
super().__init__(*args)
|
|
74
|
+
# Scheduler
|
|
149
75
|
|
|
150
76
|
|
|
151
77
|
class Scheduler:
|
|
@@ -162,11 +88,86 @@ class StepScheduler(Scheduler):
|
|
|
162
88
|
kwargs["step_size"] = config.training.step_size
|
|
163
89
|
self.lr_scheduler = StepLR(optimizer, **kwargs)
|
|
164
90
|
|
|
91
|
+
# Factories
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
class OptimizerError(Exception):
|
|
95
|
+
def __init__(self, *args: object) -> None:
|
|
96
|
+
super().__init__(*args)
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
class ModelError(Exception):
|
|
100
|
+
def __init__(self, *args: object) -> None:
|
|
101
|
+
super().__init__(*args)
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
class SchedulerError(Exception):
|
|
105
|
+
def __init__(self, *args: object) -> None:
|
|
106
|
+
super().__init__(*args)
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
class MetricError(Exception):
|
|
110
|
+
def __init__(self, *args: object) -> None:
|
|
111
|
+
super().__init__(*args)
|
|
112
|
+
|
|
165
113
|
|
|
166
114
|
@singleton
|
|
167
|
-
class
|
|
115
|
+
class Factory:
|
|
168
116
|
def __init__(self) -> None:
|
|
169
|
-
self.
|
|
117
|
+
self.loss_dict = {
|
|
118
|
+
"mse": nn.MSELoss,
|
|
119
|
+
"nll": nn.NLLLoss,
|
|
120
|
+
"cel": nn.CrossEntropyLoss,
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
self.schedulers_dict = {
|
|
124
|
+
"step": StepScheduler
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
self.optimizers_dict = {
|
|
128
|
+
"adam": AdamOptimizer,
|
|
129
|
+
"sgd": SGDOptimizer,
|
|
130
|
+
"rmsprop": RMSpropOptimizer,
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
self.models_dict = {
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
self.metrics_dict = {
|
|
137
|
+
"r2": R2Metric,
|
|
138
|
+
"rms": RMSMetric,
|
|
139
|
+
"ms": MSMetric,
|
|
140
|
+
"accuracy": AccuracyMetric,
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
self.predictor_dict = {
|
|
144
|
+
"default": Predictor,
|
|
145
|
+
"max": MaxPredictor
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
self.valuator_dict = {
|
|
149
|
+
"model": ModelValuator,
|
|
150
|
+
"gan": WGANValuator
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
def register_loss(self, key, loss_cls):
|
|
154
|
+
self.loss_dict[key] = loss_cls
|
|
155
|
+
|
|
156
|
+
def create_loss(self, kind: str):
|
|
157
|
+
"""Creates a loss criterion
|
|
158
|
+
|
|
159
|
+
Args:
|
|
160
|
+
kind (str): kind of loss, key for the factory
|
|
161
|
+
|
|
162
|
+
Returns:
|
|
163
|
+
nn.Module: pytorch loss function
|
|
164
|
+
"""
|
|
165
|
+
try:
|
|
166
|
+
criterion = self.loss_dict[kind]()
|
|
167
|
+
except KeyError:
|
|
168
|
+
raise Exception(f"loss {kind} is not handled")
|
|
169
|
+
|
|
170
|
+
return criterion
|
|
170
171
|
|
|
171
172
|
def register_scheduler(self, key, scheduler_cls):
|
|
172
173
|
self.schedulers_dict[key] = scheduler_cls
|
|
@@ -191,31 +192,100 @@ class SchedulerFactory:
|
|
|
191
192
|
|
|
192
193
|
return scheduler
|
|
193
194
|
|
|
195
|
+
def register_optimizer(self, key, optimizer_cls):
|
|
196
|
+
self.optimizers_dict[key] = optimizer_cls
|
|
194
197
|
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
def __init__(self) -> None:
|
|
198
|
-
self.loss_dict = {
|
|
199
|
-
"mse": nn.MSELoss,
|
|
200
|
-
"nll": nn.NLLLoss,
|
|
201
|
-
"cel": nn.CrossEntropyLoss,
|
|
202
|
-
}
|
|
198
|
+
def create_optimizer(self, kind: str, model, config: Config):
|
|
199
|
+
"""Creates an optimizer for the NN
|
|
203
200
|
|
|
204
|
-
|
|
205
|
-
|
|
201
|
+
Args:
|
|
202
|
+
kind (str): kind of optimizer, key for the factory
|
|
203
|
+
config (dict): config dict to use to initialize optimizer
|
|
206
204
|
|
|
207
|
-
|
|
208
|
-
|
|
205
|
+
Raises:
|
|
206
|
+
OptimizerError: error raised if no architecture fit kind key
|
|
207
|
+
|
|
208
|
+
Returns:
|
|
209
|
+
nn.Module: pytorch optimizer
|
|
210
|
+
"""
|
|
211
|
+
try:
|
|
212
|
+
optimizer = self.optimizers_dict[kind](model, config)
|
|
213
|
+
except KeyError:
|
|
214
|
+
raise OptimizerError(f"optimizer {kind} is not handled")
|
|
215
|
+
|
|
216
|
+
return optimizer
|
|
217
|
+
|
|
218
|
+
def register_model(self, key, model_cls):
|
|
219
|
+
self.models_dict[key] = model_cls
|
|
220
|
+
|
|
221
|
+
def create_model(self, kind: str, config: Config, loader, device="cpu"):
|
|
222
|
+
"""Creates an architecture of NN
|
|
209
223
|
|
|
210
224
|
Args:
|
|
211
|
-
kind (str): kind of
|
|
225
|
+
kind (str): kind of NN, key for the factory
|
|
226
|
+
config (dict): config dict to use to initialize model
|
|
227
|
+
loader (DataLoader): a dataloader to have input/output dimensions
|
|
228
|
+
device (str, optional): Defaults to "cpu".
|
|
229
|
+
|
|
230
|
+
Raises:
|
|
231
|
+
ModelError: error raised if no architecture fit kind key
|
|
212
232
|
|
|
213
233
|
Returns:
|
|
214
|
-
nn.Module: pytorch
|
|
234
|
+
nn.Module: pytorch model
|
|
215
235
|
"""
|
|
216
236
|
try:
|
|
217
|
-
|
|
237
|
+
model: Model = self.models_dict[kind](loader, config)
|
|
218
238
|
except KeyError:
|
|
219
|
-
raise
|
|
239
|
+
raise ModelError(f"model {kind} is not handled")
|
|
220
240
|
|
|
221
|
-
|
|
241
|
+
initiator = model.weight_initiator()
|
|
242
|
+
if initiator is not None:
|
|
243
|
+
model.apply(initiator)
|
|
244
|
+
|
|
245
|
+
model = model.to(device=device)
|
|
246
|
+
return model
|
|
247
|
+
|
|
248
|
+
def register_metric(self, key, metric_cls):
|
|
249
|
+
self.metrics_dict[key] = metric_cls
|
|
250
|
+
|
|
251
|
+
def create_metric(self, kind: str, config: Config, device="cpu") -> Metric:
|
|
252
|
+
"""Creates an architecture of NN
|
|
253
|
+
|
|
254
|
+
Args:
|
|
255
|
+
kind (str): kind of metric, key for the factory
|
|
256
|
+
config (dict): config dict to use to initialize metric
|
|
257
|
+
device (str, optional): Defaults to "cpu".
|
|
258
|
+
|
|
259
|
+
Raises:
|
|
260
|
+
MetricError: error raised if no architecture fit kind key
|
|
261
|
+
|
|
262
|
+
Returns:
|
|
263
|
+
iapytoo.metrics.Metric: metric
|
|
264
|
+
"""
|
|
265
|
+
try:
|
|
266
|
+
metric: Metric = self.metrics_dict[kind](config)
|
|
267
|
+
except KeyError:
|
|
268
|
+
raise MetricError(f"metric {kind} is not handled")
|
|
269
|
+
|
|
270
|
+
metric = metric.to(device=device)
|
|
271
|
+
return metric
|
|
272
|
+
|
|
273
|
+
def create_predictor(self, key: Config | str):
|
|
274
|
+
if isinstance(key, Config):
|
|
275
|
+
config: Config = key
|
|
276
|
+
kind = config.model.predictor
|
|
277
|
+
else:
|
|
278
|
+
kind = key
|
|
279
|
+
assert kind is not None, "no default predictor defined ?"
|
|
280
|
+
|
|
281
|
+
return self.predictor_dict[kind]()
|
|
282
|
+
|
|
283
|
+
def create_valuator(self, key: Config | str, model, device):
|
|
284
|
+
if isinstance(key, Config):
|
|
285
|
+
config: Config = key
|
|
286
|
+
kind = config.model.valuator
|
|
287
|
+
else:
|
|
288
|
+
kind = key
|
|
289
|
+
assert kind is not None, "no default valuator defined ?"
|
|
290
|
+
|
|
291
|
+
return self.valuator_dict[kind](model, device)
|
iapytoo/train/inference.py
CHANGED
|
@@ -1,20 +1,26 @@
|
|
|
1
1
|
import torch
|
|
2
2
|
import logging
|
|
3
3
|
|
|
4
|
+
import numpy as np
|
|
5
|
+
|
|
4
6
|
import mlflow
|
|
5
7
|
import mlflow.pyfunc
|
|
6
8
|
|
|
9
|
+
from mlflow.models import ModelSignature
|
|
10
|
+
|
|
7
11
|
from abc import ABC, abstractmethod
|
|
8
12
|
|
|
13
|
+
|
|
9
14
|
from iapytoo.utils.config import Config
|
|
10
|
-
from iapytoo.utils.model_config import MLFlowConfig
|
|
15
|
+
from iapytoo.utils.model_config import ModelConfig, MLFlowConfig
|
|
11
16
|
from iapytoo.train.logger import Logger
|
|
12
|
-
from iapytoo.train.
|
|
17
|
+
from iapytoo.train.factories import Factory
|
|
18
|
+
from iapytoo.train.mlflow_model import MlflowTransform, MlflowModel, IMlfowModelProvider
|
|
13
19
|
from iapytoo.predictions import Predictions, PredictionType
|
|
14
|
-
from iapytoo.metrics import MetricsCollection
|
|
20
|
+
from iapytoo.metrics.collection import MetricsCollection
|
|
15
21
|
|
|
16
22
|
|
|
17
|
-
class Inference(ABC):
|
|
23
|
+
class Inference(ABC, IMlfowModelProvider):
|
|
18
24
|
|
|
19
25
|
def __init__(
|
|
20
26
|
self,
|
|
@@ -24,8 +30,13 @@ class Inference(ABC):
|
|
|
24
30
|
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
25
31
|
self._config: Config = config
|
|
26
32
|
self.logger: Logger = None
|
|
27
|
-
|
|
33
|
+
|
|
28
34
|
self._models = []
|
|
35
|
+
self.valuator = None
|
|
36
|
+
self.predictor = None
|
|
37
|
+
self.predictions: Predictions = Predictions(self)
|
|
38
|
+
self.mlflow_model: MlflowModel = None
|
|
39
|
+
self.mlflow_model_provider: IMlfowModelProvider = None
|
|
29
40
|
|
|
30
41
|
def _display_device(self):
|
|
31
42
|
use_cuda = torch.cuda.is_available()
|
|
@@ -44,13 +55,55 @@ class Inference(ABC):
|
|
|
44
55
|
def model(self):
|
|
45
56
|
return self._models[0]
|
|
46
57
|
|
|
47
|
-
def _valuator(self):
|
|
48
|
-
return ModelValuator(self.model, self.device)
|
|
49
|
-
|
|
50
58
|
@abstractmethod
|
|
51
59
|
def _create_models(self, loader):
|
|
52
60
|
pass
|
|
53
61
|
|
|
62
|
+
def _init_mlflow_model(self):
|
|
63
|
+
""" Generates mlflow model by creating valuator and predictor for this inference
|
|
64
|
+
if a IMlfowModelProvider is provided, use it to initiate signature, transform and input_example
|
|
65
|
+
Take care that at that point model should already be created
|
|
66
|
+
"""
|
|
67
|
+
|
|
68
|
+
assert self.model is not None, "a pytorch model is required for mlflow model"
|
|
69
|
+
|
|
70
|
+
model_config: ModelConfig = self._config.model
|
|
71
|
+
|
|
72
|
+
factory = Factory()
|
|
73
|
+
self.valuator = factory.create_valuator(
|
|
74
|
+
model_config.valuator,
|
|
75
|
+
self.model,
|
|
76
|
+
self.device
|
|
77
|
+
)
|
|
78
|
+
self.predictor = factory.create_predictor(
|
|
79
|
+
model_config.predictor
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
self.mlflow_model = MlflowModel()
|
|
83
|
+
self.mlflow_model.model = self.model
|
|
84
|
+
self.mlflow_model.valuator_key = model_config.valuator
|
|
85
|
+
self.mlflow_model.predictor_key = model_config.predictor
|
|
86
|
+
|
|
87
|
+
if self.mlflow_model_provider is not None:
|
|
88
|
+
self.mlflow_model.signature = self.mlflow_model_provider.get_signature()
|
|
89
|
+
self.mlflow_model.input_example = self.mlflow_model_provider.get_input_example()
|
|
90
|
+
self.mlflow_model.transform = self.mlflow_model_provider.get_transform()
|
|
91
|
+
|
|
92
|
+
def get_transform(self) -> MlflowTransform:
|
|
93
|
+
if self.mlflow_model_provider is not None:
|
|
94
|
+
return self.mlflow_model_provider.get_transform()
|
|
95
|
+
return None
|
|
96
|
+
|
|
97
|
+
def get_input_example(self) -> np.array:
|
|
98
|
+
if self.mlflow_model_provider is not None:
|
|
99
|
+
return self.mlflow_model_provider.get_input_example()
|
|
100
|
+
return None
|
|
101
|
+
|
|
102
|
+
def get_signature(self) -> ModelSignature:
|
|
103
|
+
if self.mlflow_model_provider is not None:
|
|
104
|
+
return self.mlflow_model_provider.get_signature()
|
|
105
|
+
return None
|
|
106
|
+
|
|
54
107
|
@abstractmethod
|
|
55
108
|
def predict(self, loader, run_id=None):
|
|
56
109
|
pass
|
|
@@ -60,18 +113,39 @@ class MLFlowInference(Inference):
|
|
|
60
113
|
|
|
61
114
|
def __init__(self, config: Config) -> None:
|
|
62
115
|
super().__init__(config)
|
|
63
|
-
|
|
64
|
-
def _create_models(self, loader):
|
|
65
116
|
model_config: MLFlowConfig = self._config.model
|
|
66
|
-
|
|
67
117
|
logged_model = f"runs:/{model_config.run_id}/model"
|
|
68
|
-
|
|
69
|
-
|
|
118
|
+
self.mlflow_model: MlflowModel = mlflow.pyfunc.load_model(
|
|
119
|
+
logged_model).unwrap_python_model()
|
|
120
|
+
|
|
121
|
+
# overidde : use mlflow_model
|
|
122
|
+
def get_transform(self) -> MlflowTransform:
|
|
123
|
+
if self.mlflow_model is not None:
|
|
124
|
+
return self.mlflow_model.transform
|
|
125
|
+
return None
|
|
126
|
+
|
|
127
|
+
# overidde : use mlflow_model
|
|
128
|
+
def get_input_example(self) -> np.array:
|
|
129
|
+
if self.mlflow_model is not None:
|
|
130
|
+
return self.mlflow_model.input_example
|
|
131
|
+
return None
|
|
132
|
+
|
|
133
|
+
# overidde : use mlflow_model
|
|
134
|
+
def get_signature(self) -> ModelSignature:
|
|
135
|
+
if self.mlflow_model is not None:
|
|
136
|
+
return self.mlflow_model.signature
|
|
137
|
+
return None
|
|
138
|
+
|
|
139
|
+
# overidde : use mlflow_model
|
|
140
|
+
def _create_models(self, loader):
|
|
141
|
+
model = self.mlflow_model.model
|
|
142
|
+
model.to(self.device)
|
|
70
143
|
return [model]
|
|
71
144
|
|
|
72
145
|
def predict(self, loader):
|
|
73
146
|
|
|
74
|
-
metrics = MetricsCollection(
|
|
147
|
+
metrics = MetricsCollection(
|
|
148
|
+
"inference", self._config.metrics.names, self._config)
|
|
75
149
|
# metrics.to(self.device)
|
|
76
150
|
|
|
77
151
|
with Logger(self._config) as self.logger:
|
|
@@ -79,11 +153,19 @@ class MLFlowInference(Inference):
|
|
|
79
153
|
self.logger.summary()
|
|
80
154
|
|
|
81
155
|
self._models = self._create_models(loader)
|
|
82
|
-
|
|
83
156
|
assert self.model is not None, "no model loaded for prediction"
|
|
84
157
|
|
|
85
|
-
|
|
86
|
-
self.
|
|
158
|
+
factory = Factory()
|
|
159
|
+
self.valuator = factory.create_valuator(
|
|
160
|
+
self.mlflow_model.valuator_key,
|
|
161
|
+
self.model,
|
|
162
|
+
self.device
|
|
163
|
+
)
|
|
164
|
+
self.predictor = factory.create_predictor(
|
|
165
|
+
self.mlflow_model.predictor_key
|
|
166
|
+
)
|
|
167
|
+
|
|
168
|
+
self.predictions.compute(loader=loader)
|
|
87
169
|
self.logger.report_prediction(0, self.predictions)
|
|
88
170
|
metrics.update(
|
|
89
171
|
self.predictions.tensor(PredictionType.OUTPUTS),
|
iapytoo/train/logger.py
CHANGED
|
@@ -18,7 +18,7 @@ from iapytoo.utils.display import lrfind_plot
|
|
|
18
18
|
from iapytoo.train.checkpoint import CheckPoint
|
|
19
19
|
from iapytoo.train.context import Context
|
|
20
20
|
from iapytoo.predictions import Predictions
|
|
21
|
-
from iapytoo.train.mlflow_model import
|
|
21
|
+
from iapytoo.train.mlflow_model import MlflowModel
|
|
22
22
|
|
|
23
23
|
|
|
24
24
|
class Logger:
|
|
@@ -93,7 +93,6 @@ class Logger:
|
|
|
93
93
|
|
|
94
94
|
for key in [
|
|
95
95
|
"training.epochs",
|
|
96
|
-
"training.tqdm",
|
|
97
96
|
"project",
|
|
98
97
|
"run",
|
|
99
98
|
"tracking_uri",
|
|
@@ -132,15 +131,6 @@ class Logger:
|
|
|
132
131
|
ctx_path = self.context.save(tmpdirname)
|
|
133
132
|
mlflow.log_artifact(ctx_path)
|
|
134
133
|
|
|
135
|
-
def log_transform(self, transform: Transform):
|
|
136
|
-
if transform is None:
|
|
137
|
-
return
|
|
138
|
-
with tempfile.TemporaryDirectory() as tmpdirname:
|
|
139
|
-
transform_name = os.path.join(tmpdirname, "transforms.pt")
|
|
140
|
-
torch.save(transform.infer_transform, transform_name)
|
|
141
|
-
mlflow.log_artifact(local_path=transform_name,
|
|
142
|
-
artifact_path="checkpoints")
|
|
143
|
-
|
|
144
134
|
def log_checkpoint(self, checkpoint: CheckPoint):
|
|
145
135
|
with tempfile.TemporaryDirectory() as tmpdirname:
|
|
146
136
|
ckp_name = os.path.join(tmpdirname, "checkpoint.pt")
|
|
@@ -148,15 +138,12 @@ class Logger:
|
|
|
148
138
|
mlflow.log_artifact(local_path=ckp_name,
|
|
149
139
|
artifact_path="checkpoints")
|
|
150
140
|
|
|
151
|
-
def save_model(self, model_wrapper:
|
|
141
|
+
def save_model(self, model_wrapper: MlflowModel):
|
|
152
142
|
assert model_wrapper is not None, "no model_wrapper instance ?"
|
|
153
143
|
|
|
154
144
|
signature = model_wrapper.signature
|
|
155
|
-
transform = model_wrapper.transform
|
|
156
145
|
input_example = model_wrapper.input_example
|
|
157
146
|
|
|
158
|
-
self.log_transform(transform)
|
|
159
|
-
|
|
160
147
|
with self.lock:
|
|
161
148
|
mlflow.pyfunc.log_model(
|
|
162
149
|
artifact_path="model",
|
iapytoo/train/mlflow_model.py
CHANGED
|
@@ -1,80 +1,88 @@
|
|
|
1
1
|
import mlflow.pyfunc as mp
|
|
2
2
|
import numpy as np
|
|
3
3
|
from typing import Any
|
|
4
|
-
|
|
4
|
+
import torch
|
|
5
5
|
|
|
6
|
-
from
|
|
7
|
-
from iapytoo.predictions.predictors import Predictor, PredictorFactory
|
|
6
|
+
from torchvision.transforms import ToTensor
|
|
8
7
|
|
|
8
|
+
from mlflow.models import ModelSignature
|
|
9
9
|
|
|
10
|
-
class Transform:
|
|
11
10
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
if len(X.shape) == 3: # Vérifier si l'image est en couleur
|
|
16
|
-
return X.transpose(2, 0, 1)
|
|
17
|
-
else:
|
|
18
|
-
return np.expand_dims(img, axis=0).astype(np.float32)
|
|
11
|
+
from iapytoo.train.factories import Factory
|
|
12
|
+
from iapytoo.train.valuator import Valuator
|
|
13
|
+
from iapytoo.predictions.predictors import Predictor
|
|
19
14
|
|
|
20
|
-
def __init__(self, train_transform, dataset=None, infer_transform=None) -> None:
|
|
21
|
-
self.train_transform = train_transform
|
|
22
|
-
self.infer_transform = infer_transform if infer_transform is not None else train_transform
|
|
23
|
-
self.set_signature(dataset)
|
|
24
15
|
|
|
25
|
-
|
|
26
|
-
return model_input
|
|
16
|
+
class MlflowTransform:
|
|
27
17
|
|
|
28
|
-
def
|
|
29
|
-
|
|
30
|
-
raise Exception(
|
|
31
|
-
"The signature should be set. It is model-dependent")
|
|
32
|
-
return self.signature, self.input_example
|
|
18
|
+
def __init__(self, transform):
|
|
19
|
+
self.transform = transform
|
|
33
20
|
|
|
34
|
-
def
|
|
35
|
-
|
|
36
|
-
|
|
21
|
+
def __call__(self, model_input, *args, **kwds) -> torch.Tensor:
|
|
22
|
+
raise NotImplementedError
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class TorchTransform(MlflowTransform):
|
|
26
|
+
|
|
27
|
+
def __init__(self):
|
|
28
|
+
super().__init__(transform=ToTensor())
|
|
29
|
+
|
|
30
|
+
def __call__(self, model_input, *args, **kwds) -> torch.Tensor:
|
|
31
|
+
return self.transform(model_input)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class IMlfowModelProvider:
|
|
35
|
+
|
|
36
|
+
def get_input_example(self) -> np.array:
|
|
37
37
|
raise NotImplementedError
|
|
38
38
|
|
|
39
|
+
def get_transform(self) -> MlflowTransform:
|
|
40
|
+
return TorchTransform() # default one, only transforms to torch Tensor
|
|
39
41
|
|
|
40
|
-
|
|
42
|
+
def get_signature(self) -> ModelSignature:
|
|
43
|
+
# signature is useless if input_example is simple enough
|
|
44
|
+
return None
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
class MlflowModel(mp.PythonModel):
|
|
41
48
|
|
|
42
49
|
def __init__(self) -> None:
|
|
43
50
|
super().__init__()
|
|
44
|
-
|
|
51
|
+
|
|
45
52
|
self.model = None
|
|
53
|
+
self.predictor_key: str = None
|
|
54
|
+
self.valuator_key: str = None
|
|
55
|
+
|
|
56
|
+
# from IMlfowModelProvider
|
|
57
|
+
self.transform = TorchTransform() # default one, only transforms to torch Tensor
|
|
46
58
|
self.signature = None
|
|
47
59
|
self.input_example = None
|
|
48
|
-
|
|
49
|
-
|
|
60
|
+
|
|
61
|
+
# from context
|
|
50
62
|
self.predictor: Predictor = None
|
|
51
63
|
self.valuator: Valuator = None
|
|
52
64
|
|
|
53
65
|
def load_context(self, context: mp.PythonModelContext):
|
|
54
66
|
# Prédire avec le modèle sur cpu
|
|
55
|
-
|
|
56
|
-
self.
|
|
67
|
+
factory = Factory()
|
|
68
|
+
self.valuator = factory.create_valuator(
|
|
69
|
+
self.valuator_key,
|
|
70
|
+
self.model,
|
|
71
|
+
"cpu"
|
|
72
|
+
)
|
|
73
|
+
self.predictor = factory.create_predictor(
|
|
74
|
+
self.predictor_key
|
|
75
|
+
)
|
|
57
76
|
|
|
58
77
|
def predict(
|
|
59
78
|
self, context: mp.PythonModelContext, model_input: np.ndarray, params: dict[str, Any] | None = None
|
|
60
79
|
):
|
|
61
80
|
print(f"predict called with input shape: {model_input.shape}")
|
|
62
81
|
|
|
63
|
-
model_input_tensor = self.transform
|
|
64
|
-
context, model_input, params)
|
|
82
|
+
model_input_tensor = self.transform(model_input)
|
|
65
83
|
|
|
66
84
|
outputs_tensor = self.valuator.evaluate_one(model_input_tensor)
|
|
67
85
|
|
|
68
86
|
predicted_tensor = self.predictor(outputs_tensor)
|
|
69
87
|
|
|
70
88
|
return predicted_tensor.numpy()
|
|
71
|
-
|
|
72
|
-
def setattr(self, **kwargs):
|
|
73
|
-
for param, value in kwargs.items():
|
|
74
|
-
if hasattr(self, param):
|
|
75
|
-
setattr(self, param, value)
|
|
76
|
-
else:
|
|
77
|
-
raise Exception(
|
|
78
|
-
f'The wrapper class does not have the attribute "{param}"')
|
|
79
|
-
|
|
80
|
-
self.signature, self.input_example = self.transform.get_signature()
|
iapytoo/train/training.py
CHANGED
|
@@ -12,17 +12,11 @@ from iapytoo.utils.config import Config
|
|
|
12
12
|
from iapytoo.utils.timer import Timer
|
|
13
13
|
from iapytoo.utils.iterative_mean import Mean
|
|
14
14
|
from iapytoo.train.loss import Loss
|
|
15
|
-
from iapytoo.train.factories import
|
|
16
|
-
ModelFactory,
|
|
17
|
-
OptimizerFactory,
|
|
18
|
-
SchedulerFactory,
|
|
19
|
-
LossFactory,
|
|
20
|
-
)
|
|
15
|
+
from iapytoo.train.factories import Factory
|
|
21
16
|
from iapytoo.train.logger import Logger
|
|
22
17
|
from iapytoo.train.checkpoint import CheckPoint
|
|
23
|
-
from iapytoo.train.mlflow_model import ModelWrapper, Transform
|
|
24
18
|
from iapytoo.train.inference import Inference
|
|
25
|
-
from iapytoo.metrics import MetricsCollection
|
|
19
|
+
from iapytoo.metrics.collection import MetricsCollection
|
|
26
20
|
|
|
27
21
|
|
|
28
22
|
class LossType(IntEnum):
|
|
@@ -59,7 +53,6 @@ class Training(Inference):
|
|
|
59
53
|
self.loss = Loss(n_losses=2)
|
|
60
54
|
self._optimizers = []
|
|
61
55
|
self._schedulers = []
|
|
62
|
-
self.model_wrapper: ModelWrapper = None
|
|
63
56
|
|
|
64
57
|
@property
|
|
65
58
|
def scheduler(self):
|
|
@@ -110,14 +103,14 @@ class Training(Inference):
|
|
|
110
103
|
# ----------------------------------------
|
|
111
104
|
|
|
112
105
|
def _create_criterion(self):
|
|
113
|
-
return
|
|
106
|
+
return Factory().create_loss(self._config.training.loss)
|
|
114
107
|
|
|
115
108
|
def _get_lr(self, optimizer):
|
|
116
109
|
for param_group in optimizer.param_groups:
|
|
117
110
|
return param_group["lr"]
|
|
118
111
|
|
|
119
112
|
def _create_optimizers(self):
|
|
120
|
-
optimizer =
|
|
113
|
+
optimizer = Factory().create_optimizer(
|
|
121
114
|
self._config.training.optimizer, self.model, self._config
|
|
122
115
|
)
|
|
123
116
|
|
|
@@ -125,29 +118,17 @@ class Training(Inference):
|
|
|
125
118
|
|
|
126
119
|
# overwrite
|
|
127
120
|
def _create_models(self, loader):
|
|
128
|
-
model =
|
|
129
|
-
|
|
121
|
+
model = Factory().create_model(self._config.model.model,
|
|
122
|
+
self._config, loader, self.device)
|
|
130
123
|
|
|
131
124
|
return [model]
|
|
132
125
|
|
|
133
126
|
def _create_schedulers(self, optimizer):
|
|
134
|
-
scheduler =
|
|
127
|
+
scheduler = Factory().create_scheduler(
|
|
135
128
|
self._config.training.scheduler, optimizer, self._config
|
|
136
129
|
)
|
|
137
130
|
return [scheduler]
|
|
138
131
|
|
|
139
|
-
def _create_model_wrapper(self, loader: DataLoader = None, transform: Transform = None):
|
|
140
|
-
|
|
141
|
-
model_wrapper = ModelWrapper()
|
|
142
|
-
model_wrapper.setattr(
|
|
143
|
-
model=self.model,
|
|
144
|
-
transform=transform,
|
|
145
|
-
predictor_key=self._config.model.predictor,
|
|
146
|
-
loader=loader
|
|
147
|
-
)
|
|
148
|
-
|
|
149
|
-
return model_wrapper
|
|
150
|
-
|
|
151
132
|
def _inner_train(self, batch, batch_idx, metrics: MetricsCollection):
|
|
152
133
|
X, Y = batch
|
|
153
134
|
X = X.to(self.device)
|
|
@@ -183,8 +164,7 @@ class Training(Inference):
|
|
|
183
164
|
if epoch % 10 == 0 or epoch == num_epochs - 1:
|
|
184
165
|
if "valid_loader" in kwargs and len(self.predictions) > 0:
|
|
185
166
|
|
|
186
|
-
self.predictions.compute(
|
|
187
|
-
loader=kwargs["valid_loader"], valuator=self._valuator())
|
|
167
|
+
self.predictions.compute(loader=kwargs["valid_loader"])
|
|
188
168
|
self.logger.report_prediction(epoch, self.predictions)
|
|
189
169
|
|
|
190
170
|
for item in self.loss(LossType.TRAIN).buffer:
|
|
@@ -293,7 +273,7 @@ class Training(Inference):
|
|
|
293
273
|
lr / 1e-8) ** (1 / ((num_batch * num_epochs) - 1))
|
|
294
274
|
self._config.training.step_size = 1
|
|
295
275
|
self.optimizer.param_groups[0]["lr"] = 1e-8
|
|
296
|
-
self._schedulers = [
|
|
276
|
+
self._schedulers = [Factory().create_scheduler(
|
|
297
277
|
"step", self.optimizer, self._config)]
|
|
298
278
|
|
|
299
279
|
train_time = Timer()
|
|
@@ -337,17 +317,16 @@ class Training(Inference):
|
|
|
337
317
|
train_time.stop()
|
|
338
318
|
|
|
339
319
|
def fit(
|
|
340
|
-
self, train_loader: DataLoader, valid_loader: DataLoader,
|
|
320
|
+
self, train_loader: DataLoader, valid_loader: DataLoader, run_id=None
|
|
341
321
|
):
|
|
342
322
|
num_epochs = self._config.training.epochs
|
|
343
323
|
|
|
344
324
|
self.loss.reset()
|
|
345
325
|
|
|
346
|
-
self._models = self._create_models(train_loader)
|
|
326
|
+
self._models = self._create_models(loader=train_loader)
|
|
347
327
|
self._optimizers = self._create_optimizers()
|
|
348
328
|
self._schedulers = self._create_schedulers(self.optimizer)
|
|
349
|
-
|
|
350
|
-
self.model_wrapper = self._create_model_wrapper(transform=transform)
|
|
329
|
+
self._init_mlflow_model()
|
|
351
330
|
|
|
352
331
|
checkpoint = CheckPoint(run_id)
|
|
353
332
|
checkpoint.init(self)
|
|
@@ -373,7 +352,7 @@ class Training(Inference):
|
|
|
373
352
|
self._on_epoch_ended(
|
|
374
353
|
epoch, checkpoint, valid_loader=valid_loader)
|
|
375
354
|
|
|
376
|
-
self.logger.save_model(self.
|
|
355
|
+
self.logger.save_model(self.mlflow_model)
|
|
377
356
|
|
|
378
357
|
return {
|
|
379
358
|
"run_id": self.logger.run_id,
|
|
@@ -395,4 +374,4 @@ class Training(Inference):
|
|
|
395
374
|
assert self.model is not None, "no model loaded for prediction"
|
|
396
375
|
|
|
397
376
|
assert self.predictions is not None, "no predictions defined for this training"
|
|
398
|
-
self.predictions.compute(loader=loader
|
|
377
|
+
self.predictions.compute(loader=loader)
|
iapytoo/train/valuator.py
CHANGED
|
@@ -41,3 +41,21 @@ class ModelValuator(Valuator):
|
|
|
41
41
|
outputs = model_output.detach().cpu()
|
|
42
42
|
actual = Y.detach().cpu()
|
|
43
43
|
yield outputs, actual
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
class WGANValuator(ModelValuator):
|
|
47
|
+
|
|
48
|
+
# override
|
|
49
|
+
def evaluate_loader(self, loader: DataLoader):
|
|
50
|
+
|
|
51
|
+
device = self.device
|
|
52
|
+
generator = self.model
|
|
53
|
+
generator.eval()
|
|
54
|
+
with torch.no_grad():
|
|
55
|
+
for X in loader:
|
|
56
|
+
X = X.to(device)
|
|
57
|
+
gen_output = generator(X)
|
|
58
|
+
|
|
59
|
+
outputs = gen_output.detach().cpu()
|
|
60
|
+
|
|
61
|
+
yield outputs, None
|
iapytoo/train/wgan.py
CHANGED
|
@@ -1,17 +1,16 @@
|
|
|
1
1
|
import sys
|
|
2
2
|
import torch
|
|
3
|
-
from tqdm import tqdm
|
|
4
3
|
import logging
|
|
4
|
+
from enum import IntEnum
|
|
5
|
+
from tqdm import tqdm
|
|
5
6
|
|
|
6
7
|
from iapytoo.train.training import Training
|
|
7
8
|
from iapytoo.utils.config import Config
|
|
8
|
-
from iapytoo.train.factories import
|
|
9
|
+
from iapytoo.train.factories import Factory
|
|
9
10
|
from iapytoo.train.loss import Loss
|
|
10
11
|
from iapytoo.train.logger import Logger
|
|
11
12
|
from iapytoo.train.checkpoint import CheckPoint
|
|
12
13
|
from iapytoo.utils.timer import Timer
|
|
13
|
-
from iapytoo.train.training import InferenceValuator
|
|
14
|
-
from enum import IntEnum
|
|
15
14
|
|
|
16
15
|
|
|
17
16
|
class WGAN_FCT(IntEnum):
|
|
@@ -51,28 +50,27 @@ class WGAN(Training):
|
|
|
51
50
|
return None
|
|
52
51
|
|
|
53
52
|
def _create_models(self, loader):
|
|
54
|
-
|
|
53
|
+
factory = Factory()
|
|
54
|
+
generator = factory.create_model(
|
|
55
55
|
self._config.model.generator, self._config, loader, self.device
|
|
56
56
|
)
|
|
57
|
-
discriminator =
|
|
57
|
+
discriminator = factory.create_model(
|
|
58
58
|
self._config.model.discriminator, self._config, loader, self.device
|
|
59
59
|
)
|
|
60
60
|
|
|
61
61
|
return [generator, discriminator]
|
|
62
62
|
|
|
63
63
|
def _create_optimizers(self):
|
|
64
|
-
|
|
64
|
+
factory = Factory()
|
|
65
|
+
g_optimizer = factory.create_optimizer(
|
|
65
66
|
self._config.training.optimizer, self.generator, self._config
|
|
66
67
|
)
|
|
67
|
-
d_optimizer =
|
|
68
|
+
d_optimizer = factory.create_optimizer(
|
|
68
69
|
self._config.training.optimizer, self.discriminator, self._config
|
|
69
70
|
)
|
|
70
71
|
|
|
71
72
|
return [g_optimizer, d_optimizer]
|
|
72
73
|
|
|
73
|
-
def _valuator(self, loader):
|
|
74
|
-
return WGANValuator(self, loader)
|
|
75
|
-
|
|
76
74
|
@staticmethod
|
|
77
75
|
def freeze_params(model, freeze: bool):
|
|
78
76
|
for param in model.parameters():
|
|
@@ -160,7 +158,9 @@ class WGAN(Training):
|
|
|
160
158
|
def _on_epoch_ended(self, epoch, checkpoint, **kwargs):
|
|
161
159
|
if epoch % 10 == 0:
|
|
162
160
|
if "loader" in kwargs:
|
|
163
|
-
self.predictions.compute(
|
|
161
|
+
self.predictions.compute(
|
|
162
|
+
loader=kwargs["loader"]
|
|
163
|
+
)
|
|
164
164
|
self.logger.report_prediction(epoch, self.predictions)
|
|
165
165
|
|
|
166
166
|
for item in self.loss(WGAN_FCT.GENERATOR).buffer:
|
|
@@ -284,6 +284,7 @@ class WGAN(Training):
|
|
|
284
284
|
|
|
285
285
|
self._models = self._create_models(train_loader)
|
|
286
286
|
self._optimizers = self._create_optimizers()
|
|
287
|
+
self._init_mlflow_model()
|
|
287
288
|
|
|
288
289
|
checkpoint = CheckPoint(run_id)
|
|
289
290
|
checkpoint.init(self)
|
|
@@ -291,7 +292,6 @@ class WGAN(Training):
|
|
|
291
292
|
with Logger(self._config, run_id=checkpoint.run_id) as self.logger:
|
|
292
293
|
active_run_name = self.logger.active_run_name()
|
|
293
294
|
self._display_device()
|
|
294
|
-
self.logger.set_signature(train_loader)
|
|
295
295
|
self.logger.summary()
|
|
296
296
|
|
|
297
297
|
for epoch in range(checkpoint.epoch + 1, num_epochs):
|
|
@@ -304,7 +304,7 @@ class WGAN(Training):
|
|
|
304
304
|
|
|
305
305
|
self._on_epoch_ended(epoch, checkpoint, loader=valid_loader)
|
|
306
306
|
|
|
307
|
-
self.logger.save_model(self.
|
|
307
|
+
self.logger.save_model(self.mlflow_model)
|
|
308
308
|
|
|
309
309
|
return {
|
|
310
310
|
"run_id": self.logger.run_id,
|
|
@@ -312,20 +312,3 @@ class WGAN(Training):
|
|
|
312
312
|
"g_loss": self.loss(WGAN_FCT.GENERATOR).value,
|
|
313
313
|
"d_loss": self.loss(WGAN_FCT.DISCRIMINATOR).value,
|
|
314
314
|
}
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
class WGANValuator(InferenceValuator):
|
|
318
|
-
|
|
319
|
-
def evaluate(self):
|
|
320
|
-
training: WGAN = self.inference
|
|
321
|
-
device = training.device
|
|
322
|
-
generator = training.generator
|
|
323
|
-
generator.eval()
|
|
324
|
-
with torch.no_grad():
|
|
325
|
-
for X in self.loader:
|
|
326
|
-
X = X.to(device)
|
|
327
|
-
gen_output = generator(X)
|
|
328
|
-
|
|
329
|
-
outputs = gen_output.detach().cpu()
|
|
330
|
-
|
|
331
|
-
yield outputs, None
|
iapytoo/utils/model_config.py
CHANGED
|
@@ -5,7 +5,8 @@ from iapytoo.utils.singleton import singleton
|
|
|
5
5
|
|
|
6
6
|
class ModelConfig(BaseModel):
|
|
7
7
|
type: str
|
|
8
|
-
predictor: Optional[str] =
|
|
8
|
+
predictor: Optional[str] = "default"
|
|
9
|
+
valuator: Optional[str] = "model"
|
|
9
10
|
|
|
10
11
|
def _network(self) -> str:
|
|
11
12
|
pass
|
|
@@ -25,6 +26,7 @@ class DefaultModelConfig(ModelConfig):
|
|
|
25
26
|
class GanConfig(ModelConfig):
|
|
26
27
|
generator: str
|
|
27
28
|
discriminator: str
|
|
29
|
+
hidden_size: Optional[int] = 128
|
|
28
30
|
lambda_gp: Optional[float] = 10.0
|
|
29
31
|
noise_dim: Optional[int] = 100
|
|
30
32
|
n_critic: Optional[int] = 5
|
|
@@ -5,45 +5,46 @@ dockers/requirements.txt,sha256=UHHYUcXk8-6CX0hTVCZWVA5IJgk7LsTsHjusvgCMSlQ,45
|
|
|
5
5
|
dockers/run-python.sh,sha256=uvdb0f2gn6mfOCDMDbmnI448RpIl_Ii_ciWeezJ0b9o,178
|
|
6
6
|
examples/zipped_runs/selected_runs.zip,sha256=054yDlFOFpq7MTopd5Miv0Hjqy0u8vPfK0oKmUYyrAk,17588896
|
|
7
7
|
iapytoo/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
8
|
-
iapytoo/_version.py,sha256=
|
|
8
|
+
iapytoo/_version.py,sha256=CaXtNMxaqC4Pj3KcWYVMvP1hxz38qgmTUFwaZjTPaYY,524
|
|
9
9
|
iapytoo/dataset/__init__.py,sha256=3FTo2mCSPGvQVMNsMwuRIiCOE_pMD8_y-rHN5wmFgwc,2107
|
|
10
10
|
iapytoo/dataset/scaling.py,sha256=kOTVHE4EiiEW0C69GBWH6_FMIS1cHIJNcjwulXb-Liw,2484
|
|
11
|
-
iapytoo/metrics/__init__.py,sha256=
|
|
11
|
+
iapytoo/metrics/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
12
|
+
iapytoo/metrics/collection.py,sha256=8g60t7-bBBrgfZpcx9rJ7pC9pUqExQpmzq-JUHY5SVE,1274
|
|
12
13
|
iapytoo/metrics/distrib.py,sha256=r9W17STWckV1LXEcMNOSKY9cnQiuBI7H2OMpTxzIBgI,573
|
|
13
|
-
iapytoo/metrics/metric.py,sha256=
|
|
14
|
+
iapytoo/metrics/metric.py,sha256=YdU68v6dz_2rjCUPX4TAdWXlRCRtcZAA1KhNNJyC7zA,1307
|
|
14
15
|
iapytoo/metrics/predefined.py,sha256=lyGNiOGp_eg7bTg5P_7FAeh6OtkAeLxJlwx4khcNmCs,2420
|
|
15
|
-
iapytoo/predictions/__init__.py,sha256=
|
|
16
|
+
iapytoo/predictions/__init__.py,sha256=r-0rU8k1dk3RG0YBujdv-PiseTIEWKSk0FTyaleJlFk,2166
|
|
16
17
|
iapytoo/predictions/feature.py,sha256=ZjOxJPEaq3M_4N3mZbV65kscP6iCNgH4jW3TFQCnJbA,569
|
|
17
18
|
iapytoo/predictions/plotters.py,sha256=GAjlarLEErfBjrsKIMNpxd79HeLGDteFuw13LusS1k4,5477
|
|
18
|
-
iapytoo/predictions/predictors.py,sha256=
|
|
19
|
+
iapytoo/predictions/predictors.py,sha256=ZLXQhHbl9JMyDf4mpfcC1p5kRmD9A1nZDMH6RJH3sS4,352
|
|
19
20
|
iapytoo/predictions/types.py,sha256=facF4mzUCu5_7yFOTwUVgQdV8KowN8fZlUIVziZLKT4,107
|
|
20
21
|
iapytoo/train/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
21
22
|
iapytoo/train/checkpoint.py,sha256=1sSWRloJFrsEWF3t6tJStMSshP8kfPeeXyF-ltoEWbw,1785
|
|
22
23
|
iapytoo/train/context.py,sha256=zigKX8FchxAiKMQLhxIH516EVCHekkww7ZslqAQVoos,1059
|
|
23
|
-
iapytoo/train/factories.py,sha256=
|
|
24
|
-
iapytoo/train/inference.py,sha256=
|
|
25
|
-
iapytoo/train/logger.py,sha256=
|
|
24
|
+
iapytoo/train/factories.py,sha256=Xxa9PVwFIg_fVDcbeLGvsl_UAjS_BAGOW2MxlfUgrK4,8605
|
|
25
|
+
iapytoo/train/inference.py,sha256=vjTq1zPdnY5bljjQS4tVIWksXx11zvCppseFVJVxt_k,5899
|
|
26
|
+
iapytoo/train/logger.py,sha256=62uUK6TTmXqFe8aKnlcJV9Y_wCcnvgWqz4K0k4sv4uI,6007
|
|
26
27
|
iapytoo/train/loss.py,sha256=MOYvtqobShv1X1Oc544v4r9hJIrOvz9kbU4GGhk0XIY,928
|
|
27
|
-
iapytoo/train/mlflow_model.py,sha256=
|
|
28
|
-
iapytoo/train/training.py,sha256=
|
|
29
|
-
iapytoo/train/valuator.py,sha256=
|
|
30
|
-
iapytoo/train/wgan.py,sha256=
|
|
28
|
+
iapytoo/train/mlflow_model.py,sha256=LUa73dojaeDoPREsJVpMNggTRwzup-fufHfpLxvWY54,2393
|
|
29
|
+
iapytoo/train/training.py,sha256=shh81euwisi6b5P6JozX5iGMuGyagJ2S9poxAeyb8PE,13280
|
|
30
|
+
iapytoo/train/valuator.py,sha256=4OODUy6Gp0n5kmTT5wUSE3YAe4eztBU0edSi1lRVjHI,1445
|
|
31
|
+
iapytoo/train/wgan.py,sha256=lBJPv_diMpbdNJD_m0hmGB8n_cec5Pj5sy-F8saUtYs,10859
|
|
31
32
|
iapytoo/utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
32
33
|
iapytoo/utils/arguments.py,sha256=q-8et8TBVSu3tNKLehVlR_nGFXlFegA6AU33dVTH6rs,1112
|
|
33
34
|
iapytoo/utils/config.py,sha256=wh14Lu-yWldx7r0iYqxhw-fVBo9X1rlkJS2_F70gNv0,11386
|
|
34
35
|
iapytoo/utils/display.py,sha256=T2lL91QKKBfDuPjTujRYQGfHITT6b8wBwf2qgWFds2g,2703
|
|
35
36
|
iapytoo/utils/iterative_mean.py,sha256=LU0gT9XVcZXNp4RNTFnh1IqQq2W7Y0q3HmmrpJVE8x0,1709
|
|
36
|
-
iapytoo/utils/model_config.py,sha256=
|
|
37
|
+
iapytoo/utils/model_config.py,sha256=fN1XBuaRUVhWnDe2QBS5IaKgVMzyvBNTPxqM5pODENo,1817
|
|
37
38
|
iapytoo/utils/singleton.py,sha256=t1jcRH4dNNLgjMElExpecATnDso_Y5RQ1vB4h2envyM,411
|
|
38
39
|
iapytoo/utils/time_utils.py,sha256=K9EmJO9ZP4Cb-ccOIFHNwG4ruqK9uIv0GIX6J5fqlpA,935
|
|
39
40
|
iapytoo/utils/timer.py,sha256=ORSNQbkyClHhFJdPitden9xMFy1LxXR1ViVaolsbR5o,1642
|
|
40
41
|
iapytoo/utils/cf/config.json,sha256=ErTtw1v70qs3ut6hPNXguVp7JqXRulRRYBFJXh1c1Fw,351
|
|
41
42
|
iapytoo/utils/cf/config.yml,sha256=YRBsQ5sbvGZb9Zm3Db2UiVAeWkhMG00Z6KlQQjEO0rw,278
|
|
42
43
|
iapytoo/utils/cf/config_test.json,sha256=SsR4QFN4yecgUhy8Kwm9t6KghdMMkUWXIG3rf24moU0,356
|
|
43
|
-
iapytoo-0.0.6.
|
|
44
|
+
iapytoo-0.0.6.dev2.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
|
|
44
45
|
mlflow/relocate_mlruns.py,sha256=4xbcvHA-8f8K4PX23dIwU8W8Iem8hiODRIhIO_Xpv_s,2115
|
|
45
46
|
mlflow/zip_selected_runs.py,sha256=DCo4oJRpfzIMvhU7uvU_ufmQ5mHnwkykSa5ApkNfQ0w,2725
|
|
46
|
-
iapytoo-0.0.6.
|
|
47
|
-
iapytoo-0.0.6.
|
|
48
|
-
iapytoo-0.0.6.
|
|
49
|
-
iapytoo-0.0.6.
|
|
47
|
+
iapytoo-0.0.6.dev2.dist-info/METADATA,sha256=KfxxLXycogFyvB9HMIBIan08pV_i-yxwLKaPW6ZkjH4,1074
|
|
48
|
+
iapytoo-0.0.6.dev2.dist-info/WHEEL,sha256=zaaOINJESkSfm_4HQVc5ssNzHCPXhJm0kEUakpsEHaU,91
|
|
49
|
+
iapytoo-0.0.6.dev2.dist-info/top_level.txt,sha256=GsVzjiEsq_Biu7ZRyL1f-N_nMySEpkl9z65lmFMR1Ew,32
|
|
50
|
+
iapytoo-0.0.6.dev2.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|