Spaces:
Build error
Build error
Delete stable_diffusion_4kids.py
Browse files- stable_diffusion_4kids.py +0 -130
stable_diffusion_4kids.py
DELETED
|
@@ -1,130 +0,0 @@
|
|
| 1 |
-
# -*- coding: utf-8 -*-
|
| 2 |
-
"""Stable Diffusion์ง์ ๋ง๋ค์ด๋ณด๊ธฐ
|
| 3 |
-
|
| 4 |
-
Automatically generated by Colaboratory.
|
| 5 |
-
|
| 6 |
-
Original file is located at
|
| 7 |
-
https://colab.research.google.com/drive/1IjcI8U0touR68J6aS2n7Ko97vQSPnGzm
|
| 8 |
-
|
| 9 |
-
# ์ด๋ฑํ์๋ค๋ ๋ฐ๋ผํ ์ ์๋ ์ธ๊ณต์ง๋ฅ ์ด๋ฏธ์ง ๋ง๋ค๊ธฐ
|
| 10 |
-
# Stable Diffusion ์ง์ ๋ง๋ค์ด๋ณด๊ธฐ
|
| 11 |
-
> ์ ํ๋ธ [์ฝ๋์คTV](https://www.youtube.com/@conanssam)
|
| 12 |
-
|
| 13 |
-
> ์ฐธ๊ณ : ์ ํ๋ธ [๋นตํ์ ๊ฐ๋ฐ๋์๊ตญ](https://www.youtube.com/@bbanghyong)
|
| 14 |
-
|
| 15 |
-
> [์ฐธ๊ณ ๋ชจ๋ธ](https://keras.io/guides/keras_cv/generate_images_with_stable_diffusion/)
|
| 16 |
-
https://keras.io/guides/keras_cv/generate_images_with_stable_diffusion/
|
| 17 |
-
|
| 18 |
-
```
|
| 19 |
-
@misc{wood2022kerascv,
|
| 20 |
-
title={KerasCV},
|
| 21 |
-
author={Wood, Luke and Tan, Zhenyu and Ian, Stenbit and Zhu, Scott and Chollet, Fran\c{c}ois and others},
|
| 22 |
-
year={2022},
|
| 23 |
-
howpublished={\url{https://github.com/keras-team/keras-cv}},
|
| 24 |
-
}
|
| 25 |
-
```
|
| 26 |
-
|
| 27 |
-
# ํ ๋น๋ GPU(๊ทธ๋ํฝ์นด๋)์ ์ฌ์์ ๋ถ๋ฌ์ต๋๋ค.
|
| 28 |
-
"""
|
| 29 |
-
|
| 30 |
-
!nvidia-smi
|
| 31 |
-
|
| 32 |
-
"""# ์ผ๋ผ์คCV(keras-cv)๋ฅผ ์ค์นํฉ๋๋ค."""
|
| 33 |
-
|
| 34 |
-
!pip install --upgrade keras-cv
|
| 35 |
-
|
| 36 |
-
"""#๋ชจ๋ธ ๋ถ๋ฌ์ค๊ธฐ
|
| 37 |
-
> ์๊ฐ์ธก์ ์ ์ํด time
|
| 38 |
-
|
| 39 |
-
> ๋ชจ๋ธ์์ฑ์ ์ํด keras_cv๋ฅผ ๊ฐ์ ธ์ต๋๋ค.
|
| 40 |
-
|
| 41 |
-
> ํ
์ํ๋ก์ฐ์ ์๋ ์ผ๋ผ์ค๋ ๊ฐ์ ธ์ต๋๋ค.
|
| 42 |
-
|
| 43 |
-
> ๋งทํ๋กฏ๋ฆฝ์์์ ํ์ดํ๋กฏ์ ๊ฐ์ ธ์ ๊ทธ๋ํ๋ฅผ ๊ทธ๋ฆฝ๋๋ค.
|
| 44 |
-
"""
|
| 45 |
-
|
| 46 |
-
import time
|
| 47 |
-
import keras_cv
|
| 48 |
-
from tensorflow import keras
|
| 49 |
-
import matplotlib.pyplot as plt
|
| 50 |
-
|
| 51 |
-
"""## Stable Diffusion ๋ชจ๋ธ ๋ก๋(5๋ถ์ ๋ ์์)
|
| 52 |
-
> keras_cv์์ ๋ชจ๋ธ๋ค ์์ ์๋ ์คํ
์ด๋ธ ๋ํจ์ ์ ๊บผ๋ด์ค๋๋ค.
|
| 53 |
-
|
| 54 |
-
> ์ด๋ฏธ์ง ํญ๊ณผ ํฌ๊ธฐ๋ 512๋ก ์ง์ ํฉ๋๋ค.
|
| 55 |
-
|
| 56 |
-
> ์ด๋ฏธ์ง ํฌ๊ธฐ๋ฅผ ํฌ๊ฒ ํ ๊ฒฝ์ฐ ๋ฉ๋ชจ๋ฆฌ๊ฐ ๋ถ์กฑํด ์ง ์ ์์ต๋๋ค.
|
| 57 |
-
|
| 58 |
-
> ์ด๋ฏธ์ง ํฌ๊ธฐ๋ฅผ ์ค์ผ ๊ฒฝ์ฐ ์ฌ์ง ํ๋ฆฌํฐ๊ฐ ๋จ์ด์ง๊ฒ ๋ฉ๋๋ค.
|
| 59 |
-
"""
|
| 60 |
-
|
| 61 |
-
keras.mixed_precision.set_global_policy("mixed_float16")
|
| 62 |
-
model = keras_cv.models.StableDiffusion(img_height=512, img_width=512, jit_compile=True)
|
| 63 |
-
|
| 64 |
-
"""plot_image๋ผ๋ ํจ์๋ฅผ ์์ฑํ๊ณ ์ธ์๋ก ์ด๋ฏธ์ง๋ฅผ ๋ฃ์ด์ฃผ๋ฉด ์์์ ์ด๋ฏธ์ง๋ฅผ ๋ณด์ฌ์ค๋๋ค.
|
| 65 |
-
|
| 66 |
-
len์ ๋ฌธ์์ด์ ๊ธธ์ด๋ฅผ ๋ฐํํ๋ ํจ์์
๋๋ค.
|
| 67 |
-
plt.subplot์ ์ด์ฉํด์ ์ด๋ฏธ์ง์ ํฌ๊ธฐ์ ๋ง๊ฒ ํญ์ ์ง์ ํฉ๋๋ค.
|
| 68 |
-
>[keras_cv](https://keras.io/guides/keras_cv/generate_images_with_stable_diffusion/)์์ ํ๋กฌํํธ ์์ ๋ฅผ ๊ฐ์ ธ์ต๋๋ค.
|
| 69 |
-
"""
|
| 70 |
-
|
| 71 |
-
def plot_images(images):
|
| 72 |
-
plt.figure(figsize=(10, 10))
|
| 73 |
-
for i in range(len(images)):
|
| 74 |
-
ax = plt.subplot(1, len(images), i + 1)
|
| 75 |
-
plt.imshow(images[i])
|
| 76 |
-
plt.axis("off")
|
| 77 |
-
plt.tight_layout()
|
| 78 |
-
|
| 79 |
-
"""## ์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง ์์ฑ
|
| 80 |
-
|
| 81 |
-
> ์ฐ์ฃผ์ธ์ด ๋ง์ ํ๊ณ ์๋ ์ฌ์ง
|
| 82 |
-
"""
|
| 83 |
-
|
| 84 |
-
images = model.text_to_image("photograph of an astronaut riding a horse", batch_size=3)
|
| 85 |
-
|
| 86 |
-
plot_images(images)
|
| 87 |
-
|
| 88 |
-
"""## ํ๊ธ -> ์์ด ๋ฒ์ญ"""
|
| 89 |
-
|
| 90 |
-
!pip install translate
|
| 91 |
-
|
| 92 |
-
from translate import Translator
|
| 93 |
-
|
| 94 |
-
translator = Translator(from_lang="ko", to_lang="en")
|
| 95 |
-
|
| 96 |
-
translation = translator.translate("์ ํ๋ธ๋ฅผ ๋ณด๋ ํ์๋ค")
|
| 97 |
-
|
| 98 |
-
translation
|
| 99 |
-
|
| 100 |
-
"""## ํจ์ํ"""
|
| 101 |
-
|
| 102 |
-
def generate_images(text, n=3):
|
| 103 |
-
print(text)
|
| 104 |
-
translation = translator.translate(text)
|
| 105 |
-
print(translation)
|
| 106 |
-
|
| 107 |
-
images = model.text_to_image(translation, batch_size=n)
|
| 108 |
-
|
| 109 |
-
return images
|
| 110 |
-
|
| 111 |
-
images = generate_images("ํญ๊ท์ด ๊ณฐํํ
๋ฝ๋ฝํ๋ ๋ชจ์ต")
|
| 112 |
-
|
| 113 |
-
plot_images(images)
|
| 114 |
-
|
| 115 |
-
"""## ๊ทธ๋ฆผ ์์ฑ ์ฑ ๋ง๋ค๊ธฐ"""
|
| 116 |
-
|
| 117 |
-
!pip install -q gradio
|
| 118 |
-
|
| 119 |
-
import gradio as gr
|
| 120 |
-
|
| 121 |
-
def inference(text):
|
| 122 |
-
image = generate_images(text, 1).squeeze()
|
| 123 |
-
return image
|
| 124 |
-
|
| 125 |
-
demo = gr.Interface(fn=inference, inputs="text", outputs="image")
|
| 126 |
-
|
| 127 |
-
demo.launch(share=True)
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|