forked from aharley/pips
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crohddataset.py
128 lines (106 loc) · 4.78 KB
/
crohddataset.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
import torch
import numpy as np
import os
import scipy.ndimage
import torchvision.transforms as transforms
import torch.nn.functional as F
from PIL import Image
import random
import glob
import json
import imageio
import cv2
class CrohdDataset(torch.utils.data.Dataset):
def __init__(self, seqlen=8, dset='t', dataset_root='../head_tracking'):
dataset_location = '%s/HT21' % dataset_root
label_location = '%s/HT21Labels' % dataset_root
subfolders = []
if dset == 't':
dataset_location = os.path.join(dataset_location, "train")
label_location = os.path.join(label_location, "train")
subfolders = ['HT21-01', 'HT21-02', 'HT21-03', 'HT21-04']
elif dset == 'v':
dataset_location = os.path.join(dataset_location, "val")
label_location = os.path.join(label_location, "val")
subfolders = ['HT21-11', 'HT21-12', 'HT21-13', 'HT21-14', 'HT21-15']
else:
raise Exception("unexpceted dset. Choose between t and v.")
print('dataset_location', dataset_location)
print('label_location', label_location)
# read gt for subfolders
self.dataset_location = dataset_location
self.label_location = label_location
self.seqlen = seqlen
self.subfolders = subfolders
self.folder_to_gt = {} # key: folder name, value: dict with fields boxlist, scorelist, vislist
self.subfolder_lens = []
for fid, subfolder in enumerate(subfolders):
print("loading labels for folder {0}/{1}".format(fid+1, len(subfolders)))
label_path = os.path.join(label_location, subfolder, 'gt/gt.txt')
labels = np.loadtxt(label_path, delimiter=',')
n_frames = int(labels[-1,0])
self.subfolder_lens.append(n_frames // seqlen)
n_heads = int(labels[:,1].max())
# unlike our data, those lists are already aligned
# indexing the second dimension gives the information of the head throughout the seq
boxlist = np.zeros((n_frames, n_heads, 4))
scorelist = -1 * np.ones((n_frames, n_heads))
vislist = np.zeros((n_frames, n_heads))
for i in range(labels.shape[0]):
frame_id, head_id, bb_left, bb_top, bb_width, bb_height, conf, cid, vis = labels[i]
frame_id = int(frame_id) - 1 # convert 1 indexed to 0 indexed
head_id = int(head_id) - 1 # convert 1 indexed to 0 indexed
scorelist[frame_id, head_id] = 1
vislist[frame_id, head_id] = vis
box_cur = np.array([bb_left, bb_top, bb_left+bb_width, bb_top+bb_height]) # convert xywh to x1, y1, x2, y2
boxlist[frame_id, head_id] = box_cur
self.folder_to_gt[subfolder] = {
'boxlist': np.copy(boxlist),
'scorelist': np.copy(scorelist),
'vislist': np.copy(vislist)
}
def __getitem__(self, index):
# identify which sample and which starting frame it is
subfolder_id = 0
while index >= self.subfolder_lens[subfolder_id]:
index -= self.subfolder_lens[subfolder_id]
subfolder_id += 1
# start from subfolder and the frame
subfolder = self.subfolders[subfolder_id]
start_frame = index * self.seqlen
# get gt
S = self.seqlen
boxlist = self.folder_to_gt[subfolder]['boxlist'][start_frame:start_frame+S] # S, n_head, 4
scorelist = self.folder_to_gt[subfolder]['scorelist'][start_frame:start_frame+S] # S, n_head
vislist = self.folder_to_gt[subfolder]['vislist'][start_frame:start_frame+S] # S, n_head
rgbs = []
for i in range(S):
# read image
image_name = os.path.join(self.dataset_location, subfolder, 'img1', str(start_frame+i+1).zfill(6)+'.jpg')
rgb = np.array(Image.open(image_name))
rgbs.append(rgb)
rgbs = np.stack(rgbs, axis=0)
xylist = np.stack([boxlist[:, :, [0,2]].mean(2), boxlist[:, :, [1,3]].mean(2)], axis=2) # center of the box
sample = {
'rgbs': rgbs, # (S, H, W, 3) in 0-255
'boxlist': boxlist, # (S, N, 4), N = n heads
'xylist': xylist, # (S, N, 2)
'scorelist': scorelist, # (S, N)
'vislist': vislist # (S, N)
}
return sample
def __len__(self):
return sum(self.subfolder_lens)
if __name__ == "__main__":
B = 1
S = 8
shuffle=False
dataset = HeadTrackingDataset(seqlen=S)
from torch.utils.data import Dataset, DataLoader
train_dataloader = DataLoader(
dataset,
batch_size=B,
shuffle=shuffle,
num_workers=0)
train_iterloader = iter(train_dataloader)
sample = next(train_iterloader)