专业企业网站建设公司价格,微小店网站建设多少钱,安丘网站建设,广州手机网站设计本篇文章聊聊如何通过 Docker 和八十行左右的 Python 代码#xff0c;实现一款类似 Midjourney 官方图片解析功能 Describe 的 Prompt 工具。
让你在玩 Midjourney、Stable Diffusion 这类模型时#xff0c;不再为生成 Prompt 描述挠头。
写在前面
本文将提供两个版本的工…本篇文章聊聊如何通过 Docker 和八十行左右的 Python 代码实现一款类似 Midjourney 官方图片解析功能 Describe 的 Prompt 工具。
让你在玩 Midjourney、Stable Diffusion 这类模型时不再为生成 Prompt 描述挠头。
写在前面
本文将提供两个版本的工具分别支持 CPU 和 GPU 推理使用如果你有一张大于 8GB 显存的显卡可以愉快的使用全部的功能如果你只有 CPU那么也可以使用 CPU 版本的应用来进行偷懒。
本篇文章的代码已上传至 GitHub soulteary/docker-prompt-generator欢迎自取以及“一键三连”。 昨晚在玩 Midjourney 的时候在想 Prompt 的时候想到挠头。作为一个懒人计上心头能不能让模型帮我生成 Prompt 呢输入一些关键词或者句子然后让程序帮助我完成完整的 Prompt 内容。俗话文生文于是我开了个坑创建了上面的这个开源项目在简单验证可行性之后就去补觉了。
一觉起来看到有着相同兴趣爱好的同事转发了一篇文章Midjourney 发布了新功能“describe”支持解析图片为几段不同的 Prompt 文本并支持继续进行图片生成。俗话图生文然后文生图 这个功能相比昨晚折腾的小东西显然更能体现先进的生产效率嘛作为懒人体验非常好。
可惜网上扫了一圈发现官方功能并不开源那么我来实现一个吧。
“作图咒语生成器” 的使用
为了更快的上手和使用到这个工具我们需要先完成环境的配置。
应用和 Docker 环境准备
在过去的几篇文章里我提到过了我个人习惯和推荐的开发环境基于 Docker 和 Nvidia 官方基础容器的深度学习环境所以就不再赘述相关知识点感兴趣可以自行翻阅比如这篇《基于 Docker 的深度学习环境入门篇》。相信老读者应该已经很熟悉啦。
当然因为本文包含纯 CPU 也能玩的部分你也可以参考几个月前的《在搭载 M1 及 M2 芯片 MacBook设备上玩 Stable Diffusion 模型》来配置你的环境。
在准备好 Docker 环境的配置之后我们就可以继续玩啦。
我们随便找一个合适的目录使用 git clone 或者下载 Zip 压缩包的方式先把“Docker Prompt Generator(Docker 作图咒语生成器)”项目的代码下载到本地。
git clone https://github.com/soulteary/docker-prompt-generator.git
# or
curl -sL -o docker-prompt-generator.zip https://github.com/soulteary/docker-prompt-generator/archive/refs/heads/main.zip接着进入项目目录使用 Nvidia 原厂的 PyTorch Docker 基础镜像来完成基础环境的构建相比于我们直接从 DockerHub 拉制作好的镜像自行构建将能节约大量时间。
我们在项目目录中执行下面的命令就能够完成应用模型应用的构建啦
# 构建基础镜像
docker build -t soulteary/prompt-generator:base . -f docker/Dockerfile.base# 构建 CPU 应用
docker build -t soulteary/prompt-generator:cpu . -f docker/Dockerfile.cpu# 构建 GPU 应用
docker build -t soulteary/prompt-generator:gpu . -f docker/Dockerfile.gpu然后根据你的硬件环境选择性执行下面的命令就能够启动一个带有 Web UI 界面的模型应用啦。
# 运行 CPU 镜像
docker run --gpus all --ipchost --ulimit memlock-1 --ulimit stack67108864 --rm -it -p 7860:7860 soulteary/prompt-generator:cpu# 运行 GPU 镜像
docker run --gpus all --ipchost --ulimit memlock-1 --ulimit stack67108864 --rm -it -p 7860:7860 soulteary/prompt-generator:gpu我们在浏览器中输入运行容器的宿主机的 IP 地址就能够开始使用工具啦。
使用工具
工具的使用非常简单分别有使用“图片生成描述”和使用“文本生成描述”两种。
我找了一张之前模型生成的图片然后将这张图片喂给这个程序点击按钮就能获得图片的描述文本啦。 我们可以在 Midjourney 或者 Stable Diffusion 中直接使用这段文本来继续生成图片或者使用“从文本中生成”来扩展内容让内容更适合 Midjourney 这类应用。
为了体现工具的中文翻译和续写能力我们单独写一段简单的中文描述“一只小鸟立梢头一轮明月当空照一片黄叶铺枝头”。 可以看到基于我们的输入内容生成了非常多不同的文本。
想要验证文本内容是否符合原意我们可以将内容粘贴到 Midjourney 中进行测试。 因为模型存在随机性如果想要得到更好的结果还需要对描述进行更多的调整优化不过看起来工具解析图片生成的描述其实是能够做到开箱即用的而根据我们的三言两语生成的文本也生成出了符合要求的图片。 好啦工具的基础使用我们介绍完啦。
模型应用功能实现
下面是工具的实现流程和思考如果你想学习或快速使用开源模型项目来构建你的 AI 容器应用可以继续浏览。
应用功能设计
在“动手”前我们需要先明确功能设计以及考虑使用什么样的技术来做具体功能的技术支撑。
在我日常使用 Stable Diffusion、Midjourney 的过程中时常有三个场景挠头
我只有一些关键词需要发挥想象力把关键词串起来然后喂给模型应用。如果描述内容不够好或者关键词之间的关联比较远那么图片的生成效果就不会特别好。我有一张图片想让模型围绕图片中的内容比如构图、某些元素、情感等进行二次创作而不是简单的做图片中的元素替换。我更习惯使用中文做描述而不是英文但是目前模型生成图片想要好的效果需要使用英文总是借助翻译工具切换程序界面或者网页还是挺麻烦的。
解决第一个问题我们可以使用最近火爆出圈的 GPT-4 的前辈的前辈GPT-2 其实就能够满足需求将内容一句话、几个关键词进行快速续写。相比较使用 GPT-3 / GPT-4无需联网也无需付费模型文件更是“便宜大碗”用 CPU 就能跑起来。
解决第二个问题我们可以使用 OpenAI 在一年前推出的 CLIP 神经网络模型以及 Salesforce 推出的 BLIP 能够从图片中抽取出最合适的描述文本让我们用在新的 AIGC 图片生成任务中。稍作优化调整我们只需要大概使用 68GB 显存就能将这部分功能的模型跑起来。
解决第三个问题我们可以使用赫尔辛基大学开源的 OPUS MT 模型实现将中文翻译为英文进一步偷懒以及解决上面两类原始模型不支持中文输入的问题。
因为前两个场景问题中的模型不支持中文而我又是一个懒人不想输入英文来玩图所以我们先来解决第三个问题让整个应用实现流程更丝滑。
中文 Prompt 翻译为英文 Prompt 功能
想要实现第一个懒人功能从用户输入的中文内容中自动生成英文我们需要使用中英双语的翻译模型。赫尔辛基大学的开源组织将预训练模型开放在了 HuggingFace 社区Helsinki-NLP/opus-mt-zh-en。
我们可以通过写十五行简单的 Python 代码来完成模型文件的下载以及实现将中文自动转换为合适的英文内容的功能。比如下面的例子中程序运行完毕将输出《火影忍者》中的金句“青春不能回头所以青春没有终点”的译文。
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
import torchmodel AutoModelForSeq2SeqLM.from_pretrained(Helsinki-NLP/opus-mt-zh-en).eval()
tokenizer AutoTokenizer.from_pretrained(Helsinki-NLP/opus-mt-zh-en)def translate(text):with torch.no_grad():encoded tokenizer([text], return_tensorspt)sequences model.generate(**encoded)return tokenizer.batch_decode(sequences, skip_special_tokensTrue)[0]input 青春不能回头所以青春没有终点。 ——《火影忍者》
print(input, translate(input))将上面的代码保存为 translate.py然后执行 python translate.py等待模型下载完毕我们将得到类似下面的结果
青春不能回头所以青春没有终点。 Youth cant turn back, so theres no end to youth.是不是看起来还不错这部分代码保存在了项目中的 soulteary/docker-prompt-generator/app/translate.py。
接下来我们来实现 Prompt “免费续杯”有逻辑续写功能。
实现 MidJourney Prompt 续写功能
基于一些内容进行继续的内容生成是生成类模型的看家本领比如大家已经熟悉的不能再熟悉的 ChatGPT 背后的 GPT 模型系列。
作为一个懒人我在网上寻觅了一番找到了一个 Google 离职创业的“国外大姐” 基于 GPT-2 使用 25 万条 MidJourney 数据 fine-tune 好的 GPT2 模型succinctly/text2image-prompt-generator简单试了试了试效果还不错那么我们就用它来实现这部分功能吧。其实用前几篇文章里的 LLaMA 也行可以自行替换。
和上面一样我们实现一个不到 30 行的简单的程序就能够实现模型自动下载以及调用模型根据我们的输入内容上文中热血台词的翻译生成一些符合 Midjourney 或 Stable Diffusion 的新的 Prompt 内容
from transformers import pipeline, set_seed
import random
import retext_pipe pipeline(text-generation, modelsuccinctly/text2image-prompt-generator)def text_generate(input):seed random.randint(100, 1000000)set_seed(seed)for count in range(6): sequences text_pipe(input, max_lengthrandom.randint(60, 90), num_return_sequences8)list []for sequence in sequences:line sequence[generated_text].strip()if line ! input and len(line) (len(input) 4) and line.endswith((:, -, —)) is False:list.append(line)result \n.join(list)result re.sub([^ ]\.[^ ],, result)result result.replace(, ).replace(, )if result ! :return resultif count 5:return resultinput Youth cant turn back, so theres no end to youth.
print(input, text_generate(input))我们将上面的代码保存为 text-generation.py然后执行 python text-generation.py稍等片刻我们将得到类似下面的内容
# Setting pad_token_id to eos_token_id:50256 for open-end generation.
Youth cant turn back, so theres no end to youth. Youth cant turn back, so theres no end to youth. Young, handsome, confident, lonely boy sitting on his cant turn back, so theres no end to youth. Where old yang waits, young man on the streets of Bangkok::10 film poster::10 photorealism, postprocessing, low angle::10 Trending on artstation::8 —ar 47:82
Youth cant turn back, so theres no end to youth. By Karel Thole and Mike Mignola --ar 2:3
Youth cant turn back, so theres no end to youth. And there is a bright hope about a future where there will be time.内容看起来好像还不错我们直接在 Midjourney 中输入测试将得到类似下面的结果。 看起来算是及格了这部分代码保存在项目的 soulteary/docker-prompt-generator/app/text-generation.py中有需要可以自取。
完成了两个功能之后我们来实现根据图片内容生成 Prompt 描述的应用功能。
实现根据图片生成 Prompt 描述功能
相比较上面两个功能使用 CPU 就能搞定内容生成效率也非常高。
但是想要快速的根据图片生成 Prompt 则需要显卡的支持。不过根据我的试验运行起来只需要 68GB 左右的显存还是比较省钱的。没有显卡可以使用云服务器代替买个按量的玩罢销毁即可。
这里我们依旧是实现一段简单的不到 30 行的 Python 代码完成模型下载、应用加载、图片下载以及将图片转换为 Prompt 的功能
from clip_interrogator import Config, Interrogator
import torch
config Config()
config.device cuda if torch.cuda.is_available() else cpu
config.blip_offload False if torch.cuda.is_available() else True
config.chunk_size 2048
config.flavor_intermediate_count 512
config.blip_num_beams 64
config.clip_model_name ViT-H-14/laion2b_s32b_b79k
ci Interrogator(config)def get_prompt_from_image(image):return ci.interrogate(image.convert(RGB))import requests
import shutil
r requests.get(https://pic1.zhimg.com/v2-6e056c49362bff9af1eb39ce530ac0c6_1440w.jpg?sourced16d100b, streamTrue)
if r.status_code 200:with open(./image.jpg, wb) as f:r.raw.decode_content Trueshutil.copyfileobj(r.raw, f) from PIL import Image
print(get_prompt_from_image(Image.open(./image.jpg)))代码中的图片使用了我专栏中上一篇文章的题图同样使用 Midjourney 生成。将上面的内容保存为 clip.py然后执行 python clip.py稍等片刻我们将得到类似下面的结果
# WARNING:root:Pytorch pre-release version 1.14.0a0410ce96 - assuming intent to test it
Loading BLIP model...
load checkpoint from https://storage.googleapis.com/sfr-vision-language-research/BLIP/models/model_large_caption.pth
Loading CLIP model...
Loaded CLIP model and data in 8.29 seconds.
100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 55/55 [00:0000:00, 316.23it/s]
Flavor chain: 38%|███████████████████████████████████████████████████████▏ | 12/32 [00:0400:07, 2.74it/s]
100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 55/55 [00:0000:00, 441.49it/s]
100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 6/6 [00:0000:00, 346.74it/s]
100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 50/50 [00:0000:00, 457.84it/s]a robot with a speech bubble on a blue background, highly detailed hyper real retro, artificial intelligence!!, toy photography, by Emma Andijewska, markings on robot, computer generated, blueish, delete, small gadget, animated, blue body, in retro colors从结果中看描述还是比较准确的。这部分代码我保存在了项目的 soulteary/docker-midjourney-prompt-generator/app/clip.py。
好啦到目前为止三个主要功能我们就都实现完毕了。接下来我们借助 Docker 和 Gradio 来完成 Web UI 和一键运行的模型容器应用。
使用 Docker 构建 AI 应用容器
接下来我们来完成 AI 应用的容器构建和相关代码编写。
前文中提到我们将实现两个版本的应用分别支持 CPU 和 GPU 来完成快速的 AI 模型推理功能。因为后者可以向下兼容前者所以我们先来实现一个包含前两个应用功能CPU 就能跑的模型基础镜像。
完成只需要 CPU 运行的应用容器镜像
结合上文中的代码Dockerfile 文件不难编写
FROM nvcr.io/nvidia/pytorch:22.12-py3
LABEL org.opencontainers.image.authorssoultearygmail.comRUN pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple \pip install transformers sentencepiece sacremoses \pip install gradioWORKDIR /appRUN cat /get-models.py EOF
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
AutoModelForSeq2SeqLM.from_pretrained(Helsinki-NLP/opus-mt-zh-en)
AutoTokenizer.from_pretrained(Helsinki-NLP/opus-mt-zh-en)
pipeline(text-generation, modelsuccinctly/text2image-prompt-generator)
EOFRUN python /get-models.py \rm -rf /get-models.py将上面的内容保存为 Dockerfile.base然后使用 docker build -t soulteary/prompt-generator:base . -f Dockerfile.base 稍等片刻包含了模型文件的基础应用模型就搞定啦。
[] Building 189.5s (7/8) [internal] load .dockerignore 0.0s transferring context: 2B 0.0s [internal] load build definition from Dockerfile.base 0.0s transferring dockerfile: 692B 0.0s [internal] load metadata for nvcr.io/nvidia/pytorch:22.12-py3 0.0s [1/5] FROM nvcr.io/nvidia/pytorch:22.12-py3 0.0s CACHED [2/5] RUN pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple pip install transformers sentencepiece sacremoses pip install gradio 0.0s CACHED [3/5] WORKDIR /app 0.0s CACHED [4/5] RUN cat /get-models.py EOF 0.0s [5/5] RUN python /get-models.py rm -rf /get-models.py 189.4s # Downloading (…)olve/main/source.spm: 100%|██████████| 805k/805k [00:0600:00, 130kB/s] # Downloading (…)olve/main/target.spm: 100%|██████████| 807k/807k [00:0100:00, 440kB/s] # Downloading (…)olve/main/vocab.json: 100%|██████████| 1.62M/1.62M [00:0100:00, 1.21MB/s] # Downloading (…)lve/main/config.json: 100%|██████████| 907/907 [00:0000:00, 499kB/s] # Downloading pytorch_model.bin: 100%|██████████| 665M/665M [00:1100:00, 57.2MB/s] # Downloading (…)okenizer_config.json: 100%|██████████| 255/255 [00:0000:00, 81.9kB/s]实现过程中我这边的构建时间大概要 5 分钟左右可以从椅子上起来动一动听首歌放松一会。
镜像构建完毕可以使用下面的命令进入包含模型和 PyTorch 环境的 Docker 镜像。在这个镜像中我们可以自由的使用前两个功能相关的模型
docker run --gpus all --ipchost --ulimit memlock-1 --ulimit stack67108864 --rm -it -p 7680:7680 soulteary/prompt-generator:base bash有了环境之后我们来继续实现一个简单的 Web UI实现上文中的懒人功能让模型根据我们输入的中文内容生成可以绘制高质量图片的 Prompt
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
import torchmodel AutoModelForSeq2SeqLM.from_pretrained(Helsinki-NLP/opus-mt-zh-en).eval()
tokenizer AutoTokenizer.from_pretrained(Helsinki-NLP/opus-mt-zh-en)def translate(text):with torch.no_grad():encoded tokenizer([text], return_tensorspt)sequences model.generate(**encoded)return tokenizer.batch_decode(sequences, skip_special_tokensTrue)[0]from transformers import pipeline, set_seed
import random
import retext_pipe pipeline(text-generation, modelsuccinctly/text2image-prompt-generator)def text_generate(input):seed random.randint(100, 1000000)set_seed(seed)text_in_english translate(input)for count in range(6): sequences text_pipe(text_in_english, max_lengthrandom.randint(60, 90), num_return_sequences8)list []for sequence in sequences:line sequence[generated_text].strip()if line ! text_in_english and len(line) (len(text_in_english) 4) and line.endswith((:, -, —)) is False:list.append(line)result \n.join(list)result re.sub([^ ]\.[^ ],, result)result result.replace(, ).replace(, )if result ! :return resultif count 5:return resultimport gradio as grwith gr.Blocks() as block:with gr.Column():with gr.Tab(文本生成):input gr.Textbox(lines6, label你的想法, placeholder在此输入内容...)output gr.Textbox(lines6, label生成的 Prompt)submit_btn gr.Button(快给我编)submit_btn.click(fntext_generate,inputsinput,outputsoutput)block.queue(max_size64).launch(show_apiFalse, enable_queueTrue, debugTrue, shareFalse, server_name0.0.0.0)在容器环境中创建一个名为 webui.cpu.py 的文件然后使用 python webui.cpu.py将看到类似下面的日志输出
Running on local URL: http://0.0.0.0:7860To create a public link, set shareTrue in launch().然后我们在浏览器中打开容器所在设备的 IP 如果在本机运行可以访问 http://127.0.0.1:7860 就能访问 Web 服务啦。 我们在上面的输入框里输入一些内容然后点击“快给我编”按钮就能够得到一堆模型编出来的 Prompt 内容啦。
实现完“文生文”功能之后我们来实现“图生文”相关功能。
完成需要 GPU 运行的应用容器镜像
结合上文完成 GPU 相关功能需要的容器环境也不难
FROM soulteary/prompt-generator:base
LABEL org.opencontainers.image.authorssoultearygmail.comRUN pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple \pip install clip_interrogator githttps://github.com/pharmapsychotic/BLIP.gitlib#eggblipRUN cat /get-models.py EOF
from clip_interrogator import Config, Interrogator
import torch
config Config()
config.device cuda if torch.cuda.is_available() else cpu
config.blip_offload False if torch.cuda.is_available() else True
config.chunk_size 2048
config.flavor_intermediate_count 512
config.blip_num_beams 64
config.clip_model_name ViT-H-14/laion2b_s32b_b79k
ci Interrogator(config)
EOFRUN python /get-models.py \rm -rf /get-models.py将上面的内容保存为 Dockerfile.gpu 文件然后使用 docker build -t soulteary/prompt-generator:gpu . -f Dockerfile.gpu 完成镜像的构建。
耐心等待镜像构建完毕使用下面的命令能够进入包含三种模型和 PyTorch 环境的 Docker 镜像
docker run --gpus all --ipchost --ulimit memlock-1 --ulimit stack67108864 --rm -it -p 7680:7680 soulteary/prompt-generator:gpu bash接着来编写能够调用三种模型能力的 Python 程序
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
import torchmodel AutoModelForSeq2SeqLM.from_pretrained(Helsinki-NLP/opus-mt-zh-en).eval()
tokenizer AutoTokenizer.from_pretrained(Helsinki-NLP/opus-mt-zh-en)def translate(text):with torch.no_grad():encoded tokenizer([text], return_tensorspt)sequences model.generate(**encoded)return tokenizer.batch_decode(sequences, skip_special_tokensTrue)[0]from transformers import pipeline, set_seed
import random
import retext_pipe pipeline(text-generation, modelsuccinctly/text2image-prompt-generator)def text_generate(input):seed random.randint(100, 1000000)set_seed(seed)text_in_english translate(input)for count in range(6): sequences text_pipe(text_in_english, max_lengthrandom.randint(60, 90), num_return_sequences8)list []for sequence in sequences:line sequence[generated_text].strip()if line ! text_in_english and len(line) (len(text_in_english) 4) and line.endswith((:, -, —)) is False:list.append(line)result \n.join(list)result re.sub([^ ]\.[^ ],, result)result result.replace(, ).replace(, )if result ! :return resultif count 5:return resultfrom clip_interrogator import Config, Interrogator
import torch
import gradio as grconfig Config()
config.device cuda if torch.cuda.is_available() else cpu
config.blip_offload False if torch.cuda.is_available() else True
config.chunk_size 2048
config.flavor_intermediate_count 512
config.blip_num_beams 64
config.clip_model_name ViT-H-14/laion2b_s32b_b79kci Interrogator(config)def get_prompt_from_image(image, mode):image image.convert(RGB)if mode best:prompt ci.interrogate(image)elif mode classic:prompt ci.interrogate_classic(image)elif mode fast:prompt ci.interrogate_fast(image)elif mode negative:prompt ci.interrogate_negative(image)return promptwith gr.Blocks() as block:with gr.Column():gr.HTML(h1MidJourney / SD2 懒人工具/h1)with gr.Tab(从图片中生成):with gr.Row():input_image gr.Image(typepil)with gr.Column():input_mode gr.Radio([best, fast, classic, negative], valuebest, labelMode)img_btn gr.Button(这图里有啥)output_image gr.Textbox(lines6, label生成的 Prompt)with gr.Tab(从文本中生成):input_text gr.Textbox(lines6, label你的想法, placeholder在此输入内容...)output_text gr.Textbox(lines6, label生成的 Prompt)text_btn gr.Button(快给我编)img_btn.click(fnget_prompt_from_image, inputs[input_image, input_mode], outputsoutput_image)text_btn.click(fntext_generate, inputsinput_text, outputsoutput_text)block.queue(max_size64).launch(show_apiFalse, enable_queueTrue, debugTrue, shareFalse, server_name0.0.0.0)我们将上面的程序保存为 webui.gpu.py然后使用 python webui.gpu.py 运行程序将得到类似下面的日志
██████████████████████████████████████████████████████████████████████████████████████████████████████████████| 44.0/44.0 [00:0000:00, 31.5kB/s]
Downloading: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 786k/786k [00:0100:00, 772kB/s]
Downloading: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 788k/788k [00:0000:00, 863kB/s]
Downloading: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1.54M/1.54M [00:0100:00, 1.29MB/s]
Downloading: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 907/907 [00:0000:00, 618kB/s]
Downloading: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 634M/634M [00:2700:00, 23.8MB/s]
Downloading: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 255/255 [00:0000:00, 172kB/s]
Downloading: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 779k/779k [00:0100:00, 757kB/s]
Downloading: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 446k/446k [00:0000:00, 556kB/s]
Downloading: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 2.01M/2.01M [00:0100:00, 1.60MB/s]
Downloading: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 99.0/99.0 [00:0000:00, 69.2kB/s]
I0405 12:50:42.798199 140240289830720 instantiator.py:21] Created a temporary directory at /tmp/tmpuvpi8s9q
I0405 12:50:42.798363 140240289830720 instantiator.py:76] Writing /tmp/tmpuvpi8s9q/_remote_module_non_scriptable.py
W0405 12:50:42.878760 140240289830720 version.py:27] Pytorch pre-release version 1.14.0a0410ce96 - assuming intent to test it
I0405 12:50:43.373221 140240289830720 font_manager.py:1633] generated new fontManager
Loading BLIP model...
load checkpoint from https://storage.googleapis.com/sfr-vision-language-research/BLIP/models/model_large_caption.pth
Loading CLIP model...
I0405 12:51:00.455630 140240289830720 factory.py:158] Loaded ViT-H-14 model config.
I0405 12:51:06.642275 140240289830720 factory.py:206] Loading pretrained ViT-H-14 weights (laion2b_s32b_b79k).
Loaded CLIP model and data in 8.22 seconds.
Running on local URL: http://0.0.0.0:7860To create a public link, set shareTrue in launch().当看到 Running on local URL: http://0.0.0.0:7860 的日志的时候我们就可以在浏览器中访问程序啦。 将上文中的图片投喂给它然后点下“这图里有啥”按钮稍等片刻我们将得到一些比较合理的 Prompts 内容你可以用这些内容去生成图片。 当然你也可以将生成的文本内容再投喂给它来获得更多的 Prompt 内容让图片的变化更丰富一些。
其他显存资源消耗
在模型识别图片的过程中我简单记录了应用的显存消耗峰值大概在 8GB 左右。
Wed Apr 5 21:00:09 2023
-----------------------------------------------------------------------------
| NVIDIA-SMI 525.89.02 Driver Version: 525.89.02 CUDA Version: 12.0 |
|---------------------------------------------------------------------------
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
||
| 0 NVIDIA GeForce ... Off | 00000000:01:00.0 Off | Off |
| 31% 35C P8 23W / 450W | 8111MiB / 24564MiB | 0% Default |
| | | N/A |
--------------------------------------------------------------------------------------------------------------------------------------------------------
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
||
| 0 N/A N/A 1286 G /usr/lib/xorg/Xorg 9MiB |
| 0 N/A N/A 1504 G /usr/bin/gnome-shell 10MiB |
| 0 N/A N/A 115252 C python 8086MiB |
-----------------------------------------------------------------------------
最后
好了这篇文章就先聊到这里啦。
下一篇文章或许该揭示之前 fine-tune 的大模型的效果啦最近一直很忙直到今天才有空折腾模型。
–EOF 我们有一个小小的折腾群里面聚集了一些喜欢折腾的小伙伴。
在不发广告的情况下我们在里面会一起聊聊软硬件、HomeLab、编程上的一些问题也会在群里不定期的分享一些技术资料。
喜欢折腾的小伙伴欢迎阅读下面的内容扫码添加好友。
关于“交友”的一些建议和看法
添加好友时请备注实名和公司或学校、注明来源和目的否则不会通过审核。
关于折腾群入群的那些事 本文使用「署名 4.0 国际 (CC BY 4.0)」许可协议欢迎转载、或重新修改使用但需要注明来源。 署名 4.0 国际 (CC BY 4.0)
本文作者: 苏洋
创建时间: 2023年04月05日 统计字数: 10422字 阅读时间: 21分钟阅读 本文链接: https://soulteary.com/2023/04/05/eighty-lines-of-code-to-implement-the-open-source-midjourney-and-stable-diffusion-spell-drawing-tool.html