Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. class ImageDataset(Dataset):
  2. def __init__(self, highres, transform=None):
  3. """
  4. highres(list): high resolution np image list
  5. transform(callable, optional): Optional transform to be applied to a sample
  6. """
  7. self.highres = highres
  8. self.transform = transform
  9.  
  10. def __len__(self):
  11. return len(self.highres)
  12.  
  13. def __getitem__(self, idx):
  14. image_hr = self.highres[idx]
  15.  
  16. rc = transforms.RandomCrop([41,41])
  17. toPIL = transforms.ToPILImage()
  18. toTens = transforms.ToTensor()
  19.  
  20. #don't convert to tensor then .numpy()
  21. image_hr = np.array(rc(toPIL(image_hr)))
  22.  
  23. image_lr2 = cv2.resize(image_hr, (0, 0), fx=0.5, fy=0.5, interpolation = cv2.INTER_CUBIC)
  24. image_lr2 = cv2.resize(image_lr2, (41, 41), interpolation = cv2.INTER_CUBIC)
  25.  
  26. image_lr3 = cv2.resize(image_hr, (0, 0), fx=0.33, fy=0.33, interpolation = cv2.INTER_CUBIC)
  27. image_lr3 = cv2.resize(image_lr3, (41, 41), interpolation = cv2.INTER_CUBIC)
  28.  
  29. image_lr4 = cv2.resize(image_hr, (0, 0), fx=0.25, fy=0.25, interpolation = cv2.INTER_CUBIC)
  30. image_lr4 = cv2.resize(image_lr4, (41, 41), interpolation = cv2.INTER_CUBIC)
  31.  
  32. sample = {'hr': image_hr, 'lr2': image_lr2, 'lr3': image_lr3, 'lr4': image_lr4}
  33.  
  34. if self.transform:
  35. sample['hr'] = self.transform(sample['hr'])
  36. sample['lr2'] = self.transform(sample['lr2'])
  37. sample['lr3'] = self.transform(sample['lr3'])
  38. sample['lr4'] = self.transform(sample['lr4'])
  39.  
  40. return sample
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement