mmhsi 0.1.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 (59) hide show
  1. mmhsi-0.1.0/LICENSE +176 -0
  2. mmhsi-0.1.0/PKG-INFO +159 -0
  3. mmhsi-0.1.0/README.md +126 -0
  4. mmhsi-0.1.0/mmhsi/__init__.py +81 -0
  5. mmhsi-0.1.0/mmhsi/cli/__init__.py +58 -0
  6. mmhsi-0.1.0/mmhsi/cli/download.py +154 -0
  7. mmhsi-0.1.0/mmhsi/cli/test.py +52 -0
  8. mmhsi-0.1.0/mmhsi/cli/train.py +98 -0
  9. mmhsi-0.1.0/mmhsi/configs/_base_/datasets/indian_pines_patch15.py +62 -0
  10. mmhsi-0.1.0/mmhsi/configs/_base_/datasets/pavia_u_patch13.py +62 -0
  11. mmhsi-0.1.0/mmhsi/configs/_base_/datasets/salinas_patch15.py +62 -0
  12. mmhsi-0.1.0/mmhsi/configs/_base_/default_runtime.py +33 -0
  13. mmhsi-0.1.0/mmhsi/configs/_base_/schedules/schedule_100e.py +25 -0
  14. mmhsi-0.1.0/mmhsi/configs/hybridsn/hybridsn_patch13_pavia_u.py +26 -0
  15. mmhsi-0.1.0/mmhsi/configs/hybridsn/hybridsn_patch15_indian_pines.py +26 -0
  16. mmhsi-0.1.0/mmhsi/configs/hybridsn/hybridsn_patch15_salinas.py +26 -0
  17. mmhsi-0.1.0/mmhsi/datasets/__init__.py +27 -0
  18. mmhsi-0.1.0/mmhsi/datasets/base_hsi_dataset.py +256 -0
  19. mmhsi-0.1.0/mmhsi/datasets/benchmark_datasets.py +198 -0
  20. mmhsi-0.1.0/mmhsi/datasets/transforms/__init__.py +17 -0
  21. mmhsi-0.1.0/mmhsi/datasets/transforms/augmentations.py +138 -0
  22. mmhsi-0.1.0/mmhsi/evaluation/__init__.py +5 -0
  23. mmhsi-0.1.0/mmhsi/evaluation/metrics/__init__.py +5 -0
  24. mmhsi-0.1.0/mmhsi/evaluation/metrics/hsi_metric.py +142 -0
  25. mmhsi-0.1.0/mmhsi/models/__init__.py +21 -0
  26. mmhsi-0.1.0/mmhsi/models/attentions/__init__.py +5 -0
  27. mmhsi-0.1.0/mmhsi/models/attentions/spectral_attention.py +77 -0
  28. mmhsi-0.1.0/mmhsi/models/backbones/__init__.py +5 -0
  29. mmhsi-0.1.0/mmhsi/models/backbones/hybrid_sn.py +126 -0
  30. mmhsi-0.1.0/mmhsi/models/classifiers/__init__.py +5 -0
  31. mmhsi-0.1.0/mmhsi/models/classifiers/hsi_classifier.py +127 -0
  32. mmhsi-0.1.0/mmhsi/models/data_preprocessors/__init__.py +5 -0
  33. mmhsi-0.1.0/mmhsi/models/data_preprocessors/hsi_preprocessor.py +74 -0
  34. mmhsi-0.1.0/mmhsi/models/heads/__init__.py +5 -0
  35. mmhsi-0.1.0/mmhsi/models/heads/cls_head.py +98 -0
  36. mmhsi-0.1.0/mmhsi/models/losses/__init__.py +6 -0
  37. mmhsi-0.1.0/mmhsi/models/losses/focal_loss.py +65 -0
  38. mmhsi-0.1.0/mmhsi/models/losses/spectral_losses.py +60 -0
  39. mmhsi-0.1.0/mmhsi/models/necks/__init__.py +5 -0
  40. mmhsi-0.1.0/mmhsi/models/necks/spectral_reduction.py +44 -0
  41. mmhsi-0.1.0/mmhsi/registry.py +87 -0
  42. mmhsi-0.1.0/mmhsi/structures/__init__.py +5 -0
  43. mmhsi-0.1.0/mmhsi/structures/data_sample.py +78 -0
  44. mmhsi-0.1.0/mmhsi/utils/__init__.py +5 -0
  45. mmhsi-0.1.0/mmhsi/utils/checkpoint.py +39 -0
  46. mmhsi-0.1.0/mmhsi/version.py +3 -0
  47. mmhsi-0.1.0/mmhsi.egg-info/PKG-INFO +159 -0
  48. mmhsi-0.1.0/mmhsi.egg-info/SOURCES.txt +57 -0
  49. mmhsi-0.1.0/mmhsi.egg-info/dependency_links.txt +1 -0
  50. mmhsi-0.1.0/mmhsi.egg-info/entry_points.txt +4 -0
  51. mmhsi-0.1.0/mmhsi.egg-info/requires.txt +11 -0
  52. mmhsi-0.1.0/mmhsi.egg-info/top_level.txt +1 -0
  53. mmhsi-0.1.0/pyproject.toml +62 -0
  54. mmhsi-0.1.0/setup.cfg +4 -0
  55. mmhsi-0.1.0/tests/test_cli.py +72 -0
  56. mmhsi-0.1.0/tests/test_components.py +72 -0
  57. mmhsi-0.1.0/tests/test_dataset.py +53 -0
  58. mmhsi-0.1.0/tests/test_metrics.py +74 -0
  59. mmhsi-0.1.0/tests/test_models.py +65 -0
