xref: /aosp_15_r20/external/libopus/dnn/training_tf2/plc_loader.py (revision a58d3d2adb790c104798cd88c8a3aff4fa8b82cc)
1#!/usr/bin/python3
2'''Copyright (c) 2021-2022 Amazon
3
4   Redistribution and use in source and binary forms, with or without
5   modification, are permitted provided that the following conditions
6   are met:
7
8   - Redistributions of source code must retain the above copyright
9   notice, this list of conditions and the following disclaimer.
10
11   - Redistributions in binary form must reproduce the above copyright
12   notice, this list of conditions and the following disclaimer in the
13   documentation and/or other materials provided with the distribution.
14
15   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18   A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
19   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26'''
27
28import numpy as np
29from tensorflow.keras.utils import Sequence
30
31class PLCLoader(Sequence):
32    def __init__(self, features, lost, nb_burg_features, batch_size):
33        self.batch_size = batch_size
34        self.nb_batches = features.shape[0]//self.batch_size
35        self.features = features[:self.nb_batches*self.batch_size, :, :]
36        self.lost = lost.astype('float')
37        self.lost = self.lost[:(len(self.lost)//features.shape[1]-1)*features.shape[1]]
38        self.nb_burg_features = nb_burg_features
39        self.on_epoch_end()
40
41    def on_epoch_end(self):
42        self.indices = np.arange(self.nb_batches*self.batch_size)
43        np.random.shuffle(self.indices)
44        offset = np.random.randint(0, high=self.features.shape[1])
45        self.lost_offset = np.reshape(self.lost[offset:-self.features.shape[1]+offset], (-1, self.features.shape[1]))
46        self.lost_indices = np.random.randint(0, high=self.lost_offset.shape[0], size=self.nb_batches*self.batch_size)
47
48    def __getitem__(self, index):
49        features = self.features[self.indices[index*self.batch_size:(index+1)*self.batch_size], :, :]
50        burg_lost = (np.random.rand(features.shape[0], features.shape[1]) > .1).astype('float')
51        burg_lost = np.reshape(burg_lost, (features.shape[0], features.shape[1], 1))
52        burg_mask = np.tile(burg_lost, (1,1,self.nb_burg_features))
53
54        lost = self.lost_offset[self.lost_indices[index*self.batch_size:(index+1)*self.batch_size], :]
55        lost = np.reshape(lost, (features.shape[0], features.shape[1], 1))
56        lost_mask = np.tile(lost, (1,1,features.shape[2]))
57        in_features = features*lost_mask
58        in_features[:,:,:self.nb_burg_features] = in_features[:,:,:self.nb_burg_features]*burg_mask
59
60        #For the first frame after a loss, we don't have valid features, but the Burg estimate is valid.
61        #in_features[:,1:,self.nb_burg_features:] = in_features[:,1:,self.nb_burg_features:]*lost_mask[:,:-1,self.nb_burg_features:]
62        out_lost = np.copy(lost)
63        #out_lost[:,1:,:] = out_lost[:,1:,:]*out_lost[:,:-1,:]
64
65        out_features = np.concatenate([features[:,:,self.nb_burg_features:], 1.-out_lost], axis=-1)
66        burg_sign = 2*burg_lost - 1
67        # last dim is 1 for received packet, 0 for lost packet, and -1 when just the Burg info is missing
68        inputs = [in_features*lost_mask, lost*burg_sign]
69        outputs = [out_features]
70        return (inputs, outputs)
71
72    def __len__(self):
73        return self.nb_batches
74