run.py 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. from PULSE import PULSE
  2. from torch.utils.data import Dataset, DataLoader
  3. from torch.nn import DataParallel
  4. from pathlib import Path
  5. from PIL import Image
  6. import torchvision
  7. from math import log10, ceil
  8. import argparse
  9. class Images(Dataset):
  10. def __init__(self, root_dir, duplicates):
  11. self.root_path = Path(root_dir)
  12. self.image_list = list(self.root_path.glob("*.png"))
  13. self.duplicates = duplicates # Number of times to duplicate the image in the dataset to produce multiple HR images
  14. def __len__(self):
  15. return self.duplicates*len(self.image_list)
  16. def __getitem__(self, idx):
  17. img_path = self.image_list[idx//self.duplicates]
  18. image = torchvision.transforms.ToTensor()(Image.open(img_path))
  19. if(self.duplicates == 1):
  20. return image,img_path.stem
  21. else:
  22. return image,img_path.stem+f"_{(idx % self.duplicates)+1}"
  23. parser = argparse.ArgumentParser(description='PULSE')
  24. #I/O arguments
  25. parser.add_argument('-input_dir', type=str, default='input', help='input data directory')
  26. parser.add_argument('-output_dir', type=str, default='runs', help='output data directory')
  27. parser.add_argument('-cache_dir', type=str, default='cache', help='cache directory for model weights')
  28. parser.add_argument('-duplicates', type=int, default=1, help='How many HR images to produce for every image in the input directory')
  29. parser.add_argument('-batch_size', type=int, default=1, help='Batch size to use during optimization')
  30. #PULSE arguments
  31. parser.add_argument('-seed', type=int, help='manual seed to use')
  32. parser.add_argument('-loss_str', type=str, default="100*L2+0.05*GEOCROSS", help='Loss function to use')
  33. parser.add_argument('-eps', type=float, default=2e-3, help='Target for downscaling loss (L2)')
  34. parser.add_argument('-noise_type', type=str, default='trainable', help='zero, fixed, or trainable')
  35. parser.add_argument('-num_trainable_noise_layers', type=int, default=5, help='Number of noise layers to optimize')
  36. parser.add_argument('-tile_latent', action='store_true', help='Whether to forcibly tile the same latent 18 times')
  37. parser.add_argument('-bad_noise_layers', type=str, default="17", help='List of noise layers to zero out to improve image quality')
  38. parser.add_argument('-opt_name', type=str, default='adam', help='Optimizer to use in projected gradient descent')
  39. parser.add_argument('-learning_rate', type=float, default=0.4, help='Learning rate to use during optimization')
  40. parser.add_argument('-steps', type=int, default=100, help='Number of optimization steps')
  41. parser.add_argument('-lr_schedule', type=str, default='linear1cycledrop', help='fixed, linear1cycledrop, linear1cycle')
  42. parser.add_argument('-save_intermediate', action='store_true', help='Whether to store and save intermediate HR and LR images during optimization')
  43. kwargs = vars(parser.parse_args())
  44. dataset = Images(kwargs["input_dir"], duplicates=kwargs["duplicates"])
  45. out_path = Path(kwargs["output_dir"])
  46. out_path.mkdir(parents=True, exist_ok=True)
  47. dataloader = DataLoader(dataset, batch_size=kwargs["batch_size"])
  48. model = PULSE(cache_dir=kwargs["cache_dir"])
  49. model = DataParallel(model)
  50. toPIL = torchvision.transforms.ToPILImage()
  51. for ref_im, ref_im_name in dataloader:
  52. if(kwargs["save_intermediate"]):
  53. padding = ceil(log10(100))
  54. for i in range(kwargs["batch_size"]):
  55. int_path_HR = Path(out_path / ref_im_name[i] / "HR")
  56. int_path_LR = Path(out_path / ref_im_name[i] / "LR")
  57. int_path_HR.mkdir(parents=True, exist_ok=True)
  58. int_path_LR.mkdir(parents=True, exist_ok=True)
  59. for j,(HR,LR) in enumerate(model(ref_im,**kwargs)):
  60. for i in range(kwargs["batch_size"]):
  61. toPIL(HR[i].cpu().detach().clamp(0, 1)).save(
  62. int_path_HR / f"{ref_im_name[i]}_{j:0{padding}}.png")
  63. toPIL(LR[i].cpu().detach().clamp(0, 1)).save(
  64. int_path_LR / f"{ref_im_name[i]}_{j:0{padding}}.png")
  65. else:
  66. #out_im = model(ref_im,**kwargs)
  67. for j,(HR,LR) in enumerate(model(ref_im,**kwargs)):
  68. for i in range(kwargs["batch_size"]):
  69. toPIL(HR[i].cpu().detach().clamp(0, 1)).save(
  70. out_path / f"{ref_im_name[i]}.png")