# CycleGan
CycleGAN(Cycle-Consistent Generative Adversarial Network)은 이미지 변환 작업에 사용되는 딥러닝 모델이다.
CycleGAN은 두 가지 다른 도메인의 이미지 간에 쌍(pair)이 없는 상황에서도 이미지 스타일을 변환할 수 있다. 예를 들어, 말의 이미지를 얼룩말 이미지로, 여름 풍경을 겨울 풍경으로 바꾸는 것처럼 이미지 간의 스타일을 바꿀 때 주로 사용된다.
CycleGAN의 핵심 개념은 **"Cycle Consistency"**이다. 이는 A 도메인의 이미지를 B 도메인으로 변환한 후 다시 A 도메인으로 되돌리면 원래 이미지와 유사해야 한다는 아이디어임. 이를 통해 각 도메인 간의 변환이 자연스럽게 이루어지도록 보장한다.
-->여기서 도메인의 의미는 특정한 특징이나 스타일을 가진 이미지 집합을 의미한다.
# 현재 문제점..
train데이터셋은 26,000장 전부 낮에 촬영한 사진이지만 , test데이터셋은 13,000장중 6,000장 이상은 밤이나, 흐린날, 해질때 촬영한사진등등 여러 환경에서 촬영한 사진이 나온다. 우리는 test데이터셋을 잘맞춰야 하므로 모델을 훈련할때 test데이터셋과 유사한 데이터로 학습시켜줘야 test성능을 올릴수 있다고 판단하였다.
우리는 현재 train데이터셋 낮 영상과 label파일을 가지고 있는데 그영상을 CycleGan으로 바꾸면 영상만 변하고 레이블링 좌표는 그대로 있게되어 내가 원하는 부분의 객체를 환경만 바꿔서 훈련할 수 있을것이라 예측된다. CycleGan을 이용해서 test정확도를 더올려보겠다.
가장 큰 예를 들어 보면
10월15일 1회차 리더보드에 올린 모델을 test영상으로 추론한 결과 텍스트 파일을 visualize해본 결과
빨간불은 잡지 못하는 경우가 생긴다. train에서 학습해보지 못해서 인식을 못하는것 같다.. 이런영상이 3천개 이상인데, 이러한것만 잘맞출수 있다면 성능이 0.05~0.1 이상은 올라갈것같다.
#Pix2pix (Day2Night) 실습
cycleGan 논문 file
Pix2pix 실습 github
https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix?tab=readme-ov-file
Pix2Pix 설명
https://velog.io/@wilko97/%EB%85%BC%EB%AC%B8%EC%8B%A4%EC%8A%B5-Pix2Pix
(아래 오류에 관한 설명 읽어보고 진행ㄱㄱ)
- 개발환경
* linux or maxos
* python3
* CPU or GPU CUDA - git clone
$ git clone https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix
$ cd pytorch-CycleGAN-and-pix2pix
$ pip install -r requirements.txt
- dataset load
--> 쉘스크립트 기반으로 되어있음
$ bash ./datasets/download_pix2pix_dataset.sh night2day - pretrain model load
$ bash ./scripts/download_pix2pix_model.sh day2night - training
- python train.py --dataroot ./datasets/facades --name facades_pix2pix --model pix2pix --direction BtoA
Change the --dataroot and --name to your own dataset's path and model's name. Use --gpu_ids 0,1,.. to train on multiple GPUs and --batch_size to change the batch size. Add --direction BtoA if you want to train a model to transfrom from class B to A.
https://chatgpt.com/share/670f4b01-a01c-8010-9c04-0e8fddb130b3
$ python train.py --dataroot [data_path] --name [your_save_path] --model [your_model] --direction BtoA
- testing
- python test.py --dataroot ./datasets/facades --direction BtoA --model pix2pix --name facades_pix2pix
# Testing
- `python test.py --dataroot ./datasets/facades --direction BtoA --model pix2pix --name facades_pix2pix`
Change the `--dataroot`, `--name`, and `--direction` to be consistent with your trained model's configuration and how you want to transform images.
> from https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix:
> Note that we specified --direction BtoA as Facades dataset's A to B direction is photos to labels.
> If you would like to apply a pre-trained model to a collection of input images (rather than image pairs), please use --model test option. See ./scripts/test_single.sh for how to apply a model to Facade label maps (stored in the directory facades/testB).
> See a list of currently available models at ./scripts/download_pix2pix_model.sh
- visualize
#오류 발생
train 함수를 돌리려고 명령어를 쳤는데 자꾸 torch모듈이 없다고 확인하라고 떠서 살펴보니
근데 왜 못찾지 생각하던찰나에 검색을 해보니 wsl에서는 의존성 문제 때문에 모듈을 설치할때 가상환경에서 설치를 하여야 한다고 한다.
https://lagooni.tistory.com/entry/%ED%8C%8C%EC%9D%B4%EC%8D%AC-%EA%B0%80%EC%83%81%ED%99%98%EA%B2%BD-%EC%84%A4%EC%A0%95-%EB%B0%8F-%ED%8C%A8%ED%82%A4%EC%A7%80-%EC%84%A4%EC%B9%98%ED%95%98%EA%B8%B0-WSL
https://thinmug.tistory.com/55
-->git clone하고나서 requirements.txt 파일 실행시킬때 가상환경 내에서 해야한다. (모듈 충돌문제)
# pix2pix 훈련방식
pix2pix 방식의 모델을 훈련시키기 위해선 데이터가 필요한데 두개의 이미지가 일대일 대응되는 이미지여야한다.
예를들면아래의 방식과 같다
GaParmar/img2img-turbo: One-step image-to-image with Stable Diffusion turbo: sketch2image, day2night, and more (github.com)
위에거 잘안돼서 이걸로 실행
<예제>
https://cafe.daum.net/SmartRobot/RoVa/2042
<day2night 모델을 나의 train데이터와 test데이터로 학습>
https://github.com/GaParmar/img2img-turbo/blob/main/docs/training_cyclegan_turbo.md
나는 cyclegan_turbo모델을 쓰고 있다. 내가 가지고 있는 데이터로 학습시켜주면 더욱더 품질좋은 데이터를 생성할수 있을것같아서 훈련을 한번 해보겠다.
우선 위의 깃허브 훈련한것 예시를 보면 말과 얼룩말 데이터로 학습하고 있는데 tr
train_A에는 말 사진이 들어가있고
train_B에는 얼룩말 사진이 들어가있다.
이학습은 말그대로 Unpaired image이므로 사진이 같은각도에서 찍은 똑같은 사진이 아니여도 상관없다. 학습할 특징이 명확하게 나뉜 사진이면 상관없음
train_A는 1,067장이 들어가있고 train_B에는 1,334장 들어가있다. 사진 갯수도 굳이 안맞춰도 되는거같음
unpaired image 학습 --> 2개의 데이터셋이 쌍을 이루고 있지 않음, 도메인만 다름
쌍이라는 것은 두개의 이미지를 동시에 보았을 때 영상안에 있는 배경이나 객체가 일대일 대응이 되어야 한다.
내가 가지고 있는 낮 train데이터와 밤 Test data는 쌍이 아니고 도메인만 다르다. 그러므로 위의 예제 조건과 만족하므로
예제에서 주어진 day2night pretrain모델을 나의 train,test 데이터로 재학습시킨다면 생성형 모델이 나의 데이터와 비슷하게 output을 출력해줄것으로 예상된다
학습중..
시간이 너무 오래걸려서 포기