{"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"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# Importing necessary libraries and modules\n\nfrom kaggle_datasets import KaggleDatasets\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nimport tensorflow_addons as tfa","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-03-23T07:18:51.058673Z","iopub.execute_input":"2023-03-23T07:18:51.059032Z","iopub.status.idle":"2023-03-23T07:18:51.064291Z","shell.execute_reply.started":"2023-03-23T07:18:51.058999Z","shell.execute_reply":"2023-03-23T07:18:51.063263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nimport numpy as np\nimport os\nimport time\nimport random\n\n# Define the hyperparameters\nBUFFER_SIZE = 1000\nBATCH_SIZE = 1\nIMG_WIDTH = 256\nIMG_HEIGHT = 256\nEPOCHS = 50\nnoise_dim = 100\nnum_examples_to_generate = 16\n\n# Load the Monet and Photo datasets\nmonet_path = '/kaggle/input/gan-getting-started/monet_jpg'\nphoto_path = '/kaggle/input/gan-getting-started/photo_jpg'\nmonet_images = []\nphoto_images = []\nfor filename in os.listdir(monet_path):\n    monet_images.append(os.path.join(monet_path,filename))\nfor filename in os.listdir(photo_path):\n    photo_images.append(os.path.join(photo_path,filename))","metadata":{"execution":{"iopub.status.busy":"2023-03-23T10:00:38.087171Z","iopub.execute_input":"2023-03-23T10:00:38.088224Z","iopub.status.idle":"2023-03-23T10:00:38.273179Z","shell.execute_reply.started":"2023-03-23T10:00:38.088165Z","shell.execute_reply":"2023-03-23T10:00:38.272217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# obtaining Google Cloud Storage (GCS) path for Kaggle dataset\n\nPATH = KaggleDatasets().get_gcs_path('gan-getting-started')","metadata":{"execution":{"iopub.status.busy":"2023-03-23T07:18:54.558301Z","iopub.execute_input":"2023-03-23T07:18:54.558724Z","iopub.status.idle":"2023-03-23T07:18:54.915932Z","shell.execute_reply.started":"2023-03-23T07:18:54.558688Z","shell.execute_reply":"2023-03-23T07:18:54.914923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Reading the images in TFRecord format\n\nMONET_FILENAMES = tf.io.gfile.glob(str(PATH + '/monet_tfrec/*.tfrec'))\nprint('Monet TFRecord Files:', len(MONET_FILENAMES))\n\nPHOTO_FILENAMES = tf.io.gfile.glob(str(PATH + '/photo_tfrec/*.tfrec'))\nprint('Photo TFRecord Files:', len(PHOTO_FILENAMES))","metadata":{"execution":{"iopub.status.busy":"2023-03-23T07:18:55.957706Z","iopub.execute_input":"2023-03-23T07:18:55.958806Z","iopub.status.idle":"2023-03-23T07:18:56.126476Z","shell.execute_reply.started":"2023-03-23T07:18:55.958765Z","shell.execute_reply":"2023-03-23T07:18:56.125441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# performing some DATA PREPROCESSING\n\nIMAGE_SIZE = [256, 256]                    # Defining the desired image size\n\n# Function to decode image from a byte string and preprocess it\n\ndef decode_image(image):\n    # Decode the JPEG image\n    image = tf.image.decode_jpeg(image, channels=3)\n    # Convert pixel values to [-1, 1] range\n    image = (tf.cast(image, tf.float32) / 127.5) - 1\n    # Reshape the image to the desired size\n    image = tf.reshape(image, [*IMAGE_SIZE, 3])\n    return image\n\n# Function to read a single example from a TFRecord file\ndef read_tfrecord(example):\n    # Define the expected format of each example in the TFRecord file\n    tfrecord_format = {\n        \"image_name\": tf.io.FixedLenFeature([], tf.string),\n        \"image\": tf.io.FixedLenFeature([], tf.string),\n        \"target\": tf.io.FixedLenFeature([], tf.string)\n    }\n    \n# Parsing the example using the defined format\n    example = tf.io.parse_single_example(example, tfrecord_format)\n# Decode the image from the byte string\n    image = decode_image(example['image'])\n    \n    \n    \n    \n    \n# Performing  some DATA AUGMENTATION\n        \n    image = tf.image.random_crop(image, size=[int(IMAGE_SIZE[0]*0.8), int(IMAGE_SIZE[1]*0.8), 3]) # Random crop and flip\n    image = tf.image.random_flip_left_right(image)\n    noise = tf.random.normal(shape=tf.shape(image), mean=0.0, stddev=0.1, dtype=tf.float32) # Add Gaussian noise\n    image = image + noise\n    image = tf.image.adjust_brightness(image, delta=0.2) # Adjust brightness and contrast\n    image = tf.image.adjust_contrast(image, contrast_factor=1.2)\n    \n    return image\n\n","metadata":{"execution":{"iopub.status.busy":"2023-03-23T07:25:19.848432Z","iopub.execute_input":"2023-03-23T07:25:19.849447Z","iopub.status.idle":"2023-03-23T07:25:19.859834Z","shell.execute_reply.started":"2023-03-23T07:25:19.849407Z","shell.execute_reply":"2023-03-23T07:25:19.858847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Function to load a dataset\n\ndef load_dataset(filenames, labeled=True, ordered=False):\n    # Load the TFRecord files\n    dataset = tf.data.TFRecordDataset(filenames)\n    \n    # Apply preprocessing to the images in parallel\n    AUTOTUNE = tf.data.AUTOTUNE\n    dataset = dataset.map(read_tfrecord, num_parallel_calls=AUTOTUNE)\n    \n    # Return the processed dataset\n    return dataset\n\n\n\n# Load the Monet and photo datasets using the load_dataset function\nmonet_ds = load_dataset(MONET_FILENAMES, labeled=True).batch(100)\nphoto_ds = load_dataset(PHOTO_FILENAMES, labeled=True).batch(100)\n\n\n\n# Get an example Monet image and an example photo image\nexample_monet = next(iter(monet_ds))\nexample_photo = next(iter(photo_ds))\n\n","metadata":{"execution":{"iopub.status.busy":"2023-03-23T09:28:00.190454Z","iopub.execute_input":"2023-03-23T09:28:00.191057Z","iopub.status.idle":"2023-03-23T09:28:02.201076Z","shell.execute_reply.started":"2023-03-23T09:28:00.191019Z","shell.execute_reply":"2023-03-23T09:28:02.200022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(example_photo.shape)\nprint(example_monet.shape)\n","metadata":{"execution":{"iopub.status.busy":"2023-03-23T08:32:04.636044Z","iopub.execute_input":"2023-03-23T08:32:04.636749Z","iopub.status.idle":"2023-03-23T08:32:04.642829Z","shell.execute_reply.started":"2023-03-23T08:32:04.636709Z","shell.execute_reply":"2023-03-23T08:32:04.641635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axs = plt.subplots(nrows=7, ncols=2, figsize=(5, 20))\n\nfor i in range(7):\n    axs[i, 0].imshow(example_photo[i] * 0.5 + 0.5)\n    axs[i, 0].set_title('Photo')\n\n    axs[i, 1].imshow(example_monet[i] * 0.5 + 0.5)\n    axs[i, 1].set_title('Monet')\n\nplt.tight_layout()\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-03-23T09:28:04.065796Z","iopub.execute_input":"2023-03-23T09:28:04.066594Z","iopub.status.idle":"2023-03-23T09:28:05.521901Z","shell.execute_reply.started":"2023-03-23T09:28:04.066546Z","shell.execute_reply":"2023-03-23T09:28:05.520815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Define the discriminator model\ndiscriminator = tf.keras.Sequential([ \n    tf.keras.layers.Input(shape=(128, 128, 3)),\n\n# Downsample block 1\nlayers.Conv2D(64, (4, 4), strides=(2, 2), padding='same'),\nlayers.LeakyReLU(),\nlayers.Dropout(0.3),\n\n# Downsample block 2\nlayers.Conv2D(128, (4, 4), strides=(2, 2), padding='same'),\nlayers.LeakyReLU(),\nlayers.Dropout(0.3),\n\n# Downsample block 3\nlayers.Conv2D(256, (4, 4), strides=(2, 2), padding='same'),\nlayers.LeakyReLU(),\nlayers.Dropout(0.3),\n\n# Output layer\nlayers.Flatten(),\nlayers.Dense(1)\n])\n\n#Print the summary of the discriminator model\ndiscriminator.summary()","metadata":{"execution":{"iopub.status.busy":"2023-03-23T09:50:44.677028Z","iopub.execute_input":"2023-03-23T09:50:44.677977Z","iopub.status.idle":"2023-03-23T09:50:44.773248Z","shell.execute_reply.started":"2023-03-23T09:50:44.677940Z","shell.execute_reply":"2023-03-23T09:50:44.772505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define the generator model\ngenerator = tf.keras.Sequential()\ntf.keras.layers.Input(shape=(128, 128, 3))\n# Add a dense layer with input shape 100 and output shape 16*16*256\ngenerator.add(layers.Dense(16*16*256, input_shape=(100,)))\ngenerator.add(layers.Reshape((16, 16, 256)))\n\n# Add a transposed convolutional layer with 128 filters, 4x4 kernel size, and stride 2\ngenerator.add(layers.Conv2DTranspose(128, (4, 4), strides=(2, 2), padding='same'))\ngenerator.add(layers.BatchNormalization())\ngenerator.add(layers.LeakyReLU())\n\n# Add another transposed convolutional layer with 64 filters, 4x4 kernel size, and stride 2\ngenerator.add(layers.Conv2DTranspose(64, (4, 4), strides=(2, 2), padding='same'))\ngenerator.add(layers.BatchNormalization())\ngenerator.add(layers.LeakyReLU())\n\n# Add a final transposed convolutional layer with 3 filters (corresponding to RGB channels), 4x4 kernel size, and stride 2\ngenerator.add(layers.Conv2DTranspose(3, (4, 4), strides=(2, 2), padding='same', activation='tanh'))\n\n# Print the summary of the generator model\ngenerator.summary()\n","metadata":{"execution":{"iopub.status.busy":"2023-03-23T09:44:22.237848Z","iopub.execute_input":"2023-03-23T09:44:22.238889Z","iopub.status.idle":"2023-03-23T09:44:22.365764Z","shell.execute_reply.started":"2023-03-23T09:44:22.238840Z","shell.execute_reply":"2023-03-23T09:44:22.364963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}