iotools.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # encoding: utf-8
  2. """
  3. @author: sherlock
  4. @contact: sherlockliao01@gmail.com
  5. """
  6. from PIL import Image, ImageFile
  7. import errno
  8. import json
  9. import pickle as pkl
  10. import os
  11. import os.path as osp
  12. import yaml
  13. from easydict import EasyDict as edict
  14. ImageFile.LOAD_TRUNCATED_IMAGES = True
  15. def read_image(img_path):
  16. """Keep reading image until succeed.
  17. This can avoid IOError incurred by heavy IO process."""
  18. got_img = False
  19. if not osp.exists(img_path):
  20. raise IOError("{} does not exist".format(img_path))
  21. while not got_img:
  22. try:
  23. img = Image.open(img_path).convert('RGB')
  24. got_img = True
  25. except IOError:
  26. print("IOError incurred when reading '{}'. Will redo. Don't worry. Just chill.".format(img_path))
  27. pass
  28. return img
  29. def mkdir_if_missing(directory):
  30. if not osp.exists(directory):
  31. try:
  32. os.makedirs(directory)
  33. except OSError as e:
  34. if e.errno != errno.EEXIST:
  35. raise
  36. def check_isfile(path):
  37. isfile = osp.isfile(path)
  38. if not isfile:
  39. print("=> Warning: no file found at '{}' (ignored)".format(path))
  40. return isfile
  41. def read_json(fpath):
  42. with open(fpath, 'r') as f:
  43. obj = json.load(f)
  44. return obj
  45. def write_json(obj, fpath):
  46. mkdir_if_missing(osp.dirname(fpath))
  47. with open(fpath, 'w') as f:
  48. json.dump(obj, f, indent=4, separators=(',', ': '))
  49. def get_text_embedding(path, length):
  50. with open(path, 'rb') as f:
  51. word_frequency = pkl.load(f)
  52. def save_train_configs(path, args):
  53. if not os.path.exists(path):
  54. os.makedirs(path)
  55. with open(f'{path}/configs.yaml', 'w') as f:
  56. yaml.dump(vars(args), f, default_flow_style=False)
  57. def load_train_configs(path):
  58. with open(path, 'r') as f:
  59. args = yaml.load(f, Loader=yaml.FullLoader)
  60. return edict(args)