{"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":"markdown","source":"Here is the refernece link: https://discuss.pytorch.org/t/custom-ensemble-approach/52024/4?u=boom_bell","metadata":{}},{"cell_type":"code","source":"import torchvision.models as models\nimport torch.nn as nn\nclass MyEnsemble(nn.Module):\n    def __init__(self, modelA, modelB, nb_classes=8):\n        super(MyEnsemble, self).__init__()\n        self.modelA = modelA\n        self.modelB = modelB\n        # Remove last linear layer\n        self.modelA.classifier = nn.Identity()\n        self.modelB.classifier = nn.Identity()\n        # Create new classifier\n        self.classifier = nn.Linear(1280+1792, nb_classes)\n    def forward(self, x):\n        x1 = self.modelA(x.clone())  # clone to make sure x is not changed by inplace methods\n        print(x)\n        x1 = x1.view(x1.size(0), -1)\n        print(x1.shape)\n        x2 = self.modelB(x)\n        x2 = x2.view(x2.size(0), -1)\n        print(x2.shape)\n        x = torch.cat((x1, x2), dim=1)\n        x = self.classifier(F.relu(x))\n        return x","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-10-21T14:08:11.414715Z","iopub.execute_input":"2022-10-21T14:08:11.415284Z","iopub.status.idle":"2022-10-21T14:08:11.428180Z","shell.execute_reply.started":"2022-10-21T14:08:11.415229Z","shell.execute_reply":"2022-10-21T14:08:11.426607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"modelA = models.efficientnet_b0(pretrained=True)\nmodelB = models.efficientnet_b4(pretrained=True)","metadata":{"execution":{"iopub.status.busy":"2022-10-21T14:08:11.430692Z","iopub.execute_input":"2022-10-21T14:08:11.431572Z","iopub.status.idle":"2022-10-21T14:08:12.499492Z","shell.execute_reply.started":"2022-10-21T14:08:11.431531Z","shell.execute_reply":"2022-10-21T14:08:12.498309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# modelA","metadata":{"execution":{"iopub.status.busy":"2022-10-21T14:08:12.502748Z","iopub.execute_input":"2022-10-21T14:08:12.504199Z","iopub.status.idle":"2022-10-21T14:08:12.510297Z","shell.execute_reply.started":"2022-10-21T14:08:12.504135Z","shell.execute_reply":"2022-10-21T14:08:12.509198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# modelB","metadata":{"execution":{"iopub.status.busy":"2022-10-21T14:08:12.512086Z","iopub.execute_input":"2022-10-21T14:08:12.513112Z","iopub.status.idle":"2022-10-21T14:08:12.523358Z","shell.execute_reply.started":"2022-10-21T14:08:12.513053Z","shell.execute_reply":"2022-10-21T14:08:12.521994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Freeze these models\nfor param in modelA.parameters():\n    param.requires_grad_(False)\n\nfor param in modelB.parameters():\n    param.requires_grad_(False)","metadata":{"execution":{"iopub.status.busy":"2022-10-21T14:08:12.526902Z","iopub.execute_input":"2022-10-21T14:08:12.527711Z","iopub.status.idle":"2022-10-21T14:08:12.542556Z","shell.execute_reply.started":"2022-10-21T14:08:12.527449Z","shell.execute_reply":"2022-10-21T14:08:12.541294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create ensemble model\nmodel = MyEnsemble(modelA, modelB)","metadata":{"execution":{"iopub.status.busy":"2022-10-21T14:08:12.544591Z","iopub.execute_input":"2022-10-21T14:08:12.545090Z","iopub.status.idle":"2022-10-21T14:08:12.552629Z","shell.execute_reply.started":"2022-10-21T14:08:12.545048Z","shell.execute_reply":"2022-10-21T14:08:12.551309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model","metadata":{"execution":{"iopub.status.busy":"2022-10-21T14:08:12.554478Z","iopub.execute_input":"2022-10-21T14:08:12.555700Z","iopub.status.idle":"2022-10-21T14:08:12.563561Z","shell.execute_reply.started":"2022-10-21T14:08:12.555658Z","shell.execute_reply":"2022-10-21T14:08:12.562405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn.functional as F\nx = torch.randn(1, 3, 224, 224)\noutput = model(x)","metadata":{"execution":{"iopub.status.busy":"2022-10-21T14:08:12.565330Z","iopub.execute_input":"2022-10-21T14:08:12.565743Z","iopub.status.idle":"2022-10-21T14:08:12.786913Z","shell.execute_reply.started":"2022-10-21T14:08:12.565708Z","shell.execute_reply":"2022-10-21T14:08:12.785621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}