-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmakeDatasetFlow.py
More file actions
125 lines (115 loc) · 4.81 KB
/
Copy pathmakeDatasetFlow.py
File metadata and controls
125 lines (115 loc) · 4.81 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
import os
import torch
from torch.utils.data import Dataset
from PIL import Image
import numpy as np
import random
import glob
import sys
def gen_split(root_dir, stackSize, train):
DatasetX = []
DatasetY = []
Labels = []
NumFrames = []
root_dir = os.path.join(root_dir, 'flow_x_processed')
for dir_user in sorted(os.listdir(root_dir)):
if train is False: #changed because we had train S1 S3 S4 and test/val S2
if dir_user != 'S2':
continue
elif dir_user == 'S2':
continue
class_id = 0
dir = os.path.join(root_dir, dir_user)
for target in sorted(os.listdir(dir)):
dir1 = os.path.join(dir, target)
insts = sorted(os.listdir(dir1))
if insts != []:
for inst in insts:
inst_dir = os.path.join(dir1, inst)
numFrames = len(glob.glob1(inst_dir, '*.png')) #changed from .jpg
if numFrames >= stackSize:
DatasetX.append(inst_dir)
DatasetY.append(inst_dir.replace('flow_x_processed', 'flow_y_processed'))
Labels.append(class_id)
NumFrames.append(numFrames)
class_id += 1
return DatasetX, DatasetY, Labels, NumFrames
class makeDataset(Dataset):
def __init__(self, root_dir, spatial_transform=None, sequence=False, seqLen=5,
train=True, numSeg = 1, fmt='.png', flag_class=None, phase='train'):
"""
Args:
root_dir (string): Directory with all the images.
transform (callable, optional): Optional transform to be applied
on a sample.
"""
self.imagesX, self.imagesY, self.labels, self.numFrames = gen_split(root_dir, seqLen,train)
self.st_1, self.st_rgb, _ = spatial_transform
self.train = train
self.numSeg = numSeg
self.sequence = sequence
self.stackSize = seqLen
self.fmt = fmt
self.phase = phase
print('================ make dataset flow ==============')
def __len__(self):
return len(self.imagesX)
def __getitem__(self, idx):
vid_nameX = self.imagesX[idx]
vid_nameY = self.imagesY[idx]
label = self.labels[idx]
numFrame = self.numFrames[idx]
inpSeqSegs = []
temp=[]
self.st_1.randomize_parameters()
if self.sequence is True:
if numFrame <= self.stackSize:
frameStart = np.ones(self.numSeg)
else:
frameStart = np.linspace(1, numFrame - self.stackSize + 1, self.numSeg, endpoint=False)
for startFrame in frameStart:
inpSeq = []
for k in range(self.stackSize):
i = k + int(startFrame)
#flowX
fl_name = vid_nameX + '/flow_x_' + str(int(round(i))).zfill(5) + '.png'
img = Image.open(fl_name)
img = self.st_1(img.convert('L'), inv=True, flow=True)
img = self.st_rgb(img)
inpSeq.append(img)
# fl_names.append(fl_name)
#flowY
fl_name = vid_nameY + '/flow_y_' + str(int(round(i))).zfill(5) + '.png'
img = Image.open(fl_name)
img = self.st_1(img.convert('L'), inv=True, flow=True)
img = self.st_rgb(img)
inpSeq.append(img)
temp.append(k)
inpSeqSegs.append(torch.stack(inpSeq, 0).squeeze())
inpSeqSegs = torch.stack(inpSeqSegs, 0)
return inpSeqSegs, label
else:
if numFrame <= self.stackSize:
startFrame = 1
else:
if self.phase == 'train':
startFrame = random.randint(1, numFrame - self.stackSize)
else:
startFrame = np.ceil((numFrame - self.stackSize)/2)
inpSeq = []
for k in range(self.stackSize):
i = k + int(startFrame)
fl_name = vid_nameX + '/flow_x_' + str(int(round(i))).zfill(5) + '.png'
img = Image.open(fl_name)
img = self.st_1(img.convert('L'), inv=True, flow=True)
img = self.st_rgb(img)
inpSeq.append(img)
# fl_names.append(fl_name)
fl_name = vid_nameY + '/flow_y_' + str(int(round(i))).zfill(5) + '.png'
img = Image.open(fl_name)
img = self.st_1(img.convert('L'), inv=True, flow=True)
img = self.st_rgb(img)
inpSeq.append(img)
temp.append(k)
inpSeqSegs = torch.stack(inpSeq, 0).squeeze(1)
return inpSeqSegs,temp,label#, fl_name