-
Notifications
You must be signed in to change notification settings - Fork 3
/
dataset.py
60 lines (49 loc) · 1.95 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
import torch
from torch.utils.data import Dataset
import os
from image import *
class listDataset(Dataset):
def __init__(self, root, shape=None, shuffle=True, transform=None, train=False, seen=0, batch_size=1, num_workers=4):
if train:
random.shuffle(root)
self.nSamples = len(root)
self.lines = root
self.transform = transform
self.train = train
self.shape = shape
self.seen = seen
self.batch_size = batch_size
self.num_workers = num_workers
def __len__(self):
return self.nSamples
def __getitem__(self, index):
assert index <= len(self), 'index range error'
img_path = self.lines[index]
fname = os.path.basename(img_path)
img,target,kpoint,sigma_map= load_data(img_path,self.train)
'''data augmention'''
if self.train==True:
if random.random() > 0.5:
target = np.fliplr(target)
img = img.transpose(Image.FLIP_LEFT_RIGHT)
kpoint = np.fliplr(kpoint)
sigma_map = np.fliplr(sigma_map)
if random.random() > 0.5:
proportion = random.uniform(0.004, 0.015)
width, height = img.size[0], img.size[1]
num = int(height * width * proportion)
for i in range(num):
w = random.randint(0, width - 1)
h = random.randint(0, height - 1)
if random.randint(0, 1) == 0:
img.putpixel((w, h), (0, 0, 0))
else:
img.putpixel((w, h), (255, 255, 255))
target = target.copy()
kpoint = kpoint.copy()
img = img.copy()
sigma_map = sigma_map.copy()
if self.transform is not None:
img = self.transform(img)
target = torch.from_numpy(target).cuda()
return img,target,kpoint,fname,sigma_map