{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":21755,"databundleVersionId":1475600,"sourceType":"competition"}],"dockerImageVersionId":30616,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# **Setting up the environmnet**","metadata":{}},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2023-12-08T15:32:28.423777Z","iopub.execute_input":"2023-12-08T15:32:28.424688Z","iopub.status.idle":"2023-12-08T15:32:28.428890Z","shell.execute_reply.started":"2023-12-08T15:32:28.424638Z","shell.execute_reply":"2023-12-08T15:32:28.427985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport cv2\nimport numpy as np\nimport tensorflow as tf\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2023-12-08T15:32:28.431105Z","iopub.execute_input":"2023-12-08T15:32:28.431474Z","iopub.status.idle":"2023-12-08T15:32:28.438753Z","shell.execute_reply.started":"2023-12-08T15:32:28.431441Z","shell.execute_reply":"2023-12-08T15:32:28.437601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Dataset generation**","metadata":{}},{"cell_type":"code","source":"monet = \"/kaggle/input/gan-getting-started/monet_jpg\"\n\ndef load_images_from_folder(folder):\n    images = []\n    for filename in os.listdir(folder):\n        if filename.endswith('.jpg'):\n            img = cv2.imread(os.path.join(folder, filename))\n            if img is not None:\n                images.append(img)\n            else:\n                continue\n    return images\n\n\nmonet_images = load_images_from_folder(monet)","metadata":{"execution":{"iopub.status.busy":"2023-12-08T15:32:28.440104Z","iopub.execute_input":"2023-12-08T15:32:28.440463Z","iopub.status.idle":"2023-12-08T15:32:29.103034Z","shell.execute_reply.started":"2023-12-08T15:32:28.440428Z","shell.execute_reply":"2023-12-08T15:32:29.102204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"monet_images[0].shape","metadata":{"execution":{"iopub.status.busy":"2023-12-08T15:32:29.105355Z","iopub.execute_input":"2023-12-08T15:32:29.106147Z","iopub.status.idle":"2023-12-08T15:32:29.113613Z","shell.execute_reply.started":"2023-12-08T15:32:29.106110Z","shell.execute_reply":"2023-12-08T15:32:29.112740Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"monet_images = np.array(monet_images)","metadata":{"execution":{"iopub.status.busy":"2023-12-08T15:32:29.114602Z","iopub.execute_input":"2023-12-08T15:32:29.114956Z","iopub.status.idle":"2023-12-08T15:32:29.141921Z","shell.execute_reply.started":"2023-12-08T15:32:29.114927Z","shell.execute_reply":"2023-12-08T15:32:29.141086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 32\ndataset = tf.data.Dataset.from_tensor_slices(monet_images).shuffle(1000)\ndataset = dataset.batch(batch_size, drop_remainder=True).prefetch(1)","metadata":{"execution":{"iopub.status.busy":"2023-12-08T15:32:29.143063Z","iopub.execute_input":"2023-12-08T15:32:29.143400Z","iopub.status.idle":"2023-12-08T15:32:29.289377Z","shell.execute_reply.started":"2023-12-08T15:32:29.143373Z","shell.execute_reply":"2023-12-08T15:32:29.288590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **GAN (Generative Adversarial Network)**","metadata":{}},{"cell_type":"code","source":"tf.random.set_seed(42)\ntf.keras.backend.clear_session()\n\ncodings_size = 100\n\nDense = tf.keras.layers.Dense\ngenerator = tf.keras.Sequential([\n    Dense(100, activation=\"relu\", kernel_initializer=\"he_normal\"),\n    Dense(150, activation=\"relu\", kernel_initializer=\"he_normal\"),\n    Dense(200, activation=\"relu\", kernel_initializer=\"he_normal\"),\n    Dense(256 * 256 * 3, activation=\"sigmoid\"),\n    tf.keras.layers.Reshape([256, 256 , 3])\n])\ndiscriminator = tf.keras.Sequential([\n    tf.keras.layers.Flatten(),\n    Dense(200, activation=\"relu\", kernel_initializer=\"he_normal\"),\n    Dense(150, activation=\"relu\", kernel_initializer=\"he_normal\"),\n    Dense(100, activation=\"relu\", kernel_initializer=\"he_normal\"),\n    Dense(1, activation=\"sigmoid\")\n])\ngan = tf.keras.Sequential([generator, discriminator])","metadata":{"execution":{"iopub.status.busy":"2023-12-08T15:32:29.290944Z","iopub.execute_input":"2023-12-08T15:32:29.291547Z","iopub.status.idle":"2023-12-08T15:32:29.401309Z","shell.execute_reply.started":"2023-12-08T15:32:29.291511Z","shell.execute_reply":"2023-12-08T15:32:29.400517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"discriminator.compile(loss=\"binary_crossentropy\", optimizer=\"rmsprop\")\ndiscriminator.trainable = False\ngan.compile(loss=\"binary_crossentropy\", optimizer=\"rmsprop\")","metadata":{"execution":{"iopub.status.busy":"2023-12-08T15:32:29.402358Z","iopub.execute_input":"2023-12-08T15:32:29.402616Z","iopub.status.idle":"2023-12-08T15:32:29.422581Z","shell.execute_reply.started":"2023-12-08T15:32:29.402594Z","shell.execute_reply":"2023-12-08T15:32:29.421921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_multiple_images(images, n_cols=None):\n    n_cols = n_cols or len(images)\n    n_rows = (len(images) - 1) // n_cols + 1\n    if images.shape[-1] == 1:\n        images = images.squeeze(axis=-1)\n    plt.figure(figsize=(n_cols, n_rows))\n    for index, image in enumerate(images):\n        plt.subplot(n_rows, n_cols, index + 1)\n        plt.imshow(image, cmap=\"binary\")\n        plt.axis(\"off\")\n","metadata":{"execution":{"iopub.status.busy":"2023-12-08T15:32:29.425150Z","iopub.execute_input":"2023-12-08T15:32:29.425417Z","iopub.status.idle":"2023-12-08T15:32:29.431493Z","shell.execute_reply.started":"2023-12-08T15:32:29.425394Z","shell.execute_reply":"2023-12-08T15:32:29.430600Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train_gan(gan, dataset, batch_size, codings_size, n_epochs):\n    generator, discriminator = gan.layers\n    for epoch in range(n_epochs):\n        print(f\"Epoch {epoch + 1}/{n_epochs}\")\n        for X_batch in dataset:\n            # phase 1 - training the discriminator\n            noise = tf.random.normal(shape=[batch_size, codings_size])\n            generated_images = generator(noise)\n            \n            # Convert X_batch to float32\n            X_batch_float = tf.cast(X_batch, tf.float32)\n            \n            # Concatenate float tensors\n            X_fake_and_real = tf.concat([generated_images, X_batch_float], axis=0)\n            y1 = tf.constant([[0.]] * batch_size + [[1.]] * batch_size)\n            discriminator.train_on_batch(X_fake_and_real, y1)\n            \n            # phase 2 - training the generator\n            noise = tf.random.normal(shape=[batch_size, codings_size])\n            y2 = tf.constant([[1.]] * batch_size)\n            gan.train_on_batch(noise, y2)\n        \n        # Plot images during training (assuming generated_images is defined outside the loop)\n        plot_multiple_images(generated_images.numpy(), 8)\n        plt.show()\ntrain_gan(gan, dataset, batch_size, codings_size, n_epochs=50)","metadata":{"execution":{"iopub.status.busy":"2023-12-08T15:32:29.432747Z","iopub.execute_input":"2023-12-08T15:32:29.433353Z","iopub.status.idle":"2023-12-08T15:34:46.413036Z","shell.execute_reply.started":"2023-12-08T15:32:29.433320Z","shell.execute_reply":"2023-12-08T15:34:46.412047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.random.set_seed(42)\n\ncodings = tf.random.normal(shape=[batch_size, codings_size])\ngenerated_images = generator.predict(codings)","metadata":{"execution":{"iopub.status.busy":"2023-12-08T15:34:46.414394Z","iopub.execute_input":"2023-12-08T15:34:46.414785Z","iopub.status.idle":"2023-12-08T15:34:46.621567Z","shell.execute_reply.started":"2023-12-08T15:34:46.414749Z","shell.execute_reply":"2023-12-08T15:34:46.620737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_multiple_images(generated_images, 8)","metadata":{"execution":{"iopub.status.busy":"2023-12-08T15:34:46.622759Z","iopub.execute_input":"2023-12-08T15:34:46.623074Z","iopub.status.idle":"2023-12-08T15:34:48.064729Z","shell.execute_reply.started":"2023-12-08T15:34:46.623046Z","shell.execute_reply":"2023-12-08T15:34:48.063848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **DCGAN (Deep Convolutional Generative Adversarial Network)**","metadata":{}},{"cell_type":"code","source":"tf.random.set_seed(42) \ntf.keras.backend.clear_session()\n\ncodings_size = 100\n\ngenerator = tf.keras.Sequential([\n    tf.keras.layers.Dense(64 * 64 * 128),\n    tf.keras.layers.Reshape([64, 64, 128]),\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.Conv2DTranspose(64, kernel_size=5, strides=2,\n                                    padding=\"same\", activation=\"relu\"),\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.Conv2DTranspose(1, kernel_size=5, strides=2,\n                                    padding=\"same\", activation=\"tanh\"),\n])\ndiscriminator = tf.keras.Sequential([\n    tf.keras.layers.Conv2D(64, kernel_size=5, strides=2, padding=\"same\",\n                        activation=tf.keras.layers.LeakyReLU(0.2)),\n    tf.keras.layers.Dropout(0.4),\n    tf.keras.layers.Conv2D(128, kernel_size=5, strides=2, padding=\"same\",\n                        activation=tf.keras.layers.LeakyReLU(0.2)),\n    tf.keras.layers.Dropout(0.4),\n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dense(1, activation=\"sigmoid\")\n])\ngan = tf.keras.Sequential([generator, discriminator])","metadata":{"execution":{"iopub.status.busy":"2023-12-08T15:34:48.066236Z","iopub.execute_input":"2023-12-08T15:34:48.066583Z","iopub.status.idle":"2023-12-08T15:34:48.110249Z","shell.execute_reply.started":"2023-12-08T15:34:48.066553Z","shell.execute_reply":"2023-12-08T15:34:48.109484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"discriminator.compile(loss=\"binary_crossentropy\", optimizer=\"rmsprop\")\ndiscriminator.trainable = False\ngan.compile(loss=\"binary_crossentropy\", optimizer=\"rmsprop\")","metadata":{"execution":{"iopub.status.busy":"2023-12-08T15:34:48.111373Z","iopub.execute_input":"2023-12-08T15:34:48.111668Z","iopub.status.idle":"2023-12-08T15:34:48.132885Z","shell.execute_reply.started":"2023-12-08T15:34:48.111643Z","shell.execute_reply":"2023-12-08T15:34:48.132023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_dcgan = monet_images.reshape(-1, 256, 256, 1) * 2. - 1.","metadata":{"execution":{"iopub.status.busy":"2023-12-08T15:34:48.134165Z","iopub.execute_input":"2023-12-08T15:34:48.134846Z","iopub.status.idle":"2023-12-08T15:34:48.327485Z","shell.execute_reply.started":"2023-12-08T15:34:48.134793Z","shell.execute_reply":"2023-12-08T15:34:48.326625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 32\ndataset = tf.data.Dataset.from_tensor_slices(X_train_dcgan)\ndataset = dataset.shuffle(1000)\ndataset = dataset.batch(batch_size, drop_remainder=True).prefetch(1)\ntrain_gan(gan, dataset, batch_size, codings_size, n_epochs=50)","metadata":{"execution":{"iopub.status.busy":"2023-12-08T15:34:48.328637Z","iopub.execute_input":"2023-12-08T15:34:48.328978Z","iopub.status.idle":"2023-12-08T15:35:54.871951Z","shell.execute_reply.started":"2023-12-08T15:34:48.328951Z","shell.execute_reply":"2023-12-08T15:35:54.870410Z"},"trusted":true},"execution_count":null,"outputs":[]}]}