{"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"},{"sourceId":160039,"sourceType":"datasetVersion","datasetId":72477},{"sourceId":3939109,"sourceType":"datasetVersion","datasetId":2338285}],"dockerImageVersionId":29849,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## VAE MNIST(pytorch)","metadata":{}},{"cell_type":"code","source":"# VAE.py\nimport torch\nimport torch.nn as nn\nimport torch.optim as optim\nfrom torch.utils.data import DataLoader\nfrom torchvision import datasets, transforms\nimport matplotlib.pyplot as plt\n\nclass Reshape(nn.Module):\n    def __init__(self, args):\n        super(Reshape, self).__init__()\n        self.shape = args\n    def forward(self, x):\n        return x.view(self.shape)\n# 定义 VAE 编码器\nclass VAEEncoder(nn.Module):\n    def __init__(self, hidden_size, latent_size):\n        super().__init__()\n        self.conv1=nn.Conv2d(1,16,kernel_size=3,stride=2,padding=1)\n        self.conv2=nn.Conv2d(16,32,kernel_size=3,stride=2,padding=1)\n        self.conv3=nn.Conv2d(32,64,kernel_size=7)\n        self.flatten= nn.Flatten()\n            #nn.Linear(in_features=64,out_features=2),\n        self.fc1 = nn.Linear(64, hidden_size)\n        self.fc2 = nn.Linear(hidden_size, latent_size * 2)\n\n    def forward(self, x):\n        x = torch.relu(self.conv1(x))\n        x=torch.relu(self.conv2(x))\n        x=torch.relu(self.conv3(x))\n        x=self.flatten(x)\n        #print(x.shape)\n        #print(self.fc1)\n        x=torch.relu(self.fc1(x))\n        x = self.fc2(x)\n        mu, log_var = x.split(latent_size, dim=1)\n        return mu, log_var\n\n# 定义 VAE 解码器\nclass VAEDecoder(nn.Module):\n    def __init__(self, latent_size, hidden_size):\n        super().__init__()\n        self.fc1 = nn.Linear(latent_size, hidden_size)\n        self.fc2 = nn.Linear(hidden_size, 64)\n        self.reshape=Reshape((-1, 64, 1, 1))\n        self.convTran1=nn.ConvTranspose2d(64,32,kernel_size=7)\n        self.convTran2=nn.ConvTranspose2d(32,16,kernel_size=3,stride=2,padding=1,output_padding=1)\n        self.convTran3=nn.ConvTranspose2d(16,1,kernel_size=3,stride=2,padding=1,output_padding=1)\n\n    def forward(self, x):\n        x = torch.relu(self.fc1(x))\n        x = torch.relu(self.fc2(x))\n        x=self.reshape(x)\n        x=torch.relu(self.convTran1(x))\n        x=torch.relu(self.convTran2(x))\n        x=torch.sigmoid(self.convTran3(x))\n        return x\n\n# 定义 VAE 模型\nclass VAE(nn.Module):\n    def __init__(self,hidden_size, latent_size):\n        super().__init__()\n        self.encoder = VAEEncoder(hidden_size, latent_size)\n        self.decoder = VAEDecoder(latent_size, hidden_size)\n\n    def forward(self, x):\n        mu, log_var = self.encoder(x)\n        std = torch.exp(0.5 * log_var)\n        eps = torch.randn_like(std)\n        z = mu + std * eps\n        recon = self.decoder(z)\n        return recon, mu, log_var\n\n# 定义 VAE 损失函数\ndef vae_loss(recon, x, mu, log_var):\n    #print(recon,x)\n    recon_loss = nn.BCELoss(reduction='sum')(recon, x)\n    kl_loss = -0.5 * torch.sum(1 + log_var - mu.pow(2) - log_var.exp())\n    return recon_loss + kl_loss\n\n# 加载 MNIST 数据集\ntransform = transforms.Compose([\n    transforms.ToTensor(),\n    #transforms.Normalize((0.5,), (0.5,))\n])\nmnist = datasets.MNIST(root='.', download=True, transform=transform)\n\n# 定义训练参数\nbatch_size = 64\nlr = 1e-3\nnum_epochs = 10\n\n# 定义数据加载器\ndata_loader = DataLoader(mnist, batch_size=batch_size, shuffle=True) # shuffle=True 打乱数据\n\n# 定义模型、优化器和损失函数\n# 定义 VAE 模型\ninput_size = 28 * 28\nhidden_size = 256\nlatent_size = 64\n\n\n","metadata":{"execution":{"iopub.status.busy":"2024-03-18T02:57:47.398343Z","iopub.execute_input":"2024-03-18T02:57:47.398767Z","iopub.status.idle":"2024-03-18T02:57:47.464382Z","shell.execute_reply.started":"2024-03-18T02:57:47.398700Z","shell.execute_reply":"2024-03-18T02:57:47.463674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tqdm\ndevice=torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')\n\nmodel = VAE( hidden_size, latent_size)\nmodel=model.to(device)\n# 定义优化器\noptimizer = optim.Adam(model.parameters(), lr=lr)\n\n\n# 训练 VAE 模型\nfor epoch in range(num_epochs):\n    epoch_loss = 0.0\n    for x, _ in tqdm.tqdm(data_loader):\n       # x = x.view(-1, input_size)\n        #print(x)\n        x=x.to(device)\n        recon, mu, log_var = model(x)\n        loss = vae_loss(recon, x, mu, log_var)\n        optimizer.zero_grad()\n        loss.backward()\n        optimizer.step()\n        epoch_loss += loss.item()\n    print(f'Epoch {epoch+1} loss: {epoch_loss / len(mnist):.4f}')              \n    \n# 保存模型\ntorch.save(model.state_dict(), 'vae.pth')","metadata":{"execution":{"iopub.status.busy":"2024-03-18T02:59:37.747588Z","iopub.execute_input":"2024-03-18T02:59:37.747930Z","iopub.status.idle":"2024-03-18T03:02:29.053218Z","shell.execute_reply.started":"2024-03-18T02:59:37.747871Z","shell.execute_reply":"2024-03-18T03:02:29.052297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n_to_show = 30\n\nznew =torch.from_numpy( np.random.normal(size = (n_to_show,latent_size)))\nmodel.eval()\nrecon = model.decoder(znew.float().cuda())\n\nfig = plt.figure(figsize=(18, 5))\nfig.subplots_adjust(hspace=0.4, wspace=0.4)\nfor i in range(n_to_show):\n    img = recon[i].squeeze()\n    ax = fig.add_subplot(3, 10, i+1)\n    ax.imshow(img.detach().cpu().numpy(),cmap='gray')\n    ax.axis('off')\n\nplt.show()\nfig.savefig('1.png')","metadata":{"execution":{"iopub.status.busy":"2024-03-18T03:29:39.597089Z","iopub.execute_input":"2024-03-18T03:29:39.597607Z","iopub.status.idle":"2024-03-18T03:29:41.419267Z","shell.execute_reply.started":"2024-03-18T03:29:39.597341Z","shell.execute_reply":"2024-03-18T03:29:41.418558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# generate.py \nimport torch\nimport matplotlib.pyplot as plt\nfrom torch.utils.data import WeightedRandomSampler,DataLoader\n#from VAE import model, input_size, mnist # 从 VAE.py 中导入模型、输入大小和 MNIST 数据集\n\n# 加载已训练好的模型\nmodel.load_state_dict(torch.load('vae.pth'))\n\ndataloader = DataLoader(dataset=mnist,\n                    batch_size=64,  # 组织成batch大小\n                    sampler=WeightedRandomSampler(torch.ones(len(mnist)), 64))\n\nexample_batch,label=next(iter(dataloader))\nsample_images = example_batch.to(device)\n\nrecon,mu, log_var = model(sample_images)\n\n\n# 显示原始图像和生成的图像\nplt.subplot(1, 2, 1)\nplt.title('Original Image')\nplt.imshow(sample_images.squeeze()[0].detach().cpu().numpy(), cmap='gray')\nplt.subplot(1, 2, 2)\nplt.title('Generated Image')\nplt.imshow(recon.squeeze()[0].detach().cpu().numpy(), cmap='gray')\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-03-18T03:20:40.165892Z","iopub.execute_input":"2024-03-18T03:20:40.166192Z","iopub.status.idle":"2024-03-18T03:20:40.552718Z","shell.execute_reply.started":"2024-03-18T03:20:40.166148Z","shell.execute_reply":"2024-03-18T03:20:40.551236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n_to_show = 10\n\ndataloader = DataLoader(dataset=mnist,\n                    batch_size=64,  # 组织成batch大小\n                    sampler=WeightedRandomSampler(torch.ones(len(mnist)), 64))\n\nexample_batch,label=next(iter(dataloader))\nsample_images = example_batch.to(device)\n\nrecon,mu, log_var = model(sample_images)\n\nfig = plt.figure(figsize=(15, 3))\nfig.subplots_adjust(hspace=0.4, wspace=0.4)\n\nfor i in range(n_to_show):\n    img = sample_images[i].squeeze()\n    sub = fig.add_subplot(2, n_to_show, i+1)\n    sub.axis('off')        \n    sub.imshow(img.detach().cpu().numpy())\n\nfor i in range(n_to_show):\n    img = recon[i].squeeze()\n    sub = fig.add_subplot(2, n_to_show, i+n_to_show+1)\n    sub.axis('off')\n    sub.imshow(img.detach().cpu().numpy())\nfig.savefig('2.png')\n\n\n\n\n\n","metadata":{"execution":{"iopub.status.busy":"2024-03-18T03:23:21.054334Z","iopub.execute_input":"2024-03-18T03:23:21.054700Z","iopub.status.idle":"2024-03-18T03:23:21.970351Z","shell.execute_reply.started":"2024-03-18T03:23:21.054640Z","shell.execute_reply":"2024-03-18T03:23:21.969510Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## VAE fashion-mnist(tensorflow)","metadata":{}},{"cell_type":"code","source":"%load_ext autoreload\n%autoreload 2\n\nimport numpy as np\nimport matplotlib.pyplot as plt\n\nimport tensorflow as tf\nimport tensorflow.keras.backend as K\nfrom tensorflow.keras import (\n    layers,\n    models,\n    datasets,\n    callbacks,\n    losses,\n    optimizers,\n    metrics,\n)\n\nfrom scipy.stats import norm\n\nIMAGE_SIZE = 32\nBATCH_SIZE = 100\nVALIDATION_SPLIT = 0.2\nEMBEDDING_DIM = 2\nEPOCHS = 5\nBETA = 500\n\n# Load the data\n(x_train, y_train), (x_test, y_test) = datasets.fashion_mnist.load_data()\n\n# Preprocess the data\n\n\ndef preprocess(imgs):\n    \"\"\"\n    Normalize and reshape the images\n    \"\"\"\n    imgs = imgs.astype(\"float32\") / 255.0\n    imgs = np.pad(imgs, ((0, 0), (2, 2), (2, 2)), constant_values=0.0)\n    imgs = np.expand_dims(imgs, -1)\n    return imgs\n\n\nx_train = preprocess(x_train)\nx_test = preprocess(x_test)","metadata":{"execution":{"iopub.status.busy":"2024-03-17T03:06:21.186733Z","iopub.execute_input":"2024-03-17T03:06:21.187842Z","iopub.status.idle":"2024-03-17T03:06:42.343770Z","shell.execute_reply.started":"2024-03-17T03:06:21.187775Z","shell.execute_reply":"2024-03-17T03:06:42.342638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Sampling(layers.Layer):\n    def call(self, inputs):\n        z_mean, z_log_var = inputs\n        batch = tf.shape(z_mean)[0]\n        dim = tf.shape(z_mean)[1]\n        epsilon = K.random_normal(shape=(batch, dim))\n        return z_mean + tf.exp(0.5 * z_log_var) * epsilon\n\n# Encoder\nencoder_input = layers.Input(\n    shape=(IMAGE_SIZE, IMAGE_SIZE, 1), name=\"encoder_input\"\n)\nx = layers.Conv2D(32, (3, 3), strides=2, activation=\"relu\", padding=\"same\")(\n    encoder_input\n)\nx = layers.Conv2D(64, (3, 3), strides=2, activation=\"relu\", padding=\"same\")(x)\nx = layers.Conv2D(128, (3, 3), strides=2, activation=\"relu\", padding=\"same\")(x)\nshape_before_flattening = K.int_shape(x)[1:]  # the decoder will need this!\n\nx = layers.Flatten()(x)\nz_mean = layers.Dense(EMBEDDING_DIM, name=\"z_mean\")(x)\nz_log_var = layers.Dense(EMBEDDING_DIM, name=\"z_log_var\")(x)\nz = Sampling()([z_mean, z_log_var])\n\nencoder = models.Model(encoder_input, [z_mean, z_log_var, z], name=\"encoder\")\nencoder.summary()\n\n# Decoder\ndecoder_input = layers.Input(shape=(EMBEDDING_DIM,), name=\"decoder_input\")\nx = layers.Dense(np.prod(shape_before_flattening))(decoder_input)\nx = layers.Reshape(shape_before_flattening)(x)\nx = layers.Conv2DTranspose(\n    128, (3, 3), strides=2, activation=\"relu\", padding=\"same\"\n)(x)\nx = layers.Conv2DTranspose(\n    64, (3, 3), strides=2, activation=\"relu\", padding=\"same\"\n)(x)\nx = layers.Conv2DTranspose(\n    32, (3, 3), strides=2, activation=\"relu\", padding=\"same\"\n)(x)\ndecoder_output = layers.Conv2D(\n    1,\n    (3, 3),\n    strides=1,\n    activation=\"sigmoid\",\n    padding=\"same\",\n    name=\"decoder_output\",\n)(x)\n\ndecoder = models.Model(decoder_input, decoder_output)\ndecoder.summary()\n\nclass VAE(models.Model):\n    def __init__(self, encoder, decoder, **kwargs):\n        super(VAE, self).__init__(**kwargs)\n        self.encoder = encoder\n        self.decoder = decoder\n        self.total_loss_tracker = metrics.Mean(name=\"total_loss\")\n        self.reconstruction_loss_tracker = metrics.Mean(\n            name=\"reconstruction_loss\"\n        )\n        self.kl_loss_tracker = metrics.Mean(name=\"kl_loss\")\n\n    @property\n    def metrics(self):\n        return [\n            self.total_loss_tracker,\n            self.reconstruction_loss_tracker,\n            self.kl_loss_tracker,\n        ]\n\n    def call(self, inputs):\n        \"\"\"Call the model on a particular input.\"\"\"\n        z_mean, z_log_var, z = encoder(inputs)\n        reconstruction = decoder(z)\n        return z_mean, z_log_var, reconstruction\n\n    def train_step(self, data):\n        \"\"\"Step run during training.\"\"\"\n        with tf.GradientTape() as tape:\n            z_mean, z_log_var, reconstruction = self(data)\n            reconstruction_loss = tf.reduce_mean(\n                BETA\n                * losses.binary_crossentropy(\n                    data, reconstruction, axis=(1, 2, 3)\n                )\n            )\n            kl_loss = tf.reduce_mean(\n                tf.reduce_sum(\n                    -0.5\n                    * (1 + z_log_var - tf.square(z_mean) - tf.exp(z_log_var)),\n                    axis=1,\n                )\n            )\n            total_loss = reconstruction_loss + kl_loss\n\n        grads = tape.gradient(total_loss, self.trainable_weights)\n        self.optimizer.apply_gradients(zip(grads, self.trainable_weights))\n\n        self.total_loss_tracker.update_state(total_loss)\n        self.reconstruction_loss_tracker.update_state(reconstruction_loss)\n        self.kl_loss_tracker.update_state(kl_loss)\n\n        return {m.name: m.result() for m in self.metrics}\n\n    def test_step(self, data):\n        \"\"\"Step run during validation.\"\"\"\n        if isinstance(data, tuple):\n            data = data[0]\n\n        z_mean, z_log_var, reconstruction = self(data)\n        reconstruction_loss = tf.reduce_mean(\n            BETA\n            * losses.binary_crossentropy(data, reconstruction, axis=(1, 2, 3))\n        )\n        kl_loss = tf.reduce_mean(\n            tf.reduce_sum(\n                -0.5 * (1 + z_log_var - tf.square(z_mean) - tf.exp(z_log_var)),\n                axis=1,\n            )\n        )\n        total_loss = reconstruction_loss + kl_loss\n\n        return {\n            \"loss\": total_loss,\n            \"reconstruction_loss\": reconstruction_loss,\n            \"kl_loss\": kl_loss,\n        }","metadata":{"execution":{"iopub.status.busy":"2024-03-17T03:06:42.633509Z","iopub.execute_input":"2024-03-17T03:06:42.633884Z","iopub.status.idle":"2024-03-17T03:06:42.837891Z","shell.execute_reply.started":"2024-03-17T03:06:42.633853Z","shell.execute_reply":"2024-03-17T03:06:42.836855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!git clone https://github.com/davidADSP/Generative_Deep_Learning_2nd_Edition","metadata":{"execution":{"iopub.status.busy":"2024-03-17T03:06:50.484616Z","iopub.execute_input":"2024-03-17T03:06:50.485148Z","iopub.status.idle":"2024-03-17T03:06:55.655082Z","shell.execute_reply.started":"2024-03-17T03:06:50.485107Z","shell.execute_reply":"2024-03-17T03:06:55.653496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd /kaggle/working/Generative_Deep_Learning_2nd_Edition/notebooks/03_vae","metadata":{"execution":{"iopub.status.busy":"2024-03-17T03:06:56.072437Z","iopub.execute_input":"2024-03-17T03:06:56.073145Z","iopub.status.idle":"2024-03-17T03:06:56.144302Z","shell.execute_reply.started":"2024-03-17T03:06:56.073099Z","shell.execute_reply":"2024-03-17T03:06:56.142638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create a variational autoencoder\nvae = VAE(encoder, decoder)\n\n# Compile the variational autoencoder\noptimizer = optimizers.Adam(learning_rate=0.0005)\nvae.compile(optimizer=optimizer)\n\n# Create a model save checkpoint\nmodel_checkpoint_callback = callbacks.ModelCheckpoint(\n    filepath=\"./checkpoint.keras\",\n    save_weights_only=False,\n    save_freq=\"epoch\",\n    monitor=\"loss\",\n    mode=\"min\",\n    save_best_only=True,\n    verbose=0,\n)\ntensorboard_callback = callbacks.TensorBoard(log_dir=\"./logs\")","metadata":{"execution":{"iopub.status.busy":"2024-03-17T03:08:10.604233Z","iopub.execute_input":"2024-03-17T03:08:10.604877Z","iopub.status.idle":"2024-03-17T03:08:10.670129Z","shell.execute_reply.started":"2024-03-17T03:08:10.604841Z","shell.execute_reply":"2024-03-17T03:08:10.669129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vae.fit(\n    x_train,\n    epochs=EPOCHS,\n    batch_size=BATCH_SIZE,\n    shuffle=True,\n    validation_data=(x_test, x_test),\n    callbacks=[model_checkpoint_callback, tensorboard_callback],\n)","metadata":{"execution":{"iopub.status.busy":"2024-03-17T03:08:14.978324Z","iopub.execute_input":"2024-03-17T03:08:14.978731Z","iopub.status.idle":"2024-03-17T03:20:08.060256Z","shell.execute_reply.started":"2024-03-17T03:08:14.978697Z","shell.execute_reply":"2024-03-17T03:20:08.058894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n\ndef sample_batch(dataset):\n    batch = dataset.take(1).get_single_element()\n    if isinstance(batch, tuple):\n        batch = batch[0]\n    return batch.numpy()\n\n\ndef display(\n    images, n=10, size=(20, 3), cmap=\"gray_r\", as_type=\"float32\", save_to=None\n):\n    \"\"\"\n    Displays n random images from each one of the supplied arrays.\n    \"\"\"\n    if images.max() > 1.0:\n        images = images / 255.0\n    elif images.min() < 0.0:\n        images = (images + 1.0) / 2.0\n\n    plt.figure(figsize=size)\n    for i in range(n):\n        _ = plt.subplot(1, n, i + 1)\n        plt.imshow(images[i].astype(as_type), cmap=cmap)\n        plt.axis(\"off\")\n\n    if save_to:\n        plt.savefig(save_to)\n        print(f\"\\nSaved to {save_to}\")\n\n    plt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-03-17T03:20:11.160331Z","iopub.execute_input":"2024-03-17T03:20:11.161571Z","iopub.status.idle":"2024-03-17T03:20:11.225248Z","shell.execute_reply.started":"2024-03-17T03:20:11.161516Z","shell.execute_reply":"2024-03-17T03:20:11.224235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Select a subset of the test set\nn_to_predict = 5000\nexample_images = x_test[:n_to_predict]\nexample_labels = y_test[:n_to_predict]\n\n# Create autoencoder predictions and display\nz_mean, z_log_var, reconstructions = vae.predict(example_images)\nprint(\"Example real clothing items\")\ndisplay(example_images)\nprint(\"Reconstructions\")\ndisplay(reconstructions)","metadata":{"execution":{"iopub.status.busy":"2024-03-17T03:20:14.997772Z","iopub.execute_input":"2024-03-17T03:20:14.998781Z","iopub.status.idle":"2024-03-17T03:20:19.118317Z","shell.execute_reply.started":"2024-03-17T03:20:14.998737Z","shell.execute_reply":"2024-03-17T03:20:19.117106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Encode the example images\nz_mean, z_var, z = encoder.predict(example_images)\n# Some examples of the embeddings\nprint(z[:10])\n# Show the encoded points in 2D space\nfigsize = 8\n\nplt.figure(figsize=(figsize, figsize))\nplt.scatter(z[:, 0], z[:, 1], c=\"black\", alpha=0.5, s=3)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-17T03:20:45.990283Z","iopub.execute_input":"2024-03-17T03:20:45.991333Z","iopub.status.idle":"2024-03-17T03:20:47.533144Z","shell.execute_reply.started":"2024-03-17T03:20:45.991289Z","shell.execute_reply":"2024-03-17T03:20:47.532074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Sample some points in the latent space, from the standard normal distribution\ngrid_width, grid_height = (6, 3)\nz_sample = np.random.normal(size=(grid_width * grid_height, 2))\n\n# Decode the sampled points\nreconstructions = decoder.predict(z_sample)\n\n# Convert original embeddings and sampled embeddings to p-values\np = norm.cdf(z)\np_sample = norm.cdf(z_sample)\n\n# Draw a plot of...\nfigsize = 8\nplt.figure(figsize=(figsize, figsize))\n\n# ... the original embeddings ...\nplt.scatter(z[:, 0], z[:, 1], c=\"black\", alpha=0.5, s=2)\n\n# ... and the newly generated points in the latent space\nplt.scatter(z_sample[:, 0], z_sample[:, 1], c=\"#00B0F0\", alpha=1, s=40)\nplt.show()\n\n# Add underneath a grid of the decoded images\nfig = plt.figure(figsize=(figsize, grid_height * 2))\nfig.subplots_adjust(hspace=0.4, wspace=0.4)\n\nfor i in range(grid_width * grid_height):\n    ax = fig.add_subplot(grid_height, grid_width, i + 1)\n    ax.axis(\"off\")\n    ax.text(\n        0.5,\n        -0.35,\n        str(np.round(z_sample[i, :], 1)),\n        fontsize=10,\n        ha=\"center\",\n        transform=ax.transAxes,\n    )\n    ax.imshow(reconstructions[i, :, :], cmap=\"Greys\")","metadata":{"execution":{"iopub.status.busy":"2024-03-17T03:20:54.157399Z","iopub.execute_input":"2024-03-17T03:20:54.158353Z","iopub.status.idle":"2024-03-17T03:20:55.675801Z","shell.execute_reply.started":"2024-03-17T03:20:54.158306Z","shell.execute_reply":"2024-03-17T03:20:55.674609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## celeba数据集上训练VAE（pytorch）","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport json\nfrom PIL import Image\nimport matplotlib.pyplot as plt\n\nimport torch\nimport torchvision\nfrom torchvision import models,transforms\n\nimport glob\nimport os.path as osp\nimport random\nimport numpy as np\nimport json\nfrom PIL import Image\nfrom tqdm import tqdm\nimport matplotlib.pyplot as plt\n\nimport torch\nimport torch.nn as nn\nimport torch.optim as optim\nimport torch.utils.data as data\nimport torchvision\nfrom torchvision import models,transforms\n\ntorch.manual_seed(1234)\nnp.random.seed(1234)\nrandom.seed(1234)\n\n#finished()\n","metadata":{"execution":{"iopub.status.busy":"2024-03-18T00:57:48.407378Z","iopub.execute_input":"2024-03-18T00:57:48.407726Z","iopub.status.idle":"2024-03-18T00:57:49.812750Z","shell.execute_reply.started":"2024-03-18T00:57:48.407673Z","shell.execute_reply":"2024-03-18T00:57:49.812033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def make_datapath_list(root_path):\n    \"\"\"\n    Returns:\n    -----------\n    path_list:list\n    保存数据路径的列表\n    \"\"\"\n    #root_path='/kaggle/input/celebrity-face-image-dataset/Celebrity Faces Dataset'\n    target_path=osp.join(root_path+'/**/*.jpg')\n    #print(target_path)\n    \n    path_list=[]\n    \n    #使用glob取得文件路径\n    return glob.glob(target_path)\n\nroot_path='/kaggle/input/celeba'\nl=make_datapath_list(root_path)\n\ntrain_num=int(len(l)*0.8)\ntrain_list=l[:train_num]\ntest_list=l[train_num:]\nlen(test_list)","metadata":{"execution":{"iopub.status.busy":"2024-03-18T00:59:00.449567Z","iopub.execute_input":"2024-03-18T00:59:00.449880Z","iopub.status.idle":"2024-03-18T00:59:03.491438Z","shell.execute_reply.started":"2024-03-18T00:59:00.449835Z","shell.execute_reply":"2024-03-18T00:59:03.490700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#创建Dataset\nclass CelebDataset(data.Dataset):\n    \"\"\"\n    file_path:list\n    transform:object\n    phase:'train' or 'val'\n    \"\"\"\n    def __init__(self,file_list,transform=None,phase='train'):\n        self.file_list=file_list\n        self.transform=transform\n        self.phase=phase\n    def __len__(self):\n        return len(self.file_list)\n    \n    def __getitem__(self,index):\n        \"\"\"\n        获取预处理完毕的图片的张量数据和标签\n        \"\"\"\n        img_path=self.file_list[index]\n        img=Image.open(img_path)\n        img_tranformed=self.transform(img)\n        \n\n\n        return img_tranformed\n    \nInput_size=(128,128)\ntransform=torchvision.transforms.Compose([\n    torchvision.transforms.Resize(Input_size),\n    torchvision.transforms.ToTensor(),    \n])\ntrain_dataset=CelebDataset(file_list=train_list,transform=transform,phase='train')\ntest_dataset=CelebDataset(file_list=test_list,transform=transform,phase='test')\n\nindex=1\nprint(train_dataset.__getitem__(index))\nprint(train_dataset.__getitem__(index)[0].size())\n","metadata":{"execution":{"iopub.status.busy":"2024-03-18T00:59:07.789039Z","iopub.execute_input":"2024-03-18T00:59:07.789345Z","iopub.status.idle":"2024-03-18T00:59:07.881629Z","shell.execute_reply.started":"2024-03-18T00:59:07.789302Z","shell.execute_reply":"2024-03-18T00:59:07.880622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#创建DataLoader\nbatch_size=32\ntrain_dataloader=torch.utils.data.DataLoader(train_dataset,batch_size=batch_size,shuffle=True)\ntest_dataloader=torch.utils.data.DataLoader(test_dataset,batch_size=batch_size,shuffle=False)\ndataloaders_dict={\n    'train':train_dataloader,\n    'test':test_dataloader\n}\n\nbatch_iter=iter(dataloaders_dict['train'])#转成迭代器\ninputs=next(batch_iter)#取第一个元素\nprint(inputs.size())\n","metadata":{"execution":{"iopub.status.busy":"2024-03-18T02:14:37.094735Z","iopub.execute_input":"2024-03-18T02:14:37.095057Z","iopub.status.idle":"2024-03-18T02:14:37.191390Z","shell.execute_reply.started":"2024-03-18T02:14:37.095012Z","shell.execute_reply":"2024-03-18T02:14:37.190516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch.nn.functional as F\n# 定义 VAE 模型\nclass VAE(nn.Module):\n    def __init__(self,z_dim):\n        super().__init__()\n        self.z_dim=z_dim\n        self.encoder_convs=[]\n        self.decoder_convs=[]\n        self.encoder_out_channels=[32,64,64,64]\n        self.decoder_out_channels=[64,64,32]\n        self.encoder_convs.append(nn.Conv2d(in_channels=3,out_channels=32,kernel_size=3,stride=2,padding=1))\n        self.encoder_convs.append(nn.Conv2d(32,64,3,2,1))\n        self.encoder_convs.append(nn.Conv2d(64,64,3,2,1))\n        self.encoder_convs.append(nn.Conv2d(64,64,3,2,1))\n        \n            \n        self.fc1=nn.Linear(4096,self.z_dim*2)\n        self.fc2=nn.Linear(self.z_dim,4096)\n        #self.fc2=nn.Linear(4096,self.z_dim)\n        self.decoder_convs.append(nn.ConvTranspose2d(64,64,3,2,1,1))\n        self.decoder_convs.append(nn.ConvTranspose2d(64,64,3,2,1,1))\n        self.decoder_convs.append(nn.ConvTranspose2d(64,32,3,2,1,1))\n        self.decoder_convs.append(nn.ConvTranspose2d(32,3,3,2,1,1))\n        \n        \n        self.encoder_batchnorm=[]\n        self.decoder_batchnorm=[]\n        for out_c in self.encoder_out_channels:\n            self.encoder_batchnorm.append(nn.BatchNorm2d(out_c))\n        for out_c in self.decoder_out_channels:\n             self.decoder_batchnorm.append(nn.BatchNorm2d(out_c))\n        self.layers = nn.ModuleList()\n        self.layers=nn.ModuleList(self.encoder_convs+self.encoder_batchnorm+\n                                  self.decoder_convs+self.decoder_batchnorm) \n        #self.layers.append(self.fc1)\n        #self.layers.append(self.fc2)\n    def forward(self, x):\n        for convs,BN in zip(self.encoder_convs,self.encoder_batchnorm):\n            #print(x.device)\n            x=convs(x)\n            x = BN(x)\n            x = F.leaky_relu(x)\n            x = nn.Dropout(0.25)(x)\n        shape_before_flattening = x.shape\n        x=nn.Flatten()(x)\n        \n        x = self.fc1(x)\n        \n        mu, log_var = x.split(self.z_dim, dim=1)\n        std = torch.exp(0.5 * log_var)\n        eps = torch.randn_like(std)\n        x = mu + std * eps\n        \n        x=self.fc2(x)\n        x=x.reshape(shape_before_flattening)\n        for i in range(len(self.decoder_convs)):\n            convs=self.decoder_convs[i]\n            x=convs(x)\n            if(i<len(self.decoder_convs)-1):\n                BN=self.decoder_batchnorm[i]\n                x = BN(x)\n                x = F.leaky_relu(x)\n                x = nn.Dropout(0.25)(x)\n            else:\n                x=torch.sigmoid(x)\n        return x,mu,log_var\n\nmodel=VAE(200)\n","metadata":{"execution":{"iopub.status.busy":"2024-03-18T00:59:15.432959Z","iopub.execute_input":"2024-03-18T00:59:15.433305Z","iopub.status.idle":"2024-03-18T00:59:15.501595Z","shell.execute_reply.started":"2024-03-18T00:59:15.433243Z","shell.execute_reply":"2024-03-18T00:59:15.500608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pip install torchsummary","metadata":{"execution":{"iopub.status.busy":"2024-03-17T14:59:26.540443Z","iopub.execute_input":"2024-03-17T14:59:26.540753Z","iopub.status.idle":"2024-03-17T14:59:34.833316Z","shell.execute_reply.started":"2024-03-17T14:59:26.540716Z","shell.execute_reply":"2024-03-17T14:59:34.832203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfrom torchsummary import summary\n\nsummary(model, input_size=inputs.size()[1:], batch_size=-1, device='cpu')","metadata":{"execution":{"iopub.status.busy":"2024-03-17T15:37:15.578446Z","iopub.execute_input":"2024-03-17T15:37:15.578745Z","iopub.status.idle":"2024-03-17T15:37:15.614125Z","shell.execute_reply.started":"2024-03-17T15:37:15.578703Z","shell.execute_reply":"2024-03-17T15:37:15.613166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"names=[]\nfor name,_ in list(model.named_parameters()):\n    names.append(name)\nnames[:5]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"        \"\"\"def vae_r_loss(y_true, y_pred):\n            r_loss = K.mean(K.square(y_true - y_pred), axis = [1,2,3])\n            return r_loss_factor * r_loss\n\n        def vae_kl_loss(y_true, y_pred):\n            kl_loss =  -0.5 * K.sum(1 + self.log_var - K.square(self.mu) - K.exp(self.log_var), axis = 1)\n            return kl_loss\n\n        def vae_loss(y_true, y_pred):\n            r_loss = vae_r_loss(y_true, y_pred)\n            kl_loss = vae_kl_loss(y_true, y_pred)\n            return  r_loss + kl_loss\"\"\"\nR_LOSS_FACTOR = 10000\ndef rmse_loss(recon,x):\n    return torch.mean(torch.pow(recon-x,2),dim=(1,2,3))\ndef rmse_loss1(recon,x):\n    return torch.mean(torch.pow(recon-x,2))\ndef kl_loss1(mu,log_var):\n    return -0.5 * torch.sum(1+log_var-torch.exp(log_var)-mu**2,axis=1)\ndef kl_loss(mu,log_var):\n    return -0.5*torch.sum(1+log_var-torch.pow(mu,2)-torch.exp(log_var),axis=1)\ndef vae_loss(recon, x, mu, log_var):\n    return torch.sum(rmse_loss(recon,x)*R_LOSS_FACTOR+kl_loss(mu,log_var))\ncriterion=vae_loss\ndevice=torch.device('cpu' if torch.cuda.is_available() else 'cpu')\ninputs=inputs.to(device)\nmodel=model.to(device)\nx,mu,log_var=model(inputs)\ntorch.sum(rmse_loss(inputs,x)),kl_loss(mu,log_var),kl_loss1(mu,log_var),rmse_loss1(inputs,x)*32","metadata":{"execution":{"iopub.status.busy":"2024-03-18T01:00:23.519747Z","iopub.execute_input":"2024-03-18T01:00:23.520035Z","iopub.status.idle":"2024-03-18T01:00:23.863134Z","shell.execute_reply.started":"2024-03-18T01:00:23.519991Z","shell.execute_reply":"2024-03-18T01:00:23.862204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#模型训练\n\n#torch.backends.cudnn.benchmark=True\ndevice=torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')\nprint('device：',device)\nnet=VAE(200).to(device)\ncriterion=vae_loss\noptimizer=optim.Adam(params=net.parameters(),lr=0.0005)\n\nnum_epochs=5\n\nfor epoch in range(num_epochs):\n    print('Epoch {}/{}'.format(epoch+1,num_epochs))\n    print('-------------------------')\n\n    for phase in ['train','test']:\n        if phase=='train':\n            net.train()\n        else:\n            net.eval()\n\n        epoch_loss=0\n        epoch_kl=0\n\n        for inputs in tqdm(dataloaders_dict[phase]):\n            inputs=inputs.to(device)\n            optimizer.zero_grad()\n            with torch.set_grad_enabled(phase=='train'):\n                #print(inputs.device)\n                #inputs.\n                recon,mu,log_var=net(inputs)\n                #print(recon.device,mu.device,inputs.device)\n                \n                loss=criterion(recon,inputs, mu, log_var)\n                epoch_kl+=torch.sum(kl_loss(mu,log_var))\n                #print(loss.item(),kl_loss(mu,log_var).item())\n                epoch_loss+=loss\n                #print(loss)\n                if phase=='train':\n                    loss.backward()\n                    optimizer.step()\n\n                \n        epoch_loss=epoch_loss/len(dataloaders_dict[phase].dataset)\n        epoch_kl=epoch_kl/len(dataloaders_dict[phase].dataset)\n        print('{} Loss:{:.4f} kl_loss:{:.4f} '.format(phase,epoch_loss,epoch_kl))\n\n","metadata":{"execution":{"iopub.status.busy":"2024-03-18T01:01:03.938063Z","iopub.execute_input":"2024-03-18T01:01:03.938388Z","iopub.status.idle":"2024-03-18T02:10:22.402217Z","shell.execute_reply.started":"2024-03-18T01:01:03.938340Z","shell.execute_reply":"2024-03-18T02:10:22.401064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def predict(net,z):\n \n    x = z\n        \n    x=net.fc2(x)\n    x=x.reshape((-1, 64, 8, 8))\n    for i in range(len(net.decoder_convs)):\n            convs=net.decoder_convs[i]\n            x=convs(x)\n            if(i<len(net.decoder_convs)-1):\n                BN=net.decoder_batchnorm[i]\n                x = BN(x)\n                x = F.leaky_relu(x)\n                x = nn.Dropout(0.25)(x)\n            else:\n                x=torch.sigmoid(x)\n    return x","metadata":{"execution":{"iopub.status.busy":"2024-03-18T02:10:24.808764Z","iopub.execute_input":"2024-03-18T02:10:24.809061Z","iopub.status.idle":"2024-03-18T02:10:24.816953Z","shell.execute_reply.started":"2024-03-18T02:10:24.809018Z","shell.execute_reply":"2024-03-18T02:10:24.816249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n_to_show = 10\n\n\nfrom torch.utils.data import WeightedRandomSampler,DataLoader\n\ndataloader = DataLoader(dataset=test_dataset,\n                    batch_size=32,  # 组织成batch大小\n                    sampler=WeightedRandomSampler(torch.ones(len(test_dataset)), 32))\n\nexample_batch=next(iter(dataloader))\nexample_images = example_batch.to(device)\nnet.eval()\nreconst_images,mu,log_var=net(example_images)\n\n\nfig = plt.figure(figsize=(15, 3))\nfig.subplots_adjust(hspace=0.4, wspace=0.4)\n\nfor i in range(n_to_show):\n    img = example_images[i].squeeze()\n    sub = fig.add_subplot(2, n_to_show, i+1)\n    sub.axis('off')        \n    sub.imshow(img.cpu().numpy().transpose((1,2,0)))\n\nfor i in range(n_to_show):\n    img = reconst_images[i].squeeze()\n    sub = fig.add_subplot(2, n_to_show, i+n_to_show+1)\n    sub.axis('off')\n    sub.imshow(img.detach().cpu().numpy().transpose((1,2,0)))\nfig.savefig('2.png')","metadata":{"execution":{"iopub.status.busy":"2024-03-18T02:32:45.175210Z","iopub.execute_input":"2024-03-18T02:32:45.175567Z","iopub.status.idle":"2024-03-18T02:32:46.443899Z","shell.execute_reply.started":"2024-03-18T02:32:45.175504Z","shell.execute_reply":"2024-03-18T02:32:46.443040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n_to_show = 30\n\nznew = np.random.normal(size = (n_to_show,net.z_dim))\n\nreconst = predict(net,torch.randn_like(mu))\n\nfig = plt.figure(figsize=(18, 5))\nfig.subplots_adjust(hspace=0.4, wspace=0.4)\nfor i in range(n_to_show):\n    img = reconst[i].squeeze()\n    ax = fig.add_subplot(3, 10, i+1)\n    ax.imshow(img.detach().cpu().numpy().transpose((1,2,0)))\n    ax.axis('off')\n\nplt.show()\nfig.savefig('1.png')","metadata":{"execution":{"iopub.status.busy":"2024-03-18T02:20:09.929138Z","iopub.execute_input":"2024-03-18T02:20:09.929431Z","iopub.status.idle":"2024-03-18T02:20:11.500716Z","shell.execute_reply.started":"2024-03-18T02:20:09.929386Z","shell.execute_reply":"2024-03-18T02:20:11.500041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## celeba数据集上训练VAE（tensorflow）","metadata":{}},{"cell_type":"code","source":"from keras.callbacks import Callback, LearningRateScheduler\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport os\n\n#### CALLBACKS\nclass CustomCallback(Callback):\n    \n    def __init__(self, run_folder, print_every_n_batches, initial_epoch, vae):\n        self.epoch = initial_epoch\n        self.run_folder = run_folder\n        self.print_every_n_batches = print_every_n_batches\n        self.vae = vae\n\n    def on_batch_end(self, batch, logs={}):  \n        if batch % self.print_every_n_batches == 0:\n            z_new = np.random.normal(size = (1,self.vae.z_dim))\n            reconst = self.vae.decoder.predict(np.array(z_new))[0].squeeze()\n\n            filepath = os.path.join(self.run_folder, 'images', 'img_' + str(self.epoch).zfill(3) + '_' + str(batch) + '.jpg')\n            if len(reconst.shape) == 2:\n                plt.imsave(filepath, reconst, cmap='gray_r')\n            else:\n                plt.imsave(filepath, reconst)\n\n    def on_epoch_begin(self, epoch, logs={}):\n        self.epoch += 1\n\n\n\ndef step_decay_schedule(initial_lr, decay_factor=0.5, step_size=1):\n    '''\n    Wrapper function to create a LearningRateScheduler with step decay schedule.\n    '''\n    def schedule(epoch):\n        new_lr = initial_lr * (decay_factor ** np.floor(epoch/step_size))\n        \n        return new_lr\n\n    return LearningRateScheduler(schedule)","metadata":{"execution":{"iopub.status.busy":"2024-03-17T15:03:24.511678Z","iopub.execute_input":"2024-03-17T15:03:24.512026Z","iopub.status.idle":"2024-03-17T15:03:30.466062Z","shell.execute_reply.started":"2024-03-17T15:03:24.511962Z","shell.execute_reply":"2024-03-17T15:03:30.465452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfrom keras.layers import Input, Conv2D, Flatten, Dense, Conv2DTranspose, Reshape, Lambda, Activation, BatchNormalization, LeakyReLU, Dropout\nfrom keras.models import Model\nfrom keras import backend as K\nfrom keras.optimizers import Adam\nfrom keras.callbacks import ModelCheckpoint \nfrom keras.utils import plot_model\n\n\n\nimport numpy as np\nimport json\nimport os\nimport pickle\n\n\nclass VariationalAutoencoder():\n    def __init__(self\n        , input_dim\n        , encoder_conv_filters\n        , encoder_conv_kernel_size\n        , encoder_conv_strides\n        , decoder_conv_t_filters\n        , decoder_conv_t_kernel_size\n        , decoder_conv_t_strides\n        , z_dim\n        , use_batch_norm = False\n        , use_dropout= False\n        ):\n\n        self.name = 'variational_autoencoder'\n\n        self.input_dim = input_dim\n        self.encoder_conv_filters = encoder_conv_filters\n        self.encoder_conv_kernel_size = encoder_conv_kernel_size\n        self.encoder_conv_strides = encoder_conv_strides\n        self.decoder_conv_t_filters = decoder_conv_t_filters\n        self.decoder_conv_t_kernel_size = decoder_conv_t_kernel_size\n        self.decoder_conv_t_strides = decoder_conv_t_strides\n        self.z_dim = z_dim\n\n        self.use_batch_norm = use_batch_norm\n        self.use_dropout = use_dropout\n\n        self.n_layers_encoder = len(encoder_conv_filters)\n        self.n_layers_decoder = len(decoder_conv_t_filters)\n\n        self._build()\n\n    def _build(self):\n        \n        ### THE ENCODER\n        encoder_input = Input(shape=self.input_dim, name='encoder_input')\n\n        x = encoder_input\n\n        for i in range(self.n_layers_encoder):\n            conv_layer = Conv2D(\n                filters = self.encoder_conv_filters[i]\n                , kernel_size = self.encoder_conv_kernel_size[i]\n                , strides = self.encoder_conv_strides[i]\n                , padding = 'same'\n                , name = 'encoder_conv_' + str(i)\n                )\n\n            x = conv_layer(x)\n\n            if self.use_batch_norm:\n                x = BatchNormalization()(x)\n\n            x = LeakyReLU()(x)\n\n            if self.use_dropout:\n                x = Dropout(rate = 0.25)(x)\n\n        shape_before_flattening = K.int_shape(x)[1:]\n\n        x = Flatten()(x)\n        self.mu = Dense(self.z_dim, name='mu')(x)\n        self.log_var = Dense(self.z_dim, name='log_var')(x)\n\n        self.encoder_mu_log_var = Model(encoder_input, (self.mu, self.log_var))\n\n        def sampling(args):\n            mu, log_var = args\n            epsilon = K.random_normal(shape=K.shape(mu), mean=0., stddev=1.)\n            return mu + K.exp(log_var / 2) * epsilon\n\n        encoder_output = Lambda(sampling, name='encoder_output')([self.mu, self.log_var])\n\n        self.encoder = Model(encoder_input, encoder_output)\n        \n        \n\n        ### THE DECODER\n\n        decoder_input = Input(shape=(self.z_dim,), name='decoder_input')\n\n        x = Dense(np.prod(shape_before_flattening))(decoder_input)\n        x = Reshape(shape_before_flattening)(x)\n\n        for i in range(self.n_layers_decoder):\n            conv_t_layer = Conv2DTranspose(\n                filters = self.decoder_conv_t_filters[i]\n                , kernel_size = self.decoder_conv_t_kernel_size[i]\n                , strides = self.decoder_conv_t_strides[i]\n                , padding = 'same'\n                , name = 'decoder_conv_t_' + str(i)\n                )\n\n            x = conv_t_layer(x)\n\n            if i < self.n_layers_decoder - 1:\n                if self.use_batch_norm:\n                    x = BatchNormalization()(x)\n                x = LeakyReLU()(x)\n                if self.use_dropout:\n                    x = Dropout(rate = 0.25)(x)\n            else:\n                x = Activation('sigmoid')(x)\n\n            \n\n        decoder_output = x\n\n        self.decoder = Model(decoder_input, decoder_output)\n\n        ### THE FULL VAE\n        model_input = encoder_input\n        model_output = self.decoder(encoder_output)\n\n        self.model = Model(model_input, model_output)\n\n\n    def compile(self, learning_rate, r_loss_factor):\n        self.learning_rate = learning_rate\n\n        ### COMPILATION\n        def vae_r_loss(y_true, y_pred):\n            r_loss = K.mean(K.square(y_true - y_pred), axis = [1,2,3])\n            return r_loss_factor * r_loss\n\n        def vae_kl_loss(y_true, y_pred):\n            kl_loss =  -0.5 * K.sum(1 + self.log_var - K.square(self.mu) - K.exp(self.log_var), axis = 1)\n            return kl_loss\n\n        def vae_loss(y_true, y_pred):\n            r_loss = vae_r_loss(y_true, y_pred)\n            kl_loss = vae_kl_loss(y_true, y_pred)\n            return  r_loss + kl_loss\n\n        optimizer = Adam(lr=learning_rate)\n        self.model.compile(optimizer=optimizer, loss = vae_loss,  metrics = [vae_r_loss, vae_kl_loss])\n\n\n    def save(self, folder):\n\n        if not os.path.exists(folder):\n            os.makedirs(folder)\n            os.makedirs(os.path.join(folder, 'viz'))\n            os.makedirs(os.path.join(folder, 'weights'))\n            os.makedirs(os.path.join(folder, 'images'))\n\n        with open(os.path.join(folder, 'params.pkl'), 'wb') as f:\n            pickle.dump([\n                self.input_dim\n                , self.encoder_conv_filters\n                , self.encoder_conv_kernel_size\n                , self.encoder_conv_strides\n                , self.decoder_conv_t_filters\n                , self.decoder_conv_t_kernel_size\n                , self.decoder_conv_t_strides\n                , self.z_dim\n                , self.use_batch_norm\n                , self.use_dropout\n                ], f)\n\n        self.plot_model(folder)\n\n\n    def load_weights(self, filepath):\n        self.model.load_weights(filepath)\n\n    def train(self, x_train, batch_size, epochs, run_folder, print_every_n_batches = 100, initial_epoch = 0, lr_decay = 1):\n\n        custom_callback = CustomCallback(run_folder, print_every_n_batches, initial_epoch, self)\n        lr_sched = step_decay_schedule(initial_lr=self.learning_rate, decay_factor=lr_decay, step_size=1)\n        \n        checkpoint_filepath=os.path.join(run_folder, \"weights/weights-{epoch:03d}-{loss:.2f}.h5\")\n        checkpoint1 = ModelCheckpoint(checkpoint_filepath, save_weights_only = True, verbose=1)\n        checkpoint2 = ModelCheckpoint(os.path.join(run_folder, 'weights/weights.h5'), save_weights_only = True, verbose=1)\n\n        callbacks_list = [checkpoint1, checkpoint2, custom_callback, lr_sched]\n\n        self.model.fit(     \n            x_train\n            , x_train\n            , batch_size = batch_size\n            , shuffle = True\n            , epochs = epochs\n            , initial_epoch = initial_epoch\n            , callbacks = callbacks_list\n        )\n\n\n\n    def train_with_generator(self, data_flow, epochs, steps_per_epoch, run_folder, print_every_n_batches = 100, initial_epoch = 0, lr_decay = 1, ):\n\n        custom_callback = CustomCallback(run_folder, print_every_n_batches, initial_epoch, self)\n        lr_sched = step_decay_schedule(initial_lr=self.learning_rate, decay_factor=lr_decay, step_size=1)\n\n        checkpoint_filepath=os.path.join(run_folder, \"weights/weights-{epoch:03d}-{loss:.2f}.h5\")\n        checkpoint1 = ModelCheckpoint(checkpoint_filepath, save_weights_only = True, verbose=1)\n        checkpoint2 = ModelCheckpoint(os.path.join(run_folder, 'weights/weights.h5'), save_weights_only = True, verbose=1)\n\n        callbacks_list = [checkpoint1, checkpoint2, custom_callback, lr_sched]\n\n        self.model.save_weights(os.path.join(run_folder, 'weights/weights.h5'))\n                \n        self.model.fit_generator(\n            data_flow\n            , shuffle = True\n            , epochs = epochs\n            , initial_epoch = initial_epoch\n            , callbacks = callbacks_list\n            , steps_per_epoch=steps_per_epoch \n            )\n\n\n    \n    def plot_model(self, run_folder):\n        plot_model(self.model, to_file=os.path.join(run_folder ,'viz/model.png'), show_shapes = True, show_layer_names = True)\n        plot_model(self.encoder, to_file=os.path.join(run_folder ,'viz/encoder.png'), show_shapes = True, show_layer_names = True)\n        plot_model(self.decoder, to_file=os.path.join(run_folder ,'viz/decoder.png'), show_shapes = True, show_layer_names = True)\n\n\n\n        \n\n\n        \n\n        \n\n\n","metadata":{"execution":{"iopub.status.busy":"2024-03-17T15:03:33.154529Z","iopub.execute_input":"2024-03-17T15:03:33.154865Z","iopub.status.idle":"2024-03-17T15:03:33.209036Z","shell.execute_reply.started":"2024-03-17T15:03:33.154805Z","shell.execute_reply":"2024-03-17T15:03:33.208241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir vae","metadata":{"execution":{"iopub.status.busy":"2024-03-17T15:03:43.224740Z","iopub.execute_input":"2024-03-17T15:03:43.225085Z","iopub.status.idle":"2024-03-17T15:03:44.226259Z","shell.execute_reply.started":"2024-03-17T15:03:43.225028Z","shell.execute_reply":"2024-03-17T15:03:44.225359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd /kaggle/working/vae","metadata":{"execution":{"iopub.status.busy":"2024-03-17T15:03:46.811142Z","iopub.execute_input":"2024-03-17T15:03:46.811483Z","iopub.status.idle":"2024-03-17T15:03:46.817379Z","shell.execute_reply.started":"2024-03-17T15:03:46.811435Z","shell.execute_reply":"2024-03-17T15:03:46.816632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir run","metadata":{"execution":{"iopub.status.busy":"2024-03-17T15:03:49.414887Z","iopub.execute_input":"2024-03-17T15:03:49.415222Z","iopub.status.idle":"2024-03-17T15:03:50.412790Z","shell.execute_reply.started":"2024-03-17T15:03:49.415166Z","shell.execute_reply":"2024-03-17T15:03:50.411823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd run","metadata":{"execution":{"iopub.status.busy":"2024-03-17T15:03:52.394961Z","iopub.execute_input":"2024-03-17T15:03:52.395289Z","iopub.status.idle":"2024-03-17T15:03:52.400972Z","shell.execute_reply.started":"2024-03-17T15:03:52.395243Z","shell.execute_reply":"2024-03-17T15:03:52.400111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir vae","metadata":{"execution":{"iopub.status.busy":"2024-03-17T15:03:55.117278Z","iopub.execute_input":"2024-03-17T15:03:55.117635Z","iopub.status.idle":"2024-03-17T15:03:56.112746Z","shell.execute_reply.started":"2024-03-17T15:03:55.117570Z","shell.execute_reply":"2024-03-17T15:03:56.111517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd ..","metadata":{"execution":{"iopub.status.busy":"2024-03-17T15:03:58.070007Z","iopub.execute_input":"2024-03-17T15:03:58.070388Z","iopub.status.idle":"2024-03-17T15:03:58.076617Z","shell.execute_reply.started":"2024-03-17T15:03:58.070309Z","shell.execute_reply":"2024-03-17T15:03:58.075719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# run params\nsection = 'vae'\nrun_id = '0001'\ndata_name = 'faces'\nRUN_FOLDER = 'run/{}/'.format(section)\nRUN_FOLDER += '_'.join([run_id, data_name])\n\nif not os.path.exists(RUN_FOLDER):\n    os.mkdir(RUN_FOLDER)\n    os.mkdir(os.path.join(RUN_FOLDER, 'viz'))\n    os.mkdir(os.path.join(RUN_FOLDER, 'images'))\n    os.mkdir(os.path.join(RUN_FOLDER, 'weights'))\n\nmode =  'build' #'load' #\n\n\nDATA_FOLDER = '/kaggle/input/celebrity-face-image-dataset/Celebrity Faces Dataset'","metadata":{"execution":{"iopub.status.busy":"2024-03-17T15:04:00.719532Z","iopub.execute_input":"2024-03-17T15:04:00.720027Z","iopub.status.idle":"2024-03-17T15:04:00.728040Z","shell.execute_reply.started":"2024-03-17T15:04:00.719912Z","shell.execute_reply":"2024-03-17T15:04:00.726987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import glob\nINPUT_DIM = (128,128,3)\nBATCH_SIZE = 32\n\nfilenames = np.array(glob.glob(os.path.join(DATA_FOLDER, '**/*.jpg')))\n\nlen(filenames)","metadata":{"execution":{"iopub.status.busy":"2024-03-17T15:04:04.625576Z","iopub.execute_input":"2024-03-17T15:04:04.625910Z","iopub.status.idle":"2024-03-17T15:04:04.658386Z","shell.execute_reply.started":"2024-03-17T15:04:04.625850Z","shell.execute_reply":"2024-03-17T15:04:04.657688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\ndata_gen = ImageDataGenerator(rescale=1./255)\n\ndata_flow = data_gen.flow_from_directory(DATA_FOLDER\n                                         , target_size = INPUT_DIM[:2]\n                                         , batch_size = BATCH_SIZE\n                                         , shuffle = True\n                                         , class_mode = 'input'\n                                         , subset = \"training\"\n                                            )","metadata":{"execution":{"iopub.status.busy":"2024-03-17T15:04:08.935941Z","iopub.execute_input":"2024-03-17T15:04:08.936279Z","iopub.status.idle":"2024-03-17T15:04:09.049302Z","shell.execute_reply.started":"2024-03-17T15:04:08.936218Z","shell.execute_reply":"2024-03-17T15:04:09.048320Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vae = VariationalAutoencoder(\n                input_dim = INPUT_DIM\n                , encoder_conv_filters=[32,64,64, 64]\n                , encoder_conv_kernel_size=[3,3,3,3]\n                , encoder_conv_strides=[2,2,2,2]\n                , decoder_conv_t_filters=[64,64,32,3]\n                , decoder_conv_t_kernel_size=[3,3,3,3]\n                , decoder_conv_t_strides=[2,2,2,2]\n                , z_dim=200\n                , use_batch_norm=True\n                , use_dropout=True)\n\nif mode == 'build':\n    vae.save(RUN_FOLDER)\nelse:\n    vae.load_weights(os.path.join(RUN_FOLDER, 'weights/weights.h5'))","metadata":{"execution":{"iopub.status.busy":"2024-03-17T15:04:19.788608Z","iopub.execute_input":"2024-03-17T15:04:19.788953Z","iopub.status.idle":"2024-03-17T15:04:25.042505Z","shell.execute_reply.started":"2024-03-17T15:04:19.788894Z","shell.execute_reply":"2024-03-17T15:04:25.041469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vae.encoder.summary()","metadata":{"execution":{"iopub.status.busy":"2024-03-17T13:21:12.825791Z","iopub.execute_input":"2024-03-17T13:21:12.826152Z","iopub.status.idle":"2024-03-17T13:21:12.835346Z","shell.execute_reply.started":"2024-03-17T13:21:12.826086Z","shell.execute_reply":"2024-03-17T13:21:12.834618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"LEARNING_RATE = 0.0005\nR_LOSS_FACTOR = 10000\nEPOCHS = 5\nPRINT_EVERY_N_BATCHES = 100\nINITIAL_EPOCH = 0","metadata":{"execution":{"iopub.status.busy":"2024-03-17T15:04:27.201220Z","iopub.execute_input":"2024-03-17T15:04:27.201595Z","iopub.status.idle":"2024-03-17T15:04:27.207803Z","shell.execute_reply.started":"2024-03-17T15:04:27.201542Z","shell.execute_reply":"2024-03-17T15:04:27.206868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vae.compile(LEARNING_RATE, R_LOSS_FACTOR)","metadata":{"execution":{"iopub.status.busy":"2024-03-17T15:04:29.426386Z","iopub.execute_input":"2024-03-17T15:04:29.426724Z","iopub.status.idle":"2024-03-17T15:04:29.480837Z","shell.execute_reply.started":"2024-03-17T15:04:29.426667Z","shell.execute_reply":"2024-03-17T15:04:29.479905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"NUM_IMAGES = len(filenames)\nvae.train_with_generator(     \n    data_flow\n    , epochs = EPOCHS\n    , steps_per_epoch = NUM_IMAGES / BATCH_SIZE\n    , run_folder = RUN_FOLDER\n    , print_every_n_batches = PRINT_EVERY_N_BATCHES\n    , initial_epoch = INITIAL_EPOCH\n)","metadata":{"execution":{"iopub.status.busy":"2024-03-17T15:04:31.491969Z","iopub.execute_input":"2024-03-17T15:04:31.492307Z","iopub.status.idle":"2024-03-17T15:05:16.719996Z","shell.execute_reply.started":"2024-03-17T15:04:31.492248Z","shell.execute_reply":"2024-03-17T15:05:16.718959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n_to_show = 10\n\n#data_flow_generic = imageLoader.build(att, n_to_show)\n\nexample_batch = next(data_flow)\nexample_images = example_batch[0]\n\nz_points = vae.encoder.predict(example_images)\n\nreconst_images = vae.decoder.predict(z_points)\n\nfig = plt.figure(figsize=(15, 3))\nfig.subplots_adjust(hspace=0.4, wspace=0.4)\n\nfor i in range(n_to_show):\n    img = example_images[i].squeeze()\n    sub = fig.add_subplot(2, n_to_show, i+1)\n    sub.axis('off')        \n    sub.imshow(img)\n\nfor i in range(n_to_show):\n    img = reconst_images[i].squeeze()\n    sub = fig.add_subplot(2, n_to_show, i+n_to_show+1)\n    sub.axis('off')\n    sub.imshow(img)\n","metadata":{"execution":{"iopub.status.busy":"2024-03-17T15:30:30.421557Z","iopub.execute_input":"2024-03-17T15:30:30.421858Z","iopub.status.idle":"2024-03-17T15:30:31.440918Z","shell.execute_reply.started":"2024-03-17T15:30:30.421822Z","shell.execute_reply":"2024-03-17T15:30:31.440202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"example_images[0].mean()","metadata":{"execution":{"iopub.status.busy":"2024-03-17T15:30:36.669196Z","iopub.execute_input":"2024-03-17T15:30:36.669525Z","iopub.status.idle":"2024-03-17T15:30:36.675540Z","shell.execute_reply.started":"2024-03-17T15:30:36.669477Z","shell.execute_reply":"2024-03-17T15:30:36.674702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"reconst_images[0].mean()","metadata":{"execution":{"iopub.status.busy":"2024-03-17T15:30:47.418823Z","iopub.execute_input":"2024-03-17T15:30:47.419157Z","iopub.status.idle":"2024-03-17T15:30:47.425079Z","shell.execute_reply.started":"2024-03-17T15:30:47.419100Z","shell.execute_reply":"2024-03-17T15:30:47.424263Z"},"trusted":true},"execution_count":null,"outputs":[]}]}