reconstruct.py 3.0 KB

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