File size: 1,149 Bytes
bb0f5a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os

from torch.utils.data import Dataset
from PIL import Image

from PTI.utils.data_utils import make_dataset
from torchvision import transforms


class Image2Dataset(Dataset):
    def __init__(self, image) -> None:
        super().__init__()
        self.image = image
        self.transform = transforms.Compose(
            [
                transforms.ToTensor(),
                transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5]),
            ]
        )

    def __len__(self):
        return 1

    def __getitem__(self, index):
        return "customIMG", self.transform(self.image)


class ImagesDataset(Dataset):
    def __init__(self, source_root, source_transform=None):
        self.source_paths = sorted(make_dataset(source_root))
        self.source_transform = source_transform

    def __len__(self):
        return len(self.source_paths)

    def __getitem__(self, index):
        fname, from_path = self.source_paths[index]
        from_im = Image.open(from_path).convert("RGB").resize([1024, 1024])

        if self.source_transform:
            from_im = self.source_transform(from_im)

        return fname, from_im