{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import cv2\n",
    "import matplotlib.pyplot as plt\n",
    "import torch\n",
    "import random\n",
    "import numpy as np\n",
    "\n",
    "from model import Generator\n",
    "\n",
    "def load_image(path, size=None):\n",
    "    image = image2tensor(cv2.cvtColor(cv2.imread(path), cv2.COLOR_BGR2RGB))\n",
    "\n",
    "    w, h = image.shape[-2:]\n",
    "    if w != h:\n",
    "        crop_size = min(w, h)\n",
    "        left = (w - crop_size)//2\n",
    "        right = left + crop_size\n",
    "        top = (h - crop_size)//2\n",
    "        bottom = top + crop_size\n",
    "        image = image[:,:,left:right, top:bottom]\n",
    "\n",
    "    if size is not None and image.shape[-1] != size:\n",
    "        image = torch.nn.functional.interpolate(image, (size, size), mode=\"bilinear\", align_corners=True)\n",
    "    \n",
    "    return image\n",
    "\n",
    "def image2tensor(image):\n",
    "    image = torch.FloatTensor(image).permute(2,0,1).unsqueeze(0)/255.\n",
    "    return (image-0.5)/0.5\n",
    "\n",
    "def tensor2image(tensor):\n",
    "    tensor = tensor.clamp_(-1., 1.).detach().squeeze().permute(1,2,0).cpu().numpy()\n",
    "    return tensor*0.5 + 0.5\n",
    "\n",
    "def imshow(img, size=5, cmap='jet'):\n",
    "    plt.figure(figsize=(size,size))\n",
    "    plt.imshow(img, cmap=cmap)\n",
    "    plt.axis('off')\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 1317276 characters]\n",
      "text/plain": [
       "<Figure size 2880x2880 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "device = 'cpu'\n",
    "torch.set_grad_enabled(False)\n",
    "image_size = 300 # Can be tuned, works best when the face width is between 200~250 px\n",
    "\n",
    "model = Generator().eval().to(device)\n",
    "\n",
    "ckpt = torch.load(f\"checkpoint/generator_celeba_distill.pt\", map_location=device)\n",
    "model.load_state_dict(ckpt)\n",
    "    \n",
    "results = []\n",
    "for j in range(1,17):\n",
    "    image = load_image(f\"samples/faces/{j}.jpg\", image_size)\n",
    "    output = model(image.to(device))\n",
    "\n",
    "    results.append(torch.cat([image, output.cpu()], 3))\n",
    "results = torch.cat(results, 2)\n",
    "\n",
    "imshow(tensor2image(results),40)\n",
    "cv2.imwrite('./samples/face_results.jpg', cv2.cvtColor(255*tensor2image(results), cv2.COLOR_BGR2RGB))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "torch171",
   "language": "python",
   "name": "torch171"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
