iapytoo 0.0.4.dev3__tar.gz → 0.0.6__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.
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6}/.gitignore +4 -1
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6}/PKG-INFO +3 -2
- iapytoo-0.0.6/dockers/docker-compose.yml +15 -0
- iapytoo-0.0.6/dockers/requirements.txt +6 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6}/dockers/run-python.sh +1 -2
- iapytoo-0.0.6/examples/config_infer.yml +17 -0
- iapytoo-0.0.6/examples/config_mnist.yml +26 -0
- iapytoo-0.0.6/examples/config_wgan.yml +29 -0
- iapytoo-0.0.6/examples/mlflow_infer.py +37 -0
- iapytoo-0.0.6/examples/mnist.py +186 -0
- iapytoo-0.0.6/examples/mnist_infer.py +41 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6}/examples/wgan_mnist.py +26 -29
- iapytoo-0.0.6/examples/zipped_runs/selected_runs.zip +0 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6}/iapytoo/_version.py +9 -4
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6}/iapytoo/dataset/__init__.py +4 -8
- iapytoo-0.0.6/iapytoo/metrics/collection.py +39 -0
- iapytoo-0.0.6/iapytoo/metrics/metric.py +46 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6}/iapytoo/metrics/predefined.py +13 -12
- iapytoo-0.0.6/iapytoo/predictions/__init__.py +69 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6}/iapytoo/predictions/feature.py +1 -2
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6}/iapytoo/predictions/plotters.py +39 -11
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6}/iapytoo/predictions/predictors.py +0 -2
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6}/iapytoo/predictions/types.py +0 -2
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6}/iapytoo/train/checkpoint.py +18 -9
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6}/iapytoo/train/factories.py +188 -112
- iapytoo-0.0.6/iapytoo/train/inference.py +175 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6}/iapytoo/train/logger.py +52 -54
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6}/iapytoo/train/loss.py +4 -5
- iapytoo-0.0.6/iapytoo/train/mlflow_model.py +88 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6}/iapytoo/train/training.py +61 -101
- iapytoo-0.0.6/iapytoo/train/valuator.py +61 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6}/iapytoo/train/wgan.py +104 -46
- iapytoo-0.0.6/iapytoo/utils/__init__.py +0 -0
- iapytoo-0.0.6/iapytoo/utils/arguments.py +30 -0
- iapytoo-0.0.6/iapytoo/utils/cf/config.yml +21 -0
- iapytoo-0.0.6/iapytoo/utils/config.py +344 -0
- iapytoo-0.0.6/iapytoo/utils/model_config.py +74 -0
- iapytoo-0.0.6/iapytoo/utils/singleton.py +15 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6}/iapytoo.egg-info/PKG-INFO +3 -2
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6}/iapytoo.egg-info/SOURCES.txt +19 -3
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6}/iapytoo.egg-info/top_level.txt +2 -0
- iapytoo-0.0.6/mlflow/relocate_mlruns.py +67 -0
- iapytoo-0.0.6/mlflow/zip_selected_runs.py +80 -0
- iapytoo-0.0.6/tests/test_config.py +100 -0
- iapytoo-0.0.6/tests/test_factories.py +126 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6}/tests/test_metrics.py +29 -8
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6}/tests/test_model.py +3 -3
- 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/__init__.py +0 -82
- 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/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}/.github/workflows/python-app.yml +0 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6}/LICENSE +0 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6}/MANIFEST.in +0 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6}/README.md +0 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6}/dockers/Dockerfile +0 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6}/dockers/Readme.md +0 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6}/iapytoo/__init__.py +0 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6}/iapytoo/dataset/scaling.py +0 -0
- {iapytoo-0.0.4.dev3/iapytoo/train → iapytoo-0.0.6/iapytoo/metrics}/__init__.py +0 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6}/iapytoo/metrics/distrib.py +0 -0
- {iapytoo-0.0.4.dev3/iapytoo/utils → iapytoo-0.0.6/iapytoo/train}/__init__.py +0 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6}/iapytoo/train/context.py +0 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6}/iapytoo/utils/cf/config.json +0 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6}/iapytoo/utils/cf/config_test.json +0 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6}/iapytoo/utils/display.py +0 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6}/iapytoo/utils/iterative_mean.py +0 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6}/iapytoo/utils/time_utils.py +0 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6}/iapytoo/utils/timer.py +0 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6}/iapytoo.egg-info/dependency_links.txt +0 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6}/pyproject.toml +0 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6}/requirements.txt +0 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6}/setup.cfg +0 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6}/tests/test_dataset.py +0 -0
- {iapytoo-0.0.4.dev3 → iapytoo-0.0.6}/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
|
|
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
|
+
expected = dataset1[idx][1]
|
|
17
|
+
# cree le numpy array
|
|
18
|
+
img_input = np.array(pic.getdata()).reshape(
|
|
19
|
+
1, pic.size[0], pic.size[1]).astype(np.float64)
|
|
20
|
+
img_input /= 255.
|
|
21
|
+
|
|
22
|
+
# crée un batch avec une image [1, 1, 28, 28]
|
|
23
|
+
return np.array([img_input]), expected
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
if __name__ == "__main__":
|
|
27
|
+
import mlflow.pyfunc as mp
|
|
28
|
+
run_id = "5bfea07ccb91434fafe6fc1def3c2011"
|
|
29
|
+
|
|
30
|
+
logged_model = f'runs:/{run_id}/model'
|
|
31
|
+
|
|
32
|
+
# Load model as a PyFuncModel.
|
|
33
|
+
loaded_model: mp.PyFuncModel = mlflow.pyfunc.load_model(logged_model)
|
|
34
|
+
|
|
35
|
+
model_input, expected = get_model_input()
|
|
36
|
+
predicted = loaded_model.predict(model_input)
|
|
37
|
+
print(predicted[0], expected)
|
|
@@ -0,0 +1,186 @@
|
|
|
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
|
+
from PIL import Image
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
from iapytoo.predictions.plotters import ConfusionPlotter
|
|
12
|
+
from iapytoo.train.factories import Model, Scheduler, Factory
|
|
13
|
+
from iapytoo.utils.config import ConfigFactory, Config
|
|
14
|
+
from iapytoo.train.training import Training
|
|
15
|
+
from iapytoo.train.mlflow_model import MlflowTransform, IMlfowModelProvider
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
from mlflow.types.schema import TensorSpec, Schema
|
|
19
|
+
from mlflow.models import ModelSignature
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class MnistModel(Model):
|
|
23
|
+
def __init__(self, loader, config: Config):
|
|
24
|
+
super(MnistModel, self).__init__(loader, config)
|
|
25
|
+
self.conv1 = nn.Conv2d(1, 32, 3, 1)
|
|
26
|
+
self.conv2 = nn.Conv2d(32, 64, 3, 1)
|
|
27
|
+
self.dropout1 = nn.Dropout(0.25)
|
|
28
|
+
self.dropout2 = nn.Dropout(0.5)
|
|
29
|
+
self.fc1 = nn.Linear(9216, 128)
|
|
30
|
+
self.fc2 = nn.Linear(128, 10)
|
|
31
|
+
|
|
32
|
+
def forward(self, x):
|
|
33
|
+
x = self.conv1(x)
|
|
34
|
+
x = F.relu(x)
|
|
35
|
+
x = self.conv2(x)
|
|
36
|
+
x = F.relu(x)
|
|
37
|
+
x = F.max_pool2d(x, 2)
|
|
38
|
+
x = self.dropout1(x)
|
|
39
|
+
x = torch.flatten(x, 1)
|
|
40
|
+
x = self.fc1(x)
|
|
41
|
+
x = F.relu(x)
|
|
42
|
+
x = self.dropout2(x)
|
|
43
|
+
x = self.fc2(x)
|
|
44
|
+
output = F.log_softmax(x, dim=1)
|
|
45
|
+
return output
|
|
46
|
+
|
|
47
|
+
def predict(self, model_output):
|
|
48
|
+
return torch.argmax(model_output, dim=1)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
class MnistScheduler(Scheduler):
|
|
52
|
+
def __init__(self, optimizer, config: Config) -> None:
|
|
53
|
+
super().__init__(optimizer, config)
|
|
54
|
+
|
|
55
|
+
def lr_lambda(epoch):
|
|
56
|
+
# LR to be 0.1 * (1/1+0.01*epoch)
|
|
57
|
+
return 0.995**epoch
|
|
58
|
+
|
|
59
|
+
self.lr_scheduler = LambdaLR(optimizer, lr_lambda=lr_lambda)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
class MnistTransform(MlflowTransform):
|
|
63
|
+
|
|
64
|
+
def __init__(self) -> None:
|
|
65
|
+
super().__init__(
|
|
66
|
+
transform=transforms.Compose(
|
|
67
|
+
[
|
|
68
|
+
transforms.ToTensor(),
|
|
69
|
+
transforms.Normalize((0.1307,), (0.3081,))
|
|
70
|
+
]
|
|
71
|
+
)
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
# override
|
|
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)
|
|
102
|
+
# add batch size
|
|
103
|
+
x_shape = list(X.shape)
|
|
104
|
+
x_shape[0] = -1
|
|
105
|
+
y_shape = list(Y.shape)
|
|
106
|
+
y_shape[0] = -1
|
|
107
|
+
|
|
108
|
+
input_schema = Schema(
|
|
109
|
+
[TensorSpec(type=X.dtype, shape=x_shape)])
|
|
110
|
+
output_schema = Schema(
|
|
111
|
+
[TensorSpec(type=Y.dtype, shape=y_shape)])
|
|
112
|
+
|
|
113
|
+
self.signature = ModelSignature(
|
|
114
|
+
inputs=input_schema, outputs=output_schema)
|
|
115
|
+
self.input_example = X
|
|
116
|
+
self.transform: MlflowTransform = MnistTransform()
|
|
117
|
+
|
|
118
|
+
# override
|
|
119
|
+
def get_transform(self) -> MlflowTransform:
|
|
120
|
+
return self.transform
|
|
121
|
+
|
|
122
|
+
# override
|
|
123
|
+
def get_signature(self) -> ModelSignature:
|
|
124
|
+
return self.signature
|
|
125
|
+
|
|
126
|
+
# override
|
|
127
|
+
def get_input_example(self) -> np.array:
|
|
128
|
+
return self.input_example
|
|
129
|
+
|
|
130
|
+
|
|
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()
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
if __name__ == "__main__":
|
|
140
|
+
from iapytoo.utils.arguments import parse_args
|
|
141
|
+
|
|
142
|
+
factory = Factory()
|
|
143
|
+
factory.register_model("mnist", MnistModel)
|
|
144
|
+
factory.register_scheduler("mnist", MnistScheduler)
|
|
145
|
+
|
|
146
|
+
args = parse_args()
|
|
147
|
+
|
|
148
|
+
if args.run_id is not None:
|
|
149
|
+
config = ConfigFactory.from_run_id(args.run_id, args.tracking_uri)
|
|
150
|
+
config.training.epochs = args.epochs
|
|
151
|
+
else:
|
|
152
|
+
# INPUT Parameters
|
|
153
|
+
config = ConfigFactory.from_yaml(args.yaml)
|
|
154
|
+
|
|
155
|
+
Training.seed(config)
|
|
156
|
+
|
|
157
|
+
training = MnistTraining(config)
|
|
158
|
+
mflow_transform: MlflowTransform = training.get_transform()
|
|
159
|
+
train_dataset = datasets.MNIST(
|
|
160
|
+
config.dataset.path,
|
|
161
|
+
train=True,
|
|
162
|
+
download=True,
|
|
163
|
+
transform=mflow_transform.transform
|
|
164
|
+
)
|
|
165
|
+
|
|
166
|
+
test_dataset = datasets.MNIST(
|
|
167
|
+
config.dataset.path,
|
|
168
|
+
train=False,
|
|
169
|
+
transform=mflow_transform.transform
|
|
170
|
+
)
|
|
171
|
+
|
|
172
|
+
train_loader = DataLoader(
|
|
173
|
+
train_dataset,
|
|
174
|
+
batch_size=config.dataset.batch_size,
|
|
175
|
+
num_workers=config.dataset.num_workers
|
|
176
|
+
)
|
|
177
|
+
test_loader = DataLoader(
|
|
178
|
+
test_dataset, batch_size=config.dataset.batch_size,
|
|
179
|
+
num_workers=config.dataset.num_workers
|
|
180
|
+
)
|
|
181
|
+
|
|
182
|
+
training.fit(
|
|
183
|
+
train_loader=train_loader,
|
|
184
|
+
valid_loader=test_loader,
|
|
185
|
+
run_id=args.run_id
|
|
186
|
+
)
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
from torchvision import datasets
|
|
2
|
+
from torch.utils.data import DataLoader
|
|
3
|
+
from iapytoo.predictions.plotters import ConfusionPlotter
|
|
4
|
+
from iapytoo.utils.config import Config, ConfigFactory, DatasetConfig
|
|
5
|
+
|
|
6
|
+
from iapytoo.train.inference import MLFlowInference
|
|
7
|
+
from iapytoo.train.mlflow_model import MlflowTransform
|
|
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: MlflowTransform = inference.get_transform()
|
|
26
|
+
|
|
27
|
+
dataset_config: DatasetConfig = config.dataset
|
|
28
|
+
|
|
29
|
+
test_dataset = datasets.MNIST(
|
|
30
|
+
dataset_config.path,
|
|
31
|
+
train=False,
|
|
32
|
+
transform=transform.transform
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
test_loader = DataLoader(
|
|
36
|
+
test_dataset,
|
|
37
|
+
batch_size=dataset_config.batch_size,
|
|
38
|
+
num_workers=2
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
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 ConfigFactory, Config
|
|
12
|
+
from iapytoo.train.factories import Model, Factory
|
|
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 =
|
|
164
|
+
config = ConfigFactory.from_yaml(os.path.join(
|
|
165
|
+
os.path.dirname(__file__), "config_wgan.yml"))
|
|
171
166
|
|
|
172
167
|
transform = transforms.Compose(
|
|
173
168
|
[
|
|
@@ -178,22 +173,24 @@ 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)
|
|
193
190
|
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
191
|
+
factory = Factory()
|
|
192
|
+
factory.register_model("generator", Generator)
|
|
193
|
+
factory.register_model("critic", Discriminator)
|
|
197
194
|
|
|
198
195
|
wgan = WGAN(config)
|
|
199
196
|
wgan.predictions.add_plotter(Fake2DPlotter())
|
|
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'
|
|
21
|
+
__version_tuple__ = version_tuple = (0, 0, 6)
|
|
@@ -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):
|
|
@@ -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()
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
|
|
3
|
+
from iapytoo.utils.config import Config
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class Metric:
|
|
7
|
+
def __init__(self, name, config: Config, with_target=True):
|
|
8
|
+
from iapytoo.train.factories import Factory
|
|
9
|
+
self.with_target = with_target
|
|
10
|
+
self.name = name
|
|
11
|
+
self.config = config
|
|
12
|
+
self.predictor = Factory().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)
|
|
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
|