{"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"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":21755,"databundleVersionId":1475600,"sourceType":"competition"}],"dockerImageVersionId":30559,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"\nimport numpy as np \nimport pandas as pd \n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-10-23T20:00:01.052907Z","iopub.execute_input":"2023-10-23T20:00:01.053352Z","iopub.status.idle":"2023-10-23T20:00:09.350179Z","shell.execute_reply.started":"2023-10-23T20:00:01.053318Z","shell.execute_reply":"2023-10-23T20:00:09.349189Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport random\nimport tensorflow as tf  # tf.__version__ >= 2.2.0\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom tensorflow.keras import Model, Sequential\nfrom tensorflow.keras.layers import Input, Reshape, Dense, Dropout, \\\n    LeakyReLU, Conv2D, Conv2DTranspose, Embedding, \\\n    Concatenate, multiply, Flatten, BatchNormalization\nfrom tensorflow.keras.initializers import glorot_normal\nfrom tensorflow.keras.optimizers import Adam","metadata":{"execution":{"iopub.status.busy":"2023-10-23T20:14:36.275151Z","iopub.execute_input":"2023-10-23T20:14:36.276114Z","iopub.status.idle":"2023-10-23T20:14:36.281833Z","shell.execute_reply.started":"2023-10-23T20:14:36.276077Z","shell.execute_reply":"2023-10-23T20:14:36.280867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(tf.__version__)","metadata":{"execution":{"iopub.status.busy":"2023-10-23T20:00:16.927312Z","iopub.execute_input":"2023-10-23T20:00:16.928315Z","iopub.status.idle":"2023-10-23T20:00:16.933215Z","shell.execute_reply.started":"2023-10-23T20:00:16.928278Z","shell.execute_reply":"2023-10-23T20:00:16.932237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nSEED=42\nos.environ['PYTHONHASHSEED'] = str(SEED)\nrandom.seed(SEED)\nnp.random.seed(SEED)\ntf.random.set_seed(SEED)\n\nweight_init = glorot_normal(seed=SEED)  # for further usage","metadata":{"execution":{"iopub.status.busy":"2023-10-23T20:14:40.334719Z","iopub.execute_input":"2023-10-23T20:14:40.335728Z","iopub.status.idle":"2023-10-23T20:14:40.344507Z","shell.execute_reply.started":"2023-10-23T20:14:40.335695Z","shell.execute_reply":"2023-10-23T20:14:40.343697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load data\nfrom tensorflow.keras.datasets.fashion_mnist import load_data\n(x_train, y_train), (x_test, y_test) = load_data()\nx_train.shape,y_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-10-23T20:14:41.152885Z","iopub.execute_input":"2023-10-23T20:14:41.153233Z","iopub.status.idle":"2023-10-23T20:14:41.533789Z","shell.execute_reply.started":"2023-10-23T20:14:41.153207Z","shell.execute_reply":"2023-10-23T20:14:41.532895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Data Preparation\n# Change the shape as in (n,dim,dim,channel)\ndef change_shape(images):\n    shape=images.shape\n    if len(shape)==3:\n        images=images.reshape(-1,shape[-1],shape[-1],1)\n    elif len(shape)==4 and shape[-1]>3:\n        images=images.reshape(-1,shape[-1],shape[-1],shape[1])\n    return images\n\n\nx_train, x_test=change_shape(x_train),change_shape(x_test)\ny_train,y_test=y_train.reshape(-1),y_test.reshape(-1)\n\n\n# Set the channel\nchannel=x_train.shape[-1]\n\n#It is suggested to use [-1,1] as input fed into GAN training\nx_train=(x_train.astype(\"float32\")-127.5)/127.5   # this formulation makes processing faster\nx_test=(x_test.astype(\"float32\")-127.5)/127.5\n\n#Get image size\nimg_size=x_train[0].shape\n\n#Get number of classes\nn_classes=len(np.unique(y_train))","metadata":{"execution":{"iopub.status.busy":"2023-10-23T20:14:44.571742Z","iopub.execute_input":"2023-10-23T20:14:44.572598Z","iopub.status.idle":"2023-10-23T20:14:44.674356Z","shell.execute_reply.started":"2023-10-23T20:14:44.57255Z","shell.execute_reply":"2023-10-23T20:14:44.673333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"latent_dim=32","metadata":{"execution":{"iopub.status.busy":"2023-10-23T20:14:46.233997Z","iopub.execute_input":"2023-10-23T20:14:46.234778Z","iopub.status.idle":"2023-10-23T20:14:46.240241Z","shell.execute_reply.started":"2023-10-23T20:14:46.234736Z","shell.execute_reply":"2023-10-23T20:14:46.23911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# DCGAN Architecture","metadata":{}},{"cell_type":"code","source":"# Model Setup\n# Build generator with convolution layer\ndef generator_conv():\n    noise = Input(shape=(latent_dim,))\n    x = Dense(3 * 3 * 128)(noise)\n    x = LeakyReLU(alpha=0.2)(x)\n\n    ## Out size: 3 x 3 x 128\n    x = Reshape((3, 3, 128))(x)\n\n    ## Size: 7 x 7 x 128\n    # remove padding='same' to scale 6x6 up to 7x7\n    x = Conv2DTranspose(filters=128,\n                        kernel_size=(3, 3),\n                        strides=(2, 2),\n                        # padding='same',\n                        kernel_initializer=weight_init)(x)\n    x = BatchNormalization()(x)\n    x = LeakyReLU(0.2)(x)\n\n    ## Size: 14 x 14 x 64\n    x = Conv2DTranspose(64, (3, 3), strides=(2, 2), padding='same', kernel_initializer=weight_init)(x)\n    x = BatchNormalization()(x)\n    x = LeakyReLU(0.2)(x)\n\n    ## Size: 28 x 28 x channel\n    out = Conv2DTranspose(channel, (3, 3), activation='tanh', strides=(2, 2), padding='same',\n                          kernel_initializer=weight_init)(x)\n\n    model = Model(inputs=noise, outputs=out)\n    return model\n    ","metadata":{"execution":{"iopub.status.busy":"2023-10-23T20:17:26.179632Z","iopub.execute_input":"2023-10-23T20:17:26.180322Z","iopub.status.idle":"2023-10-23T20:17:26.188875Z","shell.execute_reply.started":"2023-10-23T20:17:26.180288Z","shell.execute_reply":"2023-10-23T20:17:26.187807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Build Discriminator with convolution layer\ndef discriminator_conv():\n    # 28 x 28 x channel\n    img = Input(img_size)\n\n    # 14 x 14 x 32\n    x = Conv2D(32, kernel_size=(3, 3), strides=(2, 2), padding='same', kernel_initializer=weight_init)(img)\n    x = LeakyReLU(0.2)(x)\n\n    # 7 x 7 x 64\n    x = Conv2D(64, (3, 3), strides=(2, 2), padding='same', kernel_initializer=weight_init)(x)\n    x = LeakyReLU(0.2)(x)\n\n    # 3 x 3 x 128\n    x = Conv2D(128, (3, 3), strides=(2, 2), kernel_initializer=weight_init)(x)\n    x = LeakyReLU(0.2)(x)\n\n    x = Flatten()(x)\n    x = Dropout(0.4)(x)\n    out = Dense(1)(x)\n\n    model = Model(inputs=img, outputs=out)\n    return model","metadata":{"execution":{"iopub.status.busy":"2023-10-23T20:17:26.720497Z","iopub.execute_input":"2023-10-23T20:17:26.720979Z","iopub.status.idle":"2023-10-23T20:17:26.728608Z","shell.execute_reply.started":"2023-10-23T20:17:26.720941Z","shell.execute_reply":"2023-10-23T20:17:26.727636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Build our GAN\nclass DCGAN(Model):\n    def __init__(\n        self,\n        discriminator,\n        generator,\n        latent_dim,\n        train_ratio=1,\n    ):\n        super(DCGAN, self).__init__()\n        self.discriminator = discriminator\n        self.generator = generator\n        self.latent_dim = latent_dim\n        self.train_ratio = train_ratio\n\n    def compile(self, d_optimizer, g_optimizer, d_loss_fn, g_loss_fn):\n        super(DCGAN, self).compile()\n        self.d_optimizer = d_optimizer\n        self.g_optimizer = g_optimizer\n        self.d_loss_fn = d_loss_fn\n        self.g_loss_fn = g_loss_fn\n\n    def train_step(self, data):\n        if isinstance(data, tuple):\n            real_images = data[0]\n        else:\n            real_images = data\n        # Get the batch size\n        batch_size = tf.shape(real_images)[0]\n\n        ########################### Train the Discriminator ###########################\n        # training train_ratio times on D while training once on G\n        for i in range(self.train_ratio):\n            # Get the latent vector\n            random_latent_vectors = tf.random.normal(\n                shape=(batch_size, self.latent_dim)\n            )\n            with tf.GradientTape() as tape:\n                # Generate fake images from the latent vector\n                fake_images = self.generator(random_latent_vectors, training=True)\n                # Get the logits for the fake images\n                fake_logits = self.discriminator(fake_images, training=True)\n                # Get the logits for real images\n                real_logits = self.discriminator(real_images, training=True)\n\n                # Calculate loss of D\n                d_loss = self.d_loss_fn(real_logits, fake_logits)\n\n\n            # Get the gradients w.r.t the discriminator loss\n            d_gradient = tape.gradient(d_loss, self.discriminator.trainable_variables)\n            # Update the weights of the discriminator using the discriminator optimizer\n            self.d_optimizer.apply_gradients(\n                zip(d_gradient, self.discriminator.trainable_variables)\n            )\n\n        ########################### Train the Generator ###########################\n        # Get the latent vector\n        random_latent_vectors = tf.random.normal(shape=(batch_size, self.latent_dim))\n        with tf.GradientTape() as tape:\n            # Generate fake images using the generator\n            generated_images = self.generator(random_latent_vectors, training=True)\n            # Get the discriminator logits for fake images\n            gen_img_logits = self.discriminator(generated_images, training=True)\n            # Calculate the generator loss\n            g_loss = self.g_loss_fn(gen_img_logits)\n\n        # Get the gradients w.r.t the generator loss\n        gen_gradient = tape.gradient(g_loss, self.generator.trainable_variables)\n        # Update the weights of the generator using the generator optimizer\n        self.g_optimizer.apply_gradients(\n            zip(gen_gradient, self.generator.trainable_variables)\n        )\n        return {\"d_loss\": d_loss, \"g_loss\": g_loss}","metadata":{"execution":{"iopub.status.busy":"2023-10-23T20:17:27.106588Z","iopub.execute_input":"2023-10-23T20:17:27.106973Z","iopub.status.idle":"2023-10-23T20:17:27.120082Z","shell.execute_reply.started":"2023-10-23T20:17:27.106944Z","shell.execute_reply":"2023-10-23T20:17:27.119103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Compiling Model\n# Set the optimizers for both networks\ngenerator_optimizer=Adam(learning_rate=.0002, beta_1=.5,beta_2=.9)\ndiscriminator_optimizer=Adam(learning_rate=.0002, beta_1=.5,beta_2=.9)\n\n#Define the loss functions to be used  for discriminator\ndef discriminator_loss(real_logits,fake_logits):\n    real_loss=tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=real_logits, labels=tf.ones_like(real_logits)))\n    fake_loss=tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=fake_logits, labels=tf.zeros_like(fake_logits)))\n    \n    return fake_loss+real_loss\n\ndef generator_loss(fake_logits):\n    fake_loss=tf.reduce_mean( tf.nn.sigmoid_cross_entropy_with_logits(logits=fake_logits, labels=tf.ones_like(fake_logits)))\n    \n    return fake_loss\n\nd_model=discriminator_conv()\ng_model=generator_conv()\n\ndcgan=DCGAN(generator=g_model,discriminator=d_model,latent_dim=latent_dim,train_ratio=1)\n\ndcgan.compile(d_optimizer=discriminator_optimizer,g_optimizer=generator_optimizer,g_loss_fn=generator_loss,d_loss_fn=discriminator_loss)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-23T20:17:27.753042Z","iopub.execute_input":"2023-10-23T20:17:27.753408Z","iopub.status.idle":"2023-10-23T20:17:27.911601Z","shell.execute_reply.started":"2023-10-23T20:17:27.753375Z","shell.execute_reply":"2023-10-23T20:17:27.91083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# %% ----------------------------------- Start Training ----------------------------------------------------------------\n# Plot/save generated images through training\ndef plt_img(generator):\n    np.random.seed(42)\n    n = n_classes\n\n    noise = np.random.normal(size=(n * n, latent_dim))\n    decoded_imgs = generator.predict(noise)\n\n    decoded_imgs = decoded_imgs * 0.5 + 0.5\n    x_real = x_test * 0.5 + 0.5\n\n    plt.figure(figsize=(n, n + 1))\n    for i in range(n):\n        # display original\n        ax = plt.subplot(n + 1, n, i + 1)\n        if channel == 3:\n            plt.imshow(x_real[y_test == i][0].reshape(img_size[0], img_size[1], img_size[2]))\n        else:\n            plt.imshow(x_real[y_test == i][0].reshape(img_size[0], img_size[1]))\n            plt.gray()\n        ax.get_xaxis().set_visible(False)\n        ax.get_yaxis().set_visible(False)\n\n        for j in range(n):\n            # display generation\n            ax = plt.subplot(n + 1, n, (i + 1) * n + j + 1)\n            if channel == 3:\n                plt.imshow(decoded_imgs[i * n + j].reshape(img_size[0], img_size[1], img_size[2]))\n            else:\n                plt.imshow(decoded_imgs[i * n + j].reshape(img_size[0], img_size[1]))\n                plt.gray()\n            ax.get_xaxis().set_visible(False)\n            ax.get_yaxis().set_visible(False)\n    plt.show()\n    return\n\n############################# Start training #############################\nLEARNING_STEPS = 10\nfor learning_step in range(LEARNING_STEPS):\n    print('LEARNING STEP # ', learning_step + 1, '-' * 50)\n    dcgan.fit(x_train, epochs=1, batch_size=128)\n    if (learning_step+1)%2 == 0:\n        plt_img(dcgan.generator)","metadata":{"execution":{"iopub.status.busy":"2023-10-23T20:17:36.130599Z","iopub.execute_input":"2023-10-23T20:17:36.131199Z","iopub.status.idle":"2023-10-23T20:18:49.090471Z","shell.execute_reply.started":"2023-10-23T20:17:36.131157Z","shell.execute_reply":"2023-10-23T20:18:49.089522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}