Spaces:
Sleeping
Sleeping
Download train.py from wkea/blockdiffusion-api: direct link, hf CLI and curl.
- Browser
- Download file 8.15 kB
-
https://huggingface.co/spaces/wkea/blockdiffusion-api/resolve/main/train.py
- Command line
-
hf download hf://spaces/wkea/blockdiffusion-api/train.py
-
curl -L -o train.py https://huggingface.co/spaces/wkea/blockdiffusion-api/resolve/main/train.py
8.15 kB
| from __future__ import annotations | |
| import argparse | |
| import dataclasses | |
| import os | |
| from typing import Optional | |
| import torch | |
| from torch.utils.data import DataLoader | |
| from blockdiffusion.data.texture_folder import TextureFolderDataset | |
| from blockdiffusion.data.captions import load_captions | |
| 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.unet16 import UNet16, UNet16Config | |
| from blockdiffusion.models.text_unet16 import TextCondConfig, TextCondUNet16 | |
| from blockdiffusion.text.simple_tokenizer import SimpleVocab, VocabConfig | |
| from blockdiffusion.train.trainer import Trainer, TrainerConfig | |
| from blockdiffusion.utils.seed import get_device, seed_everything | |
| def parse_args() -> argparse.Namespace: | |
| p = argparse.ArgumentParser(description="BlockDiffusion 16×16 纹理训练入口") | |
| # 数据 | |
| p.add_argument("--data_dir", type=str, required=True, help="纹理图片目录(可含子目录)") | |
| p.add_argument("--image_size", type=int, default=16, help="纹理边长(默认 16)") | |
| p.add_argument("--channels", type=int, default=3, help="纹理通道数(默认 RGB=3)") | |
| p.add_argument("--captions", type=str, default=None, help="图文配对文件(.jsonl/.csv),提供则启用提示词条件训练") | |
| p.add_argument("--max_text_len", type=int, default=32, help="文本最大长度(中文按字、英文按词)") | |
| p.add_argument("--vocab_max", type=int, default=8000, help="词表最大大小") | |
| # 扩散 | |
| p.add_argument("--timesteps", type=int, default=1000, help="扩散步数") | |
| p.add_argument("--beta_schedule", type=str, default="cosine", choices=["linear", "cosine"], help="噪声调度") | |
| # UNet | |
| p.add_argument("--base_channels", type=int, default=64, help="UNet 基础通道数") | |
| p.add_argument("--num_res_blocks", type=int, default=2, help="每个 level 的 ResBlock 数") | |
| p.add_argument("--dropout", type=float, default=0.0, help="ResBlock dropout(一般可为 0)") | |
| # 训练 | |
| p.add_argument("--batch_size", type=int, default=512) | |
| # Windows 下多进程 DataLoader 可能更挑剔,默认给 0 更稳;需要更快可手动调大。 | |
| p.add_argument("--num_workers", type=int, default=(0 if os.name == "nt" else 4)) | |
| p.add_argument("--lr", type=float, default=2e-4) | |
| p.add_argument("--weight_decay", type=float, default=1e-4) | |
| p.add_argument("--total_steps", type=int, default=200_000) | |
| p.add_argument("--grad_clip", type=float, default=1.0) | |
| p.add_argument("--no_amp", action="store_true", help="关闭混合精度") | |
| p.add_argument("--ema_decay", type=float, default=0.9999) | |
| p.add_argument("--cond_drop_prob", type=float, default=0.1, help="CFG 训练:丢条件概率(0~1)") | |
| p.add_argument("--sample_prompt", type=str, default=None, help="训练中采样使用的提示词(仅条件训练时生效)") | |
| p.add_argument("--sample_cfg_scale", type=float, default=3.0, help="采样 CFG scale(仅条件训练时生效)") | |
| # 输出 | |
| p.add_argument("--out_dir", type=str, default="outputs") | |
| p.add_argument("--run_name", type=str, default="default") | |
| p.add_argument("--resume", type=str, default=None, help="从 checkpoint 恢复训练") | |
| # 其他 | |
| p.add_argument("--seed", type=int, default=42) | |
| p.add_argument("--device", type=str, default=None, help='如 "cuda" / "cuda:0" / "cpu"') | |
| return p.parse_args() | |
| def main() -> None: | |
| args = parse_args() | |
| seed_everything(args.seed, deterministic=False) | |
| device = get_device(args.device) | |
| # 性能优化:适用于 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 | |
| sample_model_kwargs = None | |
| sample_uncond_model_kwargs = None | |
| # 选择数据集:无条件/条件(captions) | |
| if args.captions: | |
| cap_items = load_captions(args.captions, data_root=args.data_dir) | |
| # 可选:强制加入透明/不透明 token,方便学习 alpha 与提示词关联(需你在 captions 里实际使用这些词) | |
| vocab = SimpleVocab.build( | |
| (it.text for it in cap_items), | |
| VocabConfig(max_vocab=args.vocab_max, min_freq=1), | |
| extra_tokens=["transparent", "opaque"], | |
| ) | |
| dataset = TextureCaptionDataset( | |
| items=cap_items, | |
| vocab=vocab, | |
| image_size=args.image_size, | |
| channels=args.channels, | |
| max_text_len=args.max_text_len, | |
| random_flip=True, | |
| ) | |
| else: | |
| vocab = None | |
| dataset = TextureFolderDataset( | |
| root_dir=args.data_dir, | |
| image_size=args.image_size, | |
| channels=args.channels, | |
| random_flip=True, | |
| ) | |
| dataloader = DataLoader( | |
| dataset, | |
| batch_size=args.batch_size, | |
| shuffle=True, | |
| num_workers=args.num_workers, | |
| pin_memory=(device.type == "cuda"), | |
| drop_last=True, | |
| ) | |
| unet_cfg = UNet16Config( | |
| in_channels=args.channels, | |
| out_channels=args.channels, | |
| base_channels=args.base_channels, | |
| channel_mults=(1, 2, 2), # 16->8->4 | |
| num_res_blocks=args.num_res_blocks, | |
| attn_resolutions=(8, 4), | |
| dropout=args.dropout, | |
| time_emb_dim=256, | |
| cond_dim=256, | |
| ) | |
| model_type = "uncond" | |
| text_cfg = None | |
| if args.captions: | |
| # 条件模型:TextEncoder 输出维度与 time_emb_dim 对齐 | |
| text_cfg = TextCondConfig(max_text_len=args.max_text_len, emb_dim=128, dropout=0.0) | |
| model = TextCondUNet16(unet_cfg=unet_cfg, vocab=vocab, text_cfg=text_cfg) | |
| model_type = "text_cond" | |
| # 训练中采样(可选):提供 sample_prompt 才启用 CFG 采样,否则走无条件采样 | |
| if args.sample_prompt: | |
| tok = torch.tensor([vocab.encode(args.sample_prompt, max_len=args.max_text_len)], dtype=torch.long) | |
| m = (tok != vocab.pad_id) | |
| sample_model_kwargs = {"tokens": tok.to(device), "mask": m.to(device)} | |
| # 无条件分支:mask 全 0(tokens 可以随意,统一用 pad) | |
| 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)} | |
| else: | |
| model = UNet16(unet_cfg) | |
| betas = get_beta_schedule(args.beta_schedule, args.timesteps).to(device) | |
| diffusion = GaussianDiffusion(betas) | |
| trainer_cfg = TrainerConfig( | |
| image_size=args.image_size, | |
| channels=args.channels, | |
| batch_size=args.batch_size, | |
| num_workers=args.num_workers, | |
| lr=args.lr, | |
| weight_decay=args.weight_decay, | |
| total_steps=args.total_steps, | |
| grad_clip=args.grad_clip, | |
| amp=not args.no_amp, | |
| ema_decay=args.ema_decay, | |
| cond_drop_prob=args.cond_drop_prob, | |
| sample_cfg_scale=args.sample_cfg_scale, | |
| out_dir=args.out_dir, | |
| run_name=args.run_name, | |
| resume_path=args.resume, | |
| ) | |
| meta = { | |
| "model_type": model_type, | |
| "unet_cfg": dataclasses.asdict(unet_cfg), | |
| "diffusion": {"timesteps": args.timesteps, "beta_schedule": args.beta_schedule}, | |
| "data": {"image_size": args.image_size, "channels": args.channels}, | |
| } | |
| if args.captions and vocab is not None and text_cfg is not None: | |
| meta["tokenizer"] = vocab.to_dict() | |
| meta["text_cfg"] = dataclasses.asdict(text_cfg) | |
| 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, | |
| ) | |
| trainer.train() | |
| if __name__ == "__main__": | |
| main() | |