Parcourir la source

Add of visualization scripts; Updates of modules

Jérôme BUISINE il y a 5 ans
Parent
commit
02b6aaa591
6 fichiers modifiés avec 227 ajouts et 13 suppressions
  1. 1 0
      .gitignore
  2. 6 6
      generate_all.sh
  3. 7 3
      modules/utils/config.py
  4. 72 0
      modules/utils/data_type.py
  5. 3 4
      noise_computation.py
  6. 138 0
      noise_svd_visualization.py

+ 1 - 0
.gitignore

@@ -61,3 +61,4 @@ target/
 
 # others
 generated/*
+curves_pictures/*

+ 6 - 6
generate_all.sh

@@ -3,9 +3,9 @@ for noise in {"cauchy","gaussian","laplace","log_normal","mut_white","white"}; d
     for identical in {"0","1"}; do
 
         if [ ${identical} == "1" ]; then
-            python noise_computation.py --noise ${noise} --image images/calibration.png --n 999 --identical ${identical} --output ${noise}.png --all 1
+            python noise_computation.py --noise ${noise} --image images/calibration.png --n 1000 --identical ${identical} --output ${noise}.png --all 1 &
         else
-            python noise_computation.py --noise ${noise} --image images/calibration.png --n 999 --identical ${identical} --output ${noise}_color.png --all 1
+            python noise_computation.py --noise ${noise} --image images/calibration.png --n 1000 --identical ${identical} --output ${noise}_color.png --all 1 &
         fi
 
     done
@@ -15,10 +15,10 @@ done
 # specifig for salt and pepper noise
 for identical in {"0","1"}; do
     if [ ${identical} == "1" ]; then
-        python noise_computation.py --noise salt_pepper --image images/calibration.png --n 999 --identical ${identical} --output ${noise}_B.png --all 1 --p 0.1
-        python noise_computation.py --noise salt_pepper --image images/calibration.png --n 999 --identical ${identical} --output ${noise}_A.png --all 1 --p 0.01
+        python noise_computation.py --noise salt_pepper --image images/calibration.png --n 1000 --identical ${identical} --output salt_pepper_B.png --all 1 --p 0.1 &
+        python noise_computation.py --noise salt_pepper --image images/calibration.png --n 1000 --identical ${identical} --output salt_pepper_A.png --all 1 --p 0.01 &
     else
-        python noise_computation.py --noise salt_pepper --image images/calibration.png --n 999 --identical ${identical} --output ${noise}_A_color.png --all 1 --p 0.01
-        python noise_computation.py --noise salt_pepper --image images/calibration.png --n 999 --identical ${identical} --output ${noise}_B_color.png --all 1 --p 0.1
+        python noise_computation.py --noise salt_pepper --image images/calibration.png --n 1000 --identical ${identical} --output salt_pepper_A_color.png --all 1 --p 0.01 &
+        python noise_computation.py --noise salt_pepper --image images/calibration.png --n 1000 --identical ${identical} --output salt_pepper_B_color.png --all 1 --p 0.1 &
     fi
 done

+ 7 - 3
modules/utils/config.py

@@ -1,3 +1,7 @@
-
-image_kinds     = ['RGB', 'Grey']
-noise_labels    = ['cauchy', 'gaussian', 'laplace', 'log_normal', 'mut_white', 'salt_papper', 'white']
+normalization_choices  = ['svd', 'svdn', 'svdne']
+metric_choices_labels  = ['lab', 'mscn', 'mscn_revisited', 'low_bits_2', 'low_bits_3', 'low_bits_4', 'low_bits_5', 'low_bits_6','low_bits_4_shifted_2']
+image_kinds            = ['RGB', 'Grey']
+noise_labels           = ['cauchy', 'gaussian', 'laplace', 'log_normal', 'mut_white', 'salt_pepper', 'white']
+generated_folder       = 'generated'
+pictures_output_folder = 'curves_pictures'
+filename_ext           = 'png'

+ 72 - 0
modules/utils/data_type.py

@@ -0,0 +1,72 @@
+from ipfml import processing, metrics
+from PIL import Image
+from skimage import color
+
+import numpy as np
+
+def get_svd_data(data_type, block):
+    """
+    Method which returns the data type expected
+    """
+
+    if data_type == 'lab':
+
+        block_file_path = '/tmp/lab_img.png'
+        block.save(block_file_path)
+        data = processing.get_LAB_L_SVD_s(Image.open(block_file_path))
+
+    if data_type == 'mscn_revisited':
+
+        img_mscn_revisited = processing.rgb_to_mscn(block)
+
+        # save tmp as img
+        img_output = Image.fromarray(img_mscn_revisited.astype('uint8'), 'L')
+        mscn_revisited_file_path = '/tmp/mscn_revisited_img.png'
+        img_output.save(mscn_revisited_file_path)
+        img_block = Image.open(mscn_revisited_file_path)
+
+        # extract from temp image
+        data = metrics.get_SVD_s(img_block)
+
+    if data_type == 'mscn':
+
+        img_gray = np.array(color.rgb2gray(np.asarray(block))*255, 'uint8')
+        img_mscn = processing.calculate_mscn_coefficients(img_gray, 7)
+        img_mscn_norm = processing.normalize_2D_arr(img_mscn)
+
+        img_mscn_gray = np.array(img_mscn_norm*255, 'uint8')
+
+        data = metrics.get_SVD_s(img_mscn_gray)
+
+    if data_type == 'low_bits_6':
+
+        low_bits_6 = processing.rgb_to_LAB_L_low_bits(block, 6)
+        data = metrics.get_SVD_s(low_bits_6)
+
+    if data_type == 'low_bits_5':
+
+        low_bits_5 = processing.rgb_to_LAB_L_low_bits(block, 5)
+        data = metrics.get_SVD_s(low_bits_5)
+
+    if data_type == 'low_bits_4':
+
+        low_bits_4 = processing.rgb_to_LAB_L_low_bits(block, 4)
+        data = metrics.get_SVD_s(low_bits_4)
+
+    if data_type == 'low_bits_3':
+
+        low_bits_3 = processing.rgb_to_LAB_L_low_bits(block, 3)
+        data = metrics.get_SVD_s(low_bits_3)
+
+    if data_type == 'low_bits_2':
+
+        low_bits_2 = processing.rgb_to_LAB_L_low_bits(block, 2)
+        data = metrics.get_SVD_s(low_bits_2)
+
+    if data_type == 'low_bits_4_shifted_2':
+
+        data = metrics.get_SVD_s(processing.rgb_to_LAB_L_bits(block, (3, 6)))
+
+    return data
+
+

+ 3 - 4
noise_computation.py

@@ -6,9 +6,8 @@ from modules.utils import config as cfg
 from modules import noise
 
 noise_list       = cfg.noise_labels
-filename_ext     = 'png'
-
-generated_folder = 'generated'
+generated_folder = cfg.generated_folder
+filename_ext     = cfg.filename_ext
 
 def generate_noisy_image(p_image, p_n, p_noise, p_identical, p_output, p_param):
 
@@ -53,7 +52,7 @@ def main():
             p_noise = a
 
             if not p_noise in noise_list:
-                assert False, "Unknow noise parameter %s " % (noise_list)
+                assert False, "Unknow noise parameter %s, %s " % (p_noise, noise_list)
 
         elif o in ("-i", "--image"):
             p_image_path = a

+ 138 - 0
noise_svd_visualization.py

@@ -0,0 +1,138 @@
+import sys, os, getopt
+from PIL import Image
+
+from ipfml import processing
+
+from modules.utils import config as cfg
+from modules.utils import data_type as dt
+from modules import noise
+
+import matplotlib.pyplot as plt
+
+noise_list            = cfg.noise_labels
+generated_folder      = cfg.generated_folder
+filename_ext          = cfg.filename_ext
+metric_choices        = cfg.metric_choices_labels
+normalization_choices = cfg.normalization_choices
+pictures_folder       = cfg.pictures_output_folder
+
+def main():
+
+    p_step = 1
+    max_value_svd = 0
+    min_value_svd = sys.maxsize
+
+    if len(sys.argv) <= 1:
+        print('python noise_svd_visualization.py --prefix path/with/prefix --metric lab --mode svdn --n 300 --interval "0, 200" --step 30 --output filename')
+        sys.exit(2)
+    try:
+        opts, args = getopt.getopt(sys.argv[1:], "h:p:m:m:n:i:s:o", ["help=", "prefix=", "metric=", "mode=", "n=", "interval=", "step=", "output="])
+    except getopt.GetoptError:
+        # print help information and exit:
+        print('python noise_svd_visualization.py --prefix path/with/prefix --metric lab --mode svdn --n 300 --interval "0, 200" --step 30 --output filename')
+        sys.exit(2)
+    for o, a in opts:
+        if o == "-h":
+            print('python noise_svd_visualization.py --prefix path/with/prefix --metric lab --mode svdn --n 300 --interval "0, 200" --step 30 --output filename')
+            sys.exit()
+        elif o in ("-p", "--prefix"):
+            p_prefix = a
+        elif o in ("-m", "--mode"):
+            p_mode = a
+
+            if not p_mode in normalization_choices:
+                assert False, "Unknown normalization choice, %s" % normalization_choices
+
+        elif o in ("-m", "--metric"):
+            p_metric = a
+
+            if not p_metric in metric_choices:
+                assert False, "Unknown metric choice, %s" % metric_choices
+
+        elif o in ("-n", "--n"):
+            p_n = int(a)
+        elif o in ("-i", "--interval"):
+            p_interval = list(map(int, a.split(',')))
+        elif o in ("-s", "--step"):
+            p_step = int(a)
+        elif o in ("-o", "--output"):
+            p_output = a
+        else:
+            assert False, "unhandled option"
+
+
+    noise_name = p_prefix.split('/')[1].replace('_', '')
+
+    file_path = p_prefix + "{}." + filename_ext
+
+    begin, end = p_interval
+    all_svd_data = []
+
+    svd_data = []
+    image_indices = []
+
+    # get all data from images
+    for i in range(1, p_n):
+
+        image_path = file_path.format(str(i))
+        img = Image.open(image_path)
+
+        svd_values = dt.get_svd_data(p_metric, img)
+        svd_values = svd_values[begin:end]
+        all_svd_data.append(svd_values)
+
+        # update min max values
+        min_value = svd_values.min()
+        max_value = svd_values.max()
+
+        if min_value < min_value_svd:
+            min_value_svd = min_value
+
+        if max_value > min_value_svd:
+            max_value_svd = max_value
+
+        print('%.2f%%' % ((i + 1) / p_n * 100))
+        sys.stdout.write("\033[F")
+
+    print("Generation of output figure...")
+    for id, data in enumerate(all_svd_data):
+
+        if id % p_step == 0:
+
+            current_data = data
+            if p_mode == 'svdn':
+                current_data = processing.normalize_arr(current_data)
+
+            if p_mode == 'svdne':
+                current_data = processing.normalize_arr_with_range(current_data, min_value_svd, max_value_svd)
+
+            svd_data.append(current_data)
+            image_indices.append(id)
+
+    # display all data using matplotlib
+
+    plt.title(noise_name  + ' noise, interval information ['+ str(begin) +', '+ str(end) +'], ' + p_metric + ' metric, step ' + str(p_step), fontsize=20)
+    plt.ylabel('Importance of noise [1, 999]', fontsize=14)
+    plt.xlabel('Vector features', fontsize=16)
+
+    for id, data in enumerate(svd_data):
+
+        p_label = p_prefix + str(image_indices[id])
+        plt.plot(data, label=p_label)
+
+    plt.legend(bbox_to_anchor=(0.8, 1), loc=2, borderaxespad=0.2, fontsize=14)
+    plt.ylim(0, 0.1)
+    plt.show()
+
+    output_filename = noise_name + "1_to_" + str(p_n) + "_B" + str(begin) + "_E" + str(end) + "_" + p_metric + "_S" + str(p_step) + "_" + p_mode
+    output_path = os.path.join(pictures_folder, output_filename)
+
+    if not os.path.exists(pictures_folder):
+        os.makedirs(pictures_folder)
+
+    plt.savefig(output_path)
+
+
+
+if __name__== "__main__":
+    main()