-
Notifications
You must be signed in to change notification settings - Fork 2
/
dataset.py
51 lines (39 loc) · 1.48 KB
/
dataset.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from torch.utils.data import Dataset
import torch
import pandas as pd
from PIL import Image,ImageOps
import numpy as np
class UnderWater(Dataset):
"""UnderWater Dataset."""
def __init__(self, csv_file = "data.csv", transforms=None):
"""
Args:
csv_file (string): Path to the csv file with annotations.
root_dir (string): Directory with all the images.
transform (callable, optional): Optional transform to be applied
on a sample.
"""
self.files_list = pd.read_csv(csv_file)
self.transforms = transforms
def __len__(self):
return len(self.files_list)
def __getitem__(self, idx):
if torch.is_tensor(idx):
idx = idx.tolist()
uw_image = self.files_list["underwater"].iloc[idx]
gt_image = self.files_list["gt"].iloc[idx]
uw_image = Image.open(uw_image)
gt_image = Image.open(gt_image)
flip = np.random.randint(0,2)
mirror = np.random.randint(0,2)
if flip==True:
uw_image = ImageOps.flip(uw_image)
gt_image = ImageOps.flip(gt_image)
if mirror==True:
uw_image = ImageOps.mirror(uw_image)
gt_image = ImageOps.mirror(gt_image)
if self.transforms is not None:
uw_image = self.transforms(uw_image)
gt_image = self.transforms(gt_image)
sample = {'uw_image': uw_image, 'gt_image': gt_image}
return sample