File size: 5,094 Bytes
9bf40bb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
{
"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
}
|