{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30805,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# import Liberys\nimport glob\nimport imageio\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport os\nimport PIL\nimport tensorflow as tf\nfrom tensorflow.keras import layers\nimport time\nfrom IPython.display import clear_output","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-24T11:02:32.397932Z","iopub.execute_input":"2024-12-24T11:02:32.398560Z","iopub.status.idle":"2024-12-24T11:02:44.181011Z","shell.execute_reply.started":"2024-12-24T11:02:32.398528Z","shell.execute_reply":"2024-12-24T11:02:44.180203Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# data loading\ndata = np.loadtxt('/kaggle/input/digit-recognizer/train.csv',delimiter=',',skiprows=1,)\n# removed label column\ndata = data[:,1:]\n# reshape data\ndata = data.reshape(-1,28,28,1)\ndata.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-24T11:02:44.182355Z","iopub.execute_input":"2024-12-24T11:02:44.182868Z","iopub.status.idle":"2024-12-24T11:02:46.571018Z","shell.execute_reply.started":"2024-12-24T11:02:44.182838Z","shell.execute_reply":"2024-12-24T11:02:46.570068Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(data[0])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-24T11:02:46.571971Z","iopub.execute_input":"2024-12-24T11:02:46.572223Z","iopub.status.idle":"2024-12-24T11:02:46.817833Z","shell.execute_reply.started":"2024-12-24T11:02:46.572197Z","shell.execute_reply":"2024-12-24T11:02:46.816925Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"BUFFER_SIZE = 42000\nBATCH_SIZE = 32\n\n# create dataset\ndataset = tf.data.Dataset.from_tensor_slices((data))\ndataset = dataset.batch(BATCH_SIZE).shuffle(buffer_size=BUFFER_SIZE)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-24T11:02:46.819884Z","iopub.execute_input":"2024-12-24T11:02:46.820476Z","iopub.status.idle":"2024-12-24T11:02:48.171307Z","shell.execute_reply.started":"2024-12-24T11:02:46.820430Z","shell.execute_reply":"2024-12-24T11:02:48.170593Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def normalize_image(image):\n    # Normalize the image to [-1, 1] for tanh activation\n    image = (image / 127.5) - 1  # If the pixel values are in [0, 255]\n    # Alternatively, if the image data is in [0, 1] range, use:\n    # image = 2 * image - 1\n    \n    return image","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-24T11:02:48.172259Z","iopub.execute_input":"2024-12-24T11:02:48.172537Z","iopub.status.idle":"2024-12-24T11:02:48.176987Z","shell.execute_reply.started":"2024-12-24T11:02:48.172509Z","shell.execute_reply":"2024-12-24T11:02:48.176018Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dataset = dataset.map(normalize_image)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-24T11:02:48.177971Z","iopub.execute_input":"2024-12-24T11:02:48.178314Z","iopub.status.idle":"2024-12-24T11:02:48.224548Z","shell.execute_reply.started":"2024-12-24T11:02:48.178265Z","shell.execute_reply":"2024-12-24T11:02:48.223959Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for image_batch in dataset:\n    print(image_batch.shape)\n    plt.imshow(image_batch[0])\n    break","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-24T11:03:16.977326Z","iopub.execute_input":"2024-12-24T11:03:16.978002Z","iopub.status.idle":"2024-12-24T11:03:17.468240Z","shell.execute_reply.started":"2024-12-24T11:03:16.977966Z","shell.execute_reply":"2024-12-24T11:03:17.467375Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Generator function\ndef make_generator_model():\n    model = tf.keras.Sequential()\n    model.add(layers.Dense(7*7*256, use_bias=False, input_shape=(100,)))\n    model.add(layers.BatchNormalization())\n    model.add(layers.LeakyReLU())\n\n    model.add(layers.Reshape((7, 7, 256)))\n    assert model.output_shape == (None, 7, 7, 256)  # Note: None is the batch size\n\n    model.add(layers.Conv2DTranspose(128, (5, 5), strides=(1, 1), padding='same', use_bias=False))\n    assert model.output_shape == (None, 7, 7, 128)\n    model.add(layers.BatchNormalization())\n    model.add(layers.LeakyReLU())\n\n    model.add(layers.Conv2DTranspose(64, (5, 5), strides=(2, 2), padding='same', use_bias=False))\n    assert model.output_shape == (None, 14, 14, 64)\n    model.add(layers.BatchNormalization())\n    model.add(layers.LeakyReLU())\n\n    model.add(layers.Conv2DTranspose(1, (5, 5), strides=(2, 2), padding='same', use_bias=False, activation='tanh'))\n    assert model.output_shape == (None, 28, 28, 1)\n\n    return model","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-24T11:03:21.565513Z","iopub.execute_input":"2024-12-24T11:03:21.566430Z","iopub.status.idle":"2024-12-24T11:03:21.573309Z","shell.execute_reply.started":"2024-12-24T11:03:21.566392Z","shell.execute_reply":"2024-12-24T11:03:21.572415Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# create generator\ngenerator = make_generator_model()\n\n# create normal distribution data points\nnoise = tf.random.normal([1, 100])\ngenerated_image = generator(noise, training=False)\n\nplt.imshow(generated_image[0, :, :, 0])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-24T11:03:24.271973Z","iopub.execute_input":"2024-12-24T11:03:24.272784Z","iopub.status.idle":"2024-12-24T11:03:25.716798Z","shell.execute_reply.started":"2024-12-24T11:03:24.272749Z","shell.execute_reply":"2024-12-24T11:03:25.715954Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# discriminator function\ndef make_discriminator_model():\n    model = tf.keras.Sequential()\n    model.add(layers.Conv2D(64, (5, 5), strides=(2, 2), padding='same',\n                                     input_shape=[28, 28, 1]))\n    model.add(layers.LeakyReLU())\n    model.add(layers.Dropout(0.3))\n\n    model.add(layers.Conv2D(128, (5, 5), strides=(2, 2), padding='same'))\n    model.add(layers.LeakyReLU())\n    model.add(layers.Dropout(0.3))\n\n    model.add(layers.Flatten())\n    model.add(layers.Dense(1))\n\n    return model","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-24T11:03:27.006235Z","iopub.execute_input":"2024-12-24T11:03:27.006923Z","iopub.status.idle":"2024-12-24T11:03:27.012360Z","shell.execute_reply.started":"2024-12-24T11:03:27.006886Z","shell.execute_reply":"2024-12-24T11:03:27.011495Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# create discriminator\ndiscriminator = make_discriminator_model()\ndiscriminator.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-24T11:03:30.542615Z","iopub.execute_input":"2024-12-24T11:03:30.543447Z","iopub.status.idle":"2024-12-24T11:03:30.614615Z","shell.execute_reply.started":"2024-12-24T11:03:30.543415Z","shell.execute_reply":"2024-12-24T11:03:30.613698Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# This method returns a helper function to compute cross entropy loss\ncross_entropy = tf.keras.losses.BinaryCrossentropy(from_logits=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-24T11:03:32.622152Z","iopub.execute_input":"2024-12-24T11:03:32.622990Z","iopub.status.idle":"2024-12-24T11:03:32.627072Z","shell.execute_reply.started":"2024-12-24T11:03:32.622952Z","shell.execute_reply":"2024-12-24T11:03:32.625936Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# discriminator_loss function\ndef discriminator_loss(real_output, fake_output):\n    real_loss = cross_entropy(tf.ones_like(real_output), real_output)\n    fake_loss = cross_entropy(tf.zeros_like(fake_output), fake_output)\n    total_loss = real_loss + fake_loss\n    return total_loss","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-24T11:03:36.783695Z","iopub.execute_input":"2024-12-24T11:03:36.784515Z","iopub.status.idle":"2024-12-24T11:03:36.788835Z","shell.execute_reply.started":"2024-12-24T11:03:36.784482Z","shell.execute_reply":"2024-12-24T11:03:36.787866Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# generator_loss function\ndef generator_loss(fake_output):\n    return cross_entropy(tf.ones_like(fake_output), fake_output)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-24T11:03:38.203261Z","iopub.execute_input":"2024-12-24T11:03:38.203613Z","iopub.status.idle":"2024-12-24T11:03:38.207977Z","shell.execute_reply.started":"2024-12-24T11:03:38.203581Z","shell.execute_reply":"2024-12-24T11:03:38.207015Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Oprimizer adam\ngenerator_optimizer = tf.keras.optimizers.Adam(1e-4)\ndiscriminator_optimizer = tf.keras.optimizers.Adam(1e-4)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-24T11:03:39.168909Z","iopub.execute_input":"2024-12-24T11:03:39.169763Z","iopub.status.idle":"2024-12-24T11:03:39.180989Z","shell.execute_reply.started":"2024-12-24T11:03:39.169726Z","shell.execute_reply":"2024-12-24T11:03:39.180106Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# saving checkpoints\ncheckpoint_dir = './training_checkpoints'\ncheckpoint_prefix = os.path.join(checkpoint_dir, \"ckpt\")\ncheckpoint = tf.train.Checkpoint(generator_optimizer=generator_optimizer,\n                                 discriminator_optimizer=discriminator_optimizer,\n                                 generator=generator,\n                                 discriminator=discriminator)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-24T11:03:39.476098Z","iopub.execute_input":"2024-12-24T11:03:39.476417Z","iopub.status.idle":"2024-12-24T11:03:39.481705Z","shell.execute_reply.started":"2024-12-24T11:03:39.476390Z","shell.execute_reply":"2024-12-24T11:03:39.480776Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"EPOCHS = 100\nnoise_dim = 100\nnum_examples_to_generate = 16\n\n# You will reuse this seed overtime (so it's easier)\n# to visualize progress in the animated GIF)\nseed = tf.random.normal([num_examples_to_generate, noise_dim])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-24T11:03:46.159505Z","iopub.execute_input":"2024-12-24T11:03:46.159866Z","iopub.status.idle":"2024-12-24T11:03:46.165474Z","shell.execute_reply.started":"2024-12-24T11:03:46.159836Z","shell.execute_reply":"2024-12-24T11:03:46.164735Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Notice the use of `tf.function`\n# This annotation causes the function to be \"compiled\".\n@tf.function\ndef train_step(images):\n    noise = tf.random.normal([BATCH_SIZE, noise_dim])\n\n    with tf.GradientTape() as gen_tape, tf.GradientTape() as disc_tape:\n      generated_images = generator(noise, training=True)\n\n      real_output = discriminator(images, training=True)\n      fake_output = discriminator(generated_images, training=True)\n\n      gen_loss = generator_loss(fake_output)\n      disc_loss = discriminator_loss(real_output, fake_output)\n\n    gradients_of_generator = gen_tape.gradient(gen_loss, generator.trainable_variables)\n    gradients_of_discriminator = disc_tape.gradient(disc_loss, discriminator.trainable_variables)\n\n    generator_optimizer.apply_gradients(zip(gradients_of_generator, generator.trainable_variables))\n    discriminator_optimizer.apply_gradients(zip(gradients_of_discriminator, discriminator.trainable_variables))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-24T11:03:49.538584Z","iopub.execute_input":"2024-12-24T11:03:49.539292Z","iopub.status.idle":"2024-12-24T11:03:49.545122Z","shell.execute_reply.started":"2024-12-24T11:03:49.539247Z","shell.execute_reply":"2024-12-24T11:03:49.544268Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def train(dataset, epochs):\n  for epoch in range(epochs):\n    start = time.time()\n\n    for image_batch in dataset:\n        train_step(image_batch)\n\n    # Produce images for the GIF as you go\n    #clear_output(wait=True)\n    #generate_and_save_images(generator,\n    #                         epoch + 1,\n    #                         seed)\n\n    # Save the model every 15 epochs\n    if (epoch + 1) % 15 == 0:\n      checkpoint.save(file_prefix = checkpoint_prefix)\n\n    print ('Time for epoch {} is {} sec'.format(epoch + 1, time.time()-start))\n\n  # Generate after the final epoch\n    clear_output(wait=True)\n    generate_and_save_images(generator,\n                           epochs,\n                           seed)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-24T11:03:55.342968Z","iopub.execute_input":"2024-12-24T11:03:55.343730Z","iopub.status.idle":"2024-12-24T11:03:55.349350Z","shell.execute_reply.started":"2024-12-24T11:03:55.343692Z","shell.execute_reply":"2024-12-24T11:03:55.348378Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def generate_and_save_images(model, epoch, test_input):\n  # Notice `training` is set to False.\n  # This is so all layers run in inference mode (batchnorm).\n  predictions = model(test_input, training=False)\n\n  fig = plt.figure(figsize=(4, 4))\n\n  for i in range(predictions.shape[0]):\n      plt.subplot(4, 4, i+1)\n      plt.imshow(predictions[i, :, :, 0] * 127.5 + 127.5)\n      plt.axis('off')\n\n  plt.savefig('image_at_epoch_{:04d}.png'.format(epoch))\n  plt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-24T11:03:59.035509Z","iopub.execute_input":"2024-12-24T11:03:59.036392Z","iopub.status.idle":"2024-12-24T11:03:59.041373Z","shell.execute_reply.started":"2024-12-24T11:03:59.036337Z","shell.execute_reply":"2024-12-24T11:03:59.040493Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# traning gan\ntrain(dataset, EPOCHS)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-24T11:04:29.744615Z","iopub.execute_input":"2024-12-24T11:04:29.745010Z","iopub.status.idle":"2024-12-24T11:13:32.931736Z","shell.execute_reply.started":"2024-12-24T11:04:29.744981Z","shell.execute_reply":"2024-12-24T11:13:32.930890Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"checkpoint.restore(tf.train.latest_checkpoint(checkpoint_dir))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-24T11:13:32.933259Z","iopub.execute_input":"2024-12-24T11:13:32.933688Z","iopub.status.idle":"2024-12-24T11:13:33.020012Z","shell.execute_reply.started":"2024-12-24T11:13:32.933629Z","shell.execute_reply":"2024-12-24T11:13:33.019069Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Display a single image using the epoch number\ndef display_image(epoch_no):\n  return PIL.Image.open('image_at_epoch_{:04d}.png'.format(epoch_no))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-24T11:13:33.021484Z","iopub.execute_input":"2024-12-24T11:13:33.022289Z","iopub.status.idle":"2024-12-24T11:13:33.026256Z","shell.execute_reply.started":"2024-12-24T11:13:33.022241Z","shell.execute_reply":"2024-12-24T11:13:33.025364Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"display_image(EPOCHS)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-24T11:13:33.028390Z","iopub.execute_input":"2024-12-24T11:13:33.028705Z","iopub.status.idle":"2024-12-24T11:13:33.055363Z","shell.execute_reply.started":"2024-12-24T11:13:33.028662Z","shell.execute_reply":"2024-12-24T11:13:33.054501Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#anim_file = 'dcgan.gif'\n\n#with imageio.get_writer(anim_file, mode='I') as writer:\n#  filenames = glob.glob('image*.png')\n#  filenames = sorted(filenames)\n#  for filename in filenames:\n#    image = imageio.imread(filename)\n#    writer.append_data(image)\n#  image = imageio.imread(filename)\n#  writer.append_data(image)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-24T11:13:33.056641Z","iopub.execute_input":"2024-12-24T11:13:33.057302Z","iopub.status.idle":"2024-12-24T11:13:33.712018Z","shell.execute_reply.started":"2024-12-24T11:13:33.057260Z","shell.execute_reply":"2024-12-24T11:13:33.711016Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#!pip install git+https://github.com/tensorflow/docs","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-24T11:13:33.713266Z","iopub.execute_input":"2024-12-24T11:13:33.713579Z","iopub.status.idle":"2024-12-24T11:13:49.027607Z","shell.execute_reply.started":"2024-12-24T11:13:33.713549Z","shell.execute_reply":"2024-12-24T11:13:49.026697Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#import tensorflow_docs.vis.embed as embed\n#embed.embed_file(anim_file)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-24T11:13:49.028994Z","iopub.execute_input":"2024-12-24T11:13:49.029278Z","iopub.status.idle":"2024-12-24T11:13:49.059995Z","shell.execute_reply.started":"2024-12-24T11:13:49.029249Z","shell.execute_reply":"2024-12-24T11:13:49.059117Z"}},"outputs":[],"execution_count":null}]}