torchmil 0.0.1__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 (137) hide show
  1. torchmil-0.0.1/LICENSE +201 -0
  2. torchmil-0.0.1/PKG-INFO +107 -0
  3. torchmil-0.0.1/README.md +64 -0
  4. torchmil-0.0.1/pyproject.toml +72 -0
  5. torchmil-0.0.1/setup.cfg +4 -0
  6. torchmil-0.0.1/tests/data/test_collate.py +67 -0
  7. torchmil-0.0.1/tests/data/test_representations.py +199 -0
  8. torchmil-0.0.1/tests/datasets/test_binary_classification_dataset.py +116 -0
  9. torchmil-0.0.1/tests/datasets/test_camelyon16_dataset.py +57 -0
  10. torchmil-0.0.1/tests/datasets/test_ctscan_dataset.py +44 -0
  11. torchmil-0.0.1/tests/datasets/test_false_frequency_mil_dataset.py +43 -0
  12. torchmil-0.0.1/tests/datasets/test_mc_standard_mil_dataset.py +61 -0
  13. torchmil-0.0.1/tests/datasets/test_pandamil_dataset.py +67 -0
  14. torchmil-0.0.1/tests/datasets/test_processed_mil_dataset.py +173 -0
  15. torchmil-0.0.1/tests/datasets/test_rsna_mil_dataset.py +55 -0
  16. torchmil-0.0.1/tests/datasets/test_sc_standard_mil_dataset.py +49 -0
  17. torchmil-0.0.1/tests/datasets/test_toy_dataset.py +117 -0
  18. torchmil-0.0.1/tests/datasets/test_wsi_dataset.py +47 -0
  19. torchmil-0.0.1/tests/models/test_abmil.py +107 -0
  20. torchmil-0.0.1/tests/models/test_camil.py +58 -0
  21. torchmil-0.0.1/tests/models/test_clam.py +92 -0
  22. torchmil-0.0.1/tests/models/test_deepgraphsurv.py +57 -0
  23. torchmil-0.0.1/tests/models/test_dsmil.py +78 -0
  24. torchmil-0.0.1/tests/models/test_dtfdmil.py +83 -0
  25. torchmil-0.0.1/tests/models/test_gtp.py +75 -0
  26. torchmil-0.0.1/tests/models/test_iibmil.py +70 -0
  27. torchmil-0.0.1/tests/models/test_mil_model.py +82 -0
  28. torchmil-0.0.1/tests/models/test_patch_gcn.py +64 -0
  29. torchmil-0.0.1/tests/models/test_prob_smooth_abmil.py +89 -0
  30. torchmil-0.0.1/tests/models/test_setmil.py +72 -0
  31. torchmil-0.0.1/tests/models/test_sm_abmil.py +112 -0
  32. torchmil-0.0.1/tests/models/test_sm_transformer_abmil.py +152 -0
  33. torchmil-0.0.1/tests/models/test_transformer_abmil.py +86 -0
  34. torchmil-0.0.1/tests/models/test_transformer_prob_smooth_abmil.py +72 -0
  35. torchmil-0.0.1/tests/models/test_transmil.py +200 -0
  36. torchmil-0.0.1/tests/nn/attention/test_attention_pool.py +57 -0
  37. torchmil-0.0.1/tests/nn/attention/test_irpe_multihead_self_attention.py +123 -0
  38. torchmil-0.0.1/tests/nn/attention/test_multihead_cross_attention.py +65 -0
  39. torchmil-0.0.1/tests/nn/attention/test_multihead_self_attention.py +75 -0
  40. torchmil-0.0.1/tests/nn/attention/test_nystrom_attention.py +70 -0
  41. torchmil-0.0.1/tests/nn/attention/test_prob_smooth_attention_pool.py +118 -0
  42. torchmil-0.0.1/tests/nn/attention/test_sm_attention_pool.py +50 -0
  43. torchmil-0.0.1/tests/nn/gnns/test_deepgcn.py +165 -0
  44. torchmil-0.0.1/tests/nn/gnns/test_dense_mincut_pool.py +116 -0
  45. torchmil-0.0.1/tests/nn/gnns/test_gcn_conv.py +166 -0
  46. torchmil-0.0.1/tests/nn/gnns/test_gnn_identity.py +43 -0
  47. torchmil-0.0.1/tests/nn/test_max_pool.py +57 -0
  48. torchmil-0.0.1/tests/nn/test_mean_pool.py +56 -0
  49. torchmil-0.0.1/tests/nn/test_nn_utils.py +92 -0
  50. torchmil-0.0.1/tests/nn/test_relprop.py +292 -0
  51. torchmil-0.0.1/tests/nn/test_sm.py +73 -0
  52. torchmil-0.0.1/tests/nn/transformers/test_conventional_transformer.py +78 -0
  53. torchmil-0.0.1/tests/nn/transformers/test_encoder.py +66 -0
  54. torchmil-0.0.1/tests/nn/transformers/test_irpe_transformer.py +174 -0
  55. torchmil-0.0.1/tests/nn/transformers/test_layer.py +86 -0
  56. torchmil-0.0.1/tests/nn/transformers/test_nystrom_transformer.py +93 -0
  57. torchmil-0.0.1/tests/nn/transformers/test_sm_transformer.py +133 -0
  58. torchmil-0.0.1/tests/nn/transformers/test_t2t.py +89 -0
  59. torchmil-0.0.1/tests/utils/test_common.py +32 -0
  60. torchmil-0.0.1/tests/utils/test_graph_utils.py +205 -0
  61. torchmil-0.0.1/tests/utils/test_trainer.py +62 -0
  62. torchmil-0.0.1/tests/visualize/test_vis_ctscan.py +186 -0
  63. torchmil-0.0.1/tests/visualize/test_vis_wsi.py +259 -0
  64. torchmil-0.0.1/torchmil/data/__init__.py +9 -0
  65. torchmil-0.0.1/torchmil/data/collate.py +96 -0
  66. torchmil-0.0.1/torchmil/data/representations.py +84 -0
  67. torchmil-0.0.1/torchmil/datasets/__init__.py +14 -0
  68. torchmil-0.0.1/torchmil/datasets/binary_classification_dataset.py +94 -0
  69. torchmil-0.0.1/torchmil/datasets/camelyon16mil_dataset.py +92 -0
  70. torchmil-0.0.1/torchmil/datasets/ctscan_dataset.py +118 -0
  71. torchmil-0.0.1/torchmil/datasets/false_frequency_dataset.py +161 -0
  72. torchmil-0.0.1/torchmil/datasets/mc_standard_dataset.py +197 -0
  73. torchmil-0.0.1/torchmil/datasets/pandamil_dataset.py +99 -0
  74. torchmil-0.0.1/torchmil/datasets/processed_mil_dataset.py +329 -0
  75. torchmil-0.0.1/torchmil/datasets/rsnamil_dataset.py +82 -0
  76. torchmil-0.0.1/torchmil/datasets/sc_standard_dataset.py +158 -0
  77. torchmil-0.0.1/torchmil/datasets/toy_dataset.py +199 -0
  78. torchmil-0.0.1/torchmil/datasets/wsi_dataset.py +125 -0
  79. torchmil-0.0.1/torchmil/models/__init__.py +30 -0
  80. torchmil-0.0.1/torchmil/models/abmil.py +136 -0
  81. torchmil-0.0.1/torchmil/models/camil.py +294 -0
  82. torchmil-0.0.1/torchmil/models/clam.py +325 -0
  83. torchmil-0.0.1/torchmil/models/deepgraphsurv.py +170 -0
  84. torchmil-0.0.1/torchmil/models/dsmil.py +212 -0
  85. torchmil-0.0.1/torchmil/models/dtfdmil.py +227 -0
  86. torchmil-0.0.1/torchmil/models/gtp.py +224 -0
  87. torchmil-0.0.1/torchmil/models/iibmil.py +309 -0
  88. torchmil-0.0.1/torchmil/models/mil_model.py +156 -0
  89. torchmil-0.0.1/torchmil/models/patch_gcn.py +180 -0
  90. torchmil-0.0.1/torchmil/models/prob_smooth_abmil.py +256 -0
  91. torchmil-0.0.1/torchmil/models/setmil.py +425 -0
  92. torchmil-0.0.1/torchmil/models/sm_abmil.py +166 -0
  93. torchmil-0.0.1/torchmil/models/sm_transformer_abmil.py +202 -0
  94. torchmil-0.0.1/torchmil/models/transformer_abmil.py +159 -0
  95. torchmil-0.0.1/torchmil/models/transformer_prob_smooth_abmil.py +231 -0
  96. torchmil-0.0.1/torchmil/models/transmil.py +237 -0
  97. torchmil-0.0.1/torchmil/nn/__init__.py +44 -0
  98. torchmil-0.0.1/torchmil/nn/attention/__init__.py +8 -0
  99. torchmil-0.0.1/torchmil/nn/attention/attention_pool.py +111 -0
  100. torchmil-0.0.1/torchmil/nn/attention/irpe_multihead_self_attention.py +232 -0
  101. torchmil-0.0.1/torchmil/nn/attention/multihead_cross_attention.py +144 -0
  102. torchmil-0.0.1/torchmil/nn/attention/multihead_self_attention.py +151 -0
  103. torchmil-0.0.1/torchmil/nn/attention/nystrom_attention.py +199 -0
  104. torchmil-0.0.1/torchmil/nn/attention/prob_smooth_attention_pool.py +232 -0
  105. torchmil-0.0.1/torchmil/nn/attention/sm_attention_pool.py +153 -0
  106. torchmil-0.0.1/torchmil/nn/gnns/__init__.py +3 -0
  107. torchmil-0.0.1/torchmil/nn/gnns/deepgcn.py +64 -0
  108. torchmil-0.0.1/torchmil/nn/gnns/dense_mincut_pool.py +103 -0
  109. torchmil-0.0.1/torchmil/nn/gnns/gcn_conv.py +80 -0
  110. torchmil-0.0.1/torchmil/nn/gnns/gnn_identity.py +11 -0
  111. torchmil-0.0.1/torchmil/nn/irpe.py +912 -0
  112. torchmil-0.0.1/torchmil/nn/max_pool.py +45 -0
  113. torchmil-0.0.1/torchmil/nn/mean_pool.py +46 -0
  114. torchmil-0.0.1/torchmil/nn/relprop.py +558 -0
  115. torchmil-0.0.1/torchmil/nn/sm.py +207 -0
  116. torchmil-0.0.1/torchmil/nn/transformers/__init__.py +31 -0
  117. torchmil-0.0.1/torchmil/nn/transformers/conventional_transformer.py +171 -0
  118. torchmil-0.0.1/torchmil/nn/transformers/encoder.py +68 -0
  119. torchmil-0.0.1/torchmil/nn/transformers/irpe_transformer.py +219 -0
  120. torchmil-0.0.1/torchmil/nn/transformers/layer.py +111 -0
  121. torchmil-0.0.1/torchmil/nn/transformers/nystrom_transformer.py +175 -0
  122. torchmil-0.0.1/torchmil/nn/transformers/sm_transformer.py +247 -0
  123. torchmil-0.0.1/torchmil/nn/transformers/t2t.py +78 -0
  124. torchmil-0.0.1/torchmil/nn/utils.py +281 -0
  125. torchmil-0.0.1/torchmil/utils/__init__.py +19 -0
  126. torchmil-0.0.1/torchmil/utils/annealing_scheduler.py +95 -0
  127. torchmil-0.0.1/torchmil/utils/common.py +24 -0
  128. torchmil-0.0.1/torchmil/utils/graph_utils.py +157 -0
  129. torchmil-0.0.1/torchmil/utils/trainer.py +287 -0
  130. torchmil-0.0.1/torchmil/visualize/__init__.py +2 -0
  131. torchmil-0.0.1/torchmil/visualize/vis_ctscan.py +109 -0
  132. torchmil-0.0.1/torchmil/visualize/vis_wsi.py +141 -0
  133. torchmil-0.0.1/torchmil.egg-info/PKG-INFO +107 -0
  134. torchmil-0.0.1/torchmil.egg-info/SOURCES.txt +135 -0
  135. torchmil-0.0.1/torchmil.egg-info/dependency_links.txt +1 -0
  136. torchmil-0.0.1/torchmil.egg-info/requires.txt +21 -0
  137. torchmil-0.0.1/torchmil.egg-info/top_level.txt +7 -0
