{
  "id": 166153,
  "title": "candence pretrainedmodels",
  "url": "/competitions/prostate-cancer-grade-assessment/discussion/166153",
  "author_name": "TheStoneMX",
  "post_date": "2020-07-12T02:00:45.071000",
  "votes": 0,
  "comment_count": 9,
  "views": 0,
  "content": "<p>Hi there all, I was wondering if someone can help me how to load my model from candence pretrainedmodels, I am still learning.\n~~~\nimport sys\nfrom pathlib import Path</p>\n\n<p>sys.path.append('../input/pytorch-pretrained-models/repository/pretrained-models.pytorch-master')\nimport pretrainedmodels</p>\n\n<p>m = pretrainedmodels.se_resnext34(pretrained='imagenet')\nchildren = list(m.children())\nhead = nn.Sequential(nn.AdaptiveAvgPool2d(1), Flatten(), \n                                  nn.Linear(children[-1].in_features,200))\nmodel = nn.Sequential(nn.Sequential(*children[:-2]), head)\n~~~\nused model from <a href=\"http://localhost:8888/notebooks/working_last/new_5-fold-prostate-cancer-grading-intro-starter-kernel.ipynb\">here</a></p>\n\n<p>Then tries to download but as we know we dont have internet, I have the model in the directory, but I dont know how to load it,</p>\n\n<p>thanks for the help.</p>",
  "messages": [
    {
      "id": 926182,
      "postDate": "2020-07-12T14:55:59.633Z",
      "content": "<p><a href=\"/oscarrangel\">@oscarrangel</a> If this is for inference, then you can set the pretrained argument to None since you will be loading your trained model weights later on. You only need to get the model structure from the pretrainedmodels library.</p>",
      "rawMarkdown": "@oscarrangel If this is for inference, then you can set the pretrained argument to None since you will be loading your trained model weights later on. You only need to get the model structure from the pretrainedmodels library.",
      "replies": [
        {
          "id": 926208,
          "postDate": "2020-07-12T15:19:39.080Z",
          "content": "<p><a href=\"/yovinyahathugoda\">@yovinyahathugoda</a> hey thanks!!!! yes it is for inference, I tried what you told me, did not click on me that we need to load the model first then the weights.... but still having a problem. </p>\n\n<p>~~~\nm = pretrainedmodels.se_resnext34(pretrained=None)\nm =  torch.load(model_file)</p>\n\n<h1>till here everything is ok but when I tried to execute the rest, it gives me an error.</h1>\n\n<p>children = list(m.children())\nhead = nn.Sequential(nn.AdaptiveAvgPool2d(1), \n                    Flatten(), \n                    nn.Linear(children[-1].in_features, 6))</p>\n\n<p>model = nn.Sequential(nn.Sequential(*children[:-2]), head) </p>\n\n<h2>###################################################</h2>\n\n<p>AttributeError                            Traceback (most recent call last)</p>",
          "rawMarkdown": "@yovinyahathugoda hey thanks!!!! yes it is for inference, I tried what you told me, did not click on me that we need to load the model first then the weights.... but still having a problem. \n\n~~~\nm = pretrainedmodels.se_resnext34(pretrained=None)\nm =  torch.load(model_file)\n\n# till here everything is ok but when I tried to execute the rest, it gives me an error. \n\nchildren = list(m.children())\nhead = nn.Sequential(nn.AdaptiveAvgPool2d(1), \n                    Flatten(), \n                    nn.Linear(children[-1].in_features, 6))\n\nmodel = nn.Sequential(nn.Sequential(*children[:-2]), head) \n\n###################################################\n---------------------------------------------------------------------------\nAttributeError                            Traceback (most recent call last)\n"
        },
        {
          "id": 926217,
          "postDate": "2020-07-12T15:26:44.050Z",
          "content": "<p><a href=\"/oscarrangel\">@oscarrangel</a> you need to load the trained model weight after creating the entire model. From what i understand from the above code you tried loading your trained model weights before calling <code>m.children()</code>. </p>\n\n<p>If you do this your keys in weights won't match. So set pretrained argument to None and then create your entire model before loading your trained model file.</p>",
          "rawMarkdown": "@oscarrangel you need to load the trained model weight after creating the entire model. From what i understand from the above code you tried loading your trained model weights before calling `m.children()`. \n\nIf you do this your keys in weights won't match. So set pretrained argument to None and then create your entire model before loading your trained model file.",
          "votes": 1
        },
        {
          "id": 926234,
          "postDate": "2020-07-12T15:35:53.700Z",
          "content": "<p><a href=\"/yovinyahathugoda\">@yovinyahathugoda</a>  ahhh I see, this is what I did and is not giving me an error.</p>\n\n<p>~~~\nm = pretrainedmodels.resnext34(pretrained=None)\nchildren = list(m.children())\nhead = nn.Sequential(nn.AdaptiveAvgPool2d(1),\nFlatten(),\nnn.Linear(children[-1].in_features, 6))</p>\n\n<p>model = nn.Sequential(nn.Sequential(*children[:-2]), head)\n~~~~</p>\n\n<p>But now do you know how to load the model file weights ?</p>\n\n<p>Thanks a lot for your help!</p>",
          "rawMarkdown": "@yovinyahathugoda  ahhh I see, this is what I did and is not giving me an error.\n\n~~~\nm = pretrainedmodels.resnext34(pretrained=None)\nchildren = list(m.children())\nhead = nn.Sequential(nn.AdaptiveAvgPool2d(1),\nFlatten(),\nnn.Linear(children[-1].in_features, 6))\n\nmodel = nn.Sequential(nn.Sequential(*children[:-2]), head)\n~~~~\n\nBut now do you know how to load the model file weights ?\n\nThanks a lot for your help!"
        },
        {
          "id": 926249,
          "postDate": "2020-07-12T15:43:01.103Z",
          "content": "<p><a href=\"/oscarrangel\">@oscarrangel</a> now you just load the model weights the normal way like below\n<code>model.load_state_dict(torch.load('model/model.pt'))</code></p>",
          "rawMarkdown": "@oscarrangel now you just load the model weights the normal way like below\n`model.load_state_dict(torch.load('model/model.pt'))`",
          "votes": 1
        },
        {
          "id": 926269,
          "postDate": "2020-07-12T15:55:45.507Z",
          "content": "<p><a href=\"/yovinyahathugoda\">@yovinyahathugoda</a> Thanks a lot for your help!!! learn few things today hehe. \nsend you blessings.</p>",
          "rawMarkdown": "@yovinyahathugoda Thanks a lot for your help!!! learn few things today hehe. \nsend you blessings."
        },
        {
          "id": 926272,
          "postDate": "2020-07-12T16:00:21.510Z",
          "content": "<p><a href=\"/oscarrangel\">@oscarrangel</a> You're welcome😊 </p>",
          "rawMarkdown": "@oscarrangel You're welcome😊 "
        }
      ]
    },
    {
      "id": 925681,
      "postDate": "2020-07-12T08:17:26.993Z",
      "content": "<p>upload data and add data to your notebook</p>",
      "rawMarkdown": "upload data and add data to your notebook",
      "replies": [
        {
          "id": 926191,
          "postDate": "2020-07-12T15:07:38.563Z",
          "content": "<p><a href=\"/poxiaodailaizhe\">@poxiaodailaizhe</a>  thanks for the replay, but that is not it.</p>",
          "rawMarkdown": "@poxiaodailaizhe  thanks for the replay, but that is not it."
        }
      ]
    },
    {
      "id": 925299,
      "postDate": "2020-07-12T02:00:45.070Z",
      "content": "<p>Hi there all, I was wondering if someone can help me how to load my model from candence pretrainedmodels, I am still learning.\n~~~\nimport sys\nfrom pathlib import Path</p>\n\n<p>sys.path.append('../input/pytorch-pretrained-models/repository/pretrained-models.pytorch-master')\nimport pretrainedmodels</p>\n\n<p>m = pretrainedmodels.se_resnext34(pretrained='imagenet')\nchildren = list(m.children())\nhead = nn.Sequential(nn.AdaptiveAvgPool2d(1), Flatten(), \n                                  nn.Linear(children[-1].in_features,200))\nmodel = nn.Sequential(nn.Sequential(*children[:-2]), head)\n~~~\nused model from <a href=\"http://localhost:8888/notebooks/working_last/new_5-fold-prostate-cancer-grading-intro-starter-kernel.ipynb\">here</a></p>\n\n<p>Then tries to download but as we know we dont have internet, I have the model in the directory, but I dont know how to load it,</p>\n\n<p>thanks for the help.</p>",
      "rawMarkdown": "Hi there all, I was wondering if someone can help me how to load my model from candence pretrainedmodels, I am still learning.\n~~~\nimport sys\nfrom pathlib import Path\n\nsys.path.append('../input/pytorch-pretrained-models/repository/pretrained-models.pytorch-master')\nimport pretrainedmodels\n\nm = pretrainedmodels.se_resnext34(pretrained='imagenet')\nchildren = list(m.children())\nhead = nn.Sequential(nn.AdaptiveAvgPool2d(1), Flatten(), \n                                  nn.Linear(children[-1].in_features,200))\nmodel = nn.Sequential(nn.Sequential(*children[:-2]), head)\n~~~\nused model from [here](http://localhost:8888/notebooks/working_last/new_5-fold-prostate-cancer-grading-intro-starter-kernel.ipynb)\n\nThen tries to download but as we know we dont have internet, I have the model in the directory, but I dont know how to load it,\n\nthanks for the help."
    }
  ],
  "comments": [
    {
      "id": 926182,
      "author_name": "Yovin Yahathugoda",
      "author_url": "",
      "post_date": "2020-07-12T14:55:59.633000",
      "content": "<p><a href=\"/oscarrangel\">@oscarrangel</a> If this is for inference, then you can set the pretrained argument to None since you will be loading your trained model weights later on. You only need to get the model structure from the pretrainedmodels library.</p>",
      "votes": 0,
      "replies": [
        {
          "id": 926208,
          "author_name": "TheStoneMX",
          "author_url": "",
          "post_date": "2020-07-12T15:19:39.080000",
          "content": "<p><a href=\"/yovinyahathugoda\">@yovinyahathugoda</a> hey thanks!!!! yes it is for inference, I tried what you told me, did not click on me that we need to load the model first then the weights.... but still having a problem. </p>\n\n<p>~~~\nm = pretrainedmodels.se_resnext34(pretrained=None)\nm =  torch.load(model_file)</p>\n\n<h1>till here everything is ok but when I tried to execute the rest, it gives me an error.</h1>\n\n<p>children = list(m.children())\nhead = nn.Sequential(nn.AdaptiveAvgPool2d(1), \n                    Flatten(), \n                    nn.Linear(children[-1].in_features, 6))</p>\n\n<p>model = nn.Sequential(nn.Sequential(*children[:-2]), head) </p>\n\n<h2>###################################################</h2>\n\n<p>AttributeError                            Traceback (most recent call last)</p>",
          "votes": 0,
          "replies": []
        },
        {
          "id": 926217,
          "author_name": "Yovin Yahathugoda",
          "author_url": "",
          "post_date": "2020-07-12T15:26:44.050000",
          "content": "<p><a href=\"/oscarrangel\">@oscarrangel</a> you need to load the trained model weight after creating the entire model. From what i understand from the above code you tried loading your trained model weights before calling <code>m.children()</code>. </p>\n\n<p>If you do this your keys in weights won't match. So set pretrained argument to None and then create your entire model before loading your trained model file.</p>",
          "votes": 1,
          "replies": []
        },
        {
          "id": 926234,
          "author_name": "TheStoneMX",
          "author_url": "",
          "post_date": "2020-07-12T15:35:53.700000",
          "content": "<p><a href=\"/yovinyahathugoda\">@yovinyahathugoda</a>  ahhh I see, this is what I did and is not giving me an error.</p>\n\n<p>~~~\nm = pretrainedmodels.resnext34(pretrained=None)\nchildren = list(m.children())\nhead = nn.Sequential(nn.AdaptiveAvgPool2d(1),\nFlatten(),\nnn.Linear(children[-1].in_features, 6))</p>\n\n<p>model = nn.Sequential(nn.Sequential(*children[:-2]), head)\n~~~~</p>\n\n<p>But now do you know how to load the model file weights ?</p>\n\n<p>Thanks a lot for your help!</p>",
          "votes": 0,
          "replies": []
        },
        {
          "id": 926249,
          "author_name": "Yovin Yahathugoda",
          "author_url": "",
          "post_date": "2020-07-12T15:43:01.103000",
          "content": "<p><a href=\"/oscarrangel\">@oscarrangel</a> now you just load the model weights the normal way like below\n<code>model.load_state_dict(torch.load('model/model.pt'))</code></p>",
          "votes": 1,
          "replies": []
        },
        {
          "id": 926269,
          "author_name": "TheStoneMX",
          "author_url": "",
          "post_date": "2020-07-12T15:55:45.507000",
          "content": "<p><a href=\"/yovinyahathugoda\">@yovinyahathugoda</a> Thanks a lot for your help!!! learn few things today hehe. \nsend you blessings.</p>",
          "votes": 0,
          "replies": []
        },
        {
          "id": 926272,
          "author_name": "Yovin Yahathugoda",
          "author_url": "",
          "post_date": "2020-07-12T16:00:21.510000",
          "content": "<p><a href=\"/oscarrangel\">@oscarrangel</a> You're welcome😊 </p>",
          "votes": 0,
          "replies": []
        }
      ]
    },
    {
      "id": 925681,
      "author_name": "",
      "author_url": "",
      "post_date": "2020-07-12T08:17:26.993000",
      "content": "<p>upload data and add data to your notebook</p>",
      "votes": 0,
      "replies": [
        {
          "id": 926191,
          "author_name": "TheStoneMX",
          "author_url": "",
          "post_date": "2020-07-12T15:07:38.563000",
          "content": "<p><a href=\"/poxiaodailaizhe\">@poxiaodailaizhe</a>  thanks for the replay, but that is not it.</p>",
          "votes": 0,
          "replies": []
        }
      ]
    }
  ],
  "raw_markdown_by_id": {
    "926182": "@oscarrangel If this is for inference, then you can set the pretrained argument to None since you will be loading your trained model weights later on. You only need to get the model structure from the pretrainedmodels library.",
    "925681": "upload data and add data to your notebook",
    "925299": "Hi there all, I was wondering if someone can help me how to load my model from candence pretrainedmodels, I am still learning.\n~~~\nimport sys\nfrom pathlib import Path\n\nsys.path.append('../input/pytorch-pretrained-models/repository/pretrained-models.pytorch-master')\nimport pretrainedmodels\n\nm = pretrainedmodels.se_resnext34(pretrained='imagenet')\nchildren = list(m.children())\nhead = nn.Sequential(nn.AdaptiveAvgPool2d(1), Flatten(), \n                                  nn.Linear(children[-1].in_features,200))\nmodel = nn.Sequential(nn.Sequential(*children[:-2]), head)\n~~~\nused model from [here](http://localhost:8888/notebooks/working_last/new_5-fold-prostate-cancer-grading-intro-starter-kernel.ipynb)\n\nThen tries to download but as we know we dont have internet, I have the model in the directory, but I dont know how to load it,\n\nthanks for the help."
  }
}