-
Notifications
You must be signed in to change notification settings - Fork 1
/
dtl_dataloader.py
90 lines (67 loc) · 2.39 KB
/
dtl_dataloader.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
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/python
# -*- encoding: utf-8 -*-
import torch
from torch.utils.data import Dataset
import torchvision.transforms as transforms
import os.path as osp
import os
from PIL import Image
import numpy as np
import json
from transform_event import *
from collections import namedtuple
import glob
# enable eager mode
class EventAPS_Dataset(Dataset):
def __init__(self, cfg, mode='train', crop_size=(256, 512), *args, **kwargs):
super(EventAPS_Dataset, self).__init__(*args, **kwargs)
assert mode in ('ldr', 'general')
self.mode = mode
self.cfg = cfg
self.crop_h, self.crop_w = crop_size[0], crop_size[1]
self.imgs = {}
imgnames = []
impth = osp.join('./dataset/dtl_data', 'images', mode)
images = glob.glob(osp.join(impth, '*.png'))
names = [osp.basename(el.split('.')[1]) for el in images]
impths = images
imgnames.extend(names)
self.imnames = imgnames
self.len = len(self.imnames)
self.imgs.update(dict(zip(names, impths)))
## pre-processing
self.to_tensor_event = transforms.Compose([
transforms.ToTensor(),
])
def __getitem__(self, idx):
fn = self.imnames[idx]
impth = self.imgs[fn]
img = Image.open(impth).convert("RGB")
name = os.path.splitext(os.path.basename(impth))[0]
w, h = img.size
w2 = int(w / 2)
event = img.crop((0, 0, w2, h)) # crop entire image to get event
# apply the same transform to both A and B
event = event.resize((self.crop_w, self.crop_h), Image.BICUBIC) # resize event
event = self.to_tensor_event(event)
return event, name
def __len__(self):
return self.len
def convert_labels(self, label):
for k, v in self.lb_map.items():
label[label == k] = v
return label
if __name__ == "__main__":
from tqdm import tqdm
from torch.utils.data import DataLoader
ds = EventAPS_Dataset('./dataset/eventdataset', mode='train')
dl = DataLoader(ds,
batch_size=4,
shuffle=True,
num_workers=4,
drop_last=True)
for imgs, label in dl:
print(len(imgs))
for el in imgs:
print(el.size())
break