AIGC笔记--SVD中UNet加载预训练权重

1--加载方式

  1. 加载全参数(.ckpt)

  2. 加载LoRA(.safetensors)

2--简单实例

python 复制代码
import sys
sys.path.append("/mnt/dolphinfs/hdd_pool/docker/user/hadoop-waimai-aigc/liujinfu/Codes/v3d-vgen-motion")

import torch
from peft import LoraConfig
from safetensors import safe_open

from svd.models.i2v_svd_unet import UNetSpatioTemporalConditionModel
from svd.utils.util import zero_rank_print

if __name__ == "__main__":

    pretrained_model_path = "/mnt/dolphinfs/hdd_pool/docker/user/hadoop-waimai-aigc/liujinfu/Codes/svd_models/models/stable-video-diffusion-img2vid-xt"
    unet = UNetSpatioTemporalConditionModel.from_pretrained(pretrained_model_path, subfolder = "unet")

    # resume_checkpoint_path = "/mnt/dolphinfs/hdd_pool/docker/user/hadoop-waimai-aigc/liujinfu/Codes/v3d-vgen-motion/results/outputs_motionlora_realRota_0603_1024_stride5/test-0-2024-06-03T14-31-30/checkpoints/checkpoint-500.safetensors"
    resume_checkpoint_path = "/mnt/dolphinfs/hdd_pool/docker/user/hadoop-waimai-aigc/liujinfu/Codes/v3d-vgen-motion/results/outputs_motionFull_realRota_0529_stride5/test-0-2024-05-29T10-04-34/checkpoints/checkpoint-step-5000.ckpt"

    # Load pretrained unet weights
    if resume_checkpoint_path.endswith(".ckpt"):
        zero_rank_print(f"resume from checkpoint: {resume_checkpoint_path}")
        resume_checkpoint = torch.load(resume_checkpoint_path, map_location="cpu")
        # resume dit parameters
        print(f'resume_checkpoint keys: {resume_checkpoint.keys()}')
        state_dict = resume_checkpoint["state_dict"]
        m, u = unet.load_state_dict(state_dict, strict=False)
        zero_rank_print(f"dit missing keys: {len(m)}, unexpected keys: {len(u)}")
        assert len(u) == 0
        # resume global step
        resume_global_step = False
        if "global_step" in resume_checkpoint and resume_global_step:
            zero_rank_print(f"resume global_step: {resume_checkpoint['global_step']}")
            global_step = resume_checkpoint['global_step']    
            
    elif resume_checkpoint_path.endswith(".safetensors"):

        unet_lora_config = LoraConfig(
            r = 64, 
            lora_alpha = 64, # scaling = lora_alpha / r
            init_lora_weights = "gaussian", 
            target_modules = ["to_q","to_k","to_v","to_out.0"],
            lora_dropout = 0.1
        )
        unet.add_adapter(unet_lora_config)

        zero_rank_print(f"resume from safetensors: {resume_checkpoint_path}")
        
        state_dict = {}
        with safe_open(resume_checkpoint_path, framework="pt", device="cpu") as f:
            for key in f.keys():
                key_ = key.replace('unet.', '').replace('.weight', '')
                state_dict[key_] = f.get_tensor(key)

                u = 0
                try:
                    unet.get_submodule(key_+'.default').state_dict()['weight'].data.copy_(state_dict[key_])
                except:
                    u += 1
        assert u == 0, "resume unet params failed"

    print("All Done!")
相关推荐
楽码22 分钟前
AI提问:进行深度交互
aigc·openai·taro
iThinkAi1 小时前
Coze(扣子)智能体工作流:1分钟生成100个原创混剪带货视频,无需写文案,无需剪辑
aigc
爱意随风起风止意难平13 小时前
AIGC 基础篇 Python基础 05 元组,集合与字典
开发语言·python·aigc
后端小肥肠16 小时前
【效率核爆2.0】爆款短视频拆解进入流水线时代!Coze+飞书字段捷径自动生成结构化拆解报告
人工智能·aigc·coze
coder_pig1 天前
👀Trae + 掘金MCP = 裸眼3D文字海报生成器
aigc·mcp·trae
atbigapp.com1 天前
告别excel:AI 驱动的数据分析指南
人工智能·数据分析·aigc
FogLetter1 天前
WebLLM:用Fetch API唤醒前端智能新时代
前端·aigc·openai
奇舞精选1 天前
前端开发中 AI 的进阶之路:从工具使用到思维重构
前端·aigc
量子位1 天前
小扎 “超级智能” 小组第一位大佬!谷歌 DeepMind 首席研究员,“压缩即智能”核心人物
google·aigc
咧咧小侠1 天前
使用 Python 构建并调用 ComfyUI 图像生成 API:完整实战指南
开发语言·python·ai·aigc