From dcf236b2f41df7e256216f88f9f87956aa364f37 Mon Sep 17 00:00:00 2001 From: han Date: Fri, 15 May 2020 12:41:24 -0400 Subject: [PATCH] Added google colab notebook --- .../AnimeGanGoogleColab.ipynb | 247 ++++++++++++++++++ google_colab_notebook/animegangooglecolab.py | 90 +++++++ 2 files changed, 337 insertions(+) create mode 100755 google_colab_notebook/AnimeGanGoogleColab.ipynb create mode 100755 google_colab_notebook/animegangooglecolab.py diff --git a/google_colab_notebook/AnimeGanGoogleColab.ipynb b/google_colab_notebook/AnimeGanGoogleColab.ipynb new file mode 100755 index 0000000..319a9e0 --- /dev/null +++ b/google_colab_notebook/AnimeGanGoogleColab.ipynb @@ -0,0 +1,247 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "AnimeGanGoogleColab.ipynb", + "provenance": [] + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + }, + "accelerator": "GPU" + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "3QhkRpY844Ot", + "colab_type": "text" + }, + "source": [ + "Change runtime to GPU before doing anything else\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "w7gj2sJiMfut", + "colab_type": "code", + "colab": {} + }, + "source": [ + "!git clone https://github.com/Quentin-M/AnimeGAN.git" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "oC1lPXW7MyjA", + "colab_type": "code", + "colab": {} + }, + "source": [ + "import os\n", + "print(os.listdir(os.getcwd()))\n", + "os.chdir('AnimeGAN')\n", + "print(os.getcwd())" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "L1JOQ4Dw37YR", + "colab_type": "text" + }, + "source": [ + "create a new file called ```download_asset.sh``` in AnimeGan folder" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "Nc3S8OdoBLxw", + "colab_type": "code", + "colab": {} + }, + "source": [ + "with open ('download_asset.sh', 'w') as rsh:\n", + " rsh.write('''\\\n", + " checkpoint=\"$1\"\n", + " if [ \"$checkpoint\" = \"initial\" ]; then\n", + " URL=https://github.com/TachibanaYoshino/AnimeGAN/releases/download/Haoyao-style_V1.0/Haoyao-style-initial-checkpoint.zip\n", + " else\n", + " URL=https://github.com/TachibanaYoshino/AnimeGAN/releases/download/Haoyao-style_V1.0/Haoyao-style.zip\n", + " fi\n", + " ZIP_FILE=./checkpoint/Haoyao-style.zip\n", + " TARGET_DIR=./checkpoint/AnimeGAN_Hayao_lsgan_300_300_1_3_10\n", + " wget -N $URL -O $ZIP_FILE\n", + " mkdir -p $TARGET_DIR\n", + " unzip $ZIP_FILE -d $TARGET_DIR\n", + " rm $ZIP_FILE\n", + " DatesetURL=https://github.com/TachibanaYoshino/AnimeGAN/releases/download/dataset-1/dataset.zip\n", + " ZIP_FILE=./dataset.zip\n", + " TARGET_DIR=./dataset\n", + " rm -rf dataset\n", + " wget -N $DatesetURL -O $ZIP_FILE\n", + " unzip $ZIP_FILE -d $TARGET_DIR\n", + " rm $ZIP_FILE\n", + " VGG_FILE=./vgg19_weight/vgg19.npy\n", + " VGG_URL=https://github.com/TachibanaYoshino/AnimeGAN/releases/download/vgg16%2F19.npy/vgg19.npy\n", + " wget -N $VGG_URL -O $VGG_FILE\n", + " ''')" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ru8I1Hn1SFdW", + "colab_type": "text" + }, + "source": [ + "##Download assset\n", + "\n", + "Do ``` !bash download_asset.sh initial ``` if you want the inital checkpoint, ``` !bash download_asset.sh ``` for the pretrained hayao model" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "mOIinjNRM3dn", + "colab_type": "code", + "colab": {} + }, + "source": [ + "!bash download_asset.sh\n" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "CQC8fU0BUhjB", + "colab_type": "text" + }, + "source": [ + "Check your graphic card" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "gLvKDXdTjBqg", + "colab_type": "code", + "colab": {} + }, + "source": [ + "!nvidia-smi\n" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "6cT3l_1nU8AX", + "colab_type": "text" + }, + "source": [ + "Switch to tensorflow 1.15" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "rJTghwMijG_g", + "colab_type": "code", + "colab": {} + }, + "source": [ + "%tensorflow_version 1.15" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "_z8JjGhx4IHC", + "colab_type": "text" + }, + "source": [ + "## Test \n", + "\n", + "eg. `python main.py --phase test --dataset Hayao` \n", + " or `python test.py --checkpoint_dir checkpoint/AnimeGAN_Hayao_lsgan_300_300_1_3_10 --test_dir dataset/test/real --style_name H` \n", + "\n", + "To test on your own photos, upload your own directory under AnimeGan and replace ```--test_dir``` with you own directory, output will be stored in ```results```" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "KOGGCC--jgEZ", + "colab_type": "code", + "colab": {} + }, + "source": [ + "!python main.py --phase test --dataset Hayao\n" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "GK7uXSp03hcE", + "colab_type": "text" + }, + "source": [ + "Rename \n", + "\n", + "```\n", + "AnimeGAN_Hayao_lsgan_300_300_1_3_10\n", + "to\n", + "old_AnimeGAN_Hayao_lsgan_300_300_1_3_10\n", + "```\n", + "\n", + "in the checkpoint folder so we can train from sractch" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "cEM5SSXD5D4_", + "colab_type": "text" + }, + "source": [ + "## Train\n", + "\n", + "In ```main.py``` :\n", + "\n", + "For Shinkai, ```con_weight``` should be set to 1.1\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "adpOe9c6kWWh", + "colab_type": "code", + "colab": {} + }, + "source": [ + " !python main.py --phase train --dataset Shinkai --epoch 101 --init_epoch 1" + ], + "execution_count": 0, + "outputs": [] + } + ] +} \ No newline at end of file diff --git a/google_colab_notebook/animegangooglecolab.py b/google_colab_notebook/animegangooglecolab.py new file mode 100755 index 0000000..13d9230 --- /dev/null +++ b/google_colab_notebook/animegangooglecolab.py @@ -0,0 +1,90 @@ +# -*- coding: utf-8 -*- +"""AnimeGanGoogleColab.ipynb + +Automatically generated by Colaboratory. + +Original file is located at + https://colab.research.google.com/github/HanYangZhao/AnimeGAN/blob/master/google_colab_notebook/AnimeGanGoogleColab.ipynb + +Change runtime to GPU before doing anything else +""" + +!git clone https://github.com/Quentin-M/AnimeGAN.git + +import os +print(os.listdir(os.getcwd())) +os.chdir('AnimeGAN') +print(os.getcwd()) + +"""create a new file called ```download_asset.sh``` in AnimeGan folder""" + +with open ('download_asset.sh', 'w') as rsh: + rsh.write('''\ + checkpoint="$1" + if [ "$checkpoint" = "initial" ]; then + URL=https://github.com/TachibanaYoshino/AnimeGAN/releases/download/Haoyao-style_V1.0/Haoyao-style-initial-checkpoint.zip + else + URL=https://github.com/TachibanaYoshino/AnimeGAN/releases/download/Haoyao-style_V1.0/Haoyao-style.zip + fi + ZIP_FILE=./checkpoint/Haoyao-style.zip + TARGET_DIR=./checkpoint/AnimeGAN_Hayao_lsgan_300_300_1_3_10 + wget -N $URL -O $ZIP_FILE + mkdir -p $TARGET_DIR + unzip $ZIP_FILE -d $TARGET_DIR + rm $ZIP_FILE + DatesetURL=https://github.com/TachibanaYoshino/AnimeGAN/releases/download/dataset-1/dataset.zip + ZIP_FILE=./dataset.zip + TARGET_DIR=./dataset + rm -rf dataset + wget -N $DatesetURL -O $ZIP_FILE + unzip $ZIP_FILE -d $TARGET_DIR + rm $ZIP_FILE + VGG_FILE=./vgg19_weight/vgg19.npy + VGG_URL=https://github.com/TachibanaYoshino/AnimeGAN/releases/download/vgg16%2F19.npy/vgg19.npy + wget -N $VGG_URL -O $VGG_FILE + ''') + +"""##Download assset + +Do ``` !bash download_asset.sh initial ``` if you want the inital checkpoint, ``` !bash download_asset.sh ``` for the pretrained hayao model +""" + +!bash download_asset.sh + +"""Check your graphic card""" + +!nvidia-smi + +"""Switch to tensorflow 1.15""" + +# Commented out IPython magic to ensure Python compatibility. +# %tensorflow_version 1.15 + +"""## Test + +eg. `python main.py --phase test --dataset Hayao` + or `python test.py --checkpoint_dir checkpoint/AnimeGAN_Hayao_lsgan_300_300_1_3_10 --test_dir dataset/test/real --style_name H` + +To test on your own photos, upload your own directory under AnimeGan and replace ```--test_dir``` with you own directory, output will be stored in ```results``` +""" + +!python main.py --phase test --dataset Hayao + +"""Rename + +``` +AnimeGAN_Hayao_lsgan_300_300_1_3_10 +to +old_AnimeGAN_Hayao_lsgan_300_300_1_3_10 +``` + +in the checkpoint folder so we can train from sractch + +## Train + +In ```main.py``` : + +For Shinkai, ```con_weight``` should be set to 1.1 +""" + +!python main.py --phase train --dataset Shinkai --epoch 101 --init_epoch 1 \ No newline at end of file