honghong3 commited on
Commit
ca6b3ea
Β·
verified Β·
1 Parent(s): fd3b60b

Upload 19 files

Browse files
.gitattributes CHANGED
@@ -33,3 +33,14 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
 
 
 
 
 
 
 
 
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ docs/imgs/detailer_process.gif filter=lfs diff=lfs merge=lfs -text
37
+ docs/imgs/inpaintbf.png filter=lfs diff=lfs merge=lfs -text
38
+ docs/imgs/inpainting.png filter=lfs diff=lfs merge=lfs -text
39
+ docs/imgs/new_ui_3.png filter=lfs diff=lfs merge=lfs -text
40
+ docs/imgs/new_ui_4.png filter=lfs diff=lfs merge=lfs -text
41
+ docs/imgs/new_ui.png filter=lfs diff=lfs merge=lfs -text
42
+ docs/imgs/patch_size_1.png filter=lfs diff=lfs merge=lfs -text
43
+ docs/imgs/patch_size_2.png filter=lfs diff=lfs merge=lfs -text
44
+ docs/imgs/sample.png filter=lfs diff=lfs merge=lfs -text
45
+ docs/imgs/sampling.png filter=lfs diff=lfs merge=lfs -text
46
+ docs/imgs/train_img.png filter=lfs diff=lfs merge=lfs -text
CHANGELOG.md ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ## Update β€” 2026.11.20 Sampling: ETA injection equation selection & per-step latent β†’ frame save
2
+
3
+ <img src="assets/eta_frame.png" alt="update" width="50%">
4
+
5
+ ### ETA mode
6
+
7
+ `eta_mode = cos | ln | sin`
8
+
9
+ `ln`: `eta * ln(2 / (1 + x)) / ln 2`
10
+
11
+ <img src="assets/graph.png" alt="graph" width="30%">
12
+
13
+ Code: `src.utilities.predict`
14
+
15
+ - `Eta`: 0 is deterministic. The larger it is, the more fresh noise is injected at each step.
16
+ - `Eta mode`: schedule of the injected noise over the steps. Selectable in all three tabs.
17
+
18
+ ### Frame save
19
+
20
+ Collect the latent at every step, decode all of them into images, and save them as an MP4.
21
+
22
+ <img src="assets/detailer_process.gif" controls width="600">
23
+
24
+ Toggle with **Frame save** in the Sampling panel.
25
+
26
+ ### Batch
27
+
28
+ Generate 1–8 images at once. When Batch is greater than 1, numbered buttons under the result switch between images. Hover over an image to see its seed.
29
+
30
+ ---
31
+
32
+ ## Update β€” 2026.11.19 Sampling
33
+
34
+ - **Quantization**: optional quantized model loading, chosen at startup (`use quantization? y/n`). Lower VRAM usage; works with the block-offload mode.
35
+ - **Batch generation**: generate up to 8 images per run (Text2img / Inpainting). Results appear with numbered buttons to switch between images.
36
+ - **Detailer batch**: each generated patch is merged into a copy of the original image and saved separately. The original is never modified.
37
+ - **API**: `/api/generate` now accepts `batch` and returns `images` (array) and `path` (list) instead of a single `image`.
38
+
39
+ ## Update β€” 2026.11.17 Sampling
40
+
41
+ ### Sampling Improvements
42
+
43
+ - **DDIM sampler removed**
44
+ - Removed the standalone DDIM sampler due to unsatisfactory generation quality in the current implementation.
45
+
46
+ - **Noise Injection added to Euler and Heun**
47
+ - Introduced additional noise injection during the sampling process.
48
+ - Prevents the sampling trajectory from becoming overly constrained by the initial latent structure.
49
+ - Allows the model to reconstruct local features while better utilizing surrounding image context.
50
+ - Improves structural consistency across neighboring regions.
51
+
52
+ - **`eta` parameter (noise injection strength)**
53
+ - Each step integrates only down to `Οƒ_lo = Οƒ_next Γ— (1 βˆ’ eta)`, then re-noises back up to `Οƒ_next` with fresh noise.
54
+ - `eta = 0`: no noise injection. Identical to the original Euler / Heun / DDIM behavior.
55
+ - Higher `eta`: a larger back-and-forth interval per step, so more of the previous structure is re-noised and re-decided.
56
+ - Too high: details and composition can drift between steps, and Heun's second-order correction covers a shorter interval, so its benefit shrinks.
57
+ - Suggested starting range: `0.1 ~ 0.3` (default `0.3` for DDPM_PLUS). Tune per step count and CFG scale.
58
+ - The final step never injects noise, regardless of `eta`.
59
+
60
+ ### Quality Improvements
61
+
62
+ - Improved eye and facial detail reconstruction.
63
+ - Better consistency between left and right facial features.
64
+ - Improved transitions between modified and preserved regions.
65
+ - Reduced local structural artifacts.
66
+ - Significant quality improvements in realistic image generation and inpainting.
67
+
68
+ ### Available Samplers
69
+
70
+ | Sampler | Status | Uses `eta` |
71
+ |---|---|---|
72
+ | Euler + Noise Injection | Supported | Yes |
73
+ | Heun + Noise Injection | Supported | Yes |
74
+ | DDPM_PLUS | Supported | Yes |
75
+ | DDIM | Removed | - |
76
+
77
+ ### Notes
78
+
79
+ Noise injection improves the model's ability to reconstruct local structures instead of relying too heavily on the initial latent representation.
80
+
81
+ This is particularly beneficial for realistic image generation, where fine details and spatial consistency are important.
82
+
83
+ ## Inference Requirements
84
+ - **Inference:** Min VRAM 15GB
85
+ - **Storage:** Min 30GB free disk space (excluding `requirements.txt` packages)
86
+ - **Environment:** Local & Google Colab supported
87
+
88
+ ## Update 2026 11 16 Sampling GPU Optimize
89
+ ### CPU Offloading Mode
90
+
91
+ If your GPU has less than 15GB VRAM, run inference in CPU offloading mode.
92
+ The model stays on CPU; transformer blocks are streamed to the GPU in chunks.
93
+
94
+ On startup, choose `n`:
95
+
96
+ ```
97
+ model upload on gpu (y or n): n
98
+ ```
99
+
100
+ Then set the chunk size (layers loaded onto the GPU at once):
101
+
102
+ ```
103
+ block chunk size (1~8, recommended 4):
104
+ ```
105
+
106
+ | CHUNK | Layers on GPU | With prefetch | Approx. VRAM |
107
+ |-------|---------------|---------------|--------------|
108
+ | 1 | 1 | 2 | ~0.5GB |
109
+ | 2 | 2 | 4 | ~1.1GB |
110
+ | 4 | 4 | 8 | ~2.2GB |
111
+ | 8 | 8 | 16 | ~4.3GB |
112
+
113
+ - Larger chunk = faster, more VRAM.
114
+ - Smaller chunk = safer on low VRAM.
115
+ - Up to `2 Γ— CHUNK` layers are resident with prefetch.
116
+
117
+ Trade-off: VRAM drops from ~15GB to ~3GB, but inference is slower due to
118
+ CPU-GPU PCIe transfers. Output quality may also be slightly lower than full
119
+ GPU mode (differences in floating-point precision and kernel accumulation
120
+ order across CPU↔GPU transfers).
README.md CHANGED
@@ -1,3 +1,63 @@
1
- ---
2
- license: mit
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Diffusion Text-to-Image
2
+
3
+ ## UPDATE 2027-11-27
4
+ ## UI Update
5
+ UI -> CLICK
6
+
7
+ ## Backend
8
+
9
+ - Migrated the backend to **FastAPI**
10
+ - Sampling / inpainting run as background jobs (progress polling, live preview, cancel)
11
+ - Run: `uvicorn main:app --reload`
12
+
13
+ ## Checkpoint
14
+
15
+ - Switched checkpoint format to **safetensors** (previously `.pt`)
16
+
17
+ ---
18
+
19
+ ## [Update](CHANGELOG.md) Β· [Train State](docs/training.md) Β· [Architecture](docs/architecture.md) Β· [UI](docs/ui.md) Β· [TREE](docs/tree.md)
20
+
21
+ ---
22
+
23
+ ![sample_1](docs/imgs/sample.png)
24
+
25
+ ![sample_2](docs/imgs/detailer_process.gif)
26
+
27
+ ## Installation
28
+ ```bash
29
+ git clone https://github.com/FREEANIMA/diffusion-inference.git
30
+ cd diffusion-inference
31
+ python3 -m venv .venv
32
+ source .venv/bin/activate
33
+ python -m pip install -r requirements.txt
34
+ python main.py
35
+ ```
36
+
37
+ ## External Models
38
+
39
+ - **Autoencoder:** DC-AE f32c32 (32Γ— compression, 32 latent channels)
40
+ - **Text Encoder:** T5-Large
41
+
42
+ ## Model
43
+
44
+ - **Architecture:** MMDiT
45
+ - **Parameters:** 4B
46
+ - **Task:** Text-to-Image
47
+ - **Weights:** [Hugging Face](https://huggingface.co/honghong3/VER9) (currently being updated to VER10)
48
+
49
+ Please check the version in [version.py](version.py).
50
+
51
+ ---
52
+
53
+ ### Training Stages
54
+
55
+ | Stage | Description | Steps | Patch Size | max H x W |
56
+ |:---:|---|---:|:---:|:---:|
57
+ | Stage 1 | Pre-training | 600K | 2 | 1024 x 1024 |
58
+ | Stage 2 | Fine-tuning | 100k | 1 | 1024 x 1024 |
59
+ | Stage 3 | Fine-tuning | 300K | 1 | 2048 x 2048 |
60
+
61
+ ---
62
+
63
+ - **GITHUB link** https://github.com/FREEANIMA/diffusion-inference.git
docs/architecture.md ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ ### Train Architecture
2
+ - **Use Cache**
3
+ - **Cpu Multi Threading**
4
+
5
+ ![inference](assets/train_loop.png)
6
+
7
+ ### Inference Architecture
8
+
9
+ ![inference](assets/inference.png)
docs/imgs/detailer_process.gif ADDED

Git LFS Details

  • SHA256: 64d4065af3145e5f9756eab02db95bd6eb204d0b8d40daedea587a57e73f2970
  • Pointer size: 133 Bytes
  • Size of remote file: 11.4 MB
docs/imgs/inference.png ADDED
docs/imgs/inpaintbf.png ADDED

Git LFS Details

  • SHA256: f0ce8846e5ec9f3f85e9c127ff3ee04c382a6e751c887c1dbf029140032327a4
  • Pointer size: 131 Bytes
  • Size of remote file: 468 kB
docs/imgs/inpainting.png ADDED

Git LFS Details

  • SHA256: 2b191d9e2a17f21174743099e2167a1c8e3a260a758e888d7da44544e86b7508
  • Pointer size: 132 Bytes
  • Size of remote file: 1.26 MB
docs/imgs/new_ui.png ADDED

Git LFS Details

  • SHA256: c05d0fcd3a18263cc8d8edf77a1b402228142f9caaf5255e596a87aac4a5409f
  • Pointer size: 131 Bytes
  • Size of remote file: 495 kB
docs/imgs/new_ui_3.png ADDED

Git LFS Details

  • SHA256: 7771f016e8b659ac7e6f92f3076ab3fbeae0b340b81b767f4b181c304380ea6c
  • Pointer size: 131 Bytes
  • Size of remote file: 374 kB
docs/imgs/new_ui_4.png ADDED

Git LFS Details

  • SHA256: 953b9074511fbb99e037c10a013a85a5a201cc49684479b25190c5209bf31e5f
  • Pointer size: 131 Bytes
  • Size of remote file: 635 kB
docs/imgs/patch_size_1.png ADDED

Git LFS Details

  • SHA256: f0414b5271b14c5cc703202cc684f03aeb3b5d724329ddd93389d1fd9b758b1e
  • Pointer size: 131 Bytes
  • Size of remote file: 907 kB
docs/imgs/patch_size_2.png ADDED

Git LFS Details

  • SHA256: b70df5dd2deb4a08af5ba2d586cf19ba15e236bd5019a9bf53e27050f6dd8bf5
  • Pointer size: 132 Bytes
  • Size of remote file: 1.07 MB
docs/imgs/sample.png ADDED

Git LFS Details

  • SHA256: c2e13f2ea33e30264d4b8a0f48421260feb8e041119be7b38202d36272db01e3
  • Pointer size: 132 Bytes
  • Size of remote file: 1.99 MB
docs/imgs/sampling.png ADDED

Git LFS Details

  • SHA256: c3f4265dd644ba4c14955f4cac4080fbd60bc1f975cdcd2c898fc4df3f65c507
  • Pointer size: 131 Bytes
  • Size of remote file: 337 kB
docs/imgs/train_img.png ADDED

Git LFS Details

  • SHA256: 240d1c9d3753e623e5789dccf6649da316780daa32c6a44527291a10e7d03bf0
  • Pointer size: 131 Bytes
  • Size of remote file: 237 kB
docs/imgs/train_loop.png ADDED
docs/training.md ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ## Train Environment
2
+
3
+ - **Framework:** PyTorch (no custom CUDA kernels)
4
+ - **GPU:** NVIDIA RTX PRO 6000 (96GB VRAM) x 1
5
+ - **Platform:** Google Colab
6
+ - **Batch Size:** Micro-batch 4 Γ— Gradient Accumulation 8 = Effective Batch 32
7
+
8
+ - **Training Speed:** ~2.3 s / optimizer step
9
+ - **Max Resolution:** 1024 Γ— 1024
10
+ - **Dataset:** 2M images
11
+ - **Objective:** Flow Matching
12
+ - **Precision:** BF16
13
+ - **Optimizer:** AdamW
14
+ - **Learning Rate:** 1e-4 ~ 5e~5
15
+ - **EMA:** Enabled
16
+
17
+ ## Train UI
18
+
19
+ ![train_ui](imgs/train_img.png)
20
+
21
+ ### Loss by Noise Level
22
+
23
+ | Noise Level | Average Loss |
24
+ |:---:|---:|
25
+ | 1.0 – 0.9 | 4.0 ~ 4.3 |
26
+ | 0.9 – 0.8 | 2.9 ~ 3.1 |
27
+ | 0.8 – 0.7 | 2.4 ~ 2.6 |
28
+ | 0.7 – 0.6 | 2.0 ~ 2.1 |
29
+ | 0.6 – 0.5 | 1.8 ~ 1.9 |
30
+ | 0.5 – 0.4 | 1.6 ~ 1.7 |
31
+ | 0.4 – 0.3 | 1.3 ~ 1.6 |
32
+ | 0.3 – 0.2 | 1.2 ~ 1.4 |
33
+ | 0.2 – 0.1 | 1.0 ~ 1.1 |
34
+
35
+ ## Generation Results
36
+
37
+ ### Patch Size 2 + DC-AE f32c32
38
+
39
+ ![Patch Size 2](imgs/patch_size_2.png)
40
+
41
+ - **Issue:** Facial features, particularly the eyes, tend to collapse or become distorted.
42
+ - **Observation:** Limited spatial detail makes accurate reconstruction of fine facial features challenging.
43
+
44
+ ### Patch Size 1 + DC-AE f32c32
45
+
46
+ ![Patch Size 1](imgs/patch_size_1.png)
47
+
48
+ - **Fine-tuning Result:** Fine-tuning with patch size 1 improved facial feature reconstruction.
49
+ - **Improvement:** Eye and facial distortions were partially resolved, with better preservation of fine details.
docs/tree.md ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ## Project Structure
2
+
3
+ ```text
4
+ src/
5
+ β”œβ”€β”€ core/
6
+ β”‚ β”œβ”€β”€ __init__.py
7
+ β”‚ β”œβ”€β”€ config.py
8
+ β”‚ β”œβ”€β”€ runtime.py
9
+ β”‚ β”œβ”€β”€ shared.py
10
+ β”‚ β”œβ”€β”€ state.py
11
+ β”‚ └── utils.py
12
+ β”œβ”€β”€ external/
13
+ β”‚ β”œβ”€β”€ __init__.py
14
+ β”‚ β”œβ”€β”€ load.py
15
+ β”‚ β”œβ”€β”€ te.py
16
+ β”‚ └── vae.py
17
+ β”œβ”€β”€ network/
18
+ β”‚ β”œβ”€β”€ mmdit_dcae/
19
+ β”‚ β”‚ β”œβ”€β”€ __init__.py
20
+ β”‚ β”‚ β”œβ”€β”€ Block.py
21
+ β”‚ β”‚ β”œβ”€β”€ Model.py
22
+ β”‚ β”‚ β”œβ”€β”€ patch_embedding.py
23
+ β”‚ β”‚ β”œβ”€β”€ Rope2D.py
24
+ β”‚ β”‚ └── sinusoidal_pos_emb.py
25
+ β”‚ β”œβ”€β”€ tags_llm/
26
+ β”‚ β”‚ β”œβ”€β”€ __init__.py
27
+ β”‚ β”‚ β”œβ”€β”€ Block.py
28
+ β”‚ β”‚ β”œβ”€β”€ Model.py
29
+ β”‚ β”‚ └── Rope1D.py
30
+ β”‚ └── __init__.py
31
+ β”œβ”€β”€ sampling/
32
+ β”‚ β”œβ”€β”€ engine_internal/
33
+ β”‚ β”‚ β”œβ”€β”€ __init__.py
34
+ β”‚ β”‚ β”œβ”€β”€ setup.py
35
+ β”‚ β”‚ β”œβ”€β”€ solvers.py
36
+ β”‚ β”‚ β”œβ”€β”€ steps.py
37
+ β”‚ β”‚ β”œβ”€β”€ trajectory.py
38
+ β”‚ β”‚ β”œβ”€β”€ types.py
39
+ β”‚ β”‚ └── utils.py
40
+ β”‚ β”œβ”€β”€ __init__.py
41
+ β”‚ └── engine.py
42
+ β”œβ”€β”€ storage/
43
+ β”‚ β”œβ”€β”€ __init__.py
44
+ β”‚ β”œβ”€β”€ hub.py
45
+ β”‚ └── load_checkpoint.py
46
+ β”œβ”€β”€ tag/
47
+ β”‚ β”œβ”€β”€ __init__.py
48
+ β”‚ β”œβ”€β”€ tags.py
49
+ β”‚ └── utils.py
50
+ └── __init__.py
51
+ ```
docs/ui.md ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ## UI
2
+
3
+ ## UI Update
4
+
5
+ - **Multi-prompt** support
6
+ - **Custom schedulers**
7
+ - Sigma scheduler (CDF-based)
8
+ - CFG scheduler
9
+ - Eta scheduler
10
+ - **Click-button revision**
11
+
12
+ ## Screenshots
13
+
14
+ ### Sampling
15
+
16
+ ![Sampling UI](imgs/new_ui.png)
17
+
18
+ ### Inpainting
19
+
20
+ ![Inpainting UI](imgs/new_ui_4.png)
21
+
22
+ - Compare
23
+
24
+ ![Compare](imgs/new_ui_3.png)
25
+
26
+ ---
27
+
28
+ - Three tabs: **Text2img**, **Inpainting**, **Detailer**. The last tab is remembered, and `#txt2img`, `#inpainting`, `#detailer` in the URL open that tab directly.
29
+ - **Tag Browser**: browse and search tags by type, and click to add them to the prompt.
30
+ - **Tags autocomplete** and **History**: click a history entry to restore the input plus the expanded tags.
31
+ - **Fixed** switch: when on, only the entered tags are used and no LLM tag expansion happens.
32
+ - Only one job runs at a time. Generation runs in a background thread and the browser polls for the result, so long jobs do not hit proxy timeouts.
33
+
34
+ ### UI
35
+ ## Detailing & Inpainting
36
+
37
+ Both tabs share the same mask painter with two layers.
38
+
39
+ | Color | Mode | Meaning |
40
+ |-------|------|---------|
41
+ | Red | μž¬μƒμ„± (Regen) | Area to redraw |
42
+ | Blue | 보쑴 (Keep) | Area that never changes |
43
+ | – | μ§€μš°κ°œ (Erase) | Remove paint from both layers |
44
+
45
+ - Painting one color erases the other under the brush. The later stroke wins, so the layers never overlap.
46
+ - If nothing is painted, the whole image (inpainting) or the whole crop (detailer) is redrawn.
47
+ - Keep overrides Regen. The final redraw area is `regen Γ— (1 βˆ’ keep)`.
48
+ - Masks are resized to the sampling resolution with nearest-neighbor, so they stay binary.
49
+ - **Start T** (0–1): the point in the schedule where redrawing starts. Higher values change more, lower values stay closer to the original. For a full redraw without a mask, around 0.6 is recommended.
50
+ - **Brush**: brush size in px.
51
+
52
+ ---
53
+
54
+ ### Inpainting
55
+
56
+ 1. Enter the image path and press **Load**.
57
+ 2. Paint the Regen / Keep areas on the Original panel.
58
+ 3. Press **Generate**. The result appears on the right and is saved to `inpainting/`.
59
+
60
+ The image is fitted so that the long side is 1024 and the short side is snapped to a multiple of `patch_size Γ— compression_ratio`.
61
+
62
+ ### Detailer
63
+
64
+ 1. Enter the image path and press **Load**.
65
+ 2. Drag a rectangle on the Original panel. The rectangle snaps to multiples of `patch_size Γ— compression_ratio`, and only sizes that scale cleanly to `allowable_size` on the long side are allowed.
66
+ 3. The selected area appears enlarged in **Crop Β· Mask**. Paint the Regen / Keep areas there. The label under the crop shows the original size β†’ the enlarged size.
67
+ 4. Press **Generate**. The crop is redrawn at the enlarged size, scaled back down, and pasted into the original image. The result is saved to `detailer/`.
68
+
69
+ ---