mmhsi-0.1.0/LICENSE ADDED
@@ -0,0 +1,176 @@
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 exemplary 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
mmhsi-0.1.0/PKG-INFO ADDED
@@ -0,0 +1,159 @@
1
+ Metadata-Version: 2.4
2
+ Name: mmhsi
3
+ Version: 0.1.0
4
+ Summary: OpenMMLab-style Hyperspectral Imaging Toolbox based on MMEngine
5
+ Author-email: Jayant <jayant.07124@gmail.com>
6
+ License: Apache-2.0
7
+ Project-URL: Homepage, https://github.com/Jayant71/mmhsi
8
+ Project-URL: Repository, https://github.com/Jayant71/mmhsi
9
+ Project-URL: Bug Tracker, https://github.com/Jayant71/mmhsi/issues
10
+ Keywords: hyperspectral,remote-sensing,deep-learning,pytorch,mmengine,openmmlab
11
+ Classifier: Development Status :: 3 - Alpha
12
+ Classifier: Intended Audience :: Science/Research
13
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
14
+ Classifier: Topic :: Scientific/Engineering :: Image Processing
15
+ Classifier: Programming Language :: Python :: 3
16
+ Classifier: Programming Language :: Python :: 3.10
17
+ Classifier: Programming Language :: Python :: 3.11
18
+ Classifier: License :: OSI Approved :: Apache Software License
19
+ Classifier: Operating System :: OS Independent
20
+ Requires-Python: >=3.10
21
+ Description-Content-Type: text/markdown
22
+ License-File: LICENSE
23
+ Requires-Dist: torch>=2.1.0
24
+ Requires-Dist: mmengine>=0.10.0
25
+ Requires-Dist: numpy>=1.24.0
26
+ Requires-Dist: scipy>=1.10.0
27
+ Requires-Dist: scikit-learn>=1.2.0
28
+ Provides-Extra: test
29
+ Requires-Dist: pytest>=7.0.0; extra == "test"
30
+ Provides-Extra: all
31
+ Requires-Dist: mmhsi[test]; extra == "all"
32
+ Dynamic: license-file
33
+
34
+ # MMHSI: Hyperspectral Imaging Toolbox
35
+
36
+ [![PyPI](https://img.shields.io/pypi/v/mmhsi.svg)](https://pypi.org/project/mmhsi/)
37
+ [![Python](https://img.shields.io/badge/python-3.10%20%7C%203.11-blue.svg)](https://www.python.org/)
38
+ [![MMEngine](https://img.shields.io/badge/MMEngine-0.10-orange.svg)](https://github.com/open-mmlab/mmengine)
39
+ [![License](https://img.shields.io/badge/License-Apache%202.0-green.svg)](https://opensource.org/licenses/Apache-2.0)
40
+
41
+ **MMHSI** is an open-source, modular hyperspectral imaging (HSI) research toolbox built on top of [MMEngine](https://github.com/open-mmlab/mmengine) (OpenMMLab 2.0).
42
+
43
+ Designed for PhD and academic research, `mmhsi` provides a decoupled, modular architecture for hyperspectral pixel/patch classification and spectral-spatial feature extraction.
44
+
45
+ > **Status:** alpha (`0.1.x`). APIs and config names may still change between minor versions.
46
+
47
+ ---
48
+
49
+ ## 🚀 Key Features
50
+
51
+ - **Built on MMEngine**: Clean configuration management, robust hooks, visualizers, and unified training loops.
52
+ - **HSI-Centric Registry**: Modular components for Backbones, Spatial-Spectral Attentions, Necks, Classification Heads, and Losses.
53
+ - **Hydra-Style Run Logging**: Automatic timestamped directory isolation prevents overwriting past experiments (`work_dirs/<config>/run_YYYYMMDD_HHMMSS/`).
54
+ - **Benchmark Datasets**: Built-in loaders for **Indian Pines (IP)**, **Salinas (SA)**, and **Pavia University (PaviaU)** with background masking and seeded, per-class stratified splits.
55
+ - **Evaluation**: Overall Accuracy, Average Accuracy, Cohen's Kappa, and per-class accuracy.
56
+ - **Extensible Architecture**: Ships with the HybridSN baseline; add your own backbones, attention modules, and losses by registering a class, without touching the training loop.
57
+
58
+ ---
59
+
60
+ ## 🛠️ Installation
61
+
62
+ MMHSI needs Python 3.10 or 3.11 and PyTorch.
63
+
64
+ ### 1. Install PyTorch
65
+ Install the PyTorch build that matches your hardware by following the [official instructions](https://pytorch.org/get-started/locally/). For example, for CUDA 12.1:
66
+
67
+ ```bash
68
+ pip install torch --index-url https://download.pytorch.org/whl/cu121
69
+ ```
70
+
71
+ If you skip this step, `pip` installs the default PyTorch build for your platform.
72
+
73
+ ### 2. Install MMHSI
74
+
75
+ ```bash
76
+ pip install mmhsi
77
+ ```
78
+
79
+ ### From source (for development)
80
+
81
+ ```bash
82
+ git clone https://github.com/Jayant71/mmhsi.git
83
+ cd mmhsi
84
+ pip install -e ".[test]"
85
+ ```
86
+
87
+ ---
88
+
89
+ ## ⚡ Quick Start
90
+
91
+ Installing the package adds three commands: `mmhsi-download`, `mmhsi-train`, and `mmhsi-test`.
92
+
93
+ ### 1. Download a Benchmark Dataset
94
+ ```bash
95
+ # indian_pines, salinas, pavia_u, or all. Files go to ./data/ by default.
96
+ mmhsi-download --dataset indian_pines
97
+ ```
98
+
99
+ ### 2. Train a Model
100
+ ```bash
101
+ # List the bundled configs
102
+ mmhsi-train --list-configs
103
+
104
+ # Train HybridSN on Indian Pines (creates an isolated run folder under ./work_dirs/)
105
+ mmhsi-train hybridsn_patch15_indian_pines --run-name baseline_100e
106
+ ```
107
+
108
+ Bundled configs expect the dataset under `./data/`, relative to the directory you run the command from. You can also pass a path to your own config file instead of a bundled name.
109
+
110
+ ### 3. Evaluate a Checkpoint
111
+ ```bash
112
+ mmhsi-test hybridsn_patch15_indian_pines \
113
+ work_dirs/hybridsn_patch15_indian_pines/run_<TIMESTAMP>_baseline_100e/best_hsi_OA_epoch_<N>.pth
114
+ ```
115
+
116
+ ### Override Config Values from the Command Line
117
+ ```bash
118
+ mmhsi-train hybridsn_patch15_indian_pines \
119
+ --cfg-options optim_wrapper.optimizer.lr=5e-4 train_dataloader.batch_size=128
120
+ ```
121
+
122
+ In a source checkout, the equivalent scripts are also available as `python tools/train.py`, `python tools/test.py`, and `python tools/download_benchmarks.py`.
123
+
124
+ ---
125
+
126
+ ## 🧩 Using MMHSI as a Library
127
+
128
+ ```python
129
+ import mmhsi # registers all MMHSI components
130
+ from mmengine.config import Config
131
+ from mmengine.runner import Runner
132
+ from mmhsi.cli import resolve_config
133
+
134
+ cfg = Config.fromfile(resolve_config("hybridsn_patch15_indian_pines"))
135
+ cfg.work_dir = "work_dirs/my_experiment"
136
+ Runner.from_cfg(cfg).train()
137
+ ```
138
+
139
+ ---
140
+
141
+ ## 📦 Bundled Configs
142
+
143
+ | Config | Model | Dataset | Patch |
144
+ | :--- | :--- | :--- | :---: |
145
+ | `hybridsn_patch15_indian_pines` | HybridSN | Indian Pines | 15 |
146
+ | `hybridsn_patch15_salinas` | HybridSN | Salinas | 15 |
147
+ | `hybridsn_patch13_pavia_u` | HybridSN | Pavia University | 13 |
148
+
149
+ ---
150
+
151
+ ## 🔐 A Note on Checkpoints
152
+
153
+ PyTorch 2.6 and later refuse to load MMEngine checkpoints by default, because they contain training state beyond the model weights. Importing `mmhsi` restores the earlier loading behavior for local checkpoint files. **Only load checkpoints from sources you trust**: loading a checkpoint runs Python's unpickler, which can execute code.
154
+
155
+ ---
156
+
157
+ ## 📄 License
158
+
159
+ Apache License 2.0. See [LICENSE](LICENSE).
mmhsi-0.1.0/README.md ADDED
@@ -0,0 +1,126 @@
1
+ # MMHSI: Hyperspectral Imaging Toolbox
2
+
3
+ [![PyPI](https://img.shields.io/pypi/v/mmhsi.svg)](https://pypi.org/project/mmhsi/)
4
+ [![Python](https://img.shields.io/badge/python-3.10%20%7C%203.11-blue.svg)](https://www.python.org/)
5
+ [![MMEngine](https://img.shields.io/badge/MMEngine-0.10-orange.svg)](https://github.com/open-mmlab/mmengine)
6
+ [![License](https://img.shields.io/badge/License-Apache%202.0-green.svg)](https://opensource.org/licenses/Apache-2.0)
7
+
8
+ **MMHSI** is an open-source, modular hyperspectral imaging (HSI) research toolbox built on top of [MMEngine](https://github.com/open-mmlab/mmengine) (OpenMMLab 2.0).
9
+
10
+ Designed for PhD and academic research, `mmhsi` provides a decoupled, modular architecture for hyperspectral pixel/patch classification and spectral-spatial feature extraction.
11
+
12
+ > **Status:** alpha (`0.1.x`). APIs and config names may still change between minor versions.
13
+
14
+ ---
15
+
16
+ ## 🚀 Key Features
17
+
18
+ - **Built on MMEngine**: Clean configuration management, robust hooks, visualizers, and unified training loops.
19
+ - **HSI-Centric Registry**: Modular components for Backbones, Spatial-Spectral Attentions, Necks, Classification Heads, and Losses.
20
+ - **Hydra-Style Run Logging**: Automatic timestamped directory isolation prevents overwriting past experiments (`work_dirs/<config>/run_YYYYMMDD_HHMMSS/`).
21
+ - **Benchmark Datasets**: Built-in loaders for **Indian Pines (IP)**, **Salinas (SA)**, and **Pavia University (PaviaU)** with background masking and seeded, per-class stratified splits.
22
+ - **Evaluation**: Overall Accuracy, Average Accuracy, Cohen's Kappa, and per-class accuracy.
23
+ - **Extensible Architecture**: Ships with the HybridSN baseline; add your own backbones, attention modules, and losses by registering a class, without touching the training loop.
24
+
25
+ ---
26
+
27
+ ## 🛠️ Installation
28
+
29
+ MMHSI needs Python 3.10 or 3.11 and PyTorch.
30
+
31
+ ### 1. Install PyTorch
32
+ Install the PyTorch build that matches your hardware by following the [official instructions](https://pytorch.org/get-started/locally/). For example, for CUDA 12.1:
33
+
34
+ ```bash
35
+ pip install torch --index-url https://download.pytorch.org/whl/cu121
36
+ ```
37
+
38
+ If you skip this step, `pip` installs the default PyTorch build for your platform.
39
+
40
+ ### 2. Install MMHSI
41
+
42
+ ```bash
43
+ pip install mmhsi
44
+ ```
45
+
46
+ ### From source (for development)
47
+
48
+ ```bash
49
+ git clone https://github.com/Jayant71/mmhsi.git
50
+ cd mmhsi
51
+ pip install -e ".[test]"
52
+ ```
53
+
54
+ ---
55
+
56
+ ## ⚡ Quick Start
57
+
58
+ Installing the package adds three commands: `mmhsi-download`, `mmhsi-train`, and `mmhsi-test`.
59
+
60
+ ### 1. Download a Benchmark Dataset
61
+ ```bash
62
+ # indian_pines, salinas, pavia_u, or all. Files go to ./data/ by default.
63
+ mmhsi-download --dataset indian_pines
64
+ ```
65
+
66
+ ### 2. Train a Model
67
+ ```bash
68
+ # List the bundled configs
69
+ mmhsi-train --list-configs
70
+
71
+ # Train HybridSN on Indian Pines (creates an isolated run folder under ./work_dirs/)
72
+ mmhsi-train hybridsn_patch15_indian_pines --run-name baseline_100e
73
+ ```
74
+
75
+ Bundled configs expect the dataset under `./data/`, relative to the directory you run the command from. You can also pass a path to your own config file instead of a bundled name.
76
+
77
+ ### 3. Evaluate a Checkpoint
78
+ ```bash
79
+ mmhsi-test hybridsn_patch15_indian_pines \
80
+ work_dirs/hybridsn_patch15_indian_pines/run_<TIMESTAMP>_baseline_100e/best_hsi_OA_epoch_<N>.pth
81
+ ```
82
+
83
+ ### Override Config Values from the Command Line
84
+ ```bash
85
+ mmhsi-train hybridsn_patch15_indian_pines \
86
+ --cfg-options optim_wrapper.optimizer.lr=5e-4 train_dataloader.batch_size=128
87
+ ```
88
+
89
+ In a source checkout, the equivalent scripts are also available as `python tools/train.py`, `python tools/test.py`, and `python tools/download_benchmarks.py`.
90
+
91
+ ---
92
+
93
+ ## 🧩 Using MMHSI as a Library
94
+
95
+ ```python
96
+ import mmhsi # registers all MMHSI components
97
+ from mmengine.config import Config
98
+ from mmengine.runner import Runner
99
+ from mmhsi.cli import resolve_config
100
+
101
+ cfg = Config.fromfile(resolve_config("hybridsn_patch15_indian_pines"))
102
+ cfg.work_dir = "work_dirs/my_experiment"
103
+ Runner.from_cfg(cfg).train()
104
+ ```
105
+
106
+ ---
107
+
108
+ ## 📦 Bundled Configs
109
+
110
+ | Config | Model | Dataset | Patch |
111
+ | :--- | :--- | :--- | :---: |
112
+ | `hybridsn_patch15_indian_pines` | HybridSN | Indian Pines | 15 |
113
+ | `hybridsn_patch15_salinas` | HybridSN | Salinas | 15 |
114
+ | `hybridsn_patch13_pavia_u` | HybridSN | Pavia University | 13 |
115
+
116
+ ---
117
+
118
+ ## 🔐 A Note on Checkpoints
119
+
120
+ PyTorch 2.6 and later refuse to load MMEngine checkpoints by default, because they contain training state beyond the model weights. Importing `mmhsi` restores the earlier loading behavior for local checkpoint files. **Only load checkpoints from sources you trust**: loading a checkpoint runs Python's unpickler, which can execute code.
121
+
122
+ ---
123
+
124
+ ## 📄 License
125
+
126
+ Apache License 2.0. See [LICENSE](LICENSE).
@@ -0,0 +1,81 @@
1
+ """MMHSI: OpenMMLab-style Hyperspectral Imaging Toolbox based on MMEngine."""
2
+
3
+ from .datasets import (
4
+ BaseHSIDataset,
5
+ IndianPinesDataset,
6
+ PackHSIInputs,
7
+ PaviaUDataset,
8
+ RandomBandDropout,
9
+ RandomFlipHSI,
10
+ RandomSpectralNoise,
11
+ SalinasDataset,
12
+ )
13
+ from .evaluation import HSIAccuracy
14
+ from .models import (
15
+ HSIClsHead,
16
+ HSIDataPreprocessor,
17
+ HSIFocalLoss,
18
+ HSIPatchClassifier,
19
+ HybridSN,
20
+ SpatialSpectralCBAM,
21
+ SpectralAngleLoss,
22
+ SpectralAttention,
23
+ SpectralReductionNeck,
24
+ )
25
+ from .registry import (
26
+ DATA_SAMPLERS,
27
+ DATASETS,
28
+ HOOKS,
29
+ LOOPS,
30
+ METRICS,
31
+ MODELS,
32
+ OPTIM_WRAPPER_CONSTRUCTORS,
33
+ OPTIM_WRAPPERS,
34
+ OPTIMIZERS,
35
+ PARAM_SCHEDULERS,
36
+ RUNNERS,
37
+ TRANSFORMS,
38
+ VISUALIZERS,
39
+ )
40
+ from .structures import HSIDataSample
41
+ from .utils import register_checkpoint_loader
42
+ from .version import __version__
43
+
44
+ # Make MMEngine checkpoints loadable on PyTorch >= 2.6 (see mmhsi/utils/checkpoint.py)
45
+ register_checkpoint_loader()
46
+
47
+ __all__ = [
48
+ "DATASETS",
49
+ "DATA_SAMPLERS",
50
+ "HOOKS",
51
+ "LOOPS",
52
+ "METRICS",
53
+ "MODELS",
54
+ "OPTIMIZERS",
55
+ "OPTIM_WRAPPERS",
56
+ "OPTIM_WRAPPER_CONSTRUCTORS",
57
+ "PARAM_SCHEDULERS",
58
+ "RUNNERS",
59
+ "TRANSFORMS",
60
+ "VISUALIZERS",
61
+ "BaseHSIDataset",
62
+ "HSIAccuracy",
63
+ "HSIClsHead",
64
+ "HSIDataPreprocessor",
65
+ "HSIDataSample",
66
+ "HSIFocalLoss",
67
+ "HSIPatchClassifier",
68
+ "HybridSN",
69
+ "IndianPinesDataset",
70
+ "PackHSIInputs",
71
+ "PaviaUDataset",
72
+ "RandomBandDropout",
73
+ "RandomFlipHSI",
74
+ "RandomSpectralNoise",
75
+ "SalinasDataset",
76
+ "SpatialSpectralCBAM",
77
+ "SpectralAngleLoss",
78
+ "SpectralAttention",
79
+ "SpectralReductionNeck",
80
+ "__version__",
81
+ ]
@@ -0,0 +1,58 @@
1
+ """Command-line entry points: ``mmhsi-train``, ``mmhsi-test``, ``mmhsi-download``."""
2
+
3
+ import os.path as osp
4
+ from pathlib import Path
5
+
6
+ CONFIG_ROOT = Path(__file__).resolve().parent.parent / "configs"
7
+
8
+
9
+ def resolve_config(config: str) -> str:
10
+ """Return a config file path from a path or a bundled config name.
11
+
12
+ Accepts, in order:
13
+ 1. An existing file path, e.g. ``my_configs/exp.py``.
14
+ 2. A path relative to the bundled configs, with or without ``.py``,
15
+ e.g. ``hybridsn/hybridsn_patch15_indian_pines``.
16
+ 3. A bare bundled config name, e.g. ``hybridsn_patch15_indian_pines``.
17
+ """
18
+ if osp.isfile(config):
19
+ return config
20
+
21
+ name = config if config.endswith(".py") else f"{config}.py"
22
+ # Tolerate the repo-style prefix "configs/..." or "mmhsi/configs/..."
23
+ for prefix in ("mmhsi/configs/", "configs/"):
24
+ if name.startswith(prefix):
25
+ name = name[len(prefix) :]
26
+ break
27
+
28
+ candidate = CONFIG_ROOT / name
29
+ if candidate.is_file():
30
+ return str(candidate)
31
+
32
+ matches = [
33
+ p for p in CONFIG_ROOT.rglob(Path(name).name) if "_base_" not in p.parts
34
+ ]
35
+ if len(matches) == 1:
36
+ return str(matches[0])
37
+
38
+ available = sorted(
39
+ str(p.relative_to(CONFIG_ROOT).with_suffix(""))
40
+ for p in CONFIG_ROOT.rglob("*.py")
41
+ if "_base_" not in p.parts
42
+ )
43
+ raise FileNotFoundError(
44
+ f"Config '{config}' not found as a file or a bundled config. "
45
+ f"Bundled configs: {', '.join(available)}"
46
+ )
47
+
48
+
49
+ def list_configs() -> list[str]:
50
+ """Names of the bundled (non-base) configs."""
51
+ return sorted(
52
+ str(p.relative_to(CONFIG_ROOT).with_suffix(""))
53
+ for p in CONFIG_ROOT.rglob("*.py")
54
+ if "_base_" not in p.parts
55
+ )
56
+
57
+
58
+ __all__ = ["CONFIG_ROOT", "list_configs", "resolve_config"]