reconstruct.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # main imports
  2. import numpy as np
  3. import pandas as pd
  4. import os, sys, argparse
  5. # models imports
  6. from sklearn import linear_model
  7. from sklearn import svm
  8. from sklearn.utils import shuffle
  9. from joblib import dump, load
  10. # image processing imports
  11. from ipfml import metrics
  12. from PIL import Image
  13. # modules and config imports
  14. sys.path.insert(0, '') # trick to enable import of main folder module
  15. import custom_config as cfg
  16. from features import compute_feature
  17. def reconstruct(_scene_name, _model_path, _n, _feature_choice):
  18. # construct the empty output image
  19. output_image = np.empty([cfg.number_of_rows, cfg.number_of_columns])
  20. # load the trained model
  21. clf = load(_model_path)
  22. # load scene and its `n` first pixel value data
  23. scene_path = os.path.join(cfg.dataset_path, _scene_name)
  24. for id_column in range(cfg.number_of_columns):
  25. folder_path = os.path.join(scene_path, str(id_column))
  26. pixels = []
  27. for id_row in range(cfg.number_of_rows):
  28. pixel_filename = _scene_name + '_' + str(id_column) + '_' + str(id_row) + ".dat"
  29. pixel_file_path = os.path.join(folder_path, pixel_filename)
  30. with open(pixel_file_path, 'r') as f:
  31. # predict the expected pixel value
  32. lines = [float(l)/255. for l in f.readlines()]
  33. pixel_values = lines[0:int(_n)]
  34. data = compute_feature(_feature_choice, pixel_values)
  35. pixels.append(data)
  36. # predict column pixels and fill image column by column
  37. pixels_predicted = clf.predict(pixels)
  38. # change normalized predicted value to pixel value
  39. pixels_predicted = pixels_predicted*255.
  40. for id_pixel, pixel in enumerate(pixels_predicted):
  41. output_image[id_pixel, id_column] = pixel
  42. print("{0:.2f}%".format(id_column / cfg.number_of_columns * 100))
  43. sys.stdout.write("\033[F")
  44. return output_image
  45. def main():
  46. parser = argparse.ArgumentParser(description="Train model and saved it")
  47. parser.add_argument('--scene', type=str, help='Scene name to reconstruct', choices=cfg.scenes_list)
  48. parser.add_argument('--model_path', type=str, help='Model file path')
  49. parser.add_argument('--n', type=str, help='Number of pixel values approximated to keep')
  50. parser.add_argument('--feature', type=str, help='Feature choice to compute from samples', choices=cfg.features_list)
  51. parser.add_argument('--image_name', type=str, help="The ouput image name")
  52. args = parser.parse_args()
  53. param_scene_name = args.scene
  54. param_n = args.n
  55. param_feature = args.feature
  56. param_model_path = args.model_path
  57. param_image_name = args.image_name
  58. # get default value of `n` param
  59. if not param_n:
  60. param_n = param_model_path.split('_')[0]
  61. output_image = reconstruct(param_scene_name, param_model_path, param_n, param_feature)
  62. if not os.path.exists(cfg.reconstructed_folder):
  63. os.makedirs(cfg.reconstructed_folder)
  64. image_path = os.path.join(cfg.reconstructed_folder, param_image_name)
  65. img = Image.fromarray(np.uint8(output_image))
  66. img.save(image_path)
  67. if __name__== "__main__":
  68. main()