3

Stable Diffusion

케라스 코리아

고성능(3장15초) 텍스트입력 이미지 생성모델 사용하기 - Stable Diffusion

AF 김태영
2022.09.30 15:09
4356

고성능(3장15초) 텍스트입력 이미지 생성모델 사용하기 - Stable Diffusion

keras.io 가이드에서 소개된 Stable Diffusion를 빠르게 사용하는 방법에서 결론 부분 코드만 발췌해서 코랩에서 테스트해봤습니다. 15초 정도 안에 3장의 이미지를 생성할 수 있으니, 기다리는 시간을 줄이고 여러가지 시도를 해볼 수 있을 것 같습니다.

High-performance image generation using Stable Diffusion in KerasCV

환경설정

!pip install tensorflow keras_cv --upgrade --quiet
!apt install --allow-change-held-packages libcudnn8=8.1.0.77-1+cuda11.2

필요한 패키지 불러오기

import time
import keras_cv
from tensorflow import keras
import matplotlib.pyplot as plt

고성능을 위한 설정

Mixed precision : 연산 속도를 높이기 위해서 float32 정밀도 가중치를 float16 정밀도로 계산하도록 설정합니다.

XLA Compilation : 내장된 가속 선형 대수 컴파일러를 사용합니다.

keras.mixed_precision.set_global_policy("mixed_float16")
model = keras_cv.models.StableDiffusion(jit_compile=True)

가상 이미지 생성 (모델 사용)

# 이미지 표출 함수
def plot_images(images):
    plt.figure(figsize=(20, 20))
    for i in range(len(images)):
        ax = plt.subplot(1, len(images), i + 1)
        plt.imshow(images[i])
        plt.axis("off")

# 주어진 텍스트로 이미지를 생성합니다.
gen_image = model.text_to_image(
    "photograph of an astronaut riding unicorn",
    batch_size=3,
)
plot_images(gen_image)

 

위 코드는 아래 배지를 클릭하여 코랩에서 바로 사용해보실 수 있습니다.

Open In Colab

3
0개의 댓글
로그인 후 이용해주세요!