From 24da1a1db479f26d2b854102d817baf4fa412b6e Mon Sep 17 00:00:00 2001 From: chzhang Date: Sat, 5 Nov 2022 19:36:49 +0800 Subject: stable diffusion 01 --- diffusion/tutorials/01_tut.ipynb | 308 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 308 insertions(+) create mode 100644 diffusion/tutorials/01_tut.ipynb (limited to 'diffusion/tutorials/01_tut.ipynb') diff --git a/diffusion/tutorials/01_tut.ipynb b/diffusion/tutorials/01_tut.ipynb new file mode 100644 index 0000000..bcb9077 --- /dev/null +++ b/diffusion/tutorials/01_tut.ipynb @@ -0,0 +1,308 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "3306ed06", + "metadata": {}, + "source": [ + "## 1. 注册及token" + ] + }, + { + "cell_type": "markdown", + "id": "c132f4d2", + "metadata": {}, + "source": [ + "- 注册:\n", + " - https://huggingface.co\n", + "- token\n", + " - https://huggingface.co/settings/tokens\n", + "\n", + "- 安装 huggingface 命令行工具:\n", + " - `$ python -m pip install huggingface_hub`\n", + " - `$ huggingface-cli login`\n", + "\n", + "- huggingface 获取 CompVis 相关模型的访问权限\n", + " - https://huggingface.co/CompVis/stable-diffusion-v1-4" + ] + }, + { + "cell_type": "markdown", + "id": "99e0b5e9", + "metadata": {}, + "source": [ + "## 2. github 项目" + ] + }, + { + "cell_type": "markdown", + "id": "f8ee18a3", + "metadata": {}, + "source": [ + "- git clone 到本地\n", + " - `git clone https://github.com/CompVis/stable-diffusion`\n", + "- 配置虚拟环境 `ldm`(latent diffusion model)\n", + " \n", + "```\n", + "conda env create -f environment.yaml\n", + "conda activate ldm\n", + "```" + ] + }, + { + "cell_type": "markdown", + "id": "77e4ea71", + "metadata": {}, + "source": [ + "## 3. 生成第一个图片" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "0bca3d05", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/home/chzhang/anaconda3/envs/ldm/lib/python3.8/site-packages/tqdm/auto.py:22: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n", + " from .autonotebook import tqdm as notebook_tqdm\n" + ] + } + ], + "source": [ + "from torch import autocast" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "83d22a58", + "metadata": {}, + "outputs": [], + "source": [ + "from diffusers import StableDiffusionPipeline" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "eac64ab0", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Fetching 16 files: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 16/16 [00:00<00:00, 29127.11it/s]\n", + "The config attributes {'clip_sample': False} were passed to PNDMScheduler, but are not expected and will be ignored. Please verify your scheduler_config.json configuration file.\n" + ] + } + ], + "source": [ + "pipe = StableDiffusionPipeline.from_pretrained(\n", + "\t\"CompVis/stable-diffusion-v1-4\",\n", + "# revision='fp32',\n", + "# revision='fp16'\n", + "\tuse_auth_token=True\n", + ").to(\"cuda\")" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "55d7a9ec", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "StableDiffusionPipeline {\n", + " \"_class_name\": \"StableDiffusionPipeline\",\n", + " \"_diffusers_version\": \"0.6.0\",\n", + " \"feature_extractor\": [\n", + " \"transformers\",\n", + " \"CLIPFeatureExtractor\"\n", + " ],\n", + " \"safety_checker\": [\n", + " \"stable_diffusion\",\n", + " \"StableDiffusionSafetyChecker\"\n", + " ],\n", + " \"scheduler\": [\n", + " \"diffusers\",\n", + " \"PNDMScheduler\"\n", + " ],\n", + " \"text_encoder\": [\n", + " \"transformers\",\n", + " \"CLIPTextModel\"\n", + " ],\n", + " \"tokenizer\": [\n", + " \"transformers\",\n", + " \"CLIPTokenizer\"\n", + " ],\n", + " \"unet\": [\n", + " \"diffusers\",\n", + " \"UNet2DConditionModel\"\n", + " ],\n", + " \"vae\": [\n", + " \"diffusers\",\n", + " \"AutoencoderKL\"\n", + " ]\n", + "}" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pipe" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "4f07fd46", + "metadata": {}, + "outputs": [], + "source": [ + "prompt = 'a photo of a chinese woman riding a horse on mars'" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "8a30d00a", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 51/51 [00:12<00:00, 4.19it/s]\n" + ] + } + ], + "source": [ + "with autocast('cuda'):\n", + " output = pipe(prompt)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "547d0e63", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "StableDiffusionPipelineOutput(images=[], nsfw_content_detected=[False])" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "output" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "679a12f1", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[]" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "output['images']" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "668beeef", + "metadata": {}, + "outputs": [], + "source": [ + "from PIL import Image" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "ca824027", + "metadata": {}, + "outputs": [], + "source": [ + "output['images'][0].show()" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "01f4eee8", + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'output' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn [1], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43moutput\u001b[49m[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mimages\u001b[39m\u001b[38;5;124m'\u001b[39m][\u001b[38;5;241m0\u001b[39m]\u001b[38;5;241m.\u001b[39msave(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m./output/1.png\u001b[39m\u001b[38;5;124m'\u001b[39m)\n", + "\u001b[0;31mNameError\u001b[0m: name 'output' is not defined" + ] + } + ], + "source": [ + "output['images'][0].save('./output/1.png')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "479e3705", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.5" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} -- cgit v1.2.3