-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatasets.py
36 lines (33 loc) · 1.45 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
import glob
import random
import os
import numpy as np
from jittor.dataset.dataset import Dataset
import jittor.transform as transform
from PIL import Image
class ImageDataset(Dataset):
def __init__(self, root, mode, transforms=None):
super().__init__()
self.transforms = transform.Compose(transforms)
self.mode = mode
if self.mode == 'train':
self.files = sorted(glob.glob(os.path.join(root, mode, "imgs") + "/*.*"))
self.labels = sorted(glob.glob(os.path.join(root, mode, "labels") + "/*.*")) #glob.glob搜索函数
self.set_attrs(total_len=len(self.labels))
print(f"from {mode} split load {self.total_len} images.")
def __getitem__(self, index):
label_path = self.labels[index % len(self.labels)]
label_path=os.path.normpath(label_path)
photo_id = label_path.split('/')[-1][:-4]
img_B = Image.open(label_path)
img_B = Image.fromarray(np.array(img_B).astype("uint8")[:, :, np.newaxis].repeat(3,2))
if self.mode == "train":
img_A = Image.open(self.files[index % len(self.files)])
if np.random.random() < 0.5:
img_A = Image.fromarray(np.array(img_A)[:, ::-1, :], "RGB")
img_B = Image.fromarray(np.array(img_B)[:, ::-1, :], "RGB")
img_A = self.transforms(img_A)
else:
img_A = np.empty([1])
img_B = self.transforms(img_B)
return img_A, img_B, photo_id