BiliSakura commited on
Commit
0bd0c8c
·
verified ·
1 Parent(s): 0f6be63

Add files using upload-large-folder tool

Browse files
Files changed (3) hide show
  1. README.md +51 -63
  2. model_index.json +1 -1
  3. pipeline_hsigene.py +64 -6
README.md CHANGED
@@ -12,24 +12,10 @@ pipeline_tag: image-to-image
12
 
13
  # BiliSakura/HSIGene
14
 
15
- **Hyperspectral image generation** — HSIGene converted to diffusers format. Conditional generation with local controls (HED, MLSD, sketch, segmentation), global controls (content, text), and metadata embeddings. Outputs 48-band hyperspectral images (256×256 pixels).
16
 
17
  > Source: [HSIGene](https://arxiv.org/abs/2409.12470). Converted to diffusers format; model dir is self-contained (no external project for inference).
18
 
19
- ## Conversion
20
-
21
- The main diffusion checkpoint (`last.ckpt`) must be downloaded from [GoogleDrive](https://drive.google.com/file/d/1euJAbsxCgG1wIu_Eh5nPfmiSP9suWsR4/view?usp=drive_link) and placed in `projects/HSIGene-Diffusers/checkpoints/`.
22
-
23
- **Note:** `models/raw/HSIGene` contains annotator/auxiliary models (body pose, depth, SAM, etc.) only — not the main diffusion checkpoint.
24
-
25
- ```bash
26
- cd projects/HSIGene-Diffusers
27
- python convert_to_diffusers.py \
28
- --config_path configs/inference.yaml \
29
- --ckpt_path checkpoints/last.ckpt \
30
- --output_dir /root/worksapce/models/BiliSakura/HSIGene
31
- ```
32
-
33
  ## Repository Structure (after conversion)
34
 
35
  | Component | Path |
@@ -47,65 +33,59 @@ python convert_to_diffusers.py \
47
 
48
  ## Usage
49
 
50
- **Option 1 – No `sys.path.insert` (AeroGen-style):** Load the pipeline from the model path via `importlib`; the model dir is added to the path automatically.
51
 
52
  ```python
53
- import importlib.util
54
- import sys
55
-
56
- model_path = "/path/to/HSIGene" # or "BiliSakura/HSIGene" for Hub
57
- spec = importlib.util.spec_from_file_location("pipeline_hsigene", f"{model_path}/pipeline_hsigene.py")
58
- mod = importlib.util.module_from_spec(spec)
59
- sys.modules["pipeline_hsigene"] = mod
60
- spec.loader.exec_module(mod)
61
-
62
- pipe = mod.HSIGenePipeline.from_pretrained(model_path)
63
  pipe = pipe.to("cuda")
64
  ```
65
 
66
- **Option 2 – With `sys.path.insert`:** Simpler if you are fine adding the model dir to the path once.
67
 
68
- ```python
69
- import sys
70
- sys.path.insert(0, "/path/to/HSIGene")
71
- from pipeline_hsigene import HSIGenePipeline
72
 
73
- pipe = HSIGenePipeline.from_pretrained("/path/to/HSIGene")
74
- pipe = pipe.to("cuda")
 
 
 
75
  ```
76
 
77
- **Option 3 – `DiffusionPipeline.from_pretrained`:** May work with `trust_remote_code=True`. If you see "raw config (list)" errors (e.g. when loading from cache), use Option 1 or 2 instead.
 
 
 
78
 
79
  ```python
80
- from diffusers import DiffusionPipeline
81
- pipe = DiffusionPipeline.from_pretrained("/path/to/HSIGene", trust_remote_code=True)
82
- pipe = pipe.to("cuda")
83
  ```
84
 
85
- **Dependencies:** `pip install diffusers transformers torch einops safetensors`
 
 
 
86
 
87
  ```python
88
- # Conditional generation
89
- output = pipe(
90
- prompt="Wasteland",
91
- num_samples=1,
92
- height=256,
93
- width=256,
94
- num_inference_steps=50,
95
- local_conditions=local_tensor, # (B, 18, H, W) or None
96
- global_conditions=global_tensor, # (B, 768) or None
97
- metadata=metadata_tensor, # (7,) or (B, 7) or None
98
- guidance_scale=1.0,
99
- )
100
- images = output.images # (B, H, W, 48) in [0, 1]
101
  ```
102
 
103
- ### Conditioning
 
 
 
104
 
105
- - **Local**: 18-channel maps (HED, MLSD, sketch, segmentation, etc.) at 512×512 default.
106
- - **Global**: 768-dim CLIP features from reference images.
107
- - **Metadata**: 7-dim vector.
108
- - **Text**: Via `prompt`; use `text_strength` to scale.
109
 
110
  ## Model Sources
111
 
@@ -116,12 +96,20 @@ images = output.images # (B, H, W, 48) in [0, 1]
116
  ## Citation
117
 
118
  ```bibtex
119
- @misc{pang2024hsigenefoundationmodelhyperspectral,
120
- title={HSIGene: A Foundation Model For Hyperspectral Image Generation},
121
- author={Li Pang and Datao Tang and Shuang Xu and Deyu Meng and Xiangyong Cao},
122
- year={2024},
123
- eprint={2409.12470},
124
- archivePrefix={arXiv},
125
- primaryClass={cs.CV},
 
 
 
 
 
 
 
126
  }
 
127
  ```
 
12
 
13
  # BiliSakura/HSIGene
14
 
15
+ **Hyperspectral image generation** — HSIGene converted to diffusers format. Supports task-specific conditioning with local controls (HED, MLSD, sketch, segmentation), global controls (content or text), or metadata embeddings. Outputs 48-band hyperspectral images (256×256 pixels).
16
 
17
  > Source: [HSIGene](https://arxiv.org/abs/2409.12470). Converted to diffusers format; model dir is self-contained (no external project for inference).
18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  ## Repository Structure (after conversion)
20
 
21
  | Component | Path |
 
33
 
34
  ## Usage
35
 
36
+ **Inference Demo (`DiffusionPipeline.from_pretrained`)**
37
 
38
  ```python
39
+ from diffusers import DiffusionPipeline
40
+ pipe = DiffusionPipeline.from_pretrained(
41
+ "/path/to/BiliSakura/HSIGene",
42
+ trust_remote_code=True,
43
+ custom_pipeline="path/to/pipeline_hsigene.py",
44
+ model_path="path/to/BiliSakura/HSIGene"
45
+ )
 
 
 
46
  pipe = pipe.to("cuda")
47
  ```
48
 
49
+ **Dependencies:** `pip install diffusers transformers torch einops safetensors`
50
 
51
+ ### Per-Condition Inference Demos (Not Combined)
 
 
 
52
 
53
+ `local_conditions` shape: `(B, 18, H, W)`; `global_conditions` shape: `(B, 768)`; `metadata` shape: `(7,)` or `(B, 7)`.
54
+
55
+ ```python
56
+ # HED condition
57
+ output = pipe(prompt="", local_conditions=hed_local, global_conditions=None, metadata=None)
58
  ```
59
 
60
+ ```python
61
+ # MLSD condition
62
+ output = pipe(prompt="", local_conditions=mlsd_local, global_conditions=None, metadata=None)
63
+ ```
64
 
65
  ```python
66
+ # Sketch condition
67
+ output = pipe(prompt="", local_conditions=sketch_local, global_conditions=None, metadata=None)
 
68
  ```
69
 
70
+ ```python
71
+ # Segmentation condition
72
+ output = pipe(prompt="", local_conditions=seg_local, global_conditions=None, metadata=None)
73
+ ```
74
 
75
  ```python
76
+ # Content condition (global)
77
+ output = pipe(prompt="", local_conditions=None, global_conditions=content_global, metadata=None)
 
 
 
 
 
 
 
 
 
 
 
78
  ```
79
 
80
+ ```python
81
+ # Text condition
82
+ output = pipe(prompt="Wasteland", local_conditions=None, global_conditions=None, metadata=None)
83
+ ```
84
 
85
+ ```python
86
+ # Metadata condition
87
+ output = pipe(prompt="", local_conditions=None, global_conditions=None, metadata=metadata_vec)
88
+ ```
89
 
90
  ## Model Sources
91
 
 
96
  ## Citation
97
 
98
  ```bibtex
99
+ @article{pangHSIGeneFoundationModel2026,
100
+ title = {{{HSIGene}}: {{A Foundation Model}} for {{Hyperspectral Image Generation}}},
101
+ shorttitle = {{{HSIGene}}},
102
+ author = {Pang, Li and Cao, Xiangyong and Tang, Datao and Xu, Shuang and Bai, Xueru and Zhou, Feng and Meng, Deyu},
103
+ year = 2026,
104
+ month = jan,
105
+ journal = {IEEE Transactions on Pattern Analysis and Machine Intelligence},
106
+ volume = {48},
107
+ number = {1},
108
+ pages = {730--746},
109
+ issn = {1939-3539},
110
+ doi = {10.1109/TPAMI.2025.3610927},
111
+ urldate = {2026-01-02},
112
+ keywords = {Adaptation models,Computational modeling,Controllable generation,deep learning,diffusion model,Diffusion models,Foundation models,hyperspectral image synthesis,Hyperspectral imaging,Image synthesis,Noise reduction,Reliability,Superresolution,Training}
113
  }
114
+
115
  ```
model_index.json CHANGED
@@ -1,5 +1,5 @@
1
  {
2
- "_class_name": ["pipeline_hsigene", "HSIGenePipeline"],
3
  "_diffusers_version": "0.25.0",
4
  "scheduler": ["diffusers", "DDIMScheduler"],
5
  "unet": ["pipeline_hsigene", "HSIGenePipeline"],
 
1
  {
2
+ "_class_name": "HSIGenePipeline",
3
  "_diffusers_version": "0.25.0",
4
  "scheduler": ["diffusers", "DDIMScheduler"],
5
  "unet": ["pipeline_hsigene", "HSIGenePipeline"],
pipeline_hsigene.py CHANGED
@@ -212,6 +212,31 @@ def _is_component_list(v):
212
  return isinstance(v, (list, tuple)) and len(v) == 2 and isinstance(v[0], str) and isinstance(v[1], str)
213
 
214
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
215
  class HSIGenePipeline(DiffusionPipeline):
216
  """Pipeline for HSIGene hyperspectral image generation.
217
 
@@ -245,18 +270,51 @@ class HSIGenePipeline(DiffusionPipeline):
245
  scheduler=None,
246
  crs_model=None,
247
  scale_factor=0.18215,
 
 
248
  ):
249
  super().__init__()
250
  if crs_model is not None:
251
  self.register_modules(crs_model=crs_model, scheduler=scheduler)
252
  else:
253
- if any(_is_component_list(x) for x in (unet, vae, text_encoder, local_adapter,
254
- global_content_adapter, global_text_adapter, metadata_encoder) if x is not None):
255
- raise ValueError(
256
- "HSIGene received raw config (list) instead of loaded components. "
257
- "Use HSIGenePipeline.from_pretrained(path) directly, or ensure the model "
258
- "directory (with hsigene package) is on the path when loading."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
259
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
260
  crs_model = _CRSModelWrapper(
261
  unet=unet,
262
  vae=vae,
 
212
  return isinstance(v, (list, tuple)) and len(v) == 2 and isinstance(v[0], str) and isinstance(v[1], str)
213
 
214
 
215
+ def _resolve_model_root(candidate: Optional[Union[str, Path]]) -> Optional[Path]:
216
+ """Resolve candidate path/repo to model root containing model_index.json."""
217
+ if not candidate:
218
+ return None
219
+ try:
220
+ path = Path(candidate)
221
+ if not path.exists():
222
+ from huggingface_hub import snapshot_download
223
+ path = Path(snapshot_download(str(candidate)))
224
+ path = path.resolve()
225
+ if (path / "model_index.json").exists():
226
+ return path
227
+ cur = path
228
+ for _ in range(5):
229
+ parent = cur.parent
230
+ if parent == cur:
231
+ break
232
+ if (parent / "model_index.json").exists():
233
+ return parent
234
+ cur = parent
235
+ except Exception:
236
+ return None
237
+ return None
238
+
239
+
240
  class HSIGenePipeline(DiffusionPipeline):
241
  """Pipeline for HSIGene hyperspectral image generation.
242
 
 
270
  scheduler=None,
271
  crs_model=None,
272
  scale_factor=0.18215,
273
+ model_path: Optional[Union[str, Path]] = None,
274
+ _name_or_path: Optional[Union[str, Path]] = None,
275
  ):
276
  super().__init__()
277
  if crs_model is not None:
278
  self.register_modules(crs_model=crs_model, scheduler=scheduler)
279
  else:
280
+ components_are_lists = any(
281
+ _is_component_list(x)
282
+ for x in (
283
+ unet,
284
+ vae,
285
+ text_encoder,
286
+ local_adapter,
287
+ global_content_adapter,
288
+ global_text_adapter,
289
+ metadata_encoder,
290
+ )
291
+ if x is not None
292
+ )
293
+ if components_are_lists:
294
+ # Diffusers custom_pipeline may pass raw [library, class] placeholders to __init__.
295
+ # Resolve model root and materialize real components here.
296
+ model_root = (
297
+ _resolve_model_root(model_path)
298
+ or _resolve_model_root(_name_or_path)
299
+ or _resolve_model_root(getattr(getattr(self, "config", None), "_name_or_path", None))
300
  )
301
+ if model_root is None:
302
+ raise ValueError(
303
+ "HSIGene received raw config placeholders but could not resolve model path. "
304
+ "Pass `model_path` to HSIGenePipeline or load via "
305
+ "`DiffusionPipeline.from_pretrained(<path>, custom_pipeline=<pipeline_file>)` "
306
+ "with a valid local model directory."
307
+ )
308
+ loaded = load_components(model_root)
309
+ unet = loaded["unet"]
310
+ vae = loaded["vae"]
311
+ text_encoder = loaded["text_encoder"]
312
+ local_adapter = loaded["local_adapter"]
313
+ global_content_adapter = loaded["global_content_adapter"]
314
+ global_text_adapter = loaded["global_text_adapter"]
315
+ metadata_encoder = loaded["metadata_encoder"]
316
+ scheduler = loaded["scheduler"] if scheduler is None else scheduler
317
+ scale_factor = loaded["scale_factor"]
318
  crs_model = _CRSModelWrapper(
319
  unet=unet,
320
  vae=vae,