{
"title": "AI绘画工具 + Apple全家桶 = ?",
"tags": [
"post",
"AI",
"Apple",
"Diffusion model"
],
"sources": [
"xlog"
],
"external_urls": [
"https://henryq.xlog.app/AI-hui-hua-gong-ju--Apple-quan-jia-tong--"
],
"date_published": "2023-04-04T07:51:08.653Z",
"content": "## 介绍\n近日,苹果开放了[Core ML Stable Diffusion](https://github.com/apple/ml-stable-diffusion)项目,这个项目可以让用户和开发者基于原生Apple Silicon芯片运行最先进的AI绘画模型Stable Diffusion。这篇文章会基于个人使用经历介绍大致的技术背景,流程,和遇到的一些问题。\n\n![image](ipfs://bafybeigzm2gmlbo5e7evr22usruenw7kkp66heqgz3n7z4uc2vmrqz7pa4)\n\n## 背景\n图像生成领域近年最大的突破就是Diffusion系列模型的推出,具体技术细节可以看[这篇文章](https://proceedings.neurips.cc/paper_files/paper/2020/file/4c5bcfec8584af0d967f1ab10179ca4b-Paper.pdf)。\n\n原本这个领域的主要模型是[GAN](https://dl.acm.org/doi/abs/10.1145/3422622)的多个变种,但是模式崩溃和梯度爆炸导致的高训练成本问题一直存在,即使多种方法,例如Lipschitz Constraints等被应用于缓释这个问题,但依然无法与Diffusion模型相比。\n\n从Midjourney开始,扩散生成模型在这个领域的应用呈爆炸式增长,很多公司也开始将其商用化。但对于小型开发者而言,将生成模型部署到产品客户端依然是一件颇有难度的事情,至少比套皮ChatGPT接口要难多了。\n\n## 流程\n该项目主要包括了两个部分,第一部分是将[Hugging Face](https://huggingface.co/)上原本基于Torch的生成模型转编译为Core ML格式,官方已经提供了编译好的三个版本的Stable Diffusion试用。\n\n第二部分是基于此模型和对应参数进行图像的生成。与先前的工作不同,Apple似乎希望这能帮助开发者将图像生成模型集成到App开发,并提供了Swift App的版本与多个消费级终端上的测试。\n\n\n## 踩坑\n\n因为是新发布的项目,说明文档并没有涵盖所有容易踩到的坑,以下是一些容易遇到的问题:\n\n* 资源不足:消费级终端(例如我的16GB M1 Macbook Pro)需要`pip install accelerate`避免因资源不足终止运行。\n* 访问限制:直接使用本地模型也需要使用HuggingFace Token在终端`huggingface-cli login`避免访问不到服务端配置文件(该问题我已于[相应issue页面](https://github.com/apple/ml-stable-diffusion/issues/149)回复)。\n* 环境配置:建个新环境吧。\n\n## 结果\n\n下图由以下Prompt输入得到`python -m python_coreml_stable_diffusion.pipeline --prompt \"a photo of an astronaut riding a horse on mars\" --compute-unit ALL -o output --seed 93 -i models/coreml-stable-diffusion-2-base_original_packages --model-version stabilityai/stable-diffusion-2-base`\n\n![randomSeed_93_computeUnit_ALL_modelVersion_stabilityai_stable-diffusion-2-base](ipfs://bafybeigd3ruwilwb2iod736c7rrgncsrhwq26l2d5zixvspjisk4o4arnm)\n\n结果还不错,但是运行效率和官方文档给出的数据相比还是有较大的差距,看来AI普惠之路依然任重而道远。\n\n~~我们什么时候才可以让Siri帮忙生成一张用来和老板请病假的医生证明呢?~~\n\n\n#### 更多信息\nhttps://github.com/apple/ml-stable-diffusion\nhttps://huggingface.co/blog/diffusers-coreml\nhttps://github.com/huggingface/diffusers\nhttps://openaccess.thecvf.com/content/CVPR2022/html/Rombach_High-Resolution_Image_Synthesis_With_Latent_Diffusion_Models_CVPR_2022_paper.html\n\n#### 完整Prompt\n```\n(base) henry@HenrydeMacBook-Pro ml-stable-diffusion % python -m python_coreml_stable_diffusion.pipeline --prompt \"a photo of an astronaut riding a horse on mars\" --compute-unit ALL -o output.png --seed 93 -i models/coreml-stable-diffusion-2-base_original_packages --model-version stabilityai/stable-diffusion-2-base\nINFO:__main__:Setting random seed to 93\nINFO:__main__:Initializing PyTorch pipe for reference configuration\nFetching 13 files: 100%|██████████████████████████████████████████| 13/13 [00:00<00:00, 63402.27it/s]\n/Users/henry/opt/anaconda3/lib/python3.9/site-packages/transformers/models/clip/feature_extraction_clip.py:28: FutureWarning: The class CLIPFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please use CLIPImageProcessor instead.\n warnings.warn(\nWARNING:__main__:Original diffusers pipeline for stabilityai/stable-diffusion-2-base does not have a safety_checker, Core ML pipeline will mirror this behavior.\nINFO:__main__:Removed PyTorch pipe to reduce peak memory consumption\nINFO:__main__:Loading Core ML models in memory from models/coreml-stable-diffusion-2-base_original_packages\nINFO:python_coreml_stable_diffusion.coreml_model:Loading text_encoder mlpackage\nINFO:python_coreml_stable_diffusion.coreml_model:Loading models/coreml-stable-diffusion-2-base_original_packages/Stable_Diffusion_version_stabilityai_stable-diffusion-2-base_text_encoder.mlpackage\nINFO:python_coreml_stable_diffusion.coreml_model:Done. Took 19.5 seconds.\nINFO:python_coreml_stable_diffusion.coreml_model:Loading a CoreML model through coremltools triggers compilation every time. The Swift package we provide uses precompiled Core ML models (.mlmodelc) to avoid compile-on-load.\nINFO:python_coreml_stable_diffusion.coreml_model:Loading unet mlpackage\nINFO:python_coreml_stable_diffusion.coreml_model:Loading models/coreml-stable-diffusion-2-base_original_packages/Stable_Diffusion_version_stabilityai_stable-diffusion-2-base_unet.mlpackage\nINFO:python_coreml_stable_diffusion.coreml_model:Done. Took 139.2 seconds.\nINFO:python_coreml_stable_diffusion.coreml_model:Loading a CoreML model through coremltools triggers compilation every time. The Swift package we provide uses precompiled Core ML models (.mlmodelc) to avoid compile-on-load.\nINFO:python_coreml_stable_diffusion.coreml_model:Loading vae_decoder mlpackage\nINFO:python_coreml_stable_diffusion.coreml_model:Loading models/coreml-stable-diffusion-2-base_original_packages/Stable_Diffusion_version_stabilityai_stable-diffusion-2-base_vae_decoder.mlpackage\nINFO:python_coreml_stable_diffusion.coreml_model:Done. Took 6.2 seconds.\nINFO:__main__:Done.\nINFO:__main__:Initializing Core ML pipe for image generation\nWARNING:__main__:You have disabled the safety checker for <class '__main__.CoreMLStableDiffusionPipeline'> by passing `safety_checker=None`. Ensure that you abide to the conditions of the Stable Diffusion license and do not expose unfiltered results in services or applications open to the public. Both the diffusers team and Hugging Face strongly recommend to keep the safety filter enabled in all public facing circumstances, disabling it only for use-cases that involve analyzing network behavior or auditing its results. For more information, please have a look at https://github.com/huggingface/diffusers/pull/254 .\nINFO:__main__:Stable Diffusion configured to generate 512x512 images\nINFO:__main__:Done.\nINFO:__main__:Beginning image generation.\n100%|████████████████████████████████████████████████████████████████| 51/51 [00:22<00:00, 2.25it/s]\nINFO:__main__:Saving generated image to output.png/a_photo_of_an_astronaut_riding_a_horse_on_mars/randomSeed_93_computeUnit_ALL_modelVersion_stabilityai_stable-diffusion-2-base.png\n```\n\n\n\n",
"attributes": [
{
"value": "AI-hui-hua-gong-ju--Apple-quan-jia-tong--",
"trait_type": "xlog_slug"
}
]
}