iapytoo 0.0.4.dev3__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.

Files changed (38) hide show
  1. dockers/docker-compose.yml +15 -0
  2. dockers/requirements.txt +6 -0
  3. dockers/run-python.sh +1 -2
  4. examples/zipped_runs/selected_runs.zip +0 -0
  5. iapytoo/_version.py +9 -4
  6. iapytoo/dataset/__init__.py +4 -8
  7. iapytoo/metrics/__init__.py +0 -82
  8. iapytoo/metrics/collection.py +39 -0
  9. iapytoo/metrics/metric.py +16 -9
  10. iapytoo/metrics/predefined.py +13 -12
  11. iapytoo/predictions/__init__.py +33 -42
  12. iapytoo/predictions/feature.py +1 -2
  13. iapytoo/predictions/plotters.py +39 -11
  14. iapytoo/predictions/predictors.py +0 -2
  15. iapytoo/predictions/types.py +0 -2
  16. iapytoo/train/checkpoint.py +18 -9
  17. iapytoo/train/factories.py +188 -112
  18. iapytoo/train/inference.py +175 -0
  19. iapytoo/train/logger.py +52 -54
  20. iapytoo/train/loss.py +4 -5
  21. iapytoo/train/mlflow_model.py +88 -0
  22. iapytoo/train/training.py +61 -101
  23. iapytoo/train/valuator.py +61 -0
  24. iapytoo/train/wgan.py +104 -46
  25. iapytoo/utils/arguments.py +19 -34
  26. iapytoo/utils/cf/config.yml +21 -0
  27. iapytoo/utils/config.py +300 -79
  28. iapytoo/utils/model_config.py +74 -0
  29. iapytoo/utils/singleton.py +15 -0
  30. {iapytoo-0.0.4.dev3.dist-info → iapytoo-0.0.6.dev2.dist-info}/METADATA +3 -2
  31. iapytoo-0.0.6.dev2.dist-info/RECORD +50 -0
  32. {iapytoo-0.0.4.dev3.dist-info → iapytoo-0.0.6.dev2.dist-info}/WHEEL +1 -1
  33. {iapytoo-0.0.4.dev3.dist-info → iapytoo-0.0.6.dev2.dist-info}/top_level.txt +2 -0
  34. mlflow/relocate_mlruns.py +67 -0
  35. mlflow/zip_selected_runs.py +80 -0
  36. iapytoo/utils/meta_singleton.py +0 -17
  37. iapytoo-0.0.4.dev3.dist-info/RECORD +0 -39
  38. {iapytoo-0.0.4.dev3.dist-info → iapytoo-0.0.6.dev2.dist-info/licenses}/LICENSE +0 -0
@@ -0,0 +1,15 @@
1
+ version: "3.8"
2
+
3
+ services:
4
+ iapytoo:
5
+ build:
6
+ context: .
7
+ dockerfile: Dockerfile.ifpen
8
+ args:
9
+ http_proxy: ${http_proxy}
10
+ https_proxy: ${https_proxy}
11
+ no_proxy: ${no_proxy}
12
+ image: iapytoo:latest
13
+
14
+ env_file:
15
+ - .env
@@ -0,0 +1,6 @@
1
+ tqdm
2
+ numpy
3
+ matplotlib
4
+ mlflow
5
+ seaborn
6
+ iapytoo
dockers/run-python.sh CHANGED
@@ -5,5 +5,4 @@ docker run --rm --gpus all \
5
5
  -w /app \
6
6
  -e PYTHONPATH=/app \
7
7
  iapytoo:latest \
8
- python3 examples/mnist.py -c examples/config_mnist.json data
9
-
8
+ python3 examples/mnist.py --yaml examples/config_mnist.yml
Binary file
iapytoo/_version.py CHANGED
@@ -1,8 +1,13 @@
1
- # file generated by setuptools_scm
1
+ # file generated by setuptools-scm
2
2
  # don't change, don't track in version control
3
+
4
+ __all__ = ["__version__", "__version_tuple__", "version", "version_tuple"]
5
+
3
6
  TYPE_CHECKING = False
4
7
  if TYPE_CHECKING:
5
- from typing import Tuple, Union
8
+ from typing import Tuple
9
+ from typing import Union
10
+
6
11
  VERSION_TUPLE = Tuple[Union[int, str], ...]
7
12
  else:
8
13
  VERSION_TUPLE = object
@@ -12,5 +17,5 @@ __version__: str
12
17
  __version_tuple__: VERSION_TUPLE
