This repository has been archived by the owner on Aug 31, 2021. It is now read-only.
/
data.py
158 lines (134 loc) · 5.84 KB
/
data.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
158
# Copyright (c) Facebook, Inc. and its affiliates.
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
#
import sys
import torch
from torchnet.dataset import ShuffleDataset, BatchDataset, TransformDataset
from cityscapesDatasetAndFeatures import CityscapesDatasetAndFeatures
from logging import getLogger
logger = getLogger()
#-------------------------------------------------------------------------------
class featurePredictionSampler(object):
"""Samples elements from the dataset and returns input and target features
Arguments:
data_source (Dataset): dataset to sample from
"""
def __init__(self, data_source, features):
self.data_source = data_source
self.features = features
self.current = 0
self.high = len(self.data_source)
def __iter__(self):
return self
def next(self):
if self.current >= self.high:
raise StopIteration
else:
sample = self.data_source[self.current]
inputs, targets = {}, {}
for feat in self.features:
inputs[feat] = sample[u'input_features_' + feat]
targets[feat] = sample[u'target_features_' + feat]
self.current += 1
return inputs, targets, sample['seqIDs']
def __len__(self):
return len(self.data_source)
def reset(self, reshuffle = False):
self.current = 0
if reshuffle:
logger.info('Reshuffling dataset.')
dataset_to_resample = self.data_source
while not hasattr(dataset_to_resample, 'resample'):
dataset_to_resample = dataset_to_resample.dataset
dataset_to_resample.resample()
#-------------------------------------------------------------------------------
FPNfeatures = [u'fpn_res5_2_sum', u'fpn_res4_5_sum', u'fpn_res3_3_sum', u'fpn_res2_2_sum']
def intersect(a, b):
return list(set(a) & set(b))
def create_cityscapes_datasource_train(opt):
required_fpn_features = intersect(opt['features'], FPNfeatures)
cityscapes = CityscapesDatasetAndFeatures(
split = 'train',
frame_ss = opt['frame_ss'],
nSeq = opt['n_input_frames'] + opt['n_target_frames'],
features = opt['features'],
savedir = opt['save'],
size = opt['nIt'] * opt['batchsize']
)
loaded_model = cityscapes.model
def form_input_and_target_features(sample):
for feat in required_fpn_features:
sz = sample[feat].size()
nI, nCPI = opt['n_input_frames'], opt['n_channels_per_input']
nT, nCPT = opt['n_target_frames'], opt['n_channels_per_target']
sample[u'input_features_' + feat] = sample[feat][0:nI, :, :, :]
sample[u'input_features_' + feat] = \
sample[u'input_features_' + feat].view((nI *nCPI, sz[2], sz[3]))
sample[u'target_features_' + feat] = sample[feat][nI:, :, :, :]
sample[u'target_features_' + feat] = \
sample[u'target_features_' + feat].view((nT *nCPT, sz[2], sz[3]))
return sample
shuffled_cityscapes = ShuffleDataset(dataset = cityscapes)
dataset = BatchDataset( # batches
dataset = TransformDataset( # forms input and target features
dataset = shuffled_cityscapes,
transforms = form_input_and_target_features
),
batchsize = opt['batchsize'],
)
return dataset, required_fpn_features, loaded_model
# Main loading functions
def load_cityscapes_train(opt):
dataset, required_fpn_features, loaded_model = create_cityscapes_datasource_train(opt)
dataset_loader = featurePredictionSampler(dataset, required_fpn_features)
return dataset_loader, loaded_model
def create_cityscapes_datasource_val(opt):
required_fpn_features = intersect(opt['features'], FPNfeatures)
split = 'test' if opt['test_set'] else 'val'
cityscapes = CityscapesDatasetAndFeatures(
split = split,
frame_ss = opt['frame_ss'],
nSeq = opt['n_input_frames'] + opt['n_target_frames'],
features = opt['features'],
savedir = opt['save'],
size = opt['nIt'] * opt['batchsize'],
loaded_model = opt['loaded_model']
)
if opt['loaded_model'] is None:
loaded_model = cityscapes.model
def form_input_and_target_features(sample):
for feat in required_fpn_features:
sz = sample[feat].size()
nI, nCPI = opt['n_input_frames'], opt['n_channels_per_input']
nT, nCPT = opt['n_target_frames'], opt['n_channels_per_target']
sample[u'input_features_' + feat] = sample[feat][0:nI, :, :, :]
sample[u'input_features_' + feat] = \
sample[u'input_features_' + feat].view((nI *nCPI, sz[2], sz[3]))
sample[u'target_features_' + feat] = sample[feat][nI:, :, :, :]
sample[u'target_features_' + feat] = \
sample[u'target_features_' + feat].view((nT *nCPT, sz[2], sz[3]))
return sample
dataset = BatchDataset( # batches
dataset = TransformDataset( # forms input and target features
dataset = cityscapes,
transforms = form_input_and_target_features
),
batchsize = opt['batchsize']
)
if opt['loaded_model'] is None:
return dataset, required_fpn_features, loaded_model
else:
return dataset, required_fpn_features
def load_cityscapes_val(opt):
if opt['loaded_model'] is None:
dataset, required_fpn_features, loaded_model = create_cityscapes_datasource_val(opt)
else:
dataset, required_fpn_features = create_cityscapes_datasource_val(opt)
dataset_loader = featurePredictionSampler(dataset, required_fpn_features)
if opt['loaded_model'] is None:
return dataset_loader, loaded_model
else:
return dataset_loader