disent 0.0.0__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.
Files changed (185) hide show
  1. disent-0.0.0/LICENSE.txt +21 -0
  2. disent-0.0.0/MANIFEST.in +1 -0
  3. disent-0.0.0/PKG-INFO +555 -0
  4. disent-0.0.0/README.md +538 -0
  5. disent-0.0.0/disent/__init__.py +23 -0
  6. disent-0.0.0/disent/dataset/__init__.py +27 -0
  7. disent-0.0.0/disent/dataset/_base.py +429 -0
  8. disent-0.0.0/disent/dataset/data/__init__.py +62 -0
  9. disent-0.0.0/disent/dataset/data/_episodes.py +83 -0
  10. disent-0.0.0/disent/dataset/data/_episodes__custom.py +168 -0
  11. disent-0.0.0/disent/dataset/data/_groundtruth.py +418 -0
  12. disent-0.0.0/disent/dataset/data/_groundtruth__cars3d.py +222 -0
  13. disent-0.0.0/disent/dataset/data/_groundtruth__dsprites.py +80 -0
  14. disent-0.0.0/disent/dataset/data/_groundtruth__dsprites_imagenet.py +400 -0
  15. disent-0.0.0/disent/dataset/data/_groundtruth__mpi3d.py +284 -0
  16. disent-0.0.0/disent/dataset/data/_groundtruth__norb.py +353 -0
  17. disent-0.0.0/disent/dataset/data/_groundtruth__shapes3d.py +72 -0
  18. disent-0.0.0/disent/dataset/data/_groundtruth__sprites.py +286 -0
  19. disent-0.0.0/disent/dataset/data/_groundtruth__xcolumns.py +73 -0
  20. disent-0.0.0/disent/dataset/data/_groundtruth__xyobject.py +248 -0
  21. disent-0.0.0/disent/dataset/data/_groundtruth__xysquares.py +292 -0
  22. disent-0.0.0/disent/dataset/data/_random__teapots3d.py +169 -0
  23. disent-0.0.0/disent/dataset/data/_raw.py +131 -0
  24. disent-0.0.0/disent/dataset/sampling/__init__.py +41 -0
  25. disent-0.0.0/disent/dataset/sampling/_base.py +90 -0
  26. disent-0.0.0/disent/dataset/sampling/_groundtruth__dist.py +272 -0
  27. disent-0.0.0/disent/dataset/sampling/_groundtruth__pair.py +181 -0
  28. disent-0.0.0/disent/dataset/sampling/_groundtruth__pair_orig.py +120 -0
  29. disent-0.0.0/disent/dataset/sampling/_groundtruth__single.py +60 -0
  30. disent-0.0.0/disent/dataset/sampling/_groundtruth__triplet.py +440 -0
  31. disent-0.0.0/disent/dataset/sampling/_groundtruth__walk.py +130 -0
  32. disent-0.0.0/disent/dataset/sampling/_random__any.py +53 -0
  33. disent-0.0.0/disent/dataset/sampling/_random__episodes.py +97 -0
  34. disent-0.0.0/disent/dataset/sampling/_single.py +52 -0
  35. disent-0.0.0/disent/dataset/transform/__init__.py +39 -0
  36. disent-0.0.0/disent/dataset/transform/_augment.py +308 -0
  37. disent-0.0.0/disent/dataset/transform/_augment_disent.py +78 -0
  38. disent-0.0.0/disent/dataset/transform/_transforms.py +155 -0
  39. disent-0.0.0/disent/dataset/transform/functional.py +302 -0
  40. disent-0.0.0/disent/dataset/util/__init__.py +23 -0
  41. disent-0.0.0/disent/dataset/util/datafile.py +241 -0
  42. disent-0.0.0/disent/dataset/util/formats/__init__.py +23 -0
  43. disent-0.0.0/disent/dataset/util/formats/hdf5.py +789 -0
  44. disent-0.0.0/disent/dataset/util/formats/npz.py +88 -0
  45. disent-0.0.0/disent/dataset/util/state_space.py +509 -0
  46. disent-0.0.0/disent/dataset/util/stats.py +230 -0
  47. disent-0.0.0/disent/dataset/wrapper/__init__.py +30 -0
  48. disent-0.0.0/disent/dataset/wrapper/_base.py +58 -0
  49. disent-0.0.0/disent/dataset/wrapper/_dither.py +85 -0
  50. disent-0.0.0/disent/dataset/wrapper/_masked.py +99 -0
  51. disent-0.0.0/disent/frameworks/__init__.py +28 -0
  52. disent-0.0.0/disent/frameworks/_ae_mixin.py +167 -0
  53. disent-0.0.0/disent/frameworks/_framework.py +292 -0
  54. disent-0.0.0/disent/frameworks/ae/__init__.py +34 -0
  55. disent-0.0.0/disent/frameworks/ae/_supervised__adaneg_tae.py +76 -0
  56. disent-0.0.0/disent/frameworks/ae/_supervised__tae.py +59 -0
  57. disent-0.0.0/disent/frameworks/ae/_unsupervised__ae.py +187 -0
  58. disent-0.0.0/disent/frameworks/ae/_unsupervised__dotae.py +86 -0
  59. disent-0.0.0/disent/frameworks/ae/_weaklysupervised__adaae.py +79 -0
  60. disent-0.0.0/disent/frameworks/helper/__init__.py +23 -0
  61. disent-0.0.0/disent/frameworks/helper/latent_distributions.py +179 -0
  62. disent-0.0.0/disent/frameworks/helper/reconstructions.py +338 -0
  63. disent-0.0.0/disent/frameworks/helper/util.py +74 -0
  64. disent-0.0.0/disent/frameworks/vae/__init__.py +40 -0
  65. disent-0.0.0/disent/frameworks/vae/_supervised__adaneg_tvae.py +213 -0
  66. disent-0.0.0/disent/frameworks/vae/_supervised__tvae.py +65 -0
  67. disent-0.0.0/disent/frameworks/vae/_unsupervised__betatcvae.py +129 -0
  68. disent-0.0.0/disent/frameworks/vae/_unsupervised__betavae.py +103 -0
  69. disent-0.0.0/disent/frameworks/vae/_unsupervised__dfcvae.py +214 -0
  70. disent-0.0.0/disent/frameworks/vae/_unsupervised__dipvae.py +136 -0
  71. disent-0.0.0/disent/frameworks/vae/_unsupervised__dotvae.py +248 -0
  72. disent-0.0.0/disent/frameworks/vae/_unsupervised__infovae.py +213 -0
  73. disent-0.0.0/disent/frameworks/vae/_unsupervised__vae.py +257 -0
  74. disent-0.0.0/disent/frameworks/vae/_weaklysupervised__adavae.py +380 -0
  75. disent-0.0.0/disent/metrics/__init__.py +36 -0
  76. disent-0.0.0/disent/metrics/_dci.py +172 -0
  77. disent-0.0.0/disent/metrics/_factor_vae.py +237 -0
  78. disent-0.0.0/disent/metrics/_factored_components.py +762 -0
  79. disent-0.0.0/disent/metrics/_flatness.py +374 -0
  80. disent-0.0.0/disent/metrics/_mig.py +83 -0
  81. disent-0.0.0/disent/metrics/_sap.py +118 -0
  82. disent-0.0.0/disent/metrics/_unsupervised.py +107 -0
  83. disent-0.0.0/disent/metrics/utils.py +239 -0
  84. disent-0.0.0/disent/model/__init__.py +28 -0
  85. disent-0.0.0/disent/model/_base.py +164 -0
  86. disent-0.0.0/disent/model/ae/__init__.py +33 -0
  87. disent-0.0.0/disent/model/ae/_linear.py +64 -0
  88. disent-0.0.0/disent/model/ae/_norm_conv64.py +154 -0
  89. disent-0.0.0/disent/model/ae/_vae_conv64.py +130 -0
  90. disent-0.0.0/disent/model/ae/_vae_fc.py +108 -0
  91. disent-0.0.0/disent/nn/__init__.py +23 -0
  92. disent-0.0.0/disent/nn/activations.py +67 -0
  93. disent-0.0.0/disent/nn/functional/__init__.py +72 -0
  94. disent-0.0.0/disent/nn/functional/_conv2d.py +90 -0
  95. disent-0.0.0/disent/nn/functional/_conv2d_kernels.py +133 -0
  96. disent-0.0.0/disent/nn/functional/_correlation.py +96 -0
  97. disent-0.0.0/disent/nn/functional/_dct.py +135 -0
  98. disent-0.0.0/disent/nn/functional/_mean.py +113 -0
  99. disent-0.0.0/disent/nn/functional/_norm.py +132 -0
  100. disent-0.0.0/disent/nn/functional/_other.py +90 -0
  101. disent-0.0.0/disent/nn/functional/_pca.py +97 -0
  102. disent-0.0.0/disent/nn/functional/_util_generic.py +99 -0
  103. disent-0.0.0/disent/nn/loss/__init__.py +23 -0
  104. disent-0.0.0/disent/nn/loss/kl.py +106 -0
  105. disent-0.0.0/disent/nn/loss/reduction.py +96 -0
  106. disent-0.0.0/disent/nn/loss/softsort.py +260 -0
  107. disent-0.0.0/disent/nn/loss/triplet.py +322 -0
  108. disent-0.0.0/disent/nn/loss/triplet_mining.py +180 -0
  109. disent-0.0.0/disent/nn/modules.py +54 -0
  110. disent-0.0.0/disent/nn/weights.py +103 -0
  111. disent-0.0.0/disent/registry/__init__.py +323 -0
  112. disent-0.0.0/disent/registry/_registry.py +728 -0
  113. disent-0.0.0/disent/schedule/__init__.py +46 -0
  114. disent-0.0.0/disent/schedule/_schedule.py +464 -0
  115. disent-0.0.0/disent/schedule/lerp.py +125 -0
  116. disent-0.0.0/disent/util/__init__.py +78 -0
  117. disent-0.0.0/disent/util/array.py +54 -0
  118. disent-0.0.0/disent/util/deprecate.py +136 -0
  119. disent-0.0.0/disent/util/function.py +45 -0
  120. disent-0.0.0/disent/util/imports.py +76 -0
  121. disent-0.0.0/disent/util/inout/__init__.py +23 -0
  122. disent-0.0.0/disent/util/inout/cache.py +97 -0
  123. disent-0.0.0/disent/util/inout/files.py +185 -0
  124. disent-0.0.0/disent/util/inout/hashing.py +159 -0
  125. disent-0.0.0/disent/util/inout/paths.py +163 -0
  126. disent-0.0.0/disent/util/inout/tar.py +49 -0
  127. disent-0.0.0/disent/util/iters.py +122 -0
  128. disent-0.0.0/disent/util/jit.py +56 -0
  129. disent-0.0.0/disent/util/lightning/__init__.py +23 -0
  130. disent-0.0.0/disent/util/lightning/callbacks/__init__.py +31 -0
  131. disent-0.0.0/disent/util/lightning/callbacks/_callback_log_metrics.py +133 -0
  132. disent-0.0.0/disent/util/lightning/callbacks/_callback_print_progress.py +95 -0
  133. disent-0.0.0/disent/util/lightning/callbacks/_callback_vis_dists.py +350 -0
  134. disent-0.0.0/disent/util/lightning/callbacks/_callback_vis_latents.py +300 -0
  135. disent-0.0.0/disent/util/lightning/callbacks/_callbacks_base.py +84 -0
  136. disent-0.0.0/disent/util/lightning/callbacks/_helper.py +157 -0
  137. disent-0.0.0/disent/util/lightning/logger_util.py +127 -0
  138. disent-0.0.0/disent/util/math/__init__.py +23 -0
  139. disent-0.0.0/disent/util/math/dither.py +203 -0
  140. disent-0.0.0/disent/util/math/integer.py +53 -0
  141. disent-0.0.0/disent/util/math/random.py +102 -0
  142. disent-0.0.0/disent/util/profiling.py +183 -0
  143. disent-0.0.0/disent/util/seeds.py +97 -0
  144. disent-0.0.0/disent/util/strings/__init__.py +23 -0
  145. disent-0.0.0/disent/util/strings/colors.py +56 -0
  146. disent-0.0.0/disent/util/strings/fmt.py +121 -0
  147. disent-0.0.0/disent/util/visualize/__init__.py +23 -0
  148. disent-0.0.0/disent/util/visualize/plot.py +342 -0
  149. disent-0.0.0/disent/util/visualize/vis_img.py +464 -0
  150. disent-0.0.0/disent/util/visualize/vis_latents.py +159 -0
  151. disent-0.0.0/disent/util/visualize/vis_util.py +267 -0
  152. disent-0.0.0/disent.egg-info/PKG-INFO +555 -0
  153. disent-0.0.0/disent.egg-info/SOURCES.txt +183 -0
  154. disent-0.0.0/disent.egg-info/dependency_links.txt +1 -0
  155. disent-0.0.0/disent.egg-info/requires.txt +11 -0
  156. disent-0.0.0/disent.egg-info/top_level.txt +3 -0
  157. disent-0.0.0/experiment/__init__.py +23 -0
  158. disent-0.0.0/experiment/config/__init__.py +2 -0
  159. disent-0.0.0/experiment/run.py +477 -0
  160. disent-0.0.0/experiment/util/__init__.py +23 -0
  161. disent-0.0.0/experiment/util/hydra_data.py +214 -0
  162. disent-0.0.0/experiment/util/hydra_main.py +253 -0
  163. disent-0.0.0/experiment/util/hydra_utils.py +55 -0
  164. disent-0.0.0/experiment/util/path_utils.py +155 -0
  165. disent-0.0.0/experiment/util/run_utils.py +161 -0
  166. disent-0.0.0/requirements.txt +17 -0
  167. disent-0.0.0/setup.cfg +4 -0
  168. disent-0.0.0/setup.py +70 -0
  169. disent-0.0.0/tests/__init__.py +23 -0
  170. disent-0.0.0/tests/test_000_import.py +31 -0
  171. disent-0.0.0/tests/test_data_similarity.py +77 -0
  172. disent-0.0.0/tests/test_dataset_formats.py +211 -0
  173. disent-0.0.0/tests/test_docs_examples.py +60 -0
  174. disent-0.0.0/tests/test_experiment.py +95 -0
  175. disent-0.0.0/tests/test_frameworks.py +267 -0
  176. disent-0.0.0/tests/test_math.py +215 -0
  177. disent-0.0.0/tests/test_math_generic.py +148 -0
  178. disent-0.0.0/tests/test_metrics.py +64 -0
  179. disent-0.0.0/tests/test_models.py +57 -0
  180. disent-0.0.0/tests/test_registry.py +77 -0
  181. disent-0.0.0/tests/test_samplers.py +126 -0
  182. disent-0.0.0/tests/test_state_space.py +132 -0
  183. disent-0.0.0/tests/test_to_img.py +464 -0
  184. disent-0.0.0/tests/test_transform.py +69 -0
  185. disent-0.0.0/tests/util.py +94 -0
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Nathan Juraj Michlo
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1 @@
1
+ include requirements.txt
disent-0.0.0/PKG-INFO ADDED
@@ -0,0 +1,555 @@
1
+ Metadata-Version: 2.1
2
+ Name: disent
3
+ Version: 0.0.0
4
+ Summary: Vae disentanglement framework built with pytorch lightning.
5
+ Home-page: https://github.com/nmichlo/disent
6
+ Author: Nathan Juraj Michlo
7
+ Author-email: NathanJMichlo@gmail.com
8
+ Classifier: License :: OSI Approved :: MIT License
9
+ Classifier: Operating System :: OS Independent
10
+ Classifier: Programming Language :: Python :: 3.8
11
+ Classifier: Programming Language :: Python :: 3.9
12
+ Classifier: Programming Language :: Python :: 3.10
13
+ Classifier: Intended Audience :: Science/Research
14
+ Requires-Python: >=3.8
15
+ Description-Content-Type: text/markdown
16
+ License-File: LICENSE.txt
17
+
18
+
19
+ <p align="center">
20
+ <h1 align="center">🧶 Disent</h1>
21
+ <p align="center">
22
+ <i>A modular disentangled representation learning framework built with PyTorch Lightning</i>
23
+ </p>
24
+ </p>
25
+
26
+ <p align="center">
27
+ <a href="https://choosealicense.com/licenses/mit/" target="_blank">
28
+ <img alt="license" src="https://img.shields.io/github/license/nmichlo/disent?style=flat-square&color=lightgrey"/>
29
+ </a>
30
+ <a href="https://pypi.org/project/disent" target="_blank">
31
+ <img alt="python versions" src="https://img.shields.io/pypi/pyversions/disent?style=flat-square"/>
32
+ </a>
33
+ <a href="https://pypi.org/project/disent" target="_blank">
34
+ <img alt="pypi version" src="https://img.shields.io/pypi/v/disent?style=flat-square&color=blue"/>
35
+ </a>
36
+ <a href="https://github.com/nmichlo/disent/actions?query=workflow%3Atests">
37
+ <img alt="tests status" src="https://github.com/nmichlo/disent/actions/workflows/python-tests.yml/badge.svg"/>
38
+ </a>
39
+ <a href="https://github.com/psf/black" target="_blank">
40
+ <img alt="Code style: black" src="https://img.shields.io/badge/code%20style-black-000000.svg"/>
41
+ </a>
42
+ <a href="https://pycqa.github.io/isort" target="_blank">
43
+ <img alt="Imports: isort" src="https://img.shields.io/badge/%20imports-isort-%231674b1?style=flat&labelColor=ef8336"/>
44
+ </a>
45
+ <!-- <a href="https://codecov.io/gh/nmichlo/disent/"> -->
46
+ <!-- <img alt="code coverage" src="https://img.shields.io/codecov/c/gh/nmichlo/disent?token=86IZK3J038&style=flat-square"/> -->
47
+ <!-- </a> -->
48
+ <!-- <a href="https://github.com/nmichlo/disent"> -->
49
+ <!-- <img alt="last commit" src="https://img.shields.io/github/last-commit/nmichlo/disent?style=flat-square&color=lightgrey"/> -->
50
+ <!-- </a> -->
51
+ </p>
52
+
53
+ <p align="center">
54
+ <p align="center">
55
+ Visit the <a href="https://disent.michlo.dev/" target="_blank">docs</a> for more info, or browse the <a href="https://github.com/nmichlo/disent/releases">releases</a>.
56
+ </p>
57
+ <p align="center">
58
+ <a href="https://github.com/nmichlo/disent/issues/new/choose">Contributions</a> are welcome!
59
+ </p>
60
+ </p>
61
+
62
+ <p align="center">
63
+ ────────────────
64
+ <br/>
65
+ <i><b> NOTE:</b> My MSc. research has moved <a href="https://github.com/nmichlo/msc-research">here</a></i>
66
+ <br/>
67
+ <i>Some of the contributions have been incorporated directly into disent</i>
68
+ <br/>
69
+ ────────────────
70
+ </p>
71
+
72
+ ----------------------
73
+
74
+ ## Table Of Contents
75
+
76
+ - [Overview](#overview)
77
+ - [Features](#features)
78
+ * [Datasets](#datasets)
79
+ * [Frameworks](#frameworks)
80
+ * [Metrics](#metrics)
81
+ * [Schedules & Annealing](#schedules--annealing)
82
+ - [Architecture](#architecture)
83
+ - [Examples](#examples)
84
+ * [Python Example](#python-example)
85
+ * [Hydra Config Example](#hydra-config-example)
86
+ - [Install](#install)
87
+ - [Development](#development)
88
+ - [Why?](#why)
89
+
90
+ ----------------------
91
+
92
+ ## Overview
93
+
94
+ Disent is a modular disentangled representation learning framework for auto-encoders,
95
+ built upon PyTorch-Lightning. This framework consists of various composable components
96
+ that can be used to build and benchmark various disentanglement vision tasks.
97
+
98
+ > The name of the framework is derived from both **disent**anglement and scientific **dissent**.
99
+
100
+ Get started with disent by installing it with $`pip install disent` or cloning this repository.
101
+
102
+ ### Goals
103
+
104
+ Disent aims to fill the following criteria:
105
+ 1. Provide **high quality**, **readable**, **consistent** and **easily comparable** implementations of frameworks
106
+ 2. **Highlight difference** between framework implementations by overriding **hooks** and minimising duplicate code
107
+ 3. Use **best practice** eg. `torch.distributions`
108
+ 4. Be extremely **flexible** & configurable
109
+ 5. Support low memory systems
110
+
111
+ ### Citing Disent
112
+
113
+ Please use the following citation if you use Disent in your own research:
114
+
115
+ ```bibtex
116
+ @Misc{Michlo2021Disent,
117
+ author = {Nathan Juraj Michlo},
118
+ title = {Disent - A modular disentangled representation learning framework for pytorch},
119
+ howpublished = {Github},
120
+ year = {2021},
121
+ url = {https://github.com/nmichlo/disent}
122
+ }
123
+ ```
124
+
125
+ ----------------------
126
+
127
+ ## Features
128
+
129
+ Disent includes implementations of modules, metrics and
130
+ datasets from various papers.
131
+
132
+ _Note that "🧵" means that the dataset, framework or metric was introduced by disent!_
133
+
134
+ ### Datasets
135
+
136
+ Various common datasets used in disentanglement research are included with disent. The dataset loaders provide various features including:
137
+ - automatic downloads & preperation `prepare=True`
138
+ - automatic hash verification
139
+ - automatic optimization of underlying hdf5 formats for
140
+ low-memory disk-based access.
141
+
142
+ Data input and target dataset augmentations and transforms are supported, as well as augmentations
143
+ on the GPU or CPU at different points in the pipeline.
144
+
145
+ - **Ground Truth**:
146
+ + <details>
147
+ <summary>🚗 <a href="https://papers.nips.cc/paper/5845-deep-visual-analogy-making" target="_blank">Cars3D</a></summary>
148
+ <p align="center"><img height="192" src="docs/img/traversals/traversal-transpose__cars3d.jpg" alt="Cars3D Dataset Factor Traversals"></p>
149
+ </details>
150
+
151
+ + <details>
152
+ <summary>◻️ <a href="https://github.com/deepmind/dsprites-dataset" target="_blank">dSprites</a></summary>
153
+ <p align="center"><img height="192" src="docs/img/traversals/traversal-transpose__dsprites.jpg" alt="dSprites Dataset Factor Traversals"></p>
154
+ </details>
155
+
156
+ + <details>
157
+ <summary>🔺 <a href="https://arxiv.org/abs/1906.03292" target="_blank">MPI3D</a></summary>
158
+ <p align="center"><img height="192" src="docs/img/traversals/traversal-transpose__mpi3d-real.jpg" alt="MPI3D-Real Dataset Factor Traversals"></p>
159
+ </details>
160
+
161
+ + <details>
162
+ <summary>🐘 <a href="https://cs.nyu.edu/~ylclab/data/norb-v1.0-small/" target="_blank">SmallNORB</a></summary>
163
+ <p align="center"><img height="192" src="docs/img/traversals/traversal-transpose__smallnorb.jpg" alt="Small Norb Dataset Factor Traversals"></p>
164
+ </details>
165
+
166
+ + <details>
167
+ <summary>🌈 <a href="https://github.com/deepmind/3d-shapes" target="_blank">Shapes3D</a></summary>
168
+ <p align="center"><img height="192" src="docs/img/traversals/traversal-transpose__shapes3d.jpg" alt="Shapes3D Dataset Factor Traversals"></p>
169
+ </details>
170
+
171
+ + <details>
172
+ <summary>🏹 <a href="https://github.com/YingzhenLi/Sprites" target="_blank">Sprites (custom)</a></summary>
173
+ <p align="center"><img height="192" src="docs/img/traversals/traversal-transpose__sprites.jpg" alt="Sprites (Custom) Dataset Factor Traversals"></p>
174
+ </details>
175
+
176
+ + <details open>
177
+ <summary>
178
+ 🧵 <u>dSpritesImagenet</u>:
179
+ <i>Version of DSprite with foreground or background deterministically masked out with tiny-imagenet data.</i>
180
+ </summary>
181
+ <p align="center"><img height="192" src="docs/img/traversals/traversal-transpose__dsprites-imagenet-bg-100.jpg" alt="dSpritesImagenet Dataset Factor Traversals"></p>
182
+ </details>
183
+
184
+ - **Ground Truth Synthetic**:
185
+ + <details open>
186
+ <summary>
187
+ 🧵 <u>XYSquares</u>:
188
+ <i>Three non-overlapping squares that can move around a grid. This dataset is adversarial to VAEs that use pixel-wise reconstruction losses.</i>
189
+ </summary>
190
+ <p align="center"><img height="192" src="docs/img/traversals/traversal-transpose__xy-squares__spacing8.jpg" alt="XYSquares Dataset Factor Traversals"></p>
191
+ </details>
192
+
193
+ + <details>
194
+ <summary>
195
+ 🧵 <u>XYObject</u>:
196
+ <i>A simplistic version of dSprites with a single square.</i>
197
+ </summary>
198
+ <p align="center"><img height="192" src="docs/img/traversals/traversal-transpose__xy-object.jpg" alt="XYObject Dataset Factor Traversals"></p>
199
+ </details>
200
+
201
+ + <details open>
202
+ <summary>
203
+ 🧵 <u>XYObjectShaded</u>:
204
+ <i>Exact same dataset as XYObject, but ground truth factors have a different representation.</i>
205
+ </summary>
206
+ <p align="center"><img height="192" src="docs/img/traversals/traversal-transpose__xy-object-shaded.jpg" alt="XYObjectShaded Dataset Factor Traversals"></p>
207
+ </details>
208
+
209
+ ### Frameworks
210
+
211
+ Disent provides the following Auto-Encoders and Variational Auto-Encoders!
212
+
213
+ - **Unsupervised**:
214
+ + <u>AE</u>: _Auto-Encoder_
215
+ + [VAE](https://arxiv.org/abs/1312.6114): Variational Auto-Encoder
216
+ + [Beta-VAE](https://openreview.net/forum?id=Sy2fzU9gl): VAE with Scaled Loss
217
+ + [DFC-VAE](https://arxiv.org/abs/1610.00291): Deep Feature Consistent VAE
218
+ + [DIP-VAE](https://arxiv.org/abs/1711.00848): Disentangled Inferred Prior VAE
219
+ + [InfoVAE](https://arxiv.org/abs/1706.02262): Information Maximizing VAE
220
+ + [BetaTCVAE](https://arxiv.org/abs/1802.04942): Total Correlation VAE
221
+ - **Weakly Supervised**:
222
+ + [Ada-GVAE](https://arxiv.org/abs/2002.02886): Adaptive GVAE, *`AdaVae.cfg(average_mode='gvae')`*, usually better than below!
223
+ + [Ada-ML-VAE](https://arxiv.org/abs/2002.02886): Adaptive ML-VAE, *`AdaVae.cfg(average_mode='ml-vae')`*
224
+ - **Supervised**:
225
+ + <u>TAE</u>: _Triplet Auto-Encoder_
226
+ + [TVAE](https://arxiv.org/abs/1802.04403): Triplet Variational Auto-Encoder
227
+
228
+ Introduced in Disent
229
+
230
+ - **Unsupervised**:
231
+ + 🧵 <u>Ada-TVAE-D</u>: Adaptive Triplet VAE that uses data distances instead of ground-truth distances as the supervision signal.
232
+ + 🧵 <u>Ada-TAE-D</u>: Adaptive Triplet AE that uses data distances instead of ground-truth distances as the supervision signal.
233
+ - **Weakly Supervised**:
234
+ + 🧵 <u>Ada-AE</u>: Adaptive AE, the auto-encoder version of the Ada-GVAE
235
+ - **Supervised**:
236
+ + 🧵 <u>Ada-TVAE</u>: Adaptive Triplet VAE, disentangled version of the TVAE
237
+ + 🧵 <u>Ada-TAE</u>: Adaptive Triplet AE, disentangled version of the TAE
238
+
239
+ <details><summary><b>🏗 Todo</b>: <i>Many popular disentanglement frameworks still need to be added, please
240
+ submit an issue if you have a request for an additional framework.</i></summary><p>
241
+
242
+ + FactorVAE
243
+ + GroupVAE
244
+ + MLVAE
245
+
246
+ </p></details>
247
+
248
+ ### Metrics
249
+ Various metrics are provided by disent that can be used to evaluate the
250
+ learnt representations of models that have been trained on ground-truth data.
251
+
252
+ - **Disentanglement**:
253
+ + [FactorVAE Score](https://arxiv.org/abs/1802.05983)
254
+ + [DCI](https://openreview.net/forum?id=By-7dz-AZ)
255
+ + [MIG](https://arxiv.org/abs/1802.04942)
256
+ + [SAP](https://arxiv.org/abs/1711.00848)
257
+ + [Unsupervised Scores](https://github.com/google-research/disentanglement_lib)
258
+ + 🧵 <u>Flatness Components</u>: _Measures of the three components needed to learn factored representations from distances. VAEs often learn the first two (correlation & linearity), and the can happen accidentally (axis-alignment)!_
259
+ - 🪡 <u>Ground-Truth Correlation</u> - _The spearman rank correlation between latent distances and ground-truth distances._
260
+ - 🪡 <u>Linearity Ratio</u> - _How well factor traversals lie along an n-dimensional arbitrarily rotated line in the latent space_
261
+ - 🪡 <u>Axis-Alignment Ratio</u> - _How well factor traversals are represented by a single latent variable, ie. an n-dimensional line that is axis-aligned._
262
+ + 🧵 <u>Flatness Score</u> - _Measuring the max distance between factor traversal embeddings and the path length of their embeddings._
263
+
264
+ <details><summary><b>🏗 Todo</b>: <i>Some popular metrics still need to be added, please submit an issue if you wish to
265
+ add your own, or you have a request.</i></summary><p>
266
+
267
+ + [DCIMIG](https://arxiv.org/abs/1910.05587)
268
+ + [Modularity and Explicitness](https://arxiv.org/abs/1802.05312)
269
+
270
+ </p></details>
271
+
272
+ ### Schedules & Annealing
273
+
274
+ Hyper-parameter annealing is supported through the use of schedules.
275
+ The currently implemented schedules include:
276
+
277
+ - Linear Schedule
278
+ - [Cyclic](https://arxiv.org/abs/1903.10145) Schedule
279
+ - Cosine Wave Schedule
280
+ - *Various other wrapper schedules*
281
+
282
+ ----------------------
283
+
284
+ ## Architecture
285
+
286
+ The disent module structure:
287
+
288
+ - `disent.dataset`: dataset wrappers, datasets & sampling strategies
289
+ + `disent.dataset.data`: raw datasets
290
+ + `disent.dataset.sampling`: sampling strategies for `DisentDataset` when multiple elements are required by frameworks, eg. for triplet loss
291
+ + `disent.dataset.transform`: common data transforms and augmentations
292
+ + `disent.dataset.wrapper`: wrapped datasets are no longer ground-truth datasets, these may have some elements masked out. We can still unwrap these classes to obtain the original datasets for benchmarking.
293
+ - `disent.frameworks`: frameworks, including Auto-Encoders and VAEs
294
+ + `disent.frameworks.ae`: Auto-Encoder based frameworks
295
+ + `disent.frameworks.vae`: Variational Auto-Encoder based frameworks
296
+ - `disent.metrics`: metrics for evaluating disentanglement using ground truth datasets
297
+ - `disent.model`: common encoder and decoder models used for VAE research
298
+ - `disent.nn`: torch components for building models including layers, transforms, losses and general maths
299
+ - `disent.schedule`: annealing schedules that can be registered to a framework
300
+ - `disent.util`: helper classes, functions, callbacks, anything unrelated to a pytorch system/model/framework.
301
+
302
+ **⚠️ The API Is _Mostly_ Stable ⚠️**
303
+
304
+ Disent is still under development. Features and APIs are subject to change!
305
+ However, I will try and minimise the impact of these.
306
+
307
+ A small suite of tests currently exist which will be expanded upon in time.
308
+
309
+ **Hydra Experiment Directories**
310
+
311
+ Easily run experiments with hydra config, these files
312
+ are not available from `pip install`.
313
+
314
+ - `experiment/run.py`: entrypoint for running basic experiments with [hydra](https://github.com/facebookresearch/hydra) config
315
+ - `experiment/config/config.yaml`: main configuration file, this is probably what you want to edit!
316
+ - `experiment/config`: root folder for [hydra](https://github.com/facebookresearch/hydra) config files
317
+ - `experiment/util`: various helper code for experiments
318
+
319
+ **Extending The Default Configs**
320
+
321
+ All configs in `experiment/config` can easily be extended or overridden
322
+ without modifying any files. We can add a new config folder to the hydra search path
323
+ by setting the environment variable `DISENT_CONFIGS_PREPEND` to point to a config folder
324
+ that should take priority over those contained in the default folder.
325
+
326
+ The advantage of this is that new frameworks and datasets can be used with experiments without cloning or modifying
327
+ disent itself. You can separate your research code from the library!
328
+ - See the examples in the docs for more information!
329
+
330
+ ----------------------
331
+
332
+ ## Examples
333
+
334
+ ### Python Example
335
+
336
+ The following is a basic working example of disent that trains a BetaVAE with a cyclic
337
+ beta schedule and evaluates the trained model with various metrics.
338
+
339
+ <details><summary><b>💾 Basic Example</b></summary>
340
+ <p>
341
+
342
+ ```python3
343
+ import lightning as L
344
+ import torch
345
+ from torch.utils.data import DataLoader
346
+
347
+ from disent.dataset import DisentDataset
348
+ from disent.dataset.data import XYObjectData
349
+ from disent.dataset.sampling import SingleSampler
350
+ from disent.dataset.transform import ToImgTensorF32
351
+ from disent.frameworks.vae import BetaVae
352
+ from disent.metrics import metric_dci
353
+ from disent.metrics import metric_mig
354
+ from disent.model import AutoEncoder
355
+ from disent.model.ae import DecoderConv64
356
+ from disent.model.ae import EncoderConv64
357
+ from disent.schedule import CyclicSchedule
358
+
359
+ # create the dataset & dataloaders
360
+ # - ToImgTensorF32 transforms images from numpy arrays to tensors and performs checks
361
+ # - if you use `num_workers != 0` in the DataLoader, the make sure to
362
+ # wrap `trainer.fit` with `if __name__ == '__main__': ...`
363
+ data = XYObjectData()
364
+ dataset = DisentDataset(dataset=data, sampler=SingleSampler(), transform=ToImgTensorF32())
365
+ dataloader = DataLoader(dataset=dataset, batch_size=128, shuffle=True, num_workers=0)
366
+
367
+ # create the BetaVAE model
368
+ # - adjusting the beta, learning rate, and representation size.
369
+ module = BetaVae(
370
+ model=AutoEncoder(
371
+ # z_multiplier is needed to output mu & logvar when parameterising normal distribution
372
+ encoder=EncoderConv64(x_shape=data.x_shape, z_size=10, z_multiplier=2),
373
+ decoder=DecoderConv64(x_shape=data.x_shape, z_size=10),
374
+ ),
375
+ cfg=BetaVae.cfg(
376
+ optimizer='adam',
377
+ optimizer_kwargs=dict(lr=1e-3),
378
+ loss_reduction='mean_sum',
379
+ beta=4,
380
+ )
381
+ )
382
+
383
+ # cyclic schedule for target 'beta' in the config/cfg. The initial value from the
384
+ # config is saved and multiplied by the ratio from the schedule on each step.
385
+ # - based on: https://arxiv.org/abs/1903.10145
386
+ module.register_schedule(
387
+ 'beta', CyclicSchedule(
388
+ period=1024, # repeat every: trainer.global_step % period
389
+ )
390
+ )
391
+
392
+ # train model
393
+ # - for 2048 batches/steps
394
+ trainer = L.Trainer(
395
+ max_steps=2048, gpus=1 if torch.cuda.is_available() else None, logger=False, enable_checkpointing=False
396
+ )
397
+ trainer.fit(module, dataloader)
398
+
399
+ # compute disentanglement metrics
400
+ # - we cannot guarantee which device the representation is on
401
+ # - this will take a while to run
402
+ get_repr = lambda x: module.encode(x.to(module.device))
403
+
404
+ metrics = {
405
+ **metric_dci(dataset, get_repr, num_train=1000, num_test=500, show_progress=True),
406
+ **metric_mig(dataset, get_repr, num_train=2000),
407
+ }
408
+
409
+ # evaluate
410
+ print('metrics:', metrics)
411
+ ```
412
+
413
+ </p>
414
+ </details>
415
+
416
+ Visit the [docs](https://disent.michlo.dev) for more examples!
417
+
418
+
419
+ ### Hydra Config Example
420
+
421
+ The entrypoint for basic experiments is `experiment/run.py`.
422
+
423
+ Some configuration will be required, but basic experiments can
424
+ be adjusted by modifying the [Hydra Config 1.1](https://github.com/facebookresearch/hydra)
425
+ files in `experiment/config`.
426
+
427
+ Modifying the main `experiment/config/config.yaml` is all you
428
+ need for most basic experiments. The main config file contains
429
+ a defaults list with entries corresponding to yaml configuration
430
+ files (config options) in the subfolders (config groups) in
431
+ `experiment/config/<config_group>/<option>.yaml`.
432
+
433
+ <details><summary><b>💾 Config Defaults Example</b></summary>
434
+ <p>
435
+
436
+ ```yaml
437
+ defaults:
438
+ # data
439
+ - sampling: default__bb
440
+ - dataset: xyobject
441
+ - augment: none
442
+ # system
443
+ - framework: adavae_os
444
+ - model: vae_conv64
445
+ # training
446
+ - optimizer: adam
447
+ - schedule: beta_cyclic
448
+ - metrics: fast
449
+ - run_length: short
450
+ # logs
451
+ - run_callbacks: vis
452
+ - run_logging: wandb
453
+ # runtime
454
+ - run_location: local
455
+ - run_launcher: local
456
+ - run_action: train
457
+
458
+ # <rest of config.yaml left out>
459
+ ...
460
+ ```
461
+
462
+ </p>
463
+ </details>
464
+
465
+ Easily modify any of these values to adjust how the basic experiment
466
+ will be run. For example, change `framework: adavae` to `framework: betavae`, or
467
+ change the dataset from `xyobject` to `shapes3d`. Add new options by adding new
468
+ yaml files in the config group folders.
469
+
470
+ [Weights and Biases](https://docs.wandb.ai/quickstart) is supported by changing `run_logging: none` to
471
+ `run_logging: wandb`. However, you will need to login from the command line. W&B logging supports
472
+ visualisations of latent traversals.
473
+
474
+
475
+ ----------------------
476
+
477
+ ### Install
478
+
479
+ ```bash
480
+ pip install disent
481
+ ```
482
+
483
+ Otherwise, to install from source we recommend using a conda virtual environment.
484
+
485
+ <details><summary><b>⤵️ Install from Source</b></summary>
486
+
487
+ ```bash
488
+ # clone the repo
489
+ git clone https://github.com/nmichlo/disent
490
+ cd disent
491
+
492
+ # create and activate the conda environment [py38,py39,py310]
493
+ conda create -n disent-py310 python=3.10
494
+ conda activate disent-py310
495
+
496
+ # check that the correct python version is used
497
+ which python
498
+ which pip
499
+
500
+ # make sure to upgrade pip
501
+ pip install --upgrade pip
502
+
503
+ # install minimal requirements
504
+ pip install -r requirements.txt
505
+
506
+ # (optional) install extra requirements
507
+ # - first do the above because torch is required to compile torchsort while installing
508
+ pip install -r requirements-extra.txt
509
+
510
+ # (optional) install test requirements
511
+ pip install -r requirements-test.txt
512
+ ```
513
+
514
+ </details>
515
+
516
+ ----------------------
517
+
518
+ ### Development
519
+
520
+ [![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
521
+ [![Imports: isort](https://img.shields.io/badge/%20imports-isort-%231674b1?style=flat&labelColor=ef8336)](https://pycqa.github.io/isort/)
522
+
523
+ Make sure to install `pre-commit` hooks to ensure code is automatically formatted
524
+ correctly when committing or pushing changes to `disent`.
525
+
526
+ ```bash
527
+ # install git hooks
528
+ pip install pre-commit
529
+ pre-commit install
530
+
531
+ # manually trigger all pre-commit hooks
532
+ pre-commit run --all-files
533
+ ```
534
+
535
+ To run tests locally, make sure to install all the test and extra dependencies in your
536
+ environment.
537
+
538
+ ```bash
539
+ pip install -r requirements.txt
540
+ # torchsort first requires torch to be installed
541
+ pip install -r requirements-extra.txt -r requirements-test.txt
542
+ ```
543
+
544
+ ----------------------
545
+
546
+ ### Why?
547
+
548
+ - Created as part of my Computer Science MSc which ended early 2022.
549
+ - I needed custom high quality implementations of various VAE's.
550
+ - A pytorch version of [disentanglement_lib](https://github.com/google-research/disentanglement_lib).
551
+ - I didn't have time to wait for [Weakly-Supervised Disentanglement Without Compromises](https://arxiv.org/abs/2002.02886) to release
552
+ their code as part of disentanglement_lib. (As of September 2020 it has been released, but has unresolved [discrepencies](https://github.com/google-research/disentanglement_lib/issues/31)).
553
+ - disentanglement_lib still uses outdated Tensorflow 1.0, and the flow of data is unintuitive because of its use of [Gin Config](https://github.com/google/gin-config).
554
+
555
+ ----------------------