13
18
  version_tuple: VERSION_TUPLE
14
19
 
15
- __version__ = version = '0.0.4.dev3'
16
- __version_tuple__ = version_tuple = (0, 0, 4, 'dev3')
20
+ __version__ = version = '0.0.6.dev2'
21
+ __version_tuple__ = version_tuple = (0, 0, 6, 'dev2')
@@ -13,8 +13,7 @@ class DummyVisionDataset(VisionDataset):
13
13
  np.random.seed(12)
14
14
  A = np.array(range(self.dim * self.dim)).reshape((self.dim, self.dim))
15
15
  self.images = np.zeros(
16
- shape=(self.length, self.n_channels,
17
- self.dim, self.dim), dtype=np.float32
16
+ shape=(self.length, self.n_channels, self.dim, self.dim), dtype=np.float32
18
17
  )
19
18
  for idx in range(self.length):
20
19
  for c in range(self.n_channels):
@@ -24,10 +23,8 @@ class DummyVisionDataset(VisionDataset):
24
23
 
25
24
  self.targets = np.vstack(
26
25
  [
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)),
26
+ np.array(range(self.length)) + np.random.normal(size=(self.length)),
27
+ np.array(range(self.length)) + np.random.normal(size=(self.length)),
31
28
  ]
32
29
  )
33
30
 
@@ -49,8 +46,7 @@ class DummyLabelDataset(VisionDataset):
49
46
  np.random.seed(12)
50
47
  A = np.array(range(self.dim * self.dim)).reshape((self.dim, self.dim))
51
48
  self.images = np.zeros(
52
- shape=(self.length, self.n_channels,
53
- self.dim, self.dim), dtype=np.float32
49
+ shape=(self.length, self.n_channels, self.dim, self.dim), dtype=np.float32
54
50
  )
55
51
  for idx in range(self.length):
56
52
  for c in range(self.n_channels):
@@ -1,82 +0,0 @@
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,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,39 +1,46 @@
1
1
  import torch
2
2
 
3
+ from iapytoo.utils.config import Config
4
+
3
5
 
4
6
  class Metric:
5
- def __init__(self, name, config, with_target=True):
7
+ def __init__(self, name, config: Config, with_target=True):
8
+ from iapytoo.train.factories import Factory
6
9
  self.with_target = with_target
7
10
  self.name = name
8
11
  self.config = config
12
+ self.predictor = Factory().create_predictor(config)
9
13
 
10
14
  # First dimension is for the concatenation.
11
- self.predicted = torch.zeros(size=(0,))
15
+ self.outputs = torch.zeros(size=(0,))
12
16
  if self.with_target:
13
17
  self.target = torch.zeros(size=(0,))
14
18
  self.results = {}
15
19
 
16
20
  @property
17
21
  def device(self):
18
- return self.predicted.device
22
+ return self.outputs.device
23
+
24
+ @property
25
+ def predicted(self):
26
+ return self.predictor(self.outputs)
19
27
 
20
28
  def to(self, device):
21
- self.predicted = self.predicted.to(device)
29
+ self.outputs = self.outputs.to(device)
22
30
  if self.with_target:
23
31
  self.target = self.target.to(device)
24
32
  return self
25
33
 
26
- def update(self, Y_hat, Y):
27
- self.predicted = torch.cat((self.predicted, Y_hat), dim=0)
34
+ def update(self, outputs, Y):
35
+ self.outputs = torch.cat((self.outputs, outputs), dim=0)
28
36
  if self.with_target:
29
37
  self.target = torch.cat((self.target, Y), dim=0)
30
38
 
31
39
  def reset(self):
32
40
  device = self.device
33
- self.predicted = torch.zeros(size=self.output_size, device=device)
41
+ self.outputs = torch.zeros(size=(0,), device=device)
34
42
  if self.with_target:
35
- self.target = torch.zeros(
36
- size=self.output_size, device=self.device)
43
+ self.target = torch.zeros(size=(0,), device=self.device)
37
44
 
38
45
  def compute(self):
39
46
  pass
@@ -1,34 +1,35 @@
1
-
2
1
  import torch
2
+
3
+ from iapytoo.utils.config import Config
3
4
  from iapytoo.metrics.metric import Metric
4
5
 
5
6
 
6
7
  class AccuracyMetric(Metric):
7
- def __init__(self, config):
8
+ def __init__(self, config: Config):
8
9
  super(AccuracyMetric, self).__init__("accuracy", config)
9
- self.k = config['top_accuracy']
10
+ self.k = config.metrics.top_accuracy
10
11
 
