Parcourir la source

Add of SV filters metric

Jérôme BUISINE il y a 4 ans
Parent
commit
5bedd5f24a

Fichier diff supprimé car celui-ci est trop grand
+ 508 - 0
analysis/lbp_analysis.ipynb


Fichier diff supprimé car celui-ci est trop grand
+ 28 - 266
analysis/statistic_analysis_svd.ipynb


+ 367 - 0
analysis/svd_filters_analysis.ipynb

@@ -0,0 +1,367 @@
+{
+ "cells": [
+  {
+   "cell_type": "code",
+   "execution_count": 2,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "from ipfml import processing\n",
+    "from ipfml import utils\n",
+    "from ipfml import metrics\n",
+    "from PIL import Image\n",
+    "from scipy import signal\n",
+    "from skimage import color\n",
+    "import scipy.stats as stats\n",
+    "import seaborn as sns\n",
+    "import cv2\n",
+    "import numpy as np\n",
+    "import matplotlib.pyplot as plt\n",
+    "import os\n",
+    "import math\n",
+    "\n",
+    "from scipy.signal import medfilt2d, wiener, cwt"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 3,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "data_folder = \"../fichiersSVD_light\""
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "# SVD filter analysis on zones of Synthesis Images "
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "## Utils functions definition"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 4,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "def compute_images_path(dict_data):\n",
+    "    scene = dict_data['name']\n",
+    "    prefix = dict_data['prefix']\n",
+    "    indices = dict_data['indices']\n",
+    "    \n",
+    "    images_path = []\n",
+    "    for index in indices:\n",
+    "        path = os.path.join(data_folder, os.path.join(scene, prefix + index + \".png\"))\n",
+    "        print(path)\n",
+    "        images_path.append(path)\n",
+    "    return images_path"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 5,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "def get_images_zones(dict_data, images_path):\n",
+    "    \n",
+    "    zones_indices = dict_data['zones']\n",
+    "    zones_img = []\n",
+    "    \n",
+    "    for path in images_path:\n",
+    "        img = Image.open(path)\n",
+    "        zones = processing.divide_in_blocks(img, (200, 200))\n",
+    "        \n",
+    "        zones_list = []\n",
+    "        \n",
+    "        for id_zone in zones_indices:\n",
+    "            zones_list.append(zones[id_zone])\n",
+    "            \n",
+    "        zones_img.append(zones_list)\n",
+    "        \n",
+    "    return zones_img"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 6,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "def display_sv_data(dict_data, zones_data, interval, _norm=False):\n",
+    "    \n",
+    "    scene_name = dict_data['name']\n",
+    "    image_indices = dict_data['indices']\n",
+    "    zones_indices = dict_data['zones']\n",
+    "    colors = ['C0', 'C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7', 'C8', 'C9']\n",
+    "    \n",
+    "    plt.figure(figsize=(25, 20))\n",
+    "    \n",
+    "    sv_data = []\n",
+    "    begin, end = interval\n",
+    "    for id_img, zones in enumerate(zones_data):\n",
+    "        \n",
+    "        for id_zone, zone in enumerate(zones):\n",
+    "            U, s, V = processing.get_LAB_L_SVD(zone)\n",
+    "        \n",
+    "            data = s[begin:end]\n",
+    "            \n",
+    "            if _norm:\n",
+    "                data = utils.normalize_arr(data)\n",
+    "                \n",
+    "            plt.plot(data, \n",
+    "                     color=colors[id_zone], \n",
+    "                     label='Zone ' + str(zones_indices[id_zone]) + ' of ' + scene_name + '_' + str(image_indices[id_img]))\n",
+    "            \n",
+    "    plt.legend(fontsize=18)\n",
+    "    plt.show()"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 7,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "# Useful function\n",
+    "\n",
+    "def get_highest_values(arr, n):\n",
+    "    return np.array(arr).argsort()[-n:][::-1]\n",
+    "\n",
+    "def get_lowest_values(arr, n):\n",
+    "    return np.array(arr).argsort()[::-1][-n:][::-1]"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 8,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "def get_sv_std(arr):\n",
+    "    arr = np.array(arr)\n",
+    "    images = [arr]\n",
+    "    \n",
+    "    # Apply list of filter on arr\n",
+    "    images.append(medfilt2d(arr, [3, 3]))\n",
+    "    images.append(medfilt2d(arr, [5, 5]))\n",
+    "    images.append(wiener(arr, [3, 3]))\n",
+    "    images.append(wiener(arr, [5, 5]))\n",
+    "    \n",
+    "    sv_vector = []\n",
+    "    # for each new image apply SVD and get SV \n",
+    "    for img in images:\n",
+    "        s = metrics.get_SVD_s(img)\n",
+    "        sv_vector.append(s)\n",
+    "        \n",
+    "    sv_array = np.array(sv_vector)\n",
+    "    \n",
+    "    _, len = sv_array.shape\n",
+    "    \n",
+    "    sv_std = []\n",
+    "    \n",
+    "    # normalize each SV vectors and compute standard deviation for each sub vectors\n",
+    "    for i in range(len):\n",
+    "        sv_array[:, i] = utils.normalize_arr(sv_array[:, i])\n",
+    "        sv_std.append(np.std(sv_array[:, i]))\n",
+    "        \n",
+    "    \n",
+    "    return np.array(sv_std)\n"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "## Scenes zones data"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 9,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "# start 00020 - ref 00900 - step 10\n",
+    "dict_appart = {'name': 'Appart1opt02', \n",
+    "               'prefix': 'appartAopt_', \n",
+    "               'indices': [\"00020\", \"00200\", \"00900\"],\n",
+    "               'zones': [3, 6]}\n",
+    "\n",
+    "# start 00050 - ref 01200 - step 10\n",
+    "dict_cuisine = {'name': 'Cuisine01', \n",
+    "               'prefix': 'cuisine01_', \n",
+    "               'indices': [\"00050\", \"00400\", \"01200\"],\n",
+    "               'zones': [3, 6]}\n",
+    "\n",
+    "# start 00020 - ref 00950 - step 10\n",
+    "dict_sdb_c = {'name': 'SdbCentre', \n",
+    "               'prefix': 'SdB2_', \n",
+    "               'indices': [\"00020\", \"00400\", \"00950\"],\n",
+    "               'zones': [3, 6]}\n",
+    "\n",
+    "# start 00020 - ref 00950 - step 10\n",
+    "dict_sdb_d = {'name': 'SdbDroite', \n",
+    "               'prefix': 'SdB2_D_', \n",
+    "               'indices': [\"00020\", \"00400\", \"00950\"],\n",
+    "               'zones': [2, 3, 10, 13]}"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 10,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "current_dict = dict_appart\n",
+    "interval = (30, 200)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 11,
+   "metadata": {},
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "../fichiersSVD_light/Appart1opt02/appartAopt_00020.png\n",
+      "../fichiersSVD_light/Appart1opt02/appartAopt_00200.png\n",
+      "../fichiersSVD_light/Appart1opt02/appartAopt_00900.png\n"
+     ]
+    }
+   ],
+   "source": [
+    "images_path = compute_images_path(current_dict)\n",
+    "zones_data = get_images_zones(current_dict, images_path)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 12,
+   "metadata": {},
+   "outputs": [
+    {
+     "name": "stderr",
+     "output_type": "stream",
+     "text": [
+      "/home/jbuisine/.pyenv/versions/thesis-venv/lib/python3.6/site-packages/scipy/signal/signaltools.py:966: RuntimeWarning: divide by zero encountered in true_divide\n",
+      "  res *= (1 - noise / lVar)\n",
+      "/home/jbuisine/.pyenv/versions/thesis-venv/lib/python3.6/site-packages/scipy/signal/signaltools.py:966: RuntimeWarning: invalid value encountered in multiply\n",
+      "  res *= (1 - noise / lVar)\n"
+     ]
+    }
+   ],
+   "source": [
+    "lab_image = metrics.get_LAB_L(zones_data[0][0])\n",
+    "\n",
+    "sv_std_vector = get_sv_std(lab_image)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 14,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "array([4.56454151e-12, 5.98443803e-01, 2.92380551e-01, 3.11862186e+00,\n",
+       "       1.83881976e+00, 1.32616590e+00, 2.31162957e+00, 3.26004542e+00,\n",
+       "       3.69558983e+00, 4.40479644e+00, 5.86361202e+00, 4.57210378e+00,\n",
+       "       4.01233699e+00, 9.91121754e+00, 1.49335463e+01, 1.02598281e+01,\n",
+       "       9.33149634e+00, 9.02596321e+00, 7.37994999e+00, 6.44933954e+00,\n",
+       "       1.41221182e+01, 6.92716771e+00, 6.66692086e+00, 1.96695779e+02,\n",
+       "       1.35313092e+01, 4.82088913e+00, 1.30004193e+01, 1.26877063e+01,\n",
+       "       1.19550988e+01, 1.54599579e+01, 1.12188124e+01, 8.25570286e+00,\n",
+       "       8.47955315e+00, 1.20754110e+01, 7.76856416e+00, 1.55686219e+01,\n",
+       "       1.13219704e+01, 4.78254950e+01, 1.61024565e+01, 1.05388706e+01,\n",
+       "       5.00185479e+01, 1.50232416e+01, 4.66821321e+01, 5.16188334e+01,\n",
+       "       4.94272850e+00, 5.06946699e+01, 4.60741401e+01, 4.95388007e+01,\n",
+       "       1.62546781e+01, 1.70462990e+01, 1.66268801e+01, 4.64068807e+01,\n",
+       "       4.87707118e+01, 5.18047108e+01, 2.42465168e+01, 1.75765638e+01,\n",
+       "       4.80224924e+01, 4.53850435e+01, 2.49940296e+01, 2.32956634e+01,\n",
+       "       2.44845973e+01, 1.98151482e+01, 2.28386331e+01, 1.95685413e+01,\n",
+       "       2.33535639e+01, 5.43324597e+01, 4.83919154e+01, 4.44866045e+01,\n",
+       "       2.46294407e+01, 2.53035119e+01, 1.90294314e+01, 4.86445357e+01,\n",
+       "       4.51782154e+01, 5.40772949e+01, 1.81583448e+01, 4.41443592e+01,\n",
+       "       3.69757725e+01, 5.23320448e+01, 4.21467616e+01, 5.33122822e+01,\n",
+       "       4.24121613e+01, 2.70014670e+01, 4.20051035e+01, 2.21602652e+01,\n",
+       "       2.59550074e+01, 2.86226215e+01, 2.06325539e+01, 2.61912490e+01,\n",
+       "       3.34594682e+01, 3.02981684e+01, 3.05140681e+01, 3.15200613e+01,\n",
+       "       3.56035468e+01, 2.00897239e+01, 3.60414295e+01, 2.95487956e+01,\n",
+       "       1.78073124e+01, 2.08486817e+01, 3.28774249e+01, 3.17002386e+01])"
+      ]
+     },
+     "execution_count": 14,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "indices = get_highest_values(sv_std_vector, 100)\n",
+    "\n",
+    "s_lab_image = metrics.get_SVD_s(lab_image)\n",
+    "\n",
+    "s_lab_image[indices]"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 52,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "array([  1,   3,   0,  26,  28,  29,  23,  30,  27,  31,  22,  21,  20,\n",
+       "        24,  25,  11,  14,  18,   6,  15,  34,  17,  32,  37,  35,   2,\n",
+       "        12,  33,  19,  36,  38,  39,  41,  40,   7,  47,  44,  42,  43,\n",
+       "        45,  49,  46,  13,  48,   9,  50,  16,  10,   8,  53,  54,  55,\n",
+       "        51,  56,  52,  61,  57,  58,  59,  60,  63,  62,  66,  65,  64,\n",
+       "        67,  69, 197,  70, 104, 103, 105,  68,  72,  73,  71, 106,   4,\n",
+       "       107, 108, 117, 109,  74, 111,  78, 145, 114, 123, 115, 102,  97,\n",
+       "       152, 144, 101, 130, 131, 129, 127, 120, 116])"
+      ]
+     },
+     "execution_count": 52,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "get_lowest_values(sv_std_vector, 100)"
+   ]
+  }
+ ],
+ "metadata": {
+  "kernelspec": {
+   "display_name": "thesis-venv",
+   "language": "python",
+   "name": "thesis-venv"
+  },
+  "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.0"
+  }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 2
+}

