lsun.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import os
  2. import numpy as np
  3. import PIL
  4. from PIL import Image
  5. from torch.utils.data import Dataset
  6. from torchvision import transforms
  7. class LSUNBase(Dataset):
  8. def __init__(self,
  9. txt_file,
  10. data_root,
  11. size=None,
  12. interpolation="bicubic",
  13. flip_p=0.5
  14. ):
  15. self.data_paths = txt_file
  16. self.data_root = data_root
  17. with open(self.data_paths, "r") as f:
  18. self.image_paths = f.read().splitlines()
  19. self._length = len(self.image_paths)
  20. self.labels = {
  21. "relative_file_path_": [l for l in self.image_paths],
  22. "file_path_": [os.path.join(self.data_root, l)
  23. for l in self.image_paths],
  24. }
  25. self.size = size
  26. self.interpolation = {"linear": PIL.Image.LINEAR,
  27. "bilinear": PIL.Image.BILINEAR,
  28. "bicubic": PIL.Image.BICUBIC,
  29. "lanczos": PIL.Image.LANCZOS,
  30. }[interpolation]
  31. self.flip = transforms.RandomHorizontalFlip(p=flip_p)
  32. def __len__(self):
  33. return self._length
  34. def __getitem__(self, i):
  35. example = dict((k, self.labels[k][i]) for k in self.labels)
  36. image = Image.open(example["file_path_"])
  37. if not image.mode == "RGB":
  38. image = image.convert("RGB")
  39. # default to score-sde preprocessing
  40. img = np.array(image).astype(np.uint8)
  41. crop = min(img.shape[0], img.shape[1])
  42. h, w, = img.shape[0], img.shape[1]
  43. img = img[(h - crop) // 2:(h + crop) // 2,
  44. (w - crop) // 2:(w + crop) // 2]
  45. image = Image.fromarray(img)
  46. if self.size is not None:
  47. image = image.resize((self.size, self.size), resample=self.interpolation)
  48. image = self.flip(image)
  49. image = np.array(image).astype(np.uint8)
  50. example["image"] = (image / 127.5 - 1.0).astype(np.float32)
  51. return example
  52. class LSUNChurchesTrain(LSUNBase):
  53. def __init__(self, **kwargs):
  54. super().__init__(txt_file="data/lsun/church_outdoor_train.txt", data_root="data/lsun/churches", **kwargs)
  55. class LSUNChurchesValidation(LSUNBase):
  56. def __init__(self, flip_p=0., **kwargs):
  57. super().__init__(txt_file="data/lsun/church_outdoor_val.txt", data_root="data/lsun/churches",
  58. flip_p=flip_p, **kwargs)
  59. class LSUNBedroomsTrain(LSUNBase):
  60. def __init__(self, **kwargs):
  61. super().__init__(txt_file="data/lsun/bedrooms_train.txt", data_root="data/lsun/bedrooms", **kwargs)
  62. class LSUNBedroomsValidation(LSUNBase):
  63. def __init__(self, flip_p=0.0, **kwargs):
  64. super().__init__(txt_file="data/lsun/bedrooms_val.txt", data_root="data/lsun/bedrooms",
  65. flip_p=flip_p, **kwargs)
  66. class LSUNCatsTrain(LSUNBase):
  67. def __init__(self, **kwargs):
  68. super().__init__(txt_file="data/lsun/cat_train.txt", data_root="data/lsun/cats", **kwargs)
  69. class LSUNCatsValidation(LSUNBase):
  70. def __init__(self, flip_p=0., **kwargs):
  71. super().__init__(txt_file="data/lsun/cat_val.txt", data_root="data/lsun/cats",
  72. flip_p=flip_p, **kwargs)