File size: 11,700 Bytes
c388140
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
from __future__ import annotations

"""

一键训练脚本(无命令行参数):

 - 直接修改本文件顶部常量即可

 - 数据目录:DATA_DIR

 - 提示词来自文件名(HASH-xxx_yyy.png => "xxx yyy")



运行方式(推荐 Python 3.10):

 - py -3.10 train_oneclick.py

"""

import dataclasses
import os

import torch
from torch.utils.data import DataLoader

from blockdiffusion.data.filename_captions import build_caption_items_from_filenames
from blockdiffusion.data.texture_caption_dataset import TextureCaptionDataset
from blockdiffusion.diffusion.gaussian_diffusion import GaussianDiffusion
from blockdiffusion.diffusion.schedules import get_beta_schedule
from blockdiffusion.models.text_unet16 import TextCondConfig, TextCondUNet16
from blockdiffusion.models.unet16 import UNet16Config
from blockdiffusion.text.simple_tokenizer import SimpleVocab, VocabConfig
from blockdiffusion.train.trainer import Trainer, TrainerConfig
from blockdiffusion.utils.checkpoint import load_checkpoint
from blockdiffusion.utils.seed import get_device, seed_everything

# =========================
# 可修改常量(按需改这里)
# =========================

# 数据目录(你提供的路径)
DATA_DIR = r"G:\PycharmProjects\modrinth_downloads\Training_v2"

# 纹理尺寸(本项目目标 16)
IMAGE_SIZE = 16

# 通道数:3=RGB,4=RGBA(需要保留透明就用 4)
CHANNELS = 4

# 扩散步数与调度
TIMESTEPS = 1000
BETA_SCHEDULE = "cosine"  # "linear" / "cosine"

# UNet 规模(16×16 不要太深)
BASE_CHANNELS = 64
NUM_RES_BLOCKS = 2
DROPOUT = 0.0

# 文本(提示词)配置 
MAX_TEXT_LEN = 32
VOCAB_MAX = 8000
COND_DROP_PROB = 0.1  # CFG 训练丢条件概率(0~1)
PRETOKENIZE = False  # 大数据集建议 False:启动更快、占用更小

# 透明度提示词(可选):
# - 仅在 CHANNELS=4 且“新训练(非续训)”时建议开启
# - 作用:自动根据图片 alpha 是否含透明像素,为文本追加 "transparent"/"opaque"
#   这样模型才能学到“透明度与提示词相关”的监督信号
APPEND_ALPHA_TAG = False
ALPHA_TAG_TRANSPARENT = "transparent"
ALPHA_TAG_OPAQUE = "opaque"

# 训练配置(4070Ti 12G:RGBA 建议先从 128/256 试起)
BATCH_SIZE = 128
# 数据加载并行:Windows 若不稳定可改回 0;稳定时建议 4~8
NUM_WORKERS = 4
PREFETCH_FACTOR = 4  # NUM_WORKERS>0 时生效
PERSISTENT_WORKERS = True  # NUM_WORKERS>0 时生效
LR = 2e-4
WEIGHT_DECAY = 1e-4
TOTAL_STEPS = 700_000
GRAD_CLIP = 1.0
AMP = True
EMA_DECAY = 0.9999
GRAD_ACCUM_STEPS = 1  # 不想OOM但想更大有效batch:例如设为 2/4
LR_WARMUP_STEPS = 2000
LR_MIN_RATIO = 0.1
SNR_GAMMA = 5.0  # <=0 关闭;5 通常比较稳
CHANNELS_LAST = True
LOG_EVERY = 10
SAVE_EVERY = 5000
SAMPLE_EVERY = 5000

# 输出
OUT_DIR = "outputs"
RUN_NAME = "oneclick_filename_caption"
# 断点续训:
# - 若你明确指定某个 checkpoint,就填 RESUME_PATH
# - 若 RESUME_PATH=None 且 AUTO_RESUME=True,会自动扫描 outputs/<RUN_NAME>/checkpoints 下最新 step_*.pt 继续训练
RESUME_PATH = None  # 例如 r"outputs/oneclick_filename_caption/checkpoints/step_5000.pt"
AUTO_RESUME = True

# 训练中采样(可选):用这个提示词做固定可视化
SAMPLE_PROMPT = "block"  # 改成你想看的关键词;不想要可设为 None
SAMPLE_CFG_SCALE = 3.0

# 随机种子
SEED = 42
DEVICE = None  # 例如 "cuda:0";None 表示自动


