|
@@ -14,8 +14,7 @@ import time
|
|
import json
|
|
import json
|
|
|
|
|
|
from PIL import Image
|
|
from PIL import Image
|
|
-from ipfml import image_processing
|
|
|
|
-from ipfml import metrics
|
|
|
|
|
|
+from ipfml import processing, metrics
|
|
|
|
|
|
from modules.utils import config as cfg
|
|
from modules.utils import config as cfg
|
|
|
|
|
|
@@ -33,16 +32,28 @@ zones = cfg.zones_indices
|
|
seuil_expe_filename = cfg.seuil_expe_filename
|
|
seuil_expe_filename = cfg.seuil_expe_filename
|
|
|
|
|
|
metric_choices = cfg.metric_choices_labels
|
|
metric_choices = cfg.metric_choices_labels
|
|
-
|
|
|
|
output_data_folder = cfg.output_data_folder
|
|
output_data_folder = cfg.output_data_folder
|
|
|
|
+custom_min_max_folder = cfg.min_max_custom_folder
|
|
|
|
+min_max_ext = cfg.min_max_filename_extension
|
|
|
|
+
|
|
|
|
+generic_output_file_svd = '_random.csv'
|
|
|
|
+
|
|
|
|
+min_value_interval = sys.maxsize
|
|
|
|
+max_value_interval = 0
|
|
|
|
|
|
-def construct_new_line(path_seuil, interval, line, sep, index):
|
|
|
|
|
|
+def construct_new_line(path_seuil, interval, line, norm, sep, index):
|
|
begin, end = interval
|
|
begin, end = interval
|
|
|
|
|
|
line_data = line.split(';')
|
|
line_data = line.split(';')
|
|
seuil = line_data[0]
|
|
seuil = line_data[0]
|
|
metrics = line_data[begin+1:end+1]
|
|
metrics = line_data[begin+1:end+1]
|
|
|
|
|
|
|
|
+ metrics = [float(m) for m in metrics]
|
|
|
|
+
|
|
|
|
+ # TODO : check if it's always necessary to do that (loss of information for svd)
|
|
|
|
+ if norm:
|
|
|
|
+ metrics = processing.normalize_arr_with_range(metrics, min_value_interval, max_value_interval)
|
|
|
|
+
|
|
with open(path_seuil, "r") as seuil_file:
|
|
with open(path_seuil, "r") as seuil_file:
|
|
seuil_learned = int(seuil_file.readline().strip())
|
|
seuil_learned = int(seuil_file.readline().strip())
|
|
|
|
|
|
@@ -55,12 +66,71 @@ def construct_new_line(path_seuil, interval, line, sep, index):
|
|
if index:
|
|
if index:
|
|
line += " " + str(idx + 1)
|
|
line += " " + str(idx + 1)
|
|
line += sep
|
|
line += sep
|
|
- line += val
|
|
|
|
|
|
+ line += str(val)
|
|
line += '\n'
|
|
line += '\n'
|
|
|
|
|
|
return line
|
|
return line
|
|
|
|
|
|
-def generate_data_model(_filename, _interval, _choice, _metric, _scenes = scenes, _nb_zones = 4, _percent = 1, _sep=':', _index=True):
|
|
|
|
|
|
+def get_min_max_value_interval(_filename, _interval, _choice, _metric):
|
|
|
|
+
|
|
|
|
+ global min_value_interval, max_value_interval
|
|
|
|
+
|
|
|
|
+ scenes = os.listdir(path)
|
|
|
|
+
|
|
|
|
+ # remove min max file from scenes folder
|
|
|
|
+ scenes = [s for s in scenes if min_max_filename not in s]
|
|
|
|
+
|
|
|
|
+ for id_scene, folder_scene in enumerate(scenes):
|
|
|
|
+
|
|
|
|
+ # only take care of maxwell scenes
|
|
|
|
+ if folder_scene in scenes_list:
|
|
|
|
+
|
|
|
|
+ scene_path = os.path.join(path, folder_scene)
|
|
|
|
+
|
|
|
|
+ zones_folder = []
|
|
|
|
+ # create zones list
|
|
|
|
+ for index in zones:
|
|
|
|
+ index_str = str(index)
|
|
|
|
+ if len(index_str) < 2:
|
|
|
|
+ index_str = "0" + index_str
|
|
|
|
+ zones_folder.append("zone"+index_str)
|
|
|
|
+
|
|
|
|
+ # shuffle list of zones (=> randomly choose zones)
|
|
|
|
+ random.shuffle(zones_folder)
|
|
|
|
+
|
|
|
|
+ for id_zone, zone_folder in enumerate(zones_folder):
|
|
|
|
+ zone_path = os.path.join(scene_path, zone_folder)
|
|
|
|
+ data_filename = _metric + "_" + _choice + generic_output_file_svd
|
|
|
|
+ data_file_path = os.path.join(zone_path, data_filename)
|
|
|
|
+
|
|
|
|
+ # getting number of line and read randomly lines
|
|
|
|
+ f = open(data_file_path)
|
|
|
|
+ lines = f.readlines()
|
|
|
|
+
|
|
|
|
+ counter = 0
|
|
|
|
+ # check if user select current scene and zone to be part of training data set
|
|
|
|
+ for line in lines:
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ begin, end = _interval
|
|
|
|
+
|
|
|
|
+ line_data = line.split(';')
|
|
|
|
+ metrics = line_data[begin+1:end+1]
|
|
|
|
+ metrics = [float(m) for m in metrics]
|
|
|
|
+
|
|
|
|
+ min_value = min(metrics)
|
|
|
|
+ max_value = max(metrics)
|
|
|
|
+
|
|
|
|
+ if min_value < min_value_interval:
|
|
|
|
+ min_value_interval = min_value
|
|
|
|
+
|
|
|
|
+ if max_value > max_value_interval:
|
|
|
|
+ max_value_interval = max_value
|
|
|
|
+
|
|
|
|
+ counter += 1
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+def generate_data_model(_filename, _interval, _choice, _metric, _scenes = scenes_list, _nb_zones = 4, _percent = 1, _norm = False, _sep=':', _index=True):
|
|
|
|
|
|
output_train_filename = _filename + ".train"
|
|
output_train_filename = _filename + ".train"
|
|
output_test_filename = _filename + ".test"
|
|
output_test_filename = _filename + ".test"
|
|
@@ -81,50 +151,54 @@ def generate_data_model(_filename, _interval, _choice, _metric, _scenes = scenes
|
|
scenes = [s for s in scenes if min_max_filename not in s]
|
|
scenes = [s for s in scenes if min_max_filename not in s]
|
|
|
|
|
|
for id_scene, folder_scene in enumerate(scenes):
|
|
for id_scene, folder_scene in enumerate(scenes):
|
|
- scene_path = os.path.join(path, folder_scene)
|
|
|
|
|
|
|
|
- zones_folder = []
|
|
|
|
- # create zones list
|
|
|
|
- for index in zones:
|
|
|
|
- index_str = str(index)
|
|
|
|
- if len(index_str) < 2:
|
|
|
|
- index_str = "0" + index_str
|
|
|
|
- zones_folder.append("zone"+index_str)
|
|
|
|
|
|
+ # only take care of maxwell scenes
|
|
|
|
+ if folder_scene in scenes_list:
|
|
|
|
+
|
|
|
|
+ scene_path = os.path.join(path, folder_scene)
|
|
|
|
|
|
- # shuffle list of zones (=> randomly choose zones)
|
|
|
|
- random.shuffle(zones_folder)
|
|
|
|
|
|
+ zones_folder = []
|
|
|
|
+ # create zones list
|
|
|
|
+ for index in zones:
|
|
|
|
+ index_str = str(index)
|
|
|
|
+ if len(index_str) < 2:
|
|
|
|
+ index_str = "0" + index_str
|
|
|
|
+ zones_folder.append("zone"+index_str)
|
|
|
|
|
|
- for id_zone, zone_folder in enumerate(zones_folder):
|
|
|
|
- zone_path = os.path.join(scene_path, zone_folder)
|
|
|
|
- data_filename = _metric + "_" + _choice + generic_output_file_svd
|
|
|
|
- data_file_path = os.path.join(zone_path, data_filename)
|
|
|
|
|
|
+ # shuffle list of zones (=> randomly choose zones)
|
|
|
|
+ random.shuffle(zones_folder)
|
|
|
|
|
|
- # getting number of line and read randomly lines
|
|
|
|
- f = open(data_file_path)
|
|
|
|
- lines = f.readlines()
|
|
|
|
|
|
+ for id_zone, zone_folder in enumerate(zones_folder):
|
|
|
|
+ zone_path = os.path.join(scene_path, zone_folder)
|
|
|
|
+ data_filename = _metric + "_" + _choice + generic_output_file_svd
|
|
|
|
+ data_file_path = os.path.join(zone_path, data_filename)
|
|
|
|
|
|
- num_lines = len(lines)
|
|
|
|
|
|
+ # getting number of line and read randomly lines
|
|
|
|
+ f = open(data_file_path)
|
|
|
|
+ lines = f.readlines()
|
|
|
|
|
|
- lines_indexes = np.arange(num_lines)
|
|
|
|
- random.shuffle(lines_indexes)
|
|
|
|
|
|
+ num_lines = len(lines)
|
|
|
|
|
|
- path_seuil = os.path.join(zone_path, seuil_expe_filename)
|
|
|
|
|
|
+ lines_indexes = np.arange(num_lines)
|
|
|
|
+ random.shuffle(lines_indexes)
|
|
|
|
|
|
- counter = 0
|
|
|
|
- # check if user select current scene and zone to be part of training data set
|
|
|
|
- for index in lines_indexes:
|
|
|
|
- line = construct_new_line(path_seuil, _interval, lines[index], _sep, _index)
|
|
|
|
|
|
+ path_seuil = os.path.join(zone_path, seuil_expe_filename)
|
|
|
|
|
|
- percent = counter / num_lines
|
|
|
|
|
|
+ counter = 0
|
|
|
|
+ # check if user select current scene and zone to be part of training data set
|
|
|
|
+ for index in lines_indexes:
|
|
|
|
+ line = construct_new_line(path_seuil, _interval, lines[index], _norm, _sep, _index)
|
|
|
|
|
|
- if id_zone < _nb_zones and folder_scene in _scenes and percent <= _percent:
|
|
|
|
- train_file.write(line)
|
|
|
|
- else:
|
|
|
|
- test_file.write(line)
|
|
|
|
|
|
+ percent = counter / num_lines
|
|
|
|
|
|
- counter += 1
|
|
|
|
|
|
+ if id_zone < _nb_zones and folder_scene in _scenes and percent <= _percent:
|
|
|
|
+ train_file.write(line)
|
|
|
|
+ else:
|
|
|
|
+ test_file.write(line)
|
|
|
|
|
|
- f.close()
|
|
|
|
|
|
+ counter += 1
|
|
|
|
+
|
|
|
|
+ f.close()
|
|
|
|
|
|
train_file.close()
|
|
train_file.close()
|
|
test_file.close()
|
|
test_file.close()
|
|
@@ -132,19 +206,21 @@ def generate_data_model(_filename, _interval, _choice, _metric, _scenes = scenes
|
|
|
|
|
|
def main():
|
|
def main():
|
|
|
|
|
|
|
|
+ p_custom = False
|
|
|
|
+
|
|
if len(sys.argv) <= 1:
|
|
if len(sys.argv) <= 1:
|
|
print('Run with default parameters...')
|
|
print('Run with default parameters...')
|
|
- print('python generate_data_model_random.py --output xxxx --interval 0,20 --kind svdne --metric lab --scenes "A, B, D" --nb_zones 5 --percent 0.7 --sep : --rowindex 1')
|
|
|
|
|
|
+ print('python generate_data_model_random.py --output xxxx --interval 0,20 --kind svdne --metric lab --scenes "A, B, D" --nb_zones 5 --percent 0.7 --sep : --rowindex 1 --custom min_max_filename')
|
|
sys.exit(2)
|
|
sys.exit(2)
|
|
try:
|
|
try:
|
|
- opts, args = getopt.getopt(sys.argv[1:], "ho:i:k:s:n:p:r", ["help=", "output=", "interval=", "kind=", "metric=","scenes=", "nb_zones=", "percent=", "sep=", "rowindex="])
|
|
|
|
|
|
+ opts, args = getopt.getopt(sys.argv[1:], "ho:i:k:s:n:p:r:c", ["help=", "output=", "interval=", "kind=", "metric=","scenes=", "nb_zones=", "percent=", "sep=", "rowindex=", "custom="])
|
|
except getopt.GetoptError:
|
|
except getopt.GetoptError:
|
|
# print help information and exit:
|
|
# print help information and exit:
|
|
- print('python generate_data_model_random.py --output xxxx --interval 0,20 --kind svdne --metric lab --scenes "A, B, D" --nb_zones 5 --percent 0.7 --sep : --rowindex 1')
|
|
|
|
|
|
+ print('python generate_data_model_random.py --output xxxx --interval 0,20 --kind svdne --metric lab --scenes "A, B, D" --nb_zones 5 --percent 0.7 --sep : --rowindex 1 --custom min_max_filename')
|
|
sys.exit(2)
|
|
sys.exit(2)
|
|
for o, a in opts:
|
|
for o, a in opts:
|
|
if o == "-h":
|
|
if o == "-h":
|
|
- print('python generate_data_model_random.py --output xxxx --interval 0,20 --kind svdne --metric lab --scenes "A, B, D" --nb_zones 5 --percent 0.7 --sep : --rowindex 1')
|
|
|
|
|
|
+ print('python generate_data_model_random.py --output xxxx --interval 0,20 --kind svdne --metric lab --scenes "A, B, D" --nb_zones 5 --percent 0.7 --sep : --rowindex 1 --custom min_max_filename')
|
|
sys.exit()
|
|
sys.exit()
|
|
elif o in ("-o", "--output"):
|
|
elif o in ("-o", "--output"):
|
|
p_filename = a
|
|
p_filename = a
|
|
@@ -167,6 +243,8 @@ def main():
|
|
p_rowindex = True
|
|
p_rowindex = True
|
|
else:
|
|
else:
|
|
p_rowindex = False
|
|
p_rowindex = False
|
|
|
|
+ elif o in ("-c", "--custom"):
|
|
|
|
+ p_custom = a
|
|
else:
|
|
else:
|
|
assert False, "unhandled option"
|
|
assert False, "unhandled option"
|
|
|
|
|
|
@@ -175,10 +253,25 @@ def main():
|
|
|
|
|
|
for scene_id in p_scenes:
|
|
for scene_id in p_scenes:
|
|
index = scenes_indexes.index(scene_id.strip())
|
|
index = scenes_indexes.index(scene_id.strip())
|
|
- scenes_selected.append(scenes[index])
|
|
|
|
|
|
+ scenes_selected.append(scenes_list[index])
|
|
|
|
+
|
|
|
|
+ # find min max value if necessary to renormalize data
|
|
|
|
+ if p_custom:
|
|
|
|
+ get_min_max_value_interval(p_filename, p_interval, p_kind, p_metric)
|
|
|
|
+
|
|
|
|
+ # write new file to save
|
|
|
|
+ if not os.path.exists(custom_min_max_folder):
|
|
|
|
+ os.makedirs(custom_min_max_folder)
|
|
|
|
+
|
|
|
|
+ min_max_folder_path = os.path.join(os.path.dirname(__file__), custom_min_max_folder)
|
|
|
|
+ min_max_filename_path = os.path.join(min_max_folder_path, p_custom)
|
|
|
|
+
|
|
|
|
+ with open(min_max_filename_path, 'w') as f:
|
|
|
|
+ f.write(str(min_value_interval) + '\n')
|
|
|
|
+ f.write(str(max_value_interval) + '\n')
|
|
|
|
|
|
# create database using img folder (generate first time only)
|
|
# create database using img folder (generate first time only)
|
|
- generate_data_model(p_filename, p_interval, p_kind, p_metric, scenes_selected, p_nb_zones, p_percent, p_sep, p_rowindex)
|
|
|
|
|
|
+ generate_data_model(p_filename, p_interval, p_kind, p_metric, scenes_selected, p_nb_zones, p_percent, p_custom, p_sep, p_rowindex)
|
|
|
|
|
|
if __name__== "__main__":
|
|
if __name__== "__main__":
|
|
main()
|
|
main()
|