{"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":"!pip install \"/kaggle/input/torchlibrosa/torchlibrosa-0.0.5-py3-none-any.whl\"\n!pip install /kaggle/input/pip-intel-extension-for-pytorch/intel_extension_for_pytorch-1.13.100-cp37-cp37m-manylinux2014_x86_64.whl","metadata":{"execution":{"iopub.status.busy":"2023-05-23T18:12:24.803246Z","iopub.execute_input":"2023-05-23T18:12:24.805455Z","iopub.status.idle":"2023-05-23T18:13:40.158698Z","shell.execute_reply.started":"2023-05-23T18:12:24.805373Z","shell.execute_reply":"2023-05-23T18:13:40.156938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2023-05-23T18:13:40.162598Z","iopub.execute_input":"2023-05-23T18:13:40.163112Z","iopub.status.idle":"2023-05-23T18:13:46.201524Z","shell.execute_reply.started":"2023-05-23T18:13:40.163050Z","shell.execute_reply":"2023-05-23T18:13:46.199323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import intel_extension_for_pytorch as ipex\n\nimport os,sys,re,glob,random, gc\nimport pandas as pd\nimport librosa as lb\nimport IPython.display as ipd\nimport soundfile as sf\nimport numpy as np\nimport cv2\nimport ast, joblib\nfrom pathlib import Path\nimport torchaudio\n\n%matplotlib inline\nimport matplotlib.pyplot as plt\nimport librosa.display\nfrom sklearn import preprocessing\n\n#Deep learning from pytorch\nimport torch\nimport torchvision\nfrom torch.utils.data import DataLoader, Dataset\nimport torch.optim as optim\nfrom torchvision import transforms\nimport torch.nn as nn\nimport torch.nn.functional as F\n\nfrom albumentations.core.transforms_interface import ImageOnlyTransform\nfrom tqdm import tqdm\nfrom torch.nn.parameter import Parameter\nimport copy, codecs\nimport sklearn.metrics\n\n#timmのdirpathを設定\ntimm_path = \"/kaggle/input/timm-pytorch-image-models/pytorch-image-models-master\"\n\nimport sys\nsys.path.append(timm_path)\nimport timm\n\nimport concurrent.futures\nimport warnings\nwarnings.simplefilter('ignore')\n\ntorch.set_flush_denormal(True)","metadata":{"execution":{"iopub.status.busy":"2023-05-23T18:13:46.204733Z","iopub.execute_input":"2023-05-23T18:13:46.205735Z","iopub.status.idle":"2023-05-23T18:13:51.095646Z","shell.execute_reply.started":"2023-05-23T18:13:46.205685Z","shell.execute_reply":"2023-05-23T18:13:51.092955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/birdclef2023-split-creating-dataframe/train.csv\")\n\nprimary_label_counts_map = train[\"label_id\"].value_counts().to_dict()\nsecondary_label_counts_map = train[\"labels_id\"].apply(eval).explode().value_counts().to_dict()\n\nlabel_counts_map = {}\nfor key, values in primary_label_counts_map.items():\n    if key in secondary_label_counts_map:\n        label_counts_map[key] = values + secondary_label_counts_map[key]\n    else:\n        label_counts_map[key] = values\n        \nlabel_counts = pd.DataFrame.from_dict(label_counts_map, orient=\"index\").sort_values(0)\n\nsmall_lc_index = list(label_counts[:50].index)\nlarge_lc_index = list(label_counts[50:].index)","metadata":{"execution":{"iopub.status.busy":"2023-05-23T18:13:51.099500Z","iopub.execute_input":"2023-05-23T18:13:51.099923Z","iopub.status.idle":"2023-05-23T18:13:51.546853Z","shell.execute_reply.started":"2023-05-23T18:13:51.099881Z","shell.execute_reply":"2023-05-23T18:13:51.545221Z"},"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":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv(\"/kaggle/input/birdclef-2023/sample_submission.csv\")\nunique_key = list(submission.columns[1:])\nlabel2id = {label: label_id for label_id, label in enumerate(sorted(unique_key))}\nid2label = {val: key for key,val in label2id.items()}","metadata":{"execution":{"iopub.status.busy":"2023-05-23T18:13:51.548448Z","iopub.execute_input":"2023-05-23T18:13:51.549117Z","iopub.status.idle":"2023-05-23T18:13:51.583427Z","shell.execute_reply.started":"2023-05-23T18:13:51.549050Z","shell.execute_reply":"2023-05-23T18:13:51.581812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CFG:\n    #クラス数\n    CLASS_NUM = len(unique_key)\n\n    #model name\n    model_name = 'eca_nfnet_l0'\n\n    #重みを保存するディレクトリ\n    weight_path1 = f\"/kaggle/input/feature0511-mixupbugfix/ccc435294a9f5f9989f5865def93da0817c3b030/model_all_35_last.bin\"\n\n    weight_path2 = f\"/kaggle/input/featuresimplemixup/3379b034400d6bac17f3c2a39f63b0efb682813c/model_all_355_last.bin\"\n    \n    weight_path3 = f\"/kaggle/input/feature-mainaddpp64/ea70ecc95de1625212465b8e0148bc996e0237f9/model_all_35_last.bin\"\n    \n    weight_path4 = f\"/kaggle/input/main64best/c9e077c172368c31c2d1974e0288554ebd014b2f/model_all_3551_last.bin\"\n    \n    weight_path5 = f\"/kaggle/input/mainaddpp64att/a3791c561544adcc37507d5c27ec98e9d1c603a7/model_all_35_last.bin\"\n    \n    #切り取る時間(validationが5秒なので5秒)\n    period = 5\n    \n    #切り取るサンプリング周波数 (最大周波数×2を目安として取る場合が多い。)\n    sr = 32000\n    \n    #メル周波数\n    n_mel = 128\n    \n    #最小周波数\n    fmin = 50\n    \n    #最大周波数\n    fmax = 14000\n    \n    power = 2\n    \n    top_db = None\n    \n    n_fft = 1024\n    \n    hop_len = 320","metadata":{"execution":{"iopub.status.busy":"2023-05-23T18:13:51.585456Z","iopub.execute_input":"2023-05-23T18:13:51.586112Z","iopub.status.idle":"2023-05-23T18:13:51.598044Z","shell.execute_reply.started":"2023-05-23T18:13:51.586048Z","shell.execute_reply":"2023-05-23T18:13:51.596065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class WaveformDataset:\n    def __init__(self,\n                 cfg\n                 ):        \n        #make Melspectrum\n        self.cfg = cfg\n        self.sr = cfg.sr\n        self.period = cfg.period\n        \n        #wav to image helper\n        self.mel1 = torchaudio.transforms.MelSpectrogram(\n            n_mels = cfg.n_mel, \n            sample_rate= cfg.sr, \n            f_min = cfg.fmin, \n            f_max = cfg.fmax, \n            n_fft = cfg.n_fft, \n            hop_length=cfg.hop_len,\n            norm = None,\n            power = cfg.power,\n            mel_scale = 'htk')\n        \n        self.mel2 = torchaudio.transforms.MelSpectrogram(\n            n_mels = 64, \n            sample_rate= cfg.sr, \n            f_min = cfg.fmin, \n            f_max = cfg.fmax, \n            n_fft = cfg.n_fft, \n            hop_length=cfg.hop_len,\n            norm = None,\n            power = cfg.power,\n            mel_scale = 'htk')\n        \n        self.ptodb = torchaudio.transforms.AmplitudeToDB(top_db=cfg.top_db)\n    \n    def make_melspec1(self, wav):\n        melimg= self.mel1(wav)\n        dbimg = self.ptodb(melimg)\n        img = (dbimg.to(torch.float32) + 80)/80\n        return img\n    \n    def make_melspec2(self, wav):\n        melimg= self.mel2(wav)\n        dbimg = self.ptodb(melimg)\n        img = (dbimg.to(torch.float32) + 80)/80\n        return img\n\n    def crop_or_pad(self, y, length, is_train=False, start=None):\n        if len(y) < length:\n            y = np.concatenate([y, np.zeros(length - len(y))])\n\n            n_repeats = length // len(y)\n            epsilon = length % len(y)\n\n            y = np.concatenate([y]*n_repeats + [y[:epsilon]])\n\n        elif len(y) > length:\n            if not is_train:\n                start = start or 0\n            else:\n                start = start or np.random.randint(len(y) - length)\n\n            y = y[start:start + length]\n\n        return y\n\n    def __call__(self, path):\n        #データ読み込み\n        data, sr = librosa.load(path, sr=self.sr, mono=True)\n\n        #test datasetの最大長\n        max_sec = len(data)//sr\n\n        #データを5秒間隔でかつ7秒幅を取って区切る\n        datas = [data[int(i * sr):int(min(max_sec, i + self.period) * sr)] for i in range(0, max_sec, self.period)]\n\n        if len(datas[0]) < sr*self.period:\n            datas[0] = self.crop_or_pad(datas[0] , length=sr*self.period)\n        if len(datas[-1]) < sr*self.period:\n            datas[-1] = self.crop_or_pad(datas[-1] , length=sr*self.period)\n            \n        audio = torch.tensor(np.stack(datas),dtype=torch.float32)\n\n        #データをメル周波数によって画像化\n        images1 = self.make_melspec1(audio)\n        images2 = self.make_melspec2(audio)\n\n        #保存\n        filename = path.split(\"/\")[-1]\n        path = f\"/kaggle/audio_images/{filename}.pt\"\n        save_img = {\n            \"nf1024hop320\":images1,\n            \"nf2048hop512\":images2\n        }\n        torch.save(save_img, path)\n        \ndef get_audios_as_images(paths):\n    pool = joblib.Parallel(4)\n    \n    converter = WaveformDataset(\n        cfg= CFG\n    )\n    #converter(paths[0])\n    mapper = joblib.delayed(converter)\n    tasks = [mapper(path) for path in tqdm(paths)]\n    pool(tqdm(tasks))","metadata":{"execution":{"iopub.status.busy":"2023-05-23T18:13:51.600532Z","iopub.execute_input":"2023-05-23T18:13:51.600921Z","iopub.status.idle":"2023-05-23T18:13:51.636019Z","shell.execute_reply.started":"2023-05-23T18:13:51.600884Z","shell.execute_reply":"2023-05-23T18:13:51.634402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir /kaggle/audio_images\npaths = glob.glob(\"/kaggle/input/birdclef-2023/test_soundscapes/*.ogg\")\nget_audios_as_images(paths)","metadata":{"execution":{"iopub.status.busy":"2023-05-23T18:13:51.637926Z","iopub.execute_input":"2023-05-23T18:13:51.638658Z","iopub.status.idle":"2023-05-23T18:14:14.189758Z","shell.execute_reply.started":"2023-05-23T18:13:51.638599Z","shell.execute_reply":"2023-05-23T18:14:14.187893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pdf = pd.DataFrame(glob.glob(\"/kaggle/audio_images/*\"),columns=[\"path\"])\npdf[\"row_id\"] = pdf.path.apply(lambda x: x.split(\"/\")[-1].replace(\".ogg.pt\",\"\"))\npdf","metadata":{"execution":{"iopub.status.busy":"2023-05-23T18:14:14.192674Z","iopub.execute_input":"2023-05-23T18:14:14.193222Z","iopub.status.idle":"2023-05-23T18:14:14.233824Z","shell.execute_reply.started":"2023-05-23T18:14:14.193164Z","shell.execute_reply":"2023-05-23T18:14:14.232378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class gemModel(nn.Module):\n    def __init__(self,CFG,pretrained=False,path=None,training=True):\n        super(gemModel, self).__init__()\n        self.model = timm.create_model(\n            CFG.model_name,\n            pretrained=pretrained, \n            drop_rate=0, \n            drop_path_rate=0, \n            in_chans=1,\n            global_pool=\"\",\n            num_classes=0\n        )\n        in_features = self.model.num_features\n        self.fc = nn.Linear(in_features, CFG.CLASS_NUM)\n        \n    def gem(self, x, p=3, eps=1e-6):\n        return F.avg_pool2d(x.clamp(min=eps).pow(p), (x.size(-2), x.size(-1))).pow(1.0 / p)\n        \n    def forward(self, x, y=None, w=None):\n        x = self.model(x)\n        x = self.gem(x)[:,:,0,0]\n        x = self.fc(x)\n        return x\n    \nclass attModel(nn.Module):\n    def __init__(self,CFG,pretrained=False,path=None,training=True):\n        super(attModel, self).__init__()\n        self.model = timm.create_model(\n            CFG.model_name,\n            pretrained=pretrained, \n            drop_rate=0, \n            drop_path_rate=0, \n            in_chans=1,\n            global_pool=\"\",\n            num_classes=0\n        )\n        in_features = self.model.num_features\n        self.fc = nn.Linear(in_features, CFG.CLASS_NUM)\n        self.attention = nn.Sequential(nn.Linear(in_features, 512), nn.ReLU(), nn.Linear(512, 1))\n        \n    def forward(self, x, y=None, w=None):\n        x = self.model(x)\n        x = x.mean(dim=2)\n        x = x.permute(0, 2, 1)\n        attn_weights = torch.softmax(self.attention(x), dim=1)\n        x = (x * attn_weights).sum(dim=1)\n        x = self.fc(x)\n        return x","metadata":{"execution":{"iopub.status.busy":"2023-05-23T18:14:14.241776Z","iopub.execute_input":"2023-05-23T18:14:14.242235Z","iopub.status.idle":"2023-05-23T18:14:14.266110Z","shell.execute_reply.started":"2023-05-23T18:14:14.242194Z","shell.execute_reply":"2023-05-23T18:14:14.264168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Trainer:\n    def __init__(self, model1, model2, model3, model4, model5, pdf, split_size):\n        \"\"\"\n        Constructor for Trainer class\n        \"\"\"\n        self.model1 = model1\n        self.model2 = model2\n        self.model3 = model3\n        self.model4 = model4\n        self.model5 = model5\n        self.pdf = pdf\n        self.split_size = split_size\n    \n    def test_one_batch_64(self, x):\n        p3 = self.model3(x).sigmoid().detach()\n        p4 = self.model4(x).sigmoid().detach()\n        p5 = self.model5(x).sigmoid().detach()\n        p = p3*0.3 + 0.35*p4 + 0.35*p5\n        return p\n    \n    def test_one_batch(self, x):\n        p1 = self.model1(x).sigmoid().detach()\n        p2 = self.model2(x[:,:,:,50:450]).sigmoid().detach()\n        p = 0.55*p1+0.45*p2\n        return p\n        \n    def test_one_cycle(self):\n        pred_df = {}\n        \n        #get feature vectors array\n        pbar = tqdm(self.pdf.iterrows(),total=len(pdf))\n        with torch.no_grad():\n            for idx, row in pbar:\n                tmp = torch.load(row.path)\n\n                xtest = tmp[\"nf2048hop512\"][:,None,:,:]\n                b, c, f, t = xtest.shape\n                xb = xtest.reshape(b//(self.split_size*2), self.split_size*2, c, f, t)\n                xb = [x.to(memory_format=torch.channels_last) for x in xb]\n                with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor:\n                    preds = list(executor.map(self.test_one_batch_64, xb))\n                preds1 = torch.cat(preds).numpy()\n                \n                xtest = tmp[\"nf1024hop320\"][:,None,:,:]\n                b, c, f, t = xtest.shape\n                xb = xtest.reshape(b//self.split_size, self.split_size, c, f, t)\n                xb = [x.to(memory_format=torch.channels_last) for x in xb]\n                with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor:\n                    preds = list(executor.map(self.test_one_batch, xb))\n                preds2 = torch.cat(preds).numpy()\n                \n                preds = np.zeros_like(preds1)\n                \n                preds[:, small_lc_index] = 0.6*preds1[:, small_lc_index] + 0.4*preds2[:, small_lc_index]\n                preds[:, large_lc_index] = 0.4*preds1[:, large_lc_index] + 0.6*preds2[:, large_lc_index]\n                \n                #preds = 0.4*preds1 + 0.6*preds2\n                \n                for idx, pred in enumerate(preds):\n                    pred_df[f\"{row.row_id}_{(idx+1)*5}\"] = pred\n        \n        return pd.DataFrame(pred_df).T.rename(columns=id2label).reset_index().rename(columns={\"index\":\"row_id\"})","metadata":{"execution":{"iopub.status.busy":"2023-05-23T18:14:14.288264Z","iopub.execute_input":"2023-05-23T18:14:14.288728Z","iopub.status.idle":"2023-05-23T18:14:14.316292Z","shell.execute_reply.started":"2023-05-23T18:14:14.288675Z","shell.execute_reply":"2023-05-23T18:14:14.314707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def run(pdf):\n    split_size = 10\n    model1 = gemModel(CFG=CFG)\n    model1.load_state_dict(torch.load(CFG.weight_path1, map_location=torch.device('cpu')),strict=False)\n    model1.eval()\n    model1_opt = ipex.optimize(\n        model1,\n        sample_input=torch.load(pdf.path.values[0])['nf1024hop320'][:split_size,None,:,:],\n        auto_kernel_selection=True,\n        graph_mode=True\n    ).to(memory_format=torch.channels_last)\n\n    model2 = gemModel(CFG=CFG)\n    model2.load_state_dict(torch.load(CFG.weight_path2, map_location=torch.device('cpu')),strict=False)\n    model2.eval()\n    model2_opt = ipex.optimize(\n        model2,\n        sample_input=torch.load(pdf.path.values[0])['nf1024hop320'][:split_size,None,:,50:450],\n        auto_kernel_selection=True,\n        graph_mode=True\n    ).to(memory_format=torch.channels_last)\n    \n    model3 = gemModel(CFG=CFG)\n    model3.load_state_dict(torch.load(CFG.weight_path3, map_location=torch.device('cpu')),strict=False)\n    model3.eval()\n    model3_opt = ipex.optimize(\n        model3,\n        sample_input=torch.load(pdf.path.values[0])['nf2048hop512'][:split_size*2,None,:,:],\n        auto_kernel_selection=True,\n        graph_mode=True\n    ).to(memory_format=torch.channels_last)\n    \n    model4 = attModel(CFG=CFG)\n    model4.load_state_dict(torch.load(CFG.weight_path4, map_location=torch.device('cpu')),strict=False)\n    model4.eval()\n    model4_opt = ipex.optimize(\n        model4,\n        sample_input=torch.load(pdf.path.values[0])['nf2048hop512'][:split_size*2,None,:,:],\n        auto_kernel_selection=True,\n        graph_mode=True\n    ).to(memory_format=torch.channels_last)\n    \n    model5 = attModel(CFG=CFG)\n    model5.load_state_dict(torch.load(CFG.weight_path5, map_location=torch.device('cpu')),strict=False)\n    model5.eval()\n    model5_opt = ipex.optimize(\n        model5,\n        sample_input=torch.load(pdf.path.values[0])['nf2048hop512'][:split_size*2,None,:,:],\n        auto_kernel_selection=True,\n        graph_mode=True\n    ).to(memory_format=torch.channels_last)\n        \n    trainer = Trainer(\n        model1=model1_opt,\n        model2=model2_opt,\n        model3=model3_opt,\n        model4=model4_opt,\n        model5=model5_opt,\n        pdf = pdf,\n        split_size = split_size,\n    )\n    submission = trainer.test_one_cycle()\n    return submission","metadata":{"execution":{"iopub.status.busy":"2023-05-23T18:19:25.202198Z","iopub.execute_input":"2023-05-23T18:19:25.202664Z","iopub.status.idle":"2023-05-23T18:19:25.232334Z","shell.execute_reply.started":"2023-05-23T18:19:25.202627Z","shell.execute_reply":"2023-05-23T18:19:25.231167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = run(pdf)","metadata":{"execution":{"iopub.status.busy":"2023-05-23T18:19:25.339674Z","iopub.execute_input":"2023-05-23T18:19:25.340522Z","iopub.status.idle":"2023-05-23T18:20:24.630540Z","shell.execute_reply.started":"2023-05-23T18:19:25.340451Z","shell.execute_reply":"2023-05-23T18:20:24.629167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2023-05-23T18:20:32.760774Z","iopub.execute_input":"2023-05-23T18:20:32.762189Z","iopub.status.idle":"2023-05-23T18:20:32.805058Z","shell.execute_reply.started":"2023-05-23T18:20:32.762131Z","shell.execute_reply":"2023-05-23T18:20:32.803656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission[[id2label[idx] for idx in small_lc_index[:10]]]","metadata":{"execution":{"iopub.status.busy":"2023-05-23T18:20:36.171257Z","iopub.execute_input":"2023-05-23T18:20:36.172129Z","iopub.status.idle":"2023-05-23T18:20:36.203723Z","shell.execute_reply.started":"2023-05-23T18:20:36.172034Z","shell.execute_reply":"2023-05-23T18:20:36.202180Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission[[id2label[idx] for idx in large_lc_index[:10]]]","metadata":{"execution":{"iopub.status.busy":"2023-05-23T18:20:39.547181Z","iopub.execute_input":"2023-05-23T18:20:39.548580Z","iopub.status.idle":"2023-05-23T18:20:39.575813Z","shell.execute_reply.started":"2023-05-23T18:20:39.548530Z","shell.execute_reply":"2023-05-23T18:20:39.574378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv(\"submission.csv\",index=False)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# df = pd.read_csv(\"/kaggle/input/birdclef-2023/train_metadata.csv\")\n# ls_labels =  df[\"primary_label\"].value_counts().tail(10).index.values","metadata":{"execution":{"iopub.status.busy":"2023-05-23T18:15:24.033600Z","iopub.execute_input":"2023-05-23T18:15:24.034037Z","iopub.status.idle":"2023-05-23T18:15:24.041839Z","shell.execute_reply.started":"2023-05-23T18:15:24.033994Z","shell.execute_reply":"2023-05-23T18:15:24.040495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# submission[ls_labels] = 0\n# submission[ls_labels]","metadata":{"execution":{"iopub.status.busy":"2023-05-23T18:15:24.043555Z","iopub.execute_input":"2023-05-23T18:15:24.043912Z","iopub.status.idle":"2023-05-23T18:15:24.059200Z","shell.execute_reply.started":"2023-05-23T18:15:24.043877Z","shell.execute_reply":"2023-05-23T18:15:24.058025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# pred_cols = submission1.columns[1:]\n# submission = submission1.copy()\n# for pred_col in pred_cols:\n#     submission[pred_col] = 0.5*submission1[pred_col] + 0.5*submission2[pred_col]","metadata":{"execution":{"iopub.status.busy":"2023-05-23T18:15:24.061524Z","iopub.execute_input":"2023-05-23T18:15:24.062010Z","iopub.status.idle":"2023-05-23T18:15:24.071369Z","shell.execute_reply.started":"2023-05-23T18:15:24.061960Z","shell.execute_reply":"2023-05-23T18:15:24.070042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import time\n# device = \"cpu\"\n# model = attModel(CFG=CFG,path = None, pretrained=False)\n# model.load_state_dict(torch.load(CFG.weight_path3, map_location=torch.device('cpu')),strict=False)\n# model.eval()\n# xtest = torch.load(pdf.path.values[0])['nf1024hop320'][:,None,:,:]","metadata":{"execution":{"iopub.status.busy":"2023-05-23T18:15:24.073041Z","iopub.execute_input":"2023-05-23T18:15:24.073767Z","iopub.status.idle":"2023-05-23T18:15:24.083573Z","shell.execute_reply.started":"2023-05-23T18:15:24.073723Z","shell.execute_reply":"2023-05-23T18:15:24.081983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# with torch.no_grad():\n#     for i in range(5,12):\n#         xb = xtest[:i+1,:,:,:].to(memory_format=torch.channels_last)\n#         model_opt1 = ipex.optimize(\n#             model,\n#             sample_input=xb,\n#             auto_kernel_selection=True\n#         ).to(memory_format=torch.channels_last)\n#         t = []\n#         for _ in range(50):\n#             time_start = time.time()\n#             model_opt1(xb)\n#             time_end = time.time()\n#             t.append((time_end - time_start)/xb.shape[0])\n#         print(i)\n#         print(sum(t)/len(t))","metadata":{"execution":{"iopub.status.busy":"2023-05-23T18:15:24.084969Z","iopub.execute_input":"2023-05-23T18:15:24.085644Z","iopub.status.idle":"2023-05-23T18:15:24.095265Z","shell.execute_reply.started":"2023-05-23T18:15:24.085601Z","shell.execute_reply":"2023-05-23T18:15:24.094114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2023-05-23T18:15:24.097181Z","iopub.execute_input":"2023-05-23T18:15:24.098186Z","iopub.status.idle":"2023-05-23T18:15:24.159879Z","shell.execute_reply.started":"2023-05-23T18:15:24.098131Z","shell.execute_reply":"2023-05-23T18:15:24.158590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#submission[list(rrdf[rrdf > 10].index)].describe()","metadata":{"execution":{"iopub.status.busy":"2023-05-23T18:15:24.161827Z","iopub.execute_input":"2023-05-23T18:15:24.163215Z","iopub.status.idle":"2023-05-23T18:15:24.168834Z","shell.execute_reply.started":"2023-05-23T18:15:24.163160Z","shell.execute_reply":"2023-05-23T18:15:24.167507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## ","metadata":{}}]}