{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","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"},{"sourceId":100948,"sourceType":"modelInstanceVersion","modelInstanceId":81206,"modelId":105559},{"sourceId":106026,"sourceType":"modelInstanceVersion","modelInstanceId":85675,"modelId":109906},{"sourceId":107176,"sourceType":"modelInstanceVersion","isSourceIdPinned":true,"modelInstanceId":85675,"modelId":109906},{"sourceId":107373,"sourceType":"modelInstanceVersion","isSourceIdPinned":true,"modelInstanceId":85675,"modelId":109906},{"sourceId":107423,"sourceType":"modelInstanceVersion","isSourceIdPinned":true,"modelInstanceId":85675,"modelId":109906},{"sourceId":107463,"sourceType":"modelInstanceVersion","isSourceIdPinned":true,"modelInstanceId":85675,"modelId":109906}],"dockerImageVersionId":30746,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\n\nimport os","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-09-04T16:48:08.729994Z","iopub.execute_input":"2024-09-04T16:48:08.730317Z","iopub.status.idle":"2024-09-04T16:48:09.877293Z","shell.execute_reply.started":"2024-09-04T16:48:08.730292Z","shell.execute_reply":"2024-09-04T16:48:09.876487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf \nimport tensorflow.keras as keras\nfrom tensorflow.keras.preprocessing import image_dataset_from_directory","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:48:09.879179Z","iopub.execute_input":"2024-09-04T16:48:09.879538Z","iopub.status.idle":"2024-09-04T16:48:23.379204Z","shell.execute_reply.started":"2024-09-04T16:48:09.879514Z","shell.execute_reply":"2024-09-04T16:48:23.378211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset_dir = '/kaggle/input/gan-getting-started/monet_jpg'\ndatasetmonet = image_dataset_from_directory(\n    dataset_dir,\n    label_mode=None,  # We don't need labels for GANs\n    batch_size=8 # Choose an appropriate batch size\n)\ndataset_dir2 = '/kaggle/input/gan-getting-started/photo_jpg'\ndatasetphoto = image_dataset_from_directory(\n    dataset_dir2,\n    label_mode=None,  # We don't need labels for GANs\n    batch_size=8 # Choose an appropriate batch size\n)\n","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:48:23.380660Z","iopub.execute_input":"2024-09-04T16:48:23.381195Z","iopub.status.idle":"2024-09-04T16:48:37.313870Z","shell.execute_reply.started":"2024-09-04T16:48:23.381169Z","shell.execute_reply":"2024-09-04T16:48:37.313128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"repeats = (7038 + 299) // 300 \ndatasetmonet = datasetmonet.repeat(repeats + 4)\ndatasetphoto = datasetphoto.repeat(2)","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:48:37.314951Z","iopub.execute_input":"2024-09-04T16:48:37.315241Z","iopub.status.idle":"2024-09-04T16:48:37.320356Z","shell.execute_reply.started":"2024-09-04T16:48:37.315217Z","shell.execute_reply":"2024-09-04T16:48:37.319399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def normalize_img(img):\n    img = img / 255.0\n    return img\ndatasetmonet = datasetmonet.map(lambda x: normalize_img(x))\ndatasetphoto = datasetphoto.map(lambda x: normalize_img(x))","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:48:37.322721Z","iopub.execute_input":"2024-09-04T16:48:37.323004Z","iopub.status.idle":"2024-09-04T16:48:37.395494Z","shell.execute_reply.started":"2024-09-04T16:48:37.322979Z","shell.execute_reply":"2024-09-04T16:48:37.394641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def simple_augmentation(image):\n    # Randomly flip the image horizontally\n    image = tf.image.random_flip_left_right(tf.image.random_flip_up_down(image))\n    \n    return image\n\ndef black_box(image):\n    image = tf.image.random_flip_left_right(tf.image.random_flip_up_down(image))\n    image = tf.image.crop_to_bounding_box(image, 0, 0, 150, 150)\n    image = tf.image.resize_with_crop_or_pad(image, 256, 256)\n    \n    return image","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:48:37.396614Z","iopub.execute_input":"2024-09-04T16:48:37.396876Z","iopub.status.idle":"2024-09-04T16:48:37.402533Z","shell.execute_reply.started":"2024-09-04T16:48:37.396855Z","shell.execute_reply":"2024-09-04T16:48:37.401584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"AUTOTUNE = tf.data.experimental.AUTOTUNE","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:48:37.403849Z","iopub.execute_input":"2024-09-04T16:48:37.404268Z","iopub.status.idle":"2024-09-04T16:48:37.410415Z","shell.execute_reply.started":"2024-09-04T16:48:37.404238Z","shell.execute_reply":"2024-09-04T16:48:37.409576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# datasetmonet = datasetmonet.map(simple_augmentation,  num_parallel_calls=AUTOTUNE)\n# datasetphoto = datasetphoto.map(black_box,  num_parallel_calls=AUTOTUNE)","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:48:37.411754Z","iopub.execute_input":"2024-09-04T16:48:37.412040Z","iopub.status.idle":"2024-09-04T16:48:37.418808Z","shell.execute_reply.started":"2024-09-04T16:48:37.412018Z","shell.execute_reply":"2024-09-04T16:48:37.417985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.layers import Input,MaxPooling2D,DepthwiseConv2D,UpSampling2D, Dense, Reshape,Conv2D,Flatten, Conv2DTranspose, BatchNormalization, LeakyReLU, Activation, Dropout, Concatenate\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.layers import Layer","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:48:37.420010Z","iopub.execute_input":"2024-09-04T16:48:37.420634Z","iopub.status.idle":"2024-09-04T16:48:37.431767Z","shell.execute_reply.started":"2024-09-04T16:48:37.420603Z","shell.execute_reply":"2024-09-04T16:48:37.431011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# These are from -> https://openaccess.thecvf.com/content/ICCV2021/papers/Xu_DRB-GAN_A_Dynamic_ResBlock_Generative_Adversarial_Network_for_Artistic_Style_ICCV_2021_paper.pdf\nclass SWLIN(Layer):\n    def __init__(self, window_size=3, **kwargs):\n        super(SWLIN, self).__init__(**kwargs)\n        self.window_size = window_size\n        self.rho = tf.Variable(0.5, trainable=True, dtype=tf.float32)\n\n    def build(self, input_shape):\n        # Create learnable parameters gamma and beta\n        self.gamma = self.add_weight(\n            shape=(input_shape[-1],), initializer=\"ones\", trainable=True\n        )\n        self.beta = self.add_weight(\n            shape=(input_shape[-1],), initializer=\"zeros\", trainable=True\n        )\n\n    def call(self, inputs):\n        # Channel-wise (Instance Normalization)\n        phi_c = spatial_window_normalization(inputs, self.window_size, mode='channel')\n\n        # Layer-wise (Layer Normalization)\n        phi_l = spatial_window_normalization(inputs, self.window_size, mode='layer')\n\n        # Combine them using the learnable parameter rho\n        combined = self.rho * phi_c + (1 - self.rho) * phi_l\n\n        # Apply affine transformation with learnable gamma and beta\n        output = self.gamma * combined + self.beta\n        return output\n\n    def compute_output_shape(self, input_shape):\n        return input_shape\n\ndef spatial_window_normalization(inputs, window_size, mode='channel'):\n    \"\"\"\n    Applies normalization across a spatial window.\n    - mode='channel': performs instance normalization (channel-wise).\n    - mode='layer': performs layer normalization (layer-wise).\n    \"\"\"\n    mean, variance = tf.nn.moments(inputs, axes=[1, 2], keepdims=True)\n    \n    if mode == 'channel':\n        # Channel-wise normalization (like instance normalization)\n        mean, variance = tf.nn.moments(inputs, axes=[1, 2], keepdims=True)\n    elif mode == 'layer':\n        # Layer-wise normalization (like layer normalization)\n        mean, variance = tf.nn.moments(inputs, axes=[1, 2, 3], keepdims=True)\n\n    normalized = (inputs - mean) / tf.sqrt(variance + 1e-5)\n    return normalized","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:48:37.432911Z","iopub.execute_input":"2024-09-04T16:48:37.433225Z","iopub.status.idle":"2024-09-04T16:48:37.444886Z","shell.execute_reply.started":"2024-09-04T16:48:37.433203Z","shell.execute_reply":"2024-09-04T16:48:37.443996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def model_maker():\n    input1 = Input(shape=[256,256,3])\n    input2 = Input(shape=[128, 128, 64])\n    input3 = Input(shape=[32,32, 128])\n    initializer = tf.random_normal_initializer(0., 0.2)\n    # Downsample layers\n    x = DepthwiseConv2D((5, 5), strides=(2, 2), padding='same')(input1)\n    x = Concatenate()([x, input2])\n    x = Conv2D(64, (1, 1),kernel_initializer=initializer, use_bias=False)(x)\n    x = LeakyReLU()(x)\n    x = skip0 = Dropout(0.4)(x)\n    \n    x = DepthwiseConv2D((5, 5), strides=(2, 2), padding='same')(x)\n    x = Conv2D(128, (1, 1),kernel_initializer=initializer, use_bias=False)(x)\n    x = LeakyReLU()(x)\n    x = skip1 =  Dropout(0.3)(x)\n    \n    x = DepthwiseConv2D((5, 5), strides=(2, 2), padding='same')(x)\n    x = Concatenate()([x, input3])\n    x = Conv2D(256, (1, 1),kernel_initializer=initializer, use_bias=False)(x)\n    x = LeakyReLU()(x)\n\n    # Upsample to 16x16\n    \n    x = Conv2DTranspose(128, (5, 5),kernel_initializer=initializer, strides=(2, 2), padding='same', use_bias=False)(x)\n    x = SWLIN(window_size=3)(x)\n    x = LeakyReLU()(x)\n    x = Concatenate()([x, skip1])\n    \n    \n    # Upsample to 32x32\n    x = Conv2DTranspose(64, (5, 5),kernel_initializer=initializer, strides=(2, 2), padding='same', use_bias=False)(x)\n    x = SWLIN(window_size=3)(x)\n    x = LeakyReLU()(x)\n    x = Concatenate()([x, skip0])\n    \n    # Upsample to 64x64\n    x = Conv2DTranspose(32, (5, 5),kernel_initializer=initializer, strides=(2, 2), padding='same', use_bias=False)(x)\n    x = SWLIN(window_size=3)(x)\n    x = LeakyReLU()(x)\n    \n    # Output layer to generate RGB images\n    x = Conv2DTranspose(3, (1, 1),kernel_initializer=initializer, strides=(1, 1),padding='same', use_bias=False)(x)\n    output = Activation('sigmoid')(x)\n    \n    generator = Model(inputs= [input1, input2, input3], outputs = output)\n    \n    return generator","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:48:37.446077Z","iopub.execute_input":"2024-09-04T16:48:37.446350Z","iopub.status.idle":"2024-09-04T16:48:37.462280Z","shell.execute_reply.started":"2024-09-04T16:48:37.446316Z","shell.execute_reply":"2024-09-04T16:48:37.461499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = model_maker()\ntest.summary()","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:48:37.463269Z","iopub.execute_input":"2024-09-04T16:48:37.463532Z","iopub.status.idle":"2024-09-04T16:48:37.651550Z","shell.execute_reply.started":"2024-09-04T16:48:37.463510Z","shell.execute_reply":"2024-09-04T16:48:37.650724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def discriminator_maker():\n    img = Input(shape=(256, 256, 3))  # Adjust input shape for 256x256 RGB images\n    \n    x = Conv2D(64, (5, 5), strides=(2, 2), padding='same')(img)\n    x = LeakyReLU()(x)\n    x = Dropout(0.3)(x)\n    x = MaxPooling2D(pool_size=(2, 2))(x)\n    \n    x = DepthwiseConv2D((5, 5), strides=(2, 2), padding='same')(x)\n    x = Conv2D(128, (1, 1), use_bias=False)(x)\n    x = LeakyReLU()(x)\n    x = Dropout(0.3)(x)\n    x = MaxPooling2D(pool_size=(2, 2))(x)\n    \n    x = DepthwiseConv2D((5, 5), strides=(2, 2), padding='same')(x)\n    x = Conv2D(256, (1, 1), use_bias=False)(x)\n    x = LeakyReLU()(x)\n    x = Dropout(0.3)(x)\n    x = MaxPooling2D(pool_size=(2, 2))(x)\n    \n    x = DepthwiseConv2D((3, 3), strides=(1, 1), padding='same')(x)\n    x = Conv2D(512, (1, 1), use_bias=False)(x)\n    x = LeakyReLU()(x)\n    x = Dropout(0.1)(x)\n    \n    x = DepthwiseConv2D((3, 3), strides=(1, 1), padding='same')(x)\n    x = Conv2D(1024, (1, 1), use_bias=False)(x)\n    x = LeakyReLU()(x)\n    \n    x = Flatten()(x)\n    output = Dense(1, activation='sigmoid')(x)\n\n    discriminator = Model(img, output)\n    return discriminator","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:48:37.652555Z","iopub.execute_input":"2024-09-04T16:48:37.652822Z","iopub.status.idle":"2024-09-04T16:48:37.663556Z","shell.execute_reply.started":"2024-09-04T16:48:37.652800Z","shell.execute_reply":"2024-09-04T16:48:37.662665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"discriminator_maker().summary()","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:48:37.667487Z","iopub.execute_input":"2024-09-04T16:48:37.667981Z","iopub.status.idle":"2024-09-04T16:48:37.798177Z","shell.execute_reply.started":"2024-09-04T16:48:37.667958Z","shell.execute_reply":"2024-09-04T16:48:37.797382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"labels_real = tf.data.Dataset.from_tensor_slices(tf.ones(len(datasetmonet)))\nlabels_fake = tf.data.Dataset.from_tensor_slices(tf.zeros(len(datasetphoto)))\n\n# Combine datasets with their labels\ndatasetreal_with_labels = tf.data.Dataset.zip((datasetmonet, labels_real))\ndatasetfake_with_labels = tf.data.Dataset.zip((datasetphoto, labels_fake))\n\n# Concatenate the datasets\ncombined_dataset = datasetreal_with_labels.concatenate(datasetfake_with_labels)\n# Reshape the labels to match the model's output\ncombined_dataset = combined_dataset.map(lambda x, y: (x, tf.expand_dims(y, axis=-1)))\n\n# Shuffle the dataset to mix real and fake images\ncombined_dataset = combined_dataset.shuffle(buffer_size=len(combined_dataset))\n\n# Apply caching and prefetching for optimized performance\ncombined_dataset = combined_dataset.cache().prefetch(buffer_size=AUTOTUNE)\"\"\"","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:48:37.799123Z","iopub.execute_input":"2024-09-04T16:48:37.799352Z","iopub.status.idle":"2024-09-04T16:48:37.805720Z","shell.execute_reply.started":"2024-09-04T16:48:37.799332Z","shell.execute_reply":"2024-09-04T16:48:37.804887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"feature_encoder = discriminator_maker()\nfeature_encoder.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])\n\nhist = feature_encoder.fit(combined_dataset, epochs=80)\"\"\"","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2024-09-04T16:48:37.806731Z","iopub.execute_input":"2024-09-04T16:48:37.806985Z","iopub.status.idle":"2024-09-04T16:48:37.814458Z","shell.execute_reply.started":"2024-09-04T16:48:37.806961Z","shell.execute_reply":"2024-09-04T16:48:37.813520Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"feature_encoder.save_weights('/kaggle/working/feature_encoder.weights.h5')\"\"\"","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:48:37.815520Z","iopub.execute_input":"2024-09-04T16:48:37.815785Z","iopub.status.idle":"2024-09-04T16:48:37.827141Z","shell.execute_reply.started":"2024-09-04T16:48:37.815763Z","shell.execute_reply":"2024-09-04T16:48:37.826322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Adam is going to be the optimizer for both\nfrom tensorflow.keras.optimizers import Adam\n# Binary cross entropy is going to be the loss for both \nfrom tensorflow.keras.losses import MeanSquaredError","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:48:37.828117Z","iopub.execute_input":"2024-09-04T16:48:37.828374Z","iopub.status.idle":"2024-09-04T16:48:37.836619Z","shell.execute_reply.started":"2024-09-04T16:48:37.828354Z","shell.execute_reply":"2024-09-04T16:48:37.835823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def gram_matrix(tensor):\n    shape = tensor.get_shape()\n    \n    # Get the number of feature channels for the input tensor,\n    # which is assumed to be from a convolutional layer with 4-dim.\n    num_channels = int(shape[3])\n\n    # Reshape the tensor so it is a 2-dim matrix. This essentially\n    # flattens the contents of each feature-channel.\n    matrix = tf.reshape(tensor, shape=[-1, num_channels])\n    \n    # Calculate the Gram-matrix as the matrix-product of\n    # the 2-dim matrix with itself. This calculates the\n    # dot-products of all combinations of the feature-channels.\n    gram = tf.matmul(tf.transpose(matrix), matrix)\n    \n    height = tf.cast(shape[1], tf.float32)\n    width = tf.cast(shape[2], tf.float32)\n    num_elements = height * width\n    gram /= num_elements\n    \n    return gram    ","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:48:37.837556Z","iopub.execute_input":"2024-09-04T16:48:37.837802Z","iopub.status.idle":"2024-09-04T16:48:37.844298Z","shell.execute_reply.started":"2024-09-04T16:48:37.837781Z","shell.execute_reply":"2024-09-04T16:48:37.843446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ng_opt = Adam(learning_rate=0.005) \ng_loss = MeanSquaredError()\nd_opt = Adam(learning_rate=0.005) \nd_loss = MeanSquaredError()\n","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:48:37.845193Z","iopub.execute_input":"2024-09-04T16:48:37.845461Z","iopub.status.idle":"2024-09-04T16:48:37.857653Z","shell.execute_reply.started":"2024-09-04T16:48:37.845423Z","shell.execute_reply":"2024-09-04T16:48:37.856950Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class GanModel(Model):\n    def __init__(self,encoder ,generator, discriminator, *args, **kwargs):\n        super().__init__(*args, **kwargs)\n        self.encoder_monet_3 =  Model(inputs=encoder.input, outputs=encoder.get_layer(index=9).output)\n        self.encoder_monet_1 =  Model(inputs=encoder.input, outputs=encoder.get_layer(index=6).output)\n        # Feature extraction for generation part\n        self.encoder_monet_2 =  Model(inputs=encoder.input, outputs=encoder.get_layer(index=2).output)\n        self.generator = generator\n        self.discriminator = discriminator\n        \n    def compile(self, g_opt, g_loss, d_opt, d_loss, *args, **kwargs): \n        super().compile(*args, **kwargs)\n        self.g_opt = g_opt\n        self.g_loss = g_loss\n        self.d_opt = d_opt\n        self.d_loss = d_loss\n        \n    def train_step(self, combined):\n        batch1 , batch2 = combined\n        real_images = batch1\n        \n        feature_input = self.encoder_monet_2(batch1, training= False)\n        feature_input1 = self.encoder_monet_1(batch1, training= False)\n        # generated_imgs = self.generator([batch2,feature_input], training=False)\n        \n        \"\"\"\n        with tf.GradientTape() as d_tape: \n            \n            yhat_real = self.discriminator(batch1, training=True) \n            yhat_fake = self.discriminator(generated_imgs, training=True)\n            yhat_realfake = tf.concat([yhat_real, yhat_fake], axis=0)\n            \n            \n            y_realfake = tf.concat([tf.zeros_like(yhat_real), tf.ones_like(yhat_fake)], axis=0)\n            \n            \n            noise_real = 0.05*tf.random.uniform(tf.shape(yhat_real))\n            noise_fake = -0.05*tf.random.uniform(tf.shape(yhat_fake))\n            y_realfake += tf.concat([noise_real, noise_fake], axis=0)\n            \n            \n            total_d_loss = self.d_loss(y_realfake, yhat_realfake)\n            \n        dgrad = d_tape.gradient(total_d_loss, self.discriminator.trainable_variables) \n        self.d_opt.apply_gradients(zip(dgrad, self.discriminator.trainable_variables))\n        \"Discriminator_loss\": total_d_loss,\n        \"\"\"\n        \n        with tf.GradientTape() as g_tape: \n            gen_images = self.generator([batch2,feature_input, feature_input1], training=True)\n            \n            # real_t = self.discriminator(batch1, training=False)\n            # gen_t = self.discriminator(gen_images, training=False)\n            # total_g_loss += tf.reduce_mean(tf.square(real_t - gen_t))\n            feature_gen_m_2 = self.encoder_monet_2(gen_images, training=False)\n            feature_monet_3 = self.encoder_monet_3(batch1, training= False)\n            feature_gen_m_3 = self.encoder_monet_3(gen_images, training=False)\n            \n            gram_2_gen = gram_matrix(feature_gen_m_2)\n            gram_2_m = gram_matrix(feature_input)\n            gram_3_m = gram_matrix(feature_monet_3)\n            gram_3_gen = gram_matrix(feature_gen_m_3)\n            \n            total_g_loss = ( 1 * tf.reduce_mean(tf.square(gram_2_m - gram_2_gen)) + 3 * tf.reduce_mean(tf.square(gram_3_m - gram_3_gen)))\n\n        \n        ggrad = g_tape.gradient(total_g_loss, self.generator.trainable_variables)\n        self.g_opt.apply_gradients(zip(ggrad, self.generator.trainable_variables))\n        \n        return {\"Generator_loss\":total_g_loss}","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:48:37.858676Z","iopub.execute_input":"2024-09-04T16:48:37.859025Z","iopub.status.idle":"2024-09-04T16:48:37.874746Z","shell.execute_reply.started":"2024-09-04T16:48:37.858993Z","shell.execute_reply":"2024-09-04T16:48:37.873976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def filter_batches(datasetreal, datasetfake, batch_size):\n    for r, m in zip(datasetreal, datasetfake):\n        # Check if both batches have the desired number of samples\n        if r.shape[0] == batch_size and m.shape[0] == batch_size:\n            yield r, m  # Yield the batches if they meet the condition","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:48:37.875787Z","iopub.execute_input":"2024-09-04T16:48:37.877873Z","iopub.status.idle":"2024-09-04T16:48:37.883307Z","shell.execute_reply.started":"2024-09-04T16:48:37.877849Z","shell.execute_reply":"2024-09-04T16:48:37.882510Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 8\nfiltered_dataset = tf.data.Dataset.from_generator(\n    lambda: filter_batches(datasetmonet, datasetphoto, batch_size),\n    output_signature=(\n        tf.TensorSpec(shape=(batch_size, *datasetphoto.element_spec.shape[1:]), dtype=datasetphoto.element_spec.dtype),\n        tf.TensorSpec(shape=(batch_size, *datasetmonet.element_spec.shape[1:]), dtype=datasetmonet.element_spec.dtype)\n    )\n)\n\nfiltered_dataset = filtered_dataset.cache().prefetch(buffer_size=AUTOTUNE)","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:48:37.884347Z","iopub.execute_input":"2024-09-04T16:48:37.884874Z","iopub.status.idle":"2024-09-04T16:48:37.928409Z","shell.execute_reply.started":"2024-09-04T16:48:37.884844Z","shell.execute_reply":"2024-09-04T16:48:37.927709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ngenerator = model_maker()\nencoder = discriminator_maker()\ndiscriminator = discriminator_maker()\nencoder.load_weights('/kaggle/input/feature_encoder_monetgan-turbo/tensorflow2/default/3/feature_encoder.weights.h5')\n","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:48:37.929362Z","iopub.execute_input":"2024-09-04T16:48:37.929629Z","iopub.status.idle":"2024-09-04T16:48:38.304736Z","shell.execute_reply.started":"2024-09-04T16:48:37.929606Z","shell.execute_reply":"2024-09-04T16:48:38.303970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_gan = GanModel(encoder, generator, discriminator)","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:48:38.305905Z","iopub.execute_input":"2024-09-04T16:48:38.306349Z","iopub.status.idle":"2024-09-04T16:48:38.318699Z","shell.execute_reply.started":"2024-09-04T16:48:38.306313Z","shell.execute_reply":"2024-09-04T16:48:38.317830Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_gan.compile(g_opt, g_loss, d_opt, d_loss)","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:48:38.319878Z","iopub.execute_input":"2024-09-04T16:48:38.320204Z","iopub.status.idle":"2024-09-04T16:48:38.335824Z","shell.execute_reply.started":"2024-09-04T16:48:38.320182Z","shell.execute_reply":"2024-09-04T16:48:38.334991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nfrom tensorflow.keras.preprocessing.image import array_to_img\nfrom tensorflow.keras.callbacks import Callback","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:48:38.336870Z","iopub.execute_input":"2024-09-04T16:48:38.337150Z","iopub.status.idle":"2024-09-04T16:48:38.343872Z","shell.execute_reply.started":"2024-09-04T16:48:38.337129Z","shell.execute_reply":"2024-09-04T16:48:38.343160Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:48:38.344902Z","iopub.execute_input":"2024-09-04T16:48:38.345168Z","iopub.status.idle":"2024-09-04T16:48:38.349963Z","shell.execute_reply.started":"2024-09-04T16:48:38.345146Z","shell.execute_reply":"2024-09-04T16:48:38.349124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class ModelMonitor(Callback):\n    def __init__(self, num_img=1,inputs=None, output_dir='/kaggle/working/images'):\n        self.num_img = num_img\n        self.inputs = inputs\n        self.output_dir = output_dir\n        os.makedirs(self.output_dir, exist_ok=True)  # Ensure the directory exists\n\n    def on_epoch_end(self, epoch, logs=None):\n        monet , images = next(iter(self.inputs))\n        batch_m = monet[0]\n        batch_m = tf.expand_dims(batch_m, axis=0)\n        features = self.model.encoder_monet_2(batch_m, training= False)\n        features1 = self.model.encoder_monet_1(batch_m, training= False)\n        img1 = images[0]\n        img = tf.expand_dims(img1, axis=0)\n        # Generate images from noise\n        generated_images = self.model.generator([img,features, features1 ], training=False)\n        \n        # Rescale images from [0, 1] to [0, 255]\n        generated_images = generated_images * 255\n        generated_images = tf.cast(generated_images, tf.uint8)  # Convert to uint8 for saving\n        \n        # Save images\n        # print(generated_images.shape)\n        img = array_to_img(generated_images[0])\n        img.save(os.path.join(self.output_dir, f'generated_img_{epoch}.jpg'))\n","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:48:38.351011Z","iopub.execute_input":"2024-09-04T16:48:38.351356Z","iopub.status.idle":"2024-09-04T16:48:38.361136Z","shell.execute_reply.started":"2024-09-04T16:48:38.351327Z","shell.execute_reply":"2024-09-04T16:48:38.360356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# history = test_gan.fit(filtered_dataset, epochs=50, callbacks=[ModelMonitor(inputs = filtered_dataset)])","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2024-09-04T16:48:38.362463Z","iopub.execute_input":"2024-09-04T16:48:38.363016Z","iopub.status.idle":"2024-09-04T17:24:41.847821Z","shell.execute_reply.started":"2024-09-04T16:48:38.362986Z","shell.execute_reply":"2024-09-04T17:24:41.846821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Save only the weights\n# generator.save_weights('/kaggle/working/gen.weights.h5')","metadata":{"execution":{"iopub.status.busy":"2024-09-04T17:24:41.849170Z","iopub.execute_input":"2024-09-04T17:24:41.849456Z","iopub.status.idle":"2024-09-04T17:24:41.896410Z","shell.execute_reply.started":"2024-09-04T17:24:41.849430Z","shell.execute_reply":"2024-09-04T17:24:41.895466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Submission Part**","metadata":{}},{"cell_type":"code","source":"import PIL\n\nos.makedirs('/kaggle/tmp', exist_ok=True)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Code for generating Images 7000 at least \n\ngenerator = model_maker()\nencoder = discriminator_maker()\n\nencoder.load_weights('/kaggle/input/feature_encoder_monetgan-turbo/tensorflow2/default/3/feature_encoder.weights.h5')\ngenerator.load_weights(\"/kaggle/input/monet_gan_turbo_weights/tensorflow2/default/17/gen.weights.h5\")\nfeature_get = Model(inputs=encoder.input, outputs=encoder.get_layer(index=2).output)\nfeature_get1 = Model(inputs=encoder.input, outputs=encoder.get_layer(index=6).output)\ni = 0\nfor batch in filtered_dataset:\n        i += 1\n        if i > 1000: \n            break\n        monet , images = batch\n        batch_m = monet\n        # batch_m = tf.expand_dims(batch_m, axis=0)\n        features = feature_get(batch_m, training= False)\n        features1 = feature_get1(batch_m, training= False)\n        img1 = images\n        # img = tf.expand_dims(img1, axis=0)\n        print(i)\n        generated_images = generator([img1,features,features1 ], training=False)\n\n        # Rescale images from [0, 1] to [0, 255]\n        generated_images = generated_images * 255\n        generated_images = tf.cast(generated_images, tf.uint8)  # Convert to uint8 for saving\n\n        # Save images\n        # print(generated_images.shape)\n        gen = 0\n        for gen_img in generated_images:\n            gen += 1\n            img = array_to_img(gen_img)\n            img.save(os.path.join('/kaggle/tmp', f'generated_img_{i}_{gen}.jpg'))\n","metadata":{"scrolled":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nimport shutil\nshutil.make_archive(\"/kaggle/working/images\", 'zip', '/kaggle/tmp')\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}