11
12
  def compute(self):
12
13
 
13
14
  # Take care, for this metrics predicted and target do not have the same shape.
14
15
  # predicted : ouputs of the models - kind of probabilities
15
16
  # Target : label of the class
16
- _, top_pred = self.predicted.topk(self.k, 1)
17
+ _, top_pred = self.outputs.topk(self.k, 1)
17
18
  top_pred = top_pred.t()
18
19
  correct = top_pred.eq(self.target.view(1, -1).expand_as(top_pred))
19
20
  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
+ correct_k = correct[: self.k].reshape(-1).float().sum(0, keepdim=True)
21
22
 
22
23
  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)
24
+ "top-1": torch.round(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
+ ),
27
28
  }
28
29
 
29
30
 
30
31
  class R2Metric(Metric):
31
- def __init__(self, config) -> None:
32
+ def __init__(self, config: Config) -> None:
32
33
  super(R2Metric, self).__init__("r2", config)
33
34
 
34
35
  def compute(self):
@@ -49,7 +50,7 @@ class R2Metric(Metric):
49
50
 
50
51
 
51
52
  class MSMetric(Metric):
52
- def __init__(self, config) -> None:
53
+ def __init__(self, config: Config) -> None:
53
54
  super(MSMetric, self).__init__("mean_square", config)
54
55
 
55
56
  def _compute(self):
@@ -63,7 +64,7 @@ class MSMetric(Metric):
63
64
 
64
65
 
65
66
  class RMSMetric(MSMetric):
66
- def __init__(self, config) -> None:
67
+ def __init__(self, config: Config) -> None:
67
68
  Metric.__init__(self, "root_mean_square", config)
68
69
  self.name = "rms"
69
70
 
@@ -1,20 +1,35 @@
1
+ from __future__ import annotations
1
2
  import torch
2
- import numpy as np
3
3
 
4
4
  from .types import PredictionType
5
5
  from .plotters import CollectionPlotters
6
- from .predictors import Predictor
6
+
7
+ from typing import TYPE_CHECKING
8
+
9
+ if TYPE_CHECKING:
10
+ from iapytoo.train.inference import Inference
7
11
 
8
12
 
9
13
  class Predictions:
10
- def __init__(self, predictor: Predictor, norm=False):
11
- self.norm = norm
14
+ def __init__(self, inference: Inference):
12
15
  self.outputs = None # dimension is assigned in compute
13
16
  self.actual = None
14
- self.predictor = predictor
17
+
18
+ self.inference = inference
19
+
15
20
  self.prediction_plotter: CollectionPlotters = CollectionPlotters()
16
21
  self.prediction_plotter.connect(self)
17
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
+
18
33
  def __len__(self):
19
34
  """return the number of plotters used by the predictions"""
20
35
  return len(self.prediction_plotter)
@@ -22,35 +37,27 @@ class Predictions:
22
37
  def add_plotter(self, prediction_plotter):
23
38
  self.prediction_plotter.add(prediction_plotter)
24
39
 
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)
40
+ def compute(self, loader):
41
+
42
+ assert self.valuator is not None, "no valuator"
43
+ assert self.predictor is not None, "no predictor"
30
44
 
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)
45
+ self.outputs = torch.zeros(size=(0,))
46
+ self.actual = torch.zeros(size=(0,))
38
47
 
39
- self.outputs = torch.cat((self.outputs, model_output), dim=0)
40
- self.actual = torch.cat((self.actual, Y), dim=0)
48
+ for outputs, actual in self.valuator.evaluate_loader(loader):
49
+ self.outputs = torch.cat((self.outputs, outputs), dim=0)
41
50
 
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)
51
+ if actual is not None:
52
+ self.actual = torch.cat((self.actual, actual), dim=0)
45
53
 
46
54
  def tensor(self, type: PredictionType = PredictionType.PREDICTED):
47
55
  if type == PredictionType.PREDICTED:
48
- predicted = self.predictor(self.outputs)
49
- return predicted.detach().cpu()
56
+ return self.predictor(self.outputs)
50
57
  elif type == PredictionType.ACTUAL:
51
- return self.actual.detach().cpu()
58
+ return self.actual
52
59
  else:
53
- return self.outputs.detach().cpu()
60
+ return self.outputs
54
61
 
55
62
  def numpy(self, type: PredictionType = PredictionType.PREDICTED):
56
63
  return self.tensor(type).numpy()
@@ -60,19 +67,3 @@ class Predictions:
60
67
  return self.prediction_plotter.plot(epoch)
61
68
  else:
62
69
  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)
@@ -11,6 +11,5 @@ class Feature:
11
11
  for i in range(predictions.actual.shape[0]):
12
12
  actual_del = self.computer(predictions.actual[i, 0, :], **kwargs)
13
13
  self.actual.append(actual_del)
14
- predicted_del = self.computer(
15
- predictions.outputs[i, 0, :], **kwargs)
14
+ predicted_del = self.computer(predictions.outputs[i, 0, :], **kwargs)
16
15
  self.predicted.append(predicted_del)
@@ -7,10 +7,12 @@ from scipy.signal import welch
7
7
  import numpy as np
8
8
 
9
9
  from iapytoo.predictions.types import PredictionType
10
+ from iapytoo.utils.config import Config
11
+ from iapytoo.utils.singleton import singleton
10
12
 
11
13
 
12
14
  class PredictionPlotter:
13
- def __init__(self, title=None):
15
+ def __init__(self, title=None, config: Config | None = None):
14
16
  self.predictions = None
15
17
  self.title = title
16
18
 
@@ -23,7 +25,7 @@ class PredictionPlotter:
23
25
 
24
26
  class CollectionPlotters(PredictionPlotter):
25
27
 
26
- def __init__(self, title=None):
28
+ def __init__(self, title=None, config: Config | None = None):
27
29
  super(CollectionPlotters, self).__init__(title)
28
30
  self.plotters = []
29
31
 
@@ -43,7 +45,7 @@ class CollectionPlotters(PredictionPlotter):
43
45
 
44
46
 
45
47
  class ScatterPlotter(PredictionPlotter):
46
- def __init__(self):
48
+ def __init__(self, config: Config | None = None):
47
49
  super().__init__(title="actual_versus_predicted")
48
50
 
49
51
  def plot(self, epoch):
@@ -56,7 +58,7 @@ class ScatterPlotter(PredictionPlotter):
56
58
 
57
59
 
58
60
  class ConfusionPlotter(PredictionPlotter):
59
- def __init__(self):
61
+ def __init__(self, config: Config | None = None):
60
62
  super().__init__(title="confusion_matrix")
61
63
 
62
64
  def plot(self, epoch):
@@ -72,7 +74,7 @@ class ConfusionPlotter(PredictionPlotter):
72
74
 
73
75
  class TSNEPlotter(PredictionPlotter):
74
76
 
75
- def __init__(self):
77
+ def __init__(self, config: Config | None = None):
76
78
  super().__init__(title="tsne")
77
79
 
78
80
  def plot(self, epoch):
@@ -98,7 +100,7 @@ class TSNEPlotter(PredictionPlotter):
98
100
 
99
101
 
100
102
  class Fake1DPlotter(PredictionPlotter):
101
- def __init__(self, n_plot=4):
103
+ def __init__(self, n_plot=4, config: Config | None = None):
102
104
  super().__init__(title="generated_fake1d")
103
105
  self.n_plot = n_plot
104
106
 
@@ -107,7 +109,7 @@ class Fake1DPlotter(PredictionPlotter):
107
109
  f, a = plt.subplots(self.n_plot, self.n_plot, figsize=(8, 8))
108
110
  for i in range(self.n_plot):
109
111
  for j in range(self.n_plot):
110
- a[i][j].plot(fake[i * self.n_plot + j].view(-1))
112
+ a[i][j].plot(fake[i * self.n_plot + j, 0, :])
111
113
  a[i][j].set_xticks(())
112
114
  a[i][j].set_yticks(())
113
115
 
@@ -115,23 +117,26 @@ class Fake1DPlotter(PredictionPlotter):
115
117
 
116
118
 
117
119
  class DSPPlotter(PredictionPlotter):
118
- def __init__(self, f_max=4, nperseg=200):
120
+ def __init__(self, f_max=4, nperseg=200, config: Config | None = None):
119
121
  super().__init__(title="dsp")
120
122
  self.f_max = f_max
121
123
  self.nperseg = nperseg
122
124
 
123
125
  def plot(self, epoch):
124
- fake = self.predictions.predicted[0].view(-1)
126
+ fake = self.predictions.numpy(PredictionType.PREDICTED)
127
+ assert fake is not None and fake.shape[0] > 0, "no signal for DSP extraction"
125
128
  f, ax = plt.subplots()
126
129
 
