Parcourir la source

Scripts project updates

Jerome Buisine il y a 5 ans
Parent
commit
dda7042cb6

+ 4 - 4
.gitignore

@@ -1,8 +1,8 @@
 # project data
 # project data
-data
-saved_models
-threshold_map
-models_info
+data/*
+saved_models/*
+threshold_map/*
+models_info/*
 
 
 fichiersSVD_light/*/*/*.csv
 fichiersSVD_light/*/*/*.csv
 fichiersSVD_light/*_min_max_values
 fichiersSVD_light/*_min_max_values

+ 8 - 8
README.md

@@ -7,7 +7,7 @@ pip install -r requirements.txt
 ```
 ```
 
 
 Generate all needed data for each metrics
 Generate all needed data for each metrics
-```
+```bash
 python generate_all_data.py --metric all
 python generate_all_data.py --metric all
 ```
 ```
 
 
@@ -18,7 +18,7 @@ For noise detection, many metrics are available :
 - low_bits_4
 - low_bits_4
 
 
 You can also specify metric you want to compute :
 You can also specify metric you want to compute :
-```
+```bash
 python generate_all_data.py --metric mscn
 python generate_all_data.py --metric mscn
 ```
 ```
 
 
@@ -63,7 +63,7 @@ Parameters explained :
 
 
 This is an example of how to train a model
 This is an example of how to train a model
 
 
-```python
+```bash
 python models/xxxxx.py --data 'data/xxxxx.train' --output 'model_file_to_save'
 python models/xxxxx.py --data 'data/xxxxx.train' --output 'model_file_to_save'
 ```
 ```
 
 
@@ -71,7 +71,7 @@ python models/xxxxx.py --data 'data/xxxxx.train' --output 'model_file_to_save'
 
 
 Now we have a model trained, we can use it with an image as input :
 Now we have a model trained, we can use it with an image as input :
 
 
-```python
+```bash
 python metrics_predictions/predict_noisy_image_svd_lab.py --image path/to/image.png --interval "x,x" --model saved_models/xxxxxx.joblib --mode 'svdn'
 python metrics_predictions/predict_noisy_image_svd_lab.py --image path/to/image.png --interval "x,x" --model saved_models/xxxxxx.joblib --mode 'svdn'
 ```
 ```
 
 
@@ -81,7 +81,7 @@ The model will return only 0 or 1 :
 
 
 You can also use other specific metric
 You can also use other specific metric
 
 
-```python
+```bash
 python metrics_predictions/predict_noisy_image_svd_mscn.py --image path/to/image.png --interval "x,x" --model saved_models/xxxxxx.joblib --mode 'svdn'
 python metrics_predictions/predict_noisy_image_svd_mscn.py --image path/to/image.png --interval "x,x" --model saved_models/xxxxxx.joblib --mode 'svdn'
 ```
 ```
 
 
@@ -94,7 +94,7 @@ All SVD metrics you developed need :
 
 
 Now we have a model trained, we can use it with an image as input :
 Now we have a model trained, we can use it with an image as input :
 
 
-```python
+```bash
 python prediction_scene.py --data path/to/xxxx.csv --model saved_model/xxxx.joblib --output xxxxx --scene xxxx
 python prediction_scene.py --data path/to/xxxx.csv --model saved_model/xxxx.joblib --output xxxxx --scene xxxx
 ```
 ```
 **Remark** : *scene* parameter expected need to be the correct name of the Scene.
 **Remark** : *scene* parameter expected need to be the correct name of the Scene.
@@ -121,7 +121,7 @@ Parameters list :
 
 
 Main objective of this project is to predict as well as a human the noise perception on a photo realistic image. Human threshold is available from training data. So a script was developed to give the predicted treshold from model and compare predicted treshold from the expected one.
 Main objective of this project is to predict as well as a human the noise perception on a photo realistic image. Human threshold is available from training data. So a script was developed to give the predicted treshold from model and compare predicted treshold from the expected one.
 
 
-```python
+```bash
 python predict_seuil_expe.py --interval "x,x" --model 'saved_models/xxxx.joblib' --mode ["svd", "svdn", "svdne"] --metric ['lab', 'mscn', ...] --limit_detection xx
 python predict_seuil_expe.py --interval "x,x" --model 'saved_models/xxxx.joblib' --mode ["svd", "svdn", "svdne"] --metric ['lab', 'mscn', ...] --limit_detection xx
 ```
 ```
 
 
@@ -141,7 +141,7 @@ The content will be divised into two parts :
 
 
 The previous script need to already have ran to obtain and display treshold maps on this markdown file.
 The previous script need to already have ran to obtain and display treshold maps on this markdown file.
 
 
-```python
+```bash
 python save_model_result_in_md.py --interval "xx,xx" --model saved_models/xxxx.joblib --mode ["svd", "svdn", "svdne"] --metric ['lab', 'mscn'] 
 python save_model_result_in_md.py --interval "xx,xx" --model saved_models/xxxx.joblib --mode ["svd", "svdn", "svdne"] --metric ['lab', 'mscn'] 
 ```
 ```
 
 

+ 35 - 28
generateAndTrain_maxwell.sh

@@ -14,7 +14,7 @@ if [ -z "$2" ]
     exit 1
     exit 1
 fi
 fi
 
 
-
+result_filename="models_info/models_comparisons.csv"
 VECTOR_SIZE=200
 VECTOR_SIZE=200
 size=$1
 size=$1
 metric=$2
 metric=$2
@@ -25,42 +25,49 @@ scenes="A, D, G, H"
 half=$(($size/2))
 half=$(($size/2))
 start=-$half
 start=-$half
 for counter in {0..4}; do
 for counter in {0..4}; do
-end=$(($start+$size))
+    end=$(($start+$size))
 
 
-if [ "$end" -gt "$VECTOR_SIZE" ]; then
-    start=$(($VECTOR_SIZE-$size))
-    end=$(($VECTOR_SIZE))
-fi
+    if [ "$end" -gt "$VECTOR_SIZE" ]; then
+        start=$(($VECTOR_SIZE-$size))
+        end=$(($VECTOR_SIZE))
+    fi
 
 
-if [ "$start" -lt "0" ]; then
-    start=$((0))
-    end=$(($size))
-fi
+    if [ "$start" -lt "0" ]; then
+        start=$((0))
+        end=$(($size))
+    fi
 
 
-for nb_zones in {6,8,10,12,16}; do
+    for nb_zones in {6,8,10,12,16}; do
 
 
-    echo $start $end
+        echo $start $end
 
 
-    for mode in {"svd","svdn","svdne"}; do
-        for model in {"svm_model","ensemble_model","ensemble_model_v2"}; do
+        for mode in {"svd","svdn","svdne"}; do
+            for model in {"svm_model","ensemble_model","ensemble_model_v2"}; do
 
 
-            FILENAME="data/data_maxwell_N${size}_B${start}_E${end}_nb_zones_${nb_zones}_${metric}_${mode}"
-            MODEL_NAME="${model}_N${size}_B${start}_E${end}_nb_zones_${nb_zones}_${metric}_${mode}"
+                FILENAME="data/data_maxwell_N${size}_B${start}_E${end}_nb_zones_${nb_zones}_${metric}_${mode}"
+                MODEL_NAME="${model}_N${size}_B${start}_E${end}_nb_zones_${nb_zones}_${metric}_${mode}"
 
 
-            echo $FILENAME
-            python generate_data_model_random.py --output ${FILENAME} --interval "${start},${end}" --kind ${mode} --metric ${metric} --scenes "${scenes}" --nb_zones "${nb_zones}" --percent 1 --sep ';' --rowindex '0'
-            python models/${model}_train.py --data ${FILENAME}.train --output ${MODEL_NAME}
-           
-            python predict_seuil_expe_maxwell.py --interval "${start},${end}" --model "saved_models/${MODEL_NAME}.joblib" --mode "${mode}" --metric ${metric} --limit_detection '2'
-            python save_model_result_in_md_maxwell.py --interval "${start},${end}" --model "saved_models/${MODEL_NAME}.joblib" --mode "${mode}" --metric ${metric}
+                echo $FILENAME
+                
+                # only compute if necessary (perhaps server will fall.. Just in case)
+                if grep -q "${MODEL_NAME}" "${result_filename}"; then
+
+                    echo "${MODEL_NAME} results already generated..."
+                else
+                    python generate_data_model_random.py --output ${FILENAME} --interval "${start},${end}" --kind ${mode} --metric ${metric} --scenes "${scenes}" --nb_zones "${nb_zones}" --percent 1 --sep ';' --rowindex '0'
+                    python models/${model}_train.py --data ${FILENAME}.train --output ${MODEL_NAME}
+                
+                    python predict_seuil_expe_maxwell.py --interval "${start},${end}" --model "saved_models/${MODEL_NAME}.joblib" --mode "${mode}" --metric ${metric} --limit_detection '2'
+                    python save_model_result_in_md_maxwell.py --interval "${start},${end}" --model "saved_models/${MODEL_NAME}.joblib" --mode "${mode}" --metric ${metric}
+                fi
+            done
         done
         done
     done
     done
-done
 
 
-if [ "$counter" -eq "0" ]; then
-    start=$(($start+50-$half))
-else 
-    start=$(($start+50))
-fi
+    if [ "$counter" -eq "0" ]; then
+        start=$(($start+50-$half))
+    else 
+        start=$(($start+50))
+    fi
 
 
 done
 done

+ 6 - 1
models/ensemble_model_train.py

@@ -14,8 +14,9 @@ from sklearn.metrics import accuracy_score
 
 
 import sys, os, getopt
 import sys, os, getopt
 
 
+saved_models_folder = 'saved_models'
 current_dirpath = os.getcwd()
 current_dirpath = os.getcwd()
-output_model_folder = os.path.join(current_dirpath, 'saved_models')
+output_model_folder = os.path.join(current_dirpath, saved_models_folder)
 
 
 def get_best_model(X_train, y_train):
 def get_best_model(X_train, y_train):
     Cs = [0.001, 0.01, 0.1, 1, 10]
     Cs = [0.001, 0.01, 0.1, 1, 10]
@@ -100,6 +101,10 @@ def main():
     y_pred = ensemble_model.predict(X_test)
     y_pred = ensemble_model.predict(X_test)
     print("**Test :** " + str(accuracy_score(y_test, y_pred)))
     print("**Test :** " + str(accuracy_score(y_test, y_pred)))
 
 
+    # create path if not exists
+    if not os.path.exists(saved_models_folder):
+        os.makedirs(saved_models_folder)
+
     joblib.dump(ensemble_model, output_model_folder + '/' + p_output + '.joblib') 
     joblib.dump(ensemble_model, output_model_folder + '/' + p_output + '.joblib') 
 
 
 if __name__== "__main__":
 if __name__== "__main__":

+ 7 - 1
models/ensemble_model_train_v2.py

@@ -16,8 +16,10 @@ from sklearn.metrics import accuracy_score
 
 
 import sys, os, getopt
 import sys, os, getopt
 
 
+
+saved_models_folder = 'saved_models'
 current_dirpath = os.getcwd()
 current_dirpath = os.getcwd()
-output_model_folder = os.path.join(current_dirpath, 'saved_models')
+output_model_folder = os.path.join(current_dirpath, saved_models_folder)
 
 
 def get_best_model(X_train, y_train):
 def get_best_model(X_train, y_train):
     Cs = [0.001, 0.01, 0.1, 1, 10, 20, 30]
     Cs = [0.001, 0.01, 0.1, 1, 10, 20, 30]
@@ -107,6 +109,10 @@ def main():
     y_pred = ensemble_model.predict(X_test)
     y_pred = ensemble_model.predict(X_test)
     print("**Test :** " + str(accuracy_score(y_test, y_pred)))
     print("**Test :** " + str(accuracy_score(y_test, y_pred)))
 
 
+    # create path if not exists
+    if not os.path.exists(saved_models_folder):
+        os.makedirs(saved_models_folder)
+
     joblib.dump(ensemble_model, output_model_folder + '/' +  p_output + '.joblib') 
     joblib.dump(ensemble_model, output_model_folder + '/' +  p_output + '.joblib') 
 
 
 if __name__== "__main__":
 if __name__== "__main__":

+ 6 - 1
models/svm_model_train.py

@@ -13,8 +13,9 @@ from sklearn.metrics import accuracy_score
 
 
 import sys, os, getopt
 import sys, os, getopt
 
 
+saved_models_folder = 'saved_models'
 current_dirpath = os.getcwd()
 current_dirpath = os.getcwd()
-output_model_folder = os.path.join(current_dirpath, 'saved_models')
+output_model_folder = os.path.join(current_dirpath, saved_models_folder)
 
 
 def get_best_model(X_train, y_train):
 def get_best_model(X_train, y_train):
     
     
@@ -84,6 +85,10 @@ def main():
     y_pred = svm_model.predict(X_test)
     y_pred = svm_model.predict(X_test)
     print("**Test :** " + str(accuracy_score(y_test, y_pred)))
     print("**Test :** " + str(accuracy_score(y_test, y_pred)))
 
 
+    # create path if not exists
+    if not os.path.exists(saved_models_folder):
+        os.makedirs(saved_models_folder)
+        
     joblib.dump(svm_model, output_model_folder + '/' + p_output + '.joblib') 
     joblib.dump(svm_model, output_model_folder + '/' + p_output + '.joblib') 
 
 
 if __name__== "__main__":
 if __name__== "__main__":

+ 1 - 1
predict_seuil_expe_maxwell.py

@@ -167,7 +167,7 @@ def main():
             # end of scene => display of results
             # end of scene => display of results
 
 
             # construct path using model name for saving threshold map folder
             # construct path using model name for saving threshold map folder
-            model_treshold_path = os.path.join(threshold_map_folder, p_model_file.split('/')[-1].replace('.joblib', '_'))
+            model_treshold_path = os.path.join(threshold_map_folder, p_model_file.split('/')[-1].replace('.joblib', ''))
             
             
             # create threshold model path if necessary
             # create threshold model path if necessary
             if not os.path.exists(model_treshold_path):
             if not os.path.exists(model_treshold_path):

+ 1 - 1
runAll_maxwell.sh

@@ -12,6 +12,6 @@ echo 'model_name; vector_size; start; end; nb_zones; metric; mode; train; test;
 for size in {"4","8","16","26","32","40"}; do
 for size in {"4","8","16","26","32","40"}; do
 
 
     for metric in {"lab","mscn","low_bits_4","low_bits_2"}; do
     for metric in {"lab","mscn","low_bits_4","low_bits_2"}; do
-        bash generateAndTrain_maxwell.sh ${size} ${metric}
+        bash generateAndTrain_maxwell.sh ${size} ${metric} &
     done
     done
 done
 done