{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "metadata": {}
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2024-06-21 21:22:32,580 - INFO - Loading model from /home/andras/projects/bipolaroid/models/run_39\n",
      "2024-06-21 21:22:32,580 - INFO - Hyperparameters: {'batch_size': 64, 'edit_count': 12, 'bin_count': 16, 'clip_gradients': False, 'learning_rate': 0.0012057557955036278, 'scheduler_gamma': 0.7598365808426665, 'num_epochs': 24, 'model_type': 'Residual', 'loss': 'kl'}\n",
      "2024-06-21 21:22:33,440 - INFO - Loaded 2498 original images\n"
     ]
    }
   ],
   "source": [
    "from editor.utils import set_up_logging\n",
    "\n",
    "set_up_logging()\n",
    "\n",
    "import torch\n",
    "from editor.training import random_edit, HistogramDataset\n",
    "from editor.visualisation import display_images\n",
    "from editor.models import load_model\n",
    "from config import MODELS_PATH, TEST_DATA\n",
    "\n",
    "device = torch.device(\"cuda:0\") if torch.cuda.is_available() else torch.device(\"cpu\")\n",
    "model, hyperparameters = load_model(MODELS_PATH / \"run_39\", device)\n",
    "dataset = HistogramDataset(\n",
    "    TEST_DATA,\n",
    "    edit_count=hyperparameters[\"edit_count\"],\n",
    "    bin_count=hyperparameters[\"bin_count\"],\n",
    "    target_size=(640, 640),\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "img = dataset.get_original_image(1008)\n",
    "\n",
    "edits = {\"Original\": img}\n",
    "edits.update({f\"Edit {i}\": random_edit(img.copy(), seed=i) for i in range(1, 36)})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 7118300 characters]",
      "text/plain": [
       "<Figure size 2400x1600 with 36 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display_images(edits, images_per_row=6, figsize=(24, 16))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy\n",
    "from editor.utils import compute_histogram\n",
    "\n",
    "\n",
    "inputs = (\n",
    "    torch.tensor(\n",
    "        numpy.array(\n",
    "            [\n",
    "                compute_histogram(\n",
    "                    image, bins=hyperparameters[\"bin_count\"], normalize=True\n",
    "                )\n",
    "                for image in edits.values()\n",
    "            ]\n",
    "        ),\n",
    "        dtype=torch.float,\n",
    "    ).unsqueeze(1)\n",
    ").to(device)\n",
    "\n",
    "outputs = model(inputs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 7296752 characters]",
      "text/plain": [
       "<Figure size 2400x1600 with 36 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from editor.histogram_transfer import apply_histogram\n",
    "from PIL import Image\n",
    "\n",
    "\n",
    "predicted = {\n",
    "    f\"Predicted {i}\": Image.fromarray(\n",
    "        apply_histogram(image, histogram, hyperparameters[\"bin_count\"])\n",
    "    )\n",
    "    for i, (image, histogram) in enumerate(zip(edits.values(), outputs))\n",
    "}\n",
    "display_images(predicted, images_per_row=6, figsize=(24, 16))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "bipolaroid",
   "language": "python",
   "name": "python3"
  },
  "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.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