+ 1 - 1
modules/utils/config.py

@@ -35,7 +35,7 @@ cycle_scenes_indices            = ['E', 'I']
 normalization_choices           = ['svd', 'svdn', 'svdne']
 zones_indices                   = np.arange(16)
 
-metric_choices_labels           = ['lab', 'mscn', 'low_bits_2', 'low_bits_3', 'low_bits_4', 'low_bits_5', 'low_bits_6','low_bits_4_shifted_2', 'sub_blocks_stats', 'sub_blocks_area', 'sub_blocks_stats_reduced', 'sub_blocks_area_normed', 'mscn_var_4', 'mscn_var_16', 'mscn_var_64', 'mscn_var_16_max', 'mscn_var_64_max', 'ica_diff', 'svd_trunc_diff', 'ipca_diff', 'svd_reconstruct']
+metric_choices_labels           = ['lab', 'mscn', 'low_bits_2', 'low_bits_3', 'low_bits_4', 'low_bits_5', 'low_bits_6','low_bits_4_shifted_2', 'sub_blocks_stats', 'sub_blocks_area', 'sub_blocks_stats_reduced', 'sub_blocks_area_normed', 'mscn_var_4', 'mscn_var_16', 'mscn_var_64', 'mscn_var_16_max', 'mscn_var_64_max', 'ica_diff', 'svd_trunc_diff', 'ipca_diff', 'svd_reconstruct', 'highest_sv_std_filters', 'lowest_sv_std_filters']
 
 keras_epochs                    = 500
 keras_batch                     = 32

