HuggingFace 준비하기
HuggingFace란?
Transformer 모델을 사용하기 쉽게 weight 파일과 사전 정의된 모델들을 공유/사용하는 API 라이브러리
사전에 설치할 것들
pip install transformers
transformers
State-of-the-art Machine Learning for JAX, PyTorch and TensorFlow
pypi.org
Access Token 발급하기
최근에 나온 핫 한 것들은 사설 repo로 둔 경우 많으며, 이 경우 인증을 받아야 함.
사전에 사용자는 Access Tokens를 발급받아 두어야 함.
Access Token 인증하기
쥬피터 노트북을 사용하는 경우,
from huggingface_hub import notebook_login
notebook_login()
그냥 python 사용하는 경우,
huggingface-cli login
하고 아까 만든 auth token을 복사해 넣는다.
HuggingFace로 Stable Diffusion 사용하기
자세한 내용은 아래 링크 내용을 참고하였다.
https://huggingface.co/CompVis/stable-diffusion-v1-4
CompVis/stable-diffusion-v1-4 · Hugging Face
This repository is publicly accessible, but you have to accept the conditions to access its files and content. One more step before getting this model. This model is open access and available to all, with a CreativeML OpenRAIL-M license further specifying
huggingface.co
위 링크에서 제공해주는 코드에서 내 입맛에 맞게 약간 수정하였다.
대동소이하다.
import torch
from diffusers import StableDiffusionPipeline
# from huggingface_hub import notebook_login
# notebook_login()
pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", revision="fp16", torch_dtype=torch.float16, use_auth_token=True)
pipe = pipe.to("cuda")
from torch import autocast
prompt = "photo of a panda surfing"
# with autocast("mps"): # mps는 아직 지원하지 않는다. 곧 pytorch에서 지원할 것으로 예상.. cpu로 해도 잘 안된다..
with autocast("cuda"):
image = pipe(prompt)["sample"][0]
image.save(f"panda_surfer.png")
실행하면 다음과 같이 weight 파일을 주욱 다운받는다. 크기가 쫌 된다.
(pt) whyun@k8s-worker-node4:~/workspace/ml/diffusion-huggingface$ python diffuse_hugfce.py
Downloading: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1.34k/1.34k [00:00<00:00, 904kB/s]
Downloading: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 12.5k/12.5k [00:00<00:00, 59.7kB/s]
Downloading: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 342/342 [00:00<00:00, 336kB/s]
Downloading: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 543/543 [00:00<00:00, 382kB/s]
Downloading: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 4.63k/4.63k [00:00<00:00, 2.93MB/s]
Downloading: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 608M/608M [02:07<00:00, 4.76MB/s]
Downloading: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 209/209 [00:00<00:00, 131kB/s]
Downloading: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 209/209 [00:00<00:00, 168kB/s]
Downloading: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 572/572 [00:00<00:00, 389kB/s]
Downloading: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 246M/246M [00:52<00:00, 4.73MB/s]
Downloading: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 525k/525k [00:00<00:00, 592kB/s]
Downloading: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 472/472 [00:00<00:00, 434kB/s]
Downloading: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 788/788 [00:00<00:00, 614kB/s]
Downloading: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1.06M/1.06M [00:01<00:00, 681kB/s]
Downloading: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 772/772 [00:00<00:00, 645kB/s]
Downloading: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1.72G/1.72G [05:48<00:00, 4.93MB/s]
Downloading: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 71.2k/71.2k [00:00<00:00, 164kB/s]
Downloading: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 550/550 [00:00<00:00, 211kB/s]
Downloading: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 167M/167M [00:33<00:00, 4.93MB/s]
{'trained_betas'} was not found in config. Values will be initialized to default values.
100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 51/51 [00:05<00:00, 8.69it/s]
이 weight 파일들이 저장된 위치는 아래와 같다.
~/.cache/huggingface/diffusers/models--CompVis--stable-diffusion-v1-4/blobs
근데 파일 포맷이 무엇인지는 잘 모르겠음.
체크포인트 파일인 것 같은데..
같은 파일을 두번 실행해 봤다.
각기 다른 파일이 생성된다. 랜덤이란 얘기지..
허깅페이스를 쓰니 꽤 단순하고 사용하기 용이한 면이 있다.
'인공지능' 카테고리의 다른 글
중국의 AI 이미지 생성 모델 - ERNIE-ViLG, 무료/오픈소스 (0) | 2022.11.11 |
---|---|
[찍먹분투기] YOLACT Segmentation (0) | 2022.11.06 |
[찍먹분투기] AI Hub 일상생활 및 구어체 한-영 번역 말뭉치 데이터 AI 모델 분석 (0) | 2022.11.06 |
[요약] 농업분야에서 실제 인공지능 활용 가능 분야 (0) | 2022.11.06 |
[찍먹분투기] KoBART를 이용한 영한 번역 인공지능 만들기 (0) | 2022.11.05 |