{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from skimage.metrics import structural_similarity\n", "import numpy as np\n", "import natsort\n", "import os\n", "import cv2" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "def load_filename(path):\n", " dirFiles = os.listdir(path)\n", " for i, file in enumerate(dirFiles):\n", " dirFiles[i] = path + file\n", " return natsort.natsorted(dirFiles ,reverse=False)\n", "\n", "def load_images(list_path):\n", " img_list = list()\n", " for filename in list_path:\n", " pixels = cv2.imread(filename)\n", " img_list.append(pixels)\n", " return np.asarray(img_list)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "def compute_l2(imgs1, imgs2):\n", " l2_scores = []\n", " for i in range(len(imgs1)):\n", " score = (np.square(imgs1[i] - imgs2[i])).mean()\n", " l2_scores.append(score)\n", " return np.mean(l2_scores)\n", "\n", "def compute_ssim(imgs1, imgs2):\n", " ssim_scores = []\n", " for i in range(len(imgs1)):\n", " grayA = cv2.cvtColor(imgs1[i], cv2.COLOR_BGR2GRAY)\n", " grayB = cv2.cvtColor(imgs2[i], cv2.COLOR_BGR2GRAY)\n", " (score, diff) = structural_similarity(grayA, grayB, full=True)\n", " ssim_scores.append(score)\n", " return np.mean(score)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Compute L2-norm and SSIM" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Pixel loss weight : 1 - Contextual loss weight : 0 => L2-norm: 90.68295599999999 :: SSIM: 0.7858032359073482\n" ] } ], "source": [ "imgs1 = load_images(load_filename(\"Dataset/CUHK/Testing photo/\"))\n", "imgs2 = load_images(load_filename(\"Generated Images/Generated_Pixel[1]_Context[0]/\"))\n", "\n", "l2 = compute_l2(imgs1, imgs2)\n", "ssim = compute_ssim(imgs1, imgs2)\n", "\n", "print(\"Pixel loss weight : 1 - Contextual loss weight : 0 => L2-norm: \" + str(l2) + \" :: SSIM: \" + str(ssim))" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Pixel loss weight : 0.8 - Contextual loss weight : 0.2 => L2-norm: 92.38721946666665 :: SSIM: 0.7712359671590553\n" ] } ], "source": [ "imgs1 = load_images(load_filename(\"Dataset/CUHK/Testing photo/\"))\n", "imgs2 = load_images(load_filename(\"Generated Images/Generated_Pixel[08]_Context[02]/\"))\n", "\n", "l2 = compute_l2(imgs1, imgs2)\n", "ssim = compute_ssim(imgs1, imgs2)\n", "\n", "print(\"Pixel loss weight : 0.8 - Contextual loss weight : 0.2 => L2-norm: \" + str(l2) + \" :: SSIM: \" + str(ssim))" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Pixel loss weight : 0.5 - Contextual loss weight : 0.5 => L2-norm: 93.4520664666667 :: SSIM: 0.7266711360066652\n" ] } ], "source": [ "imgs1 = load_images(load_filename(\"Dataset/CUHK/Testing photo/\"))\n", "imgs2 = load_images(load_filename(\"Generated Images/Generated_Pixel[05]_Context[05]/\"))\n", "\n", "l2 = compute_l2(imgs1, imgs2)\n", "ssim = compute_ssim(imgs1, imgs2)\n", "\n", "print(\"Pixel loss weight : 0.5 - Contextual loss weight : 0.5 => L2-norm: \" + str(l2) + \" :: SSIM: \" + str(ssim))" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Pixel loss weight : 0.2 - Contextual loss weight : 0.8 => L2-norm: 93.62968573333333 :: SSIM: 0.76595402890904\n" ] } ], "source": [ "imgs1 = load_images(load_filename(\"Dataset/CUHK/Testing photo/\"))\n", "imgs2 = load_images(load_filename(\"Generated Images/Generated_Pixel[02]_Context[08]/\"))\n", "\n", "l2 = compute_l2(imgs1, imgs2)\n", "ssim = compute_ssim(imgs1, imgs2)\n", "\n", "print(\"Pixel loss weight : 0.2 - Contextual loss weight : 0.8 => L2-norm: \" + str(l2) + \" :: SSIM: \" + str(ssim))" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.6.7" } }, "nbformat": 4, "nbformat_minor": 2 }