generate_data_model_random_augmented.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. # main imports
  2. import sys, os, argparse
  3. import numpy as np
  4. import pandas as pd
  5. import random
  6. # image processing imports
  7. from PIL import Image
  8. from ipfml import utils
  9. # modules imports
  10. sys.path.insert(0, '') # trick to enable import of main folder module
  11. import custom_config as cfg
  12. from modules.utils import data as dt
  13. from data_attributes import get_image_features
  14. # getting configuration information
  15. learned_folder = cfg.learned_zones_folder
  16. min_max_filename = cfg.min_max_filename_extension
  17. # define all scenes variables
  18. all_scenes_list = cfg.scenes_names
  19. all_scenes_indices = cfg.scenes_indices
  20. normalization_choices = cfg.normalization_choices
  21. zones = cfg.zones_indices
  22. seuil_expe_filename = cfg.seuil_expe_filename
  23. renderer_choices = cfg.renderer_choices
  24. features_choices = cfg.features_choices_labels
  25. output_data_folder = cfg.output_data_folder
  26. custom_min_max_folder = cfg.min_max_custom_folder
  27. min_max_ext = cfg.min_max_filename_extension
  28. generic_output_file_svd = '_random.csv'
  29. min_value_interval = sys.maxsize
  30. max_value_interval = 0
  31. def construct_new_line(interval, line_data, choice, each, norm):
  32. begin, end = interval
  33. label = line_data[2]
  34. features = line_data[begin+3:end+3]
  35. # keep only if modulo result is 0 (keep only each wanted values)
  36. features = [float(m) for id, m in enumerate(features) if id % each == 0]
  37. # TODO : check if it's always necessary to do that (loss of information for svd)
  38. if norm:
  39. if choice == 'svdne':
  40. features = utils.normalize_arr_with_range(features, min_value_interval, max_value_interval)
  41. if choice == 'svdn':
  42. features = utils.normalize_arr(features)
  43. line = label
  44. for val in features:
  45. line += ';'
  46. line += str(val)
  47. line += '\n'
  48. return line
  49. def get_min_max_value_interval(_path, _scenes_list, _interval, _feature):
  50. global min_value_interval, max_value_interval
  51. data_filename = _feature + "_svd" + generic_output_file_svd
  52. data_file_path = os.path.join(_path, data_filename)
  53. # getting number of line and read randomly lines
  54. f = open(data_file_path)
  55. lines = f.readlines()
  56. # check if user select current scene and zone to be part of training data set
  57. for line in lines:
  58. begin, end = _interval
  59. line_data = line.split(';')
  60. features = line_data[begin+3:end+3]
  61. features = [float(m) for m in features]
  62. min_value = min(features)
  63. max_value = max(features)
  64. if min_value < min_value_interval:
  65. min_value_interval = min_value
  66. if max_value > max_value_interval:
  67. max_value_interval = max_value
  68. def generate_data_model(_path, _scenes_list, _filename, _interval, _choice, _feature, _scenes, _nb_zones = 4, _percent = 1, _random=0, _step=1, _each=1, _custom = False):
  69. output_train_filename = _filename + ".train"
  70. output_test_filename = _filename + ".test"
  71. if not '/' in output_train_filename:
  72. raise Exception("Please select filename with directory path to save data. Example : data/dataset")
  73. # create path if not exists
  74. if not os.path.exists(output_data_folder):
  75. os.makedirs(output_data_folder)
  76. train_file_data = []
  77. test_file_data = []
  78. # if custom normalization choices then we use svd values not already normalized
  79. if _custom:
  80. data_filename = _feature + "_svd"+ generic_output_file_svd
  81. else:
  82. data_filename = _feature + "_" + _choice + generic_output_file_svd
  83. data_file_path = os.path.join(_path, data_filename)
  84. # getting number of line and read randomly lines
  85. f = open(data_file_path)
  86. lines = f.readlines()
  87. num_lines = len(lines)
  88. # randomly shuffle image
  89. if _random:
  90. random.shuffle(lines)
  91. counter = 0
  92. # check if user select current scene data line of training data set
  93. for data in lines:
  94. percent = counter / num_lines
  95. data = data.split(';')
  96. scene_name = data[0]
  97. image_index = int(data[1])
  98. if image_index % _step == 0:
  99. line = construct_new_line(_interval, data, _choice, int(_each), _custom)
  100. if scene_name in _scenes and percent <= _percent:
  101. train_file_data.append(line)
  102. else:
  103. test_file_data.append(line)
  104. counter += 1
  105. f.close()
  106. train_file = open(output_train_filename, 'w')
  107. test_file = open(output_test_filename, 'w')
  108. for line in train_file_data:
  109. train_file.write(line)
  110. for line in test_file_data:
  111. test_file.write(line)
  112. train_file.close()
  113. test_file.close()
  114. def main():
  115. # getting all params
  116. parser = argparse.ArgumentParser(description="Generate data for model using correlation matrix information from data")
  117. parser.add_argument('--output', type=str, help='output file name desired (.train and .test)')
  118. parser.add_argument('--folder', type=str, help='folder path of data augmented database')
  119. parser.add_argument('--interval', type=str, help='Interval value to keep from svd', default='"0, 200"')
  120. parser.add_argument('--kind', type=str, help='Kind of normalization level wished', choices=normalization_choices)
  121. parser.add_argument('--feature', type=str, help='feature data choice', choices=features_choices)
  122. parser.add_argument('--scenes', type=str, help='List of scenes to use for training data')
  123. parser.add_argument('--random', type=int, help='Data will be randomly filled or not', choices=[0, 1])
  124. parser.add_argument('--percent', type=float, help='Percent of data use for train and test dataset (by default 1)')
  125. parser.add_argument('--step', type=int, help='Photo step to keep for build datasets', default=1)
  126. parser.add_argument('--each', type=int, help='Each features to keep from interval', default=1)
  127. parser.add_argument('--renderer', type=str, help='Renderer choice in order to limit scenes used', choices=renderer_choices, default='all')
  128. parser.add_argument('--custom', type=str, help='Name of custom min max file if use of renormalization of data', default=False)
  129. args = parser.parse_args()
  130. p_filename = args.output
  131. p_folder = args.folder
  132. p_interval = list(map(int, args.interval.split(',')))
  133. p_kind = args.kind
  134. p_feature = args.feature
  135. p_scenes = args.scenes.split(',')
  136. p_random = args.random
  137. p_percent = args.percent
  138. p_step = args.step
  139. p_each = args.each
  140. p_renderer = args.renderer
  141. p_custom = args.custom
  142. # list all possibles choices of renderer
  143. scenes_list = dt.get_renderer_scenes_names(p_renderer)
  144. scenes_indices = dt.get_renderer_scenes_indices(p_renderer)
  145. # getting scenes from indexes user selection
  146. scenes_selected = []
  147. for scene_id in p_scenes:
  148. index = scenes_indices.index(scene_id.strip())
  149. scenes_selected.append(scenes_list[index])
  150. # find min max value if necessary to renormalize data
  151. if p_custom:
  152. get_min_max_value_interval(p_folder, scenes_list, p_interval, p_feature)
  153. # write new file to save
  154. if not os.path.exists(custom_min_max_folder):
  155. os.makedirs(custom_min_max_folder)
  156. min_max_filename_path = os.path.join(custom_min_max_folder, p_custom)
  157. with open(min_max_filename_path, 'w') as f:
  158. f.write(str(min_value_interval) + '\n')
  159. f.write(str(max_value_interval) + '\n')
  160. # create database using img folder (generate first time only)
  161. generate_data_model(p_folder, scenes_list, p_filename, p_interval, p_kind, p_feature, scenes_selected, p_percent, p_random, p_step, p_each, p_custom)
  162. if __name__== "__main__":
  163. main()