在Kaggle上使用Stable Diffusion进行AI绘图

news/2024/12/23 9:01:40/

前言

  • 因为使用Stable Diffusion进行AI绘图需要GPU,这让其应用得到了限制
  • 本文介绍如何在Kaggle中部署Stable Diffusion,并使用免费的P100 GPU进行推理(每周可免费使用30小时),部署好后可以在任意移动端使用。
  • 本项目在stable-diffusion-webui-kaggle基础上进行改进,原作者Github项目地址
  • 欢迎大家Copy and Edit我的kaggle notebook,项目地址。效果展示
    在这里插入图片描述

使用教程

  • 首先需要注册Kaggle账号,注册过程中可能需要科学上网,这里不展开教程了,请大家查找其他教程解决
  • 新建notebook,并将下列代码放入其中
import os
import shutil
import subprocess
import threading
import time
import socket!apt -y update -qq
!apt -y install -qq aria2
!pip install -q torch==1.13.1+cu117 torchvision==0.14.1+cu117 -f https://download.pytorch.org/whl/torch_stable.html
!pip install -q https://github.com/camenduru/stable-diffusion-webui-colab/releases/download/0.0.16/xformers-0.0.16+814314d.d20230118-cp38-cp38-linux_x86_64.whl
!pip install -q huggingface-hub==0.11.0 -U!git clone -b v2.0 https://github.com/camenduru/stable-diffusion-webui
!wget https://raw.githubusercontent.com/camenduru/stable-diffusion-webui-scripts/main/run_n_times.py -O /kaggle/working/stable-diffusion-webui/scripts/run_n_times.py
!git clone https://github.com/AlUlkesh/stable-diffusion-webui-images-browser /kaggle/working/stable-diffusion-webui/extensions/stable-diffusion-webui-images-browser
!git clone https://github.com/camenduru/stable-diffusion-webui-huggingface /kaggle/working/stable-diffusion-webui/extensions/stable-diffusion-webui-huggingface
!git clone -b v2.0 https://github.com/camenduru/sd-civitai-browser /kaggle/working/stable-diffusion-webui/extensions/sd-civitai-browser
!git clone https://github.com/kohya-ss/sd-webui-additional-networks /kaggle/working/stable-diffusion-webui/extensions/sd-webui-additional-networks
!git clone https://github.com/etherealxx/batchlinks-webui /kaggle/working/stable-diffusion-webui/extensions/batchlinks-webui
!git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui-nsfw-censor /kaggle/working/stable-diffusion-webui/extensions/stable-diffusion-webui-nsfw-censor
%cd /kaggle/working/stable-diffusion-webui!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/anything-v4.5-vae-swapped/resolve/main/anything-v4.5-vae-swapped.safetensors -d /kaggle/working/stable-diffusion-webui/models/Stable-diffusion -o anything-v4.5-vae-swapped.safetensors!sed -i -e 's/numpy==1.23.3/numpy==1.21.6/g' requirements_versions.txt
!sed -i -e 's/blendmodes==2022/blendmodes==2021/g' requirements_versions.txt
!sed -i -e 's/fastapi==0.90.1/fastapi==0.89.1/g' requirements_versions.txt
!sed -i -e '''/    prepare_environment()/a\    os.system\(f\"""sed -i -e ''\"s/dict()))/dict())).cuda()/g\"'' /kaggle/working/stable-diffusion-webui/repositories/stable-diffusion-stability-ai/ldm/util.py""")''' /kaggle/working/stable-diffusion-webui/launch.py!npm install -g localtunnel
def iframe_thread(port):while True:time.sleep(0.5)sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)result = sock.connect_ex(('127.0.0.1', port))if result == 0:breaksock.close()p = subprocess.Popen(["lt", "--port", "{}".format(port)], stdout=subprocess.PIPE)for line in p.stdout:print(line.decode(), end='')
threading.Thread(target=iframe_thread, daemon=True, args=(7860,)).start()# 删除原始模型
os.remove('/kaggle/working/stable-diffusion-webui/models/Stable-diffusion/anything-v4.5-vae-swapped.safetensors')
os.mkdir('/kaggle/working/stable-diffusion-webui/models/Lora')# 下载GuoFeng3模型
!wget https://civitai.com/api/download/models/36644
# 重命名文件
os.rename("/kaggle/working/stable-diffusion-webui/36644","/kaggle/working/stable-diffusion-webui/GuoFeng3.safetensors")
# 移动至主模型文件夹
shutil.move("/kaggle/working/stable-diffusion-webui/GuoFeng3.safetensors",'/kaggle/working/stable-diffusion-webui/models/Stable-diffusion')# 下载KoreanDollLikeness_v10
!wget https://civitai.com/api/download/models/22968
# 重命名文件
os.rename("/kaggle/working/stable-diffusion-webui/22968","/kaggle/working/stable-diffusion-webui/koreanDollLikeness_v10.safetensors")
# 移动至主模型文件夹
shutil.move("/kaggle/working/stable-diffusion-webui/koreanDollLikeness_v10.safetensors",'/kaggle/working/stable-diffusion-webui/models/Lora')!python launch.py --share --xformers --enable-insecure-extension-access --theme dark --gradio-queue
  • 这里我将原始的anything-v4.5-vae-swapped模型删除了,下载了GuoFeng3主干模型和KoreanDollLikenessLora模型。
  • 主干模型相当于图片的主调,比如GuoFeng3模型就是中国华丽古风风格,也可以说是一个古风游戏角色。
  • Lora模型相当于微调主干模型的某一部分,KoreanDollLikeness就是主要调整面部结构,更多具体的说明可以自行查询。
  • 粘贴好上面代码以后运行代码块,大约需要5~10分钟时间,中间可能会有警告和错误,不需要管,只要运行没有终止就可以(截止2023年5月24日上述代码可用)。
  • 部署好后会出现如下链接:
