iapytoo 0.0.4.dev3__tar.gz → 0.0.6.dev0__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.4.dev3 → iapytoo-0.0.6.dev0}/.gitignore +2 -1
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6.dev0}/PKG-INFO +3 -2
- iapytoo-0.0.6.dev0/dockers/docker-compose.yml +15 -0
- iapytoo-0.0.6.dev0/dockers/requirements.txt +6 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6.dev0}/dockers/run-python.sh +1 -2
- iapytoo-0.0.6.dev0/examples/config_infer.yml +17 -0
- iapytoo-0.0.6.dev0/examples/config_mnist.yml +26 -0
- iapytoo-0.0.6.dev0/examples/config_wgan.yml +28 -0
- iapytoo-0.0.6.dev0/examples/mlflow_infer.py +35 -0
- iapytoo-0.0.6.dev0/examples/mnist.py +167 -0
- iapytoo-0.0.6.dev0/examples/mnist_infer.py +40 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6.dev0}/examples/wgan_mnist.py +23 -26
- iapytoo-0.0.6.dev0/examples/zipped_runs/selected_runs.zip +0 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6.dev0}/iapytoo/_version.py +9 -4
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6.dev0}/iapytoo/dataset/__init__.py +4 -8
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6.dev0}/iapytoo/metrics/__init__.py +7 -7
- iapytoo-0.0.6.dev0/iapytoo/metrics/metric.py +46 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6.dev0}/iapytoo/metrics/predefined.py +13 -12
- iapytoo-0.0.6.dev0/iapytoo/predictions/__init__.py +53 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6.dev0}/iapytoo/predictions/feature.py +1 -2
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6.dev0}/iapytoo/predictions/plotters.py +39 -11
- iapytoo-0.0.6.dev0/iapytoo/predictions/predictors.py +33 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6.dev0}/iapytoo/predictions/types.py +0 -2
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6.dev0}/iapytoo/train/checkpoint.py +18 -9
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6.dev0}/iapytoo/train/factories.py +32 -26
- iapytoo-0.0.6.dev0/iapytoo/train/inference.py +93 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6.dev0}/iapytoo/train/logger.py +65 -54
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6.dev0}/iapytoo/train/loss.py +4 -5
- iapytoo-0.0.6.dev0/iapytoo/train/mlflow_model.py +80 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6.dev0}/iapytoo/train/training.py +72 -91
- iapytoo-0.0.6.dev0/iapytoo/train/valuator.py +43 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6.dev0}/iapytoo/train/wgan.py +114 -39
- iapytoo-0.0.6.dev0/iapytoo/utils/arguments.py +30 -0
- iapytoo-0.0.6.dev0/iapytoo/utils/cf/config.yml +21 -0
- iapytoo-0.0.6.dev0/iapytoo/utils/config.py +344 -0
- iapytoo-0.0.6.dev0/iapytoo/utils/model_config.py +72 -0
- iapytoo-0.0.6.dev0/iapytoo/utils/singleton.py +15 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6.dev0}/iapytoo.egg-info/PKG-INFO +3 -2
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6.dev0}/iapytoo.egg-info/SOURCES.txt +17 -3
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6.dev0}/iapytoo.egg-info/top_level.txt +2 -0
- iapytoo-0.0.6.dev0/mlflow/relocate_mlruns.py +67 -0
- iapytoo-0.0.6.dev0/mlflow/zip_selected_runs.py +80 -0
- iapytoo-0.0.6.dev0/tests/test_config.py +100 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6.dev0}/tests/test_metrics.py +28 -7
- iapytoo-0.0.4.dev3/examples/config_mnist.json +0 -19
- iapytoo-0.0.4.dev3/examples/config_wgan.json +0 -25
- iapytoo-0.0.4.dev3/examples/mnist.py +0 -106
- iapytoo-0.0.4.dev3/iapytoo/metrics/metric.py +0 -39
- iapytoo-0.0.4.dev3/iapytoo/predictions/__init__.py +0 -78
- iapytoo-0.0.4.dev3/iapytoo/predictions/predictors.py +0 -15
- iapytoo-0.0.4.dev3/iapytoo/utils/arguments.py +0 -45
- iapytoo-0.0.4.dev3/iapytoo/utils/config.py +0 -123
- iapytoo-0.0.4.dev3/iapytoo/utils/meta_singleton.py +0 -17
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6.dev0}/.github/workflows/python-app.yml +0 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6.dev0}/LICENSE +0 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6.dev0}/MANIFEST.in +0 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6.dev0}/README.md +0 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6.dev0}/dockers/Dockerfile +0 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6.dev0}/dockers/Readme.md +0 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6.dev0}/iapytoo/__init__.py +0 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6.dev0}/iapytoo/dataset/scaling.py +0 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6.dev0}/iapytoo/metrics/distrib.py +0 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6.dev0}/iapytoo/train/__init__.py +0 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6.dev0}/iapytoo/train/context.py +0 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6.dev0}/iapytoo/utils/__init__.py +0 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6.dev0}/iapytoo/utils/cf/config.json +0 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6.dev0}/iapytoo/utils/cf/config_test.json +0 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6.dev0}/iapytoo/utils/display.py +0 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6.dev0}/iapytoo/utils/iterative_mean.py +0 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6.dev0}/iapytoo/utils/time_utils.py +0 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6.dev0}/iapytoo/utils/timer.py +0 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6.dev0}/iapytoo.egg-info/dependency_links.txt +0 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6.dev0}/pyproject.toml +0 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6.dev0}/requirements.txt +0 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6.dev0}/setup.cfg +0 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6.dev0}/tests/test_dataset.py +0 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6.dev0}/tests/test_model.py +0 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6.dev0}/tests/test_scaling.py +0 -0
|
@@ -1,12 +1,13 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
2
|
Name: iapytoo
|
|
3
|
-
Version: 0.0.
|
|
3
|
+
Version: 0.0.6.dev0
|
|
4
4
|
Summary: Un toolkit pour vite coder un modèle pytorch d'IA utilisant MLflow
|
|
5
5
|
Author-email: Jean-François Lecomte <zwergon@free.fr>
|
|
6
6
|
License: Apache-2.0
|
|
7
7
|
Requires-Python: >=3.7
|
|
8
8
|
Description-Content-Type: text/markdown
|
|
9
9
|
License-File: LICENSE
|
|
10
|
+
Dynamic: license-file
|
|
10
11
|
|
|
11
12
|
[](https://opensource.org/licenses/Apache-2.0)
|
|
12
13
|
[:
|
|
8
|
+
|
|
9
|
+
dataset1 = datasets.MNIST(
|
|
10
|
+
"../data",
|
|
11
|
+
train=True,
|
|
12
|
+
download=False
|
|
13
|
+
)
|
|
14
|
+
# prend une image du dataset MNIST de train
|
|
15
|
+
pic: Image = dataset1[idx][0]
|
|
16
|
+
# cree le numpy array
|
|
17
|
+
img_input = np.array(pic.getdata()).reshape(
|
|
18
|
+
1, pic.size[0], pic.size[1]).astype(np.float32)
|
|
19
|
+
img_input /= 255.
|
|
20
|
+
|
|
21
|
+
# crée un batch avec une image [1, 1, 28, 28]
|
|
22
|
+
return np.array([img_input])
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
if __name__ == "__main__":
|
|
26
|
+
run_id = "a821dbc9a0974bb0815479634c0e845c"
|
|
27
|
+
|
|
28
|
+
logged_model = f'runs:/{run_id}/model'
|
|
29
|
+
|
|
30
|
+
# Load model as a PyFuncModel.
|
|
31
|
+
loaded_model = mlflow.pyfunc.load_model(logged_model)
|
|
32
|
+
|
|
33
|
+
model_input = get_model_input()
|
|
34
|
+
predicted = loaded_model.predict(model_input)
|
|
35
|
+
print(predicted)
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
import torch
|
|
3
|
+
import torch.nn as nn
|
|
4
|
+
import torch.nn.functional as F
|
|
5
|
+
from torch.utils.data import DataLoader
|
|
6
|
+
from torchvision import datasets, transforms
|
|
7
|
+
from torch.optim.lr_scheduler import LambdaLR
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
from iapytoo.predictions.plotters import ConfusionPlotter
|
|
11
|
+
from iapytoo.train.factories import Model, ModelFactory, SchedulerFactory, Scheduler
|
|
12
|
+
from iapytoo.utils.config import ConfigFactory, Config
|
|
13
|
+
from iapytoo.train.training import Training
|
|
14
|
+
from iapytoo.train.mlflow_model import Transform
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
from mlflow.types.schema import TensorSpec, Schema
|
|
18
|
+
from mlflow.models import ModelSignature
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class MnistModel(Model):
|
|
22
|
+
def __init__(self, loader, config: Config):
|
|
23
|
+
super(MnistModel, self).__init__(loader, config)
|
|
24
|
+
self.conv1 = nn.Conv2d(1, 32, 3, 1)
|
|
25
|
+
self.conv2 = nn.Conv2d(32, 64, 3, 1)
|
|
26
|
+
self.dropout1 = nn.Dropout(0.25)
|
|
27
|
+
self.dropout2 = nn.Dropout(0.5)
|
|
28
|
+
self.fc1 = nn.Linear(9216, 128)
|
|
29
|
+
self.fc2 = nn.Linear(128, 10)
|
|
30
|
+
|
|
31
|
+
def forward(self, x):
|
|
32
|
+
x = self.conv1(x)
|
|
33
|
+
x = F.relu(x)
|
|
34
|
+
x = self.conv2(x)
|
|
35
|
+
x = F.relu(x)
|
|
36
|
+
x = F.max_pool2d(x, 2)
|
|
37
|
+
x = self.dropout1(x)
|
|
38
|
+
x = torch.flatten(x, 1)
|
|
39
|
+
x = self.fc1(x)
|
|
40
|
+
x = F.relu(x)
|
|
41
|
+
x = self.dropout2(x)
|
|
42
|
+
x = self.fc2(x)
|
|
43
|
+
output = F.log_softmax(x, dim=1)
|
|
44
|
+
return output
|
|
45
|
+
|
|
46
|
+
def predict(self, model_output):
|
|
47
|
+
return torch.argmax(model_output, dim=1)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
class MnistScheduler(Scheduler):
|
|
51
|
+
def __init__(self, optimizer, config: Config) -> None:
|
|
52
|
+
super().__init__(optimizer, config)
|
|
53
|
+
|
|
54
|
+
def lr_lambda(epoch):
|
|
55
|
+
# LR to be 0.1 * (1/1+0.01*epoch)
|
|
56
|
+
return 0.995**epoch
|
|
57
|
+
|
|
58
|
+
self.lr_scheduler = LambdaLR(optimizer, lr_lambda=lr_lambda)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
class MnistTraining(Training):
|
|
62
|
+
def __init__(self, config: Config) -> None:
|
|
63
|
+
super().__init__(config)
|
|
64
|
+
self.predictions.add_plotter(ConfusionPlotter())
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
class MnistTransform(Transform):
|
|
68
|
+
|
|
69
|
+
def __init__(self, dataset):
|
|
70
|
+
super().__init__(
|
|
71
|
+
train_transform=transforms.Compose(
|
|
72
|
+
[
|
|
73
|
+
transforms.ToTensor(),
|
|
74
|
+
transforms.Normalize((0.1307,), (0.3081,))
|
|
75
|
+
]
|
|
76
|
+
),
|
|
77
|
+
dataset=dataset
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
# override
|
|
81
|
+
def set_signature(self, dataset):
|
|
82
|
+
img, Y = dataset[0]
|
|
83
|
+
X = Transform.img_to_numpy(img)
|
|
84
|
+
# add batch size
|
|
85
|
+
X = np.expand_dims(X, axis=0)
|
|
86
|
+
Y = np.expand_dims(Y, axis=0)
|
|
87
|
+
x_shape = list(X.shape)
|
|
88
|
+
x_shape[0] = -1
|
|
89
|
+
y_shape = list(Y.shape)
|
|
90
|
+
y_shape[0] = -1
|
|
91
|
+
|
|
92
|
+
input_schema = Schema(
|
|
93
|
+
[TensorSpec(type=X.dtype, shape=x_shape)])
|
|
94
|
+
output_schema = Schema(
|
|
95
|
+
[TensorSpec(type=Y.dtype, shape=y_shape)])
|
|
96
|
+
|
|
97
|
+
self.signature = ModelSignature(
|
|
98
|
+
inputs=input_schema, outputs=output_schema)
|
|
99
|
+
self.input_example = X
|
|
100
|
+
|
|
101
|
+
# override
|
|
102
|
+
|
|
103
|
+
def transform(self, context, model_input, params=None):
|
|
104
|
+
|
|
105
|
+
model_input_transformed = [
|
|
106
|
+
self.infer_transform(img) for img in model_input]
|
|
107
|
+
print(model_input_transformed[0].permute(1, 0, 2).shape)
|
|
108
|
+
|
|
109
|
+
model_input_tensor = torch.stack(
|
|
110
|
+
[img.permute(1, 0, 2) for img in model_input_transformed])
|
|
111
|
+
|
|
112
|
+
return model_input_tensor
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
if __name__ == "__main__":
|
|
116
|
+
from iapytoo.utils.arguments import parse_args
|
|
117
|
+
|
|
118
|
+
ModelFactory().register_model("mnist", MnistModel)
|
|
119
|
+
SchedulerFactory().register_scheduler("mnist", MnistScheduler)
|
|
120
|
+
|
|
121
|
+
args = parse_args()
|
|
122
|
+
|
|
123
|
+
if args.run_id is not None:
|
|
124
|
+
config = ConfigFactory.from_run_id(args.run_id, args.tracking_uri)
|
|
125
|
+
config.training.epochs = args.epochs
|
|
126
|
+
else:
|
|
127
|
+
# INPUT Parameters
|
|
128
|
+
config = ConfigFactory.from_yaml(args.yaml)
|
|
129
|
+
|
|
130
|
+
Training.seed(config)
|
|
131
|
+
|
|
132
|
+
signature_dataset = datasets.MNIST(
|
|
133
|
+
config.dataset.path,
|
|
134
|
+
train=False
|
|
135
|
+
)
|
|
136
|
+
transform = MnistTransform(signature_dataset)
|
|
137
|
+
|
|
138
|
+
training = MnistTraining(config)
|
|
139
|
+
train_dataset = datasets.MNIST(
|
|
140
|
+
config.dataset.path,
|
|
141
|
+
train=True,
|
|
142
|
+
download=True,
|
|
143
|
+
transform=transform.train_transform
|
|
144
|
+
)
|
|
145
|
+
|
|
146
|
+
train_dataset = datasets.MNIST(
|
|
147
|
+
config.dataset.path,
|
|
148
|
+
train=False,
|
|
149
|
+
transform=transform.infer_transform
|
|
150
|
+
)
|
|
151
|
+
|
|
152
|
+
train_loader = DataLoader(
|
|
153
|
+
train_dataset,
|
|
154
|
+
batch_size=config.dataset.batch_size,
|
|
155
|
+
num_workers=config.dataset.num_workers
|
|
156
|
+
)
|
|
157
|
+
test_loader = DataLoader(
|
|
158
|
+
train_dataset, batch_size=config.dataset.batch_size,
|
|
159
|
+
num_workers=config.dataset.num_workers
|
|
160
|
+
)
|
|
161
|
+
|
|
162
|
+
training.fit(
|
|
163
|
+
train_loader=train_loader,
|
|
164
|
+
valid_loader=test_loader,
|
|
165
|
+
transform=transform,
|
|
166
|
+
run_id=args.run_id
|
|
167
|
+
)
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
from torchvision import datasets, transforms
|
|
3
|
+
|
|
4
|
+
from iapytoo.predictions.plotters import ConfusionPlotter
|
|
5
|
+
from iapytoo.utils.config import Config, ConfigFactory
|
|
6
|
+
|
|
7
|
+
from iapytoo.train.inference import MLFlowInference
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class MnistInference(MLFlowInference):
|
|
11
|
+
def __init__(self, config: Config) -> None:
|
|
12
|
+
super().__init__(config)
|
|
13
|
+
self.predictions.add_plotter(ConfusionPlotter())
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
if __name__ == "__main__":
|
|
17
|
+
import os
|
|
18
|
+
|
|
19
|
+
# INPUT Parameters
|
|
20
|
+
config = ConfigFactory.from_yaml(os.path.join(
|
|
21
|
+
os.path.dirname(__file__), "config_infer.yml"))
|
|
22
|
+
|
|
23
|
+
inference = MnistInference(config)
|
|
24
|
+
|
|
25
|
+
transform = transforms.Compose(
|
|
26
|
+
[transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,))]
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
test_dataset = datasets.MNIST(
|
|
30
|
+
config.dataset.path,
|
|
31
|
+
train=False,
|
|
32
|
+
transform=transform
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
test_loader = torch.utils.data.DataLoader(
|
|
36
|
+
test_dataset, batch_size=config.dataset.batch_size,
|
|
37
|
+
num_workers=2
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
inference.predict(loader=test_loader)
|
|
@@ -1,25 +1,16 @@
|
|
|
1
1
|
import os
|
|
2
|
-
import torch
|
|
3
|
-
import torch.nn as nn
|
|
4
|
-
import torch.optim as optim
|
|
5
|
-
import numpy as np
|
|
6
|
-
from torch.autograd import grad
|
|
7
|
-
|
|
8
|
-
from iapytoo.train.wgan import WGAN
|
|
9
|
-
from iapytoo.utils.config import Config
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
from iapytoo.train.factories import Model, ModelFactory, OptimizerFactory
|
|
13
|
-
from iapytoo.predictions.plotters import PredictionPlotter, Fake2DPlotter
|
|
14
2
|
|
|
15
3
|
import torch
|
|
16
4
|
import torch.nn as nn
|
|
17
5
|
from torch.utils.data import Dataset
|
|
18
|
-
import torchvision
|
|
19
|
-
import numpy as np
|
|
20
6
|
from torchvision.datasets import MNIST
|
|
21
7
|
import torchvision.transforms as transforms
|
|
22
|
-
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
from iapytoo.train.wgan import WGAN
|
|
11
|
+
from iapytoo.utils.config import Config
|
|
12
|
+
from iapytoo.train.factories import Model, ModelFactory
|
|
13
|
+
from iapytoo.predictions.plotters import Fake2DPlotter
|
|
23
14
|
from iapytoo.train.factories import WeightInitiator
|
|
24
15
|
|
|
25
16
|
|
|
@@ -47,9 +38,9 @@ class Generator(Model):
|
|
|
47
38
|
"""
|
|
48
39
|
return torch.randn(n_samples, z_dim, device=device)
|
|
49
40
|
|
|
50
|
-
def __init__(self, loader, config):
|
|
41
|
+
def __init__(self, loader, config: Config):
|
|
51
42
|
super(Generator, self).__init__(loader, config)
|
|
52
|
-
self.z_dim = config
|
|
43
|
+
self.z_dim = config.model.noise_dim
|
|
53
44
|
# Filters [1024, 512, 256]
|
|
54
45
|
# Input_dim = 100
|
|
55
46
|
# Output_dim = 1 (number of channels)
|
|
@@ -107,15 +98,18 @@ class Generator(Model):
|
|
|
107
98
|
|
|
108
99
|
|
|
109
100
|
class Discriminator(Model):
|
|
110
|
-
def __init__(self, loader, config):
|
|
101
|
+
def __init__(self, loader, config: Config):
|
|
111
102
|
super(Discriminator, self).__init__(loader, config)
|
|
112
103
|
# Filters [256, 512, 1024]
|
|
113
104
|
# Input_dim = 1 (Cx64x64)
|
|
114
105
|
# Output_dim = 1
|
|
115
106
|
self.main_module = nn.Sequential(
|
|
116
|
-
# Omitting batch normalization in critic because our new penalized training objective
|
|
117
|
-
#
|
|
118
|
-
#
|
|
107
|
+
# Omitting batch normalization in critic because our new penalized training objective
|
|
108
|
+
# (WGAN with gradient penalty) is no longer valid
|
|
109
|
+
# in this setting, since we penalize the norm of the critic's gradient with respect
|
|
110
|
+
# to each input independently and not the enitre batch.
|
|
111
|
+
# There is not good & fast implementation of layer normalization -->
|
|
112
|
+
# using per instance normalization nn.InstanceNorm2d()
|
|
119
113
|
# Image (Cx32x32)
|
|
120
114
|
nn.Conv2d(
|
|
121
115
|
in_channels=1, out_channels=256, kernel_size=4, stride=2, padding=1
|
|
@@ -167,7 +161,8 @@ class LatentDataset(Dataset):
|
|
|
167
161
|
|
|
168
162
|
|
|
169
163
|
if __name__ == "__main__":
|
|
170
|
-
config = Config(os.path.join(
|
|
164
|
+
config = Config.create_from_yaml(os.path.join(
|
|
165
|
+
os.path.dirname(__file__), "config_wgan.yml"))
|
|
171
166
|
|
|
172
167
|
transform = transforms.Compose(
|
|
173
168
|
[
|
|
@@ -178,15 +173,17 @@ if __name__ == "__main__":
|
|
|
178
173
|
)
|
|
179
174
|
|
|
180
175
|
# load training data
|
|
181
|
-
trainset = MNIST(config.
|
|
176
|
+
trainset = MNIST(config.dataset.path, download=True,
|
|
177
|
+
train=True, transform=transform)
|
|
182
178
|
|
|
183
179
|
trainloader = torch.utils.data.DataLoader(
|
|
184
|
-
trainset, batch_size=config.batch_size, shuffle=True, drop_last=True
|
|
180
|
+
trainset, batch_size=config.dataset.batch_size, shuffle=True, drop_last=True
|
|
185
181
|
)
|
|
186
182
|
|
|
187
|
-
latentset = LatentDataset(config.noise_dim, size=16)
|
|
183
|
+
latentset = LatentDataset(config.model.noise_dim, size=16)
|
|
188
184
|
|
|
189
|
-
valid_loader = torch.utils.data.DataLoader(
|
|
185
|
+
valid_loader = torch.utils.data.DataLoader(
|
|
186
|
+
latentset, batch_size=1, shuffle=False)
|
|
190
187
|
|
|
191
188
|
item = next(iter(valid_loader))
|
|
192
189
|
print(item.shape)
|
|
Binary file
|
|
@@ -1,8 +1,13 @@
|
|
|
1
|
-
# file generated by
|
|
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
|
|
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.
|
|
16
|
-
__version_tuple__ = version_tuple = (0, 0,
|
|
20
|
+
__version__ = version = '0.0.6.dev0'
|
|
21
|
+
__version_tuple__ = version_tuple = (0, 0, 6, 'dev0')
|
|
@@ -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,13 +1,12 @@
|
|
|
1
|
-
import torch
|
|
2
|
-
|
|
3
|
-
from iapytoo.utils.meta_singleton import MetaSingleton
|
|
4
1
|
|
|
2
|
+
from iapytoo.utils.config import Config
|
|
3
|
+
from iapytoo.utils.singleton import singleton
|
|
5
4
|
from .predefined import R2Metric, RMSMetric, MSMetric, AccuracyMetric
|
|
6
5
|
from .metric import Metric
|
|
7
6
|
|
|
8
7
|
|
|
9
8
|
class MetricsCollection(Metric):
|
|
10
|
-
def __init__(self, tag: str, metric_names: list, config):
|
|
9
|
+
def __init__(self, tag: str, metric_names: list[str], config: Config):
|
|
11
10
|
super().__init__(tag, config)
|
|
12
11
|
self.metrics = {}
|
|
13
12
|
factory = MetricFactory()
|
|
@@ -33,7 +32,7 @@ class MetricsCollection(Metric):
|
|
|
33
32
|
results = m.compute()
|
|
34
33
|
for name, result in results.items():
|
|
35
34
|
# prepend the name of the collection to all inner results : validation_accuracy for example
|
|
36
|
-
self.results[f
|
|
35
|
+
self.results[f"{self.name}_{name}"] = result
|
|
37
36
|
return self.results
|
|
38
37
|
|
|
39
38
|
# overwrite
|
|
@@ -47,7 +46,8 @@ class MetricError(Exception):
|
|
|
47
46
|
super().__init__(*args)
|
|
48
47
|
|
|
49
48
|
|
|
50
|
-
|
|
49
|
+
@singleton
|
|
50
|
+
class MetricFactory:
|
|
51
51
|
def __init__(self) -> None:
|
|
52
52
|
self.metrics_dict = {
|
|
53
53
|
"r2": R2Metric,
|
|
@@ -59,7 +59,7 @@ class MetricFactory(metaclass=MetaSingleton):
|
|
|
59
59
|
def register_metric(self, key, metric_cls):
|
|
60
60
|
self.metrics_dict[key] = metric_cls
|
|
61
61
|
|
|
62
|
-
def create_metric(self, kind: str, config:
|
|
62
|
+
def create_metric(self, kind: str, config: Config, device="cpu") -> Metric:
|
|
63
63
|
"""Creates an architecture of NN
|
|
64
64
|
|
|
65
65
|
Args:
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
|
|
3
|
+
from iapytoo.utils.config import Config
|
|
4
|
+
from iapytoo.predictions.predictors import PredictorFactory
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class Metric:
|
|
8
|
+
def __init__(self, name, config: Config, with_target=True):
|
|
9
|
+
self.with_target = with_target
|
|
10
|
+
self.name = name
|
|
11
|
+
self.config = config
|
|
12
|
+
self.predictor = PredictorFactory().create_predictor(config)
|
|
13
|
+
|
|
14
|
+
# First dimension is for the concatenation.
|
|
15
|
+
self.outputs = torch.zeros(size=(0,))
|
|
16
|
+
if self.with_target:
|
|
17
|
+
self.target = torch.zeros(size=(0,))
|
|
18
|
+
self.results = {}
|
|
19
|
+
|
|
20
|
+
@property
|
|
21
|
+
def device(self):
|
|
22
|
+
return self.outputs.device
|
|
23
|
+
|
|
24
|
+
@property
|
|
25
|
+
def predicted(self):
|
|
26
|
+
return self.predictor(self.outputs) if self.predictor else self.outputs
|
|
27
|
+
|
|
28
|
+
def to(self, device):
|
|
29
|
+
self.outputs = self.outputs.to(device)
|
|
30
|
+
if self.with_target:
|
|
31
|
+
self.target = self.target.to(device)
|
|
32
|
+
return self
|
|
33
|
+
|
|
34
|
+
def update(self, outputs, Y):
|
|
35
|
+
self.outputs = torch.cat((self.outputs, outputs), dim=0)
|
|
36
|
+
if self.with_target:
|
|
37
|
+
self.target = torch.cat((self.target, Y), dim=0)
|
|
38
|
+
|
|
39
|
+
def reset(self):
|
|
40
|
+
device = self.device
|
|
41
|
+
self.outputs = torch.zeros(size=(0,), device=device)
|
|
42
|
+
if self.with_target:
|
|
43
|
+
self.target = torch.zeros(size=(0,), device=self.device)
|
|
44
|
+
|
|
45
|
+
def compute(self):
|
|
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
|
|
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.
|
|
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
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
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
|
|