+ 55 - 0
modules/utils/data.py

@@ -8,6 +8,8 @@ from sklearn.decomposition import IncrementalPCA
 from sklearn.decomposition import TruncatedSVD
 from numpy.linalg import svd as lin_svd
 
+from scipy.signal import medfilt2d, wiener, cwt
+
 import numpy as np
 
 
@@ -267,8 +269,61 @@ def get_svd_data(data_type, block):
 
         data = metrics.get_SVD_s(output_img)
 
+    if 'sv_std_filters' in data_type:
+
+        # convert into lab by default to apply filters
+        lab_img = metrics.get_LAB_L(block)
+        arr = np.array(lab_img)
+        images = []
+        
+        # Apply list of filter on arr
+        images.append(medfilt2d(arr, [3, 3]))
+        images.append(medfilt2d(arr, [5, 5]))
+        images.append(wiener(arr, [3, 3]))
+        images.append(wiener(arr, [5, 5]))
+        
+        # By default computation of current block image
+        s_arr = metrics.get_SVD_s(arr)
+        sv_vector = [s_arr]
+
+        # for each new image apply SVD and get SV 
+        for img in images:
+            s = metrics.get_SVD_s(img)
+            sv_vector.append(s)
+            
+        sv_array = np.array(sv_vector)
+        
+        _, len = sv_array.shape
+        
+        sv_std = []
+        
+        # normalize each SV vectors and compute standard deviation for each sub vectors
+        for i in range(len):
+            sv_array[:, i] = utils.normalize_arr(sv_array[:, i])
+            sv_std.append(np.std(sv_array[:, i]))
+        
+        indices = []
+
+        if 'lowest' in data_type:
+            indices = get_lowest_values(sv_std, 200)
+
+        if 'highest' in data_type:
+            indices = get_highest_values(sv_std, 200)
+
+        # data are arranged following std trend computed
+        data = s_arr[indices]
+
     return data
 
