├── DiffMopher.ipynb
├── 240522facefusion_new.ipynb
├── My_Stable_Diffusion.ipynb
├── My_img2img.ipynb
├── clip_interrogator.ipynb
├── fast_stable_diffusion_AUTOMATIC1111.ipynb
└── My_iPERCore.ipynb
/DiffMopher.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {
6 | "id": "view-in-github",
7 | "colab_type": "text"
8 | },
9 | "source": [
10 | "
"
11 | ]
12 | },
13 | {
14 | "cell_type": "code",
15 | "execution_count": null,
16 | "metadata": {
17 | "id": "6FZD9IdB5HNV"
18 | },
19 | "outputs": [],
20 | "source": [
21 | "%cd /content/\n",
22 | "!git lfs install\n",
23 | "!git clone https://huggingface.co/spaces/microsoft/HuggingGPT\n",
24 | "%cd /content/HuggingGPT/\n",
25 | "\n",
26 | "!pip install -r requirements.txt\n",
27 | "!pip install gradio\n",
28 | "\n",
29 | "!pip install --upgrade pyngrok\n",
30 | "import os\n",
31 | "from pyngrok import ngrok, conf\n",
32 | "\n",
33 | "# ngrokトークンを設定\n",
34 | "conf.get_default().auth_token = \"1ooR0mbvnaIetot2hIRcaZGLtJT_4hrSyR972wYZBfAp9Wbzi\"\n",
35 | "public_url = ngrok.connect(7860)\n",
36 | "print(f\"ngrok URL: {public_url}\")\n"
37 | ]
38 | },
39 | {
40 | "cell_type": "code",
41 | "execution_count": null,
42 | "metadata": {
43 | "id": "-rQS8b284zmS"
44 | },
45 | "outputs": [],
46 | "source": [
47 | "! git clone https://github.com/cedro3/DiffMorpher.git\n",
48 | "%cd DiffMorpher\n",
49 | "! pip install -r requirements.txt\n",
50 | "! python app.py"
51 | ]
52 | }
53 | ],
54 | "metadata": {
55 | "accelerator": "GPU",
56 | "colab": {
57 | "machine_shape": "hm",
58 | "provenance": [],
59 | "authorship_tag": "ABX9TyMWvRgG4F2IPFDWaj4VfFWv",
60 | "include_colab_link": true
61 | },
62 | "kernelspec": {
63 | "display_name": "Python 3",
64 | "name": "python3"
65 | },
66 | "language_info": {
67 | "name": "python"
68 | }
69 | },
70 | "nbformat": 4,
71 | "nbformat_minor": 0
72 | }
--------------------------------------------------------------------------------
/240522facefusion_new.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "provenance": [],
7 | "machine_shape": "hm",
8 | "gpuType": "T4",
9 | "authorship_tag": "ABX9TyNtyzgGbUVuSOYpQZUAptAs",
10 | "include_colab_link": true
11 | },
12 | "kernelspec": {
13 | "name": "python3",
14 | "display_name": "Python 3"
15 | },
16 | "language_info": {
17 | "name": "python"
18 | },
19 | "accelerator": "GPU"
20 | },
21 | "cells": [
22 | {
23 | "cell_type": "markdown",
24 | "metadata": {
25 | "id": "view-in-github",
26 | "colab_type": "text"
27 | },
28 | "source": [
29 | "
"
30 | ]
31 | },
32 | {
33 | "cell_type": "code",
34 | "execution_count": null,
35 | "metadata": {
36 | "id": "uKVj7Agr1Q4_"
37 | },
38 | "outputs": [],
39 | "source": [
40 | "!wget https://archive.creativaier.com/comfyui/scripts/setup_script001\n",
41 | "!mv setup_script001 setup_script.sh\n",
42 | "!chmod +x setup_script.sh\n",
43 | "!./setup_script.sh\n",
44 | "!rm -rf setup_script.sh\n",
45 | "\n",
46 | "%cd /content/source_code\n",
47 | "!python install.py --torch cuda-12.1 --onnxruntime cuda-12.1 --skip-venv\n",
48 | "\n",
49 | "!wget https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64.deb\n",
50 | "!dpkg -i cloudflared-linux-amd64.deb\n",
51 | "\n",
52 | "import subprocess\n",
53 | "import threading\n",
54 | "import time\n",
55 | "import socket\n",
56 | "\n",
57 | "def iframe_thread(port):\n",
58 | " while True:\n",
59 | " time.sleep(0.5)\n",
60 | " sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n",
61 | " result = sock.connect_ex(('127.0.0.1', port))\n",
62 | " if result == 0:\n",
63 | " break\n",
64 | " sock.close()\n",
65 | "\n",
66 | " p = subprocess.Popen([\"cloudflared\", \"tunnel\", \"--url\", f\"http://127.0.0.1:{port}\"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)\n",
67 | " for line in p.stderr:\n",
68 | " l = line.decode()\n",
69 | " if \"trycloudflare.com \" in l:\n",
70 | " print(\"This is the URL to access:\", l[l.find(\"http\"):], end='')\n",
71 | "\n",
72 | "\n",
73 | "# スレッドの開始\n",
74 | "threading.Thread(target=iframe_thread, daemon=True, args=(7860,)).start()\n",
75 | "\n",
76 | "# コードの実行\n",
77 | "%cd /content/source_code\n",
78 | "!python run.py --execution-providers cuda"
79 | ]
80 | }
81 | ]
82 | }
--------------------------------------------------------------------------------
/My_Stable_Diffusion.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {
6 | "id": "view-in-github",
7 | "colab_type": "text"
8 | },
9 | "source": [
10 | "
"
11 | ]
12 | },
13 | {
14 | "cell_type": "code",
15 | "execution_count": null,
16 | "metadata": {
17 | "id": "RrqY6TqCNWNo"
18 | },
19 | "outputs": [],
20 | "source": [
21 | "#@title セットアップ\n",
22 | "\n",
23 | "# ライブラリのインストール\n",
24 | "!pip install diffusers==0.8.0 transformers scipy ftfy\n",
25 | "\n",
26 | "# トークン設定\n",
27 | "YOUR_TOKEN=\"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\"#@param {type:\"string\"}\n",
28 | "\n",
29 | "# パイプライン構築\n",
30 | "from diffusers import StableDiffusionPipeline\n",
31 | "pipe = StableDiffusionPipeline.from_pretrained(\"CompVis/stable-diffusion-v1-4\", use_auth_token=YOUR_TOKEN)\n",
32 | "pipe.to(\"cuda\")"
33 | ]
34 | },
35 | {
36 | "cell_type": "code",
37 | "execution_count": null,
38 | "metadata": {
39 | "id": "ppaduE-0O9gT"
40 | },
41 | "outputs": [],
42 | "source": [
43 | "#@title 画像生成\n",
44 | "\n",
45 | "# 設定\n",
46 | "import os\n",
47 | "out_dir = \"test\" #@param {type:\"string\"} \n",
48 | "os.makedirs(out_dir, exist_ok=True)\n",
49 | "\n",
50 | "# 生成\n",
51 | "prompt = \"An astronaut riding a horse in a photorealistic style\" #@param {type:\"string\"}\n",
52 | "image = pipe(prompt)[\"images\"][0]\n",
53 | "\n",
54 | "# 保存\n",
55 | "sentence = prompt.replace(' ','_')\n",
56 | "out_path = out_dir+'/'+sentence+'.png'\n",
57 | "image.save(out_path)\n",
58 | "\n",
59 | "# 表示\n",
60 | "from IPython.display import Image,display\n",
61 | "display(Image(out_path))"
62 | ]
63 | },
64 | {
65 | "cell_type": "code",
66 | "execution_count": null,
67 | "metadata": {
68 | "id": "QrXp6dv8Twyx"
69 | },
70 | "outputs": [],
71 | "source": [
72 | "#@title 画像のダウンロード\n",
73 | "from google.colab import files\n",
74 | "files.download(out_path)"
75 | ]
76 | },
77 | {
78 | "cell_type": "code",
79 | "execution_count": null,
80 | "metadata": {
81 | "id": "yAx77SNWcSko"
82 | },
83 | "outputs": [],
84 | "source": [
85 | "#@title 連続画像生成\n",
86 | "\n",
87 | "# 設定\n",
88 | "import os\n",
89 | "out_dir = \"01\" #@param {type:\"string\"} \n",
90 | "os.makedirs(out_dir, exist_ok=True)\n",
91 | "repeat = 1000 #@param {type:\"integer\"}\n",
92 | "\n",
93 | "for i in range(repeat):\n",
94 | " # 生成\n",
95 | " prompt = \"An astronaut riding a horse in a photorealistic style\" #@param {type:\"string\"}\n",
96 | " image = pipe(prompt)[\"images\"][0]\n",
97 | "\n",
98 | " # 保存\n",
99 | " sentence = prompt.replace(' ','_')\n",
100 | " out_path = out_dir+'/'+str(i)+'_'+sentence+'.png'\n",
101 | " image.save(out_path)\n",
102 | "\n",
103 | " # 表示\n",
104 | " print(i)\n",
105 | " from IPython.display import Image,display\n",
106 | " display(Image(out_path))"
107 | ]
108 | },
109 | {
110 | "cell_type": "code",
111 | "execution_count": null,
112 | "metadata": {
113 | "id": "pzDidkSldluW"
114 | },
115 | "outputs": [],
116 | "source": [
117 | "#@title 画像のダウンロード\n",
118 | "\n",
119 | "if os.path.isfile('data.zip'):\n",
120 | " os.remove('data.zip')\n",
121 | "\n",
122 | "! zip -r data.zip $out_dir\n",
123 | "from google.colab import files\n",
124 | "files.download('data.zip')"
125 | ]
126 | }
127 | ],
128 | "metadata": {
129 | "accelerator": "GPU",
130 | "colab": {
131 | "provenance": [],
132 | "authorship_tag": "ABX9TyOcRpok1pAcGGpwwFDaXyfa",
133 | "include_colab_link": true
134 | },
135 | "gpuClass": "standard",
136 | "kernelspec": {
137 | "display_name": "Python 3",
138 | "name": "python3"
139 | },
140 | "language_info": {
141 | "name": "python"
142 | }
143 | },
144 | "nbformat": 4,
145 | "nbformat_minor": 0
146 | }
--------------------------------------------------------------------------------
/My_img2img.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "provenance": [],
7 | "private_outputs": true,
8 | "include_colab_link": true
9 | },
10 | "kernelspec": {
11 | "name": "python3",
12 | "display_name": "Python 3"
13 | },
14 | "language_info": {
15 | "name": "python"
16 | },
17 | "accelerator": "GPU"
18 | },
19 | "cells": [
20 | {
21 | "cell_type": "markdown",
22 | "metadata": {
23 | "id": "view-in-github",
24 | "colab_type": "text"
25 | },
26 | "source": [
27 | "
"
28 | ]
29 | },
30 | {
31 | "cell_type": "code",
32 | "source": [
33 | "#@title **Setup**\n",
34 | "\n",
35 | "!nvidia-smi -L\n",
36 | "!pip install diffusers transformers ftfy accelerate\n",
37 | "\n",
38 | "import torch\n",
39 | "from torch import autocast\n",
40 | "from diffusers import StableDiffusionImg2ImgPipeline\n",
41 | "\n",
42 | "YOUR_TOKEN = \"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\"\n",
43 | "model_id = \"CompVis/stable-diffusion-v1-4\"\n",
44 | "device = \"cuda\"\n",
45 | "\n",
46 | "# make pipeline\n",
47 | "pipe = StableDiffusionImg2ImgPipeline.from_pretrained(model_id, revision=\"fp16\", torch_dtype=torch.float16, use_auth_token=YOUR_TOKEN)\n",
48 | "pipe = pipe.to(device)"
49 | ],
50 | "metadata": {
51 | "id": "aPaGiubnWmsp"
52 | },
53 | "execution_count": null,
54 | "outputs": []
55 | },
56 | {
57 | "cell_type": "code",
58 | "source": [
59 | "#@title **inital image setting**\n",
60 | "from PIL import Image\n",
61 | "\n",
62 | "pic = 'test.jpg' #@param {type:\"string\"}\n",
63 | "init_img = Image.open(pic)\n",
64 | "init_img = init_img.resize((512, 512))\n",
65 | "init_img"
66 | ],
67 | "metadata": {
68 | "id": "_WLViXFzJpB6"
69 | },
70 | "execution_count": null,
71 | "outputs": []
72 | },
73 | {
74 | "cell_type": "code",
75 | "execution_count": null,
76 | "metadata": {
77 | "id": "ZHHenrs4EKFM"
78 | },
79 | "outputs": [],
80 | "source": [
81 | "#@title **img2img(Single)**\n",
82 | "\n",
83 | "# setting\n",
84 | "folder = 'single' #@param {type:\"string\"}\n",
85 | "prompt = \"the style of Gogh\" #@param {type:\"string\"}\n",
86 | "strength = 0.5 #@param {type:\"slider\", min:0, max:1, step:0.01}\n",
87 | "rand_num = 5 #@param {type:\"integer\"}\n",
88 | "os.makedirs(folder, exist_ok=True)\n",
89 | "\n",
90 | "# Pipeline execution\n",
91 | "generator = torch.Generator(device).manual_seed(rand_num) # 再現できるようにseedを設定\n",
92 | "with autocast(device):\n",
93 | " image = pipe(prompt, init_image=init_img, guidance_scale=7.5, strength=strength, generator=generator).images[0] \n",
94 | " \n",
95 | "# save & display\n",
96 | "import os\n",
97 | "save_name = folder+'/'+os.path.splitext(pic)[0]+'_'+str(strength)+'_'+str(rand_num)+os.path.splitext(pic)[1]\n",
98 | "image.save(save_name)\n",
99 | "print(save_name)\n",
100 | "image"
101 | ]
102 | },
103 | {
104 | "cell_type": "code",
105 | "source": [
106 | "#@title **img2img(Multiple-random)**\n",
107 | "import os\n",
108 | "from IPython.display import Image,display\n",
109 | "import random\n",
110 | "\n",
111 | "# setting\n",
112 | "folder = '04' #@param {type:\"string\"}\n",
113 | "prompt = \"the style of Gogh\" #@param {type:\"string\"}\n",
114 | "strength = 0.4 #@param {type:\"slider\", min:0, max:1, step:0.05}\n",
115 | "num = 10 #@param {type:\"slider\", min:1, max:100, step:1}\n",
116 | "os.makedirs(folder, exist_ok=True)\n",
117 | "\n",
118 | "# random\n",
119 | "rand_list = random.sample(range(1000), num)\n",
120 | "\n",
121 | "# Pipeline execution\n",
122 | "for i, rand_num in enumerate(rand_list):\n",
123 | " generator = torch.Generator(device).manual_seed(rand_num) # 再現できるようにseedを設定\n",
124 | " with autocast(device):\n",
125 | " image = pipe(prompt, init_image=init_img, guidance_scale=7.5, strength=strength, generator=generator).images[0] \n",
126 | " save_name = folder+'/'+os.path.splitext(pic)[0]+'_'+str(strength)+'_'+str(rand_num)+os.path.splitext(pic)[1]\n",
127 | " image.save(save_name)\n",
128 | " print('No=', i+1, save_name)\n",
129 | " display(Image(save_name))"
130 | ],
131 | "metadata": {
132 | "id": "OpI9NHjfSINl"
133 | },
134 | "execution_count": null,
135 | "outputs": []
136 | },
137 | {
138 | "cell_type": "code",
139 | "source": [
140 | "#@title **img2img(Search strength)**\n",
141 | "import os\n",
142 | "from IPython.display import Image,display\n",
143 | "import numpy as np\n",
144 | "\n",
145 | "# setting\n",
146 | "folder = 'serach' #@param {type:\"string\"}\n",
147 | "prompt = \"the style of Gogh\" #@param {type:\"string\"}\n",
148 | "rand_num = 2 #@param {type:\"integer\"}\n",
149 | "os.makedirs(folder, exist_ok=True)\n",
150 | "\n",
151 | "# strength_list\n",
152 | "strength_list = np.arange(0.2, 0.9, 0.1)\n",
153 | "print(strength_list)\n",
154 | "\n",
155 | "# Pipeline execution\n",
156 | "for strength in strength_list:\n",
157 | " generator = torch.Generator(device).manual_seed(rand_num) # 再現できるようにseedを設定\n",
158 | " with autocast(device):\n",
159 | " image = pipe(prompt, init_image=init_img, guidance_scale=7.5, strength=strength, generator=generator).images[0] \n",
160 | " save_name = folder+'/'+os.path.splitext(pic)[0]+'_'+str(strength)[:4]+'_'+str(rand_num)+os.path.splitext(pic)[1]\n",
161 | " image.save(save_name)\n",
162 | " print(save_name)\n",
163 | " display(Image(save_name))"
164 | ],
165 | "metadata": {
166 | "id": "_1_vUb8-rDlz"
167 | },
168 | "execution_count": null,
169 | "outputs": []
170 | },
171 | {
172 | "cell_type": "code",
173 | "source": [
174 | "#@title **img2img(Multiple-sequential)**\n",
175 | "import os\n",
176 | "from IPython.display import Image,display\n",
177 | "\n",
178 | "# setting\n",
179 | "prompt = \"the style of Gogh\" #@param {type:\"string\"}\n",
180 | "strength = 0.75 #@param {type:\"slider\", min:0, max:1, step:0.01}\n",
181 | "num = 2 #@param {type:\"slider\", min:1, max:100, step:1}\n",
182 | "\n",
183 | "# Pipeline execution\n",
184 | "for i in range(num):\n",
185 | " generator = torch.Generator(device).manual_seed(i) # 再現できるようにseedを設定\n",
186 | " with autocast(device):\n",
187 | " image = pipe(prompt, init_image=init_img, guidance_scale=7.5, strength=strength, generator=generator).images[0] \n",
188 | " save_name = os.path.splitext(pic)[0]+'_'+str(strength)+'_'+str(i)+os.path.splitext(pic)[1]\n",
189 | " image.save(save_name)\n",
190 | " print('No=', i, save_name)\n",
191 | " display(Image(save_name))"
192 | ],
193 | "metadata": {
194 | "id": "5dLjk5uPzslT"
195 | },
196 | "execution_count": null,
197 | "outputs": []
198 | }
199 | ]
200 | }
--------------------------------------------------------------------------------
/clip_interrogator.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {
6 | "id": "view-in-github",
7 | "colab_type": "text"
8 | },
9 | "source": [
10 | "
"
11 | ]
12 | },
13 | {
14 | "cell_type": "markdown",
15 | "metadata": {
16 | "id": "ytxkysgmrJEi"
17 | },
18 | "source": [
19 | "# CLIP Interrogator by [@pharmapsychotic](https://twitter.com/pharmapsychotic) \n",
20 | "\n",
21 | "
\n",
22 | "\n",
23 | "What do the different OpenAI CLIP models see in an image? What might be a good text prompt to create similar images using CLIP guided diffusion or another text to image model? The CLIP Interrogator is here to get you answers!\n",
24 | "\n",
25 | "
\n",
26 | "\n",
27 | "If this notebook is helpful to you please consider buying me a coffee via [ko-fi](https://ko-fi.com/pharmapsychotic) or following me on [twitter](https://twitter.com/pharmapsychotic) for more cool Ai stuff. 🙂\n",
28 | "\n",
29 | "And if you're looking for more Ai art tools check out my [Ai generative art tools list](https://pharmapsychotic.com/tools.html).\n"
30 | ]
31 | },
32 | {
33 | "cell_type": "code",
34 | "execution_count": null,
35 | "metadata": {
36 | "cellView": "form",
37 | "id": "YQk0eemUrSC7"
38 | },
39 | "outputs": [],
40 | "source": [
41 | "#@title Check GPU\n",
42 | "!nvidia-smi -L"
43 | ]
44 | },
45 | {
46 | "cell_type": "code",
47 | "execution_count": null,
48 | "metadata": {
49 | "cellView": "form",
50 | "id": "30xPxDSDrJEl"
51 | },
52 | "outputs": [],
53 | "source": [
54 | "#@title Setup\n",
55 | "!pip3 install ftfy regex tqdm transformers==4.15.0 timm==0.4.12 fairscale==0.4.4\n",
56 | "!pip3 install git+https://github.com/openai/CLIP.git\n",
57 | "!git clone -b v1 https://github.com/pharmapsychotic/clip-interrogator.git\n",
58 | "!git clone https://github.com/salesforce/BLIP\n",
59 | "%cd /content/BLIP\n",
60 | "\n",
61 | "import clip\n",
62 | "import gc\n",
63 | "import numpy as np\n",
64 | "import os\n",
65 | "import pandas as pd\n",
66 | "import requests\n",
67 | "import torch\n",
68 | "import torchvision.transforms as T\n",
69 | "import torchvision.transforms.functional as TF\n",
70 | "\n",
71 | "from IPython.display import display\n",
72 | "from PIL import Image\n",
73 | "from torch import nn\n",
74 | "from torch.nn import functional as F\n",
75 | "from torchvision import transforms\n",
76 | "from torchvision.transforms.functional import InterpolationMode\n",
77 | "from models.blip import blip_decoder\n",
78 | "\n",
79 | "device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')\n",
80 | "\n",
81 | "blip_image_eval_size = 384\n",
82 | "blip_model_url = 'https://storage.googleapis.com/sfr-vision-language-research/BLIP/models/model*_base_caption.pth' \n",
83 | "blip_model = blip_decoder(pretrained=blip_model_url, image_size=blip_image_eval_size, vit='base')\n",
84 | "blip_model.eval()\n",
85 | "blip_model = blip_model.to(device)\n",
86 | "\n",
87 | "def generate_caption(pil_image):\n",
88 | " gpu_image = transforms.Compose([\n",
89 | " transforms.Resize((blip_image_eval_size, blip_image_eval_size), interpolation=InterpolationMode.BICUBIC),\n",
90 | " transforms.ToTensor(),\n",
91 | " transforms.Normalize((0.48145466, 0.4578275, 0.40821073), (0.26862954, 0.26130258, 0.27577711))\n",
92 | " ])(image).unsqueeze(0).to(device)\n",
93 | "\n",
94 | " with torch.no_grad():\n",
95 | " caption = blip_model.generate(gpu_image, sample=False, num_beams=3, max_length=20, min_length=5)\n",
96 | " return caption[0]\n",
97 | "\n",
98 | "def load_list(filename):\n",
99 | " with open(filename, 'r', encoding='utf-8', errors='replace') as f:\n",
100 | " items = [line.strip() for line in f.readlines()]\n",
101 | " return items\n",
102 | "\n",
103 | "def rank(model, image_features, text_array, top_count=1):\n",
104 | " top_count = min(top_count, len(text_array))\n",
105 | " text_tokens = clip.tokenize([text for text in text_array]).cuda()\n",
106 | " with torch.no_grad():\n",
107 | " text_features = model.encode_text(text_tokens).float()\n",
108 | " text_features /= text_features.norm(dim=-1, keepdim=True)\n",
109 | "\n",
110 | " similarity = torch.zeros((1, len(text_array))).to(device)\n",
111 | " for i in range(image_features.shape[0]):\n",
112 | " similarity += (100.0 * image_features[i].unsqueeze(0) @ text_features.T).softmax(dim=-1)\n",
113 | " similarity /= image_features.shape[0]\n",
114 | "\n",
115 | " top_probs, top_labels = similarity.cpu().topk(top_count, dim=-1) \n",
116 | " return [(text_array[top_labels[0][i].numpy()], (top_probs[0][i].numpy()*100)) for i in range(top_count)]\n",
117 | "\n",
118 | "def interrogate(image, models):\n",
119 | " caption = generate_caption(image)\n",
120 | " if len(models) == 0:\n",
121 | " print(f\"\\n\\n{caption}\")\n",
122 | " return\n",
123 | "\n",
124 | " table = []\n",
125 | " bests = [[('',0)]]*5\n",
126 | " for model_name in models:\n",
127 | " print(f\"Interrogating with {model_name}...\")\n",
128 | " model, preprocess = clip.load(model_name)\n",
129 | " model.cuda().eval()\n",
130 | "\n",
131 | " images = preprocess(image).unsqueeze(0).cuda()\n",
132 | " with torch.no_grad():\n",
133 | " image_features = model.encode_image(images).float()\n",
134 | " image_features /= image_features.norm(dim=-1, keepdim=True)\n",
135 | "\n",
136 | " ranks = [\n",
137 | " rank(model, image_features, mediums),\n",
138 | " rank(model, image_features, [\"by \"+artist for artist in artists]),\n",
139 | " rank(model, image_features, trending_list),\n",
140 | " rank(model, image_features, movements),\n",
141 | " rank(model, image_features, flavors, top_count=3)\n",
142 | " ]\n",
143 | "\n",
144 | " for i in range(len(ranks)):\n",
145 | " confidence_sum = 0\n",
146 | " for ci in range(len(ranks[i])):\n",
147 | " confidence_sum += ranks[i][ci][1]\n",
148 | " if confidence_sum > sum(bests[i][t][1] for t in range(len(bests[i]))):\n",
149 | " bests[i] = ranks[i]\n",
150 | "\n",
151 | " row = [model_name]\n",
152 | " for r in ranks:\n",
153 | " row.append(', '.join([f\"{x[0]} ({x[1]:0.1f}%)\" for x in r]))\n",
154 | "\n",
155 | " table.append(row)\n",
156 | "\n",
157 | " del model\n",
158 | " gc.collect()\n",
159 | " display(pd.DataFrame(table, columns=[\"Model\", \"Medium\", \"Artist\", \"Trending\", \"Movement\", \"Flavors\"]))\n",
160 | "\n",
161 | " flaves = ', '.join([f\"{x[0]}\" for x in bests[4]])\n",
162 | " medium = bests[0][0][0]\n",
163 | " if caption.startswith(medium):\n",
164 | " print(f\"\\n\\n{caption} {bests[1][0][0]}, {bests[2][0][0]}, {bests[3][0][0]}, {flaves}\")\n",
165 | " else:\n",
166 | " print(f\"\\n\\n{caption}, {medium} {bests[1][0][0]}, {bests[2][0][0]}, {bests[3][0][0]}, {flaves}\")\n",
167 | "\n",
168 | "data_path = \"../clip-interrogator/data/\"\n",
169 | "\n",
170 | "artists = load_list(os.path.join(data_path, 'artists.txt'))\n",
171 | "flavors = load_list(os.path.join(data_path, 'flavors.txt'))\n",
172 | "mediums = load_list(os.path.join(data_path, 'mediums.txt'))\n",
173 | "movements = load_list(os.path.join(data_path, 'movements.txt'))\n",
174 | "\n",
175 | "sites = ['Artstation', 'behance', 'cg society', 'cgsociety', 'deviantart', 'dribble', 'flickr', 'instagram', 'pexels', 'pinterest', 'pixabay', 'pixiv', 'polycount', 'reddit', 'shutterstock', 'tumblr', 'unsplash', 'zbrush central']\n",
176 | "trending_list = [site for site in sites]\n",
177 | "trending_list.extend([\"trending on \"+site for site in sites])\n",
178 | "trending_list.extend([\"featured on \"+site for site in sites])\n",
179 | "trending_list.extend([site+\" contest winner\" for site in sites])\n"
180 | ]
181 | },
182 | {
183 | "cell_type": "code",
184 | "execution_count": null,
185 | "metadata": {
186 | "cellView": "form",
187 | "id": "rbDEMDGJrJEo"
188 | },
189 | "outputs": [],
190 | "source": [
191 | "#@title Interrogate!\n",
192 | "\n",
193 | "#@markdown \n",
194 | "\n",
195 | "#@markdown #####**Image:**\n",
196 | "\n",
197 | "image_path_or_url = \"\" #@param {type:\"string\"}\n",
198 | "\n",
199 | "#@markdown \n",
200 | "\n",
201 | "#@markdown #####**CLIP models:**\n",
202 | "\n",
203 | "#@markdown For [StableDiffusion](https://stability.ai/blog/stable-diffusion-announcement) you can just use ViTL14
\n",
204 | "#@markdown For [DiscoDiffusion](https://colab.research.google.com/github/alembics/disco-diffusion/blob/main/Disco_Diffusion.ipynb) and \n",
205 | "#@markdown [JAX](https://colab.research.google.com/github/huemin-art/jax-guided-diffusion/blob/v2.7/Huemin_Jax_Diffusion_2_7.ipynb) enable all the same models here as you intend to use when generating your images\n",
206 | "\n",
207 | "ViTB32 = False #@param{type:\"boolean\"}\n",
208 | "ViTB16 = False #@param{type:\"boolean\"}\n",
209 | "ViTL14 = True #@param{type:\"boolean\"}\n",
210 | "ViTL14_336px = False #@param{type:\"boolean\"}\n",
211 | "RN101 = False #@param{type:\"boolean\"}\n",
212 | "RN50 = False #@param{type:\"boolean\"}\n",
213 | "RN50x4 = False #@param{type:\"boolean\"}\n",
214 | "RN50x16 = False #@param{type:\"boolean\"}\n",
215 | "RN50x64 = False #@param{type:\"boolean\"}\n",
216 | "\n",
217 | "models = []\n",
218 | "if ViTB32: models.append('ViT-B/32')\n",
219 | "if ViTB16: models.append('ViT-B/16')\n",
220 | "if ViTL14: models.append('ViT-L/14')\n",
221 | "if ViTL14_336px: models.append('ViT-L/14@336px')\n",
222 | "if RN101: models.append('RN101')\n",
223 | "if RN50: models.append('RN50')\n",
224 | "if RN50x4: models.append('RN50x4')\n",
225 | "if RN50x16: models.append('RN50x16')\n",
226 | "if RN50x64: models.append('RN50x64')\n",
227 | "\n",
228 | "if str(image_path_or_url).startswith('http://') or str(image_path_or_url).startswith('https://'):\n",
229 | " image = Image.open(requests.get(image_path_or_url, stream=True).raw).convert('RGB')\n",
230 | "else:\n",
231 | " image = Image.open(image_path_or_url).convert('RGB')\n",
232 | "\n",
233 | "thumb = image.copy()\n",
234 | "thumb.thumbnail([blip_image_eval_size, blip_image_eval_size])\n",
235 | "display(thumb)\n",
236 | "\n",
237 | "interrogate(image, models=models)\n"
238 | ]
239 | }
240 | ],
241 | "metadata": {
242 | "accelerator": "GPU",
243 | "colab": {
244 | "provenance": [],
245 | "include_colab_link": true
246 | },
247 | "gpuClass": "standard",
248 | "kernelspec": {
249 | "display_name": "Python 3.9.7 ('pytorch')",
250 | "language": "python",
251 | "name": "python3"
252 | },
253 | "language_info": {
254 | "name": "python",
255 | "version": "3.9.7"
256 | },
257 | "orig_nbformat": 4,
258 | "vscode": {
259 | "interpreter": {
260 | "hash": "d4dd9c310c32a31bb53615812f2f2c6cba010b7aa4dfb14e2b192e650667fecd"
261 | }
262 | }
263 | },
264 | "nbformat": 4,
265 | "nbformat_minor": 0
266 | }
--------------------------------------------------------------------------------
/fast_stable_diffusion_AUTOMATIC1111.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {
6 | "id": "view-in-github",
7 | "colab_type": "text"
8 | },
9 | "source": [
10 | "
"
11 | ]
12 | },
13 | {
14 | "cell_type": "markdown",
15 | "metadata": {
16 | "id": "47kV9o1Ni8GH"
17 | },
18 | "source": [
19 | "# **Colab From https://github.com/TheLastBen/fast-stable-diffusion, if you face any issues, feel free to discuss them.** [Support](https://ko-fi.com/thelastben)\n",
20 | "\n",
21 | "\n",
22 | "\n",
23 | "\n",
24 | "\n",
25 | "\n",
26 | "\n"
27 | ]
28 | },
29 | {
30 | "cell_type": "code",
31 | "execution_count": null,
32 | "metadata": {
33 | "id": "Y9EBc437WDOs",
34 | "cellView": "form"
35 | },
36 | "outputs": [],
37 | "source": [
38 | "#@title **connect google drive**\n",
39 | "from google.colab import drive\n",
40 | "drive.mount('/content/gdrive')"
41 | ]
42 | },
43 | {
44 | "cell_type": "code",
45 | "execution_count": null,
46 | "metadata": {
47 | "cellView": "form",
48 | "id": "CFWtw-6EPrKi"
49 | },
50 | "outputs": [],
51 | "source": [
52 | "#@title **Install AUTOMATIC1111 repo**\n",
53 | "from IPython.utils import capture\n",
54 | "from IPython.display import clear_output\n",
55 | "from google.colab import runtime\n",
56 | "from IPython.display import display_markdown\n",
57 | "import time\n",
58 | "\n",
59 | "\n",
60 | "with capture.capture_output() as cap:\n",
61 | " fgitclone = \"git clone --depth 1\" \n",
62 | " %cd /content/gdrive/MyDrive/\n",
63 | " %mkdir sd\n",
64 | " %cd sd\n",
65 | " !$fgitclone https://github.com/Stability-AI/stablediffusion\n",
66 | " !$fgitclone https://github.com/AUTOMATIC1111/stable-diffusion-webui\n",
67 | " %cd /content/gdrive/MyDrive/sd/stable-diffusion-webui/\n",
68 | " !mkdir -p cache/{huggingface,torch}\n",
69 | " %cd /content/\n",
70 | " !ln -s /content/gdrive/MyDrive/sd/stable-diffusion-webui/cache/huggingface ../root/.cache/\n",
71 | " !ln -s /content/gdrive/MyDrive/sd/stable-diffusion-webui/cache/torch ../root/.cache/\n",
72 | " !pip install -q -U gdown\n",
73 | "\n",
74 | "Update_repo = True\n",
75 | "if Update_repo: \n",
76 | " !rm /content/gdrive/MyDrive/sd/stable-diffusion-webui/webui.sh \n",
77 | " !rm /content/gdrive/MyDrive/sd/stable-diffusion-webui/modules/paths.py\n",
78 | " !rm /content/gdrive/MyDrive/sd/stable-diffusion-webui/webui.py \n",
79 | " !rm /content/gdrive/MyDrive/sd/stable-diffusion-webui/modules/ui.py\n",
80 | " !rm /content/gdrive/MyDrive/sd/stable-diffusion-webui/style.css\n",
81 | " %cd /content/gdrive/MyDrive/sd/stable-diffusion-webui/\n",
82 | " clear_output()\n",
83 | " print('\u001b[1;32m')\n",
84 | " !git pull\n",
85 | " clear_output()\n",
86 | " print('\u001b[1;32mDONE !')"
87 | ]
88 | },
89 | {
90 | "cell_type": "code",
91 | "execution_count": null,
92 | "metadata": {
93 | "cellView": "form",
94 | "id": "ZGV_5H4xrOSp"
95 | },
96 | "outputs": [],
97 | "source": [
98 | "#@title **Install Requirements Library**\n",
99 | "\n",
100 | "import os\n",
101 | "import time\n",
102 | "from IPython.utils import capture\n",
103 | "from IPython.display import clear_output\n",
104 | "from subprocess import getoutput\n",
105 | "from re import search\n",
106 | "\n",
107 | "with capture.capture_output() as cap:\n",
108 | " \n",
109 | " if not os.path.exists('/content/gdrive/MyDrive/sd/stablediffusion/src/k-diffusion/k_diffusion'):\n",
110 | " !mkdir /content/gdrive/MyDrive/sd/stablediffusion/src\n",
111 | " %cd /content/gdrive/MyDrive/sd/stablediffusion/src\n",
112 | " !$fgitclone https://github.com/CompVis/taming-transformers\n",
113 | " !$fgitclone https://github.com/openai/CLIP\n",
114 | " !$fgitclone https://github.com/salesforce/BLIP\n",
115 | " !$fgitclone https://github.com/sczhou/CodeFormer\n",
116 | " !$fgitclone https://github.com/crowsonkb/k-diffusion\n",
117 | " !mv /content/gdrive/MyDrive/sd/stablediffusion/src/CLIP /content/gdrive/MyDrive/sd/stablediffusion/src/clip\n",
118 | " !mv /content/gdrive/MyDrive/sd/stablediffusion/src/BLIP /content/gdrive/MyDrive/sd/stablediffusion/src/blip \n",
119 | " !mv /content/gdrive/MyDrive/sd/stablediffusion/src/CodeFormer /content/gdrive/MyDrive/sd/stablediffusion/src/codeformer \n",
120 | " !cp -r /content/gdrive/MyDrive/sd/stablediffusion/src/k-diffusion/k_diffusion /content/gdrive/MyDrive/sd/stable-diffusion-webui/\n",
121 | "\n",
122 | "\n",
123 | " %cd /content/\n",
124 | " for i in range(1,7):\n",
125 | " !wget \"https://github.com/TheLastBen/fast-stable-diffusion/raw/main/Dependencies/Dependencies_AUT.{i}\"\n",
126 | " !mv \"Dependencies_AUT.{i}\" \"Dependencies_AUT.7z.00{i}\"\n",
127 | " !7z x Dependencies_AUT.7z.001\n",
128 | " time.sleep(2)\n",
129 | " !cp -r /content/usr/local/lib/python3.8/dist-packages /usr/local/lib/python3.8/\n",
130 | " !rm -r /content/usr\n",
131 | " for i in range(1,7):\n",
132 | " !rm \"Dependencies_AUT.7z.00{i}\"\n",
133 | " !pip install -U -q pillow\n",
134 | " \n",
135 | " %cd /content/gdrive/MyDrive/sd/stable-diffusion-webui/modules\n",
136 | " !wget -O paths.py https://raw.githubusercontent.com/TheLastBen/fast-stable-diffusion/main/AUTOMATIC1111_files/paths.py\n",
137 | "\n",
138 | " if os.path.exists('/content/gdrive/MyDrive/sd/stable-diffusion-webui/extensions/deforum-for-automatic1111-webui/scripts/deforum_helpers/src/infer.py'):\n",
139 | " %cd /content/gdrive/MyDrive/sd/stable-diffusion-webui/extensions/deforum-for-automatic1111-webui/scripts/deforum_helpers\n",
140 | " !wget -O animation.py https://raw.githubusercontent.com/TheLastBen/fast-stable-diffusion/main/AUTOMATIC1111_files/deforum/animation.py\n",
141 | " !wget -O depth.py https://raw.githubusercontent.com/TheLastBen/fast-stable-diffusion/main/AUTOMATIC1111_files/deforum/depth.py \n",
142 | " %cd /content/gdrive/MyDrive/sd/stable-diffusion-webui/extensions/deforum-for-automatic1111-webui/scripts/deforum_helpers/src \n",
143 | " !wget -O infer.py https://raw.githubusercontent.com/TheLastBen/fast-stable-diffusion/main/AUTOMATIC1111_files/deforum/infer.py\n",
144 | "\n",
145 | " if not os.path.exists('/content/gdrive/MyDrive/sd/stablediffusion/repositories/midas'):\n",
146 | " %cd /content/gdrive/MyDrive/sd/stablediffusion\n",
147 | " !mkdir repositories\n",
148 | " %cd repositories\n",
149 | " !git clone https://github.com/isl-org/MiDaS.git midas\n",
150 | " !git clone https://github.com/compphoto/BoostingMonocularDepth.git\n",
151 | " \n",
152 | " %cd /content\n",
153 | " s = getoutput('nvidia-smi')\n",
154 | " if \"A100\" in s:\n",
155 | " !wget https://github.com/TheLastBen/fast-stable-diffusion/raw/main/precompiled/A100/A100\n",
156 | " %cd /usr/local/lib/python3.8/dist-packages/xformers\n",
157 | " !7z x -y /content/A100\n",
158 | " !rm /content/A100\n",
159 | " if not (\"T4\" in s or \"A100\" in s):\n",
160 | " !pip uninstall -q -y xformers\n",
161 | "\n",
162 | "\n",
163 | "base = '/content/gdrive/MyDrive/sd/stable-diffusion'\n",
164 | "dirs = [base, f'{base}/src/taming-transformers', f'{base}/src/clip',\n",
165 | " f'{base}/src/GFPGAN', f'{base}/src/blip', f'{base}/src/codeformer',\n",
166 | " f'{base}/src/realesrgan', f'{base}/src/k-diffusion', f'{base}/src/ldm']\n",
167 | "for d in dirs:\n",
168 | " !rm -rf {d + '/.git'}\n",
169 | "clear_output()\n",
170 | "print('\u001b[1;32mDone!')"
171 | ]
172 | },
173 | {
174 | "cell_type": "code",
175 | "execution_count": null,
176 | "metadata": {
177 | "cellView": "form",
178 | "id": "p4wj_txjP3TC"
179 | },
180 | "outputs": [],
181 | "source": [
182 | "#@title **Model Download/Load**\n",
183 | "import os\n",
184 | "from IPython.display import clear_output\n",
185 | "import time\n",
186 | "\n",
187 | "Model_Version = \"1.5\" #@param [ \"1.5\", \"V2-512px\", \"V2-768px\"]\n",
188 | "\n",
189 | "RunwayML_Inpainting_Model = False #@param {type:\"boolean\"}\n",
190 | "\n",
191 | "token = \"\" #@param {type:\"string\"}\n",
192 | "\n",
193 | "Redownload_the_original_model = False #@param {type:\"boolean\"}\n",
194 | "\n",
195 | "if Redownload_the_original_model:\n",
196 | " if os.path.exists('/content/mainmodel.ckpt'):\n",
197 | " !rm /content/mainmodel.ckpt\n",
198 | " %cd /content/gdrive/MyDrive/sd/stable-diffusion-webui/models/Stable-diffusion \n",
199 | " !wget -q -O model.ckpt https://raw.githubusercontent.com/TheLastBen/fast-stable-diffusion/main/precompiled/attention.py\n",
200 | " !mv /content/gdrive/MyDrive/sd/stable-diffusion-webui/models/Stable-diffusion/model.ckpt /content/gdrive/MyDrive/sd/stable-diffusion-webui/models/Stable-diffusion/trashfile.f \n",
201 | " time.sleep(2)\n",
202 | " !rm /content/gdrive/MyDrive/sd/stable-diffusion-webui/models/Stable-diffusion/trashfile.f\n",
203 | " time.sleep(2)\n",
204 | " clear_output()\n",
205 | "\n",
206 | "#@markdown Or\n",
207 | "Path_to_CKPT = \"/content/gdrive/MyDrive/AUTOMATIC1111/nakata.ckpt\" #@param {type:\"string\"}\n",
208 | "#@markdown - Insert the full path of your trained model (eg: /content/gdrive/MyDrive/zarathustra.ckpt) or to a folder containing multiple models.\n",
209 | "\n",
210 | "#@markdown Or\n",
211 | "Link_CKPT = \"\" #@param {type:\"string\"}\n",
212 | "#@markdown - A direct link to a CKPT or a shared gdrive link.\n",
213 | "\n",
214 | "def newmdl(token):\n",
215 | "\n",
216 | " if not os.path.exists('/content/gdrive/MyDrive/sd/stable-diffusion-webui/models/Stable-diffusion/model.ckpt'):\n",
217 | " if token==\"\":\n",
218 | " token=input(\"Insert your huggingface token :\")\n",
219 | " %cd /content/\n",
220 | " clear_output()\n",
221 | " !git init\n",
222 | " !git lfs install --system --skip-repo\n",
223 | " !$fgitclone --branch fp16 \"https://USER:{token}@huggingface.co/runwayml/stable-diffusion-v1-5\"\n",
224 | " if os.path.exists('/content/stable-diffusion-v1-5'):\n",
225 | " !$fgitclone \"https://USER:{token}@huggingface.co/stabilityai/sd-vae-ft-mse\"\n",
226 | " !rm -r /content/stable-diffusion-v1-5/vae\n",
227 | " !mv /content/sd-vae-ft-mse /content/stable-diffusion-v1-5/vae \n",
228 | " !wget -O convertosd.py https://github.com/TheLastBen/fast-stable-diffusion/raw/main/Dreambooth/convertosd.py\n",
229 | " !sed -i '201s@.*@ model_path = \"/content/stable-diffusion-v1-5\"@' /content/convertosd.py\n",
230 | " !sed -i '202s@.*@ checkpoint_path= \"/content/gdrive/MyDrive/sd/stable-diffusion-webui/models/Stable-diffusion/model.ckpt\"@' /content/convertosd.py\n",
231 | " clear_output() \n",
232 | " !python /content/convertosd.py \n",
233 | " if os.path.exists('/content/gdrive/MyDrive/sd/stable-diffusion-webui/models/Stable-diffusion/model.ckpt'):\n",
234 | " model='/content/gdrive/MyDrive/sd/stable-diffusion-webui/models/Stable-diffusion/model.ckpt'\n",
235 | " clear_output()\n",
236 | " print('\u001b[1;32mDONE !')\n",
237 | " else:\n",
238 | " print('\u001b[1;31mSomething went wrong, try again')\n",
239 | " else:\n",
240 | " print('\u001b[1;31mMake sure you accept the terms at https://huggingface.co/runwayml/stable-diffusion-v1-5')\n",
241 | "\n",
242 | " else:\n",
243 | " model='/content/gdrive/MyDrive/sd/stable-diffusion-webui/models/Stable-diffusion/model.ckpt'\n",
244 | " clear_output()\n",
245 | " print('\u001b[1;32mModel already exists, check the box \"Redownload_the_original_model\" to redownload/download the V1.5')\n",
246 | "\n",
247 | " if os.path.exists('/content/.git'):\n",
248 | " !rm -r /content/.git\n",
249 | "\n",
250 | " if os.path.exists('/content/stable-diffusion-v1-5'):\n",
251 | " !rm -r /content/stable-diffusion-v1-5 \n",
252 | "\n",
253 | "\n",
254 | "def V2(token):\n",
255 | "\n",
256 | " if not os.path.exists('/content/gdrive/MyDrive/sd/stable-diffusion-webui/models/Stable-diffusion/model.ckpt'):\n",
257 | " %cd /content/\n",
258 | " clear_output()\n",
259 | " !mkdir \"/content/stable-diffusion-V2\"\n",
260 | " %cd \"/content/stable-diffusion-V2\"\n",
261 | " !git init\n",
262 | " !git lfs install --system --skip-repo\n",
263 | " if Model_Version == \"V2-768px\":\n",
264 | " !git remote add -f origin \"https://USER:{token}@huggingface.co/stabilityai/stable-diffusion-2-1\"\n",
265 | " elif Model_Version == \"V2-512px\":\n",
266 | " !git remote add -f origin \"https://USER:{token}@huggingface.co/stabilityai/stable-diffusion-2-1-base\" \n",
267 | " !git config core.sparsecheckout true\n",
268 | " !echo -e \"scheduler\\ntext_encoder\\ntokenizer\\nunet\\nvae\\nmodel_index.json\" > .git/info/sparse-checkout\n",
269 | " !git pull origin main\n",
270 | " %cd /content\n",
271 | " !wget -O convertosdv2.py https://github.com/TheLastBen/fast-stable-diffusion/raw/main/Dreambooth/convertosdv2.py\n",
272 | " clear_output()\n",
273 | " !python /content/convertosdv2.py --fp16 /content/stable-diffusion-V2 /content/gdrive/MyDrive/sd/stable-diffusion-webui/models/Stable-diffusion/model.ckpt\n",
274 | " if os.path.exists('/content/gdrive/MyDrive/sd/stable-diffusion-webui/models/Stable-diffusion/model.ckpt'):\n",
275 | " model='/content/gdrive/MyDrive/sd/stable-diffusion-webui/models/Stable-diffusion/model.ckpt'\n",
276 | " clear_output()\n",
277 | " print('\u001b[1;32mDONE !')\n",
278 | " else:\n",
279 | " print('\u001b[1;31mSomething went wrong, try again')\n",
280 | "\n",
281 | " else:\n",
282 | " model='/content/gdrive/MyDrive/sd/stable-diffusion-webui/models/Stable-diffusion/model.ckpt'\n",
283 | " clear_output()\n",
284 | " print('\u001b[1;32mModel already exists, check the box \"Redownload_the_original_model\" to redownload/download the V2.')\n",
285 | "\n",
286 | " if os.path.exists('/content/.git'):\n",
287 | " !rm -r /content/.git\n",
288 | " !rm -r /content/convertosdv2.py\n",
289 | " if os.path.exists('/content/stable-diffusion-V2'):\n",
290 | " !rm -r /content/stable-diffusion-V2\n",
291 | "\n",
292 | "def inpmdl(token):\n",
293 | "\n",
294 | " if not os.path.exists('/content/gdrive/MyDrive/sd/stable-diffusion-webui/models/Stable-diffusion/sd-v1-5-inpainting.ckpt'):\n",
295 | " if token==\"\":\n",
296 | " token=input(\"Insert your huggingface token :\")\n",
297 | " %cd /content/\n",
298 | " clear_output()\n",
299 | " !git init\n",
300 | " !git lfs install --system --skip-repo\n",
301 | " !$fgitclone --branch fp16 \"https://USER:{token}@huggingface.co/runwayml/stable-diffusion-inpainting\"\n",
302 | " if os.path.exists('/content/stable-diffusion-inpainting'):\n",
303 | " !$fgitclone \"https://USER:{token}@huggingface.co/stabilityai/sd-vae-ft-mse\"\n",
304 | " !rm -r /content/stable-diffusion-inpainting/vae\n",
305 | " !mv /content/sd-vae-ft-mse /content/stable-diffusion-inpainting/vae \n",
306 | " !wget -O convertosd.py https://github.com/TheLastBen/fast-stable-diffusion/raw/main/Dreambooth/convertosd.py\n",
307 | " !sed -i '201s@.*@ model_path = \"/content/stable-diffusion-inpainting\"@' /content/convertosd.py\n",
308 | " !sed -i '202s@.*@ checkpoint_path= \"/content/gdrive/MyDrive/sd/stable-diffusion-webui/models/Stable-diffusion/sd-v1-5-inpainting.ckpt\"@' /content/convertosd.py\n",
309 | " clear_output() \n",
310 | " !python /content/convertosd.py \n",
311 | " if os.path.exists('/content/gdrive/MyDrive/sd/stable-diffusion-webui/models/Stable-diffusion/sd-v1-5-inpainting.ckpt'):\n",
312 | " clear_output()\n",
313 | " print('\u001b[1;32mDONE !')\n",
314 | " else:\n",
315 | " print('\u001b[1;31mSomething went wrong, try again')\n",
316 | " else:\n",
317 | " print('\u001b[1;31mMake sure you have accepted the terms at https://huggingface.co/runwayml/stable-diffusion-inpainting')\n",
318 | "\n",
319 | " else:\n",
320 | " clear_output()\n",
321 | " print('\u001b[1;32mModel already exists.')\n",
322 | "\n",
323 | " if os.path.exists('/content/.git'):\n",
324 | " !rm -r /content/.git\n",
325 | "\n",
326 | " if os.path.exists('/content/stable-diffusion-inpainting'):\n",
327 | " !rm -r /content/stable-diffusion-inpainting \n",
328 | "\n",
329 | "\n",
330 | "if (Path_to_CKPT !=''):\n",
331 | " if os.path.exists(str(Path_to_CKPT)):\n",
332 | " print('\u001b[1;32mUsing the trained model.')\n",
333 | " else:\n",
334 | " while not os.path.exists(str(Path_to_CKPT)):\n",
335 | " print('\u001b[1;31mWrong path, use the colab file explorer to copy the path : ')\n",
336 | " Path_to_CKPT=input()\n",
337 | " if os.path.exists(str(Path_to_CKPT)):\n",
338 | " print('\u001b[1;32mUsing the trained model.')\n",
339 | "\n",
340 | " model=Path_to_CKPT\n",
341 | "\n",
342 | "elif Link_CKPT != \"\":\n",
343 | " if os.path.exists('/content/mainmodel.ckpt'):\n",
344 | " !rm /content/mainmodel.ckpt\n",
345 | " %cd /content/gdrive/MyDrive/sd/stable-diffusion-webui/models/Stable-diffusion\n",
346 | " !wget -q -O model.ckpt https://raw.githubusercontent.com/TheLastBen/fast-stable-diffusion/main/precompiled/attention.py\n",
347 | " !mv /content/gdrive/MyDrive/sd/stable-diffusion-webui/models/Stable-diffusion/model.ckpt /content/gdrive/MyDrive/sd/stable-diffusion-webui/models/Stable-diffusion/trashfile.f \n",
348 | " time.sleep(2)\n",
349 | " !rm /content/gdrive/MyDrive/sd/stable-diffusion-webui/models/Stable-diffusion/trashfile.f\n",
350 | " time.sleep(2)\n",
351 | " clear_output()\n",
352 | " !gdown --fuzzy -O model.ckpt $Link_CKPT\n",
353 | " if os.path.exists('/content/gdrive/MyDrive/sd/stable-diffusion-webui/models/Stable-diffusion/model.ckpt'):\n",
354 | " if os.path.getsize(\"/content/gdrive/MyDrive/sd/stable-diffusion-webui/models/Stable-diffusion/model.ckpt\") > 1810671599:\n",
355 | " model='/content/gdrive/MyDrive/sd/stable-diffusion-webui/models/Stable-diffusion/model.ckpt'\n",
356 | " clear_output()\n",
357 | " print('\u001b[1;32mModel downloaded, using the trained model.')\n",
358 | " else:\n",
359 | " print('\u001b[1;31mWrong link, check that the link is valid')\n",
360 | " else:\n",
361 | " print('\u001b[1;31mWrong link, check that the link is valid')\n",
362 | "\n",
363 | "\n",
364 | "elif Model_Version==\"1.5\":\n",
365 | " newmdl(token)\n",
366 | "\n",
367 | "elif Model_Version==\"V2-512px\" or Model_Version==\"V2-768px\":\n",
368 | " V2(token)\n",
369 | "\n",
370 | "if RunwayML_Inpainting_Model:\n",
371 | " inpmdl(token)"
372 | ]
373 | },
374 | {
375 | "cell_type": "code",
376 | "execution_count": null,
377 | "metadata": {
378 | "cellView": "form",
379 | "id": "PjzwxTkPSPHf"
380 | },
381 | "outputs": [],
382 | "source": [
383 | "#@title **Start stable-diffusion**\n",
384 | "from IPython.utils import capture\n",
385 | "import time\n",
386 | "import sys\n",
387 | "import fileinput\n",
388 | "\n",
389 | "Model_Version = \"1.5\" #@param [\"1.5\", \"V2-512\", \"V2-768\"]\n",
390 | "#@markdown - Important! Choose the correct version and resolution of the model\n",
391 | "\n",
392 | "Use_Gradio_Server = False #@param {type:\"boolean\"}\n",
393 | "#@markdown - Only if you have trouble connecting to the local server\n",
394 | "\n",
395 | "Enable_API = False #@param {type:\"boolean\"}\n",
396 | "#@markdown - You can find the docs in the [wiki](https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/API)\n",
397 | " \n",
398 | "with capture.capture_output() as cap: \n",
399 | " if not os.path.exists('/tools/node/bin/lt'):\n",
400 | " !npm install -g localtunnel\n",
401 | "\n",
402 | "with capture.capture_output() as cap: \n",
403 | " %cd /content/gdrive/MyDrive/sd/stable-diffusion-webui/\n",
404 | " time.sleep(1)\n",
405 | " !wget -O webui.py https://raw.githubusercontent.com/AUTOMATIC1111/stable-diffusion-webui/master/webui.py\n",
406 | " !sed -i 's@ui.create_ui().*@ui.create_ui();shared.demo.queue(concurrency_count=111500)@' /content/gdrive/MyDrive/sd/stable-diffusion-webui/webui.py\n",
407 | " %cd /content/gdrive/MyDrive/sd/stable-diffusion-webui/modules/\n",
408 | " !wget -O ui.py https://raw.githubusercontent.com/AUTOMATIC1111/stable-diffusion-webui/master/modules/ui.py\n",
409 | " !sed -i 's@css = \"\".*@with open(os.path.join(script_path, \"style.css\"), \"r\", encoding=\"utf8\") as file:\\n css = file.read()@' /content/gdrive/MyDrive/sd/stable-diffusion-webui/modules/ui.py\n",
410 | " %cd /content/gdrive/MyDrive/sd/stable-diffusion-webui\n",
411 | " !wget -O style.css https://raw.githubusercontent.com/AUTOMATIC1111/stable-diffusion-webui/master/style.css\n",
412 | " !sed -i 's@min-height: 4.*@min-height: 5.5em;@g' /content/gdrive/MyDrive/sd/stable-diffusion-webui/style.css\n",
413 | " !sed -i \"s@'extensions/depthmap2mask/scripts/depthmap_for_depth2img.py'@\\\"/content/gdrive/MyDrive/sd/stable-diffusion-webui/extensions/depthmap2mask/scripts/depthmap_for_depth2img.py\\\"@\" /content/gdrive/MyDrive/sd/stable-diffusion-webui/extensions/depthmap2mask/scripts/depth2image_depthmask.py\n",
414 | " !sed -i 's@\"repositories/BoostingMonocularDepth\"@\\\"/content/gdrive/MyDrive/sd/stablediffusion/repositories/BoostingMonocularDepth\\\"@' /content/gdrive/MyDrive/sd/stable-diffusion-webui/extensions/stable-diffusion-webui-depthmap-script/scripts/depthmap.py\n",
415 | " %cd /content\n",
416 | "\n",
417 | "\n",
418 | "share=''\n",
419 | "if Use_Gradio_Server:\n",
420 | " share='--share'\n",
421 | " for line in fileinput.input('/usr/local/lib/python3.8/dist-packages/gradio/blocks.py', inplace=True):\n",
422 | " if line.strip().startswith('self.server_name ='):\n",
423 | " line = ' self.server_name = server_name\\n'\n",
424 | " if line.strip().startswith('self.server_port ='):\n",
425 | " line = ' self.server_port = server_port\\n'\n",
426 | " sys.stdout.write(line)\n",
427 | " clear_output()\n",
428 | " \n",
429 | "else:\n",
430 | " share=''\n",
431 | " !nohup lt --port 7860 > srv.txt 2>&1 &\n",
432 | " time.sleep(2)\n",
433 | " !grep -o 'https[^ ]*' /content/srv.txt >srvr.txt\n",
434 | " time.sleep(2)\n",
435 | " srv= getoutput('cat /content/srvr.txt')\n",
436 | "\n",
437 | " for line in fileinput.input('/usr/local/lib/python3.8/dist-packages/gradio/blocks.py', inplace=True):\n",
438 | " if line.strip().startswith('self.server_name ='):\n",
439 | " line = f' self.server_name = \"{srv[8:]}\"\\n'\n",
440 | " if line.strip().startswith('self.server_port ='):\n",
441 | " line = ' self.server_port = 443\\n'\n",
442 | " if line.strip().startswith('self.protocol = \"https\"'):\n",
443 | " line = ' self.protocol = \"https\"\\n'\n",
444 | " if line.strip().startswith('if self.local_url.startswith(\"https\") or self.is_colab'):\n",
445 | " line = '' \n",
446 | " if line.strip().startswith('else \"http\"'):\n",
447 | " line = '' \n",
448 | " sys.stdout.write(line)\n",
449 | " \n",
450 | " !sed -i '13s@.*@ \"PUBLIC_SHARE_TRUE\": \"\u001b[32mConnected\",@' /usr/local/lib/python3.8/dist-packages/gradio/strings.py\n",
451 | " \n",
452 | " !rm /content/srv.txt\n",
453 | " !rm /content/srvr.txt\n",
454 | "\n",
455 | "with capture.capture_output() as cap: \n",
456 | " %cd /content/gdrive/MyDrive/sd/stablediffusion\n",
457 | "\n",
458 | "api = '--api' if Enable_API else ''\n",
459 | "\n",
460 | "if Model_Version == \"V2-768\":\n",
461 | " configf=\"--config /content/gdrive/MyDrive/sd/stablediffusion/configs/stable-diffusion/v2-inference-v.yaml\"\n",
462 | " !sed -i 's@def load_state_dict(checkpoint_path: str, map_location.*@def load_state_dict(checkpoint_path: str, map_location=\"cuda\"):@' /usr/local/lib/python3.8/dist-packages/open_clip/factory.py \n",
463 | "elif Model_Version == \"V2-512\":\n",
464 | " configf=\"--config /content/gdrive/MyDrive/sd/stablediffusion/configs/stable-diffusion/v2-inference.yaml\"\n",
465 | " !sed -i 's@def load_state_dict(checkpoint_path: str, map_location.*@def load_state_dict(checkpoint_path: str, map_location=\"cuda\"):@' /usr/local/lib/python3.8/dist-packages/open_clip/factory.py \n",
466 | "else:\n",
467 | " configf=\"\"\n",
468 | " !sed -i 's@def load_state_dict(checkpoint_path: str, map_location.*@def load_state_dict(checkpoint_path: str, map_location=\"cpu\"):@' /usr/local/lib/python3.8/dist-packages/open_clip/factory.py \n",
469 | "\n",
470 | "if os.path.exists('/usr/local/lib/python3.8/dist-packages/xformers'):\n",
471 | " xformers=\"--xformers\" \n",
472 | "else:\n",
473 | " xformers=\"\"\n",
474 | "\n",
475 | "try:\n",
476 | " model\n",
477 | " if os.path.isfile(model):\n",
478 | " !python /content/gdrive/MyDrive/sd/stable-diffusion-webui/webui.py $share $api --disable-safe-unpickle --no-half-vae --ckpt \"$model\" $configf $xformers\n",
479 | " else:\n",
480 | " !python /content/gdrive/MyDrive/sd/stable-diffusion-webui/webui.py $share $api --disable-safe-unpickle --no-half-vae --ckpt-dir \"$model\" $configf $xformers\n",
481 | "except:\n",
482 | " !python /content/gdrive/MyDrive/sd/stable-diffusion-webui/webui.py $share $api --disable-safe-unpickle --no-half-vae $configf $xformers"
483 | ]
484 | }
485 | ],
486 | "metadata": {
487 | "accelerator": "GPU",
488 | "colab": {
489 | "provenance": [],
490 | "include_colab_link": true
491 | },
492 | "kernelspec": {
493 | "display_name": "Python 3",
494 | "name": "python3"
495 | },
496 | "language_info": {
497 | "name": "python"
498 | }
499 | },
500 | "nbformat": 4,
501 | "nbformat_minor": 0
502 | }
--------------------------------------------------------------------------------
/My_iPERCore.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {
6 | "id": "view-in-github",
7 | "colab_type": "text"
8 | },
9 | "source": [
10 | "
"
11 | ]
12 | },
13 | {
14 | "cell_type": "markdown",
15 | "metadata": {
16 | "id": "rTjLpNgipXPR"
17 | },
18 | "source": [
19 | ""
20 | ]
21 | },
22 | {
23 | "cell_type": "code",
24 | "execution_count": null,
25 | "metadata": {
26 | "id": "aqBT7r7G_pCL"
27 | },
28 | "outputs": [],
29 | "source": [
30 | "#@title 1-1.セットアップ\n",
31 | "\n",
32 | "# ffmpegインストール\n",
33 | "!apt-get install ffmpeg\n",
34 | "\n",
35 | "# CUDAセッティング\n",
36 | "import os\n",
37 | "os.environ[\"CUDA_HOME\"] = \"/usr/local/cuda-11\"\n",
38 | "!echo $CUDA_HOME\n",
39 | "\n",
40 | "# githubからコードをコピー\n",
41 | "!git clone https://github.com/sugi-san/iPERCore.git\n",
42 | "%cd /content/iPERCore/\n",
43 | "\n",
44 | "# python3.8の導入\n",
45 | "!update-alternatives --install /usr/local/bin/python3 python3 /usr/bin/python3.8 2 \n",
46 | "!update-alternatives --install /usr/local/bin/python3 python3 /usr/bin/python3.9 1 \n",
47 | "!python --version \n",
48 | "!apt-get update\n",
49 | "!apt install software-properties-common\n",
50 | "!sudo dpkg --remove --force-remove-reinstreq python3-pip python3-setuptools python3-wheel\n",
51 | "!apt-get install python3-pip\n",
52 | "\n",
53 | "# ライブラリのインストール\n",
54 | "! python3.8 -m pip install torch==1.7.1+cu110 torchvision==0.8.2+cu110 torchaudio==0.7.1 -f https://download.pytorch.org/whl/torch_stable.html\n",
55 | "! python3.8 -m pip install mmcv-full==1.2.2\n",
56 | "! python3.8 -m pip install git+https://github.com/open-mmlab/mmdetection.git@8179440ec5f75fe95484854af61ce6f6279f3bbc\n",
57 | "! python3.8 -m pip install git+https://github.com/open-mmlab/mmediting@d4086aaf8a36ae830f1714aad585900d24ad1156\n",
58 | "! python3.8 -m pip install git+https://github.com/iPERDance/neural_renderer.git@e5f54f71a8941acf372514eb92e289872f272653\n",
59 | "! python3.8 -m pip install tensorboardX>=2.1\n",
60 | "! python3.8 -m pip install toml\n",
61 | "! python3.8 -m pip install easydict\n",
62 | "! python3.8 -m pip install numpy==1.23.5\n",
63 | "\n",
64 | "# 重みのダウンロード\n",
65 | "#! pip install --upgrade gdown\n",
66 | "import gdown\n",
67 | "gdown.download('https://drive.google.com/uc?id=1jpp_KytMplNNFA_IJSzzSWhvyqIdmnJv', 'assets/checkpoints.zip', quiet=False)\n",
68 | "!unzip -o assets/checkpoints.zip -d assets/\n",
69 | "!rm assets/checkpoints.zip\n",
70 | "\n",
71 | "# サンプルのダウンロード\n",
72 | "!wget -O assets/samples.zip \"https://download.impersonator.org/iper_plus_plus_latest_samples.zip\"\n",
73 | "!unzip -o assets/samples.zip -d assets\n",
74 | "!rm assets/samples.zip\n",
75 | "\n",
76 | "# ディレクトリ移動\n",
77 | "%cd /content/iPERCore/\n",
78 | "\n",
79 | "# ライブラリーのインポート\n",
80 | "import os\n",
81 | "import os.path as osp\n",
82 | "import platform\n",
83 | "import argparse\n",
84 | "import time\n",
85 | "import sys\n",
86 | "import subprocess\n",
87 | "from IPython.display import HTML\n",
88 | "from base64 import b64encode\n"
89 | ]
90 | },
91 | {
92 | "cell_type": "code",
93 | "execution_count": null,
94 | "metadata": {
95 | "id": "98ZXnpcmhZQI"
96 | },
97 | "outputs": [],
98 | "source": [
99 | "#@title 1-2.初期設定&関数定義\n",
100 | "\n",
101 | "#--- 初期設定 ---\n",
102 | "# the gpu ids\n",
103 | "gpu_ids = \"0\"\n",
104 | "\n",
105 | "# the image size\n",
106 | "image_size = 512\n",
107 | "\n",
108 | "# the default number of source images, it will be updated if the actual number of sources <= num_source\n",
109 | "num_source = 2\n",
110 | "\n",
111 | "# the assets directory. This is very important, please download it from `one_drive_url` firstly.\n",
112 | "assets_dir = \"/content/iPERCore/assets\"\n",
113 | "\n",
114 | "# the output directory.\n",
115 | "output_dir = \"./results\"\n",
116 | "\n",
117 | "# the model id of this case. This is a random model name.\n",
118 | "# model_id = \"model_\" + str(time.time())\n",
119 | "\n",
120 | "# # This is a specific model name, and it will be used if you do not change it.\n",
121 | "# model_id = \"axing_1\"\n",
122 | "\n",
123 | "# symlink from the actual assets directory to this current directory\n",
124 | "work_asserts_dir = os.path.join(\"./assets\")\n",
125 | "if not os.path.exists(work_asserts_dir):\n",
126 | " os.symlink(osp.abspath(assets_dir), osp.abspath(work_asserts_dir),\n",
127 | " target_is_directory=(platform.system() == \"Windows\"))\n",
128 | "\n",
129 | "cfg_path = osp.join(work_asserts_dir, \"configs\", \"deploy.toml\")\n",
130 | "\n",
131 | "\n",
132 | "#---関数定義---\n",
133 | "import matplotlib.pyplot as plt\n",
134 | "from PIL import Image\n",
135 | "import os\n",
136 | "import numpy as np\n",
137 | "import glob\n",
138 | "\n",
139 | "# 汎用画像ファイル表示\n",
140 | "def display_pic(folder):\n",
141 | " fig = plt.figure(figsize=(20, 20))\n",
142 | " files = os.listdir(folder)\n",
143 | " files.sort()\n",
144 | " for i, file in enumerate(files):\n",
145 | " if file == '.ipynb_checkpoints':\n",
146 | " continue\n",
147 | " if file == '.DS_Store':\n",
148 | " continue \n",
149 | " img = Image.open(folder+'/'+file) \n",
150 | " images = np.asarray(img)\n",
151 | " ax = fig.add_subplot(5, 5, i+1, xticks=[], yticks=[])\n",
152 | " image_plt = np.array(images)\n",
153 | " ax.imshow(image_plt)\n",
154 | " ax.set_xlabel(file, fontsize=20) \n",
155 | " plt.show()\n",
156 | " plt.close()\n",
157 | "\n",
158 | "# movie表示(専用画像使用)\n",
159 | "def display_movie(folder):\n",
160 | " fig = plt.figure(figsize=(20, 20))\n",
161 | " files = os.listdir(folder)\n",
162 | " files.sort()\n",
163 | " for i, file in enumerate(files):\n",
164 | " if file == '.ipynb_checkpoints':\n",
165 | " continue\n",
166 | " if file == '.DS_Store':\n",
167 | " continue \n",
168 | " img = Image.open(folder+'/'+file) \n",
169 | " images = np.asarray(img)\n",
170 | " ax = fig.add_subplot(5, 5, i+1, xticks=[], yticks=[])\n",
171 | " image_plt = np.array(images)\n",
172 | " ax.imshow(image_plt)\n",
173 | " ax.set_xlabel(file[:-4]+'.mp4', fontsize=20) \n",
174 | " plt.show()\n",
175 | " plt.close()\n",
176 | "\n",
177 | "# フォルダー名+ペア画像表示用サブ\n",
178 | "def display_pic_mini(folder):\n",
179 | " fig = plt.figure(figsize=(20, 20))\n",
180 | " files = os.listdir(folder)\n",
181 | " files.sort()\n",
182 | " for i, file in enumerate(files):\n",
183 | " if file == '.ipynb_checkpoints':\n",
184 | " continue\n",
185 | " if file == '.DS_Store':\n",
186 | " continue \n",
187 | " img = Image.open(folder+'/'+file) \n",
188 | " images = np.asarray(img)\n",
189 | " ax = fig.add_subplot(5, 5, i+1, xticks=[], yticks=[])\n",
190 | " image_plt = np.array(images)\n",
191 | " ax.imshow(image_plt)\n",
192 | " #ax.set_xlabel(file, fontsize=20) \n",
193 | " plt.show()\n",
194 | " plt.close()\n",
195 | "\n",
196 | "# フォルダー名+ペア画像表示用メイン\n",
197 | "def display_pic_double(folder):\n",
198 | " folders = glob.glob(os.path.join(folder,'*'))\n",
199 | " folders.sort()\n",
200 | " for folder in folders:\n",
201 | " print(folder[11:])\n",
202 | " display_pic_mini(folder)"
203 | ]
204 | },
205 | {
206 | "cell_type": "markdown",
207 | "metadata": {
208 | "id": "_omzBcxz6ADw"
209 | },
210 | "source": [
211 | ""
212 | ]
213 | },
214 | {
215 | "cell_type": "code",
216 | "execution_count": null,
217 | "metadata": {
218 | "id": "iPRpgw8oL2bc"
219 | },
220 | "outputs": [],
221 | "source": [
222 | "#@title 2.画像と動画の表示\n",
223 | "print('===== pic =====')\n",
224 | "display_pic('single/pic')\n",
225 | "\n",
226 | "print('===== movie =====')\n",
227 | "display_movie('single/pic_movie')"
228 | ]
229 | },
230 | {
231 | "cell_type": "code",
232 | "execution_count": null,
233 | "metadata": {
234 | "id": "9mcgrtu1qkFL"
235 | },
236 | "outputs": [],
237 | "source": [
238 | "#@title 3.動画の生成(5〜10分かかります)\n",
239 | "from pathlib import Path\n",
240 | "pic = '04.jpg'#@param {type:\"string\"}\n",
241 | "movie = 'd1.mp4'#@param {type:\"string\"}\n",
242 | "pic_name = Path(pic).stem\n",
243 | "movie_name = Path(movie).stem\n",
244 | "\n",
245 | "# This is a specific model name, and it will be used if you do not change it. This is the case of `trump`\n",
246 | "#model_id = \"donald_trump_2\"\n",
247 | "model_id = \"test01\"\n",
248 | "\n",
249 | "# the source input information, here \\\" is escape character of double duote \"\n",
250 | "#src_path = \"\\\"path?=/content/iPERCore/assets/samples/sources/donald_trump_2/00000.PNG,name?=donald\\\"\"\n",
251 | "src_path = \"\\\"path?=/content/iPERCore/single/pic/\"+pic+\",name?=\"+pic_name+\"\\\"\"\n",
252 | "\n",
253 | "#ref_path = \"\\\"path?=/content/iPERCore/movie/d1.mp4,\" \\\n",
254 | "ref_path = \"\\\"path?=/content/iPERCore/single/movie/\"+movie+\",\" \\\n",
255 | " \"name?=\"+movie_name+\",\" \\\n",
256 | " \"pose_fc?=300\\\"\"\n",
257 | "\n",
258 | "print(ref_path)\n",
259 | "\n",
260 | "#!python -m iPERCore.services.run_imitator \\\n",
261 | "!python3.8 -m iPERCore.services.run_imitator \\\n",
262 | " --gpu_ids $gpu_ids \\\n",
263 | " --num_source $num_source \\\n",
264 | " --image_size $image_size \\\n",
265 | " --output_dir $output_dir \\\n",
266 | " --model_id $model_id \\\n",
267 | " --cfg_path $cfg_path \\\n",
268 | " --src_path $src_path \\\n",
269 | " --ref_path $ref_path"
270 | ]
271 | },
272 | {
273 | "cell_type": "code",
274 | "execution_count": null,
275 | "metadata": {
276 | "cellView": "form",
277 | "colab": {
278 | "background_save": true
279 | },
280 | "id": "xUaUmeJbrtOC"
281 | },
282 | "outputs": [],
283 | "source": [
284 | "#@title 4.動画の再生\n",
285 | "mp4 = open('results/primitives/'+pic_name+'/synthesis/imitations/'+pic_name+'-'+movie, 'rb').read()\n",
286 | "data_url = \"data:video/mp4;base64,\" + b64encode(mp4).decode()\n",
287 | "HTML(f\"\"\"\n",
288 | "\"\"\")"
291 | ]
292 | },
293 | {
294 | "cell_type": "code",
295 | "execution_count": null,
296 | "metadata": {
297 | "cellView": "form",
298 | "id": "zqVA6Wwp2Has"
299 | },
300 | "outputs": [],
301 | "source": [
302 | "#@title 5.動画のダウンロード\n",
303 | "from google.colab import files\n",
304 | "files.download('results/primitives/'+pic_name+'/synthesis/imitations/'+pic_name+'-'+movie)"
305 | ]
306 | },
307 | {
308 | "cell_type": "markdown",
309 | "metadata": {
310 | "id": "TqATbZ7151FC"
311 | },
312 | "source": [
313 | ""
314 | ]
315 | },
316 | {
317 | "cell_type": "code",
318 | "execution_count": null,
319 | "metadata": {
320 | "cellView": "form",
321 | "id": "ylnUxfE7qlHO"
322 | },
323 | "outputs": [],
324 | "source": [
325 | "#@title 6.画像のアップロード\n",
326 | "import os\n",
327 | "import shutil\n",
328 | "from google.colab import files\n",
329 | "\n",
330 | "# ルートへ画像をアップロード\n",
331 | "uploaded = files.upload() \n",
332 | "uploaded = list(uploaded.keys()) \n",
333 | "\n",
334 | "# ルートからpicフォルダーへ移動\n",
335 | "for i in range(len(uploaded)):\n",
336 | " shutil.move(uploaded[i], './single/pic/'+uploaded[i])"
337 | ]
338 | },
339 | {
340 | "cell_type": "code",
341 | "execution_count": null,
342 | "metadata": {
343 | "cellView": "form",
344 | "id": "uLF5hYM2sN9w"
345 | },
346 | "outputs": [],
347 | "source": [
348 | "#@title 7.画像と動画の表示\n",
349 | "print('===== pic =====')\n",
350 | "display_pic('single/pic')\n",
351 | "\n",
352 | "print('===== movie =====')\n",
353 | "display_movie('single/pic_movie')"
354 | ]
355 | },
356 | {
357 | "cell_type": "code",
358 | "execution_count": null,
359 | "metadata": {
360 | "id": "Z9jqD67fsbqU"
361 | },
362 | "outputs": [],
363 | "source": [
364 | "#@title 8.動画の生成(5〜10分かかります)\n",
365 | "from pathlib import Path\n",
366 | "pic = '126.jpg'#@param {type:\"string\"}\n",
367 | "movie = 'd2.mp4'#@param {type:\"string\"}\n",
368 | "pic_name = Path(pic).stem\n",
369 | "movie_name = Path(movie).stem\n",
370 | "\n",
371 | "# This is a specific model name, and it will be used if you do not change it. This is the case of `trump`\n",
372 | "#model_id = \"donald_trump_2\"\n",
373 | "model_id = \"test01\"\n",
374 | "\n",
375 | "# the source input information, here \\\" is escape character of double duote \"\n",
376 | "#src_path = \"\\\"path?=/content/iPERCore/assets/samples/sources/donald_trump_2/00000.PNG,name?=donald\\\"\"\n",
377 | "src_path = \"\\\"path?=/content/iPERCore/single/pic/\"+pic+\",name?=\"+pic_name+\"\\\"\"\n",
378 | "\n",
379 | "#ref_path = \"\\\"path?=/content/iPERCore/movie/d1.mp4,\" \\\n",
380 | "ref_path = \"\\\"path?=/content/iPERCore/single/movie/\"+movie+\",\" \\\n",
381 | " \"name?=\"+movie_name+\",\" \\\n",
382 | " \"pose_fc?=300\\\"\"\n",
383 | "\n",
384 | "print(ref_path)\n",
385 | "\n",
386 | "!python3.8 -m iPERCore.services.run_imitator \\\n",
387 | " --gpu_ids $gpu_ids \\\n",
388 | " --num_source $num_source \\\n",
389 | " --image_size $image_size \\\n",
390 | " --output_dir $output_dir \\\n",
391 | " --model_id $model_id \\\n",
392 | " --cfg_path $cfg_path \\\n",
393 | " --src_path $src_path \\\n",
394 | " --ref_path $ref_path"
395 | ]
396 | },
397 | {
398 | "cell_type": "code",
399 | "execution_count": null,
400 | "metadata": {
401 | "cellView": "form",
402 | "id": "WIGGsdBKsqUM"
403 | },
404 | "outputs": [],
405 | "source": [
406 | "#@title 9.動画の再生\n",
407 | "mp4 = open('results/primitives/'+pic_name+'/synthesis/imitations/'+pic_name+'-'+movie, 'rb').read()\n",
408 | "data_url = \"data:video/mp4;base64,\" + b64encode(mp4).decode()\n",
409 | "HTML(f\"\"\"\n",
410 | "\"\"\")"
413 | ]
414 | },
415 | {
416 | "cell_type": "code",
417 | "execution_count": null,
418 | "metadata": {
419 | "cellView": "form",
420 | "id": "2p2uBg9Pswrf"
421 | },
422 | "outputs": [],
423 | "source": [
424 | "#@title 10.動画のダウンロード\n",
425 | "from google.colab import files\n",
426 | "files.download('results/primitives/'+pic_name+'/synthesis/imitations/'+pic_name+'-'+movie)"
427 | ]
428 | },
429 | {
430 | "cell_type": "markdown",
431 | "metadata": {
432 | "id": "l7PZuUISa3S-"
433 | },
434 | "source": [
435 | "\n"
436 | ]
437 | },
438 | {
439 | "cell_type": "code",
440 | "execution_count": null,
441 | "metadata": {
442 | "cellView": "form",
443 | "id": "dT9RXYUNWftz"
444 | },
445 | "outputs": [],
446 | "source": [
447 | "#@title 11.フォルダーと動画の表示\n",
448 | "print('===== folder =====')\n",
449 | "display_pic_double('double/pic')\n",
450 | "\n",
451 | "print('===== movie =====')\n",
452 | "display_movie('double/pic_movie')"
453 | ]
454 | },
455 | {
456 | "cell_type": "code",
457 | "execution_count": null,
458 | "metadata": {
459 | "id": "8ZAAoSX1zM3b"
460 | },
461 | "outputs": [],
462 | "source": [
463 | "#@title 12.動画の生成(5〜10分かかります)\n",
464 | "from pathlib import Path\n",
465 | "folder = 'f01'#@param {type:\"string\"}\n",
466 | "movie = 'd01.mp4'#@param {type:\"string\"}\n",
467 | "movie_name = Path(movie).stem\n",
468 | "\n",
469 | "# This is a specific model name, and it will be used if you do not change it. This is the case of `trump`\n",
470 | "#model_id = \"axing_1\"\n",
471 | "model_id = \"test02\"\n",
472 | "\n",
473 | "# the source input information, here \\\" is escape character of double duote \"\n",
474 | "src_path = \"\\\"path?=/content/iPERCore/double/pic/\"+folder+\",name?=\"+folder+\"\\\"\"\n",
475 | "\n",
476 | "\n",
477 | "## the reference input information. There are three reference videos in this case.\n",
478 | "# here \\\" is escape character of double duote \"\n",
479 | "ref_path = \"\\\"path?=/content/iPERCore/double/movie/\"+movie+\",\" \\\n",
480 | " \"name?=\"+movie_name+\",\" \\\n",
481 | " \"pose_fc?=300\\\"\"\n",
482 | "\n",
483 | "print(ref_path)\n",
484 | "\n",
485 | "!python3.8 -m iPERCore.services.run_imitator \\\n",
486 | " --gpu_ids $gpu_ids \\\n",
487 | " --num_source $num_source \\\n",
488 | " --image_size $image_size \\\n",
489 | " --output_dir $output_dir \\\n",
490 | " --model_id $model_id \\\n",
491 | " --cfg_path $cfg_path \\\n",
492 | " --src_path $src_path \\\n",
493 | " --ref_path $ref_path"
494 | ]
495 | },
496 | {
497 | "cell_type": "code",
498 | "execution_count": null,
499 | "metadata": {
500 | "cellView": "form",
501 | "id": "0zzxmdsvzz_o"
502 | },
503 | "outputs": [],
504 | "source": [
505 | "#@title 13.動画の再生\n",
506 | "mp4 = open(\"./results/primitives/\"+folder+\"/synthesis/imitations/\"+folder+\"-\"+movie, \"rb\").read()\n",
507 | "data_url = \"data:video/mp4;base64,\" + b64encode(mp4).decode()\n",
508 | "HTML(f\"\"\"\n",
509 | "\"\"\")"
512 | ]
513 | },
514 | {
515 | "cell_type": "code",
516 | "execution_count": null,
517 | "metadata": {
518 | "cellView": "form",
519 | "id": "yHPx0n5YLBEC"
520 | },
521 | "outputs": [],
522 | "source": [
523 | "#@title 14.動画のダウンロード\n",
524 | "from google.colab import files\n",
525 | "files.download('results/primitives/'+folder+'/synthesis/imitations/'+folder+'-'+movie)"
526 | ]
527 | },
528 | {
529 | "cell_type": "markdown",
530 | "metadata": {
531 | "id": "bQytTKnb6ey_"
532 | },
533 | "source": [
534 | ""
535 | ]
536 | },
537 | {
538 | "cell_type": "code",
539 | "execution_count": null,
540 | "metadata": {
541 | "cellView": "form",
542 | "id": "b_mGU0BCFWLW"
543 | },
544 | "outputs": [],
545 | "source": [
546 | "#@title 15.前後画像のアップロード\n",
547 | "#@markdown ・前を向いた画像(000.jpg)と後ろを向いた画像(001.jpg)をペアでアップロードして下さい\n",
548 | "import os\n",
549 | "import shutil\n",
550 | "\n",
551 | "# フォルダ内のフォルダ数(ファイル数)をカウント\n",
552 | "# 但し、'.ipynb_checkpoints' はカウントしない\n",
553 | "def count_folder(path):\n",
554 | " folders = os.listdir(path)\n",
555 | " offset = 0 \n",
556 | " for folder in folders:\n",
557 | " if folder == '.ipynb_checkpoints':\n",
558 | " offset = 1\n",
559 | " num = len(os.listdir(path))-offset\n",
560 | " return num\n",
561 | "\n",
562 | "# ファイル数が2でないフォルダは削除\n",
563 | "folders = os.listdir('double/pic')\n",
564 | "for folder in folders:\n",
565 | " num = count_folder('double/pic/'+folder) # ファイル数カウント \n",
566 | " if num != 2: \n",
567 | " shutil.rmtree('double/pic/'+folder) # 削除\n",
568 | "\n",
569 | "# 新規フォルダの作成\n",
570 | "number = count_folder('double/pic') # フォルダ数カウント\n",
571 | "folder_path = 'double/pic/f'+str(number+1).zfill(2) # フォルダ名設定\n",
572 | "os.makedirs(folder_path, exist_ok=True)\n",
573 | "\n",
574 | "# 画像をルートへアップロード\n",
575 | "from google.colab import files\n",
576 | "uploaded = files.upload() \n",
577 | "uploaded = list(uploaded.keys()) \n",
578 | "\n",
579 | "# ルートから新規フォルダへ移動\n",
580 | "for i in range(len(uploaded)):\n",
581 | " shutil.move(uploaded[i], folder_path+'/'+uploaded[i])"
582 | ]
583 | },
584 | {
585 | "cell_type": "code",
586 | "execution_count": null,
587 | "metadata": {
588 | "cellView": "form",
589 | "id": "kkEbSlgu0x1Y"
590 | },
591 | "outputs": [],
592 | "source": [
593 | "#@title 16.フォルダーと動画の表示\n",
594 | "print('===== folder =====')\n",
595 | "display_pic_double('double/pic')\n",
596 | "\n",
597 | "print('===== movie =====')\n",
598 | "display_movie('double/pic_movie')"
599 | ]
600 | },
601 | {
602 | "cell_type": "code",
603 | "execution_count": null,
604 | "metadata": {
605 | "id": "2ge4pssC01hR"
606 | },
607 | "outputs": [],
608 | "source": [
609 | "#@title 17.動画の生成(5〜10分かかります)\n",
610 | "from pathlib import Path\n",
611 | "folder = ''#@param {type:\"string\"}\n",
612 | "movie = ''#@param {type:\"string\"}\n",
613 | "movie_name = Path(movie).stem\n",
614 | "\n",
615 | "# This is a specific model name, and it will be used if you do not change it. This is the case of `trump`\n",
616 | "#model_id = \"axing_1\"\n",
617 | "model_id = \"test02\"\n",
618 | "\n",
619 | "# the source input information, here \\\" is escape character of double duote \"\n",
620 | "src_path = \"\\\"path?=/content/iPERCore/double/pic/\"+folder+\",name?=\"+folder+\"\\\"\"\n",
621 | "\n",
622 | "\n",
623 | "## the reference input information. There are three reference videos in this case.\n",
624 | "# here \\\" is escape character of double duote \"\n",
625 | "ref_path = \"\\\"path?=/content/iPERCore/double/movie/\"+movie+\",\" \\\n",
626 | " \"name?=\"+movie_name+\",\" \\\n",
627 | " \"pose_fc?=300\\\"\"\n",
628 | "\n",
629 | "print(ref_path)\n",
630 | "\n",
631 | "!python3.8 -m iPERCore.services.run_imitator \\\n",
632 | " --gpu_ids $gpu_ids \\\n",
633 | " --num_source $num_source \\\n",
634 | " --image_size $image_size \\\n",
635 | " --output_dir $output_dir \\\n",
636 | " --model_id $model_id \\\n",
637 | " --cfg_path $cfg_path \\\n",
638 | " --src_path $src_path \\\n",
639 | " --ref_path $ref_path"
640 | ]
641 | },
642 | {
643 | "cell_type": "code",
644 | "execution_count": null,
645 | "metadata": {
646 | "cellView": "form",
647 | "id": "vRoCYIid01o_"
648 | },
649 | "outputs": [],
650 | "source": [
651 | "#@title 18.動画の再生\n",
652 | "mp4 = open(\"./results/primitives/\"+folder+\"/synthesis/imitations/\"+folder+\"-\"+movie, \"rb\").read()\n",
653 | "data_url = \"data:video/mp4;base64,\" + b64encode(mp4).decode()\n",
654 | "HTML(f\"\"\"\n",
655 | "\"\"\")"
658 | ]
659 | },
660 | {
661 | "cell_type": "code",
662 | "execution_count": null,
663 | "metadata": {
664 | "cellView": "form",
665 | "id": "RrMHTdd_1DLj"
666 | },
667 | "outputs": [],
668 | "source": [
669 | "#@title 19.動画のダウンロード\n",
670 | "from google.colab import files\n",
671 | "files.download('results/primitives/'+folder+'/synthesis/imitations/'+folder+'-'+movie)"
672 | ]
673 | },
674 | {
675 | "cell_type": "markdown",
676 | "metadata": {
677 | "id": "oc5AM9tBgk9c"
678 | },
679 | "source": [
680 | "###【備考】\n",
681 | "・動作がおかしくなった場合は、右上にある「ランタイム/ランタイムを出荷時設定にリセット」をクリックして、再度1番から順番に操作を行います。\n",
682 | "\n",
683 | "・このノートを実行して閉じてから、すぐ再度実行する場合は、前の記憶が不完全に残っていて上手く動かないことがあります。その場合は、右上にある「ランタイム/ランタイムを出荷時設定にリセット」をクリックして、再度1番から操作を行います。\n",
684 | "\n",
685 | "・長時間使用するとgoogleから使用制限を受ける場合がありますが、12h〜24h程度経つと使用制限が解除されます。"
686 | ]
687 | }
688 | ],
689 | "metadata": {
690 | "accelerator": "GPU",
691 | "colab": {
692 | "provenance": [],
693 | "include_colab_link": true
694 | },
695 | "kernelspec": {
696 | "display_name": "Python 3",
697 | "name": "python3"
698 | }
699 | },
700 | "nbformat": 4,
701 | "nbformat_minor": 0
702 | }
--------------------------------------------------------------------------------