generate_data_model_random_augmented.py 7.7 KB

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