Browse Source

Update of SVD metric

Jerome Buisine 3 years ago
parent
commit
efad8cb6e8
3 changed files with 20 additions and 25 deletions
  1. 9 16
      classification_cnn_keras_svd_img.py
  2. 7 6
      modules/image_metrics/svd_metric.py
  3. 4 3
      run.sh

+ 9 - 16
classification_cnn_keras_svd_img.py

@@ -28,7 +28,7 @@ import json
 
 from keras.preprocessing.image import ImageDataGenerator
 from keras.models import Sequential
-from keras.layers import Conv2D, MaxPooling2D, AveragePooling2D
+from keras.layers import Conv2D, MaxPooling2D, AveragePooling2D, Cropping2D
 from keras.layers import Activation, Dropout, Flatten, Dense, BatchNormalization
 from keras.optimizers import Adam
 from keras.regularizers import l2
@@ -83,7 +83,7 @@ def init_directory(img_size, generate_data):
 
         os.makedirs(str(validation_data_dir.replace('**img_size**', img_size_str) + '/final'))
         os.makedirs(str(validation_data_dir.replace('**img_size**', img_size_str) + '/noisy'))
-    
+
         for f in Path('./data').walkfiles():
             if 'png' in f:
                 img = Image.open(f)
@@ -101,30 +101,23 @@ def generate_model():
 
     model = Sequential()
 
-    model.add(Conv2D(50, (2, 2), input_shape=input_shape))
-    model.add(Activation('relu'))
-    model.add(MaxPooling2D(pool_size=(2, 2)))
+    model.add(Cropping2D(cropping=((20, 20), (20, 20)), input_shape=input_shape))
 
-    model.add(Conv2D(30, (2, 2)))
+    model.add(Conv2D(50, (2, 2)))
     model.add(Activation('relu'))
-    model.add(MaxPooling2D(pool_size=(2, 2)))
+    model.add(AveragePooling2D(pool_size=(2, 2)))
 
     model.add(Flatten())
 
     model.add(Dense(100, kernel_regularizer=l2(0.01)))
-    model.add(BatchNormalization())
     model.add(Activation('relu'))
+    model.add(BatchNormalization())
     model.add(Dropout(0.2))
 
     model.add(Dense(100, kernel_regularizer=l2(0.01)))
-    model.add(BatchNormalization())
     model.add(Activation('relu'))
-    model.add(Dropout(0.2))
-
-    model.add(Dense(20, kernel_regularizer=l2(0.01)))
     model.add(BatchNormalization())
-    model.add(Activation('relu'))
-    model.add(Dropout(0.1))
+    model.add(Dropout(0.5))
 
     model.add(Dense(1))
     model.add(Activation('sigmoid'))
@@ -183,12 +176,12 @@ def main():
 
     # update global variable and not local
     global batch_size
-    global epochs   
+    global epochs
     global input_shape
     global train_data_dir
     global validation_data_dir
     global nb_train_samples
-    global nb_validation_samples 
+    global nb_validation_samples
 
     if len(sys.argv) <= 1:
         print('Run with default parameters...')

+ 7 - 6
modules/image_metrics/svd_metric.py

@@ -10,7 +10,7 @@ import time
 import numpy as np
 from sklearn import preprocessing
 
-import modules.model_helper.image_conversion as img_c 
+import modules.model_helper.image_conversion as img_c
 
 '''
 Method which extracts SVD features from image and returns 's' vector
@@ -34,12 +34,13 @@ def get_s_model_data_img(image):
     plt.rcParams["figure.figsize"] = fig_size
 
     U, s, V = svd(image, full_matrices=False)
-    
+
     plt.figure()   # create a new figure
-      
-    plt.plot(s[:, 0])
-    plt.plot(s[:, 1])
-    plt.plot(s[:, 2])
+
+    output_normalized = preprocessing.normalize(s, norm='l1', axis=0, copy=True, return_norm=False)
+    plt.plot(output_normalized[70:100, 0])
+    plt.plot(output_normalized[70:100:, 1])
+    plt.plot(output_normalized[70:100:, 2])
 
     img = img_c.fig2img(plt.gcf())
 

+ 4 - 3
run.sh

@@ -3,8 +3,8 @@
 size=$1
 
 if [ -z ${size} ]; then
-  echo "Need size parameter : ./run.sh 20"; 
-else 
+  echo "Need size parameter : ./run.sh 20";
+else
   echo "Run algorithms with image of size ${size}.."
 fi
 
@@ -12,4 +12,5 @@ fi
 # python classification_cnn_keras_cross_validation.py --directory ../models/$size/ --output cnn_cross_validation_model --batch_size 32 --epochs 150 --img $size
 # python classification_cnn_keras_svd.py --directory ../models/$size/ --output svd_model --batch_size 32 --epochs 150 --img $size
 
-python classification_cnn_keras_svd_img.py --directory ../models/$size/ --output svd_img_model --batch_size 32 --epochs 150 --img $size --generate n
+python classification_cnn_keras_svd_img.py --directory ../models/$size/ --output svd_img_model --batch_size 32 --epochs 150 --img $size --generate y
+python classification_cnn_keras_svd_img.py --directory ../models/$size/ --output svd_img_model --batch_size 32 --epochs 150 --img $size --generate n