-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdataloader.py
More file actions
executable file
·168 lines (153 loc) · 5.59 KB
/
dataloader.py
File metadata and controls
executable file
·168 lines (153 loc) · 5.59 KB
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
158
159
160
161
162
163
164
165
166
167
168
from pathlib import Path
import pickle
from typing import Union
import cv2
from PIL import Image
import numpy as np
import math
import matplotlib.pyplot as plt
import torch
from torchvision import transforms
from torch.utils.data import Dataset
from pyntcloud import PyntCloud
def cartesian_to_polar(x: float, y: float) -> torch.Tensor:
"""Convert cartesian coordinates to polar coordinates
x: (float) x direction
y: (float) y direction
"""
# Calculating radius
radius = math.sqrt(x * x + y * y)
# Calculating angle (theta) in radian
theta = math.atan2(y, x)
return torch.Tensor([radius, theta]).float()
def imread(address: str):
img = cv2.imread(address, cv2.IMREAD_UNCHANGED)
if len(img.shape) == 3:
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
return Image.fromarray(img)
class SocialNavDataset(Dataset):
def __init__(
self,
root: str,
train: bool = True,
seed: int = 42,
boost: int = 1,
resize: Union[list, tuple] = (224, 224),
):
"""Dataloader for social navigation task
root (str): samples.pkl address
"""
# setting seed value
torch.random.manual_seed(seed)
self.resize = resize
self.train = train
# read and store directories
with Path(root).open("rb") as f:
self.data = pickle.load(f)
self.batch_read_number = 0
def __len__(self):
return len(self.data["past_positions"])
def __getitem__(self, idx):
"""Return a sample"""
if self.train:
transform = transforms.Compose(
[
# Crop(self.crop),
transforms.Resize(self.resize, antialias=True),
transforms.ToTensor(),
transforms.Normalize(
mean=[0.485, 0.456, 0.406], # ImageNet mean hardcoded
std=[0.229, 0.224, 0.225],
), # ImageNet std hardcoded
]
)
else:
transform = transforms.Compose(
[
# Crop(self.crop),
transforms.Resize(self.resize, antialias=True),
transforms.ToTensor(),
transforms.Normalize(
mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]
),
]
)
# Read image data and add to the list
past_frames = []
for img_address in self.data["past_frames"][idx]:
# read all images and append them to a list
img = imread(img_address.as_posix())
# print(f"{img.shape = }")
img = transform(img)
# img = img.refine_names(..., 'channels', 'height', 'width')
past_frames.append(img)
future_frames = []
for img_address in self.data["future_frames"][idx]:
# read all images and append them to a list
img = imread(img_address)
img = transform(img)
# img = img.refine_names(..., 'channels', 'height', 'width')
future_frames.append(img)
sample = {
"past_positions": torch.Tensor(
np.array(self.data["past_positions"][idx])
).float(),
"future_positions": torch.Tensor(
np.array(self.data["future_positions"][idx])
).float(),
"past_yaw": torch.Tensor(np.array(self.data["past_yaw"][idx]))
.float()
.view(-1),
"future_yaw": torch.Tensor(np.array(self.data["future_yaw"][idx]))
.float()
.view(-1),
"past_vw": torch.Tensor(np.array(self.data["past_vw"][idx]))
.float()
.view(-1),
"future_vw": torch.Tensor(np.array(self.data["future_vw"][idx]))
.float(),
"past_frames": past_frames,
"future_frames": future_frames,
}
current = sample["past_positions"][-1] # current position
rot = torch.Tensor(
[
[np.cos(sample["past_yaw"][-1]), -np.sin(sample["past_yaw"][-1])],
[np.sin(sample["past_yaw"][-1]), np.cos(sample["past_yaw"][-1])],
]
).float()
# sample["past_positions"] = torch.mm(
# (sample["past_positions"] - current.unsqueeze(0)), torch.linalg.inv(rot)
# ) # these will be behind the ego
sample["future_positions"] = torch.mm(
(sample["future_positions"] - current.unsqueeze(0)), rot
)
# how many steps to each the goal?
dt = np.random.randint(
low=len(sample["future_positions"]) // 2,
high=len(sample["future_positions"]),
)
goal = sample["future_positions"][dt]
goal = cartesian_to_polar(goal[0], goal[1])
sample["goal_direction"] = goal
sample["dt"] = torch.Tensor(
[
dt / len(sample["future_positions"]),
]
)
return sample
if __name__ == "__main__":
# data = SocialNavDataset("social_nav/data/processed/samples.pkl")
# sample = data[9]
# print(f"{sample['past_positions'] = }")
# print(f"{sample['future_positions'] = }")
# print(f"{sample['past_yaw'] = }")
# print(f"{sample['future_yaw'] = }")
# print(f"{sample['past_vw'] = }")
# print(f"{sample['future_vw'] = }")
# print(f"{len(sample['past_frames']) = }")
# print(f"{len(sample['future_frames']) = }")
# # plt.imshow(img.squeeze(), cmap='gray')
# plt.imshow(sample["past_frames"][0].permute(1, 2, 0))
# plt.show()
pass