Running on local URL:  http://127.0.0.1:7860
your url is: https://rotten-cities-float.loca.lt
Running on public URL: https://7b1114ee-9fb9-42db.gradio.liveThis share link expires in 72 hours. For free permanent hosting and GPU upgrades (NEW!), check out Spaces: https://huggingface.co/spaces
  • 点击public URL中的链接https://7b1114ee-9fb9-42db.gradio.live,就可以进入gradio空间,不仅是你的电脑,任何移动联网设备都可以。
  • 关于Stable Diffusion webUI的使用,这里推荐哔哩哔哩UP主,鱼摆摆喂教程,网页地址
  • 生成的图片可以直接在图框里右击选择另存为,或者回到kaggle中/kaggle/working/stable-diffusion-webui/output文件夹中进行下载
  • 这里提供两个示例Prompt
prompt: best quality, masterpiece, highres, 1girl,blush,(seductive smile:0.8),star-shaped pupils,china hanfu,hair ornament,necklace, jewelry,Beautiful face,upon_body, tyndall effect,photorealistic, dark studio, rim lighting, two tone lighting,(high detailed skin:1.2), 8k uhd, dslr, soft lighting, high quality, volumetric lighting, candid, Photograph, high resolution, 4k, 8k, BokehNegative prompt: (((simple background))),monochrome ,lowres, bad anatomy, bad hands, text, error, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality, normal quality, jpeg artifacts, signature, watermark, username, blurry, lowres, bad anatomy, bad hands, text, error, extra digit, fewer digits, cropped, worst quality, low quality, normal quality, jpeg artifacts, signature, watermark, username, blurry, ugly,pregnant,vore,duplicate,morbid,mut ilated,tran nsexual, hermaphrodite,long neck,mutated hands,poorly drawn hands,poorly drawn face,mutation,deformed,blurry,bad anatomy,bad proportions,malformed limbs,extra limbs,cloned face,disfigured,gross proportions, (((missing arms))),(((missing legs))), (((extra arms))),(((extra legs))),pubic hair, plump,bad legs,error legs,username,blurry,bad feetSteps: 30, Sampler: Euler a, CFG scale: 9, Seed: 3556647833, Size: 640x1024, Model hash: 4078eb4174, Model: gf_anylora_gf3.2_anylora1.2, Denoising strength: 0, Clip skip: 2, ENSD: 31337, Hires upscale: 2, Hires steps: 64, Hires upscaler: R-ESRGAN 4x+ Anime6B
prompt: <lora:koreanDollLikeness_v10:0.35>,best quality ,masterpiece, illustration, an extremely delicate and beautiful, extremely detailed ,CG ,unity ,8k wallpaper, Amazing, finely detail, masterpiece,best quality,official art,extremely detailed CG unity 8k wallpaper,absurdres, incredibly absurdres, huge filesize , ultra-detailed, highres, extremely detailed,beautiful detailed girl, extremely detailed eyes and face, beautiful detailed eyes,light on face,(Hanfu:1.1),1girlNegative prompt: sketches, (worst quality:2), (low quality:2), (normal quality:2), lowres, normal quality, ((monochrome)), ((grayscale)), skin spots, acnes, skin blemishes, bad anatomy,(long hair:1.4),DeepNegative,(fat:1.2),facing away, looking away,tilted head, Multiple people, lowres,bad anatomy,bad hands, text, error, missing fingers,extra digit, fewer digits, cropped, worstquality, low quality, normal quality,jpegartifacts,signature, watermark, username,blurry,bad feet,cropped,poorly drawn hands,poorly drawn face,mutation,deformed,worst quality,low quality,normal quality,jpeg artifacts,signature,watermark,extra fingers,fewer digits,extra limbs,extra arms,extra legs,malformed limbs,fused fingers,too many fingers,long neck,cross-eyed,mutated hands,polar lowres,bad body,bad proportions,gross proportions,text,error,missing fingers,missing arms,missing legs,extra digit, extra arms, extra leg, extra foot,Steps: 35, Sampler: DPM++ SDE Karras, CFG scale: 7.5, Seed: 4047600339, Size: 672x1056, Model hash: 26d8b87829, Model: BeautyProMix, Variation seed: 1750564158, Variation seed strength: 0.1, Denoising strength: 0.57, Clip skip: 2, ENSD: 31337, Mask blur: 4

