Mac | iOS | Windows:安装Stable diffusion教程
Posted 山青咏芝
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mac | iOS | Windows:安装Stable diffusion教程相关的知识,希望对你有一定的参考价值。
Apple已支持的开源库:https://machinelearning.apple.com/research/stable-diffusion-coreml-apple-silicon
git clone https://github.com/apple/ml-stable-diffusion
// 下载的文件夹运行
pip install -e .
![](https://image.cha138.com/20230413/b04aa1fe4205401998265c2b55b2eb81.jpg)
接下去命令行输入指令,按提示输入
huggingface-cli login
![](https://image.cha138.com/20230413/60670a17fe77427d86fd845bb7cea2ad.jpg)
运行指令,在文件夹下运行,顺便创建一个放置ml模型的文件夹,替换下面的-o
python -m python_coreml_stable_diffusion.torch2coreml --convert-unet --convert-text-encoder --convert-vae-decoder --convert-safety-checker -o ./output_ml
--model-version runwayml/stable-diffusion-v1-5 #可以指定其他版本的diffusion模型,默认是 CompVis/stable-diffusion-v1-4
--bundle-resources-for-swift-cli #将ml文件整理成一个swift包,python生成不需要使用
--chunk-unet #ios和ipados部署需要,后面两个之后有机会我想去尝试一下在真机上的部署
--attention-implementation #在Apple芯片上的npu上实现
python -m python_coreml_stable_diffusion.torch2coreml --convert-unet --convert-text-encoder --convert-vae-decoder --convert-safety-checker -o ./sd2_ml --chunk-unet --model-version stabilityai/stable-diffusion-2-1-base --bundle-resources-for-swift-cli
![](https://image.cha138.com/20230413/90fef1a67b154de9aed443384d77d2cd.jpg)
![](https://image.cha138.com/20230413/fa752e7dc3c54ffc882e61eb692e8ee2.jpg)
python -m python_coreml_stable_diffusion.pipeline --prompt "a photo of an astronaut riding a horse on mars" -i ./output_ml -o ./output_image --compute-unit ALL --seed 93
swift run StableDiffusionSample "A photo of a little girl walking on the beach with the Jenny Turtle" --resource-path ./sd2_ml/Resources/ --seed 93 --output-path ./output_image
--model-version #如果前面修改了这个也要修改
--num-inference-steps #默认推理50次,用这个可以自定义次数
![](https://image.cha138.com/20230413/ab89b9aeaad348cfbca6d54020d7326d.jpg)
就是从上图中我们可以看到似乎无后缀的M芯片由于GPU数量较少吧我猜--compute-unit 都推荐选CPU_AND_NE,pro系列芯片选ALL,以上选CPU_AND_GPU。
![](https://image.cha138.com/20230413/59dd555a4f7b42a78aacebb9756128a3.jpg)
import SwiftUI
import StableDiffusion
import CoreML
struct ContentView: View
@State var prompt: String = "a photo of an astronaut riding a horse on mars"
@State var step = 10
@State var seed = 100
@State var image: CGImage?
@State var progress = 0.0
@State var generating = false
@State var booting = true
@State var pipeline: StableDiffusionPipeline?
private let disableSafety = false
var body: some View
VStack
if booting
Text("Initializing...")
else
if let image
Image(uiImage: UIImage(cgImage: image))
.resizable()
.scaledToFit()
if generating
ProgressView(value: progress)
if !generating
TextField("Prompt", text: $prompt)
Stepper(value: $step, in: 1...100)
Text("steps: \\(step)")
Stepper(value: $seed, in: 0...10000)
Text("Seed: \\(seed)")
Button("Generate")
progress = 0.0
image = nil
generating = true
Task.detached(priority: .high)
var images: [CGImage?]?
do
print("generate")
images = try pipeline?.generateImages(prompt: prompt, stepCount: step,seed: seed, disableSafety: disableSafety, progressHandler: progress in
print("test")
self.progress = Double(progress.step) / Double(step)
if let image = progress.currentImages.first
self.image = image
return true
)
catch let error
print(error.localizedDescription)
print("finish")
if let image = images?.first
self.image = image
generating = false
.padding()
.onAppear
Task.detached(priority: .high)
do
print(os_proc_available_memory())
guard let path = Bundle.main.path(forResource: "CoreMLModels", ofType: nil, inDirectory: nil) else
fatalError("Fatal error: failed to find the CoreML models.")
let resourceURL = URL(fileURLWithPath: path)
let config = MLModelConfiguration()
config.computeUnits = .cpuAndNeuralEngine
pipeline = try StableDiffusionPipeline(resourcesAt: resourceURL, configuration: config,reduceMemory: true)
try pipeline?.loadResources()
print("initialized pipeline")
catch let error
print("error initializing pipeline")
print(error.localizedDescription)
booting = false
struct ContentView_Previews: PreviewProvider
static var previews: some View
ContentView()
三、Windows部署
下载库:去github网址下载 - > https://github.com/CompVis/stable-diffusion
git clone https://github.com/CompVis/stable-diffusion.git
配置环境和文件
简单的操作,在下载后的文件夹下直接输下面两个:
conda env create -f environment.yaml
conda activate ldm
pip install transformers==4.19.2 diffusers invisible-watermark
pip install -e .
pip install OmegaConf einops taming-transformers pytorch-lighnting clip kornia
问题
第一个问题官网是有说明的,但就是这个下载地址藏得很深不好找。
找了一会才找到https://huggingface.co/CompVis/stable-diffusion-v-1-4-original,把这个sd-v1-4.ckpt文件下载下来,随便哪个都行,大概四个G。
下载模型文件放置到这个文件夹下,就是上面官方说明的那个位置,命名成model.ckpt。
运行下面的代码,不出意外会报错。
python scripts/txt2img.py --prompt "a photograph of an astronaut riding a horse" --plms
问题好像是原作者修改了这个库,将你的quantize.py(报错的信息里包含文件所在的绝对路径)文件替换成这个网址的文件
https://github.com/CompVis/taming-transformers/blob/master/taming/modules/vqvae/quantize.py
再运行一次又报错
现在有一个简单的办法了,感谢数学系之耻的建议,直接降精度就可以释放显存了。如下修改txt2img.py文件第244行
效果图:
或者接下去看比较复杂的方法!
不知道需要多大的内存才可以,网上找到方法是用优化的库。网上还有一种办法说注释掉什么安全检查的我试了没有变化。
https://github.com/basujindal/stable-diffusion
下载完后在新的库上也是需要安装一些环境,在新的文件夹下运行下面的安装代码
pip install -e .
优化的库代码放在optimizedSD文件夹下,也保留了之前的源代码,不要搞错了。
重新安装一下这个优化库的环境,将ckpt放到对应的位置。
python optimizedSD/optimized_txt2img.py --prompt "Cyberpunk style image of a Tesla car reflection in rain" --H 512 --W 512 --seed 27 --n_iter 2 --n_samples 5 --ddim_steps 50
运行后报这个错。查了一下,好像是最近优化的作者也换了一个库https://github.com/basujindal/stable-diffusion/issues/175 用下面的办法就能解决了。
pip install git+https://github.com/crowsonkb/k-diffusion.git
然后打开编辑optimizedSD/ddpm.py文件,将from samplers…改成上面图片的三个from k_diffusion…然后贫穷的显卡的电脑也就可以跑了,不说了要努力搬砖买24g的显卡了。
在Windows下怎么 如何用vm虚拟机安装mac苹果操作系统 如何启动mac 苹果电脑root用户账户
在Windows下用 vm 虚拟机安装 mac 苹果操作系统,苹果电脑启动 root 用户账号
首先下载两个文件一个是unlocker,下载地址:链接:http://pan.baidu.com/s/1qX8jBkg 密码:6nyr
一个是OS X 10.11.1(15B42).cdr:下载地址:链接:http://pan.baidu.com/s/1o8Fp4Lk 密码:tjvi
1 创建虚拟机,点击新建虚拟机选择典型
2 现在安装程序光盘
3 在下载的目录中选择所有文件,才能看见cdr文件
4 选择apple mac
5 自定路径后,设置虚拟机磁盘大小,选择将虚拟机磁盘存储为单个文件
6 自己配置处理器和内存
7 配置完后,启动虚拟机,将会出现以下错误
8 将下载的unlocker解压虚拟机安装的同一父目录下即可,比如我的就解压到G盘下就可以,不是解压到mac目录下,右键点击
用管理员权限运行
9 找到虚拟机的目录,用记事本打开下面文件
10 在第五行后面添加 smc.version=0,保存后退出
再次启动虚拟机就可以正常安装了,在选择磁盘分区时
11 选择抹掉,其他默认,然后抹掉
12 抹掉之后就返回
13 选择未命名,继续
14 后面就是选择输入法等,自行配置,默认安装完是没有启动root用户的
下启动root用户过程
1 点击设置,群组和用户
2 点击 点按锁按钮以进行更改,输入刚才安装过程中用户的密码
3 依次点击
4 按顺序点击,即可启用root用户,并未root用户设置密码
5 最后,如果觉得安装实在太麻烦,那么可以直接下载下载已经安装好的vm文件,下载后直接用vm虚拟机打开就可以用啦
下载地址:链接:http://pan.baidu.com/s/1gf3YxQN 密码:bfcr
以上是关于Mac | iOS | Windows:安装Stable diffusion教程的主要内容,如果未能解决你的问题,请参考以下文章
Windows下虚拟机安装Mac OS X ----- VM12安装Mac OS X 10.11
在 Visual Studio 中使用 C# 或 VB 为 Mac、Windows 或 iOS 创建桌面应用程序 [关闭]