{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.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":70203,"databundleVersionId":8068726,"sourceType":"competition"},{"sourceId":4054846,"sourceType":"datasetVersion","datasetId":2401244},{"sourceId":5148212,"sourceType":"datasetVersion","datasetId":2991134},{"sourceId":8694391,"sourceType":"datasetVersion","datasetId":4799706},{"sourceId":121796742,"sourceType":"kernelVersion"},{"sourceId":121811216,"sourceType":"kernelVersion"}],"dockerImageVersionId":30407,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## This notebook was created by refferencing [this notebook](https://www.kaggle.com/code/honglihang/openvino-is-all-you-need)","metadata":{"execution":{"iopub.status.busy":"2024-05-30T19:50:55.824271Z","iopub.execute_input":"2024-05-30T19:50:55.824768Z","iopub.status.idle":"2024-05-30T19:51:26.153549Z","shell.execute_reply.started":"2024-05-30T19:50:55.824725Z","shell.execute_reply":"2024-05-30T19:51:26.152319Z"}}},{"cell_type":"code","source":"!pip install openvino-dev[onnx]\n!pip install nnAudio","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport torch\nimport os\nimport pytorch_lightning as pl\nfrom torch.utils.data import Dataset, DataLoader\nfrom sklearn import model_selection\nimport torchvision.transforms as transforms\nimport torchvision.io\nimport librosa\nfrom PIL import Image\nimport albumentations as alb\nimport torch.multiprocessing as mp\nimport warnings\n\n\nwarnings.filterwarnings('ignore')\nimport torch.nn as nn\nfrom torch.nn.functional import cross_entropy\nimport timm\nfrom pathlib import Path\nimport librosa as lb\nimport soundfile as sf\nfrom  soundfile import SoundFile \nimport torchaudio\n\nimport torch.nn as nn\nfrom torch.nn.functional import cross_entropy,binary_cross_entropy_with_logits\nimport torchvision\nimport time\nfrom torch.nn import functional as F\nfrom torch.distributions import Beta\nfrom torch.nn.parameter import Parameter\nfrom nnAudio.Spectrogram import CQT1992v2, CQT2010v2","metadata":{"execution":{"iopub.status.busy":"2024-05-30T19:51:41.062789Z","iopub.execute_input":"2024-05-30T19:51:41.063396Z","iopub.status.idle":"2024-05-30T19:52:01.330661Z","shell.execute_reply.started":"2024-05-30T19:51:41.063334Z","shell.execute_reply":"2024-05-30T19:52:01.327707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def init_layer(layer):\n    nn.init.xavier_uniform_(layer.weight)\n\n    if hasattr(layer, \"bias\"):\n        if layer.bias is not None:\n            layer.bias.data.fill_(0.)\n\n\ndef init_bn(bn):\n    bn.bias.data.fill_(0.)\n    bn.weight.data.fill_(1.0)\n\n\ndef init_weights(model):\n    classname = model.__class__.__name__\n    if classname.find(\"Conv2d\") != -1:\n        nn.init.xavier_uniform_(model.weight, gain=np.sqrt(2))\n        model.bias.data.fill_(0)\n    elif classname.find(\"BatchNorm\") != -1:\n        model.weight.data.normal_(1.0, 0.02)\n        model.bias.data.fill_(0)\n    elif classname.find(\"GRU\") != -1:\n        for weight in model.parameters():\n            if len(weight.size()) > 1:\n                nn.init.orghogonal_(weight.data)\n    elif classname.find(\"Linear\") != -1:\n        model.weight.data.normal_(0, 0.01)\n        model.bias.data.zero_()\n\n\ndef interpolate(x: torch.Tensor, ratio: int):\n    \"\"\"Interpolate data in time domain. This is used to compensate the\n    resolution reduction in downsampling of a CNN.\n    Args:\n      x: (batch_size, time_steps, classes_num)\n      ratio: int, ratio to interpolate\n    Returns:\n      upsampled: (batch_size, time_steps * ratio, classes_num)\n    \"\"\"\n    (batch_size, time_steps, classes_num) = x.shape\n    upsampled = x[:, :, None, :].repeat(1, 1, ratio, 1)\n    upsampled = upsampled.reshape(batch_size, time_steps * ratio, classes_num)\n    return upsampled\n\n\ndef pad_framewise_output(framewise_output: torch.Tensor, frames_num: int):\n    \"\"\"Pad framewise_output to the same length as input frames. The pad value\n    is the same as the value of the last frame.\n    Args:\n      framewise_output: (batch_size, frames_num, classes_num)\n      frames_num: int, number of frames to pad\n    Outputs:\n      output: (batch_size, frames_num, classes_num)\n    \"\"\"\n    output = F.interpolate(\n        framewise_output.unsqueeze(1),\n        size=(frames_num, framewise_output.size(2)),\n        align_corners=True,\n        mode=\"bilinear\").squeeze(1)\n\n    return output\n\n\ndef gem(x: torch.Tensor, p=3, eps=1e-6):\n    return F.avg_pool2d(x.clamp(min=eps).pow(p), (x.size(-2), x.size(-1))).pow(1. / p)\n\n\nclass GeM(nn.Module):\n    def __init__(self, p=3, eps=1e-6):\n        super().__init__()\n        self.p = nn.Parameter(torch.ones(1) * p)\n        self.eps = eps\n\n    def forward(self, x):\n        return gem(x, p=self.p, eps=self.eps)\n\n    def __repr__(self):\n        return self.__class__.__name__ + f\"(p={self.p.data.tolist()[0]:.4f}, eps={self.eps})\"\n\n\nclass AttBlockV2(nn.Module):\n    def __init__(self,\n                 in_features: int,\n                 out_features: int,\n                 activation=\"linear\"):\n        super().__init__()\n\n        self.activation = activation\n        self.att = nn.Conv1d(\n            in_channels=in_features,\n            out_channels=out_features,\n            kernel_size=1,\n            stride=1,\n            padding=0,\n            bias=True)\n        self.cla = nn.Conv1d(\n            in_channels=in_features,\n            out_channels=out_features,\n            kernel_size=1,\n            stride=1,\n            padding=0,\n            bias=True)\n\n        self.init_weights()\n\n    def init_weights(self):\n        init_layer(self.att)\n        init_layer(self.cla)\n\n    def forward(self, x):\n        # x: (n_samples, n_in, n_time)\n        norm_att = torch.softmax(torch.tanh(self.att(x)), dim=-1)\n        cla = self.nonlinear_transform(self.cla(x))\n        x = torch.sum(norm_att * cla, dim=2)\n        return x, norm_att, cla\n\n    def nonlinear_transform(self, x):\n        if self.activation == 'linear':\n            return x\n        elif self.activation == 'sigmoid':\n            return torch.sigmoid(x)\n\nclass Mixup(nn.Module):\n    def __init__(self, mix_beta):\n\n        super(Mixup, self).__init__()\n        self.beta_distribution = Beta(mix_beta, mix_beta)\n\n    def forward(self, X, Y, weight=None):\n\n        bs = X.shape[0]\n        n_dims = len(X.shape)\n        perm = torch.randperm(bs)\n        coeffs = self.beta_distribution.rsample(torch.Size((bs,))).to(X.device)\n\n        if n_dims == 2:\n            X = coeffs.view(-1, 1) * X + (1 - coeffs.view(-1, 1)) * X[perm]\n        elif n_dims == 3:\n            X = coeffs.view(-1, 1, 1) * X + (1 - coeffs.view(-1, 1, 1)) * X[perm]\n        else:\n            X = coeffs.view(-1, 1, 1, 1) * X + (1 - coeffs.view(-1, 1, 1, 1)) * X[perm]\n\n        Y = coeffs.view(-1, 1) * Y + (1 - coeffs.view(-1, 1)) * Y[perm]\n\n        if weight is None:\n            return X, Y\n        else:\n            weight = coeffs.view(-1) * weight + (1 - coeffs.view(-1)) * weight[perm]\n            return X, Y, weight\n\nclass BirdClefModelSED(torch.nn.Module):\n    def __init__(self, model_name, num_classes = 264, pretrained = False):\n        super().__init__()\n        self.num_classes = num_classes\n        self.bn0 = nn.BatchNorm2d(128)\n\n        base_model = timm.create_model(\n           model_name, pretrained=pretrained, in_chans=3)\n        #base_model.conv_stem.stride = (1,1)\n        layers = list(base_model.children())[:-2]\n        self.encoder = nn.Sequential(*layers)\n\n        if 'efficientnet' in model_name:\n            in_features = base_model.classifier.in_features\n        elif 'eca' in model_name:\n            in_features = base_model.head.fc.in_features \n        elif 'res' in model_name:\n            in_features = base_model.fc.in_features\n        #if hasattr(base_model, \"fc\"):\n        #    in_features = base_model.fc.in_features\n        #elif hasattr(base_model.head,'fc'):\n        #    in_features = base_model.head.fc.in_features\n        #else:\n        #    in_features = base_model.head.fc.in_features\n            #in_features = base_model.classifier.in_features\n\n\n\n        self.fc1 = nn.Linear(in_features, in_features, bias=True)\n        self.att_block = AttBlockV2(\n            in_features, num_classes, activation=\"sigmoid\")\n\n        self.init_weight()\n\n        #self.loss_function = nn.BCEWithLogitsLoss(reduction='none') \n        self.loss_function = nn.CrossEntropyLoss(label_smoothing=0.1,reduction='none')\n        self.mixup = Mixup(mix_beta=5)\n\n    def init_weight(self):\n        init_layer(self.fc1)\n        init_bn(self.bn0)\n\n    def forward(self,x,tta_delta=2):\n        center=True\n        train_duration=10\n        infer_duration = 5\n        #y = batch[1]\n        #weight = batch[2]\n        #x = transform_to_spec(x,train=self.training)\n\n        x = x.permute((0,1,3,2))\n        frames_num = x.shape[2]\n\n        x = x.transpose(1, 3)\n        x = self.bn0(x)\n        x = x.transpose(1, 3)\n\n        #if self.training:\n        #    x = self.spec_augmenter(x)\n\n        x = x.transpose(2, 3)\n        # (batch_size, channels, freq, frames)\n        x = self.encoder(x)\n\n        # (batch_size, channels, frames)\n        x = torch.mean(x, dim=2)\n\n        # channel smoothing\n        x1 = F.max_pool1d(x, kernel_size=3, stride=1, padding=1)\n        x2 = F.avg_pool1d(x, kernel_size=3, stride=1, padding=1)\n        x = x1 + x2\n\n        x = F.dropout(x, p=0.5, training=self.training)\n        x = x.transpose(1, 2)\n        x = F.relu_(self.fc1(x))\n        x = x.transpose(1, 2)\n        x = F.dropout(x, p=0.5, training=self.training)\n \n        time_att = torch.tanh(self.att_block.att(x))\n        #norm_att = torch.softmax(time_att, dim=-1)\n        #cla = self.att_block.nonlinear_transform(self.att_block.cla(x))\n        #clipwise_output_long = torch.sum(norm_att * cla, dim=2)\n        feat_time = x.size(-1)\n        if center:\n            start = (\n                feat_time / 2 - feat_time * (infer_duration / train_duration) / 2\n            )\n            end = start + feat_time * (infer_duration / train_duration)\n            start = int(start)\n            end = int(end)\n            clipwise_output = self.attention_infer(start,end,x,time_att)\n\n            #tta_delta = 2\n\n            start_minus = start-tta_delta\n            end_minus=end-tta_delta\n            clipwise_output_minus = self.attention_infer(start_minus,end_minus,x,time_att)\n\n            start_plus = start+tta_delta\n            end_plus=end+tta_delta\n            clipwise_output_plus = self.attention_infer(start_plus,end_plus,x,time_att)\n\n            clipwise_output = 0.5*clipwise_output + 0.25*clipwise_output_minus + 0.25*clipwise_output_plus\n        else:\n            start = 0\n            end = feat_time\n            feat = x[:, :, start:end]\n            att = torch.softmax(time_att[:, :, start:end], dim=-1)\n            #             print(feat_time, start, end)\n            #             print(att_a.sum(), att.sum(), time_att.shape)\n            framewise_pred = torch.sigmoid(self.att_block.cla(feat))\n            framewise_pred_max = framewise_pred.max(dim=2)[0]\n            clipwise_output = torch.sum(framewise_pred * att, dim=-1) \n        \n        return clipwise_output\n\n    def attention_infer(self,start,end,x,time_att):\n        feat = x[:, :, start:end]\n        att = torch.softmax(time_att[:, :, start:end], dim=-1)\n        #             print(feat_time, start, end)\n        #             print(att_a.sum(), att.sum(), time_att.shape)\n        framewise_pred = torch.sigmoid(self.att_block.cla(feat))\n        framewise_pred_max = framewise_pred.max(dim=2)[0]\n        #clipwise_output = torch.sum(framewise_pred * att, dim=-1) \n        #logits = torch.sum(\n        #    self.att_block.cla(feat) * att,\n        #    dim=-1,\n        #)\n\n        #return clipwise_output\n        return framewise_pred_max","metadata":{"execution":{"iopub.status.busy":"2024-05-30T19:52:01.336457Z","iopub.execute_input":"2024-05-30T19:52:01.336936Z","iopub.status.idle":"2024-05-30T19:52:01.417374Z","shell.execute_reply.started":"2024-05-30T19:52:01.336877Z","shell.execute_reply":"2024-05-30T19:52:01.415296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nclass Net(nn.Module):\n    def __init__(self, base_model_name: str, pretrained=False, num_classes=24, in_channels=1):\n        super().__init__()\n        self.qtransform = CQT1992v2(sr=32000, fmin=256, n_bins=160, hop_length=250, output_format='Magnitude',\n                                    norm=1, window='tukey',bins_per_octave=27, verbose=False)\n        #self.resize = TT.Resize([CFG.img_size[0], CFG.img_size[1]])\n        self.spec_augmenter = SpecAugmentation(time_drop_width=64//2, time_stripes_num=2,\n                                               freq_drop_width=8//2, freq_stripes_num=2)\n\n        base_model = timm.create_model(\n            base_model_name, pretrained=pretrained, in_chans=in_channels)\n        #layers = list(base_model.children())[:-2]\n        self.encoder = base_model#nn.Sequential(*layers)\n        #print(base_model)\n        #print(layers)\n        if hasattr(base_model, \"fc\"):\n            in_features = base_model.fc.in_features\n        elif hasattr(base_model, \"classifier\"):\n            in_features = base_model.classifier.in_features\n            self.encoder.global_pool = nn.Identity()\n            self.encoder.classifier = nn.Identity()\n        else:\n            in_features = base_model.head.fc.in_features\n        self.pooling = GeM()\n        self.fc = nn.Linear(in_features, num_classes)\n        #self.init_weight()\n\n    def init_weight(self):\n        init_bn(self.bn0)\n        init_layer(self.fc1)\n        \n\n    def forward(self, input_data):\n        x = input_data # (batch_size, 3, time_steps, mel_bins)\n        x = self.qtransform(x)\n        #x = self.resize(x)\n        x = x.unsqueeze(1)\n        \n        batch_size = x.size(0)\n        # frames_num = x.shape[2]\n\n        # x = x.transpose(1, 3)\n        # x = self.bn0(x)\n        # x = x.transpose(1, 3)\n\n        if self.training:\n            if random.random() < 0.25:\n                x = self.spec_augmenter(x)\n\n        #x = x.transpose(2, 3)\n\n        x = self.encoder(x)\n        x = self.pooling(x)\n        x = x.view(batch_size, -1)\n        x = self.fc(x)\n        \n        return x\n    \ndef init_layer(layer):\n    nn.init.xavier_uniform_(layer.weight)\n\n    if hasattr(layer, \"bias\"):\n        if layer.bias is not None:\n            layer.bias.data.fill_(0.)\n\n\ndef init_bn(bn):\n    bn.bias.data.fill_(0.)\n    bn.weight.data.fill_(1.0)\n\n\ndef init_weights(model):\n    classname = model.__class__.__name__\n    if classname.find(\"Conv2d\") != -1:\n        nn.init.xavier_uniform_(model.weight, gain=np.sqrt(2))\n        model.bias.data.fill_(0)\n    elif classname.find(\"BatchNorm\") != -1:\n        model.weight.data.normal_(1.0, 0.02)\n        model.bias.data.fill_(0)\n    elif classname.find(\"GRU\") != -1:\n        for weight in model.parameters():\n            if len(weight.size()) > 1:\n                nn.init.orghogonal_(weight.data)\n    elif classname.find(\"Linear\") != -1:\n        model.weight.data.normal_(0, 0.01)\n        model.bias.data.zero_()\n\n\ndef interpolate(x: torch.Tensor, ratio: int):\n    \"\"\"Interpolate data in time domain. This is used to compensate the\n    resolution reduction in downsampling of a CNN.\n    Args:\n      x: (batch_size, time_steps, classes_num)\n      ratio: int, ratio to interpolate\n    Returns:\n      upsampled: (batch_size, time_steps * ratio, classes_num)\n    \"\"\"\n    (batch_size, time_steps, classes_num) = x.shape\n    upsampled = x[:, :, None, :].repeat(1, 1, ratio, 1)\n    upsampled = upsampled.reshape(batch_size, time_steps * ratio, classes_num)\n    return upsampled\n\n\ndef pad_framewise_output(framewise_output: torch.Tensor, frames_num: int):\n    \"\"\"Pad framewise_output to the same length as input frames. The pad value\n    is the same as the value of the last frame.\n    Args:\n      framewise_output: (batch_size, frames_num, classes_num)\n      frames_num: int, number of frames to pad\n    Outputs:\n      output: (batch_size, frames_num, classes_num)\n    \"\"\"\n    output = F.interpolate(\n        framewise_output.unsqueeze(1),\n        size=(frames_num, framewise_output.size(2)),\n        align_corners=True,\n        mode=\"bilinear\").squeeze(1)\n\n    return output\n\n\nclass AttBlockV2(nn.Module):\n    def __init__(self,\n                 in_features: int,\n                 out_features: int,\n                 activation=\"linear\"):\n        super().__init__()\n\n        self.activation = activation\n        self.att = nn.Conv1d(\n            in_channels=in_features,\n            out_channels=out_features,\n            kernel_size=1,\n            stride=1,\n            padding=0,\n            bias=True)\n        self.cla = nn.Conv1d(\n            in_channels=in_features,\n            out_channels=out_features,\n            kernel_size=1,\n            stride=1,\n            padding=0,\n            bias=True)\n\n        self.init_weights()\n\n    def init_weights(self):\n        init_layer(self.att)\n        init_layer(self.cla)\n\n    def forward(self, x):\n        # x: (n_samples, n_in, n_time)\n        norm_att = torch.softmax(torch.tanh(self.att(x)), dim=-1)\n        cla = self.nonlinear_transform(self.cla(x))\n        x = torch.sum(norm_att * cla, dim=2)\n        return x, norm_att, cla\n\n    def nonlinear_transform(self, x):\n        if self.activation == 'linear':\n            return x\n        elif self.activation == 'sigmoid':\n            return torch.sigmoid(x)\n\n    \nclass TimmSED(nn.Module):\n    def __init__(self, base_model_name: str, pretrained=False, num_classes=24, in_channels=1):\n        super().__init__()\n        \n        self.qtransform = CQT1992v2(sr=32000, fmin=256, n_bins=160, hop_length=250, output_format='Magnitude',\n                                    norm=1, window='tukey',bins_per_octave=27, verbose=False)\n\n        self.spec_augmenter = SpecAugmentation(time_drop_width=64//2, time_stripes_num=2,\n                                               freq_drop_width=8//2, freq_stripes_num=2)\n\n        self.bn0 = nn.BatchNorm2d(320) # 641 CFG.n_mels\n        self.resize = TT.Resize([CFG.img_size[0], CFG.img_size[1]])\n        base_model = timm.create_model(\n            base_model_name, pretrained=pretrained, in_chans=in_channels)\n        #layers = list(base_model.children())[:-2]\n        self.encoder = base_model#nn.Sequential(*layers)\n        #print(base_model)\n        #print(layers)\n        if hasattr(base_model, \"fc\"):\n            in_features = base_model.fc.in_features\n        elif hasattr(base_model, \"classifier\"):\n            in_features = base_model.classifier.in_features\n            self.encoder.global_pool = nn.Identity()\n            self.encoder.classifier = nn.Identity()\n        else:\n            in_features = base_model.head.fc.in_features\n\n        self.fc1 = nn.Linear(in_features, in_features, bias=True)\n        self.att_block = AttBlockV2(\n            in_features, num_classes, activation=\"sigmoid\")\n\n        self.init_weight()\n\n    def init_weight(self):\n        init_bn(self.bn0)\n        init_layer(self.fc1)\n        \n\n    def forward(self, input_data):\n        x = input_data # (batch_size, 3, time_steps, mel_bins)\n        x = self.qtransform(x)\n        x = (64*x + 1).log()\n        x = self.resize(x)\n        x = x.unsqueeze(1)\n        frames_num = x.shape[2]\n\n        x = x.transpose(1, 3)\n        x = self.bn0(x)\n        x = x.transpose(1, 3)\n\n        if self.training:\n            if random.random() < 0.25:\n                x = self.spec_augmenter(x)\n\n        x = x.transpose(2, 3)\n\n        x = self.encoder(x)\n        # Aggregate in frequency axis\n        x = torch.mean(x, dim=3)\n\n        x1 = F.max_pool1d(x, kernel_size=3, stride=1, padding=1)\n        x2 = F.avg_pool1d(x, kernel_size=3, stride=1, padding=1)\n        x = x1 + x2\n\n        x = F.dropout(x, p=0.5, training=self.training)\n        x = x.transpose(1, 2)\n        x = F.relu_(self.fc1(x))\n        x = x.transpose(1, 2)\n        x = F.dropout(x, p=0.5, training=self.training)\n\n        (clipwise_output, norm_att, segmentwise_output) = self.att_block(x)\n        logit = torch.sum(norm_att * self.att_block.cla(x), dim=2)\n        segmentwise_logit = self.att_block.cla(x).transpose(1, 2)\n        segmentwise_output = segmentwise_output.transpose(1, 2)\n\n        interpolate_ratio = frames_num // segmentwise_output.size(1)\n\n        # Get framewise output\n        framewise_output = interpolate(segmentwise_output,\n                                       interpolate_ratio)\n        framewise_output = pad_framewise_output(framewise_output, frames_num)\n\n        framewise_logit = interpolate(segmentwise_logit, interpolate_ratio)\n        framewise_logit = pad_framewise_output(framewise_logit, frames_num)\n\n        output_dict = {\n            'framewise_output': framewise_output,\n            'clipwise_output': clipwise_output,\n            'logit': logit,\n            'framewise_logit': framewise_logit,\n        }\n\n        return output_dict","metadata":{"execution":{"iopub.status.busy":"2024-05-30T19:52:01.420499Z","iopub.execute_input":"2024-05-30T19:52:01.421058Z","iopub.status.idle":"2024-05-30T19:52:01.504288Z","shell.execute_reply.started":"2024-05-30T19:52:01.421000Z","shell.execute_reply":"2024-05-30T19:52:01.501484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class GeM(nn.Module):\n    def __init__(self, p=3, eps=1e-6):\n        super(GeM, self).__init__()\n        self.p = nn.Parameter(torch.ones(1) * p)\n        self.eps = eps\n\n    def forward(self, x):\n        return self.gem(x)\n\n    def gem(self, x):\n        p = self.p.item()\n        return F.avg_pool2d(x.clamp(min=self.eps).pow(p), (x.size(-2), x.size(-1))).pow(1. / p)\n\n    def __repr__(self):\n        return self.__class__.__name__ + \\\n               '(' + 'p=' + '{:.4f}'.format(self.p.data.tolist()[0]) + \\\n               ', ' + 'eps=' + str(self.eps) + ')'\n    \nclass CustomModel(nn.Module):\n    def __init__(self, model_name, in_chans=3, reshape_factor=128, drop_rate=0.1, drop_path_rate=0.1,\n                 num_classes: int = 6, pretrained: bool = True):\n        super(CustomModel, self).__init__()\n        self.backbone = timm.create_model(\n            model_name,\n            in_chans=in_chans,\n            pretrained=pretrained,\n            drop_rate = drop_rate,\n            drop_path_rate = drop_path_rate,\n        )\n        self.reshape_factor = reshape_factor\n\n        if 'efficient' in model_name:\n            self.backbone.global_pool = nn.Identity()\n            self.backbone.classifier = nn.Identity()\n            self.fc = nn.Sequential(\n                nn.AdaptiveAvgPool2d(1),\n                #GeM(),\n                nn.Flatten(),\n                nn.Linear(self.backbone.num_features, num_classes)\n            )\n\n        elif \"convnext\" in model_name:\n            self.backbone.head.fc = nn.Identity()\n            self.fc = nn.Linear(self.backbone.num_features, num_classes)\n\n        elif \"nfnet\" in model_name:\n            self.backbone.head = nn.Identity()\n            self.fc = nn.Sequential(\n                nn.AdaptiveAvgPool2d(1),\n                nn.Flatten(),\n                nn.Dropout(drop_rate),\n                nn.Linear(self.backbone.num_features, num_classes)\n            )\n        elif model_name == \"tiny_vit_21m_512\":\n            self.backbone.head.fc = nn.Identity()\n            self.fc = nn.Linear(self.backbone.num_features, num_classes)\n\n        elif \"maxvit_\" in model_name:\n            self.backbone.head.fc = nn.Identity()\n            self.fc = nn.Linear(self.backbone.num_features, num_classes)\n\n        elif \"vit_\" in model_name:\n            self.backbone.head = nn.Identity()\n            self.fc = nn.Linear(self.backbone.num_features, num_classes)\n\n    def __reshape(self, x):\n        bs, d = x.shape\n        reshaped_tensor = x.view(bs, d//self.reshape_factor, self.reshape_factor)\n        x = torch.unsqueeze(reshaped_tensor, dim=1)\n        x = torch.cat([x, x, x], dim=1)\n        return x\n\n    def forward(self, x):\n        x = self.__reshape(x)\n        x = self.backbone(x) # (bs, ch, feat_size, feat_size)\n        x = self.fc(x) # (bs, 6)\n        return x","metadata":{"execution":{"iopub.status.busy":"2024-05-30T19:52:01.506999Z","iopub.execute_input":"2024-05-30T19:52:01.507650Z","iopub.status.idle":"2024-05-30T19:52:01.541657Z","shell.execute_reply.started":"2024-05-30T19:52:01.507588Z","shell.execute_reply":"2024-05-30T19:52:01.540506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_paths = ['/kaggle/input/birdclef2024-model/EXP034_reshape_effnetb0_5sec_removedupl_gsk5folds_ds2_unlabelnoise_cleandata/best_fold-0.bin',\n               ]\nmodels = []\n","metadata":{"execution":{"iopub.status.busy":"2024-05-30T19:52:01.543607Z","iopub.execute_input":"2024-05-30T19:52:01.545140Z","iopub.status.idle":"2024-05-30T19:52:01.561354Z","shell.execute_reply.started":"2024-05-30T19:52:01.545055Z","shell.execute_reply":"2024-05-30T19:52:01.559637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 48\ninput_sed = torch.randn(batch_size, 80000)","metadata":{"execution":{"iopub.status.busy":"2024-05-30T19:52:01.563762Z","iopub.execute_input":"2024-05-30T19:52:01.565030Z","iopub.status.idle":"2024-05-30T19:52:01.630367Z","shell.execute_reply.started":"2024-05-30T19:52:01.564971Z","shell.execute_reply":"2024-05-30T19:52:01.628402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# pytorch infer","metadata":{}},{"cell_type":"code","source":"# loop = 10\n# # pytorch\n# times = []\n# for i in range(loop):\n#     t1=time.time()\n#     with torch.no_grad():\n#         model(input_sed)\n#     t2=time.time()\n#     times.append(t2-t1)\n# np.mean(times)","metadata":{"execution":{"iopub.status.busy":"2024-05-30T19:52:01.645617Z","iopub.execute_input":"2024-05-30T19:52:01.647065Z","iopub.status.idle":"2024-05-30T19:52:01.654560Z","shell.execute_reply.started":"2024-05-30T19:52:01.646987Z","shell.execute_reply":"2024-05-30T19:52:01.652660Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# openvino(fp32)","metadata":{}},{"cell_type":"code","source":"import subprocess\n\ninput_names = [\"x\"]\noutput_names = [\"x\"]\n#traced_model = torch.jit.trace(model, input_sed)\nabs_path = '/kaggle/working/'\nfor model_path in model_paths:\n    model = CustomModel(model_name='tf_efficientnet_b0_ns',\n                        pretrained=False,\n                        num_classes=182,\n                        in_chans=3,\n                        reshape_factor=200\n                        )\n    model.load_state_dict(torch.load(model_path, map_location=torch.device('cpu')))\n    model.eval()\n    folder, base = model_path.split('/')[-2:]\n    base = base.split('.')[0]\n    os.makedirs(folder, exist_ok=True)\n    path = abs_path + folder\n    print(path, base)\n    torch.onnx.export(model, input_sed, f\"{path}/{base}.onnx\", verbose=False,\n                      input_names=input_names, output_names=output_names,\n                      dynamic_axes={input_names[0]: {0 : 'batch_size'}, output_names[0]: {0 : 'batch_size'}}\n                     )\n    os.chdir(path)\n    command = f'mo --input_model {base}.onnx'\n    ret = subprocess.run(command, shell=True, capture_output=True, text=True)\n    os.chdir('../')","metadata":{"execution":{"iopub.status.busy":"2024-05-30T19:52:01.656752Z","iopub.execute_input":"2024-05-30T19:52:01.657340Z","iopub.status.idle":"2024-05-30T19:52:40.469284Z","shell.execute_reply.started":"2024-05-30T19:52:01.657273Z","shell.execute_reply":"2024-05-30T19:52:40.467107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.chdir('../')","metadata":{"execution":{"iopub.status.busy":"2024-05-30T19:52:40.472091Z","iopub.execute_input":"2024-05-30T19:52:40.472697Z","iopub.status.idle":"2024-05-30T19:52:40.480212Z","shell.execute_reply.started":"2024-05-30T19:52:40.472640Z","shell.execute_reply":"2024-05-30T19:52:40.478936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!dir","metadata":{"execution":{"iopub.status.busy":"2024-05-30T19:52:40.482081Z","iopub.execute_input":"2024-05-30T19:52:40.482950Z","iopub.status.idle":"2024-05-30T19:52:41.670360Z","shell.execute_reply.started":"2024-05-30T19:52:40.482900Z","shell.execute_reply":"2024-05-30T19:52:41.668519Z"},"trusted":true},"execution_count":null,"outputs":[]}]}