AIGC——微调技术(Datawhale X 魔搭 Al夏令营)

种地  论坛元老 | 2024-9-25 04:24:43 | 来自手机 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 1710|帖子 1710|积分 5130

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
微调(Fine-tuning)是一种在深度学习和机器学习领域中常用的技术,旨在通过调整预训练模型的参数来使其更好地顺应特定任务。其基本原理和参数的理解对于实现更好的效果至关紧张。
  
前言

了解微调的基本原理,对微调的各种参数有一个更加清晰的了解,来实现一个更好的效果,并且在这个Task中给各人先容一下文生图的工作流平台工具ComfyUI,来实现一个更加高度定制的文生图。

一、工具初探一ComfyUI应用场景探索

ComfyUI是一个基于节点流程的AI绘图工具WebUI,它专注于提供更加精准的工作流定制,通过将Stable Diffusion的流程拆分成节点,实现了工作流的定制和可复现性。
1、20分钟速通安装ComfyUI

选择利用魔搭社区提供的Notebook和免费的GPU算力体验来体验ComfyUI。

2、下载脚本代码文件

下载安装ComfyUI的执行文件和task1中微调完成Lora文件
  1. git lfs install
  2. git clone https://www.modelscope.cn/datasets/maochase/kolors_test_comfyui.git
  3. mv kolors_test_comfyui/* ./
  4. rm -rf kolors_test_comfyui/
  5. mkdir -p /mnt/workspace/models/lightning_logs/version_0/checkpoints/
  6. mv epoch=0-step=500.ckpt /mnt/workspace/models/lightning_logs/version_0/checkpoints/   
复制代码


3、进入ComfyUI的安装文件


4、一键执行安装步伐(约莫10min)



5、进入预览界面

当执行到最后一个节点的内容输出了一个访问的链接的时候,复制链接到欣赏器中访问
PS:如果链接访问白屏,或者报错,就等一会再访问重试,步伐可能没有正常启动完毕


6、浅尝ComfyUI工作流

1.不带Lora的工作流样例

创建.json格式文件:
  1. {
  2.   "last_node_id": 15,
  3.   "last_link_id": 18,
  4.   "nodes": [
  5.     {
  6.       "id": 11,
  7.       "type": "VAELoader",
  8.       "pos": [
  9.         1323,
  10.         240
  11.       ],
  12.       "size": {
  13.         "0": 315,
  14.         "1": 58
  15.       },
  16.       "flags": {},
  17.       "order": 0,
  18.       "mode": 0,
  19.       "outputs": [
  20.         {
  21.           "name": "VAE",
  22.           "type": "VAE",
  23.           "links": [
  24.             12
  25.           ],
  26.           "shape": 3
  27.         }
  28.       ],
  29.       "properties": {
  30.         "Node name for S&R": "VAELoader"
  31.       },
  32.       "widgets_values": [
  33.         "sdxl.vae.safetensors"
  34.       ]
  35.     },
  36.     {
  37.       "id": 10,
  38.       "type": "VAEDecode",
  39.       "pos": [
  40.         1368,
  41.         369
  42.       ],
  43.       "size": {
  44.         "0": 210,
  45.         "1": 46
  46.       },
  47.       "flags": {},
  48.       "order": 6,
  49.       "mode": 0,
  50.       "inputs": [
  51.         {
  52.           "name": "samples",
  53.           "type": "LATENT",
  54.           "link": 18
  55.         },
  56.         {
  57.           "name": "vae",
  58.           "type": "VAE",
  59.           "link": 12,
  60.           "slot_index": 1
  61.         }
  62.       ],
  63.       "outputs": [
  64.         {
  65.           "name": "IMAGE",
  66.           "type": "IMAGE",
  67.           "links": [
  68.             13
  69.           ],
  70.           "shape": 3,
  71.           "slot_index": 0
  72.         }
  73.       ],
  74.       "properties": {
  75.         "Node name for S&R": "VAEDecode"
  76.       }
  77.     },
  78.     {
  79.       "id": 14,
  80.       "type": "KolorsSampler",
  81.       "pos": [
  82.         1011,
  83.         371
  84.       ],
  85.       "size": {
  86.         "0": 315,
  87.         "1": 222
  88.       },
  89.       "flags": {},
  90.       "order": 5,
  91.       "mode": 0,
  92.       "inputs": [
  93.         {
  94.           "name": "kolors_model",
  95.           "type": "KOLORSMODEL",
  96.           "link": 16
  97.         },
  98.         {
  99.           "name": "kolors_embeds",
  100.           "type": "KOLORS_EMBEDS",
  101.           "link": 17
  102.         }
  103.       ],
  104.       "outputs": [
  105.         {
  106.           "name": "latent",
  107.           "type": "LATENT",
  108.           "links": [
  109.             18
  110.           ],
  111.           "shape": 3,
  112.           "slot_index": 0
  113.         }
  114.       ],
  115.       "properties": {
  116.         "Node name for S&R": "KolorsSampler"
  117.       },
  118.       "widgets_values": [
  119.         1024,
  120.         1024,
  121.         1000102404233412,
  122.         "fixed",
  123.         25,
  124.         5,
  125.         "EulerDiscreteScheduler"
  126.       ]
  127.     },
  128.     {
  129.       "id": 6,
  130.       "type": "DownloadAndLoadKolorsModel",
  131.       "pos": [
  132.         201,
  133.         368
  134.       ],
  135.       "size": {
  136.         "0": 315,
  137.         "1": 82
  138.       },
  139.       "flags": {},
  140.       "order": 1,
  141.       "mode": 0,
  142.       "outputs": [
  143.         {
  144.           "name": "kolors_model",
  145.           "type": "KOLORSMODEL",
  146.           "links": [
  147.             16
  148.           ],
  149.           "shape": 3,
  150.           "slot_index": 0
  151.         }
  152.       ],
  153.       "properties": {
  154.         "Node name for S&R": "DownloadAndLoadKolorsModel"
  155.       },
  156.       "widgets_values": [
  157.         "Kwai-Kolors/Kolors",
  158.         "fp16"
  159.       ]
  160.     },
  161.     {
  162.       "id": 3,
  163.       "type": "PreviewImage",
  164.       "pos": [
  165.         1366,
  166.         468
  167.       ],
  168.       "size": [
  169.         535.4001724243165,
  170.         562.2001106262207
  171.       ],
  172.       "flags": {},
  173.       "order": 7,
  174.       "mode": 0,
  175.       "inputs": [
  176.         {
  177.           "name": "images",
  178.           "type": "IMAGE",
  179.           "link": 13
  180.         }
  181.       ],
  182.       "properties": {
  183.         "Node name for S&R": "PreviewImage"
  184.       }
  185.     },
  186.     {
  187.       "id": 12,
  188.       "type": "KolorsTextEncode",
  189.       "pos": [
  190.         519,
  191.         529
  192.       ],
  193.       "size": [
  194.         457.2893696934723,
  195.         225.28656056301645
  196.       ],
  197.       "flags": {},
  198.       "order": 4,
  199.       "mode": 0,
  200.       "inputs": [
  201.         {
  202.           "name": "chatglm3_model",
  203.           "type": "CHATGLM3MODEL",
  204.           "link": 14,
  205.           "slot_index": 0
  206.         }
  207.       ],
  208.       "outputs": [
  209.         {
  210.           "name": "kolors_embeds",
  211.           "type": "KOLORS_EMBEDS",
  212.           "links": [
  213.             17
  214.           ],
  215.           "shape": 3,
  216.           "slot_index": 0
  217.         }
  218.       ],
  219.       "properties": {
  220.         "Node name for S&R": "KolorsTextEncode"
  221.       },
  222.       "widgets_values": [
  223.         "cinematic photograph of an astronaut riding a horse in space |\nillustration of a cat wearing a top hat and a scarf  |\nphotograph of a goldfish in a bowl |\nanime screencap of a red haired girl",
  224.         "",
  225.         1
  226.       ]
  227.     },
  228.     {
  229.       "id": 15,
  230.       "type": "Note",
  231.       "pos": [
  232.         200,
  233.         636
  234.       ],
  235.       "size": [
  236.         273.5273818969726,
  237.         149.55464588512064
  238.       ],
  239.       "flags": {},
  240.       "order": 2,
  241.       "mode": 0,
  242.       "properties": {
  243.         "text": ""
  244.       },
  245.       "widgets_values": [
  246.         "Text encoding takes the most VRAM, quantization can reduce that a lot.\n\nApproximate values I have observed:\nfp16 - 12 GB\nquant8 - 8-9 GB\nquant4 - 4-5 GB\n\nquant4 reduces the quality quite a bit, 8 seems fine"
  247.       ],
  248.       "color": "#432",
  249.       "bgcolor": "#653"
  250.     },
  251.     {
  252.       "id": 13,
  253.       "type": "DownloadAndLoadChatGLM3",
  254.       "pos": [
  255.         206,
  256.         522
  257.       ],
  258.       "size": [
  259.         274.5334274291992,
  260.         58
  261.       ],
  262.       "flags": {},
  263.       "order": 3,
  264.       "mode": 0,
  265.       "outputs": [
  266.         {
  267.           "name": "chatglm3_model",
  268.           "type": "CHATGLM3MODEL",
  269.           "links": [
  270.             14
  271.           ],
  272.           "shape": 3
  273.         }
  274.       ],
  275.       "properties": {
  276.         "Node name for S&R": "DownloadAndLoadChatGLM3"
  277.       },
  278.       "widgets_values": [
  279.         "fp16"
  280.       ]
  281.     }
  282.   ],
  283.   "links": [
  284.     [
  285.       12,
  286.       11,
  287.       0,
  288.       10,
  289.       1,
  290.       "VAE"
  291.     ],
  292.     [
  293.       13,
  294.       10,
  295.       0,
  296.       3,
  297.       0,
  298.       "IMAGE"
  299.     ],
  300.     [
  301.       14,
  302.       13,
  303.       0,
  304.       12,
  305.       0,
  306.       "CHATGLM3MODEL"
  307.     ],
  308.     [
  309.       16,
  310.       6,
  311.       0,
  312.       14,
  313.       0,
  314.       "KOLORSMODEL"
  315.     ],
  316.     [
  317.       17,
  318.       12,
  319.       0,
  320.       14,
  321.       1,
  322.       "KOLORS_EMBEDS"
  323.     ],
  324.     [
  325.       18,
  326.       14,
  327.       0,
  328.       10,
  329.       0,
  330.       "LATENT"
  331.     ]
  332.   ],
  333.   "groups": [],
  334.   "config": {},
  335.   "extra": {
  336.     "ds": {
  337.       "scale": 1.1,
  338.       "offset": {
  339.         "0": -114.73954010009766,
  340.         "1": -139.79705810546875
  341.       }
  342.     }
  343.   },
  344.   "version": 0.4
  345. }
复制代码
加载模型,并完成第一次生图
PS:初次点击生成图片会加载资源,时间较长,各人耐心等待

2.带Lora的工作流样例

创建.json格式文件:
  1.       
  2. {
  3.   "last_node_id": 16,
  4.   "last_link_id": 20,
  5.   "nodes": [
  6.     {
  7.       "id": 11,
  8.       "type": "VAELoader",
  9.       "pos": [
  10.         1323,
  11.         240
  12.       ],
  13.       "size": {
  14.         "0": 315,
  15.         "1": 58
  16.       },
  17.       "flags": {},
  18.       "order": 0,
  19.       "mode": 0,
  20.       "outputs": [
  21.         {
  22.           "name": "VAE",
  23.           "type": "VAE",
  24.           "links": [
  25.             12
  26.           ],
  27.           "shape": 3
  28.         }
  29.       ],
  30.       "properties": {
  31.         "Node name for S&R": "VAELoader"
  32.       },
  33.       "widgets_values": [
  34.         "sdxl.vae.safetensors"
  35.       ]
  36.     },
  37.     {
  38.       "id": 10,
  39.       "type": "VAEDecode",
  40.       "pos": [
  41.         1368,
  42.         369
  43.       ],
  44.       "size": {
  45.         "0": 210,
  46.         "1": 46
  47.       },
  48.       "flags": {},
  49.       "order": 7,
  50.       "mode": 0,
  51.       "inputs": [
  52.         {
  53.           "name": "samples",
  54.           "type": "LATENT",
  55.           "link": 18
  56.         },
  57.         {
  58.           "name": "vae",
  59.           "type": "VAE",
  60.           "link": 12,
  61.           "slot_index": 1
  62.         }
  63.       ],
  64.       "outputs": [
  65.         {
  66.           "name": "IMAGE",
  67.           "type": "IMAGE",
  68.           "links": [
  69.             13
  70.           ],
  71.           "shape": 3,
  72.           "slot_index": 0
  73.         }
  74.       ],
  75.       "properties": {
  76.         "Node name for S&R": "VAEDecode"
  77.       }
  78.     },
  79.     {
  80.       "id": 15,
  81.       "type": "Note",
  82.       "pos": [
  83.         200,
  84.         636
  85.       ],
  86.       "size": {
  87.         "0": 273.5273742675781,
  88.         "1": 149.5546417236328
  89.       },
  90.       "flags": {},
  91.       "order": 1,
  92.       "mode": 0,
  93.       "properties": {
  94.         "text": ""
  95.       },
  96.       "widgets_values": [
  97.         "Text encoding takes the most VRAM, quantization can reduce that a lot.\n\nApproximate values I have observed:\nfp16 - 12 GB\nquant8 - 8-9 GB\nquant4 - 4-5 GB\n\nquant4 reduces the quality quite a bit, 8 seems fine"
  98.       ],
  99.       "color": "#432",
  100.       "bgcolor": "#653"
  101.     },
  102.     {
  103.       "id": 13,
  104.       "type": "DownloadAndLoadChatGLM3",
  105.       "pos": [
  106.         206,
  107.         522
  108.       ],
  109.       "size": {
  110.         "0": 274.5334167480469,
  111.         "1": 58
  112.       },
  113.       "flags": {},
  114.       "order": 2,
  115.       "mode": 0,
  116.       "outputs": [
  117.         {
  118.           "name": "chatglm3_model",
  119.           "type": "CHATGLM3MODEL",
  120.           "links": [
  121.             14
  122.           ],
  123.           "shape": 3
  124.         }
  125.       ],
  126.       "properties": {
  127.         "Node name for S&R": "DownloadAndLoadChatGLM3"
  128.       },
  129.       "widgets_values": [
  130.         "fp16"
  131.       ]
  132.     },
  133.     {
  134.       "id": 6,
  135.       "type": "DownloadAndLoadKolorsModel",
  136.       "pos": [
  137.         201,
  138.         368
  139.       ],
  140.       "size": {
  141.         "0": 315,
  142.         "1": 82
  143.       },
  144.       "flags": {},
  145.       "order": 3,
  146.       "mode": 0,
  147.       "outputs": [
  148.         {
  149.           "name": "kolors_model",
  150.           "type": "KOLORSMODEL",
  151.           "links": [
  152.             19
  153.           ],
  154.           "shape": 3,
  155.           "slot_index": 0
  156.         }
  157.       ],
  158.       "properties": {
  159.         "Node name for S&R": "DownloadAndLoadKolorsModel"
  160.       },
  161.       "widgets_values": [
  162.         "Kwai-Kolors/Kolors",
  163.         "fp16"
  164.       ]
  165.     },
  166.     {
  167.       "id": 12,
  168.       "type": "KolorsTextEncode",
  169.       "pos": [
  170.         519,
  171.         529
  172.       ],
  173.       "size": {
  174.         "0": 457.28936767578125,
  175.         "1": 225.28656005859375
  176.       },
  177.       "flags": {},
  178.       "order": 4,
  179.       "mode": 0,
  180.       "inputs": [
  181.         {
  182.           "name": "chatglm3_model",
  183.           "type": "CHATGLM3MODEL",
  184.           "link": 14,
  185.           "slot_index": 0
  186.         }
  187.       ],
  188.       "outputs": [
  189.         {
  190.           "name": "kolors_embeds",
  191.           "type": "KOLORS_EMBEDS",
  192.           "links": [
  193.             17
  194.           ],
  195.           "shape": 3,
  196.           "slot_index": 0
  197.         }
  198.       ],
  199.       "properties": {
  200.         "Node name for S&R": "KolorsTextEncode"
  201.       },
  202.       "widgets_values": [
  203.         "二次元,长发,少女,白色背景",
  204.         "",
  205.         1
  206.       ]
  207.     },
  208.     {
  209.       "id": 3,
  210.       "type": "PreviewImage",
  211.       "pos": [
  212.         1366,
  213.         469
  214.       ],
  215.       "size": {
  216.         "0": 535.400146484375,
  217.         "1": 562.2001342773438
  218.       },
  219.       "flags": {},
  220.       "order": 8,
  221.       "mode": 0,
  222.       "inputs": [
  223.         {
  224.           "name": "images",
  225.           "type": "IMAGE",
  226.           "link": 13
  227.         }
  228.       ],
  229.       "properties": {
  230.         "Node name for S&R": "PreviewImage"
  231.       }
  232.     },
  233.     {
  234.       "id": 16,
  235.       "type": "LoadKolorsLoRA",
  236.       "pos": [
  237.         606,
  238.         368
  239.       ],
  240.       "size": {
  241.         "0": 317.4000244140625,
  242.         "1": 82
  243.       },
  244.       "flags": {},
  245.       "order": 5,
  246.       "mode": 0,
  247.       "inputs": [
  248.         {
  249.           "name": "kolors_model",
  250.           "type": "KOLORSMODEL",
  251.           "link": 19
  252.         }
  253.       ],
  254.       "outputs": [
  255.         {
  256.           "name": "kolors_model",
  257.           "type": "KOLORSMODEL",
  258.           "links": [
  259.             20
  260.           ],
  261.           "shape": 3,
  262.           "slot_index": 0
  263.         }
  264.       ],
  265.       "properties": {
  266.         "Node name for S&R": "LoadKolorsLoRA"
  267.       },
  268.       "widgets_values": [
  269.         "/mnt/workspace/models/lightning_logs/version_0/checkpoints/epoch=0-step=500.ckpt",
  270.         2
  271.       ]
  272.     },
  273.     {
  274.       "id": 14,
  275.       "type": "KolorsSampler",
  276.       "pos": [
  277.         1011,
  278.         371
  279.       ],
  280.       "size": {
  281.         "0": 315,
  282.         "1": 266
  283.       },
  284.       "flags": {},
  285.       "order": 6,
  286.       "mode": 0,
  287.       "inputs": [
  288.         {
  289.           "name": "kolors_model",
  290.           "type": "KOLORSMODEL",
  291.           "link": 20
  292.         },
  293.         {
  294.           "name": "kolors_embeds",
  295.           "type": "KOLORS_EMBEDS",
  296.           "link": 17
  297.         },
  298.         {
  299.           "name": "latent",
  300.           "type": "LATENT",
  301.           "link": null
  302.         }
  303.       ],
  304.       "outputs": [
  305.         {
  306.           "name": "latent",
  307.           "type": "LATENT",
  308.           "links": [
  309.             18
  310.           ],
  311.           "shape": 3,
  312.           "slot_index": 0
  313.         }
  314.       ],
  315.       "properties": {
  316.         "Node name for S&R": "KolorsSampler"
  317.       },
  318.       "widgets_values": [
  319.         1024,
  320.         1024,
  321.         0,
  322.         "fixed",
  323.         25,
  324.         5,
  325.         "EulerDiscreteScheduler",
  326.         1
  327.       ]
  328.     }
  329.   ],
  330.   "links": [
  331.     [
  332.       12,
  333.       11,
  334.       0,
  335.       10,
  336.       1,
  337.       "VAE"
  338.     ],
  339.     [
  340.       13,
  341.       10,
  342.       0,
  343.       3,
  344.       0,
  345.       "IMAGE"
  346.     ],
  347.     [
  348.       14,
  349.       13,
  350.       0,
  351.       12,
  352.       0,
  353.       "CHATGLM3MODEL"
  354.     ],
  355.     [
  356.       17,
  357.       12,
  358.       0,
  359.       14,
  360.       1,
  361.       "KOLORS_EMBEDS"
  362.     ],
  363.     [
  364.       18,
  365.       14,
  366.       0,
  367.       10,
  368.       0,
  369.       "LATENT"
  370.     ],
  371.     [
  372.       19,
  373.       6,
  374.       0,
  375.       16,
  376.       0,
  377.       "KOLORSMODEL"
  378.     ],
  379.     [
  380.       20,
  381.       16,
  382.       0,
  383.       14,
  384.       0,
  385.       "KOLORSMODEL"
  386.     ]
  387.   ],
  388.   "groups": [],
  389.   "config": {},
  390.   "extra": {
  391.     "ds": {
  392.       "scale": 1.2100000000000002,
  393.       "offset": {
  394.         "0": -183.91309381910426,
  395.         "1": -202.11110769225016
  396.       }
  397.     }
  398.   },
  399.   "version": 0.4
  400. }
  401.    
复制代码
7、关闭魔塔GPU环境


二、Lora微调

Lora微调,全称为Low-Rank Adaptation(低秩顺应),是一种高效的模型微调技术,特别适用于大型预训练模型。该技术通过引入低秩矩阵来保持预训练模型的大部分参数不变,仅调整少量参数以顺应特定任务。
1.Lora微调的基本原理


  • 参数矩阵的低秩近似

    • 大模型通常具有过参数化的特点,即参数矩阵的维度很高,但在特定任务中,只有一小部分参数起紧张作用。
    • Lora利用低秩矩阵分解的思想,通过引入两个维度较小的矩阵A和B(A的维度为dxr,B的维度为rxd,其中r远小于d)来近似原始权重矩阵。
    • 这两个矩阵相乘后,得到的矩阵AB的秩远小于原始权重矩阵的秩,但可以或许在一定程度上保持模型在特定任务上的性能。

  • 旁路布局

    • 在网络中增加一个旁路布局,该旁路是A和B两个矩阵相乘的结果。
    • 在训练过程中,冻结原始网络的参数,只训练旁路参数A和B。
    • 由于A和B的参数量远远小于原始网络的参数,因此训练时所需的显存开销大大减小。

2.Task2中的的微调代码

代码如下:
  1. import os
  2. cmd = """
  3. python DiffSynth-Studio/examples/train/kolors/train_kolors_lora.py \ # 选择使用可图的Lora训练脚本DiffSynth-Studio/examples/train/kolors/train_kolors_lora.py
  4.   --pretrained_unet_path models/kolors/Kolors/unet/diffusion_pytorch_model.safetensors \ # 选择unet模型
  5.   --pretrained_text_encoder_path models/kolors/Kolors/text_encoder \ # 选择text_encoder
  6.   --pretrained_fp16_vae_path models/sdxl-vae-fp16-fix/diffusion_pytorch_model.safetensors \ # 选择vae模型
  7.   --lora_rank 16 \ # lora_rank 16 表示在权衡模型表达能力和训练效率时,选择了使用 16 作为秩,适合在不显著降低模型性能的前提下,通过 LoRA 减少计算和内存的需求
  8.   --lora_alpha 4.0 \ # 设置 LoRA 的 alpha 值,影响调整的强度
  9.   --dataset_path data/lora_dataset_processed \ # 指定数据集路径,用于训练模型
  10.   --output_path ./models \ # 指定输出路径,用于保存模型
  11.   --max_epochs 1 \ # 设置最大训练轮数为 1
  12.   --center_crop \ # 启用中心裁剪,这通常用于图像预处理
  13.   --use_gradient_checkpointing \ # 启用梯度检查点技术,以节省内存
  14.   --precision "16-mixed" # 指定训练时的精度为混合 16 位精度(half precision),这可以加速训练并减少显存使用
  15. """.strip()
  16. os.system(cmd) # 执行可图Lora训练   
复制代码

总结

AIGC作为人工智能领域的一个紧张分支,正在徐徐改变我们的生存方式和工作方式。随着技术的不停发展和美满,我们有来由信赖AIGC将在更多领域展现出其独特的魅力和价值。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

种地

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表