{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"from kaggle_datasets import KaggleDatasets\nfrom sklearn.model_selection import train_test_split\nimport tensorflow as tf\nimport os\nfrom pathlib import Path\nimport shutil\nfrom tqdm import tqdm\nimport matplotlib.pyplot as plt","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Prelimiary"},{"metadata":{},"cell_type":"markdown","source":"Hugely inspired by: \n- https://www.kaggle.com/amyjang/monet-cyclegan-tutorial\n- https://colab.research.google.com/github/junyanz/pytorch-CycleGAN-and-pix2pix/blob/master/CycleGAN.ipynb#scrollTo=TRm-USlsHgEV"},{"metadata":{},"cell_type":"markdown","source":"## Get dataset"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"GCS_PATH = KaggleDatasets().get_gcs_path()\nDATA_PATH = '/kaggle/input/gan-getting-started'\nNEW_DATA_PATH = '/kaggle/new_scheme/gan-getting-started'\n\nMONET_FILENAMES = tf.io.gfile.glob(str(GCS_PATH + '/monet_jpg/*.jpg'))\nprint('Monet .jpg Files:', len(MONET_FILENAMES))\n\nPHOTO_FILENAMES = tf.io.gfile.glob(str(GCS_PATH + '/photo_jpg/*.jpg'))\nprint('Photo .jpg Files:', len(PHOTO_FILENAMES))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"TEST_RATIO = 0.00000000001\nphoto_train_paths, photo_test_paths = train_test_split(PHOTO_FILENAMES, test_size=TEST_RATIO, random_state=42)\nmonet_train_paths, monet_test_paths = train_test_split(MONET_FILENAMES, test_size=TEST_RATIO, random_state=42)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"os.chdir(DATA_PATH)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dir_names = ['trainA', 'trainB', 'testA', 'testB']\nall_image_paths = [photo_train_paths, monet_train_paths, photo_test_paths, monet_test_paths]\n\n# sample cutter for debugging purposes\nSAMPLE = 1\nN = int(SAMPLE)\nall_image_paths = [x[:int(SAMPLE*len(x))] for x in all_image_paths] \nNEW_DATA_PATH = '/kaggle/new_scheme/gan-getting-started' + '_' + str(SAMPLE)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Crate new directory"},{"metadata":{"trusted":true},"cell_type":"code","source":"for dir_name, single_image_paths in zip(dir_names, all_image_paths):\n    new_dir_path = Path(NEW_DATA_PATH) / dir_name\n    old_dir_path = Path(DATA_PATH) \n    \n    new_dir_path.mkdir(parents=True, exist_ok=True)\n    #old_dir_path.mkdir(parents=True, exist_ok=True)\n    \n    for single_image_path in tqdm(single_image_paths):\n        shutil.copy(old_dir_path / Path(single_image_path).parent.name / Path(single_image_path).name, new_dir_path / Path(single_image_path).name)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"DATA_PATH","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"NEW_DATA_PATH","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Import CycleGan Lib"},{"metadata":{"trusted":true},"cell_type":"code","source":"WORKING_PATH = '/kaggle/working'\nos.chdir(WORKING_PATH)\n!git clone https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix\nimport os\nos.chdir('pytorch-CycleGAN-and-pix2pix/')\n!pip install -r requirements.txt","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"! python train.py --dataroot \"/kaggle/new_scheme/gan-getting-started_0.01\" --name photo2monet --model cycle_gan --n_epochs 10 --n_epochs_decay 10","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"MODEL_PATH = Path(\"/kaggle/working/pytorch-CycleGAN-and-pix2pix/checkpoints/photo2monet\")\nos.listdir(MODEL_PATH)\nshutil.copy(MODEL_PATH / 'latest_net_G_A.pth', MODEL_PATH / 'latest_net_G.pth')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"os.listdir('/kaggle/working/pytorch-CycleGAN-and-pix2pix/checkpoints')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#! python test.py --dataroot \"/kaggle/new_scheme/gan-getting-started_0.01/testA\" --name photo2monet002 --model test --no_dropout\n! python test.py --dataroot \"/kaggle/input/gan-getting-started/photo_jpg/\" --name photo2monet --model test --no_dropout --num_test 7010\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Check out results"},{"metadata":{"trusted":true},"cell_type":"code","source":"RESULTS_PATH = Path('/kaggle/working/pytorch-CycleGAN-and-pix2pix/results/photo2monet/test_latest/images')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"result_imgs = os.listdir(RESULTS_PATH)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"result_imgs_real = sorted([RESULTS_PATH / x for x in result_imgs if \"real\" in x])\nresult_imgs_fake = sorted([RESULTS_PATH / x for x in result_imgs if \"fake\" in x])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sorted(['a','c', 'b'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.imshow(plt.imread(result_imgs_fake[0]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.imshow(plt.imread(result_imgs_real[0]))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Save results"},{"metadata":{"trusted":true},"cell_type":"code","source":"results_path = Path('/kaggle/working/results')\nresults_path.mkdir(parents=True, exist_ok=True)\n\nsubmission_path = Path('/kaggle/working/images')\nsubmission_path.mkdir(parents=True, exist_ok=True)\n\nfor img_path in result_imgs_fake:\n    shutil.copy(img_path, results_path / os.path.basename(img_path))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import shutil\nshutil.make_archive(submission_path, 'zip', results_path)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.imshow(plt.imread(results_path / os.listdir(results_path)[0]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"len(os.listdir(results_path))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"! rm -r \"/kaggle/working/pytorch-CycleGAN-and-pix2pix\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"! rm -r \"/kaggle/working/results\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}