Updated 29/Nov/2021 by Yoshihisa Nitta
Assuming that you have already executed CycleGAN_VidTIMIT_Train.ipynb, analyzethe Model.
既に CycleGAN_VidTIMIT_Train.ipynb を実行していることを前提とし, 学習済みモデルを解析する。
save_path = '/content/drive/MyDrive/ColabRun/CycleGAN_VidTIMIT01'
VERBOSE = False
#! pip install tensorflow==2.7.0
! pip install tensorflow_addons
%tensorflow_version 2.x
import tensorflow as tf
print(tf.__version__)
import numpy as np
np.random.seed(2022)
if VERBOSE:
! nvidia-smi
! cat /proc/cpuinfo
! cat /etc/issue
! free -h
from google.colab import drive
drive.mount('/content/drive')
if VERBOSE:
! ls /content/drive
Basically, gdown
from Google Drive.
Only if you cannot download from Google Drive, wget
from nw.tsuda.ac.jp.
基本的に、Google Drive から gdown
してください。
Google Drive からダウンロードができない場合にのみ、nw.tsuda.ac.jp から wget
してください。
# Download source file
nw_path = './nw'
! rm -rf {nw_path}
! mkdir -p {nw_path}
if True: # from Google Drive
url_model = 'https://drive.google.com/uc?id=1aNvpPDNeDWYQFu_PA1kOtFlzcO5seHky'
! (cd {nw_path}; gdown {url_model})
else: # from nw.tsuda.ac.jp
URL_NW = 'https://nw.tsuda.ac.jp/lec/GoogleColab/pub'
url_model = f'{URL_NW}/models/CycleGAN.py'
! wget -nd {url_model} -P {nw_path}
if VERBOSE:
! cat {nw_path}/CycleGAN.py
Official WWW of VidTIMIT dataset: http://conradsanderson.id.au/vidtimit/
zip files of 2 persons of VidTIMIT dataset:
https://zenodo.org/record/158963/files/fadg0.zip
https://zenodo.org/record/158963/files/faks0.zip
zip files mirrored on my Google Drive:
https://drive.google.com/uc?id=
VidTIMIT データセットの公式ページ: http://conradsanderson.id.au/vidtimit/
VidTIMIT の2名の顔写真の zip ファイル:
https://zenodo.org/record/158963/files/fadg0.zip
https://zenodo.org/record/158963/files/faks0.zip
自分の Google Drive 上にミラーした顔写真:
https://drive.google.com/uc?id=
https://drive.google.com/uc?id=
# Download zip files
VidTIMIT_site = 'https://zenodo.org/record/158963/files/'
VidTIMIT_fnames = [ 'fadg0', 'faks0']
Mirrored_files = [
'https://drive.google.com/uc?id=1_Fv4p9MDNphMZMnLpEvtCtnwXgN8N5Cj',
'https://drive.google.com/uc?id=1Y8j7ThPVqB0gbx4hb9aMEp9Ptr9wFuoz'
]
data_dir = './datasets'
! rm -rf $data_dir
! mkdir -p $data_dir
for i, fname in enumerate(VidTIMIT_fnames):
fzip = fname + '.zip'
if False:
url = VidTIMIT_site + fzip
!wget {url}
else:
url = Mirrored_files[i]
!gdown {url}
!unzip -q {fzip} -d {data_dir}
IMAGE_SIZE = 128
import os
import glob
imgA_paths = glob.glob(os.path.join(data_dir, VidTIMIT_fnames[0], 'video/*/[0-9]*'))
imgB_paths = glob.glob(os.path.join(data_dir, VidTIMIT_fnames[1], 'video/*/[0-9]*'))
import numpy as np
validation_split = 0.05
nA, nB = len(imgA_paths), len(imgB_paths)
splitA = int(nA * (1 - validation_split))
splitB = int(nB * (1 - validation_split))
np.random.shuffle(imgA_paths)
np.random.shuffle(imgB_paths)
train_imgA_paths = imgA_paths[:splitA]
test_imgA_paths = imgA_paths[splitA:]
train_imgB_paths = imgB_paths[:splitB]
test_imgB_paths = imgB_paths[splitB:]
# Image: [-1, 1] --> [0, 1]
def M1P1_ZeroP1(imgs):
imgs = (imgs + 1) * 0.5
return np.clip(imgs, 0, 1)
# Image: [0, 1] --> [-1, 1]
def ZeroP1_M1P1(imgs):
return imgs * 2 - 1
from nw.CycleGAN import PairDataset
pair_flow = PairDataset(train_imgA_paths, train_imgB_paths, target_size=(IMAGE_SIZE, IMAGE_SIZE))
test_pair_flow = PairDataset(test_imgA_paths, test_imgB_paths, target_size=(IMAGE_SIZE, IMAGE_SIZE))
from nw.CycleGAN import CycleGAN
gan = CycleGAN.load(save_path, 350)
print(gan.epoch)
# Display the graph of losses in training
%matplotlib inline
gan.showLoss()
# Display images
# 画像を表示する。
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
def showImages(imgs, rows=-1, cols=-1, w=2, h=2):
N = len(imgs)
if rows < 0: rows = 1
if cols < 0: cols = (N + rows -1) // rows
fig, ax = plt.subplots(rows, cols, figsize=(w*cols, h*rows))
idx = 0
for row in range(rows):
for col in range(cols) :
if rows == 1 and cols == 1:
axis = ax
elif rows == 1:
axis = ax[col]
elif cols == 1:
axis = ax[row]
else:
axis = ax[row][col]
if idx < N:
axis.imshow(imgs[idx])
axis.axis('off')
idx += 1
plt.show()
# Display generated and cycle images.
# 生成画像とサイクル画像を表示する。
test_pairs = test_pair_flow[:5]
test_imgsA = test_pairs[:,0]
test_imgsB = test_pairs[:,1]
imgsAB = gan.generate_image_from_A(test_imgsA)
imgsBA = gan.generate_image_from_B(test_imgsB)
print('A-->B-->A, ID')
showImages(M1P1_ZeroP1(imgsAB), 4)
print('B-->A-->B, ID')
showImages(M1P1_ZeroP1(imgsBA), 4)
from nw.CycleGAN import CycleGAN
test_pairs = test_pair_flow[:5]
test_imgsA = test_pairs[:,0]
test_imgsB = test_pairs[:,1]
showImages(M1P1_ZeroP1(np.concatenate([test_imgsA, test_imgsB],axis=0)), 2)
for epoch in [200, 250, 300, 350, 400, 450, 500, 550, 600]:
gan = CycleGAN.load(save_path, epoch)
print(gan.epoch)
imgsAB = gan.generate_image_from_A(test_imgsA)[5:10]
imgsBA = gan.generate_image_from_B(test_imgsB)[5:10]
showImages(M1P1_ZeroP1(np.concatenate([imgsAB, imgsBA],axis=0)), 2)