# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="FpHPaEEoVSUn" colab_type="text"
# # DCGAN sample using tensorflow
#
# tensorflow を利用して MNIST で DCGAN を実行するサンプルです。
#
# - [Deep Convolutional Generative Adversarial Network][tutorial]
#
# [tutorial]: https://www.tensorflow.org/tutorials/generative/dcgan

# + [markdown] id="lcmZkCYOLQvk" colab_type="text"
# ## 環境の確認

# + id="LnMGmEyyLT2f" colab_type="code" outputId="45144fc0-8267-4774-f710-2089ad258944" colab={"base_uri": "https://localhost:8080/", "height": 53}
# !cat /etc/issue

# + id="UDMUXlaTLpG2" colab_type="code" outputId="913796cf-f9a4-4bf2-8ae6-a0529466b97e" colab={"base_uri": "https://localhost:8080/", "height": 71}
# !free -h

# + id="WHn3f12zLsTw" colab_type="code" outputId="b16b4deb-3d0f-4fdf-a0df-7280ffdc9911" colab={"base_uri": "https://localhost:8080/", "height": 1000}
# !cat /proc/cpuinfo

# + id="3FUZPKAcL4jO" colab_type="code" outputId="fe8f507b-fad4-455e-87e0-de49a1de987f" colab={"base_uri": "https://localhost:8080/", "height": 323}
# !nvidia-smi

# + id="w2V3sVtNPYdT" colab_type="code" outputId="a6fbfacf-2f49-420a-a82b-7a0c9125b066" colab={"base_uri": "https://localhost:8080/", "height": 35}
# !python --version

# + id="Zyf3UfsENhOf" colab_type="code" colab={}
from logging import Logger


def get_logger() -> Logger:
    import logging
    logger = logging.getLogger(__name__)

    fmt = "%(asctime)s %(levelname)s %(name)s :%(message)s"
    logging.basicConfig(level=logging.INFO, format=fmt)

    return logger


logger = get_logger()


# + id="EppKsLuHMYjR" colab_type="code" outputId="82552424-df85-48da-f25a-6b66654880aa" colab={"base_uri": "https://localhost:8080/", "height": 35}
def check_tf_version() -> None:
    import tensorflow as tf
    logger.info(tf.__version__)


check_tf_version()

# + [markdown] id="SKyYk7oMY21S" colab_type="text"
# ## ソースコードの取得

# + id="PjgDKO_RV_U5" colab_type="code" outputId="a80b4b94-64d3-450f-84f6-c65ba3889e30" colab={"base_uri": "https://localhost:8080/", "height": 395}
# 対象のコードを取得
# !git clone -n https://github.com/iimuz/til.git
# %cd til
# !git checkout fdfa134
# %cd python/dcgan_tensorflow

# + [markdown] id="_vUlewGLYtT1" colab_type="text"
# ## 実行

# + [markdown] id="J4llcBuDKMus" colab_type="text"
# ### 事前準備

# + id="H8Ng5oAwIevW" colab_type="code" colab={}
import tensorflow.compat.v1 as tfv1


tfv1.enable_eager_execution()

# + [markdown] id="NDZFpP_IKKCr" colab_type="text"
# ### データセットの確認

# + id="8AwAXG4UHZ7V" colab_type="code" outputId="ce17000a-df4a-48f1-dd97-d4895f0cf8e3" colab={"base_uri": "https://localhost:8080/", "height": 599}
# %run -i dataset.py

# + id="nv1OyOv3I9f4" colab_type="code" outputId="4c17d097-54b0-45e8-9592-50f57c0e91e2" colab={"base_uri": "https://localhost:8080/", "height": 91}
import dataset


raw_train, raw_test = dataset.get_batch_dataset()

# + [markdown] id="leX-NiQxKRHh" colab_type="text"
# ### ネットワークの確認

# + id="sLKS5JeWHdNl" colab_type="code" outputId="5d339bc2-6492-4a75-daa5-59bfdd2dd6a4" colab={"base_uri": "https://localhost:8080/", "height": 1000}
# %run -i network.py

# + [markdown] id="lpsueOcOJRrK" colab_type="text"
# ### 学習の実行

# + id="H60exsmWJWPo" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 550} outputId="0ada8b18-fcb6-4632-fe63-5ccc76fee25d"
import train


train.train(raw_train, batch_size=256, epochs=100, gen_input_dim=100, disc_input_shape=(28, 28, 1))

# + [markdown] id="0QiTCqIlJtMG" colab_type="text"
# ### 結果

# + id="J83wMaOaJvSA" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 377} outputId="5bbc75d7-a678-47fd-80a1-249a955ff5bc"
import utils

import IPython
from IPython import display

def show_generated_images():
    filepath = "_data/dcgan.gif"
    utils.save_gif("_data/", "image_at_epoch_*", filepath)

    try:
        from google.colab import files
    except ImportError:
        pass
    else:
        files.download(filepath)


show_generated_images()

StarcoderdataJupyterScriptsDedupFiltered