{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"\n\n!pip install /kaggle/input/audiomentations-v0290/resampy-0.4.2-py3-none-any.whl\n!pip install  /kaggle/input/audiomentations-v0290/librosa-0.9.2-py3-none-any.whl\n\n\n!export OMP_NUM_THREADS=N\n\n!export OMP_SCHEDULE=STATIC\n!export OMP_PROC_BIND=CLOSE\n!export GOMP_CPU_AFFINITY=\"N-M\"\n\n","metadata":{"execution":{"iopub.status.busy":"2023-06-18T19:19:40.887199Z","iopub.execute_input":"2023-06-18T19:19:40.887702Z","iopub.status.idle":"2023-06-18T19:20:47.972302Z","shell.execute_reply.started":"2023-06-18T19:19:40.887662Z","shell.execute_reply":"2023-06-18T19:20:47.970923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n!pip install /kaggle/input/openvino-wheels/openvino-2022.3.0-9052-cp37-cp37m-manylinux_2_17_x86_64.whl --no-index --find-links /kaggle/input/openvino-wheels","metadata":{"execution":{"iopub.status.busy":"2023-06-18T19:20:47.975152Z","iopub.execute_input":"2023-06-18T19:20:47.975850Z","iopub.status.idle":"2023-06-18T19:20:59.134840Z","shell.execute_reply.started":"2023-06-18T19:20:47.975804Z","shell.execute_reply":"2023-06-18T19:20:59.133313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import openvino.runtime as ov","metadata":{"execution":{"iopub.status.busy":"2023-06-18T19:20:59.136545Z","iopub.execute_input":"2023-06-18T19:20:59.137008Z","iopub.status.idle":"2023-06-18T19:20:59.144059Z","shell.execute_reply.started":"2023-06-18T19:20:59.136963Z","shell.execute_reply":"2023-06-18T19:20:59.142851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from pathlib import Path\nimport random\n\nimport numpy as np\nimport pandas as pd\nimport time\nimport os\nimport matplotlib.pyplot as plt\n# These transformations will be passed to our model class\nimport torch\nimport torch.nn.functional as F\nimport torch.nn as nn\nimport yaml\nfrom tqdm.auto import tqdm\nimport glob\nfrom torch.distributions import Beta\nimport librosa","metadata":{"papermill":{"duration":6.939158,"end_time":"2021-12-13T10:09:03.241098","exception":false,"start_time":"2021-12-13T10:08:56.30194","status":"completed"},"scrolled":true,"tags":[],"execution":{"iopub.status.busy":"2023-06-18T19:20:59.146114Z","iopub.execute_input":"2023-06-18T19:20:59.146527Z","iopub.status.idle":"2023-06-18T19:20:59.155946Z","shell.execute_reply.started":"2023-06-18T19:20:59.146486Z","shell.execute_reply":"2023-06-18T19:20:59.155085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nfrom os.path import exists\n\nWAV_SIZE=16000\nSTEP_SIZE=500\nTIMES_REAL=4\nTIMES_TRAIN=8\nis_mixed_precision = True\nTARGET_COLS = ['StartHesitation', 'Turn', 'Walking']\n\nclass GaitDataset(torch.utils.data.Dataset):\n\n    def __init__(self, df, is_train=False,transforms=None):\n        self.is_train = is_train\n        self.data = df\n\n    def __len__(self):\n        if self.is_train:\n            return len(self.data)*TIMES_TRAIN\n        else:\n            return len(self.data)\n    \n    \n    def __getitem__(self, idx):\n        g0=9.80665\n        row = self.data.iloc[idx]\n        data = pd.read_csv(row.filename)\n        \n        print(row.Id, data.shape)\n        \n        sig = data[[ 'AccV', 'AccML', 'AccAP']].values\n        \n        if row.type == 0:\n            sigs = []\n            for c in range(3):\n                sigs.append(librosa.resample(sig[:,c],orig_sr=128,target_sr=100))\n            wav = np.stack(sigs,axis=1)\n        else:\n            wav = sig*g0\n        \n        print('after resampling',wav.shape)\n        wav = wav/40.\n        act_len = len(wav)\n        nchunk = len(wav)//WAV_SIZE\n        rem_size = len(wav) - nchunk*WAV_SIZE\n        arrs = []\n        for chk in range(nchunk):\n            arrs.append(wav[chk*WAV_SIZE:(chk+1)*WAV_SIZE])\n\n        \n        if rem_size > 0:\n            last_arr = wav[-WAV_SIZE:]\n            arrs.append(last_arr)\n            \n        wav = np.stack(arrs,axis=0)\n        \n        print('wav',wav.shape,rem_size)\n        \n        sample = {\"wav\": wav, \"Id\":row.Id, 'type': row.type, 'df_length':len(data), \n                  'act_len':act_len,'nchunk':nchunk,'rem_size':rem_size}\n\n        return sample\n        \n\ndef getDataLoader(params,val_x):\n    \n    val_dataset = GaitDataset(df=val_x, transforms=None)\n\n    valDataLoader = torch.utils.data.DataLoader(\n                        val_dataset,\n                        batch_size=1,\n                        num_workers=params['num_workers'],\n                        shuffle=False,\n                        pin_memory=False,\n                    )\n    \n    return valDataLoader","metadata":{"papermill":{"duration":0.04712,"end_time":"2021-12-13T10:09:03.372538","exception":false,"start_time":"2021-12-13T10:09:03.325418","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-06-18T19:21:19.904922Z","iopub.execute_input":"2023-06-18T19:21:19.905759Z","iopub.status.idle":"2023-06-18T19:21:19.921637Z","shell.execute_reply.started":"2023-06-18T19:21:19.905694Z","shell.execute_reply":"2023-06-18T19:21:19.920736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Wave_Block(nn.Module):\n\n    def __init__(self, in_channels, out_channels, dilation_rates, kernel_size):\n        super(Wave_Block, self).__init__()\n        self.num_rates = dilation_rates\n        self.convs = nn.ModuleList()\n        self.filter_convs = nn.ModuleList()\n        self.gate_convs = nn.ModuleList()\n\n        self.convs.append(nn.Conv1d(in_channels, out_channels, kernel_size=1))\n        dilation_rates = [2 ** i for i in range(dilation_rates)]\n        for dilation_rate in dilation_rates:\n            self.filter_convs.append(\n                nn.Conv1d(out_channels, out_channels, kernel_size=kernel_size, padding=int((dilation_rate*(kernel_size-1))/2), dilation=dilation_rate))\n            self.gate_convs.append(\n                nn.Conv1d(out_channels, out_channels, kernel_size=kernel_size, padding=int((dilation_rate*(kernel_size-1))/2), dilation=dilation_rate))\n            self.convs.append(nn.Conv1d(out_channels, out_channels, kernel_size=1))\n\n    def forward(self, x):\n        x = self.convs[0](x)\n        res = x\n        for i in range(self.num_rates):\n            x = torch.tanh(self.filter_convs[i](x)) * torch.sigmoid(self.gate_convs[i](x))\n            x = self.convs[i + 1](x)\n            res = res + x\n        return res\n# detail \nclass Classifier(nn.Module):\n    def __init__(self, inch=3, kernel_size=3):\n        super().__init__()\n        self.LSTM = nn.GRU(input_size=128, hidden_size=128, num_layers=4, \n                           batch_first=True, bidirectional=True)\n        \n        #self.wave_block1 = Wave_Block(inch, 16, 12, kernel_size)\n        self.wave_block2 = Wave_Block(inch, 32, 8, kernel_size)\n        self.wave_block3 = Wave_Block(32, 64, 4, kernel_size)\n        self.wave_block4 = Wave_Block(64, 128, 1, kernel_size)\n        self.fc1 = nn.Linear(256, 3)\n\n    def forward(self, x):\n        x = x.permute(0, 2, 1)\n        #x = self.wave_block1(x)\n        x = self.wave_block2(x)\n        x = self.wave_block3(x)\n\n        x = self.wave_block4(x)\n        x = x.permute(0, 2, 1)\n        x, h = self.LSTM(x)\n        x = self.fc1(x)\n    \n        \n        return x","metadata":{"execution":{"iopub.status.busy":"2023-06-18T19:20:59.178994Z","iopub.execute_input":"2023-06-18T19:20:59.179744Z","iopub.status.idle":"2023-06-18T19:20:59.196923Z","shell.execute_reply.started":"2023-06-18T19:20:59.179697Z","shell.execute_reply":"2023-06-18T19:20:59.195735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def save_model(epoch,model,ckpt_path='./',name='',val_rmse=0):\n    path = os.path.join(ckpt_path, '{}_{}.pth'.format(name, epoch))\n    torch.save(model.state_dict(), path, _use_new_zipfile_serialization=False)\n    \ndef load_model(model,ckpt_path):\n    state = torch.load(ckpt_path,map_location=torch.device('cpu'))\n    print(model.load_state_dict(state,strict=False))\n    return model","metadata":{"papermill":{"duration":0.032012,"end_time":"2021-12-13T10:09:04.072285","exception":false,"start_time":"2021-12-13T10:09:04.040273","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-06-18T19:20:59.198870Z","iopub.execute_input":"2023-06-18T19:20:59.199308Z","iopub.status.idle":"2023-06-18T19:20:59.210900Z","shell.execute_reply.started":"2023-06-18T19:20:59.199263Z","shell.execute_reply":"2023-06-18T19:20:59.209635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\ndef validation_step(model, batch, batch_idx):\n    # Load images and labels\n    x = batch[\"wav\"].float()\n    if GPU:\n        x= x.cuda(non_blocking=True)\n    x = x[0]\n    \n    print('x',x.shape)\n    # Forward pass & softmax\n    \n    flat_pred = np.zeros((batch['act_len'][0],3))\n    with torch.no_grad():\n        preds = model.infer(inputs=[x])\n        preds = torch.tensor(preds[list(preds.keys())[0]])\n\n        print('preds',preds.shape)\n     \n    for i in range(batch['nchunk'][0]):\n        flat_pred[i*WAV_SIZE:(i+1)*WAV_SIZE] = torch.sigmoid(preds[i]).detach().cpu().numpy()\n        \n    rem_sz = batch['rem_size'][0]\n    if rem_sz > 0:\n        flat_pred[-rem_sz:] = torch.sigmoid(preds[-1]).detach().cpu().numpy()[-rem_sz:]\n\n    return flat_pred\n\n","metadata":{"papermill":{"duration":0.040406,"end_time":"2021-12-13T10:09:04.193173","exception":false,"start_time":"2021-12-13T10:09:04.152767","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-06-18T19:20:59.212405Z","iopub.execute_input":"2023-06-18T19:20:59.214665Z","iopub.status.idle":"2023-06-18T19:20:59.226510Z","shell.execute_reply.started":"2023-06-18T19:20:59.214615Z","shell.execute_reply":"2023-06-18T19:20:59.225378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import roc_auc_score,f1_score,precision_score,average_precision_score\n        \n\ndef test_epoch(model,valDataLoader):\n\n    total_loss=0\n    total_step=0\n    #model.eval()\n    pred_dfs = []\n\n    pbar=tqdm(enumerate(valDataLoader),total=len(valDataLoader))\n    for bi,data in pbar :\n        pred = validation_step(model,data,bi)\n        \n        if data['type'][0] == 0:\n            preds = []\n            for c in range(3):\n                preds.append(librosa.resample(pred[:,c].astype(np.float32),orig_sr=100,target_sr=128))\n            pred = np.stack(preds,axis=1)\n            pred = np.clip(pred,0,1)\n            \n            pred1 = np.zeros((data['df_length'][0],3))\n            pred1[0:data['df_length'][0]] = pred[0:data['df_length'][0]]\n            pred=pred1\n        \n        total_step+=1\n        preds_df = pd.DataFrame(pred)\n        print('preds_df',preds_df.shape)\n        preds_df.columns = TARGET_COLS\n        preds_df['Id'] = data['Id'][0]\n        preds_df['Id'] = preds_df['Id'] + '_' + preds_df.index.values.astype(str)\n\n        pred_dfs.append(preds_df)\n      \n    print('len preds_df',len(preds))\n    preds = pd.concat(pred_dfs)\n\n    print('preds',preds.shape)\n    return preds[TARGET_COLS] , preds[['Id']]","metadata":{"papermill":{"duration":0.036917,"end_time":"2021-12-13T10:09:04.254423","exception":false,"start_time":"2021-12-13T10:09:04.217506","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-06-18T19:20:59.228524Z","iopub.execute_input":"2023-06-18T19:20:59.229042Z","iopub.status.idle":"2023-06-18T19:20:59.244392Z","shell.execute_reply.started":"2023-06-18T19:20:59.228999Z","shell.execute_reply":"2023-06-18T19:20:59.243540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"core = ov.Core()","metadata":{"execution":{"iopub.status.busy":"2023-06-18T19:20:59.245912Z","iopub.execute_input":"2023-06-18T19:20:59.247052Z","iopub.status.idle":"2023-06-18T19:20:59.261528Z","shell.execute_reply.started":"2023-06-18T19:20:59.247004Z","shell.execute_reply":"2023-06-18T19:20:59.260319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"GPU=False\ndef test_loop(params,test_x,ckpt_paths):\n    \n    #create model\n    \n    \n    models = []\n    \n    ckpt_paths = glob.glob('/kaggle/input/openvino-model-converter-data/*.onnx')\n    ckpt_paths1 = glob.glob('/kaggle/input/gait-wavenet-focal-onnx/*.onnx')\n    #ckpt_paths2 = glob.glob('/kaggle/input/wavenet-from-pretrain-v1/*.onnx')\n    ckpt_paths2 = glob.glob('/kaggle/input/gait-all-models-v2/wavenet-from-pretrain-v3_wavenet_2000_pretrain*.onnx')\n    ckpt_paths.extend(ckpt_paths1)\n    ckpt_paths.extend(ckpt_paths2)\n    \n    print('ckpt_paths',ckpt_paths2)\n    \n    valDataLoader = getDataLoader(params,test_x)\n    \n    preds_dfs = []\n    id_df = None\n    for ckpt_path in ckpt_paths:\n        openvino_model = core.read_model(model=ckpt_path)\n        compiled_model = core.compile_model(openvino_model, device_name=\"CPU\")\n        infer_request = compiled_model.create_infer_request()\n\n        df, id_df = test_epoch(infer_request,valDataLoader)\n        print('df',df)\n        preds_dfs.append(df)\n        \n        del infer_request,compiled_model,openvino_model\n        gc.collect()\n    \n    preds = preds_dfs[0].copy()\n    for pred_df in preds_dfs:\n        for c in TARGET_COLS:\n            preds[c] += pred_df[c]\n        \n    for c in TARGET_COLS:\n        preds[c] /= len(ckpt_paths)\n    preds['Id'] = id_df\n    return preds","metadata":{"execution":{"iopub.status.busy":"2023-06-18T19:20:59.263438Z","iopub.execute_input":"2023-06-18T19:20:59.264252Z","iopub.status.idle":"2023-06-18T19:20:59.275578Z","shell.execute_reply.started":"2023-06-18T19:20:59.264201Z","shell.execute_reply":"2023-06-18T19:20:59.274676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hparams = {\n    # Optional hparams\n    \"backbone\": 'wavenet_4096', #'', #'tf_efficientnetv2_b2',\n    \"learning_rate\": [5e-4],\n    \"max_epochs\": 121,\n    \"batch_size\": 8,\n    \"num_workers\": 0,\n    \"val_sanity_checks\": 0,\n    \"fast_dev_run\": False,\n    \"output_path\": f\"\",\n    \"gpu\": torch.cuda.is_available(),\n    'div_factor':10,\n    'final_div_factor':20,\n}","metadata":{"papermill":{"duration":0.031307,"end_time":"2021-12-13T10:09:04.424259","exception":false,"start_time":"2021-12-13T10:09:04.392952","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-06-18T19:20:59.276684Z","iopub.execute_input":"2023-06-18T19:20:59.279662Z","iopub.status.idle":"2023-06-18T19:20:59.289225Z","shell.execute_reply.started":"2023-06-18T19:20:59.279614Z","shell.execute_reply":"2023-06-18T19:20:59.288405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random\nseed=42\ndef set_seed(seed=42):\n    torch.manual_seed(seed)\n    torch.cuda.manual_seed(seed)\n    torch.backends.cudnn.deterministic = False\n    torch.use_deterministic_algorithms = True\n    random.seed(0)\n    np.random.seed(0)\nset_seed(seed)\n\n","metadata":{"execution":{"iopub.status.busy":"2023-06-18T19:20:59.292931Z","iopub.execute_input":"2023-06-18T19:20:59.293267Z","iopub.status.idle":"2023-06-18T19:20:59.303373Z","shell.execute_reply.started":"2023-06-18T19:20:59.293234Z","shell.execute_reply":"2023-06-18T19:20:59.302259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import glob\nimport os\nimport pandas as pd\n\ntdcsfog_files = glob.glob('/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/test/tdcsfog/*.csv')\ntdcsfog_df = pd.DataFrame({'filename':tdcsfog_files, 'type':0} )\ndefog_files = glob.glob('/kaggle/input/tlvmc-parkinsons-freezing-gait-prediction/test/defog/*.csv')\ndefog_df = pd.DataFrame({'filename':defog_files, 'type':1} )\n\nfog_data = pd.concat([tdcsfog_df,defog_df]).reset_index(drop=True)\nfog_data['Id'] = fog_data.filename.apply(lambda f:os.path.basename(f).replace('.csv',''))","metadata":{"execution":{"iopub.status.busy":"2023-06-18T19:20:59.309633Z","iopub.execute_input":"2023-06-18T19:20:59.309954Z","iopub.status.idle":"2023-06-18T19:20:59.327070Z","shell.execute_reply.started":"2023-06-18T19:20:59.309922Z","shell.execute_reply":"2023-06-18T19:20:59.325847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fog_data","metadata":{"execution":{"iopub.status.busy":"2023-06-18T19:20:59.328829Z","iopub.execute_input":"2023-06-18T19:20:59.329544Z","iopub.status.idle":"2023-06-18T19:20:59.341709Z","shell.execute_reply.started":"2023-06-18T19:20:59.329498Z","shell.execute_reply":"2023-06-18T19:20:59.340341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nimport gc\nimport glob\nversion='1'\nfn=0\n\n\n\nsubm = test_loop(hparams,fog_data,None)","metadata":{"papermill":{"duration":0.04818,"end_time":"2021-12-13T13:17:52.100451","exception":false,"start_time":"2021-12-13T13:17:52.052271","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-06-18T19:21:27.111929Z","iopub.execute_input":"2023-06-18T19:21:27.112327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"subm","metadata":{"execution":{"iopub.status.busy":"2023-06-18T19:20:59.701636Z","iopub.execute_input":"2023-06-18T19:20:59.701961Z","iopub.status.idle":"2023-06-18T19:20:59.730779Z","shell.execute_reply.started":"2023-06-18T19:20:59.701929Z","shell.execute_reply":"2023-06-18T19:20:59.728963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"subm[['Id','StartHesitation','Turn','Walking']].to_csv('./submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2023-06-18T19:20:59.731678Z","iopub.status.idle":"2023-06-18T19:20:59.732084Z","shell.execute_reply.started":"2023-06-18T19:20:59.731887Z","shell.execute_reply":"2023-06-18T19:20:59.731908Z"},"trusted":true},"execution_count":null,"outputs":[]}]}