You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

kvasir_SEG.py 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import os
  2. import os.path as osp
  3. from utils.transform import *
  4. from torch.utils.data import Dataset
  5. from torchvision import transforms
  6. # KavSir-SEG Dataset
  7. class kvasir_SEG(Dataset):
  8. def __init__(self, root, data2_dir, mode='train', transform=None):
  9. super(kvasir_SEG, self).__init__()
  10. data_path = osp.join(root, data2_dir)
  11. self.imglist = []
  12. self.gtlist = []
  13. datalist = os.listdir(osp.join(data_path, 'images'))
  14. for data in datalist:
  15. self.imglist.append(osp.join(data_path+'/images', data))
  16. self.gtlist.append(osp.join(data_path+'/masks', data))
  17. if transform is None:
  18. if mode == 'train':
  19. transform = transforms.Compose([
  20. Resize((320,320 )),
  21. RandomHorizontalFlip(),
  22. RandomVerticalFlip(),
  23. RandomRotation(90),
  24. RandomZoom((0.9, 1.1)),
  25. Translation(10),
  26. RandomCrop((256, 256)),
  27. ToTensor(),
  28. ])
  29. elif mode == 'valid' or mode == 'test':
  30. transform = transforms.Compose([
  31. Resize((320, 320)),
  32. ToTensor(),
  33. ])
  34. self.transform = transform
  35. def __getitem__(self, index):
  36. img_path = self.imglist[index]
  37. gt_path = self.gtlist[index]
  38. img = Image.open(img_path).convert('RGB')
  39. gt = Image.open(gt_path).convert('L')
  40. data = {'image': img, 'label': gt}
  41. if self.transform:
  42. data = self.transform(data)
  43. return data
  44. def __len__(self):
  45. return len(self.imglist)
  46. """
  47. class test_dataset:
  48. def __init__(self, image_root, gt_root, testsize):
  49. self.testsize = testsize
  50. self.images = [image_root + f for f in os.listdir(image_root) if f.endswith('.jpg') or f.endswith('.png')]
  51. self.gts = [gt_root + f for f in os.listdir(gt_root) if f.endswith('.tif') or f.endswith('.png')]
  52. self.images = sorted(self.images)
  53. self.gts = sorted(self.gts)
  54. self.transform = transforms.Compose([
  55. transforms.Resize((self.testsize, self.testsize)),
  56. transforms.ToTensor(),
  57. #transforms.Normalize([0.485, 0.456, 0.406],
  58. #[0.229, 0.224, 0.225])])
  59. self.gt_transform = transforms.ToTensor()
  60. self.size = len(self.images)
  61. self.index = 0
  62. def load_data(self):
  63. image = self.rgb_loader(self.images[self.index])
  64. image = self.transform(image).unsqueeze(0)
  65. gt = self.binary_loader(self.gts[self.index])
  66. name = self.images[self.index].split('/')[-1]
  67. if name.endswith('.jpg'):
  68. name = name.split('.jpg')[0] + '.png'
  69. self.index += 1
  70. return image, gt, name
  71. def rgb_loader(self, path):
  72. with open(path, 'rb') as f:
  73. img = Image.open(f)
  74. return img.convert('RGB')
  75. def binary_loader(self, path):
  76. with open(path, 'rb') as f:
  77. img = Image.open(f)
  78. return img.convert('L')
  79. """