iapytoo 0.0.6.dev0__tar.gz → 0.0.6.dev2__tar.gz
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-0.0.6.dev0 → iapytoo-0.0.6.dev2}/PKG-INFO +1 -1
- {iapytoo-0.0.6.dev0 → iapytoo-0.0.6.dev2}/examples/config_infer.yml +1 -1
- {iapytoo-0.0.6.dev0 → iapytoo-0.0.6.dev2}/examples/config_mnist.yml +1 -1
- {iapytoo-0.0.6.dev0 → iapytoo-0.0.6.dev2}/examples/config_wgan.yml +1 -0
- {iapytoo-0.0.6.dev0 → iapytoo-0.0.6.dev2}/examples/mlflow_infer.py +8 -6
- {iapytoo-0.0.6.dev0 → iapytoo-0.0.6.dev2}/examples/mnist.py +57 -38
- {iapytoo-0.0.6.dev0 → iapytoo-0.0.6.dev2}/examples/mnist_infer.py +12 -11
- {iapytoo-0.0.6.dev0 → iapytoo-0.0.6.dev2}/examples/wgan_mnist.py +6 -6
- {iapytoo-0.0.6.dev0 → iapytoo-0.0.6.dev2}/iapytoo/_version.py +2 -2
- iapytoo-0.0.6.dev2/iapytoo/metrics/collection.py +39 -0
- {iapytoo-0.0.6.dev0 → iapytoo-0.0.6.dev2}/iapytoo/metrics/metric.py +3 -3
- {iapytoo-0.0.6.dev0 → iapytoo-0.0.6.dev2}/iapytoo/predictions/__init__.py +24 -8
- iapytoo-0.0.6.dev2/iapytoo/predictions/predictors.py +13 -0
- {iapytoo-0.0.6.dev0 → iapytoo-0.0.6.dev2}/iapytoo/train/factories.py +170 -100
- iapytoo-0.0.6.dev2/iapytoo/train/inference.py +175 -0
- {iapytoo-0.0.6.dev0 → iapytoo-0.0.6.dev2}/iapytoo/train/logger.py +2 -15
- iapytoo-0.0.6.dev2/iapytoo/train/mlflow_model.py +88 -0
- {iapytoo-0.0.6.dev0 → iapytoo-0.0.6.dev2}/iapytoo/train/training.py +14 -35
- {iapytoo-0.0.6.dev0 → iapytoo-0.0.6.dev2}/iapytoo/train/valuator.py +18 -0
- {iapytoo-0.0.6.dev0 → iapytoo-0.0.6.dev2}/iapytoo/train/wgan.py +14 -31
- iapytoo-0.0.6.dev2/iapytoo/utils/__init__.py +0 -0
- {iapytoo-0.0.6.dev0 → iapytoo-0.0.6.dev2}/iapytoo/utils/model_config.py +3 -1
- {iapytoo-0.0.6.dev0 → iapytoo-0.0.6.dev2}/iapytoo.egg-info/PKG-INFO +1 -1
- {iapytoo-0.0.6.dev0 → iapytoo-0.0.6.dev2}/iapytoo.egg-info/SOURCES.txt +1 -0
- {iapytoo-0.0.6.dev0 → iapytoo-0.0.6.dev2}/tests/test_metrics.py +1 -1
- {iapytoo-0.0.6.dev0 → iapytoo-0.0.6.dev2}/tests/test_model.py +3 -3
- iapytoo-0.0.6.dev0/iapytoo/metrics/__init__.py +0 -82
- iapytoo-0.0.6.dev0/iapytoo/predictions/predictors.py +0 -33
- iapytoo-0.0.6.dev0/iapytoo/train/inference.py +0 -93
- iapytoo-0.0.6.dev0/iapytoo/train/mlflow_model.py +0 -80
- {iapytoo-0.0.6.dev0 → iapytoo-0.0.6.dev2}/.github/workflows/python-app.yml +0 -0
- {iapytoo-0.0.6.dev0 → iapytoo-0.0.6.dev2}/.gitignore +0 -0
- {iapytoo-0.0.6.dev0 → iapytoo-0.0.6.dev2}/LICENSE +0 -0
- {iapytoo-0.0.6.dev0 → iapytoo-0.0.6.dev2}/MANIFEST.in +0 -0
- {iapytoo-0.0.6.dev0 → iapytoo-0.0.6.dev2}/README.md +0 -0
- {iapytoo-0.0.6.dev0 → iapytoo-0.0.6.dev2}/dockers/Dockerfile +0 -0
- {iapytoo-0.0.6.dev0 → iapytoo-0.0.6.dev2}/dockers/Readme.md +0 -0
- {iapytoo-0.0.6.dev0 → iapytoo-0.0.6.dev2}/dockers/docker-compose.yml +0 -0
- {iapytoo-0.0.6.dev0 → iapytoo-0.0.6.dev2}/dockers/requirements.txt +0 -0
- {iapytoo-0.0.6.dev0 → iapytoo-0.0.6.dev2}/dockers/run-python.sh +0 -0
- {iapytoo-0.0.6.dev0 → iapytoo-0.0.6.dev2}/examples/zipped_runs/selected_runs.zip +0 -0
- {iapytoo-0.0.6.dev0 → iapytoo-0.0.6.dev2}/iapytoo/__init__.py +0 -0
- {iapytoo-0.0.6.dev0 → iapytoo-0.0.6.dev2}/iapytoo/dataset/__init__.py +0 -0
- {iapytoo-0.0.6.dev0 → iapytoo-0.0.6.dev2}/iapytoo/dataset/scaling.py +0 -0
- {iapytoo-0.0.6.dev0/iapytoo/train → iapytoo-0.0.6.dev2/iapytoo/metrics}/__init__.py +0 -0
- {iapytoo-0.0.6.dev0 → iapytoo-0.0.6.dev2}/iapytoo/metrics/distrib.py +0 -0
- {iapytoo-0.0.6.dev0 → iapytoo-0.0.6.dev2}/iapytoo/metrics/predefined.py +0 -0
- {iapytoo-0.0.6.dev0 → iapytoo-0.0.6.dev2}/iapytoo/predictions/feature.py +0 -0
- {iapytoo-0.0.6.dev0 → iapytoo-0.0.6.dev2}/iapytoo/predictions/plotters.py +0 -0
- {iapytoo-0.0.6.dev0 → iapytoo-0.0.6.dev2}/iapytoo/predictions/types.py +0 -0
- {iapytoo-0.0.6.dev0/iapytoo/utils → iapytoo-0.0.6.dev2/iapytoo/train}/__init__.py +0 -0
- {iapytoo-0.0.6.dev0 → iapytoo-0.0.6.dev2}/iapytoo/train/checkpoint.py +0 -0
- {iapytoo-0.0.6.dev0 → iapytoo-0.0.6.dev2}/iapytoo/train/context.py +0 -0
- {iapytoo-0.0.6.dev0 → iapytoo-0.0.6.dev2}/iapytoo/train/loss.py +0 -0
- {iapytoo-0.0.6.dev0 → iapytoo-0.0.6.dev2}/iapytoo/utils/arguments.py +0 -0
- {iapytoo-0.0.6.dev0 → iapytoo-0.0.6.dev2}/iapytoo/utils/cf/config.json +0 -0
- {iapytoo-0.0.6.dev0 → iapytoo-0.0.6.dev2}/iapytoo/utils/cf/config.yml +0 -0
- {iapytoo-0.0.6.dev0 → iapytoo-0.0.6.dev2}/iapytoo/utils/cf/config_test.json +0 -0
- {iapytoo-0.0.6.dev0 → iapytoo-0.0.6.dev2}/iapytoo/utils/config.py +0 -0
- {iapytoo-0.0.6.dev0 → iapytoo-0.0.6.dev2}/iapytoo/utils/display.py +0 -0
- {iapytoo-0.0.6.dev0 → iapytoo-0.0.6.dev2}/iapytoo/utils/iterative_mean.py +0 -0
- {iapytoo-0.0.6.dev0 → iapytoo-0.0.6.dev2}/iapytoo/utils/singleton.py +0 -0
- {iapytoo-0.0.6.dev0 → iapytoo-0.0.6.dev2}/iapytoo/utils/time_utils.py +0 -0
- {iapytoo-0.0.6.dev0 → iapytoo-0.0.6.dev2}/iapytoo/utils/timer.py +0 -0
- {iapytoo-0.0.6.dev0 → iapytoo-0.0.6.dev2}/iapytoo.egg-info/dependency_links.txt +0 -0
- {iapytoo-0.0.6.dev0 → iapytoo-0.0.6.dev2}/iapytoo.egg-info/top_level.txt +0 -0
- {iapytoo-0.0.6.dev0 → iapytoo-0.0.6.dev2}/mlflow/relocate_mlruns.py +0 -0
- {iapytoo-0.0.6.dev0 → iapytoo-0.0.6.dev2}/mlflow/zip_selected_runs.py +0 -0
- {iapytoo-0.0.6.dev0 → iapytoo-0.0.6.dev2}/pyproject.toml +0 -0
- {iapytoo-0.0.6.dev0 → iapytoo-0.0.6.dev2}/requirements.txt +0 -0
- {iapytoo-0.0.6.dev0 → iapytoo-0.0.6.dev2}/setup.cfg +0 -0
- {iapytoo-0.0.6.dev0 → iapytoo-0.0.6.dev2}/tests/test_config.py +0 -0
- {iapytoo-0.0.6.dev0 → iapytoo-0.0.6.dev2}/tests/test_dataset.py +0 -0
- {iapytoo-0.0.6.dev0 → iapytoo-0.0.6.dev2}/tests/test_scaling.py +0 -0
|
@@ -13,23 +13,25 @@ def get_model_input(idx=0):
|
|
|
13
13
|
)
|
|
14
14
|
# prend une image du dataset MNIST de train
|
|
15
15
|
pic: Image = dataset1[idx][0]
|
|
16
|
+
expected = dataset1[idx][1]
|
|
16
17
|
# cree le numpy array
|
|
17
18
|
img_input = np.array(pic.getdata()).reshape(
|
|
18
|
-
1, pic.size[0], pic.size[1]).astype(np.
|
|
19
|
+
1, pic.size[0], pic.size[1]).astype(np.float64)
|
|
19
20
|
img_input /= 255.
|
|
20
21
|
|
|
21
22
|
# crée un batch avec une image [1, 1, 28, 28]
|
|
22
|
-
return np.array([img_input])
|
|
23
|
+
return np.array([img_input]), expected
|
|
23
24
|
|
|
24
25
|
|
|
25
26
|
if __name__ == "__main__":
|
|
26
|
-
|
|
27
|
+
import mlflow.pyfunc as mp
|
|
28
|
+
run_id = "5bfea07ccb91434fafe6fc1def3c2011"
|
|
27
29
|
|
|
28
30
|
logged_model = f'runs:/{run_id}/model'
|
|
29
31
|
|
|
30
32
|
# Load model as a PyFuncModel.
|
|
31
|
-
loaded_model = mlflow.pyfunc.load_model(logged_model)
|
|
33
|
+
loaded_model: mp.PyFuncModel = mlflow.pyfunc.load_model(logged_model)
|
|
32
34
|
|
|
33
|
-
model_input = get_model_input()
|
|
35
|
+
model_input, expected = get_model_input()
|
|
34
36
|
predicted = loaded_model.predict(model_input)
|
|
35
|
-
print(predicted)
|
|
37
|
+
print(predicted[0], expected)
|
|
@@ -5,13 +5,14 @@ import torch.nn.functional as F
|
|
|
5
5
|
from torch.utils.data import DataLoader
|
|
6
6
|
from torchvision import datasets, transforms
|
|
7
7
|
from torch.optim.lr_scheduler import LambdaLR
|
|
8
|
+
from PIL import Image
|
|
8
9
|
|
|
9
10
|
|
|
10
11
|
from iapytoo.predictions.plotters import ConfusionPlotter
|
|
11
|
-
from iapytoo.train.factories import Model,
|
|
12
|
+
from iapytoo.train.factories import Model, Scheduler, Factory
|
|
12
13
|
from iapytoo.utils.config import ConfigFactory, Config
|
|
13
14
|
from iapytoo.train.training import Training
|
|
14
|
-
from iapytoo.train.mlflow_model import
|
|
15
|
+
from iapytoo.train.mlflow_model import MlflowTransform, IMlfowModelProvider
|
|
15
16
|
|
|
16
17
|
|
|
17
18
|
from mlflow.types.schema import TensorSpec, Schema
|
|
@@ -58,32 +59,47 @@ class MnistScheduler(Scheduler):
|
|
|
58
59
|
self.lr_scheduler = LambdaLR(optimizer, lr_lambda=lr_lambda)
|
|
59
60
|
|
|
60
61
|
|
|
61
|
-
class
|
|
62
|
-
def __init__(self, config: Config) -> None:
|
|
63
|
-
super().__init__(config)
|
|
64
|
-
self.predictions.add_plotter(ConfusionPlotter())
|
|
62
|
+
class MnistTransform(MlflowTransform):
|
|
65
63
|
|
|
66
|
-
|
|
67
|
-
class MnistTransform(Transform):
|
|
68
|
-
|
|
69
|
-
def __init__(self, dataset):
|
|
64
|
+
def __init__(self) -> None:
|
|
70
65
|
super().__init__(
|
|
71
|
-
|
|
66
|
+
transform=transforms.Compose(
|
|
72
67
|
[
|
|
73
68
|
transforms.ToTensor(),
|
|
74
69
|
transforms.Normalize((0.1307,), (0.3081,))
|
|
75
70
|
]
|
|
76
|
-
)
|
|
77
|
-
dataset=dataset
|
|
71
|
+
)
|
|
78
72
|
)
|
|
79
73
|
|
|
80
74
|
# override
|
|
81
|
-
def
|
|
82
|
-
|
|
83
|
-
|
|
75
|
+
def __call__(self, model_input, *args, **kwds):
|
|
76
|
+
|
|
77
|
+
images_pil = []
|
|
78
|
+
for i in range(model_input.shape[0]):
|
|
79
|
+
# Extraire l'image (en supprimant la dimension du canal si nécessaire)
|
|
80
|
+
# Supposons que l'image est en niveaux de gris
|
|
81
|
+
img_array = model_input[i, 0, :, :]
|
|
82
|
+
|
|
83
|
+
# Normaliser l'image si nécessaire (par exemple, si les valeurs sont entre 0 et 1)
|
|
84
|
+
img_array = (img_array * 255).astype(np.uint8)
|
|
85
|
+
|
|
86
|
+
# Créer une image PIL
|
|
87
|
+
# 'L' pour les niveaux de gris
|
|
88
|
+
img_pil = Image.fromarray(img_array, 'L')
|
|
89
|
+
images_pil.append(img_pil)
|
|
90
|
+
|
|
91
|
+
model_input_tensor = torch.stack(
|
|
92
|
+
[self.transform(img) for img in images_pil])
|
|
93
|
+
|
|
94
|
+
return model_input_tensor
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
class MnistMlfowModel(IMlfowModelProvider):
|
|
98
|
+
|
|
99
|
+
def __init__(self):
|
|
100
|
+
X = np.random.rand(1, 1, 28, 28)
|
|
101
|
+
Y = np.zeros(shape=(1,), dtype=np.int64)
|
|
84
102
|
# add batch size
|
|
85
|
-
X = np.expand_dims(X, axis=0)
|
|
86
|
-
Y = np.expand_dims(Y, axis=0)
|
|
87
103
|
x_shape = list(X.shape)
|
|
88
104
|
x_shape[0] = -1
|
|
89
105
|
y_shape = list(Y.shape)
|
|
@@ -97,26 +113,35 @@ class MnistTransform(Transform):
|
|
|
97
113
|
self.signature = ModelSignature(
|
|
98
114
|
inputs=input_schema, outputs=output_schema)
|
|
99
115
|
self.input_example = X
|
|
116
|
+
self.transform: MlflowTransform = MnistTransform()
|
|
100
117
|
|
|
101
118
|
# override
|
|
119
|
+
def get_transform(self) -> MlflowTransform:
|
|
120
|
+
return self.transform
|
|
102
121
|
|
|
103
|
-
|
|
122
|
+
# override
|
|
123
|
+
def get_signature(self) -> ModelSignature:
|
|
124
|
+
return self.signature
|
|
104
125
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
126
|
+
# override
|
|
127
|
+
def get_input_example(self) -> np.array:
|
|
128
|
+
return self.input_example
|
|
108
129
|
|
|
109
|
-
model_input_tensor = torch.stack(
|
|
110
|
-
[img.permute(1, 0, 2) for img in model_input_transformed])
|
|
111
130
|
|
|
112
|
-
|
|
131
|
+
class MnistTraining(Training):
|
|
132
|
+
|
|
133
|
+
def __init__(self, config: Config) -> None:
|
|
134
|
+
super().__init__(config)
|
|
135
|
+
self.predictions.add_plotter(ConfusionPlotter())
|
|
136
|
+
self.mlflow_model_provider: IMlfowModelProvider = MnistMlfowModel()
|
|
113
137
|
|
|
114
138
|
|
|
115
139
|
if __name__ == "__main__":
|
|
116
140
|
from iapytoo.utils.arguments import parse_args
|
|
117
141
|
|
|
118
|
-
|
|
119
|
-
|
|
142
|
+
factory = Factory()
|
|
143
|
+
factory.register_model("mnist", MnistModel)
|
|
144
|
+
factory.register_scheduler("mnist", MnistScheduler)
|
|
120
145
|
|
|
121
146
|
args = parse_args()
|
|
122
147
|
|
|
@@ -129,24 +154,19 @@ if __name__ == "__main__":
|
|
|
129
154
|
|
|
130
155
|
Training.seed(config)
|
|
131
156
|
|
|
132
|
-
signature_dataset = datasets.MNIST(
|
|
133
|
-
config.dataset.path,
|
|
134
|
-
train=False
|
|
135
|
-
)
|
|
136
|
-
transform = MnistTransform(signature_dataset)
|
|
137
|
-
|
|
138
157
|
training = MnistTraining(config)
|
|
158
|
+
mflow_transform: MlflowTransform = training.get_transform()
|
|
139
159
|
train_dataset = datasets.MNIST(
|
|
140
160
|
config.dataset.path,
|
|
141
161
|
train=True,
|
|
142
162
|
download=True,
|
|
143
|
-
transform=transform
|
|
163
|
+
transform=mflow_transform.transform
|
|
144
164
|
)
|
|
145
165
|
|
|
146
|
-
|
|
166
|
+
test_dataset = datasets.MNIST(
|
|
147
167
|
config.dataset.path,
|
|
148
168
|
train=False,
|
|
149
|
-
transform=transform
|
|
169
|
+
transform=mflow_transform.transform
|
|
150
170
|
)
|
|
151
171
|
|
|
152
172
|
train_loader = DataLoader(
|
|
@@ -155,13 +175,12 @@ if __name__ == "__main__":
|
|
|
155
175
|
num_workers=config.dataset.num_workers
|
|
156
176
|
)
|
|
157
177
|
test_loader = DataLoader(
|
|
158
|
-
|
|
178
|
+
test_dataset, batch_size=config.dataset.batch_size,
|
|
159
179
|
num_workers=config.dataset.num_workers
|
|
160
180
|
)
|
|
161
181
|
|
|
162
182
|
training.fit(
|
|
163
183
|
train_loader=train_loader,
|
|
164
184
|
valid_loader=test_loader,
|
|
165
|
-
transform=transform,
|
|
166
185
|
run_id=args.run_id
|
|
167
186
|
)
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import
|
|
2
|
-
from
|
|
3
|
-
|
|
1
|
+
from torchvision import datasets
|
|
2
|
+
from torch.utils.data import DataLoader
|
|
4
3
|
from iapytoo.predictions.plotters import ConfusionPlotter
|
|
5
|
-
from iapytoo.utils.config import Config, ConfigFactory
|
|
4
|
+
from iapytoo.utils.config import Config, ConfigFactory, DatasetConfig
|
|
6
5
|
|
|
7
6
|
from iapytoo.train.inference import MLFlowInference
|
|
7
|
+
from iapytoo.train.mlflow_model import MlflowTransform
|
|
8
8
|
|
|
9
9
|
|
|
10
10
|
class MnistInference(MLFlowInference):
|
|
@@ -22,18 +22,19 @@ if __name__ == "__main__":
|
|
|
22
22
|
|
|
23
23
|
inference = MnistInference(config)
|
|
24
24
|
|
|
25
|
-
transform =
|
|
26
|
-
|
|
27
|
-
|
|
25
|
+
transform: MlflowTransform = inference.get_transform()
|
|
26
|
+
|
|
27
|
+
dataset_config: DatasetConfig = config.dataset
|
|
28
28
|
|
|
29
29
|
test_dataset = datasets.MNIST(
|
|
30
|
-
|
|
30
|
+
dataset_config.path,
|
|
31
31
|
train=False,
|
|
32
|
-
transform=transform
|
|
32
|
+
transform=transform.transform
|
|
33
33
|
)
|
|
34
34
|
|
|
35
|
-
test_loader =
|
|
36
|
-
test_dataset,
|
|
35
|
+
test_loader = DataLoader(
|
|
36
|
+
test_dataset,
|
|
37
|
+
batch_size=dataset_config.batch_size,
|
|
37
38
|
num_workers=2
|
|
38
39
|
)
|
|
39
40
|
|
|
@@ -8,8 +8,8 @@ import torchvision.transforms as transforms
|
|
|
8
8
|
|
|
9
9
|
|
|
10
10
|
from iapytoo.train.wgan import WGAN
|
|
11
|
-
from iapytoo.utils.config import Config
|
|
12
|
-
from iapytoo.train.factories import Model,
|
|
11
|
+
from iapytoo.utils.config import ConfigFactory, Config
|
|
12
|
+
from iapytoo.train.factories import Model, Factory
|
|
13
13
|
from iapytoo.predictions.plotters import Fake2DPlotter
|
|
14
14
|
from iapytoo.train.factories import WeightInitiator
|
|
15
15
|
|
|
@@ -161,7 +161,7 @@ class LatentDataset(Dataset):
|
|
|
161
161
|
|
|
162
162
|
|
|
163
163
|
if __name__ == "__main__":
|
|
164
|
-
config =
|
|
164
|
+
config = ConfigFactory.from_yaml(os.path.join(
|
|
165
165
|
os.path.dirname(__file__), "config_wgan.yml"))
|
|
166
166
|
|
|
167
167
|
transform = transforms.Compose(
|
|
@@ -188,9 +188,9 @@ if __name__ == "__main__":
|
|
|
188
188
|
item = next(iter(valid_loader))
|
|
189
189
|
print(item.shape)
|
|
190
190
|
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
191
|
+
factory = Factory()
|
|
192
|
+
factory.register_model("generator", Generator)
|
|
193
|
+
factory.register_model("critic", Discriminator)
|
|
194
194
|
|
|
195
195
|
wgan = WGAN(config)
|
|
196
196
|
wgan.predictions.add_plotter(Fake2DPlotter())
|
|
@@ -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')
|
|
@@ -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()
|
|
@@ -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)
|
|
@@ -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:
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class Predictor:
|
|
5
|
+
def __call__(self, outputs: torch.Tensor) -> torch.Tensor:
|
|
6
|
+
# By default, do nothing outputs of the model are predictions.
|
|
7
|
+
return outputs
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class MaxPredictor(Predictor):
|
|
11
|
+
def __call__(self, outputs: torch.Tensor) -> torch.Tensor:
|
|
12
|
+
_, preds = torch.max(outputs.data, 1)
|
|
13
|
+
return preds
|