def main() -> None:
    seed_everything(SEED, deterministic=False)
    device = get_device(DEVICE)
    print(f"[init] device={device}  channels={CHANNELS}  image_size={IMAGE_SIZE}")

    # RTX 40 系列性能优化(不影响数值正确性)
    if hasattr(torch, "set_float32_matmul_precision"):
        torch.set_float32_matmul_precision("high")
    if device.type == "cuda":
        torch.backends.cuda.matmul.allow_tf32 = True
        torch.backends.cudnn.allow_tf32 = True

    # =====
    # 续训注意:
    # - 如果设置了 RESUME_PATH,为保证词表/模型结构不变,本脚本会优先从 checkpoint 读取并复用配置;
    # - 新增数据会自动被扫描并参与训练;新出现的词如果不在旧词表里,会被编码为 <unk>(可继续训练,但不会学到新词语义)。
    # =====

    def _auto_find_latest_ckpt() -> str | None:
        ckpt_dir = os.path.join(OUT_DIR, RUN_NAME, "checkpoints")
        if not os.path.isdir(ckpt_dir):
            return None
        best_step = -1
        best_path = None
        for fn in os.listdir(ckpt_dir):
            if not (fn.startswith("step_") and fn.endswith(".pt")):
                continue
            # 形如 step_5000.pt
            core = fn[len("step_") : -len(".pt")]
            try:
                step = int(core)
            except Exception:
                continue
            if step > best_step:
                best_step = step
                best_path = os.path.join(ckpt_dir, fn)
        return best_path

    effective_resume_path = RESUME_PATH
    if (effective_resume_path is None) and AUTO_RESUME:
        effective_resume_path = _auto_find_latest_ckpt()

    resume_meta = None
    if effective_resume_path:
        print(f"[resume] 使用 checkpoint: {effective_resume_path}")
        ckpt = load_checkpoint(effective_resume_path, map_location="cpu")
        resume_meta = ckpt.get("meta", {}) or {}

    # 从 checkpoint 复用关键配置(避免“重建词表”导致继续训练变差/不可用)
    if resume_meta:
        data_meta = resume_meta.get("data", {}) or {}
        diff_meta = resume_meta.get("diffusion", {}) or {}
        unet_cfg_dict = resume_meta.get("unet_cfg", None)
        text_cfg_dict = resume_meta.get("text_cfg", None)
        tok_dict = resume_meta.get("tokenizer", None)

        cfg_image_size = int(data_meta.get("image_size", IMAGE_SIZE))
        cfg_channels = int(data_meta.get("channels", CHANNELS))
        cfg_timesteps = int(diff_meta.get("timesteps", TIMESTEPS))
        cfg_beta_schedule = str(diff_meta.get("beta_schedule", BETA_SCHEDULE))

        if unet_cfg_dict is None or text_cfg_dict is None or tok_dict is None:
            raise ValueError("RESUME_PATH 指向的 checkpoint 缺少 tokenizer/text_cfg/unet_cfg,无法安全续训")
        vocab = SimpleVocab.from_dict(tok_dict)
        text_cfg = TextCondConfig(**text_cfg_dict)
        unet_cfg = UNet16Config(**unet_cfg_dict)
    else:
        cfg_image_size = IMAGE_SIZE
        cfg_channels = CHANNELS
        cfg_timesteps = TIMESTEPS
        cfg_beta_schedule = BETA_SCHEDULE
        text_cfg = TextCondConfig(max_text_len=MAX_TEXT_LEN, emb_dim=128, dropout=0.0)
        unet_cfg = UNet16Config(
            in_channels=cfg_channels,
            out_channels=cfg_channels,
            base_channels=BASE_CHANNELS,
            channel_mults=(1, 2, 2),  # 16->8->4
            num_res_blocks=NUM_RES_BLOCKS,
            attn_resolutions=(8, 4),
            dropout=DROPOUT,
            time_emb_dim=256,
            cond_dim=256,
        )

        # 非续训:词表会基于当前数据构建
        vocab = None

    # 扫描目录(只扫描一次):用于训练集;非续训时也用于构建词表
    print("[data] 扫描图片并从文件名提取提示词...")
    cap_items = build_caption_items_from_filenames(DATA_DIR, verbose=True)
    if vocab is None:
        # 新训练:可强制加入额外 token,避免采样时提示词变成 <unk> 而“看起来不遵从”
        extra_tokens = None
        append_alpha_tag = bool(APPEND_ALPHA_TAG and (cfg_channels == 4))
        if append_alpha_tag:
            extra_tokens = [ALPHA_TAG_TRANSPARENT, ALPHA_TAG_OPAQUE]
        vocab = SimpleVocab.build(
            (it.text for it in cap_items),
            VocabConfig(max_vocab=VOCAB_MAX, min_freq=1),
            extra_tokens=extra_tokens,
        )
    else:
        # 续训:vocab 固定,不能改变;否则 embedding 尺寸不匹配
        append_alpha_tag = False

    dataset = TextureCaptionDataset(
        items=cap_items,
        vocab=vocab,
        image_size=cfg_image_size,
        channels=cfg_channels,
        max_text_len=text_cfg.max_text_len,
        random_flip=True,
        pretokenize=PRETOKENIZE,
        append_alpha_tag=append_alpha_tag,
        alpha_tag_transparent=ALPHA_TAG_TRANSPARENT,
        alpha_tag_opaque=ALPHA_TAG_OPAQUE,
        verbose=True,
    )
    print(f"[data] dataset_size={len(dataset)}  vocab_size={vocab.size}  pretokenize={PRETOKENIZE}")
    dl_kwargs = dict(
        batch_size=BATCH_SIZE,
        shuffle=True,
        num_workers=NUM_WORKERS,
        pin_memory=(device.type == "cuda"),
        drop_last=True,
    )
    if NUM_WORKERS > 0:
        dl_kwargs["prefetch_factor"] = int(PREFETCH_FACTOR)
        dl_kwargs["persistent_workers"] = bool(PERSISTENT_WORKERS)
    dataloader = DataLoader(dataset, **dl_kwargs)
    print(f"[data] dataloader_workers={NUM_WORKERS}")

    # 2) 模型(文本条件)
    model = TextCondUNet16(unet_cfg=unet_cfg, vocab=vocab, text_cfg=text_cfg)

    # 3) Diffusion
    betas = get_beta_schedule(cfg_beta_schedule, cfg_timesteps).to(device)
    diffusion = GaussianDiffusion(betas)

    # 4) 训练配置
    trainer_cfg = TrainerConfig(
        image_size=cfg_image_size,
        channels=cfg_channels,
        batch_size=BATCH_SIZE,
        num_workers=NUM_WORKERS,
        lr=LR,
        weight_decay=WEIGHT_DECAY,
        total_steps=TOTAL_STEPS,
        grad_clip=GRAD_CLIP,
        amp=AMP,
        ema_decay=EMA_DECAY,
        cond_drop_prob=COND_DROP_PROB,
        grad_accum_steps=GRAD_ACCUM_STEPS,
        lr_warmup_steps=LR_WARMUP_STEPS,
        lr_min_ratio=LR_MIN_RATIO,
        snr_gamma=SNR_GAMMA,
        channels_last=CHANNELS_LAST,
        log_every=LOG_EVERY,
        save_every=SAVE_EVERY,
        sample_every=SAMPLE_EVERY,
        sample_cfg_scale=SAMPLE_CFG_SCALE,
        out_dir=OUT_DIR,
        run_name=RUN_NAME,
        resume_path=effective_resume_path,
    )

    # 训练中采样 CFG(可选)
    sample_model_kwargs = None
    sample_uncond_model_kwargs = None
    if SAMPLE_PROMPT:
        tok = torch.tensor([vocab.encode(SAMPLE_PROMPT, max_len=text_cfg.max_text_len)], dtype=torch.long)
        m = (tok != vocab.pad_id)
        sample_model_kwargs = {"tokens": tok.to(device), "mask": m.to(device)}
        un_tok = torch.full_like(tok, fill_value=vocab.pad_id)
        un_m = torch.zeros_like(m)
        sample_uncond_model_kwargs = {"tokens": un_tok.to(device), "mask": un_m.to(device)}

    meta = {
        "model_type": "text_cond",
        "unet_cfg": dataclasses.asdict(unet_cfg),
        "diffusion": {"timesteps": cfg_timesteps, "beta_schedule": cfg_beta_schedule},
        "data": {"image_size": cfg_image_size, "channels": cfg_channels},
        "tokenizer": vocab.to_dict(),
        "text_cfg": dataclasses.asdict(text_cfg),
        # 额外信息:方便回溯
        "data_dir": os.path.abspath(DATA_DIR),
        "caption_source": "filename",
    }

    trainer = Trainer(
        model=model,
        diffusion=diffusion,
        dataloader=dataloader,
        device=device,
        cfg=trainer_cfg,
        meta=meta,
        sample_model_kwargs=sample_model_kwargs,
        sample_uncond_model_kwargs=sample_uncond_model_kwargs,
    )
    print("[train] start...")
    trainer.train()


if __name__ == "__main__":
    main()