效果展示

  • 这里贴两张我自己画的,供参考细节
    请添加图片描述
    请添加图片描述

http://www.ppmy.cn/news/78469.html

相关文章

CANoe使用教程

目录 1.CAN IG使用 2.Visual Sequence 自动发送报文序列&#xff0c;可以设置多个visual sequence&#xff0c;可用于重复的网络模拟和write窗口输出&#xff0c;比IG灵活&#xff0c;比CAPL简单 3.CANoe中的系统变量和环境变量都是一种用于存储和传递数据的变量&#xff0c;…

网络安全证书合集

网络安全作为2022年十大高新紧缺技能之一&#xff0c;足以证明网络安全的重要性&#xff0c;作为网络安全工程师更是责任重大&#xff0c;证书作为能力证明的一方面&#xff0c;也是尤为重要&#xff0c;本文将着重盘点一下网络安全证书合集。 国家注册渗透测试工程师&#xf…

项目风险应对策略:项目经理应对不确定性的指南

风险应对是项目经理管理项目未来的工具箱。它可以帮助管理人员弄清楚可能会出现什么问题&#xff0c;并让他们有机会为这些问题做好准备。 对抗负面风险的5种策略 如果没有风险管理计划&#xff0c;项目可能会因意外问题或不良风险而迅速脱轨。什么策略可以用来对抗负面风险&…

【老王读SpringMVC-6】Controller method 的正常返回值处理

handler method 的返回值处理包含两个方面&#xff1a; 一是&#xff1a;返回值对象的处理&#xff1b; 二是&#xff1a;视图的渲染。 返回值对象的处理 通过前面的分析&#xff0c;我们知道&#xff0c; 被 RequestMapping 标记 handler method 的执行是通过调用 RequestMa…

Pytest模式执行python脚本不生成allure测试报告

1.安装allure 下载allure的zip安装包将allure.zip解压到python的lib目录中将allure的bin路径添加到环境变量path中(注意&#xff1a;配置环境变量后&#xff0c;一定要重启电脑。因为环境变量没生效&#xff0c;我搞了半天在pycharm不能生成报告&#xff0c;在cmd中可以生成报…

Linux中的 loop设备

loop设备是一种伪设备&#xff08;pseudo-device&#xff09;&#xff0c;或者说是仿真设备。它可以使我们在类UNIX系统上像块设备一样访问一个文件。loop设备必须与一个文件进行连接&#xff0c;才能被使用。通过这种方式&#xff0c;文件可以被mount为块设备&#xff0c;并像…

C/C++ 数据结构设计与应用(四):C++数据压缩与传输:从理论到实践的全景解析

C数据压缩与传输&#xff1a;从理论到实践的全景解析 一、数据压缩的策略与方法 (Strategies and Methods of Data Compression)1.1 数据压缩的基本概念与原理 (Basic Concepts and Principles of Data Compression)1.2 C中的数据压缩技术 (Data Compression Techniques in C)1…

【无标题】ELISA-3(加装跟踪装置)—让群体协作更智能!

群体智能是近年来发展迅速的一个人工智能学科领域&#xff0c;通过对蚂蚁、蜜蜂等为代表的社会性昆虫群体行为的研究&#xff0c;实现分布式等智能行为。作为新一代人工智能的重要方向&#xff0c;群体智能通常用于无人机、机器人集群的协同作业。目前&#xff0c;群体智能在基…