-
Notifications
You must be signed in to change notification settings - Fork 2
/
dataset.py
72 lines (58 loc) · 2.31 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
import torch
from torch.utils.data import Dataset
import os
import random
from image import *
import numpy as np
import numbers
from torchvision import datasets, transforms
class listDataset(Dataset):
def __init__(self, root, shape=None, shuffle=True, transform=None, train=False, seen=0, batch_size=1,
num_workers=4, args=None):
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
self.args = args
def __len__(self):
return self.nSamples
def __getitem__(self, index):
assert index <= len(self), 'index range error'
if self.args['preload_data'] == True:
fname = self.lines[index]['fname']
img = self.lines[index]['img']
kpoint = self.lines[index]['kpoint']
fidt_map = self.lines[index]['fidt_map']
else:
img_path = self.lines[index]
fname = os.path.basename(img_path)
img, fidt_map, kpoint = load_data_fidt(img_path, self.args, self.train)
'''data augmention'''
if self.train == True:
if random.random() > 0.5:
fidt_map = np.fliplr(fidt_map)
img = img.transpose(Image.FLIP_LEFT_RIGHT)
kpoint = np.fliplr(kpoint)
fidt_map = fidt_map.copy()
kpoint = kpoint.copy()
img = img.copy()
if self.transform is not None:
img = self.transform(img)
'''crop size'''
if self.train == True:
fidt_map = torch.from_numpy(fidt_map).cuda()
width = self.args['crop_size']
height = self.args['crop_size']
# print(img.shape)
crop_size_x = random.randint(0, img.shape[1] - width)
crop_size_y = random.randint(0, img.shape[2] - height)
img = img[:, crop_size_x: crop_size_x + width, crop_size_y:crop_size_y + height]
kpoint = kpoint[crop_size_x: crop_size_x + width, crop_size_y:crop_size_y + height]
fidt_map = fidt_map[crop_size_x: crop_size_x + width, crop_size_y:crop_size_y + height]
return fname, img, fidt_map, kpoint