+
+def get_highest_values(arr, n):
+    return np.array(arr).argsort()[-n:][::-1]
+
+
+def get_lowest_values(arr, n):
+    return np.array(arr).argsort()[::-1][-n:][::-1]
+
+
 def _get_mscn_variance(block, sub_block_size=(50, 50)):
 
     blocks = processing.divide_in_blocks(block, sub_block_size)

+ 2 - 1
runAll_maxwell_custom.sh

@@ -18,7 +18,8 @@ fi
 
 for size in {"4","8","16","26","32","40"}; do
 
-    for metric in {"lab","mscn","low_bits_2","low_bits_3","low_bits_4","low_bits_5","low_bits_6","low_bits_4_shifted_2","ica_diff","svd_trunc_diff","ipca_diff","svd_reconstruct"}; do
+    # for metric in {"lab","mscn","low_bits_2","low_bits_3","low_bits_4","low_bits_5","low_bits_6","low_bits_4_shifted_2","ica_diff","svd_trunc_diff","ipca_diff","svd_reconstruct"}; do
+    for metric in {"highest_sv_std_filters","lowest_sv_std_filters"}; do
         bash generateAndTrain_maxwell_custom.sh ${size} ${metric}
     done
 done

+ 2 - 1
runAll_maxwell_custom_center.sh

@@ -18,7 +18,8 @@ fi
 
 for size in {"4","8","16","26","32","40"}; do
 
-    for metric in {"lab","mscn","low_bits_2","low_bits_3","low_bits_4","low_bits_5","low_bits_6","low_bits_4_shifted_2","ica_diff","svd_trunc_diff","ipca_diff","svd_reconstruct"}; do
+    # for metric in {"lab","mscn","low_bits_2","low_bits_3","low_bits_4","low_bits_5","low_bits_6","low_bits_4_shifted_2","ica_diff","svd_trunc_diff","ipca_diff","svd_reconstruct"}; do
+    for metric in {"highest_sv_std_filters","lowest_sv_std_filters"}; do
         bash generateAndTrain_maxwell_custom_center.sh ${size} ${metric}
     done
 done

+ 2 - 1
runAll_maxwell_custom_split.sh

@@ -18,7 +18,8 @@ fi
 
 for size in {"4","8","16","26","32","40"}; do
 
-    for metric in {"lab","mscn","low_bits_2","low_bits_3","low_bits_4","low_bits_5","low_bits_6","low_bits_4_shifted_2","ica_diff","svd_trunc_diff","ipca_diff","svd_reconstruct"}; do
+    #for metric in {"lab","mscn","low_bits_2","low_bits_3","low_bits_4","low_bits_5","low_bits_6","low_bits_4_shifted_2","ica_diff","svd_trunc_diff","ipca_diff","svd_reconstruct"}; do
+    for metric in {"highest_sv_std_filters","lowest_sv_std_filters"}; do
         bash generateAndTrain_maxwell_custom_split.sh ${size} ${metric}
     done
 done