-
Notifications
You must be signed in to change notification settings - Fork 1
/
create_dataset.py
157 lines (132 loc) · 6.56 KB
/
create_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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import sys, os
import numpy as np
import torch.nn as nn
import cv2
import torch
from torch.utils.data.sampler import SequentialSampler
from torch.utils.data import Dataset
from torchvision import transforms
import pandas as pd
from PIL import Image
sys.path.append('../')
# from FlowNetPytorch.models import FlowNetS
from variables import RootVariables
from helpers import standarization
class All_Dataset:
def __init__(self):
self.var = RootVariables()
def get_dataset(self, original_img_csv, feat, labels, index):
if index == 0:
return self.SIG_FINAL_DATASET(feat, labels)
elif index == 1:
return self.VIS_FINAL_DATASET(original_img_csv, labels)
else:
return self.FUSION_DATASET(original_img_csv, feat, labels)
class FUSION_DATASET(Dataset):
def __init__(self, original_img_csv, imu_feat, labels):
self.imu_data, self.gaze_data = [], []
self.indexes = []
self.var = RootVariables()
self.ori_imgs_path = pd.read_csv(self.var.root + original_img_csv + '.csv')
name_index = 5 if len(self.ori_imgs_path.iloc[0, 1].split('/')) > 5 else 4
subfolder = self.ori_imgs_path.iloc[0, 1].split('/')[name_index]
f_index = 7
checkedLast = False
for index in range(len(labels)):
check = np.isnan(labels[index])
imu_check = np.isnan(imu_feat[index])
if check.any() or imu_check.any():
f_index += 1
else:
f_index += 1
self.gaze_data.append(labels[index])
if self.ori_imgs_path.iloc[f_index, 1].split('/')[name_index] == subfolder:
self.indexes.append(f_index)
else:
f_index += 8#1
self.indexes.append(f_index)
subfolder = self.ori_imgs_path.iloc[f_index, 1].split('/')[name_index]
self.imu_data.append(imu_feat[index])
self.imu_data = standarization(self.imu_data)
assert len(self.imu_data) == len(self.indexes)
assert len(self.gaze_data) == len(self.indexes)
self.transforms = transforms.Compose([transforms.ToTensor()])
def __len__(self):
return len(self.indexes) # len(self.labels)
def __getitem__(self, index):
f_index = self.indexes[index]
##Imgs
for i in range(f_index-8, f_index +1, 1):
# print(self.ori_imgs_path.iloc[i, 1], f_index)
img = torch.cat((img, self.transforms(Image.open(self.ori_imgs_path.iloc[i, 1])).unsqueeze(dim=1)), 1) if i > f_index-8 else self.transforms(Image.open(self.ori_imgs_path.iloc[i, 1])).unsqueeze(dim=1)
targets = self.gaze_data[index]
targets[:,0] *= 512.0
targets[:,1] *= 288.0
return (img).to("cuda:0"), torch.from_numpy(self.imu_data[index]).to("cuda:0"), torch.from_numpy(targets).to("cuda:0")
class VIS_FINAL_DATASET(Dataset):
def __init__(self, original_img_csv, labels):
self.gaze_data = []
self.indexes = []
self.var = RootVariables()
self.ori_imgs_path = pd.read_csv(self.var.root + original_img_csv + '.csv')
name_index = 5 if len(self.ori_imgs_path.iloc[0, 1].split('/')) > 5 else 4
subfolder = self.ori_imgs_path.iloc[0, 1].split('/')[name_index]
f_index = 7
checkedLast = False
for index in range(len(labels)):
check = np.isnan(labels[index])
if check.any():
f_index += 1
# continue
else:
f_index += 1
self.gaze_data.append(labels[index])
if self.ori_imgs_path.iloc[f_index, 1].split('/')[name_index] == subfolder:
self.indexes.append(f_index)
else:
f_index += 8#1
self.indexes.append(f_index)
subfolder = self.ori_imgs_path.iloc[f_index, 1].split('/')[name_index]
self.transforms = transforms.ToTensor()
assert len(self.gaze_data) == len(self.indexes)
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
def __len__(self):
return len(self.indexes) # len(self.labels)
def __getitem__(self, index):
f_index = self.indexes[index]
##Imgs
for i in range(f_index-8, f_index +1, 1):
# print(self.ori_imgs_path.iloc[i, 1], f_index)
img = torch.cat((img, self.transforms(Image.open(self.ori_imgs_path.iloc[i, 1])).unsqueeze(dim=1)), 1) if i > f_index-8 else self.transforms(Image.open(self.ori_imgs_path.iloc[i, 1])).unsqueeze(dim=1)
# for i in range(f_index, f_index-5, -1):
# print(self.ori_imgs_path.iloc[i, 1], f_index)
# img = torch.cat((img, self.transforms(Image.open(self.ori_imgs_path.iloc[i, 1])).unsqueeze(dim=1)), 1) if i < f_index else self.transforms(Image.open(self.ori_imgs_path.iloc[i, 1])).unsqueeze(dim=1)
# print(img.shape)
targets = self.gaze_data[index]
#targets[:,0] *= 0.2667
#targets[:,1] *= 0.3556
targets[:,0] *= 512.0
targets[:,1] *= 288.0
return (img).to("cuda:0"), torch.from_numpy(targets).to("cuda:0")
class SIG_FINAL_DATASET(Dataset):
def __init__(self, feat, labels):
self.gaze_data, self.imu_data = [], []
checkedLast = False
for index in range(len(labels)):
check = np.isnan(labels[index])
imu_check = np.isnan(feat[index])
if check.any() or imu_check.any():
continue
else:
self.gaze_data.append(labels[index])
self.imu_data.append(feat[index])
self.imu_data = standarization(self.imu_data)
assert len(self.imu_data) == len(self.gaze_data)
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
def __len__(self):
return len(self.gaze_data) # len(self.labels)
def __getitem__(self, index):
targets = self.gaze_data[index]
targets[:,0] *= 512.0
targets[:,1] *= 288 #384 #288.0 # 384
return torch.from_numpy(self.imu_data[index]).to(self.device), torch.from_numpy(targets).to(self.device)