{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "bsFBAHonXKXK" }, "source": [ "# 事前準備" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "nW6H2RLjXIIm" }, "outputs": [], "source": [ "# Explore the platform of the execution environment\n", "# 実行環境のプラットホームを調べる\n", "import os\n", "import platform\n", "\n", "is_colab = 'google.colab' in str(get_ipython()) # Google Colab\n", "is_win = (os.name == 'nt') # Windows\n", "is_mac = 'macOS' in str(platform.platform()) # mac" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "b58QlbtoXT9d" }, "outputs": [], "source": [ "## [CHECK] If you get errors when running this page, please install specific versions of the packages.\n", "### [CHECK] このページの実行でエラーが起きる場合は、特定のバージョンのパッケージをインストールしてください。\n", "\n", "#! pip install tensorflow==2.8.2\n", "#! pip install numpy==1.21.6\n", "#! pip install matplotlib==3.2.2" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "CyyLwUwJXZhe", "outputId": "806bffa4-8425-46f3-a808-5cd854c85eac" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Colab only includes TensorFlow 2.x; %tensorflow_version has no effect.\n", "Reading package lists... Done\n", "Building dependency tree \n", "Reading state information... Done\n", "libcudnn8 is already the newest version (8.4.1.50-1+cuda11.6).\n", "The following package was automatically installed and is no longer required:\n", " libnvidia-common-460\n", "Use 'apt autoremove' to remove it.\n", "0 upgraded, 0 newly installed, 0 to remove and 18 not upgraded.\n" ] } ], "source": [ "if is_colab:\n", " %tensorflow_version 2.x\n", " # Install the specific versions of GPU Libraries\n", " !apt install --allow-change-held-packages libcudnn8=8.4.1.50-1+cuda11.6" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "ogKjc9_bXcFD", "outputId": "643f8731-e5bc-4111-c0c9-8227172e4fc0" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2.8.2\n" ] } ], "source": [ "import tensorflow as tf\n", "print(tf.__version__)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "e3ledtiDXcIa", "outputId": "d7ae2f35-a3dd-4117-f94a-45b764e57154" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1.21.6\n" ] } ], "source": [ "import numpy as np\n", "print(np.__version__)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "j-cakKlDXcMd", "outputId": "ca6a51ac-f746-4247-abcf-ec86ad2dd7a5" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3.2.2\n" ] } ], "source": [ "%matplotlib inline\n", "import matplotlib.pyplot as plt\n", "\n", "# verson check\n", "import matplotlib\n", "print(matplotlib.__version__)" ] }, { "cell_type": "markdown", "metadata": { "id": "FfzNBS8WXrQm" }, "source": [ "# 保存に関する設定" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "z6dWfVclXcQT", "outputId": "cdd86f2e-9882-48b5-e6be-2622cc76cf16" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount(\"/content/drive\", force_remount=True).\n" ] } ], "source": [ "FILE_PREFIX = '.'\n", "\n", "if is_colab:\n", " from google.colab import drive\n", " drive.mount('/content/drive')\n", "\n", " FILE_PREFIX = '/content/drive/MyDrive/'\n", "elif is_win or is_mac:\n", " FILE_PREFIX = os.path.join(os.path.expanduser('~'), 'Documents')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "6LtCEOFYXcTq" }, "outputs": [], "source": [ "### [CHECK] If you want to change the value of the FILE_PREFIX variable, please do it here.\n", "\n", "#FILE_PREFIX = \".\" ### [CHECK] ###" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "RmNueHNNXcXS", "outputId": "336b996d-2955-47b3-dfa4-697e87373dbb" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/content/drive/MyDrive/tf/styletransfer_run\n" ] } ], "source": [ "### [CHECK] If you want to change the saving path, please change it here.\n", "\n", "SAVE_PREFIX = os.path.join(FILE_PREFIX, \"tf/styletransfer_run\")\n", "\n", "print(SAVE_PREFIX)" ] }, { "cell_type": "markdown", "metadata": { "id": "Dw0kglQqVXEy" }, "source": [ "# 5.5 ニューラルスタイル変換\n", "\n", "CycleGAN では、訓練セットの画像は対(pair)になっている必要はない。\n", "\n", "ニューラルスタイル変換では、訓練セットを一切持たずに画像の画風を変換する。\n", "以下の3つの異なる損失関数の和を最小化する。\n", "
画像 | 特徴1 | >特徴2 | >特徴3 |
---|---|---|---|
画像1 | \n",
" \n", "1 0 0 0 0\n", "2 1 0 0 0\n", "2 2 1 0 0\n", "2 2 2 1 0\n", " | \n",
" \n", "1 0 0 0 0\n", "2 1 0 0 0\n", "2 1 1 1 0\n", "2 2 2 1 0\n", " | \n",
" \n", "2 2 0 1 2\n", "0 0 0 0 0\n", "0 0 0 0 0\n", "0 0 0 0 0\n", " |
画像2 | \n",
" \n", "0 0 0 0 0\n", "2 2 2 2 1\n", "0 2 2 2 1\n", "0 0 0 0 0\n", " | \n",
" \n", "0 0 0 0 1\n", "1 2 2 2 1\n", "0 2 2 2 1\n", "0 0 0 0 0\n", " | \n",
" \n", "0 0 0 2 0\n", "0 0 0 2 0\n", "1 0 0 2 0\n", "2 2 2 2 2\n", " |
画像3 | \n",
" \n", "2 2 2 2 2\n", "2 1 1 0 0\n", "0 0 0 0 0\n", "0 0 0 0 0\n", " | \n",
" \n", "0 0 0 0 0\n", "1 2 2 2 2\n", "2 2 2 2 2\n", "2 2 2 2 2\n", " | \n",
" \n", "0 0 0 0 0\n", "1 1 1 2 2\n", "2 2 2 2 2\n", "2 2 2 2 2\n", " |