styleGAN2 project

styleGAN2_ada 사용하기

코딩펭귄 2023. 2. 19. 01:42

0. 환경 설정
1) dataset_tool.py 들어가서 channel=3으로 변경

2) pip install -r requirements.txt  (requirements 두번째줄 경로 길게 적혀있는 부분 지워주기, 토치관련된것도 지우기_환경에맞게 설치를 따로 할 것이기 때문)

3) pip install torch==1.7.1+cu110 torchvision==0.8.2+cu110 torchaudio==0.7.2 -f https://download.pytorch.org/whl/torch_stable.html

 

1. 데이터셋생성 : 테스트 할 이미지

python dataset_tool.py --source=./girls_dataset --dest=./dataset/dataset.zip --width=256 --height=256

* 주의할점 : 만약 기사사진같은 이미지를 이용하여 데이터셋을 생성할것이라면, 알파채널이 이미지에 포함되어있을확률이 크기때문에 알파채널을 제거해줘야한다 * 

3가지 색상을 표현하는 RGB 이미지는 row x column x channel인 3차원 배열로 표현됩니다. 하지만 OpenCV는 이 순서의 반대인 BGR 방법을 사용합니다. RGBA 방법은 배경을 투명하게 처리하기 위해 알파(alpha) 채널을 추가한 것을 의미하는데 주로 배경의 투명도를 표현하기 위해 0,255만을 사용합니다. cv2.imread()의 두번째 인자가 cv2.IMREAD_COLOR일 경우 BGR로 인식하고 cv2.IMREAD_UNCHANGED인 경우에는 이미지가 알파채널을 가지고 있을 때 BGRA로 읽어 들입니다. _ 출처 : 하단_ 컴퓨터가 이미지에 색상을 표현하는 방법 관련

# rgba, rgb 값이 틀리면 맞춰주는 방법
if img.shape[2] == 3:
            channels = img.shape[2]
        else:
            rgb=np.zeros((img.shape[0],img.shape[1],3),dtype="float32")
            r,g,b,a =img[:,:,0,],img[:,:,1],img[:,:,2],img[:,:,3]
            a=np.asarray(a,dtype="float32")/255.0
            R,G,B=(255,255,255)
            rgb[:,:,0]=r*a+(1.0-a) * R
            rgb[:,:,1]=g*a+(1.0-a) * G
            rgb[:,:,2]=b*a+(1.0-a) * B
            img=np.asarray(rgb,dtype="uint8")
            channels = img.shape[2] if img.ndim == 3 else 1
            cur_image_attrs = {
            ...
            ...

or

if img.shape[2] == 4:
            img = img[:,:,:3]

        channels = img.shape[2] if img.ndim == 3 else 1
        cur_image_attrs = {
            'width': img.shape[1],
            'height': img.shape[0],
            'channels': img.shape[2]
        }
        if dataset attrs is None:
        ...
        ..


2. 훈련모듈 실행 

python train.py --outdir=./training-runs --data=./dataset/dataset.zip --kimg=1000 --batch=28 --workers=8 --snap=10

-> 모델생성됨

 

 

3. latent 벡터 구하기 (테스트 할 타켓 이미지 : girls(2).jpg ) _ 프리트레인을 사람이 학습된걸 써야됨 : (ffhq-res256-mirror-paper256-noaug.pkl)                     * 주의할점 : 모델 이미지 사이즈와 테스트할 이미지 사이즈가 같아야됨 *

python projector.py --outdir=out --target=./girls_dataset/girls(2).jpg --num-steps=1000 --seed=486 --save-video=False --network=./ffhq-res256-mirror-paper256-noaug.pkl

stylegan2-ada-pytorch / pretrained / transfer-learning-source-nets (nvidia.com)  ->  여기에서 프리트레인된 256모델 가져오기(이미지 사이즈에 따라서 맞는걸로 다운받아서 사용)

 

 

4. generator.py 돌리기

1) python generate.py --outdir=./out --projected-w=./out/projected_w.npz --network=./kian84.pkl
2) 아니면 generator 로 샘플이미지 만들어보기
python generate.py --outdir=./output --trunc=1 --seeds=1-10 --network=./학습된 모델 pkl파일

 

 

-- 소녀시대 기사사진을 사용해 모델만들기 --

 

 

참고한 깃헙 및 사이트

How to Run StyleGAN2-ADA-PyTorch | Paperspace Blog

 

How to Run StyleGAN2-ADA-PyTorch | Paperspace Blog

After reading this post, you will be able to set up, train, test, and use the latest StyleGAN2 implementation with PyTorch.

blog.paperspace.com

https://github.com/NVlabs/stylegan2-ada-pytorch

 

GitHub - NVlabs/stylegan2-ada-pytorch: StyleGAN2-ADA - Official PyTorch implementation

StyleGAN2-ADA - Official PyTorch implementation. Contribute to NVlabs/stylegan2-ada-pytorch development by creating an account on GitHub.

github.com

GitHub - happy-jihye/Cartoon-StyleGAN: Fine-tuning StyleGAN2 for Cartoon Face Generation

 

GitHub - happy-jihye/Cartoon-StyleGAN: Fine-tuning StyleGAN2 for Cartoon Face Generation

Fine-tuning StyleGAN2 for Cartoon Face Generation. Contribute to happy-jihye/Cartoon-StyleGAN development by creating an account on GitHub.

github.com

https://colab.research.google.com/drive/1WyUoMjpiTyOWt52KgwCBhzY1itDhUgvF?usp=sharing 

 

Toonify yourself의 빵형

Colaboratory notebook

colab.research.google.com

 

컴퓨터가 이미지에 색상을 표현하는 방법 관련

https://rahites.tistory.com/55

 

[ 파이썬으로 만드는 OpenCV 프로젝트🔥] 4장. 이미지 프로세싱 기초

--- 본 포스팅은 데이콘 서포터즈 "데이크루 2기" 활동의 일환입니다 --- - 안녕하세요 데이콘 서포터즈 데이크루 2기 포스(POS)팀의 Rahites입니다 :) - POS팀은 Python OpenCV Study의 약자로 활동 기간동안

rahites.tistory.com