[AI] 딥페이크 탐지 기술_GAN으로 생성한 이미지를 Gradient 변환하여 탐지하기
참조 논문 : Learning on Gradients: Generalized Artifacts Representation for GAN-Generated Images Detection
해당 논문의 github 주소 : LGrad
개발 환경
PyCharm / python 3.8
순서
1. Getting the Data
데이터 다운받기
다음 주소에서 'Data'에 해당하는 부분의 Readme를 보고, 데이터를 다운받는다.
https://github.com/peterwang512/CNNDetection
GitHub - PeterWang512/CNNDetection: Code for the paper: CNN-generated images are surprisingly easy to spot... for now https://pe
Code for the paper: CNN-generated images are surprisingly easy to spot... for now https://peterwang512.github.io/CNNDetection/ - PeterWang512/CNNDetection
github.com
데이터 폴더 위치
프로젝트 내의 다음 위치에 각각의 파일을 넣는다.
2. Transform Image to Gradients
pretrained model 저장
networks 폴더를 생성한 뒤, 선행 모델인 stylegan을 다운받아 저장한다.
mkdir -p ./img2gad/stylegan/networks
curl https://lid-1302259812.cos.ap-nanjing.myqcloud.com/tmp/karras2019stylegan-bedrooms-256x256.pkl -O ./img2gad/stylegan/networks/karras2019stylegan-bedrooms-256x256.pkl
Gradient로 변환
먼저, Gradient로 변환된 이미지를 저장할 디렉토리를 만들어준다.
그 뒤,
sh ./transform_img2grad.sh {GPU-ID} {Data-Root-Dir} {Grad-Save-Dir}
GPU-ID, Data-Root 디렉토리 경로, 변환 이미지 저장 디렉토리 경로를 입력한다.
예를 들자면,
sh ./transform_img2grad.sh 0 ~~~~~/dataset ~~~~~/dataset/grad
이런식으로.
디렉토리 경로 편하게 넣기
해당 디렉토리 우클릭, Copy Path 눌러서, 절대경로(Absolute Path) 클릭하면 바로 복사됨.
요렇게 grad 폴더에 이미지가 gradient 변환되어 저장된 것을 확인할 수 있다.
+) Inference (Testing the detector)
선행 모델 다운
학습이 이미 완료된 탐지 모델을(weight files 전부) 다운받는다 : pretrained weight files
탐지 모델 테스트
다운받은 모델의 경로, gradient로 변환된 이미지 디렉토리 경로와, 배치 사이즈를 설정해서 다음 명령을 실행한다.
cd CNNDetection
CUDA_VISIBLE_DEVICES=0 python eval_test8gan.py --model_path {Model-Path} --dataroot {Grad-Test-Path} --batch_size {BS}
예를 들자면,
CUDA_VISIBLE_DEVICES=0 python eval_test8gan.py --model_path ~~~~/CNNDetection/LGrad-4class-Trainon-Progan_car_cat_chair_horse.pth --dataroot ./dataset/grad/test --batch_size 1
이런식으로.
위의 결과값들을 논문과 비교해보았다.
이를 통해 이미지가 gradient로 잘 변환이 되었음을 알 수 있다.
3. Training the model
이미지 변환도 잘 되었겠다, 잘 변환된 이미지로, 이번엔 직접 학습을 시켜보자.
sh ./train-detector.sh {GPU-ID} {Grad-Save-Dir}
예를 들자면,
sh ./train-detector.sh 0 ~~~~/LGrad/CNNDetection/dataset/grad
이런식으로.
epoch가 진행됨에 따라 전체 정확도와 평균 정밀도 모두 증가하고, 손실값은 감소하고 있다.
손실값을, 텐서보드를 설치하여 그래프로 확인해보았다.
학습 반복 횟수(iteration)가 증가할수록 손실값이 낮아지고 있다. 학습을 통해 모델이 예측을 점점 더 잘하게 된다고 판단할 수 있다.
Train시킨 모델로 detect한 결과
![]() |
![]() |
![]() |
(내장 GPU로 세월아네월아 돌리다보니, 트레이닝을 여러번 시키는게 시간적으로 무리가 컸다. 같은 코드로 친구가 돌린걸 보니, 정확도가 98,88,91퍼센트로, 꽤 높게 나오던데... 무튼, GPU 한탄을 좀 해봤다..)
이렇게 계절학기 개별연구 끝!