127
- frequencies, power_spectrum = welch(fake, fs=self.f_max, nperseg=self.nperseg)
130
+ frequencies, power_spectrum = welch(
131
+ fake[0, 0, :], fs=self.f_max, nperseg=self.nperseg
132
+ )
128
133
  ax.plot(frequencies, power_spectrum)
129
134
 
130
135
  return {self.title: f}
131
136
 
132
137
 
133
138
  class Fake2DPlotter(PredictionPlotter):
134
- def __init__(self, n_plot=4):
139
+ def __init__(self, n_plot=4, config: Config | None = None):
135
140
  super().__init__("generated_fake2d")
136
141
  self.n_plot = n_plot
137
142
 
@@ -144,3 +149,26 @@ class Fake2DPlotter(PredictionPlotter):
144
149
  ax.imshow(grid_img.permute(1, 2, 0))
145
150
 
146
151
  return {self.title: f}
152
+
153
+
154
+ @singleton
155
+ class PlotterFactory:
156
+ def __init__(self) -> None:
157
+ self.plotter_dict: dict[str, type[PredictionPlotter]] = {
158
+ "scatter": ScatterPlotter,
159
+ "confusion": ConfusionPlotter,
160
+ "tsne": TSNEPlotter,
161
+ "fake1d": Fake1DPlotter,
162
+ "fake2d": Fake2DPlotter,
163
+ "dsp": DSPPlotter,
164
+ }
165
+
166
+ def register_plotter(self, key: str, plotter_cls: type[PredictionPlotter]) -> None:
167
+ self.plotter_dict[key] = plotter_cls
168
+
169
+ def create_plotter(self, kind, **kwargs) -> PredictionPlotter:
170
+ try:
171
+ plotter: PredictionPlotter = self.plotter_dict[kind](**kwargs)
172
+ except KeyError:
173
+ raise KeyError(f"Plotter {kind} doesn't exist")
174
+ return plotter
@@ -1,4 +1,3 @@
1
-
2
1
  import torch
3
2
 
4
3
 
@@ -9,7 +8,6 @@ class Predictor:
9
8
 
10
9
 
11
10
  class MaxPredictor(Predictor):
12
-
13
11
  def __call__(self, outputs: torch.Tensor) -> torch.Tensor:
14
12
  _, preds = torch.max(outputs.data, 1)
15
13
  return preds
@@ -1,5 +1,3 @@
1
-
2
-
3
1
  from enum import IntEnum
4
2
 
5
3
 
@@ -7,22 +7,22 @@ import tempfile
7
7
  class CheckPoint:
8
8
  def __init__(self, run_id=None) -> None:
9
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}"
10
+ artifact_uri = self.artifact_uri
11
+ if artifact_uri is not None:
13
12
  with tempfile.TemporaryDirectory() as tmpdir:
14
- local_artifact = mlflow.artifacts.download_artifacts(
15
- run.info.artifact_uri, dst_path=tmpdir
13
+ ck_path = mlflow.artifacts.download_artifacts(
14
+ artifact_uri=artifact_uri,
15
+ dst_path=tmpdir
16
16
  )
17
- ck_path = os.path.join(local_artifact, "checkpoints")
18
17
  files = []
19
18
  for f in os.listdir(ck_path):
20
19
  ck_name = os.path.join(ck_path, f)
21
- if os.path.isfile(ck_name):
20
+ if "checkpoint" in f and os.path.isfile(ck_name):
22
21
  files.append(ck_name)
23
22
  files.sort(key=lambda x: os.path.getmtime(x))
24
23
  ck_name = files[-1]
25
- self.params = torch.load(ck_name)
24
+ self.params = torch.load(
25
+ ck_name, weights_only=False)
26
26
  else:
27
27
  self.params["epoch"] = -1
28
28
 
@@ -30,6 +30,14 @@ class CheckPoint:
30
30
  def run_id(self):
31
31
  return self.params["run_id"]
32
32
 
33
+ @property
34
+ def artifact_uri(self):
35
+ if self.run_id is None:
36
+ return None
37
+ run = mlflow.get_run(self.run_id)
38
+ assert run is not None, f"unable to find run {self.run_id}"
39
+ return os.path.join(run.info.artifact_uri, "checkpoints")
40
+
33
41
  @property
34
42
  def epoch(self):
35
43
  return self.params["epoch"]
@@ -41,4 +49,5 @@ class CheckPoint:
41
49
 
42
50
  def init(self, training, only_model=False):
43
51
  if self.run_id is not None:
44
- training.load_state_dict(self.params["training"], only_model=only_model)
52
+ training.load_state_dict(
53
+ self.params["training"], only_model=only_model)