{"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":"gpu","dataSources":[{"sourceId":1176357,"sourceType":"datasetVersion","datasetId":667852}],"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import tensorflow as tf\ndevices = tf.config.list_physical_devices('GPU')\nfor device in devices:\n    tf.config.experimental.set_memory_growth(device, True)\n\n# try:\n#     tpu = tf.distribute.cluster_resolver.TPUClusterResolver() \n#     print('Running on TPU ', tpu.master())\n# except ValueError:\n#     tpu = None\n\n# if tpu:\n#     tf.config.experimental_connect_to_cluster(tpu)\n#     tf.tpu.experimental.initialize_tpu_system(tpu)\n#     tpu_strategy = tf.distribute.experimental.TPUStrategy(tpu)\n# else:\n#     tpu_strategy = tf.distribute.get_strategy() \n\n# print(\"REPLICAS: \", tpu_strategy.num_replicas_in_sync)\n\n# instantiating the model in the strategy scope creates the model on the TPU\n\n\n\n# train model normally\n","metadata":{"execution":{"iopub.status.busy":"2024-07-05T11:26:53.939114Z","iopub.execute_input":"2024-07-05T11:26:53.939802Z","iopub.status.idle":"2024-07-05T11:26:57.691283Z","shell.execute_reply.started":"2024-07-05T11:26:53.939767Z","shell.execute_reply":"2024-07-05T11:26:57.690397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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\n\nimport os\n\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-07-05T11:26:57.692885Z","iopub.execute_input":"2024-07-05T11:26:57.693459Z","iopub.status.idle":"2024-07-05T11:26:57.698758Z","shell.execute_reply.started":"2024-07-05T11:26:57.693428Z","shell.execute_reply":"2024-07-05T11:26:57.697752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import glob\ncat_fold = os.path.join('/kaggle/input/animal-faces/afhq/train/', 'cat/')\ncats = glob.glob(f'{cat_fold}*.jpg')","metadata":{"execution":{"iopub.status.busy":"2024-07-05T11:26:57.699816Z","iopub.execute_input":"2024-07-05T11:26:57.700059Z","iopub.status.idle":"2024-07-05T11:26:57.736770Z","shell.execute_reply.started":"2024-07-05T11:26:57.700037Z","shell.execute_reply":"2024-07-05T11:26:57.736071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2 as cv\nimport matplotlib.pyplot as plt\nds = []\nfor path in cats[:1000]:\n    img = cv.imread(path)\n    img = cv.resize(img, (256, 256))\n    img = img/255\n    img = np.array(img)\n    ds.append(img)\nds = np.array(ds)","metadata":{"execution":{"iopub.status.busy":"2024-07-05T11:26:57.739158Z","iopub.execute_input":"2024-07-05T11:26:57.739810Z","iopub.status.idle":"2024-07-05T11:27:02.384075Z","shell.execute_reply.started":"2024-07-05T11:26:57.739782Z","shell.execute_reply":"2024-07-05T11:27:02.383188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dset = tf.data.Dataset.from_tensor_slices(ds).shuffle(1000)\n","metadata":{"execution":{"iopub.status.busy":"2024-07-05T11:27:02.385296Z","iopub.execute_input":"2024-07-05T11:27:02.385672Z","iopub.status.idle":"2024-07-05T11:27:05.992653Z","shell.execute_reply.started":"2024-07-05T11:27:02.385638Z","shell.execute_reply":"2024-07-05T11:27:05.991803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print(dset.shape)\nplt.imshow(dset.as_numpy_iterator().next())\nplt.axis('off')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-07-05T11:27:05.993737Z","iopub.execute_input":"2024-07-05T11:27:05.994008Z","iopub.status.idle":"2024-07-05T11:27:07.385527Z","shell.execute_reply.started":"2024-07-05T11:27:05.993984Z","shell.execute_reply":"2024-07-05T11:27:07.384646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dset = dset.batch(64)\ndset = dset.prefetch(32)","metadata":{"execution":{"iopub.status.busy":"2024-07-05T11:27:07.386588Z","iopub.execute_input":"2024-07-05T11:27:07.386871Z","iopub.status.idle":"2024-07-05T11:27:07.395887Z","shell.execute_reply.started":"2024-07-05T11:27:07.386847Z","shell.execute_reply":"2024-07-05T11:27:07.395061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del ds, cats\nimport gc\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2024-07-05T11:27:07.396902Z","iopub.execute_input":"2024-07-05T11:27:07.397158Z","iopub.status.idle":"2024-07-05T11:27:07.593997Z","shell.execute_reply.started":"2024-07-05T11:27:07.397128Z","shell.execute_reply":"2024-07-05T11:27:07.593048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow.keras as keras\nfrom tensorflow.keras import Model\nfrom tensorflow.keras.layers import Input, Conv2D, Conv2DTranspose, MaxPooling2D, Dense, Flatten, Reshape, LeakyReLU, BatchNormalization, AveragePooling2D, Dropout","metadata":{"execution":{"iopub.status.busy":"2024-07-05T11:27:07.595200Z","iopub.execute_input":"2024-07-05T11:27:07.595510Z","iopub.status.idle":"2024-07-05T11:27:07.615479Z","shell.execute_reply.started":"2024-07-05T11:27:07.595484Z","shell.execute_reply":"2024-07-05T11:27:07.614485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def gen():\n    inputs = Input((4,4))\n    x = Flatten()(inputs)\n    x = Dense(64)(x)\n    x = LeakyReLU(0.2)(x)\n    x = Reshape((8,8,1))(x)\n    x = Conv2DTranspose(64, (3,3), strides=(2,2), padding='same')(x)\n    x = BatchNormalization()(x)\n    x = LeakyReLU(0.2)(x)\n    \n    x = Conv2DTranspose(128, (3,3), strides=(2,2), padding='same')(x)\n    x = LeakyReLU(0.2)(x)\n    x = BatchNormalization()(x)\n    x = Conv2D(128, (3,3), padding='same')(x)\n    x = LeakyReLU(0.2)(x)\n    x = BatchNormalization()(x)\n    x = Conv2DTranspose(64, (3,3), strides=(2,2), padding='same')(x)\n    x = LeakyReLU(0.2)(x)\n    x = BatchNormalization()(x)\n    \n    x = Conv2DTranspose(16, (3,3), strides=(2,2), padding='same')(x)\n    x = LeakyReLU(0.2)(x)\n    x = BatchNormalization()(x)\n\n    \n    outputs = Conv2DTranspose(3, (3,3), strides=(2,2), padding='same', activation='sigmoid')(x)\n    model = Model(inputs=inputs, outputs=outputs)\n    return model","metadata":{"execution":{"iopub.status.busy":"2024-07-05T11:27:07.619837Z","iopub.execute_input":"2024-07-05T11:27:07.620213Z","iopub.status.idle":"2024-07-05T11:27:07.632479Z","shell.execute_reply.started":"2024-07-05T11:27:07.620187Z","shell.execute_reply":"2024-07-05T11:27:07.631006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"generator = gen()\ngenerator.summary()","metadata":{"execution":{"iopub.status.busy":"2024-07-05T11:27:07.634053Z","iopub.execute_input":"2024-07-05T11:27:07.634619Z","iopub.status.idle":"2024-07-05T11:27:07.809704Z","shell.execute_reply.started":"2024-07-05T11:27:07.634585Z","shell.execute_reply":"2024-07-05T11:27:07.808714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def dis():\n    inputs = Input((256,256,3))\n   \n    x = Conv2D(32, (3,3), padding='same')(inputs)\n    x = LeakyReLU(0.2)(x)\n    x = Dropout(0.2)(x)\n    x = MaxPooling2D()(x)\n    x = AveragePooling2D((2,2), strides=(2,2))(x)\n\n    x = Dropout(0.2)(x)\n    x = Conv2D(64, (3,3), padding='same')(x)\n    x = LeakyReLU(0.2)(x)\n    x = MaxPooling2D()(x)\n    x = Dropout(0.2)(x)\n    x = Flatten()(x)\n    x = Dense(32)(x)\n    x = LeakyReLU(0.2)(x)\n    \n    x = Dense(8)(x)\n    x = LeakyReLU(0.2)(x)\n    outputs = Dense(1, activation='sigmoid')(x)\n    model = Model(inputs=inputs, outputs=outputs)\n    return model","metadata":{"execution":{"iopub.status.busy":"2024-07-05T11:27:07.810866Z","iopub.execute_input":"2024-07-05T11:27:07.811154Z","iopub.status.idle":"2024-07-05T11:27:07.819130Z","shell.execute_reply.started":"2024-07-05T11:27:07.811128Z","shell.execute_reply":"2024-07-05T11:27:07.818137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"discriminator = dis()\ndiscriminator.summary()","metadata":{"execution":{"iopub.status.busy":"2024-07-05T11:27:07.820418Z","iopub.execute_input":"2024-07-05T11:27:07.820787Z","iopub.status.idle":"2024-07-05T11:27:07.917769Z","shell.execute_reply.started":"2024-07-05T11:27:07.820760Z","shell.execute_reply":"2024-07-05T11:27:07.916910Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"g_opt = keras.optimizers.Adam(learning_rate=1e-3)\nd_opt = keras.optimizers.SGD(learning_rate=3e-3)\ng_loss = keras.losses.BinaryCrossentropy()\nd_loss = keras.losses.BinaryCrossentropy()","metadata":{"execution":{"iopub.status.busy":"2024-07-05T11:27:07.918859Z","iopub.execute_input":"2024-07-05T11:27:07.919127Z","iopub.status.idle":"2024-07-05T11:27:07.932331Z","shell.execute_reply.started":"2024-07-05T11:27:07.919103Z","shell.execute_reply":"2024-07-05T11:27:07.931440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class GAN(Model):\n    def __init__(self, generator, discriminator, *args, **kwargs):\n        super().__init__(*args, **kwargs)\n        self.generator = generator\n        self.discriminator = discriminator\n    def compile(self, g_opt, d_opt, g_loss, d_loss, *args, **kwargs):\n        super().compile(*args, **kwargs)\n        self.g_opt = g_opt\n        self.d_opt = d_opt\n        self.g_loss = g_loss\n        self.d_loss = d_loss\n    def train_step(self, real_imgs):\n       \n        fake_imgs = self.generator(tf.random.normal((64,4,4)), training = False)      \n        with tf.GradientTape() as d_tape:\n           \n            yhat_real = self.discriminator(real_imgs, training=True)\n            yhat_fake = self.discriminator(fake_imgs, training=True)\n            yhat = tf.concat([yhat_real, yhat_fake], axis=0)\n            \n            y = tf.concat([tf.zeros_like(yhat_real), tf.ones_like(yhat_fake)], axis=0)\n            \n            noise_real = 0.15*tf.random.uniform(tf.shape(yhat_real))\n            noise_fake = -0.15*tf.random.uniform(tf.shape(yhat_fake))\n            y += tf.concat([noise_real, noise_fake], axis=0)\n            \n            total_d_loss = self.d_loss(y, yhat)\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            \n        with tf.GradientTape() as g_tape:\n            gen_images = self.generator(tf.random.normal((64,4,4)), training=True)\n            pred = self.discriminator(gen_images, training=False)\n            \n            total_g_loss = self.g_loss(tf.zeros_like(pred), pred)\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 {'g_loss':total_g_loss, 'd_loss':total_d_loss}","metadata":{"execution":{"iopub.status.busy":"2024-07-05T11:27:07.933557Z","iopub.execute_input":"2024-07-05T11:27:07.933899Z","iopub.status.idle":"2024-07-05T11:27:07.947375Z","shell.execute_reply.started":"2024-07-05T11:27:07.933867Z","shell.execute_reply":"2024-07-05T11:27:07.946638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# with tpu_strategy.scope():\ngan = GAN(generator, discriminator)\ngan.compile(g_opt, d_opt, g_loss, d_loss)","metadata":{"execution":{"iopub.status.busy":"2024-07-05T11:27:07.948660Z","iopub.execute_input":"2024-07-05T11:27:07.949083Z","iopub.status.idle":"2024-07-05T11:27:07.968779Z","shell.execute_reply.started":"2024-07-05T11:27:07.949050Z","shell.execute_reply":"2024-07-05T11:27:07.967891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.callbacks import Callback\nclass ModelMonitor(Callback):\n    def __init__(self, num_img=1, latent_dim=4):\n        self.num_img = num_img\n        self.latent_dim = latent_dim\n\n    def on_epoch_end(self, epoch, logs=None):\n        if epoch == 1500:\n            g_opt = keras.optimizers.Adam(learning_rate=3e-5)\n            d_opt = keras.optimizers.Adam(learning_rate=1e-4)\n        if (epoch+1)%5==0:\n            random_latent_vectors = tf.random.uniform((self.num_img, 4,4))\n            generated_images = self.model.generator(random_latent_vectors)\n            generated_images *= 255\n            generated_images.numpy()\n            plt.imshow(generated_images[0])\n            plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-07-05T11:27:07.970037Z","iopub.execute_input":"2024-07-05T11:27:07.970383Z","iopub.status.idle":"2024-07-05T11:27:07.978022Z","shell.execute_reply.started":"2024-07-05T11:27:07.970353Z","shell.execute_reply":"2024-07-05T11:27:07.977223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"call = [ModelMonitor()]","metadata":{"execution":{"iopub.status.busy":"2024-07-05T11:27:07.979066Z","iopub.execute_input":"2024-07-05T11:27:07.979883Z","iopub.status.idle":"2024-07-05T11:27:07.990874Z","shell.execute_reply.started":"2024-07-05T11:27:07.979849Z","shell.execute_reply":"2024-07-05T11:27:07.989975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gc.collect()","metadata":{"execution":{"iopub.status.busy":"2024-07-05T11:27:07.993083Z","iopub.execute_input":"2024-07-05T11:27:07.993523Z","iopub.status.idle":"2024-07-05T11:27:08.178887Z","shell.execute_reply.started":"2024-07-05T11:27:07.993498Z","shell.execute_reply":"2024-07-05T11:27:08.177845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hist = gan.fit(dset, epochs=2000, callbacks=call, shuffle=True)","metadata":{"execution":{"iopub.status.busy":"2024-07-05T11:27:08.180597Z","iopub.execute_input":"2024-07-05T11:27:08.181051Z","iopub.status.idle":"2024-07-05T13:02:28.182161Z","shell.execute_reply.started":"2024-07-05T11:27:08.181015Z","shell.execute_reply":"2024-07-05T13:02:28.181088Z"},"scrolled":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hist.history.keys()","metadata":{"execution":{"iopub.status.busy":"2024-07-05T13:03:05.203033Z","iopub.execute_input":"2024-07-05T13:03:05.203737Z","iopub.status.idle":"2024-07-05T13:03:05.210118Z","shell.execute_reply.started":"2024-07-05T13:03:05.203700Z","shell.execute_reply":"2024-07-05T13:03:05.209065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\nsns.lineplot(data=hist.history['d_loss'], label='d_loss')\nsns.lineplot(data=hist.history['g_loss'], label='g_loss')","metadata":{"execution":{"iopub.status.busy":"2024-07-05T13:04:32.015509Z","iopub.execute_input":"2024-07-05T13:04:32.016128Z","iopub.status.idle":"2024-07-05T13:04:32.325764Z","shell.execute_reply.started":"2024-07-05T13:04:32.016092Z","shell.execute_reply":"2024-07-05T13:04:32.324793Z"},"trusted":true},"execution_count":null,"outputs":[]}]}