{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":70203,"databundleVersionId":8068726,"sourceType":"competition"},{"sourceId":8472495,"sourceType":"datasetVersion","datasetId":4904171,"isSourceIdPinned":true},{"sourceId":8603619,"sourceType":"datasetVersion","datasetId":5093037,"isSourceIdPinned":true},{"sourceId":8618607,"sourceType":"datasetVersion","datasetId":4807916,"isSourceIdPinned":true},{"sourceId":8679105,"sourceType":"datasetVersion","datasetId":5202665}],"dockerImageVersionId":30684,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"#import\n\nimport random\nimport cv2\nimport json\nimport copy\nimport gc\nimport os\nimport pickle\nimport time\n\n\nfrom glob import glob\nfrom tqdm import tqdm\n\nimport pandas as pd\nimport numpy as np\nimport soundfile as sf\n\nimport torch\nimport torchaudio\nimport torch.nn as nn\nfrom torch.utils.data import DataLoader\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-06-13T07:32:22.859593Z","iopub.execute_input":"2024-06-13T07:32:22.859982Z","iopub.status.idle":"2024-06-13T07:32:28.626336Z","shell.execute_reply.started":"2024-06-13T07:32:22.859948Z","shell.execute_reply":"2024-06-13T07:32:28.625050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install /kaggle/input/openvino-package/openvino-2024.0.0-14509-cp310-cp310-manylinux2014_x86_64.whl --no-index --find-links /kaggle/input/openvino-package","metadata":{"execution":{"iopub.status.busy":"2024-06-13T07:32:31.020800Z","iopub.execute_input":"2024-06-13T07:32:31.021355Z","iopub.status.idle":"2024-06-13T07:32:48.518428Z","shell.execute_reply.started":"2024-06-13T07:32:31.021321Z","shell.execute_reply":"2024-06-13T07:32:48.517231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import openvino as ov\nimport openvino.properties.hint as hints\n\nclass VINOEngine:\n    def __init__(self, onnx_f):\n        core = ov.Core()\n        \n        model_onnx = core.read_model(onnx_f)        \n        \n        self.compiled_model = core.compile_model(model=model_onnx,device_name='AUTO')\n        \n        \n        self.output_layer = self.compiled_model.output(0)\n    def __call__(self, data):\n        \n        result_infer = self.compiled_model(data)[self.output_layer]\n        return result_infer\n\n","metadata":{"execution":{"iopub.status.busy":"2024-06-13T07:33:00.439433Z","iopub.execute_input":"2024-06-13T07:33:00.439852Z","iopub.status.idle":"2024-06-13T07:33:00.582795Z","shell.execute_reply.started":"2024-06-13T07:33:00.439818Z","shell.execute_reply":"2024-06-13T07:33:00.581685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CFG={\n    'batch_size':1,\n    'num_worker':4,\n    'slice_len':5,\n    'used_frame':48,\n    'sample_rate':32000,\n    'data':'/kaggle/input/birdclef-2024/test_soundscapes',\n    'weights_1d':'/kaggle/input/bird-1d',\n    'weights_spec':'/kaggle/input/bird-spec',\n    'weights_mix':'/kaggle/input/another-spec',\n    'nm2cls':{'asbfly': 0, 'ashdro1': 1, 'ashpri1': 2, 'ashwoo2': 3, 'asikoe2': 4, 'asiope1': 5,\n                     'aspfly1': 6, 'aspswi1': 7, 'barfly1': 8, 'barswa': 9, 'bcnher': 10, 'bkcbul1': 11,\n                     'bkrfla1': 12, 'bkskit1': 13, 'bkwsti': 14, 'bladro1': 15, 'blaeag1': 16, 'blakit1': 17,\n                     'blhori1': 18, 'blnmon1': 19, 'blrwar1': 20, 'bncwoo3': 21, 'brakit1': 22, 'brasta1': 23,\n                     'brcful1': 24, 'brfowl1': 25, 'brnhao1': 26, 'brnshr': 27, 'brodro1': 28, 'brwjac1': 29,\n                     'brwowl1': 30, 'btbeat1': 31, 'bwfshr1': 32, 'categr': 33, 'chbeat1': 34, 'cohcuc1': 35,\n                     'comfla1': 36, 'comgre': 37, 'comior1': 38, 'comkin1': 39, 'commoo3': 40, 'commyn': 41,\n                     'compea': 42, 'comros': 43, 'comsan': 44, 'comtai1': 45, 'copbar1': 46, 'crbsun2': 47,\n                     'cregos1': 48, 'crfbar1': 49, 'crseag1': 50, 'dafbab1': 51, 'darter2': 52, 'eaywag1': 53,\n                     'emedov2': 54, 'eucdov': 55, 'eurbla2': 56, 'eurcoo': 57, 'forwag1': 58, 'gargan': 59,\n                     'gloibi': 60, 'goflea1': 61, 'graher1': 62, 'grbeat1': 63, 'grecou1': 64, 'greegr': 65,\n                     'grefla1': 66, 'grehor1': 67, 'grejun2': 68, 'grenig1': 69, 'grewar3': 70, 'grnsan': 71,\n                     'grnwar1': 72, 'grtdro1': 73, 'gryfra': 74, 'grynig2': 75, 'grywag': 76, 'gybpri1': 77,\n                     'gyhcaf1': 78, 'heswoo1': 79, 'hoopoe': 80, 'houcro1': 81, 'houspa': 82, 'inbrob1': 83,\n                     'indpit1': 84, 'indrob1': 85, 'indrol2': 86, 'indtit1': 87, 'ingori1': 88, 'inpher1': 89,\n                     'insbab1': 90, 'insowl1': 91, 'integr': 92, 'isbduc1': 93, 'jerbus2': 94, 'junbab2': 95,\n                     'junmyn1': 96, 'junowl1': 97, 'kenplo1': 98, 'kerlau2': 99, 'labcro1': 100, 'laudov1': 101,\n                     'lblwar1': 102, 'lesyel1': 103, 'lewduc1': 104, 'lirplo': 105, 'litegr': 106, 'litgre1': 107,\n                     'litspi1': 108, 'litswi1': 109, 'lobsun2': 110, 'maghor2': 111, 'malpar1': 112, 'maltro1': 113,\n                     'malwoo1': 114, 'marsan': 115, 'mawthr1': 116, 'moipig1': 117, 'nilfly2': 118, 'niwpig1': 119,\n                     'nutman': 120, 'orihob2': 121, 'oripip1': 122, 'pabflo1': 123, 'paisto1': 124, 'piebus1': 125,\n                     'piekin1': 126, 'placuc3': 127, 'plaflo1': 128, 'plapri1': 129, 'plhpar1': 130, 'pomgrp2': 131,\n                     'purher1': 132, 'pursun3': 133, 'pursun4': 134, 'purswa3': 135, 'putbab1': 136, 'redspu1': 137,\n                     'rerswa1': 138, 'revbul': 139, 'rewbul': 140, 'rewlap1': 141, 'rocpig': 142, 'rorpar': 143,\n                     'rossta2': 144, 'rufbab3': 145, 'ruftre2': 146, 'rufwoo2': 147, 'rutfly6': 148, 'sbeowl1': 149,\n                     'scamin3': 150, 'shikra1': 151, 'smamin1': 152, 'sohmyn1': 153, 'spepic1': 154, 'spodov': 155,\n                     'spoowl1': 156, 'sqtbul1': 157, 'stbkin1': 158, 'sttwoo1': 159, 'thbwar1': 160, 'tibfly3': 161,\n                     'tilwar1': 162, 'vefnut1': 163, 'vehpar1': 164, 'wbbfly1': 165, 'wemhar1': 166, 'whbbul2': 167,\n                     'whbsho3': 168, 'whbtre1': 169, 'whbwag1': 170, 'whbwat1': 171, 'whbwoo2': 172, 'whcbar1': 173,\n                     'whiter2': 174, 'whrmun': 175, 'whtkin2': 176, 'woosan': 177, 'wynlau1': 178, 'yebbab1': 179,\n                     'yebbul3': 180, 'zitcis1': 181}\n\n}\n\n\nif len(os.listdir('/kaggle/input/birdclef-2024/test_soundscapes'))==1:\n    CFG['data'] = '/kaggle/input/birdclef-2024/unlabeled_soundscapes/'\n","metadata":{"execution":{"iopub.status.busy":"2024-06-13T07:34:00.456994Z","iopub.execute_input":"2024-06-13T07:34:00.457380Z","iopub.status.idle":"2024-06-13T07:34:00.482591Z","shell.execute_reply.started":"2024-06-13T07:34:00.457350Z","shell.execute_reply":"2024-06-13T07:34:00.481352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"weights_1d=[]\n\n\nCFG['weights_1d']=[os.path.join(CFG['weights_1d'],x) for x in sorted(os.listdir(CFG['weights_1d']))]\nCFG['weights_spec']=[os.path.join(CFG['weights_spec'],x) for x in sorted(os.listdir(CFG['weights_spec']))]\nCFG['weights_mix']=[os.path.join(CFG['weights_mix'],x) for x in sorted(os.listdir(CFG['weights_mix']))]\n\n\n","metadata":{"execution":{"iopub.status.busy":"2024-06-13T07:34:00.787354Z","iopub.execute_input":"2024-06-13T07:34:00.787899Z","iopub.status.idle":"2024-06-13T07:34:00.798011Z","shell.execute_reply.started":"2024-06-13T07:34:00.787851Z","shell.execute_reply":"2024-06-13T07:34:00.796761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# SPECTRUM=False\n\n\n\n\nclass Transform(nn.Module):\n    def __init__(self, ):\n        super().__init__()\n\n        \n        self.wave_transform = nn.Sequential(\n            torchaudio.transforms.MelSpectrogram(\n                32000,\n                n_mels=512,\n                f_min=0,\n                f_max=16000,\n                n_fft=2048*2,\n                hop_length=512,\n                normalized=True,\n            ),\n            torchaudio.transforms.AmplitudeToDB(top_db=80.0),\n\n        )\n        \n        self.resize = nn.UpsamplingBilinear2d(size=(256, 256))\n\n    def forward(self, x):\n        bs = x.size(0)\n        image = self.wave_transform(x)\n        \n        resized_image = torch.unsqueeze(image, dim=1)\n        resized_image = self.resize(resized_image)\n        resized_image = torch.squeeze(resized_image, dim=1)\n        \n        return image,resized_image\n\n","metadata":{"execution":{"iopub.status.busy":"2024-06-13T07:34:01.041290Z","iopub.execute_input":"2024-06-13T07:34:01.041706Z","iopub.status.idle":"2024-06-13T07:34:01.051478Z","shell.execute_reply.started":"2024-06-13T07:34:01.041672Z","shell.execute_reply":"2024-06-13T07:34:01.050368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#dataiter\nclass AlaskaDataIter():\n    def __init__(self, \n                 data_dir,\n                 training_flag=False,\n                 shuffle=False,\n                 use_spec=False):\n        \n        file_list=os.listdir(data_dir)\n        \n        self.file_list=[os.path.join(CFG['data'],x) for x in file_list if 'ogg' in x]\n        self.file_list.sort()\n        if CFG['data']=='/kaggle/input/birdclef-2024/unlabeled_soundscapes/':\n            self.file_list=self.file_list[:2]\n        self.training_flag=training_flag\n        self.use_spec=use_spec\n        if self.use_spec:\n            self.wave2spec=Transform().to('cpu')\n    def __getitem__(self, item):\n        \n        return self.single_map_func(self.file_list[item], self.training_flag)\n\n    def __len__(self):\n\n        return len(self.file_list)\n    \n    def safe_pad(self,waves,valid_lenth=32000*5):\n        L=waves.shape[0]\n\n\n        if L<valid_lenth:\n            padded_array = np.zeros(valid_lenth)\n            padded_array[:L] = waves\n\n            return padded_array\n        else:\n            return waves\n    def single_map_func(self, fn, is_training):\n        \"\"\"Data augmentation function.\"\"\"\n        \n        ####customed here\n        base_name = os.path.basename(fn)\n        \n        row_id=base_name.rsplit('.',1)[0]\n        \n        \n        waves, samplerate = sf.read(fn)\n            \n        \n        waves=np.reshape(waves,newshape=[-1,CFG['sample_rate']*CFG['slice_len']])\n\n        data=waves.astype(np.float32)\n        \n        # clip\n        for i in range(data.shape[0]):\n            max_v=np.max(np.abs(data[i]))\n            if max_v>1:\n                data[i]=data[i]/max_v\n        \n        raw=data\n        if self.use_spec:\n            data_tensor = torch.from_numpy(data).to('cpu')\n            \n            spec,spec_256 = self.wave2spec(data_tensor)\n            spec = spec.cpu().numpy().astype(np.float32)\n            spec_256= spec_256.cpu().numpy().astype(np.float32)\n            \n            \n        else:\n            spec=None\n            spec_256=None\n        row_ids=[]\n\n        for i in range(int(waves.shape[0]*(CFG['slice_len']/5))):\n            row_ids.append(row_id+'_%d'%(5*(i+1)))\n        \n        data={'raw':raw,\n              'spec':spec,\n              'mix':spec_256,\n              'row_ids':row_ids}\n        return data\n        ","metadata":{"execution":{"iopub.status.busy":"2024-06-13T07:34:01.309986Z","iopub.execute_input":"2024-06-13T07:34:01.310415Z","iopub.status.idle":"2024-06-13T07:34:01.330557Z","shell.execute_reply.started":"2024-06-13T07:34:01.310382Z","shell.execute_reply":"2024-06-13T07:34:01.329132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def run_single_model(models,data):\n    output_collection=[]\n    for vino_modle in models:\n        output = vino_modle(data)\n        # new a array to avoid err,\n        output = np.array(output)\n        output_collection.append(output)\n    ans=np.mean(output_collection,axis=0)\n    \n    return ans","metadata":{"execution":{"iopub.status.busy":"2024-06-13T07:34:01.656751Z","iopub.execute_input":"2024-06-13T07:34:01.657205Z","iopub.status.idle":"2024-06-13T07:34:01.663859Z","shell.execute_reply.started":"2024-06-13T07:34:01.657170Z","shell.execute_reply":"2024-06-13T07:34:01.662638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def inference(test_loader, models):\n    \n    prediction_dict = {}\n    preds = []\n    row_ids=[]\n    with tqdm(test_loader, unit=\"test_batch\", desc='Inference') as tqdm_test_loader:\n        for step, X in enumerate(tqdm_test_loader):\n            \n            X=X[0]\n            used_frame=CFG['used_frame']\n            \n            x_raw=X['raw']\n            x_spec=X['spec']\n            x_spec_256=X['mix']\n            \n            row_id=X['row_ids']\n            tmppre=[]\n            for j in range(len(x_raw)):\n                input_raw=x_raw[j][None,]\n                input_spec=x_spec[j][None,]\n                input_spec_256=x_spec_256[j][None,]\n\n                output1=run_single_model(models['raw'],[input_raw])\n                output2=run_single_model(models['spec'],[input_spec])\n                output3=run_single_model(models['mix'],[input_raw,input_spec_256])\n                \n                ans=output1*0.5+output2*0.4+output3*0.1\n                \n                tmppre.append(ans)\n            y_preds=np.concatenate(tmppre,axis=0)\n            \n            \n            preds.append(y_preds) \n            row_ids.append(row_id) \n                \n    prediction_dict[\"predictions\"] = np.concatenate(preds) \n    prediction_dict[\"row_ids\"] = np.concatenate(row_ids) \n    return prediction_dict","metadata":{"execution":{"iopub.status.busy":"2024-06-13T07:34:02.153666Z","iopub.execute_input":"2024-06-13T07:34:02.154064Z","iopub.status.idle":"2024-06-13T07:34:02.166441Z","shell.execute_reply.started":"2024-06-13T07:34:02.154034Z","shell.execute_reply":"2024-06-13T07:34:02.165172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def run():\n    \n    t0=time.time()\n    # dataloader\n    test_dataset = AlaskaDataIter(CFG['data'], training_flag=False, shuffle=False,use_spec=True)\n    test_loader = DataLoader(test_dataset,\n                     CFG['batch_size'],\n                     num_workers=CFG['num_worker'],\n                     shuffle=False,\n                     collate_fn=lambda x :x)\n    vino_models={'raw':[],\n               'spec':[],\n                'mix':[]}\n    \n    \n    for model_weight in CFG['weights_1d'][:3]:\n        model = VINOEngine(model_weight)\n        vino_models['raw'].append(model)\n        \n    for model_weight in CFG['weights_spec'][:1]:\n        model = VINOEngine(model_weight)\n        vino_models['spec'].append(model)\n        \n    for model_weight in CFG['weights_mix'][:1]:\n        model = VINOEngine(model_weight)\n        vino_models['mix'].append(model)\n    \n    prediction_dict = inference(test_loader, vino_models)\n    \n    \n\n    print('probably %.2f seconds'%(1100/2*(time.time()-t0)))\n    \n    return prediction_dict","metadata":{"execution":{"iopub.status.busy":"2024-06-13T07:34:02.612944Z","iopub.execute_input":"2024-06-13T07:34:02.613893Z","iopub.status.idle":"2024-06-13T07:34:02.624134Z","shell.execute_reply.started":"2024-06-13T07:34:02.613853Z","shell.execute_reply":"2024-06-13T07:34:02.622997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction_dict=run()","metadata":{"execution":{"iopub.status.busy":"2024-06-13T07:34:02.919618Z","iopub.execute_input":"2024-06-13T07:34:02.919983Z","iopub.status.idle":"2024-06-13T07:34:19.763315Z","shell.execute_reply.started":"2024-06-13T07:34:02.919955Z","shell.execute_reply":"2024-06-13T07:34:19.762003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(prediction_dict['row_ids'].shape)\nprint(prediction_dict['predictions'].shape)","metadata":{"execution":{"iopub.status.busy":"2024-06-13T07:34:21.985965Z","iopub.execute_input":"2024-06-13T07:34:21.986445Z","iopub.status.idle":"2024-06-13T07:34:21.993332Z","shell.execute_reply.started":"2024-06-13T07:34:21.986404Z","shell.execute_reply":"2024-06-13T07:34:21.991913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"row_ids= list(prediction_dict['row_ids'])\nrow_ids=[str(x) for x in row_ids]\n\nsub_pred = pd.DataFrame(prediction_dict['predictions'], columns=CFG[\"nm2cls\"].keys())\nsub_id = pd.DataFrame({'row_id': row_ids})\n\nsub = pd.concat([sub_id, sub_pred], axis=1)\n\nsub.to_csv('submission.csv',index=False)\nprint(f'Submissionn shape: {sub.shape}')\nsub.head(20)","metadata":{"execution":{"iopub.status.busy":"2024-06-13T07:34:22.141328Z","iopub.execute_input":"2024-06-13T07:34:22.141740Z","iopub.status.idle":"2024-06-13T07:34:22.243781Z","shell.execute_reply.started":"2024-06-13T07:34:22.141707Z","shell.execute_reply":"2024-06-13T07:34:22.242593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}