1""" 2/* Copyright (c) 2022 Amazon 3 Written by Jan Buethe */ 4/* 5 Redistribution and use in source and binary forms, with or without 6 modification, are permitted provided that the following conditions 7 are met: 8 9 - Redistributions of source code must retain the above copyright 10 notice, this list of conditions and the following disclaimer. 11 12 - Redistributions in binary form must reproduce the above copyright 13 notice, this list of conditions and the following disclaimer in the 14 documentation and/or other materials provided with the distribution. 15 16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 20 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 21 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 22 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 23 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 24 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 25 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27*/ 28""" 29 30import torch 31import numpy as np 32 33class RDOVAEDataset(torch.utils.data.Dataset): 34 def __init__(self, 35 feature_file, 36 sequence_length, 37 num_used_features=20, 38 num_features=36, 39 lambda_min=0.0002, 40 lambda_max=0.0135, 41 quant_levels=16, 42 enc_stride=2): 43 44 self.sequence_length = sequence_length 45 self.lambda_min = lambda_min 46 self.lambda_max = lambda_max 47 self.enc_stride = enc_stride 48 self.quant_levels = quant_levels 49 self.denominator = (quant_levels - 1) / np.log(lambda_max / lambda_min) 50 51 if sequence_length % enc_stride: 52 raise ValueError(f"RDOVAEDataset.__init__: enc_stride {enc_stride} does not divide sequence length {sequence_length}") 53 54 self.features = np.reshape(np.fromfile(feature_file, dtype=np.float32), (-1, num_features)) 55 self.features = self.features[:, :num_used_features] 56 self.num_sequences = self.features.shape[0] // sequence_length 57 58 def __len__(self): 59 return self.num_sequences 60 61 def __getitem__(self, index): 62 features = self.features[index * self.sequence_length: (index + 1) * self.sequence_length, :] 63 q_ids = np.random.randint(0, self.quant_levels, (1)).astype(np.int64) 64 q_ids = np.repeat(q_ids, self.sequence_length // self.enc_stride, axis=0) 65 rate_lambda = self.lambda_min * np.exp(q_ids.astype(np.float32) / self.denominator).astype(np.float32) 66 67 return features, rate_lambda, q_ids 68