iapytoo 0.0.2__tar.gz → 0.0.4.dev3__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/.github/workflows/python-app.yml +78 -0
- iapytoo-0.0.4.dev3/.gitignore +168 -0
- {iapytoo-0.0.2/iapytoo.egg-info → iapytoo-0.0.4.dev3}/PKG-INFO +1 -1
- iapytoo-0.0.4.dev3/dockers/Dockerfile +13 -0
- iapytoo-0.0.4.dev3/dockers/Readme.md +7 -0
- iapytoo-0.0.4.dev3/dockers/run-python.sh +9 -0
- iapytoo-0.0.4.dev3/examples/config_mnist.json +19 -0
- iapytoo-0.0.4.dev3/examples/config_wgan.json +25 -0
- iapytoo-0.0.4.dev3/examples/mnist.py +106 -0
- iapytoo-0.0.4.dev3/examples/wgan_mnist.py +200 -0
- iapytoo-0.0.4.dev3/iapytoo/_version.py +16 -0
- iapytoo-0.0.4.dev3/iapytoo/utils/cf/config.json +17 -0
- iapytoo-0.0.4.dev3/iapytoo/utils/cf/config_test.json +17 -0
- {iapytoo-0.0.2 → iapytoo-0.0.4.dev3/iapytoo.egg-info}/PKG-INFO +1 -1
- {iapytoo-0.0.2 → iapytoo-0.0.4.dev3}/iapytoo.egg-info/SOURCES.txt +13 -0
- iapytoo-0.0.4.dev3/iapytoo.egg-info/top_level.txt +3 -0
- {iapytoo-0.0.2 → iapytoo-0.0.4.dev3}/pyproject.toml +8 -12
- iapytoo-0.0.4.dev3/requirements.txt +6 -0
- iapytoo-0.0.2/iapytoo.egg-info/top_level.txt +0 -1
- {iapytoo-0.0.2 → iapytoo-0.0.4.dev3}/LICENSE +0 -0
- {iapytoo-0.0.2 → iapytoo-0.0.4.dev3}/MANIFEST.in +0 -0
- {iapytoo-0.0.2 → iapytoo-0.0.4.dev3}/README.md +0 -0
- {iapytoo-0.0.2 → iapytoo-0.0.4.dev3}/iapytoo/__init__.py +0 -0
- {iapytoo-0.0.2 → iapytoo-0.0.4.dev3}/iapytoo/dataset/__init__.py +0 -0
- {iapytoo-0.0.2 → iapytoo-0.0.4.dev3}/iapytoo/dataset/scaling.py +0 -0
- {iapytoo-0.0.2 → iapytoo-0.0.4.dev3}/iapytoo/metrics/__init__.py +0 -0
- {iapytoo-0.0.2 → iapytoo-0.0.4.dev3}/iapytoo/metrics/distrib.py +0 -0
- {iapytoo-0.0.2 → iapytoo-0.0.4.dev3}/iapytoo/metrics/metric.py +0 -0
- {iapytoo-0.0.2 → iapytoo-0.0.4.dev3}/iapytoo/metrics/predefined.py +0 -0
- {iapytoo-0.0.2 → iapytoo-0.0.4.dev3}/iapytoo/predictions/__init__.py +0 -0
- {iapytoo-0.0.2 → iapytoo-0.0.4.dev3}/iapytoo/predictions/feature.py +0 -0
- {iapytoo-0.0.2 → iapytoo-0.0.4.dev3}/iapytoo/predictions/plotters.py +0 -0
- {iapytoo-0.0.2 → iapytoo-0.0.4.dev3}/iapytoo/predictions/predictors.py +0 -0
- {iapytoo-0.0.2 → iapytoo-0.0.4.dev3}/iapytoo/predictions/types.py +0 -0
- {iapytoo-0.0.2 → iapytoo-0.0.4.dev3}/iapytoo/train/__init__.py +0 -0
- {iapytoo-0.0.2 → iapytoo-0.0.4.dev3}/iapytoo/train/checkpoint.py +0 -0
- {iapytoo-0.0.2 → iapytoo-0.0.4.dev3}/iapytoo/train/context.py +0 -0
- {iapytoo-0.0.2 → iapytoo-0.0.4.dev3}/iapytoo/train/factories.py +0 -0
- {iapytoo-0.0.2 → iapytoo-0.0.4.dev3}/iapytoo/train/logger.py +0 -0
- {iapytoo-0.0.2 → iapytoo-0.0.4.dev3}/iapytoo/train/loss.py +0 -0
- {iapytoo-0.0.2 → iapytoo-0.0.4.dev3}/iapytoo/train/training.py +0 -0
- {iapytoo-0.0.2 → iapytoo-0.0.4.dev3}/iapytoo/train/wgan.py +0 -0
- {iapytoo-0.0.2 → iapytoo-0.0.4.dev3}/iapytoo/utils/__init__.py +0 -0
- {iapytoo-0.0.2 → iapytoo-0.0.4.dev3}/iapytoo/utils/arguments.py +0 -0
- {iapytoo-0.0.2 → iapytoo-0.0.4.dev3}/iapytoo/utils/config.py +0 -0
- {iapytoo-0.0.2 → iapytoo-0.0.4.dev3}/iapytoo/utils/display.py +0 -0
- {iapytoo-0.0.2 → iapytoo-0.0.4.dev3}/iapytoo/utils/iterative_mean.py +0 -0
- {iapytoo-0.0.2 → iapytoo-0.0.4.dev3}/iapytoo/utils/meta_singleton.py +0 -0
- {iapytoo-0.0.2 → iapytoo-0.0.4.dev3}/iapytoo/utils/time_utils.py +0 -0
- {iapytoo-0.0.2 → iapytoo-0.0.4.dev3}/iapytoo/utils/timer.py +0 -0
- {iapytoo-0.0.2 → iapytoo-0.0.4.dev3}/iapytoo.egg-info/dependency_links.txt +0 -0
- {iapytoo-0.0.2 → iapytoo-0.0.4.dev3}/setup.cfg +0 -0
- {iapytoo-0.0.2 → iapytoo-0.0.4.dev3}/tests/test_dataset.py +0 -0
- {iapytoo-0.0.2 → iapytoo-0.0.4.dev3}/tests/test_metrics.py +0 -0
- {iapytoo-0.0.2 → iapytoo-0.0.4.dev3}/tests/test_model.py +0 -0
- {iapytoo-0.0.2 → iapytoo-0.0.4.dev3}/tests/test_scaling.py +0 -0
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
# This workflow will install Python dependencies,
|
|
2
|
+
# run tests and lint with a single version of Python
|
|
3
|
+
# It will also publish on release branch or if tag v* is pushed
|
|
4
|
+
|
|
5
|
+
name: Python application
|
|
6
|
+
|
|
7
|
+
on:
|
|
8
|
+
push:
|
|
9
|
+
branches:
|
|
10
|
+
- main
|
|
11
|
+
- 'release/v*'
|
|
12
|
+
tags:
|
|
13
|
+
- 'v*'
|
|
14
|
+
pull_request:
|
|
15
|
+
branches: [ "main" ]
|
|
16
|
+
|
|
17
|
+
permissions:
|
|
18
|
+
contents: read
|
|
19
|
+
|
|
20
|
+
jobs:
|
|
21
|
+
build:
|
|
22
|
+
|
|
23
|
+
runs-on: ubuntu-latest
|
|
24
|
+
|
|
25
|
+
steps:
|
|
26
|
+
- uses: actions/checkout@v3
|
|
27
|
+
- name: Set up Python 3.10
|
|
28
|
+
uses: actions/setup-python@v3
|
|
29
|
+
with:
|
|
30
|
+
python-version: "3.10"
|
|
31
|
+
- name: Install dependencies
|
|
32
|
+
run: |
|
|
33
|
+
python -m pip install --upgrade pip
|
|
34
|
+
pip install flake8 pytest
|
|
35
|
+
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
|
|
36
|
+
- name: Lint with flake8
|
|
37
|
+
run: |
|
|
38
|
+
# stop the build if there are Python syntax errors or undefined names
|
|
39
|
+
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
|
|
40
|
+
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
|
|
41
|
+
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
|
|
42
|
+
- name: set pythonpath
|
|
43
|
+
run: |
|
|
44
|
+
echo "----PYTHONPATH------"
|
|
45
|
+
PWD=$(pwd)
|
|
46
|
+
export PYTHONPATH=$PWD:$PYTHONPATH
|
|
47
|
+
echo "PYTHONPATH=$PYTHONPATH" >> $GITHUB_ENV
|
|
48
|
+
- name: Test with pytest
|
|
49
|
+
run: |
|
|
50
|
+
pytest
|
|
51
|
+
|
|
52
|
+
publish:
|
|
53
|
+
needs: build
|
|
54
|
+
if: startsWith(github.ref, 'refs/heads/release/v') || startsWith(github.ref, 'refs/tags/v')
|
|
55
|
+
runs-on: ubuntu-latest
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
steps:
|
|
59
|
+
- uses: actions/checkout@v3
|
|
60
|
+
with:
|
|
61
|
+
fetch-depth: 0 # Fetch all history for all tags and branches
|
|
62
|
+
- name: Set up Python
|
|
63
|
+
uses: actions/setup-python@v3
|
|
64
|
+
with:
|
|
65
|
+
python-version: '3.x'
|
|
66
|
+
- name: Install dependencies
|
|
67
|
+
run: |
|
|
68
|
+
python -m pip install --upgrade pip
|
|
69
|
+
pip install build setuptools setuptools-scm twine
|
|
70
|
+
|
|
71
|
+
- name: Build the package
|
|
72
|
+
run: python -m build
|
|
73
|
+
|
|
74
|
+
- name: Publish to PyPI
|
|
75
|
+
env:
|
|
76
|
+
TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }}
|
|
77
|
+
TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
|
|
78
|
+
run: twine upload dist/*
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*/__pycache__/
|
|
4
|
+
*.py[cod]
|
|
5
|
+
*$py.class
|
|
6
|
+
|
|
7
|
+
# C extensions
|
|
8
|
+
*.so
|
|
9
|
+
|
|
10
|
+
# Distribution / packaging
|
|
11
|
+
.Python
|
|
12
|
+
build/
|
|
13
|
+
develop-eggs/
|
|
14
|
+
dist/
|
|
15
|
+
downloads/
|
|
16
|
+
eggs/
|
|
17
|
+
.eggs/
|
|
18
|
+
lib/
|
|
19
|
+
lib64/
|
|
20
|
+
parts/
|
|
21
|
+
sdist/
|
|
22
|
+
var/
|
|
23
|
+
wheels/
|
|
24
|
+
share/python-wheels/
|
|
25
|
+
*.egg-info/
|
|
26
|
+
.installed.cfg
|
|
27
|
+
*.egg
|
|
28
|
+
MANIFEST
|
|
29
|
+
|
|
30
|
+
# PyInstaller
|
|
31
|
+
# Usually these files are written by a python script from a template
|
|
32
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
33
|
+
*.manifest
|
|
34
|
+
*.spec
|
|
35
|
+
|
|
36
|
+
# Installer logs
|
|
37
|
+
pip-log.txt
|
|
38
|
+
pip-delete-this-directory.txt
|
|
39
|
+
|
|
40
|
+
# Unit test / coverage reports
|
|
41
|
+
htmlcov/
|
|
42
|
+
.tox/
|
|
43
|
+
.nox/
|
|
44
|
+
.coverage
|
|
45
|
+
.coverage.*
|
|
46
|
+
.cache
|
|
47
|
+
nosetests.xml
|
|
48
|
+
coverage.xml
|
|
49
|
+
*.cover
|
|
50
|
+
*.py,cover
|
|
51
|
+
.hypothesis/
|
|
52
|
+
.pytest_cache/
|
|
53
|
+
cover/
|
|
54
|
+
|
|
55
|
+
# Translations
|
|
56
|
+
*.mo
|
|
57
|
+
*.pot
|
|
58
|
+
|
|
59
|
+
# Django stuff:
|
|
60
|
+
*.log
|
|
61
|
+
local_settings.py
|
|
62
|
+
db.sqlite3
|
|
63
|
+
db.sqlite3-journal
|
|
64
|
+
|
|
65
|
+
# Flask stuff:
|
|
66
|
+
instance/
|
|
67
|
+
.webassets-cache
|
|
68
|
+
|
|
69
|
+
# Scrapy stuff:
|
|
70
|
+
.scrapy
|
|
71
|
+
|
|
72
|
+
# Sphinx documentation
|
|
73
|
+
docs/_build/
|
|
74
|
+
|
|
75
|
+
# PyBuilder
|
|
76
|
+
.pybuilder/
|
|
77
|
+
target/
|
|
78
|
+
|
|
79
|
+
# Jupyter Notebook
|
|
80
|
+
.ipynb_checkpoints
|
|
81
|
+
|
|
82
|
+
# IPython
|
|
83
|
+
profile_default/
|
|
84
|
+
ipython_config.py
|
|
85
|
+
|
|
86
|
+
# pyenv
|
|
87
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
88
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
89
|
+
# .python-version
|
|
90
|
+
|
|
91
|
+
# pipenv
|
|
92
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
93
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
94
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
95
|
+
# install all needed dependencies.
|
|
96
|
+
#Pipfile.lock
|
|
97
|
+
|
|
98
|
+
# poetry
|
|
99
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
100
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
101
|
+
# commonly ignored for libraries.
|
|
102
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
103
|
+
#poetry.lock
|
|
104
|
+
|
|
105
|
+
# pdm
|
|
106
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
107
|
+
#pdm.lock
|
|
108
|
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
|
109
|
+
# in version control.
|
|
110
|
+
# https://pdm.fming.dev/#use-with-ide
|
|
111
|
+
.pdm.toml
|
|
112
|
+
|
|
113
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
114
|
+
__pypackages__/
|
|
115
|
+
|
|
116
|
+
# Celery stuff
|
|
117
|
+
celerybeat-schedule
|
|
118
|
+
celerybeat.pid
|
|
119
|
+
|
|
120
|
+
# SageMath parsed files
|
|
121
|
+
*.sage.py
|
|
122
|
+
|
|
123
|
+
# Environments
|
|
124
|
+
.env
|
|
125
|
+
.venv
|
|
126
|
+
env/
|
|
127
|
+
venv/
|
|
128
|
+
ENV/
|
|
129
|
+
env.bak/
|
|
130
|
+
venv.bak/
|
|
131
|
+
|
|
132
|
+
# Spyder project settings
|
|
133
|
+
.spyderproject
|
|
134
|
+
.spyproject
|
|
135
|
+
|
|
136
|
+
# Rope project settings
|
|
137
|
+
.ropeproject
|
|
138
|
+
|
|
139
|
+
# mkdocs documentation
|
|
140
|
+
/site
|
|
141
|
+
|
|
142
|
+
# mypy
|
|
143
|
+
.mypy_cache/
|
|
144
|
+
.dmypy.json
|
|
145
|
+
dmypy.json
|
|
146
|
+
|
|
147
|
+
# Pyre type checker
|
|
148
|
+
.pyre/
|
|
149
|
+
|
|
150
|
+
# pytype static type analyzer
|
|
151
|
+
.pytype/
|
|
152
|
+
|
|
153
|
+
# Cython debug symbols
|
|
154
|
+
cython_debug/
|
|
155
|
+
|
|
156
|
+
# PyCharm
|
|
157
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
158
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
159
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
160
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
161
|
+
.idea/
|
|
162
|
+
|
|
163
|
+
# VS Code
|
|
164
|
+
.vscode
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
mlruns/
|
|
168
|
+
data/
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
FROM nvidia/cuda:12.2.0-runtime-ubuntu22.04
|
|
2
|
+
|
|
3
|
+
RUN apt-get update && apt-get install -y \
|
|
4
|
+
curl \
|
|
5
|
+
git \
|
|
6
|
+
python3 \
|
|
7
|
+
python3-pip
|
|
8
|
+
|
|
9
|
+
RUN pip install torch==2.4.1 torchvision==0.19.1 torchaudio==2.4.1 --index-url https://download.pytorch.org/whl/cu121
|
|
10
|
+
|
|
11
|
+
# Install pip dependencies
|
|
12
|
+
COPY requirements.txt .
|
|
13
|
+
RUN pip install --no-cache-dir -r requirements.txt
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"project": "mnist",
|
|
3
|
+
"run": "mnist",
|
|
4
|
+
"tracking_uri": null,
|
|
5
|
+
"tqdm": true,
|
|
6
|
+
"normalization": true,
|
|
7
|
+
"n_steps_by_batch": 10,
|
|
8
|
+
"ratio_train_test": 0.9,
|
|
9
|
+
"num_workers": 2,
|
|
10
|
+
"loss": "nll",
|
|
11
|
+
"cuda": true,
|
|
12
|
+
"model": "mnist",
|
|
13
|
+
"optimizer": "adam",
|
|
14
|
+
"scheduler": "step",
|
|
15
|
+
"gamma": 0.7,
|
|
16
|
+
"seed": 1,
|
|
17
|
+
"batch_size": 64,
|
|
18
|
+
"epochs": 11
|
|
19
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"project": "wgan",
|
|
3
|
+
"run": "wgan",
|
|
4
|
+
"data_dir": "./data",
|
|
5
|
+
"tracking_uri": null,
|
|
6
|
+
"tqdm": true,
|
|
7
|
+
"normalization": true,
|
|
8
|
+
"n_steps_by_batch": 10,
|
|
9
|
+
"ratio_train_test": 0.9,
|
|
10
|
+
"num_workers": 2,
|
|
11
|
+
"cuda": true,
|
|
12
|
+
"generator": "generator",
|
|
13
|
+
"discriminator": "critic",
|
|
14
|
+
"n_critic": 1,
|
|
15
|
+
"optimizer": "adam",
|
|
16
|
+
"scheduler": "step",
|
|
17
|
+
"gamma": 0.7,
|
|
18
|
+
"betas": [0.5, 0.99],
|
|
19
|
+
"lambda_gp": 10,
|
|
20
|
+
"noise_dim": 100,
|
|
21
|
+
"seed": 1,
|
|
22
|
+
"batch_size": 256,
|
|
23
|
+
"epochs": 11,
|
|
24
|
+
"learning_rate": 0.0002
|
|
25
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
import torch.nn as nn
|
|
3
|
+
import torch.nn.functional as F
|
|
4
|
+
from torchvision import datasets, transforms
|
|
5
|
+
from torch.optim.lr_scheduler import LambdaLR
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
from iapytoo.dataset.scaling import Scaling
|
|
9
|
+
from iapytoo.predictions.predictors import MaxPredictor
|
|
10
|
+
from iapytoo.predictions.plotters import ConfusionPlotter
|
|
11
|
+
from iapytoo.train.factories import Model, ModelFactory, SchedulerFactory, Scheduler
|
|
12
|
+
from iapytoo.utils.config import Config
|
|
13
|
+
from iapytoo.train.training import Training
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
import matplotlib.pyplot as plt
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class MnistModel(Model):
|
|
20
|
+
def __init__(self, loader, config):
|
|
21
|
+
super(MnistModel, self).__init__(loader, config)
|
|
22
|
+
self.conv1 = nn.Conv2d(1, 32, 3, 1)
|
|
23
|
+
self.conv2 = nn.Conv2d(32, 64, 3, 1)
|
|
24
|
+
self.dropout1 = nn.Dropout(0.25)
|
|
25
|
+
self.dropout2 = nn.Dropout(0.5)
|
|
26
|
+
self.fc1 = nn.Linear(9216, 128)
|
|
27
|
+
self.fc2 = nn.Linear(128, 10)
|
|
28
|
+
|
|
29
|
+
def forward(self, x):
|
|
30
|
+
x = self.conv1(x)
|
|
31
|
+
x = F.relu(x)
|
|
32
|
+
x = self.conv2(x)
|
|
33
|
+
x = F.relu(x)
|
|
34
|
+
x = F.max_pool2d(x, 2)
|
|
35
|
+
x = self.dropout1(x)
|
|
36
|
+
x = torch.flatten(x, 1)
|
|
37
|
+
x = self.fc1(x)
|
|
38
|
+
x = F.relu(x)
|
|
39
|
+
x = self.dropout2(x)
|
|
40
|
+
x = self.fc2(x)
|
|
41
|
+
output = F.log_softmax(x, dim=1)
|
|
42
|
+
return output
|
|
43
|
+
|
|
44
|
+
def predict(self, model_output):
|
|
45
|
+
return torch.argmax(model_output, dim=1)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
class MnistScheduler(Scheduler):
|
|
49
|
+
def __init__(self, optimizer, config) -> None:
|
|
50
|
+
super().__init__(optimizer, config)
|
|
51
|
+
|
|
52
|
+
def lr_lambda(epoch):
|
|
53
|
+
# LR to be 0.1 * (1/1+0.01*epoch)
|
|
54
|
+
return 0.995**epoch
|
|
55
|
+
|
|
56
|
+
self.lr_scheduler = LambdaLR(optimizer, lr_lambda=lr_lambda)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
class MnistTraining(Training):
|
|
60
|
+
def __init__(self, config: Config) -> None:
|
|
61
|
+
super().__init__(config, predictor=MaxPredictor(), metric_names=[])
|
|
62
|
+
self.predictions.add_plotter(ConfusionPlotter())
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
if __name__ == "__main__":
|
|
66
|
+
import os
|
|
67
|
+
|
|
68
|
+
from iapytoo.utils.arguments import parse_args
|
|
69
|
+
|
|
70
|
+
ModelFactory().register_model("mnist", MnistModel)
|
|
71
|
+
SchedulerFactory().register_scheduler("mnist", MnistScheduler)
|
|
72
|
+
|
|
73
|
+
args = parse_args()
|
|
74
|
+
|
|
75
|
+
if args.run_id is not None:
|
|
76
|
+
config = Config.create_from_run_id(args.run_id, args.tracking_uri)
|
|
77
|
+
config.epochs = args.epochs
|
|
78
|
+
else:
|
|
79
|
+
# INPUT Parameters
|
|
80
|
+
config = Config.create_from_args(args)
|
|
81
|
+
|
|
82
|
+
Training.seed(config)
|
|
83
|
+
|
|
84
|
+
config = Config.create_from_args(args)
|
|
85
|
+
|
|
86
|
+
training = MnistTraining(config)
|
|
87
|
+
|
|
88
|
+
transform = transforms.Compose(
|
|
89
|
+
[transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,))]
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
dataset1 = datasets.MNIST(
|
|
93
|
+
args.dataset, train=True, download=True, transform=transform
|
|
94
|
+
)
|
|
95
|
+
dataset2 = datasets.MNIST(args.dataset, train=False, transform=transform)
|
|
96
|
+
|
|
97
|
+
train_loader = torch.utils.data.DataLoader(
|
|
98
|
+
dataset1, batch_size=config.batch_size, num_workers=config.num_workers
|
|
99
|
+
)
|
|
100
|
+
test_loader = torch.utils.data.DataLoader(
|
|
101
|
+
dataset2, batch_size=config.batch_size, num_workers=config.num_workers
|
|
102
|
+
)
|
|
103
|
+
|
|
104
|
+
training.fit(
|
|
105
|
+
train_loader=train_loader, valid_loader=test_loader, run_id=args.run_id
|
|
106
|
+
)
|
|
@@ -0,0 +1,200 @@
|
|
|
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
|
+
|
|
15
|
+
import torch
|
|
16
|
+
import torch.nn as nn
|
|
17
|
+
from torch.utils.data import Dataset
|
|
18
|
+
import torchvision
|
|
19
|
+
import numpy as np
|
|
20
|
+
from torchvision.datasets import MNIST
|
|
21
|
+
import torchvision.transforms as transforms
|
|
22
|
+
import matplotlib.pyplot as plt
|
|
23
|
+
from iapytoo.train.factories import WeightInitiator
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class MNISTInitiator(WeightInitiator):
|
|
27
|
+
def __call__(self, m):
|
|
28
|
+
if isinstance(m, nn.Conv2d) or isinstance(m, nn.ConvTranspose2d):
|
|
29
|
+
torch.nn.init.normal_(m.weight, 0.0, 0.02)
|
|
30
|
+
if isinstance(m, nn.BatchNorm2d):
|
|
31
|
+
torch.nn.init.normal_(m.weight, 0.0, 0.02)
|
|
32
|
+
torch.nn.init.constant_(m.bias, 0)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class Generator(Model):
|
|
36
|
+
"""See https://github.com/Zeleni9/pytorch-wgan"""
|
|
37
|
+
|
|
38
|
+
@staticmethod
|
|
39
|
+
def get_noise(n_samples, z_dim, device="cpu"):
|
|
40
|
+
"""
|
|
41
|
+
Function for creating noise vectors: Given the dimensions (n_samples, z_dim)
|
|
42
|
+
creates a tensor of that shape filled with random numbers from the normal distribution.
|
|
43
|
+
Parameters:
|
|
44
|
+
n_samples: the number of samples to generate, a scalar
|
|
45
|
+
z_dim: the dimension of the noise vector, a scalar
|
|
46
|
+
device: the device type
|
|
47
|
+
"""
|
|
48
|
+
return torch.randn(n_samples, z_dim, device=device)
|
|
49
|
+
|
|
50
|
+
def __init__(self, loader, config):
|
|
51
|
+
super(Generator, self).__init__(loader, config)
|
|
52
|
+
self.z_dim = config["noise_dim"]
|
|
53
|
+
# Filters [1024, 512, 256]
|
|
54
|
+
# Input_dim = 100
|
|
55
|
+
# Output_dim = 1 (number of channels)
|
|
56
|
+
self.main_module = nn.Sequential(
|
|
57
|
+
# Z latent vector 100
|
|
58
|
+
nn.ConvTranspose2d(
|
|
59
|
+
in_channels=self.z_dim,
|
|
60
|
+
out_channels=1024,
|
|
61
|
+
kernel_size=4,
|
|
62
|
+
stride=1,
|
|
63
|
+
padding=0,
|
|
64
|
+
),
|
|
65
|
+
nn.BatchNorm2d(num_features=1024),
|
|
66
|
+
nn.ReLU(True),
|
|
67
|
+
# State (1024x4x4)
|
|
68
|
+
nn.ConvTranspose2d(
|
|
69
|
+
in_channels=1024, out_channels=512, kernel_size=4, stride=2, padding=1
|
|
70
|
+
),
|
|
71
|
+
nn.BatchNorm2d(num_features=512),
|
|
72
|
+
nn.ReLU(True),
|
|
73
|
+
# State (512x8x8)
|
|
74
|
+
nn.ConvTranspose2d(
|
|
75
|
+
in_channels=512, out_channels=256, kernel_size=4, stride=2, padding=1
|
|
76
|
+
),
|
|
77
|
+
nn.BatchNorm2d(num_features=256),
|
|
78
|
+
nn.ReLU(True),
|
|
79
|
+
# State (256x16x16)
|
|
80
|
+
nn.ConvTranspose2d(
|
|
81
|
+
in_channels=256, out_channels=1, kernel_size=4, stride=2, padding=1
|
|
82
|
+
),
|
|
83
|
+
)
|
|
84
|
+
# output of main module --> Image (Cx32x32)
|
|
85
|
+
|
|
86
|
+
self.output = nn.Tanh()
|
|
87
|
+
|
|
88
|
+
def unsqueeze_noise(self, noise):
|
|
89
|
+
"""
|
|
90
|
+
Function for completing a forward pass of the generator: Given a noise tensor,
|
|
91
|
+
returns a copy of that noise with width and height = 1 and channels = z_dim.
|
|
92
|
+
Parameters:
|
|
93
|
+
noise: a noise tensor with dimensions (n_samples, z_dim)
|
|
94
|
+
"""
|
|
95
|
+
return noise.view(noise.shape[0], self.z_dim, 1, 1)
|
|
96
|
+
|
|
97
|
+
def forward(self, noise):
|
|
98
|
+
"""
|
|
99
|
+
Function for completing a forward pass of the generator: Given a noise tensor,
|
|
100
|
+
returns generated images.
|
|
101
|
+
Parameters:
|
|
102
|
+
noise: a noise tensor with dimensions (n_samples, z_dim)
|
|
103
|
+
"""
|
|
104
|
+
x = self.unsqueeze_noise(noise)
|
|
105
|
+
x = self.main_module(x)
|
|
106
|
+
return self.output(x)
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
class Discriminator(Model):
|
|
110
|
+
def __init__(self, loader, config):
|
|
111
|
+
super(Discriminator, self).__init__(loader, config)
|
|
112
|
+
# Filters [256, 512, 1024]
|
|
113
|
+
# Input_dim = 1 (Cx64x64)
|
|
114
|
+
# Output_dim = 1
|
|
115
|
+
self.main_module = nn.Sequential(
|
|
116
|
+
# Omitting batch normalization in critic because our new penalized training objective (WGAN with gradient penalty) is no longer valid
|
|
117
|
+
# in this setting, since we penalize the norm of the critic's gradient with respect to each input independently and not the enitre batch.
|
|
118
|
+
# There is not good & fast implementation of layer normalization --> using per instance normalization nn.InstanceNorm2d()
|
|
119
|
+
# Image (Cx32x32)
|
|
120
|
+
nn.Conv2d(
|
|
121
|
+
in_channels=1, out_channels=256, kernel_size=4, stride=2, padding=1
|
|
122
|
+
),
|
|
123
|
+
nn.InstanceNorm2d(256, affine=True),
|
|
124
|
+
nn.LeakyReLU(0.2, inplace=True),
|
|
125
|
+
# State (256x16x16)
|
|
126
|
+
nn.Conv2d(
|
|
127
|
+
in_channels=256, out_channels=512, kernel_size=4, stride=2, padding=1
|
|
128
|
+
),
|
|
129
|
+
nn.InstanceNorm2d(512, affine=True),
|
|
130
|
+
nn.LeakyReLU(0.2, inplace=True),
|
|
131
|
+
# State (512x8x8)
|
|
132
|
+
nn.Conv2d(
|
|
133
|
+
in_channels=512, out_channels=1024, kernel_size=4, stride=2, padding=1
|
|
134
|
+
),
|
|
135
|
+
nn.InstanceNorm2d(1024, affine=True),
|
|
136
|
+
nn.LeakyReLU(0.2, inplace=True),
|
|
137
|
+
)
|
|
138
|
+
# output of main module --> State (1024x4x4)
|
|
139
|
+
|
|
140
|
+
self.output = nn.Sequential(
|
|
141
|
+
# The output of D is no longer a probability, we do not apply sigmoid at the output of D.
|
|
142
|
+
nn.Conv2d(
|
|
143
|
+
in_channels=1024, out_channels=1, kernel_size=4, stride=1, padding=0
|
|
144
|
+
)
|
|
145
|
+
)
|
|
146
|
+
|
|
147
|
+
def forward(self, x):
|
|
148
|
+
x = self.main_module(x)
|
|
149
|
+
return self.output(x)
|
|
150
|
+
|
|
151
|
+
def feature_extraction(self, x):
|
|
152
|
+
# Use discriminator for feature extraction then flatten to vector of 16384
|
|
153
|
+
x = self.main_module(x)
|
|
154
|
+
return x.view(-1, 1024 * 4 * 4)
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
class LatentDataset(Dataset):
|
|
158
|
+
def __init__(self, noise_dim, size) -> None:
|
|
159
|
+
super().__init__()
|
|
160
|
+
self.noise = torch.randn(size, noise_dim)
|
|
161
|
+
|
|
162
|
+
def __len__(self):
|
|
163
|
+
return self.noise.shape[0]
|
|
164
|
+
|
|
165
|
+
def __getitem__(self, idx):
|
|
166
|
+
return self.noise[idx, :]
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
if __name__ == "__main__":
|
|
170
|
+
config = Config(os.path.join(os.path.dirname(__file__), "config_wgan.json"))
|
|
171
|
+
|
|
172
|
+
transform = transforms.Compose(
|
|
173
|
+
[
|
|
174
|
+
transforms.Resize(32),
|
|
175
|
+
transforms.ToTensor(),
|
|
176
|
+
transforms.Normalize((0.5,), (0.5,)),
|
|
177
|
+
]
|
|
178
|
+
)
|
|
179
|
+
|
|
180
|
+
# load training data
|
|
181
|
+
trainset = MNIST(config.data_dir, download=True, train=True, transform=transform)
|
|
182
|
+
|
|
183
|
+
trainloader = torch.utils.data.DataLoader(
|
|
184
|
+
trainset, batch_size=config.batch_size, shuffle=True, drop_last=True
|
|
185
|
+
)
|
|
186
|
+
|
|
187
|
+
latentset = LatentDataset(config.noise_dim, size=16)
|
|
188
|
+
|
|
189
|
+
valid_loader = torch.utils.data.DataLoader(latentset, batch_size=1, shuffle=False)
|
|
190
|
+
|
|
191
|
+
item = next(iter(valid_loader))
|
|
192
|
+
print(item.shape)
|
|
193
|
+
|
|
194
|
+
model_factory = ModelFactory()
|
|
195
|
+
model_factory.register_model("generator", Generator)
|
|
196
|
+
model_factory.register_model("critic", Discriminator)
|
|
197
|
+
|
|
198
|
+
wgan = WGAN(config)
|
|
199
|
+
wgan.predictions.add_plotter(Fake2DPlotter())
|
|
200
|
+
wgan.fit(train_loader=trainloader, valid_loader=valid_loader, run_id=None)
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# file generated by setuptools_scm
|
|
2
|
+
# don't change, don't track in version control
|
|
3
|
+
TYPE_CHECKING = False
|
|
4
|
+
if TYPE_CHECKING:
|
|
5
|
+
from typing import Tuple, Union
|
|
6
|
+
VERSION_TUPLE = Tuple[Union[int, str], ...]
|
|
7
|
+
else:
|
|
8
|
+
VERSION_TUPLE = object
|
|
9
|
+
|
|
10
|
+
version: str
|
|
11
|
+
__version__: str
|
|
12
|
+
__version_tuple__: VERSION_TUPLE
|
|
13
|
+
version_tuple: VERSION_TUPLE
|
|
14
|
+
|
|
15
|
+
__version__ = version = '0.0.4.dev3'
|
|
16
|
+
__version_tuple__ = version_tuple = (0, 0, 4, 'dev3')
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"project": "iapytoo",
|
|
3
|
+
"run": "run",
|
|
4
|
+
"tracking_uri": null,
|
|
5
|
+
"tqdm": true,
|
|
6
|
+
"normalization": true,
|
|
7
|
+
"n_steps_by_batch": 10,
|
|
8
|
+
"ratio_train_test": 0.9,
|
|
9
|
+
"num_workers": 2,
|
|
10
|
+
"model": "CNN",
|
|
11
|
+
"kernel_size": 3,
|
|
12
|
+
"dropout": 0,
|
|
13
|
+
"loss": "mse",
|
|
14
|
+
"optimizer": "adam",
|
|
15
|
+
"cuda": true,
|
|
16
|
+
"seed":true
|
|
17
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"project": "iapytoo_test",
|
|
3
|
+
"run": "run",
|
|
4
|
+
"tracking_uri": null,
|
|
5
|
+
"tqdm": true,
|
|
6
|
+
"normalization": true,
|
|
7
|
+
"n_steps_by_batch": 10,
|
|
8
|
+
"ratio_train_test": 0.9,
|
|
9
|
+
"num_workers": 2,
|
|
10
|
+
"model": "CNN",
|
|
11
|
+
"kernel_size": 3,
|
|
12
|
+
"dropout": 0,
|
|
13
|
+
"loss": "mse",
|
|
14
|
+
"optimizer": "adam",
|
|
15
|
+
"cuda": true,
|
|
16
|
+
"seed":true
|
|
17
|
+
}
|
|
@@ -1,8 +1,19 @@
|
|
|
1
|
+
.gitignore
|
|
1
2
|
LICENSE
|
|
2
3
|
MANIFEST.in
|
|
3
4
|
README.md
|
|
4
5
|
pyproject.toml
|
|
6
|
+
requirements.txt
|
|
7
|
+
.github/workflows/python-app.yml
|
|
8
|
+
dockers/Dockerfile
|
|
9
|
+
dockers/Readme.md
|
|
10
|
+
dockers/run-python.sh
|
|
11
|
+
examples/config_mnist.json
|
|
12
|
+
examples/config_wgan.json
|
|
13
|
+
examples/mnist.py
|
|
14
|
+
examples/wgan_mnist.py
|
|
5
15
|
iapytoo/__init__.py
|
|
16
|
+
iapytoo/_version.py
|
|
6
17
|
iapytoo.egg-info/PKG-INFO
|
|
7
18
|
iapytoo.egg-info/SOURCES.txt
|
|
8
19
|
iapytoo.egg-info/dependency_links.txt
|
|
@@ -34,6 +45,8 @@ iapytoo/utils/iterative_mean.py
|
|
|
34
45
|
iapytoo/utils/meta_singleton.py
|
|
35
46
|
iapytoo/utils/time_utils.py
|
|
36
47
|
iapytoo/utils/timer.py
|
|
48
|
+
iapytoo/utils/cf/config.json
|
|
49
|
+
iapytoo/utils/cf/config_test.json
|
|
37
50
|
tests/test_dataset.py
|
|
38
51
|
tests/test_metrics.py
|
|
39
52
|
tests/test_model.py
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
[build-system]
|
|
2
|
-
requires = ["setuptools>=61.0"]
|
|
2
|
+
requires = ["setuptools>=61.0", "setuptools_scm>=8"]
|
|
3
3
|
build-backend = "setuptools.build_meta"
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "iapytoo"
|
|
7
|
-
|
|
7
|
+
dynamic = ["version"]
|
|
8
8
|
description = "Un toolkit pour vite coder un modèle pytorch d'IA utilisant MLflow"
|
|
9
9
|
|
|
10
10
|
authors = [
|
|
@@ -13,20 +13,16 @@ authors = [
|
|
|
13
13
|
|
|
14
14
|
license = { text = "Apache-2.0" }
|
|
15
15
|
|
|
16
|
-
|
|
17
16
|
readme = "README.md"
|
|
18
17
|
requires-python = ">=3.7"
|
|
19
18
|
|
|
20
|
-
[tool.
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
"iapytoo.metrics",
|
|
24
|
-
"iapytoo.utils",
|
|
25
|
-
"iapytoo.predictions",
|
|
26
|
-
"iapytoo.train"
|
|
27
|
-
]
|
|
19
|
+
[tool.setuptools_scm]
|
|
20
|
+
version_file = "iapytoo/_version.py"
|
|
21
|
+
local_scheme = "no-local-version"
|
|
28
22
|
|
|
29
|
-
|
|
23
|
+
[tool.setuptools.packages.find]
|
|
24
|
+
where = ["."]
|
|
25
|
+
exclude = ["examples", "tests"]
|
|
30
26
|
|
|
31
27
|
[tool.setuptools.dynamic]
|
|
32
28
|
dependencies = {file = ["requirements.txt"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
iapytoo
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|