torchmil-0.0.1/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,107 @@
1
+ Metadata-Version: 2.4
2
+ Name: torchmil
3
+ Version: 0.0.1
4
+ Summary: Deep Multiple Instance Learning library for Pytorch
5
+ Author-email: Francisco Miguel Castro-Macías <francastro8b@gmail.com>
6
+ License-Expression: Apache-2.0
7
+ Project-URL: homepage, https://franblueee.github.io/torchmil/
8
+ Project-URL: documentation, https://franblueee.github.io/torchmil/api/
9
+ Project-URL: repository, https://github.com/Franblueee/torchmil
10
+ Project-URL: issues, https://github.com/Franblueee/torchmil/issues
11
+ Keywords: deep-learning,pytorch,multiple-instance-learning,multi-instance-learning,mil
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: Intended Audience :: Information Technology
14
+ Classifier: Intended Audience :: Science/Research
15
+ Classifier: Natural Language :: English
16
+ Classifier: Programming Language :: Python :: 3
17
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
18
+ Classifier: Topic :: Scientific/Engineering :: Information Analysis
19
+ Classifier: Topic :: Scientific/Engineering :: Mathematics
20
+ Requires-Python: >=3.10
21
+ Description-Content-Type: text/markdown
22
+ License-File: LICENSE
23
+ Requires-Dist: torch
24
+ Requires-Dist: tensordict
25
+ Requires-Dist: torchmetrics
26
+ Requires-Dist: einops
27
+ Requires-Dist: scipy
28
+ Requires-Dist: tqdm
29
+ Requires-Dist: numpy
30
+ Provides-Extra: docs
31
+ Requires-Dist: mkdocs; extra == "docs"
32
+ Requires-Dist: mkdocs-material; extra == "docs"
33
+ Requires-Dist: mknotebooks; extra == "docs"
34
+ Requires-Dist: mkdocstrings; extra == "docs"
35
+ Requires-Dist: mkdocstrings-python; extra == "docs"
36
+ Requires-Dist: mkdocs-exclude; extra == "docs"
37
+ Provides-Extra: tests
38
+ Requires-Dist: torchmil; extra == "tests"
39
+ Requires-Dist: pytest; extra == "tests"
40
+ Requires-Dist: pytest-cov; extra == "tests"
41
+ Requires-Dist: pandas; extra == "tests"
42
+ Dynamic: license-file
43
+
44
+ # torchmil
45
+
46
+ **torchmil** is a [PyTorch](https://pytorch.org/)-based library for deep Multiple Instance Learning (MIL).
47
+ It provides a simple, flexible, and extensible framework for working with MIL models and data.
48
+
49
+ It includes:
50
+
51
+ - A collection of popular [MIL models](https://franblueee.github.io/torchmil/api/models/).
52
+ - Different [PyTorch modules](https://franblueee.github.io/torchmil/api/nn/) frequently used in MIL models.
53
+ - Handy tools to deal with [MIL data](https://franblueee.github.io/torchmil/api/data/).
54
+ - A collection of popular [MIL datasets](https://franblueee.github.io/torchmil/api/datasets/).
55
+
56
+ ## Installation
57
+
58
+ ```bash
59
+ pip install torchmil
60
+ ```
61
+
62
+ ## Quick start
63
+
64
+ You can load a MIL dataset and train a MIL model in just a few lines of code:
65
+
66
+ ```python
67
+ from torchmil.datasets import Camelyon16MIL
68
+ from torchmil.models import ABMIL
69
+ from torchmil.utils import Trainer
70
+ from torchmil.data import collate_fn
71
+ from torch.utils.data import DataLoader
72
+
73
+ # Load the Camelyon16 dataset
74
+ dataset = Camelyon16MIL(root='data', features='UNI')
75
+ dataloader = DataLoader(dataset, batch_size=4, shuffle=True, collate_fn=collate_fn)
76
+
77
+ # Instantiate the ABMIL model and optimizer
78
+ model = ABMIL(in_shape=(2048,), criterion=torch.nn.BCEWithLogitsLoss()) # each model has its own criterion
79
+ optimizer = torch.optim.Adam(model.parameters(), lr=1e-4)
80
+
81
+ # Instantiate the Trainer
82
+ trainer = Trainer(model, optimizer, device='cuda')
83
+
84
+ # Train the model
85
+ trainer.train(dataloader, epochs=10)
86
+
87
+ # Save the model
88
+ torch.save(model.state_dict(), 'model.pth')
89
+ ```
90
+
91
+ ## Next steps
92
+
93
+ You can take a look at the [examples](https://franblueee.github.io/torchmil/examples/) to see how to use **torchmil** in practice.
94
+ To see the full list of available models, datasets, and modules, check the [API reference](https://franblueee.github.io/torchmil/api/).
95
+
96
+ ## Citation
97
+
98
+ If you find this library useful, please consider citing it:
99
+
100
+ ```bibtex
101
+ @misc{torchmil,
102
+ author = {Castro-Mac{\'\i}as, Francisco M and S{\'a}ez-Maldonado, Francisco Javier and Morales Alvarez, Pablo and Molina, Rafael},
103
+ title = {torchmil: A PyTorch-based library for deep Multiple Instance Learning},
104
+ year = {2025},
105
+ howpublished = {\url{https://franblueee.github.io/torchmil/}}
106
+ }
107
+ ```
@@ -0,0 +1,64 @@
1
+ # torchmil
2
+
3
+ **torchmil** is a [PyTorch](https://pytorch.org/)-based library for deep Multiple Instance Learning (MIL).
4
+ It provides a simple, flexible, and extensible framework for working with MIL models and data.
5
+
6
+ It includes:
7
+
8
+ - A collection of popular [MIL models](https://franblueee.github.io/torchmil/api/models/).
9
+ - Different [PyTorch modules](https://franblueee.github.io/torchmil/api/nn/) frequently used in MIL models.
10
+ - Handy tools to deal with [MIL data](https://franblueee.github.io/torchmil/api/data/).
11
+ - A collection of popular [MIL datasets](https://franblueee.github.io/torchmil/api/datasets/).
12
+
13
+ ## Installation
14
+
15
+ ```bash
16
+ pip install torchmil
17
+ ```
18
+
19
+ ## Quick start
20
+
21
+ You can load a MIL dataset and train a MIL model in just a few lines of code:
22
+
23
+ ```python
24
+ from torchmil.datasets import Camelyon16MIL
25
+ from torchmil.models import ABMIL
26
+ from torchmil.utils import Trainer
27
+ from torchmil.data import collate_fn
28
+ from torch.utils.data import DataLoader
29
+
30
+ # Load the Camelyon16 dataset
31
+ dataset = Camelyon16MIL(root='data', features='UNI')
32
+ dataloader = DataLoader(dataset, batch_size=4, shuffle=True, collate_fn=collate_fn)
33
+
34
+ # Instantiate the ABMIL model and optimizer
35
+ model = ABMIL(in_shape=(2048,), criterion=torch.nn.BCEWithLogitsLoss()) # each model has its own criterion
36
+ optimizer = torch.optim.Adam(model.parameters(), lr=1e-4)
37
+
38
+ # Instantiate the Trainer
39
+ trainer = Trainer(model, optimizer, device='cuda')
40
+
41
+ # Train the model
42
+ trainer.train(dataloader, epochs=10)
43
+
44
+ # Save the model
45
+ torch.save(model.state_dict(), 'model.pth')
46
+ ```
47
+
48
+ ## Next steps
49
+
50
+ You can take a look at the [examples](https://franblueee.github.io/torchmil/examples/) to see how to use **torchmil** in practice.
51
+ To see the full list of available models, datasets, and modules, check the [API reference](https://franblueee.github.io/torchmil/api/).
52
+
53
+ ## Citation
54
+
55
+ If you find this library useful, please consider citing it:
56
+
57
+ ```bibtex
58
+ @misc{torchmil,
59
+ author = {Castro-Mac{\'\i}as, Francisco M and S{\'a}ez-Maldonado, Francisco Javier and Morales Alvarez, Pablo and Molina, Rafael},
60
+ title = {torchmil: A PyTorch-based library for deep Multiple Instance Learning},
61
+ year = {2025},
62
+ howpublished = {\url{https://franblueee.github.io/torchmil/}}
63
+ }
64
+ ```
@@ -0,0 +1,72 @@
1
+ [build-system]
2
+ requires = ["setuptools", "wheel"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "torchmil"
7
+ version = "0.0.1"
8
+ authors = [
9
+ { name="Francisco Miguel Castro-Macías", email="francastro8b@gmail.com" },
10
+ ]
11
+ description = "Deep Multiple Instance Learning library for Pytorch"
12
+ readme = "README.md"
13
+ requires-python = ">=3.10"
14
+ license = "Apache-2.0"
15
+ keywords=[
16
+ "deep-learning",
17
+ "pytorch",
18
+ "multiple-instance-learning",
19
+ "multi-instance-learning",
20
+ "mil",
21
+ ]
22
+ classifiers = [
23
+ "Intended Audience :: Developers",
24
+ "Intended Audience :: Information Technology",
25
+ "Intended Audience :: Science/Research",
26
+ "Natural Language :: English",
27
+ "Programming Language :: Python :: 3",
28
+ "Topic :: Scientific/Engineering :: Artificial Intelligence",
29
+ "Topic :: Scientific/Engineering :: Information Analysis",
30
+ "Topic :: Scientific/Engineering :: Mathematics",
31
+ ]
32
+ dependencies = [
33
+ "torch",
34
+ "tensordict",
35
+ "torchmetrics",
36
+ "einops",
37
+ "scipy",
38
+ "tqdm",
39
+ "numpy",
40
+ ]
41
+
42
+ [project.optional-dependencies]
43
+ docs = [
44
+ "mkdocs",
45
+ "mkdocs-material",
46
+ "mknotebooks",
47
+ "mkdocstrings",
48
+ "mkdocstrings-python",
49
+ "mkdocs-exclude",
50
+ ]
51
+ tests = [
52
+ "torchmil",
53
+ "pytest",
54
+ "pytest-cov",
55
+ "pandas",
56
+ ]
57
+
58
+ [project.urls]
59
+ homepage = "https://franblueee.github.io/torchmil/"
60
+ documentation = "https://franblueee.github.io/torchmil/api/"
61
+ repository = "https://github.com/Franblueee/torchmil"
62
+ issues = "https://github.com/Franblueee/torchmil/issues"
63
+
64
+ [tool.flake8]
65
+ max-line-length = 100
66
+ exclude = [".ipynb", "build", ".pytest_cache"]
67
+
68
+ [tool.pytest.ini_options]
69
+ testpaths = ["tests"]
70
+
71
+ [tool.setuptools.packages.find]
72
+ where = ["."]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,67 @@
1
+ import pytest
2
+ import torch
3
+ from torchmil.data.collate import pad_tensors, collate_fn
4
+ from tensordict import TensorDict
5
+
6
+ def test_pad_tensors_single_tensor():
7
+ # Test the pad_tensors function with a single tensor.
8
+ # Ensure that the tensor is padded correctly and the mask is generated as expected.
9
+ tensor = torch.tensor([[1, 2], [3, 4]])
10
+ padded_tensor, mask = pad_tensors([tensor], padding_value=0)
11
+
12
+ assert padded_tensor.shape == (1, 2, 2)
13
+ assert torch.equal(padded_tensor[0], tensor)
14
+ assert torch.equal(mask, torch.tensor([[1, 1]], dtype=torch.uint8))
15
+
16
+ def test_pad_tensors_multiple_tensors():
17
+ # Test the pad_tensors function with multiple tensors of varying sizes.
18
+ # Verify that all tensors are padded to the same size and the mask is correct.
19
+ tensor1 = torch.tensor([[1, 2], [3, 4]])
20
+ tensor2 = torch.tensor([[5, 6]])
21
+ padded_tensor, mask = pad_tensors([tensor1, tensor2], padding_value=0)
22
+
23
+ assert padded_tensor.shape == (2, 2, 2)
24
+ assert torch.equal(padded_tensor[0], tensor1)
25
+ assert torch.equal(padded_tensor[1], torch.tensor([[5, 6], [0, 0]]))
26
+ assert torch.equal(mask, torch.tensor([[1, 1], [1, 0]], dtype=torch.uint8))
27
+
28
+ def test_collate_fn_dense_tensors():
29
+ # Test the collate_fn function with dense tensors.
30
+ # Check that the tensors are collated correctly into a TensorDict with proper padding and masks.
31
+ batch_list = [
32
+ {"data": torch.tensor([[1, 2], [3, 4]])},
33
+ {"data": torch.tensor([[5, 6]])}
34
+ ]
35
+ result = collate_fn(batch_list, sparse=False)
36
+
37
+ assert isinstance(result, TensorDict)
38
+ assert "data" in result
39
+ assert "mask" in result
40
+ assert result["data"].shape == (2, 2, 2)
41
+ assert torch.equal(result["data"][0], torch.tensor([[1, 2], [3, 4]]))
42
+ assert torch.equal(result["data"][1], torch.tensor([[5, 6], [0, 0]]))
43
+ assert torch.equal(result["mask"], torch.tensor([[1, 1], [1, 0]], dtype=torch.uint8))
44
+
45
+ def test_collate_fn_sparse_tensors():
46
+ # Test the collate_fn function with sparse tensors.
47
+ # Ensure that sparse tensors are collated correctly into a TensorDict.
48
+ sparse_tensor1 = torch.sparse_coo_tensor(
49
+ indices=[[0, 1], [0, 1]],
50
+ values=[1, 2],
51
+ size=(2, 2)
52
+ )
53
+ sparse_tensor2 = torch.sparse_coo_tensor(
54
+ indices=[[0], [0]],
55
+ values=[3],
56
+ size=(2, 2)
57
+ )
58
+ batch_list = [
59
+ {"data": sparse_tensor1},
60
+ {"data": sparse_tensor2}
61
+ ]
62
+ result = collate_fn(batch_list, sparse=True)
63
+
64
+ assert isinstance(result, TensorDict)
65
+ assert "data" in result
66
+ assert result["data"].is_sparse
67
+ assert result["data"].shape == (2, 2, 2)