-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdatasets.py
72 lines (62 loc) · 2.71 KB
/
datasets.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import os
import os.path
import torch.utils.data as data
from PIL import Image
def make_dataset(root):
if isinstance(root,str):
image_path = os.path.join(root, 'Imgs')
#image_path = os.path.join(root, 'without_Edge')
mask_path = os.path.join(root, 'GT')
img_list = [f for f in os.listdir(image_path) if f.endswith('.jpg') or f.endswith('png')]
imgs = [(os.path.join(image_path, img_name), os.path.join(mask_path, os.path.splitext(img_name)[0] + '.png')) for img_name in img_list]
return imgs
else:
imgs = []
for root_dir in root:
image_path = os.path.join(root_dir, 'Imgs')
# image_path = os.path.join(root, 'without_Edge')
mask_path = os.path.join(root_dir, 'GT')
img_list = [f for f in os.listdir(image_path) if f.endswith('.jpg') or f.endswith('png')]
sub_imgs = [
(os.path.join(image_path, img_name), os.path.join(mask_path, os.path.splitext(img_name)[0] + '.png'))
for img_name in img_list]
imgs.extend(sub_imgs)
return imgs
class ImageFolder(data.Dataset):
# image and gt should be in the same folder and have same filename except extended name (jpg and png respectively)
def __init__(self, root, joint_transform=None, transform=None, target_transform=None):
self.root = root
self.imgs = make_dataset(root)
self.imgs = self.imgs
self.joint_transform = joint_transform
self.transform = transform
self.target_transform = target_transform
def __getitem__(self, index):
img_path, gt_path = self.imgs[index]
img = Image.open(img_path).convert('RGB')
target = Image.open(gt_path).convert('L')
if self.joint_transform is not None:
img, target = self.joint_transform(img, target)
if self.transform is not None:
img = self.transform(img)
if self.target_transform is not None:
target = self.target_transform(target)
return img, target
def __len__(self):
return len(self.imgs)
class ImageFolder_test(data.Dataset):
# image and gt should be in the same folder and have same filename except extended name (jpg and png respectively)
def __init__(self, root, transform=None):
self.root = root
self.imgs = make_dataset(root)
self.transform = transform
def __getitem__(self, index):
img_path, gt_path = self.imgs[index]
img = Image.open(img_path).convert('RGB')
if self.transform is not None:
img = self.transform(img)
return img
def __len__(self):
return len(self.imgs)
if __name__ == "__main__":
print(isinstance('path',str))