forked from merlresearch/InSeGAN-ICCV2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset.py
79 lines (68 loc) · 2.37 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python3
# Copyright (c) 2021,2022 Mitsubishi Electric Research Laboratories (MERL)
#
# SPDX-License-Identifier: AGPL-3.0-or-later
import os
import glob
from PIL import Image
from torch.utils.data import Dataset
import torchvision.transforms as transforms
IMG_HEIGHT = 64
INIT_CROP = 214# 600 #214
train_transforms = [
transforms.CenterCrop(INIT_CROP),
transforms.Resize(IMG_HEIGHT),
transforms.CenterCrop(IMG_HEIGHT),
transforms.RandomHorizontalFlip(),
transforms.RandomVerticalFlip(),
transforms.RandomRotation((-20,20)),
transforms.Grayscale(num_output_channels=1),
transforms.ToTensor(),
transforms.Normalize(mean=[0.5], std=[0.5]),
]
test_transforms = [
transforms.CenterCrop(INIT_CROP),
transforms.Resize(IMG_HEIGHT),
transforms.CenterCrop(IMG_HEIGHT),
transforms.Grayscale(num_output_channels=1),
transforms.ToTensor(),
transforms.Normalize(mean=[0.5], std=[0.5]),
]
gt_transforms = [
transforms.CenterCrop(INIT_CROP),
transforms.Resize(IMG_HEIGHT,interpolation=Image.NEAREST),
transforms.CenterCrop(IMG_HEIGHT),
transforms.ToTensor()
]
class ObjectDataset(Dataset):
def __init__(self,
root,
transforms_=None,
mode='train'):
self.transform = transforms.Compose(transforms_)
self.files = glob.glob('%s/*.png' % root)
def __getitem__(self, index):
filepath = self.files[index % len(self.files)]
img = self.transform(Image.open(filepath).convert('RGB'))
return img
def __len__(self):
return len(self.files)
class ObjectDatasetTest(Dataset):
def __init__(self,
root,
gtroot,
transforms_= None,
gt_transforms_ = None,
mode='test'):
self.transform = transforms.Compose(transforms_)
self.gt_transform = transforms.Compose(gt_transforms_)
self.files = glob.glob('%s/*.png' % root)
self.gtroot = gtroot
def __getitem__(self, index):
filepath = self.files[index % len(self.files)]
gtfile = os.path.join(self.gtroot, filepath.split('/')[-1].replace('depth', 'rgb'))
img = self.transform(Image.open(filepath).convert('RGB'))
gt = self.gt_transform(Image.open(gtfile).convert('RGB'))
return (img, gt)
def __len__(self):
return len(self.files)