{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":91844,"databundleVersionId":11361821,"sourceType":"competition"},{"sourceId":11092019,"sourceType":"datasetVersion","datasetId":6914328}],"dockerImageVersionId":30918,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport torch\nimport torchaudio\nimport pandas as pd\nimport matplotlib.pyplot as plt","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-03-22T06:10:12.899566Z","iopub.execute_input":"2025-03-22T06:10:12.899863Z","iopub.status.idle":"2025-03-22T06:10:18.664098Z","shell.execute_reply.started":"2025-03-22T06:10:12.899834Z","shell.execute_reply":"2025-03-22T06:10:18.662861Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class Quantizer:\n    def __init__(self, num_bits):\n        self.max = 2**num_bits - 1\n        self.mid = 2**(num_bits - 1)\n        if num_bits <= 8:\n            self.dtype = torch.uint8\n        elif num_bits <= 16:\n            self.dtype = torch.uint16\n        elif num_bits <= 32:\n            self.dtype = torch.uint32\n\n    def quantize(self, tensor):\n        min_val = tensor.min()\n        max_val = tensor.max()\n        if min_val == max_val:  # Edge case: all values are the same\n            return torch.full_like(tensor, self.mid, dtype=torch.uint16), min_val, max_val\n        scale = self.max / (max_val - min_val)\n        quantized_tensor = torch.round((tensor - min_val) * scale).clamp(0, self.max).to(self.dtype)\n        return quantized_tensor, min_val, max_val\n\n    def dequantize(self, quantized_tensor, min_val, max_val):\n        if min_val == max_val:\n            return torch.full_like(quantized_tensor, min_val, dtype=torch.float32)\n        scale = (max_val - min_val) / self.max\n        return quantized_tensor.to(torch.float32) * scale + min_val\n\nq = Quantizer(num_bits=16)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T06:10:18.665302Z","iopub.execute_input":"2025-03-22T06:10:18.665980Z","iopub.status.idle":"2025-03-22T06:10:18.675698Z","shell.execute_reply.started":"2025-03-22T06:10:18.665935Z","shell.execute_reply":"2025-03-22T06:10:18.674485Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/birdclef-2025/train.csv').merge(\n    pd.read_parquet('/kaggle/input/birdclef-2025-16-bit-melspecs/quantize_params.parquet'),\n    on=\"filename\"\n)\ndf.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T06:10:18.684367Z","iopub.execute_input":"2025-03-22T06:10:18.684769Z","iopub.status.idle":"2025-03-22T06:10:19.190139Z","shell.execute_reply.started":"2025-03-22T06:10:18.684730Z","shell.execute_reply":"2025-03-22T06:10:19.189049Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"to_spec = torch.nn.Sequential(\n    torchaudio.transforms.MelSpectrogram(\n        sample_rate=32000,\n        n_mels=128,\n        n_fft=1920,\n        hop_length=640,\n        center=False,\n        power=2,\n    ),\n    torchaudio.transforms.AmplitudeToDB(\n        stype=\"power\",\n        top_db=80.0,\n    )\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T06:10:36.175566Z","iopub.execute_input":"2025-03-22T06:10:36.175901Z","iopub.status.idle":"2025-03-22T06:10:36.270543Z","shell.execute_reply.started":"2025-03-22T06:10:36.175875Z","shell.execute_reply":"2025-03-22T06:10:36.269584Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"filename = df.filename[0]\noriginal_audio = torchaudio.load('/kaggle/input/birdclef-2025/train_audio/' + filename)[0][0]\noriginal_spec = to_spec(original_audio)\n\nplt.imshow(original_spec[:, :248])\nplt.colorbar();","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T06:10:36.324660Z","iopub.execute_input":"2025-03-22T06:10:36.324988Z","iopub.status.idle":"2025-03-22T06:10:37.199946Z","shell.execute_reply.started":"2025-03-22T06:10:36.324960Z","shell.execute_reply":"2025-03-22T06:10:37.198747Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"spec_filename = filename.split('.')[0] + '.npy'\nquantized_spec = np.load('/kaggle/input/birdclef-2025-16-bit-melspecs/train_audio_specs/' + spec_filename)\ndequantized_spec = q.dequantize(torch.tensor(quantized_spec), df.min_value[0], df.max_value[0])\n\nplt.imshow(dequantized_spec[:, :248])\nplt.colorbar();","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T06:10:37.201407Z","iopub.execute_input":"2025-03-22T06:10:37.201772Z","iopub.status.idle":"2025-03-22T06:10:37.590430Z","shell.execute_reply.started":"2025-03-22T06:10:37.201740Z","shell.execute_reply":"2025-03-22T06:10:37.589319Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pd.Series((original_spec - dequantized_spec).abs().flatten()).describe()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-22T06:10:45.607632Z","iopub.execute_input":"2025-03-22T06:10:45.608028Z","iopub.status.idle":"2025-03-22T06:10:45.654751Z","shell.execute_reply.started":"2025-03-22T06:10:45.607996Z","shell.execute_reply":"2025-03-22T06:10:45.653712Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}