토니의 연습장

CLIP, taming-transformers 필요 패키지 버전 문제 본문

기타/환경 & 라이브러리 (vscode, Conda, import)

CLIP, taming-transformers 필요 패키지 버전 문제

bellmake 2024. 11. 1. 11:18

1. CLIP 필요 패키지 확인 (https://github.com/openai/CLIP/blob/main/requirements.txt)

requirements.txt 파일 위치시키기

ftfy
packaging
regex
tqdm
torch
torchvision

 

2. taming-transformers 필요 패키지 확인 (https://github.com/CompVis/taming-transformers/blob/master/environment.yaml)

envrionment.yaml 파일 위치시키기

name: taming
channels:
- pytorch
- defaults
dependencies:
- python=3.8.5
- pip=20.3
- cudatoolkit=10.2
- pytorch=1.7.0
- torchvision=0.8.1
- numpy=1.19.2
- pip:
- albumentations==0.4.3
- opencv-python==4.1.2.30
- pudb==2019.2
- imageio==2.9.0
- imageio-ffmpeg==0.4.2
- pytorch-lightning==1.0.8
- omegaconf==2.0.0
- test-tube>=0.7.5
- streamlit>=0.73.1
- einops==0.3.0
- more-itertools>=8.0.0
- transformers==4.3.1

 

3. 최적 가상환경 만들기

(1) 필요시 기존 환경 삭제

conda env list

conda env remove -n multimodal_generation

(2) 1번에서 두 환경 충돌 패키지 없는 것 확인함에 따라 포괄적인 yaml 파일로 환경 만들기

conda env create -f envrionments.yaml

하지만, 위의 방법으로 만들었을 때 conda 문제 등으로 python3.8.5로 환경 만들어졌고 환경 activate 이후에 python --version 해 보았을 때도 버전 3.8.5 이지만 실행 중에 python3.11/site-packages 를 참조하는 등의 문제가 발생할 수 있습니다.

이를 방지하기 위한 확실한 방법은 아래와 같이 환경을 만드는 것입니다.

 

conda create -n multimodal_generation python=3.8.5

conda activate multimodal_generation

conda env update -f environment.yaml

pip install -r requirements.txt

 

4. conda 환경 실행 후 해당 위치에서 code .

ctrl+shift+p : interpreter 해당 환경 설정

분명 yaml 에 omegaconf 도 있는데, conv list && pip list 확인했을 때 omegaconf 설치 안 되어 있는 경우도 있습니다.

이 때는 아래와 같이 터미널 또는 ipynb에서 명시적으로 필요 패키지를 설치 해주어야 합니다. (ipynb의 경우 앞에 ! 필요)

pip install omegaconf==2.0.0 pytorch-lightning==1.0.8

 

이외 기타 사항 포함 :

!pip install --no-deps ftfy regex tqdm
!pip install omegaconf==2.0.0 pytorch-lightning==1.0.8
!pip uninstall torchtext --yes
!pip install einops

 

5. 최종 코드 실행시 정상동작 확인

example code :

 

from taming.models.vqgan import VQModel

def load_config(config_path, display=False):
config_data = OmegaConf.load(config_path)
if display:
print(yaml.dump(OmegaConf.to_container(config_data)))
return config_data

def load_vqgan(config, chk_path=None):
model = VQModel(**config.model.params)
if chk_path is not None:
state_dict = torch.load(chk_path, map_location="cpu")["state_dict"]
missing, unexpected = model.load_state_dict(state_dict, strict=False)
 
return model.eval() # not intending to train model

def generator(x):
x = taming_model.post_quant_conv(x)
x = taming_model.decoder(x)
return x

taming_config = load_config("./models/vqgan_imagenet_f16_16384/configs/model.yaml", display=True)
taming_model = load_vqgan(taming_config, chk_path="./models/vqgan_imagenet_f16_16384/checkpoints/last.ckpt").to(device)