File size: 12,666 Bytes
f670afc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# Copyright (C) 2021 NVIDIA CORPORATION & AFFILIATES.  All rights reserved.
#
# This work is made available under the Nvidia Source Code License-NC.
# To view a copy of this license, check out LICENSE.md
import copy
import random
import torch

from imaginaire.datasets.paired_videos import Dataset as VideoDataset
from imaginaire.model_utils.fs_vid2vid import select_object
from imaginaire.utils.distributed import master_only_print as print


class Dataset(VideoDataset):
    r"""Paired video dataset for use in few-shot vid2vid.

    Args:
        cfg (Config): Loaded config object.
        is_inference (bool): In train or inference mode?
        sequence_length (int): What sequence of images to provide?
        few_shot_K (int): How many images to provide for few-shot?
    """

    def __init__(self, cfg, is_inference=False, sequence_length=None,
                 few_shot_K=None, is_test=False):
        self.paired = True
        # Get initial few shot K.
        if few_shot_K is None:
            self.few_shot_K = cfg.data.initial_few_shot_K
        else:
            self.few_shot_K = few_shot_K
        # Initialize.
        super(Dataset, self).__init__(
            cfg, is_inference, sequence_length=sequence_length, is_test=is_test)

    def set_inference_sequence_idx(self, index, k_shot_index,
                                   k_shot_frame_index):
        r"""Get frames from this sequence during inference.

        Args:
            index (int): Index of inference sequence.
            k_shot_index (int): Index of sequence from which k_shot is sampled.
            k_shot_frame_index (int): Index of frame to sample.
        """
        assert self.is_inference
        assert index < len(self.mapping)
        assert k_shot_index < len(self.mapping)
        assert k_shot_frame_index < len(self.mapping[k_shot_index])

        self.inference_sequence_idx = index
        self.inference_k_shot_sequence_index = k_shot_index
        self.inference_k_shot_frame_index = k_shot_frame_index
        self.epoch_length = len(
            self.mapping[self.inference_sequence_idx]['filenames'])

    def set_sequence_length(self, sequence_length, few_shot_K=None):
        r"""Set the length of sequence you want as output from dataloader.

        Args:
            sequence_length (int): Length of output sequences.
            few_shot_K (int): Number of few-shot frames.
        """
        if few_shot_K is None:
            few_shot_K = self.few_shot_K
        assert isinstance(sequence_length, int)
        assert isinstance(few_shot_K, int)
        if (sequence_length + few_shot_K) > self.sequence_length_max:
            error_message = \
                'Requested sequence length (%d) ' % (sequence_length) + \
                '+ few shot K (%d) > ' % (few_shot_K) + \
                'max sequence length (%d). ' % (self.sequence_length_max)
            print(error_message)
            sequence_length = self.sequence_length_max - few_shot_K
            print('Reduced sequence length to %s' % (sequence_length))
        self.sequence_length = sequence_length
        self.few_shot_K = few_shot_K
        # Recalculate mapping as some sequences might no longer be useful.
        self.mapping, self.epoch_length = self._create_mapping()
        print('Epoch length:', self.epoch_length)

    def _create_mapping(self):
        r"""Creates mapping from idx to key in LMDB.

        Returns:
            (tuple):
              - self.mapping (dict): Dict of seq_len to list of sequences.
              - self.epoch_length (int): Number of samples in an epoch.
        """
        # Create dict mapping length to sequence.
        length_to_key, num_selected_seq = {}, 0
        has_additional_lists = len(self.additional_lists) > 0
        for lmdb_idx, sequence_list in enumerate(self.sequence_lists):
            for sequence_name, filenames in sequence_list.items():
                if len(filenames) >= (self.sequence_length + self.few_shot_K):
                    if len(filenames) not in length_to_key:
                        length_to_key[len(filenames)] = []
                    if has_additional_lists:
                        obj_indices = self.additional_lists[lmdb_idx][
                            sequence_name]
                    else:
                        obj_indices = [0 for _ in range(len(filenames))]
                    length_to_key[len(filenames)].append({
                        'lmdb_root': self.lmdb_roots[lmdb_idx],
                        'lmdb_idx': lmdb_idx,
                        'sequence_name': sequence_name,
                        'filenames': filenames,
                        'obj_indices': obj_indices,
                    })
                    num_selected_seq += 1
        self.mapping = length_to_key
        self.epoch_length = num_selected_seq

        # At inference time, we want to use all sequences,
        # irrespective of length.
        if self.is_inference:
            sequence_list = []
            for key, sequences in self.mapping.items():
                sequence_list.extend(sequences)
            self.mapping = sequence_list

        return self.mapping, self.epoch_length

    def _sample_keys(self, index):
        r"""Gets files to load for this sample.

        Args:
            index (int): Index in [0, len(dataset)].
        Returns:
            key (dict):
                - lmdb_idx (int): Chosen LMDB dataset root.
                - sequence_name (str): Chosen sequence in chosen dataset.
                - filenames (list of str): Chosen filenames in chosen sequence.
        """
        if self.is_inference:
            assert index < self.epoch_length
            chosen_sequence = self.mapping[self.inference_sequence_idx]
            chosen_filenames = [chosen_sequence['filenames'][index]]
            chosen_obj_indices = [chosen_sequence['obj_indices'][index]]
            k_shot_chosen_sequence = self.mapping[
                self.inference_k_shot_sequence_index]
            k_shot_chosen_filenames = [k_shot_chosen_sequence['filenames'][
                                       self.inference_k_shot_frame_index]]
            k_shot_chosen_obj_indices = [k_shot_chosen_sequence['obj_indices'][
                                         self.inference_k_shot_frame_index]]
            # Prepare few shot key.
            few_shot_key = copy.deepcopy(k_shot_chosen_sequence)
            few_shot_key['filenames'] = k_shot_chosen_filenames
            few_shot_key['obj_indices'] = k_shot_chosen_obj_indices
        else:
            # Pick a time step for temporal augmentation.
            time_step = random.randint(1, self.augmentor.max_time_step)
            required_sequence_length = 1 + \
                (self.sequence_length - 1) * time_step

            # If step is too large, default to step size of 1.
            if required_sequence_length + self.few_shot_K > \
                    self.sequence_length_max:
                required_sequence_length = self.sequence_length
                time_step = 1

            # Find valid sequences.
            valid_sequences = []
            for sequence_length, sequences in self.mapping.items():
                if sequence_length >= required_sequence_length + \
                        self.few_shot_K:
                    valid_sequences.extend(sequences)

            # Pick a sequence.
            chosen_sequence = random.choice(valid_sequences)

            # Choose filenames.
            max_start_idx = len(chosen_sequence['filenames']) - \
                required_sequence_length
            start_idx = random.randint(0, max_start_idx)
            end_idx = start_idx + required_sequence_length
            chosen_filenames = chosen_sequence['filenames'][
                start_idx:end_idx:time_step]
            chosen_obj_indices = chosen_sequence['obj_indices'][
                start_idx:end_idx:time_step]

            # Find the K few shot filenames.
            valid_range = list(range(start_idx)) + \
                list(range(end_idx, len(chosen_sequence['filenames'])))
            k_shot_chosen = sorted(random.sample(valid_range, self.few_shot_K))
            k_shot_chosen_filenames = [chosen_sequence['filenames'][idx]
                                       for idx in k_shot_chosen]
            k_shot_chosen_obj_indices = [chosen_sequence['obj_indices'][idx]
                                         for idx in k_shot_chosen]
            assert not (set(chosen_filenames) & set(k_shot_chosen_filenames))

            assert len(chosen_filenames) == self.sequence_length
            assert len(k_shot_chosen_filenames) == self.few_shot_K

            # Prepare few shot key.
            few_shot_key = copy.deepcopy(chosen_sequence)
            few_shot_key['filenames'] = k_shot_chosen_filenames
            few_shot_key['obj_indices'] = k_shot_chosen_obj_indices

        # Prepre output key.
        key = copy.deepcopy(chosen_sequence)
        key['filenames'] = chosen_filenames
        key['obj_indices'] = chosen_obj_indices
        return key, few_shot_key

    def _prepare_data(self, keys):
        r"""Load data and perform augmentation.

        Args:
            keys (dict): Key into LMDB/folder dataset for this item.
        Returns:
            data (dict): Dict with all chosen data_types.
        """
        # Unpack keys.
        lmdb_idx = keys['lmdb_idx']
        sequence_name = keys['sequence_name']
        filenames = keys['filenames']
        obj_indices = keys['obj_indices']

        # Get key and lmdbs.
        keys, lmdbs = {}, {}
        for data_type in self.dataset_data_types:
            keys[data_type] = self._create_sequence_keys(
                sequence_name, filenames)
            lmdbs[data_type] = self.lmdbs[data_type][lmdb_idx]

        # Load all data for this index.
        data = self.load_from_dataset(keys, lmdbs)

        # Apply ops pre augmentation.
        data = self.apply_ops(data, self.pre_aug_ops)

        # Select the object in data using the object indices.
        data = select_object(data, obj_indices)

        # Do augmentations for images.
        data, is_flipped = self.perform_augmentation(data, paired=True, augment_ops=self.augmentor.augment_ops)

        # Create copy of keypoint data types before post aug.
        # kp_data = {}
        # for data_type in self.keypoint_data_types:
        #     new_key = data_type + '_xy'
        #     kp_data[new_key] = copy.deepcopy(data[data_type])

        # Create copy of keypoint data types before post aug.
        kp_data = {}
        for data_type in self.keypoint_data_types:
            new_key = data_type + '_xy'
            kp_data[new_key] = copy.deepcopy(data[data_type])

        # Apply ops post augmentation.
        data = self.apply_ops(data, self.post_aug_ops)

        data = self.apply_ops(data, self.full_data_post_aug_ops, full_data=True)

        # Convert images to tensor.
        data = self.to_tensor(data)

        # Pack the sequence of images.
        for data_type in self.image_data_types:
            for idx in range(len(data[data_type])):
                data[data_type][idx] = data[data_type][idx].unsqueeze(0)
            data[data_type] = torch.cat(data[data_type], dim=0)

        # Add keypoint xy to data.
        data.update(kp_data)

        data['is_flipped'] = is_flipped
        data['key'] = keys

        return data

    def _getitem(self, index):
        r"""Gets selected files.

        Args:
            index (int): Index into dataset.
        Returns:
            data (dict): Dict with all chosen data_types.
        """
        # Select a sample from the available data.
        keys, few_shot_keys = self._sample_keys(index)

        data = self._prepare_data(keys)
        few_shot_data = self._prepare_data(few_shot_keys)

        # Add few shot data into data.
        for key, value in few_shot_data.items():
            data['few_shot_' + key] = few_shot_data[key]

        # Apply full data ops.
        if self.is_inference:
            if index == 0:
                pass
            elif index < self.cfg.data.num_workers:
                data_0 = self._getitem(0)
                if 'common_attr' in data_0:
                    self.common_attr = data['common_attr'] = \
                        data_0['common_attr']
            else:
                if hasattr(self, 'common_attr'):
                    data['common_attr'] = self.common_attr

        data = self.apply_ops(data, self.full_data_ops, full_data=True)

        if self.is_inference and index == 0 and 'common_attr' in data:
            self.common_attr = data['common_attr']

        return data