{"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":7327,"databundleVersionId":861871,"sourceType":"competition"}],"dockerImageVersionId":30664,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Using VAE to create dogs breed","metadata":{}},{"cell_type":"markdown","source":"Referências:\n- [VAE Dogs](https://handtalk.notion.site/VAE-Dogs-82e2c5f64d7d43d38fc586a6b20d5268)\n- [Autoencoder Variacional Convolucional](https://www.tensorflow.org/tutorials/generative/cvae?hl=pt-br)\n- [Variational Autoencoder in TensorFlow](https://learnopencv.com/variational-autoencoder-in-tensorflow/)","metadata":{}},{"cell_type":"markdown","source":"> Explanation: In autoencoders, the information from the input data is mapped into a fixed latent representation. This is particularly useful when we aim to train models to generate deterministic predictions. In contrast, a variational autoencoder (VAE) converts the input data to a variational representation vector (as the name suggests), where the elements of this vector represent different attributes about the input data distribution. This probabilistic property of the VAE makes it a generative model. The latent representation in VAE is composed of a probability distribution (μ, σ) that best defines our input data","metadata":{}},{"cell_type":"code","source":"import os\nimport time\nimport cv2\nimport tensorflow as tf\nfrom tensorflow.keras import layers\nfrom IPython import display\nimport matplotlib.pyplot as plt\nimport numpy as np\nfrom tensorflow.keras import backend as K\n%matplotlib inline\nfrom sklearn.manifold import TSNE\nimport matplotlib.pyplot as plt\nfrom tensorflow import keras","metadata":{"execution":{"iopub.status.busy":"2024-03-24T19:03:20.187775Z","iopub.execute_input":"2024-03-24T19:03:20.188411Z","iopub.status.idle":"2024-03-24T19:03:23.907665Z","shell.execute_reply.started":"2024-03-24T19:03:20.188379Z","shell.execute_reply":"2024-03-24T19:03:23.906822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Open and normalize data","metadata":{}},{"cell_type":"code","source":"img_height, img_width = 256, 256\nbatch_size = 128\n\ntrain_ds = tf.keras.preprocessing.image_dataset_from_directory(\n    '/kaggle/input/dog-breed-identification',\n    image_size=(img_height, img_width),\n    batch_size=batch_size,\n    label_mode=None)\n\nnormalization_layer = layers.Rescaling(1./255)\nnormalized_ds = train_ds.map(lambda x: normalization_layer(x))","metadata":{"execution":{"iopub.status.busy":"2024-03-24T19:03:23.910149Z","iopub.execute_input":"2024-03-24T19:03:23.911073Z","iopub.status.idle":"2024-03-24T19:03:35.90197Z","shell.execute_reply.started":"2024-03-24T19:03:23.911035Z","shell.execute_reply":"2024-03-24T19:03:35.900968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for images in train_ds.take(1):\n    for i in range(9):\n        ax = plt.subplot(3, 3, i + 1)\n        plt.imshow(images[i].numpy().astype(\"uint8\"))\n        plt.axis(\"off\")","metadata":{"execution":{"iopub.status.busy":"2024-03-24T19:03:35.903281Z","iopub.execute_input":"2024-03-24T19:03:35.90364Z","iopub.status.idle":"2024-03-24T19:03:38.611646Z","shell.execute_reply.started":"2024-03-24T19:03:35.903607Z","shell.execute_reply":"2024-03-24T19:03:38.610723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Functions for Variational Autoencoder (VAE)","metadata":{}},{"cell_type":"code","source":"def sampling_model(distribution_params):\n    mean, log_var = distribution_params\n    epsilon = K.random_normal(shape=K.shape(mean), mean=0., stddev=1.)\n    return mean + K.exp(log_var / 2) * epsilon","metadata":{"execution":{"iopub.status.busy":"2024-03-24T19:03:38.612978Z","iopub.execute_input":"2024-03-24T19:03:38.613607Z","iopub.status.idle":"2024-03-24T19:03:38.618919Z","shell.execute_reply.started":"2024-03-24T19:03:38.613574Z","shell.execute_reply":"2024-03-24T19:03:38.618044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def encoder(input_encoder):\n    inputs = keras.Input(shape=input_encoder, name='input_layer')\n    print(inputs)\n    \n    # Using He initialization which is good for ReLUs\n    initializer = tf.keras.initializers.HeNormal()\n    \n    # Block-1\n    x = layers.Conv2D(32, kernel_size=3, strides=2, padding='same', kernel_initializer=initializer, name='conv_1')(inputs)\n    x = layers.BatchNormalization(name='bn_1')(x)\n    x = layers.LeakyReLU(name='lrelu_1')(x)\n  \n    # Block-2\n    x = layers.Conv2D(64, kernel_size=3, strides= 2, padding='same', kernel_initializer=initializer, name='conv_2')(x)\n    x = layers.BatchNormalization(name='bn_2')(x)\n    x = layers.LeakyReLU(name='lrelu_2')(x)\n   \n    # Block-3\n    x = layers.Conv2D(64, 3, 2, padding='same', kernel_initializer=initializer, name='conv_3')(x)\n    x = layers.BatchNormalization(name='bn_3')(x)\n    x = layers.LeakyReLU(name='lrelu_3')(x)\n   \n    # Block-4\n    x = layers.Conv2D(64, 3, 2, padding='same', kernel_initializer=initializer, name='conv_4')(x)\n    x = layers.BatchNormalization(name='bn_4')(x)\n    x = layers.LeakyReLU(name='lrelu_4')(x)\n  \n    # Block-5\n    x = layers.Conv2D(64, 3, 2, padding='same', kernel_initializer=initializer, name='conv_5')(x)\n    x = layers.BatchNormalization(name='bn_5')(x)\n    x = layers.LeakyReLU(name='lrelu_5')(x)\n  \n    # Final Block adjustments\n    flatten = layers.Flatten()(x)\n    mean = layers.Dense(200, name='mean', kernel_initializer=initializer)(flatten)\n    log_var = layers.Dense(200, name='log_var', kernel_initializer=initializer)(flatten)\n    model = tf.keras.Model(inputs, (mean, log_var), name=\"Encoder\")\n    return model","metadata":{"execution":{"iopub.status.busy":"2024-03-24T19:03:38.620225Z","iopub.execute_input":"2024-03-24T19:03:38.620932Z","iopub.status.idle":"2024-03-24T19:03:38.636149Z","shell.execute_reply.started":"2024-03-24T19:03:38.620897Z","shell.execute_reply":"2024-03-24T19:03:38.635295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_encoder = (256, 256, 3)\nenc = encoder(input_encoder)\nenc.summary()","metadata":{"execution":{"iopub.status.busy":"2024-03-24T19:03:38.637327Z","iopub.execute_input":"2024-03-24T19:03:38.637614Z","iopub.status.idle":"2024-03-24T19:03:38.87114Z","shell.execute_reply.started":"2024-03-24T19:03:38.637591Z","shell.execute_reply":"2024-03-24T19:03:38.870241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_1 = (200,)\ninput_2 = (200,)","metadata":{"execution":{"iopub.status.busy":"2024-03-24T19:03:38.872374Z","iopub.execute_input":"2024-03-24T19:03:38.872661Z","iopub.status.idle":"2024-03-24T19:03:38.876962Z","shell.execute_reply.started":"2024-03-24T19:03:38.872636Z","shell.execute_reply":"2024-03-24T19:03:38.875968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def sampling(input_1,input_2):\n    #input1 = layers.Lambda(sampling_model, name='encoder_output')([mean, log_var])\n    mean = keras.Input(shape=input_1, name='input_layer1')\n    log_var = keras.Input(shape=input_2, name='input_layer2')\n    out = layers.Lambda(sampling_model, name='encoder_output')([mean, log_var])\n    enc_2 = tf.keras.Model([mean,log_var], out,  name=\"Encoder_2\")\n    return enc_2","metadata":{"execution":{"iopub.status.busy":"2024-03-24T19:03:38.88086Z","iopub.execute_input":"2024-03-24T19:03:38.881194Z","iopub.status.idle":"2024-03-24T19:03:38.887925Z","shell.execute_reply.started":"2024-03-24T19:03:38.881164Z","shell.execute_reply":"2024-03-24T19:03:38.887078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final = sampling(input_1,input_2)\nfinal.summary()","metadata":{"execution":{"iopub.status.busy":"2024-03-24T19:03:38.88921Z","iopub.execute_input":"2024-03-24T19:03:38.88978Z","iopub.status.idle":"2024-03-24T19:03:38.931332Z","shell.execute_reply.started":"2024-03-24T19:03:38.889749Z","shell.execute_reply":"2024-03-24T19:03:38.930528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def decoder(input_decoder):\n    inputs = keras.Input(shape=input_decoder, name='input_layer')\n    x = layers.Dense(4096, name='dense_1')(inputs)\n    x = layers.Reshape((8,8,64), name='Reshape')(x)\n    \n    # Block-1\n    x = layers.Conv2DTranspose(64, 3, strides= 2, padding='same',name='conv_transpose_1')(x)\n    x = layers.BatchNormalization(name='bn_1')(x)\n    x = layers.LeakyReLU(name='lrelu_1')(x)\n   \n    # Block-2\n    x = layers.Conv2DTranspose(64, 3, strides= 2, padding='same', name='conv_transpose_2')(x)\n    x = layers.BatchNormalization(name='bn_2')(x)\n    x = layers.LeakyReLU(name='lrelu_2')(x)\n \n    # Block-3\n    x = layers.Conv2DTranspose(64, 3, 2, padding='same', name='conv_transpose_3')(x)\n    x = layers.BatchNormalization(name='bn_3')(x)\n    x = layers.LeakyReLU(name='lrelu_3')(x)\n   \n    # Block-4\n    x = layers.Conv2DTranspose(32, 3, 2, padding='same', name='conv_transpose_4')(x)\n    x = layers.BatchNormalization(name='bn_4')(x)\n    x = layers.LeakyReLU(name='lrelu_4')(x)\n\n    # Block-5\n    outputs = layers.Conv2DTranspose(3, 3, 2,padding='same', activation='sigmoid', name='conv_transpose_5')(x)\n    model = tf.keras.Model(inputs, outputs, name=\"Decoder\")\n    return model","metadata":{"execution":{"iopub.status.busy":"2024-03-24T19:03:38.932326Z","iopub.execute_input":"2024-03-24T19:03:38.932559Z","iopub.status.idle":"2024-03-24T19:03:38.942645Z","shell.execute_reply.started":"2024-03-24T19:03:38.932539Z","shell.execute_reply":"2024-03-24T19:03:38.941696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_decoder = (200,)\ndec = decoder(input_decoder)\ndec.summary()","metadata":{"execution":{"iopub.status.busy":"2024-03-24T19:03:38.943695Z","iopub.execute_input":"2024-03-24T19:03:38.944053Z","iopub.status.idle":"2024-03-24T19:03:39.173716Z","shell.execute_reply.started":"2024-03-24T19:03:38.944008Z","shell.execute_reply":"2024-03-24T19:03:39.172849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import keras\nkeras.__version__","metadata":{"execution":{"iopub.status.busy":"2024-03-24T19:03:39.175083Z","iopub.execute_input":"2024-03-24T19:03:39.17551Z","iopub.status.idle":"2024-03-24T19:03:39.181825Z","shell.execute_reply.started":"2024-03-24T19:03:39.175478Z","shell.execute_reply":"2024-03-24T19:03:39.180878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Training","metadata":{}},{"cell_type":"code","source":"# Keras must be in the version 2.15.0. Instaled and restart the kernel\n# pip install keras==\"2.15.0\"\n\nfrom tensorflow.keras.optimizers.legacy import Adam\noptimizer = Adam(learning_rate=1e-4, clipvalue=0.5)","metadata":{"execution":{"iopub.status.busy":"2024-03-24T19:03:39.18313Z","iopub.execute_input":"2024-03-24T19:03:39.183423Z","iopub.status.idle":"2024-03-24T19:03:39.191929Z","shell.execute_reply.started":"2024-03-24T19:03:39.1834Z","shell.execute_reply":"2024-03-24T19:03:39.190998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def mse_loss(y_true, y_pred):\n    r_loss = K.mean(K.square(y_true - y_pred), axis = [1,2,3])\n    return 1000 * r_loss\n\ndef Kl_loss(mean, log_var):\n    kl_loss = -0.5 * K.sum(1 + log_var - K.square(mean) - K.exp(log_var), axis = 1)\n    return kl_loss\n\ndef vae_loss(y_true, y_pred, mean, log_var):\n    r_loss = mse_loss(y_true, y_pred)\n    kl_loss = Kl_loss(mean, log_var)\n    return  r_loss + kl_loss","metadata":{"execution":{"iopub.status.busy":"2024-03-24T19:03:39.19288Z","iopub.execute_input":"2024-03-24T19:03:39.193164Z","iopub.status.idle":"2024-03-24T19:03:39.203988Z","shell.execute_reply.started":"2024-03-24T19:03:39.193141Z","shell.execute_reply":"2024-03-24T19:03:39.203055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"@tf.function\ndef train_step(images):\n    with tf.GradientTape() as encoder, tf.GradientTape() as decoder:\n        mean, log_var = enc(images, training=True)\n        latent = final([mean, log_var])\n        generated_images = dec(latent, training=True)\n        loss = vae_loss(images, generated_images, mean, log_var)\n\n    gradients_of_enc = encoder.gradient(loss, enc.trainable_variables)\n    gradients_of_dec = decoder.gradient(loss, dec.trainable_variables)\n\n    optimizer.apply_gradients(zip(gradients_of_enc, enc.trainable_variables))\n    optimizer.apply_gradients(zip(gradients_of_dec, dec.trainable_variables))\n    return loss","metadata":{"execution":{"iopub.status.busy":"2024-03-24T19:03:39.205231Z","iopub.execute_input":"2024-03-24T19:03:39.205791Z","iopub.status.idle":"2024-03-24T19:03:39.218433Z","shell.execute_reply.started":"2024-03-24T19:03:39.20576Z","shell.execute_reply":"2024-03-24T19:03:39.217526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.makedirs('tf_vae/dog_breed/training_weights', exist_ok=True)\nos.makedirs('tf_vae/dog_breed/images', exist_ok=True)","metadata":{"execution":{"iopub.status.busy":"2024-03-24T19:03:39.219638Z","iopub.execute_input":"2024-03-24T19:03:39.219948Z","iopub.status.idle":"2024-03-24T19:03:39.229588Z","shell.execute_reply.started":"2024-03-24T19:03:39.219902Z","shell.execute_reply":"2024-03-24T19:03:39.228762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train(dataset, epochs):\n    for epoch in range(epochs):\n        start = time.time()\n        i = 0\n        loss_ = []\n        for image_batch in dataset:\n            i += 1\n            loss = train_step(image_batch)\n            loss_.append(loss)\n        seed = image_batch[:25]\n        display.clear_output(wait=True)\n        generate_and_save_images([enc,final,dec], epoch + 1, seed)\n        enc.save_weights('tf_vae/dog_breed/training_weights/enc_'+ str(epoch)+'.h5')\n        dec.save_weights('tf_vae/dog_breed/training_weights/dec_'+ str(epoch)+'.h5')\n        print ('Time for epoch {} is {} sec'.format(epoch + 1, time.time()-start))\n    print(loss_)\n    display.clear_output(wait=True)\n    generate_and_save_images([enc,final,dec], epochs, seed)","metadata":{"execution":{"iopub.status.busy":"2024-03-24T19:03:39.230945Z","iopub.execute_input":"2024-03-24T19:03:39.231344Z","iopub.status.idle":"2024-03-24T19:03:39.239449Z","shell.execute_reply.started":"2024-03-24T19:03:39.23132Z","shell.execute_reply":"2024-03-24T19:03:39.238576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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    mean, var = enc(test_input, training=False)\n    latent = final([mean, var])\n    predictions = dec(latent, training=False)\n    print(predictions.shape)\n    fig = plt.figure(figsize=(4,4))\n    for i in range(predictions.shape[0]):\n        plt.subplot(5, 5, i+1)\n        pred = predictions[i, :, :, :] * 255\n        pred = np.array(pred)  \n        pred = pred.astype(np.uint8)\n        plt.imshow(pred)\n        plt.axis('off')\n\n    plt.savefig('tf_vae/dog_breed/images/image_at_epoch_{:d}.png'.format(epoch))\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-24T19:03:39.240654Z","iopub.execute_input":"2024-03-24T19:03:39.241234Z","iopub.status.idle":"2024-03-24T19:03:39.24927Z","shell.execute_reply.started":"2024-03-24T19:03:39.24121Z","shell.execute_reply":"2024-03-24T19:03:39.248303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train(normalized_ds, 100)","metadata":{"execution":{"iopub.status.busy":"2024-03-24T19:03:39.250314Z","iopub.execute_input":"2024-03-24T19:03:39.250673Z","iopub.status.idle":"2024-03-24T19:48:00.98035Z","shell.execute_reply.started":"2024-03-24T19:03:39.250641Z","shell.execute_reply":"2024-03-24T19:48:00.979542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Latent Space Projection with t-SNE of VAE","metadata":{}},{"cell_type":"code","source":"enc.load_weights('/kaggle/working/tf_vae/dog_breed/training_weights/enc_29.h5')\ndec.load_weights('/kaggle/working/tf_vae/dog_breed/training_weights/dec_29.h5')","metadata":{"execution":{"iopub.status.busy":"2024-03-24T20:23:51.05933Z","iopub.execute_input":"2024-03-24T20:23:51.060053Z","iopub.status.idle":"2024-03-24T20:23:51.1313Z","shell.execute_reply.started":"2024-03-24T20:23:51.060017Z","shell.execute_reply":"2024-03-24T20:23:51.130342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"embeddings = None\nmean = None\nvar = None\nfor i in normalized_ds:\n    m,v = enc.predict(i)\n    embed = final.predict([m,v])\n    #embed = dec.predict(latent)\n    if embeddings is None:\n        embeddings = embed\n        mean = m\n        var = v\n    else:\n        embeddings = np.concatenate((embeddings, embed))\n        mean = np.concatenate((mean, m))\n        var = np.concatenate((var, v))\n    if embeddings.shape[0] > 5000:\n        break\nembeddings.shape","metadata":{"execution":{"iopub.status.busy":"2024-03-24T20:24:13.398465Z","iopub.execute_input":"2024-03-24T20:24:13.399385Z","iopub.status.idle":"2024-03-24T20:24:24.840482Z","shell.execute_reply.started":"2024-03-24T20:24:13.399347Z","shell.execute_reply":"2024-03-24T20:24:24.839569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n_to_show = 5000\ngrid_size = 15\nfigsize = 12\n\ntsne = TSNE(n_components=2, init='pca', random_state=0)\nX_tsne = tsne.fit_transform(embeddings)\nmin_x = min(X_tsne[:, 0])\nmax_x = max(X_tsne[:, 0])\nmin_y = min(X_tsne[:, 1])\nmax_y = max(X_tsne[:, 1])\n\n\nplt.figure(figsize=(figsize, figsize))\nplt.scatter(X_tsne[:, 0] , X_tsne[:, 1], alpha=0.5, s=2)\nplt.xlabel(\"Dimension-1\", size=20)\nplt.ylabel(\"Dimension-2\", size=20)\nplt.xticks(size=20)\nplt.yticks(size=20)\nplt.title(\"VAE - Projection of 2D Latent-Space (dog_breed Set)\", size=20)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-24T20:24:30.096583Z","iopub.execute_input":"2024-03-24T20:24:30.097487Z","iopub.status.idle":"2024-03-24T20:24:58.21329Z","shell.execute_reply.started":"2024-03-24T20:24:30.097455Z","shell.execute_reply":"2024-03-24T20:24:58.212383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"reconstruction = None\nlat_space = None\nfor i in normalized_ds:\n    m,v = enc.predict(i)\n    latent = final([m,v])\n    out = dec.predict(latent)\n    if reconstruction is None:\n        reconstruction = out\n        lat_space = latent\n    else:\n        reconstruction = np.concatenate((reconstruction, out))\n        lat_space = np.concatenate((lat_space, latent))\n    if reconstruction.shape[0] > 5000:\n        break","metadata":{"execution":{"iopub.status.busy":"2024-03-24T20:24:58.215128Z","iopub.execute_input":"2024-03-24T20:24:58.215515Z","iopub.status.idle":"2024-03-24T20:25:44.683609Z","shell.execute_reply.started":"2024-03-24T20:24:58.215481Z","shell.execute_reply":"2024-03-24T20:25:44.68256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"reconstruction.shape","metadata":{"execution":{"iopub.status.busy":"2024-03-24T20:25:44.685395Z","iopub.execute_input":"2024-03-24T20:25:44.686178Z","iopub.status.idle":"2024-03-24T20:25:44.692431Z","shell.execute_reply.started":"2024-03-24T20:25:44.68614Z","shell.execute_reply":"2024-03-24T20:25:44.691544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Reconstructing data","metadata":{}},{"cell_type":"code","source":"figsize = 15\n\nfig = plt.figure(figsize=(figsize, 10))\n\nfor i in range(25):\n    ax = fig.add_subplot(5, 5, i+1)\n    ax.axis('off')\n    pred = reconstruction[i, :, :, :] * 255\n    pred = np.array(pred)  \n    pred = pred.astype(np.uint8)\n    ax.imshow(pred)","metadata":{"execution":{"iopub.status.busy":"2024-03-24T20:25:44.69441Z","iopub.execute_input":"2024-03-24T20:25:44.694694Z","iopub.status.idle":"2024-03-24T20:25:46.008857Z","shell.execute_reply.started":"2024-03-24T20:25:44.69467Z","shell.execute_reply":"2024-03-24T20:25:46.007932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Reconstructing dog breed images from a latent-vector sampled with normal distribution","metadata":{}},{"cell_type":"code","source":"figsize = 15\nx = np.random.normal(size = (10,200))\nreconstruct = dec.predict(x)\nfig = plt.figure(figsize=(figsize, 10))\n\nfor i in range(10):\n    ax = fig.add_subplot(5, 5, i+1)\n    ax.axis('off')\n    pred = reconstruct[i, :, :, :] * 255\n    pred = np.array(pred)  \n    pred = pred.astype(np.uint8)\n    ax.imshow(pred)","metadata":{"execution":{"iopub.status.busy":"2024-03-24T20:25:46.010093Z","iopub.execute_input":"2024-03-24T20:25:46.010389Z","iopub.status.idle":"2024-03-24T20:25:46.853065Z","shell.execute_reply.started":"2024-03-24T20:25:46.010364Z","shell.execute_reply":"2024-03-24T20:25:46.852138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Reconstructing dog breed images with latent-vector sampled uniformly","metadata":{}},{"cell_type":"code","source":"figsize = 15\nmin_x = lat_space.min(axis=0)\nmax_x = lat_space.max(axis=0)\nx = np.random.uniform(size = (10,200))\nx = x * (max_x - (np.abs(min_x))) \nprint(x.shape)\nreconstruct = dec.predict(x)\n\nfig = plt.figure(figsize=(figsize, 10))\nfig.subplots_adjust(hspace=0.2, wspace=0.2)\n\nfor i in range(10):\n    ax = fig.add_subplot(5, 5, i+1)\n    ax.axis('off')\n    pred = reconstruct[i, :, :, :] * 255\n    pred = np.array(pred)  \n    pred = pred.astype(np.uint8)\n    ax.imshow(pred)","metadata":{"execution":{"iopub.status.busy":"2024-03-24T20:25:46.854538Z","iopub.execute_input":"2024-03-24T20:25:46.854947Z","iopub.status.idle":"2024-03-24T20:25:47.903472Z","shell.execute_reply.started":"2024-03-24T20:25:46.854916Z","shell.execute_reply":"2024-03-24T20:25:47.902607Z"},"trusted":true},"execution_count":null,"outputs":[]}]}