{
  "id": 206521,
  "title": "How to use the pretrained model  when submitting?(eg:ResNet50 from pytorch)",
  "url": "/competitions/cassava-leaf-disease-classification/discussion/206521",
  "author_name": "qiuye",
  "post_date": "2020-12-25T03:29:57.133000",
  "votes": 2,
  "comment_count": 22,
  "views": 0,
  "content": "<p>In the notebook，by setting pretrained model ：<br>\n     model = torchvision.models.resnet18(pretrained=True)<br>\nIt works.<br>\nBut when I want to submit my code,it requests me not use the internet,so I can't download model from pytorch.How to solve this problem?</p>",
  "messages": [
    {
      "id": 1125774,
      "postDate": "2020-12-25T03:50:13.237Z",
      "content": "<p>when you train the model, save the weight in the kaggle dataset. Then, submit your code with saved weight. (you upload the weight in the kaggle data tab and then add the dataset in the notebook) </p>\n<p><img src=\"https://drive.google.com/uc?export=view&amp;id=1C2a8GkUCyLZO67Shz58sHw0AAm51OZWk\" alt=\"\"></p>",
      "rawMarkdown": "when you train the model, save the weight in the kaggle dataset. Then, submit your code with saved weight. (you upload the weight in the kaggle data tab and then add the dataset in the notebook) \n\n![](https://drive.google.com/uc?export=view&id=1C2a8GkUCyLZO67Shz58sHw0AAm51OZWk)",
      "votes": 1,
      "replies": [
        {
          "id": 1125889,
          "postDate": "2020-12-25T06:45:30.117Z",
          "content": "<p>In addition, <br>\n<code>model = torchvision.models.resnet18(pretrained=False)</code><br>\nYou need to set the pretrained argument <strong>False</strong>, otherwise it will try to download the pre-trained weight which is not needed for inference.</p>",
          "rawMarkdown": "In addition, \n`model = torchvision.models.resnet18(pretrained=False)`\nYou need to set the pretrained argument **False**, otherwise it will try to download the pre-trained weight which is not needed for inference.",
          "votes": 1
        },
        {
          "id": 1126023,
          "postDate": "2020-12-25T09:09:29.137Z",
          "content": "<p>Thanks a lot！</p>",
          "rawMarkdown": "Thanks a lot！"
        },
        {
          "id": 1126025,
          "postDate": "2020-12-25T09:10:06.263Z",
          "content": "<p>Nice idea！</p>",
          "rawMarkdown": "Nice idea！"
        },
        {
          "id": 1126346,
          "postDate": "2020-12-25T14:36:03.323Z",
          "content": "<p>I found another question…<br>\nCan I ask what's your model-saved path?<br>\nWhen I save my model by using './model.pth',it was found at /kaggle/working (dir).But if want to upload my model data,it must be in notebook (dir).</p>",
          "rawMarkdown": "I found another question...\nCan I ask what's your model-saved path?\nWhen I save my model by using './model.pth',it was found at /kaggle/working (dir).But if want to upload my model data,it must be in notebook (dir)."
        },
        {
          "id": 1126537,
          "postDate": "2020-12-25T17:21:32.607Z",
          "content": "<p>The competition dataset and whatever you add in the notebook will be in the input directory, and the files you create during your notebook session will be under output directory. That's how kaggle handles/manages files.</p>",
          "rawMarkdown": "The competition dataset and whatever you add in the notebook will be in the input directory, and the files you create during your notebook session will be under output directory. That's how kaggle handles/manages files.\n"
        },
        {
          "id": 1126857,
          "postDate": "2020-12-26T03:39:49.710Z",
          "content": "<p>It likes this.<br>\n<img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F3197411%2F4405438349df0ddac5a51f908c6b0ac2%2F.png?generation=1608954020820419&amp;alt=media\" alt=\"\"><br>\n<img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F3197411%2F2bfdddf0d5b4599596a58f3590bf1be2%2F1.png?generation=1608953972401427&amp;alt=media\" alt=\"\"></p>",
          "rawMarkdown": "It likes this.\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F3197411%2F4405438349df0ddac5a51f908c6b0ac2%2F.png?generation=1608954020820419&alt=media)\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F3197411%2F2bfdddf0d5b4599596a58f3590bf1be2%2F1.png?generation=1608953972401427&alt=media)\n\n"
        },
        {
          "id": 1126864,
          "postDate": "2020-12-26T03:54:33.333Z",
          "content": "<p>Can I see your save code and output file in the training notebook ? above picture(output) is the training notebook results?? </p>\n<p>I usually save the weight of the model in the local and upload it on the Data tab. (i used local gpu) </p>",
          "rawMarkdown": "Can I see your save code and output file in the training notebook ? above picture(output) is the training notebook results?? \n\nI usually save the weight of the model in the local and upload it on the Data tab. (i used local gpu) "
        },
        {
          "id": 1126882,
          "postDate": "2020-12-26T04:28:34.937Z",
          "content": "<p>url:<a href=\"https://www.kaggle.com/qiujunfu/notebook-train\" target=\"_blank\">https://www.kaggle.com/qiujunfu/notebook-train</a>.</p>",
          "rawMarkdown": "url:https://www.kaggle.com/qiujunfu/notebook-train."
        },
        {
          "id": 1126935,
          "postDate": "2020-12-26T05:25:27.767Z",
          "content": "<p>I think it's a version problem!! There was no problem when I turned it around and checked it out there was no problem. </p>\n<p><img src=\"https://drive.google.com/uc?export=view&amp;id=1MCV3J6dGnX_KVW-4Q0T7pfJqmnv7V8eV\" alt=\"\"></p>",
          "rawMarkdown": "I think it's a version problem!! There was no problem when I turned it around and checked it out there was no problem. \n\n![](https://drive.google.com/uc?export=view&id=1MCV3J6dGnX_KVW-4Q0T7pfJqmnv7V8eV)"
        },
        {
          "id": 1127236,
          "postDate": "2020-12-26T11:06:39.517Z",
          "content": "<p>I hava not solved the problem till now.In fact,the 'model.pth' can be found in notebook,however ,it can't be used to upload dataset(emmm….maybe it's a bug.)<br>\nSo at last, I download the 'model.pth' to my computer and then upload it to the dataset,this is a feasible solution.<br>\nThank you so much for your kindness! </p>",
          "rawMarkdown": "I hava not solved the problem till now.In fact,the 'model.pth' can be found in notebook,however ,it can't be used to upload dataset(emmm....maybe it's a bug.)\nSo at last, I download the 'model.pth' to my computer and then upload it to the dataset,this is a feasible solution.\nThank you so much for your kindness! ",
          "votes": 1
        }
      ]
    },
    {
      "id": 1125767,
      "postDate": "2020-12-25T03:29:57.133Z",
      "content": "<p>In the notebook，by setting pretrained model ：<br>\n     model = torchvision.models.resnet18(pretrained=True)<br>\nIt works.<br>\nBut when I want to submit my code,it requests me not use the internet,so I can't download model from pytorch.How to solve this problem?</p>",
      "rawMarkdown": "In the notebook，by setting pretrained model ：\n     model = torchvision.models.resnet18(pretrained=True)\nIt works.\nBut when I want to submit my code,it requests me not use the internet,so I can't download model from pytorch.How to solve this problem?",
      "votes": 2
    },
    {
      "id": 1153862,
      "postDate": "2021-01-15T07:52:15.950Z",
      "content": "<p>I have saved my model like this:<br>\nPATH = './model_18_20.pth'<br>\ntorch.save(model.state_dict(), PATH)</p>\n<p>but when I am running my inference notebook while keeping the internet off and saving the notebook for submission, No output file is showing during submission. But when the internet is on output file is visible, but we cannot submit it because the internet should be off. Can someone help?</p>",
      "rawMarkdown": "I have saved my model like this:\nPATH = './model_18_20.pth'\ntorch.save(model.state_dict(), PATH)\n\nbut when I am running my inference notebook while keeping the internet off and saving the notebook for submission, No output file is showing during submission. But when the internet is on output file is visible, but we cannot submit it because the internet should be off. Can someone help?",
      "replies": [
        {
          "id": 1153884,
          "postDate": "2021-01-15T08:08:56.883Z",
          "content": "<p>Can you go through your notebook cell outputs and notebook run logs?<br>\nDo you see any error?</p>",
          "rawMarkdown": "Can you go through your notebook cell outputs and notebook run logs?\nDo you see any error?"
        },
        {
          "id": 1153907,
          "postDate": "2021-01-15T08:31:52.473Z",
          "content": "<p>There is no error every cell is running fine! As I said when the internet is ON, I am able to see output file during submission.</p>",
          "rawMarkdown": "There is no error every cell is running fine! As I said when the internet is ON, I am able to see output file during submission."
        },
        {
          "id": 1153922,
          "postDate": "2021-01-15T08:49:26.117Z",
          "content": "<p>When the internet is off, do you see any error logs?<br>\nThe reason I am asking this  - may be your  <strong>pretrained</strong> flag is set to <strong>True</strong>. And when the internet is off, it can't download the pretrained weight.<br>\nIs your notebook public? If it is you can share the link, then we can help you with appropriate suggestion.</p>",
          "rawMarkdown": "When the internet is off, do you see any error logs?\nThe reason I am asking this  - may be your  **pretrained** flag is set to **True**. And when the internet is off, it can't download the pretrained weight.\nIs your notebook public? If it is you can share the link, then we can help you with appropriate suggestion."
        },
        {
          "id": 1153924,
          "postDate": "2021-01-15T08:51:00.183Z",
          "content": "<p>I have made it public for u now! <a href=\"https://www.kaggle.com/satyapr/inf-casava\" target=\"_blank\">https://www.kaggle.com/satyapr/inf-casava</a></p>",
          "rawMarkdown": "I have made it public for u now! https://www.kaggle.com/satyapr/inf-casava"
        },
        {
          "id": 1153929,
          "postDate": "2021-01-15T08:58:29.943Z",
          "content": "<p>in <strong>In [22]</strong> cell </p>\n<p><code>model_ft = models.resnet18(pretrained = True)</code><br>\n<code>num_ftrs = model_ft.fc.in_features</code></p>\n<p>Can you try with <strong>pretrained=False</strong>? Because of the <strong>True</strong> value, you were in need of internet access.</p>",
          "rawMarkdown": "in **In [22]** cell \n\n`model_ft = models.resnet18(pretrained = True)`\n`num_ftrs = model_ft.fc.in_features`\n\nCan you try with **pretrained=False**? Because of the **True** value, you were in need of internet access.",
          "votes": 1
        },
        {
          "id": 1153950,
          "postDate": "2021-01-15T09:25:17.457Z",
          "content": "<p>oops, I missed that, Thank you very much for your help!!</p>",
          "rawMarkdown": "oops, I missed that, Thank you very much for your help!!",
          "votes": 1
        },
        {
          "id": 1153972,
          "postDate": "2021-01-15T09:50:02.147Z",
          "content": "<p>I have another problem now..after submission it is showing an error and no submission csv found. Any idea about it?</p>",
          "rawMarkdown": "I have another problem now..after submission it is showing an error and no submission csv found. Any idea about it?"
        },
        {
          "id": 1154013,
          "postDate": "2021-01-15T10:32:07.030Z",
          "content": "<p>I think the issue is in your predictions appending code.</p>\n<pre><code># you are appending each batch results  in y_preds, this will be a 2-D list\ny_preds.append(winners)\n\n# and you are trying to create a column with the above 2-D list\nsubmission = pd.DataFrame({'image_id': test_images, 'label': y_preds})\n</code></pre>\n<p>When there is only one image in the test set, it's working perfectly.<br>\nBut when you will be processing test images in a batch, you can not create pandas dataframe with the prediction results directly.<br>\nHere is a code snippet for you to understand, what is happening in your code -&gt; <a href=\"https://ideone.com/aW5laD\" target=\"_blank\">https://ideone.com/aW5laD</a></p>\n<p>You can concate the prediction results in a single list with this line -&gt; <br>\n<code>submission = pd.DataFrame({'image_id': test_images, 'label':  np.concatenate(y_preds)})</code></p>\n<p>Hope this helps you.</p>",
          "rawMarkdown": "I think the issue is in your predictions appending code.\n``` \n# you are appending each batch results  in y_preds, this will be a 2-D list\ny_preds.append(winners)\n\n# and you are trying to create a column with the above 2-D list\nsubmission = pd.DataFrame({'image_id': test_images, 'label': y_preds})\n```\nWhen there is only one image in the test set, it's working perfectly.\nBut when you will be processing test images in a batch, you can not create pandas dataframe with the prediction results directly.\nHere is a code snippet for you to understand, what is happening in your code -> https://ideone.com/aW5laD\n\nYou can concate the prediction results in a single list with this line -> \n`submission = pd.DataFrame({'image_id': test_images, 'label':  np.concatenate(y_preds)})`\n\nHope this helps you.",
          "votes": 1
        },
        {
          "id": 1154258,
          "postDate": "2021-01-15T14:14:42.437Z",
          "content": "<p>I got your point now! I will do that! BTW thank you very much for your quick responses.</p>",
          "rawMarkdown": "I got your point now! I will do that! BTW thank you very much for your quick responses."
        }
      ]
    },
    {
      "id": 1125777,
      "postDate": "2020-12-25T03:59:55.327Z",
      "rawMarkdown": "",
      "isDeleted": true
    }
  ],
  "comments": [
    {
      "id": 1125774,
      "author_name": "choco",
      "author_url": "",
      "post_date": "2020-12-25T03:50:13.237000",
      "content": "<p>when you train the model, save the weight in the kaggle dataset. Then, submit your code with saved weight. (you upload the weight in the kaggle data tab and then add the dataset in the notebook) </p>\n<p><img src=\"https://drive.google.com/uc?export=view&amp;id=1C2a8GkUCyLZO67Shz58sHw0AAm51OZWk\" alt=\"\"></p>",
      "votes": 1,
      "replies": [
        {
          "id": 1125889,
          "author_name": "jabertuhin",
          "author_url": "",
          "post_date": "2020-12-25T06:45:30.117000",
          "content": "<p>In addition, <br>\n<code>model = torchvision.models.resnet18(pretrained=False)</code><br>\nYou need to set the pretrained argument <strong>False</strong>, otherwise it will try to download the pre-trained weight which is not needed for inference.</p>",
          "votes": 1,
          "replies": []
        },
        {
          "id": 1126023,
          "author_name": "qiuye",
          "author_url": "",
          "post_date": "2020-12-25T09:09:29.137000",
          "content": "<p>Thanks a lot！</p>",
          "votes": 0,
          "replies": []
        },
        {
          "id": 1126025,
          "author_name": "qiuye",
          "author_url": "",
          "post_date": "2020-12-25T09:10:06.263000",
          "content": "<p>Nice idea！</p>",
          "votes": 0,
          "replies": []
        },
        {
          "id": 1126346,
          "author_name": "qiuye",
          "author_url": "",
          "post_date": "2020-12-25T14:36:03.323000",
          "content": "<p>I found another question…<br>\nCan I ask what's your model-saved path?<br>\nWhen I save my model by using './model.pth',it was found at /kaggle/working (dir).But if want to upload my model data,it must be in notebook (dir).</p>",
          "votes": 0,
          "replies": []
        },
        {
          "id": 1126537,
          "author_name": "jabertuhin",
          "author_url": "",
          "post_date": "2020-12-25T17:21:32.607000",
          "content": "<p>The competition dataset and whatever you add in the notebook will be in the input directory, and the files you create during your notebook session will be under output directory. That's how kaggle handles/manages files.</p>",
          "votes": 0,
          "replies": []
        },
        {
          "id": 1126857,
          "author_name": "qiuye",
          "author_url": "",
          "post_date": "2020-12-26T03:39:49.710000",
          "content": "<p>It likes this.<br>\n<img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F3197411%2F4405438349df0ddac5a51f908c6b0ac2%2F.png?generation=1608954020820419&amp;alt=media\" alt=\"\"><br>\n<img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F3197411%2F2bfdddf0d5b4599596a58f3590bf1be2%2F1.png?generation=1608953972401427&amp;alt=media\" alt=\"\"></p>",
          "votes": 0,
          "replies": []
        },
        {
          "id": 1126864,
          "author_name": "choco",
          "author_url": "",
          "post_date": "2020-12-26T03:54:33.333000",
          "content": "<p>Can I see your save code and output file in the training notebook ? above picture(output) is the training notebook results?? </p>\n<p>I usually save the weight of the model in the local and upload it on the Data tab. (i used local gpu) </p>",
          "votes": 0,
          "replies": []
        },
        {
          "id": 1126882,
          "author_name": "qiuye",
          "author_url": "",
          "post_date": "2020-12-26T04:28:34.937000",
          "content": "<p>url:<a href=\"https://www.kaggle.com/qiujunfu/notebook-train\" target=\"_blank\">https://www.kaggle.com/qiujunfu/notebook-train</a>.</p>",
          "votes": 0,
          "replies": []
        },
        {
          "id": 1126935,
          "author_name": "choco",
          "author_url": "",
          "post_date": "2020-12-26T05:25:27.767000",
          "content": "<p>I think it's a version problem!! There was no problem when I turned it around and checked it out there was no problem. </p>\n<p><img src=\"https://drive.google.com/uc?export=view&amp;id=1MCV3J6dGnX_KVW-4Q0T7pfJqmnv7V8eV\" alt=\"\"></p>",
          "votes": 0,
          "replies": []
        },
        {
          "id": 1127236,
          "author_name": "qiuye",
          "author_url": "",
          "post_date": "2020-12-26T11:06:39.517000",
          "content": "<p>I hava not solved the problem till now.In fact,the 'model.pth' can be found in notebook,however ,it can't be used to upload dataset(emmm….maybe it's a bug.)<br>\nSo at last, I download the 'model.pth' to my computer and then upload it to the dataset,this is a feasible solution.<br>\nThank you so much for your kindness! </p>",
          "votes": 1,
          "replies": []
        }
      ]
    },
    {
      "id": 1153862,
      "author_name": "satya",
      "author_url": "",
      "post_date": "2021-01-15T07:52:15.950000",
      "content": "<p>I have saved my model like this:<br>\nPATH = './model_18_20.pth'<br>\ntorch.save(model.state_dict(), PATH)</p>\n<p>but when I am running my inference notebook while keeping the internet off and saving the notebook for submission, No output file is showing during submission. But when the internet is on output file is visible, but we cannot submit it because the internet should be off. Can someone help?</p>",
      "votes": 0,
      "replies": [
        {
          "id": 1153884,
          "author_name": "jabertuhin",
          "author_url": "",
          "post_date": "2021-01-15T08:08:56.883000",
          "content": "<p>Can you go through your notebook cell outputs and notebook run logs?<br>\nDo you see any error?</p>",
          "votes": 0,
          "replies": []
        },
        {
          "id": 1153907,
          "author_name": "satya",
          "author_url": "",
          "post_date": "2021-01-15T08:31:52.473000",
          "content": "<p>There is no error every cell is running fine! As I said when the internet is ON, I am able to see output file during submission.</p>",
          "votes": 0,
          "replies": []
        },
        {
          "id": 1153922,
          "author_name": "jabertuhin",
          "author_url": "",
          "post_date": "2021-01-15T08:49:26.117000",
          "content": "<p>When the internet is off, do you see any error logs?<br>\nThe reason I am asking this  - may be your  <strong>pretrained</strong> flag is set to <strong>True</strong>. And when the internet is off, it can't download the pretrained weight.<br>\nIs your notebook public? If it is you can share the link, then we can help you with appropriate suggestion.</p>",
          "votes": 0,
          "replies": []
        },
        {
          "id": 1153924,
          "author_name": "satya",
          "author_url": "",
          "post_date": "2021-01-15T08:51:00.183000",
          "content": "<p>I have made it public for u now! <a href=\"https://www.kaggle.com/satyapr/inf-casava\" target=\"_blank\">https://www.kaggle.com/satyapr/inf-casava</a></p>",
          "votes": 0,
          "replies": []
        },
        {
          "id": 1153929,
          "author_name": "jabertuhin",
          "author_url": "",
          "post_date": "2021-01-15T08:58:29.943000",
          "content": "<p>in <strong>In [22]</strong> cell </p>\n<p><code>model_ft = models.resnet18(pretrained = True)</code><br>\n<code>num_ftrs = model_ft.fc.in_features</code></p>\n<p>Can you try with <strong>pretrained=False</strong>? Because of the <strong>True</strong> value, you were in need of internet access.</p>",
          "votes": 1,
          "replies": []
        },
        {
          "id": 1153950,
          "author_name": "satya",
          "author_url": "",
          "post_date": "2021-01-15T09:25:17.457000",
          "content": "<p>oops, I missed that, Thank you very much for your help!!</p>",
          "votes": 1,
          "replies": []
        },
        {
          "id": 1153972,
          "author_name": "satya",
          "author_url": "",
          "post_date": "2021-01-15T09:50:02.147000",
          "content": "<p>I have another problem now..after submission it is showing an error and no submission csv found. Any idea about it?</p>",
          "votes": 0,
          "replies": []
        },
        {
          "id": 1154013,
          "author_name": "jabertuhin",
          "author_url": "",
          "post_date": "2021-01-15T10:32:07.030000",
          "content": "<p>I think the issue is in your predictions appending code.</p>\n<pre><code># you are appending each batch results  in y_preds, this will be a 2-D list\ny_preds.append(winners)\n\n# and you are trying to create a column with the above 2-D list\nsubmission = pd.DataFrame({'image_id': test_images, 'label': y_preds})\n</code></pre>\n<p>When there is only one image in the test set, it's working perfectly.<br>\nBut when you will be processing test images in a batch, you can not create pandas dataframe with the prediction results directly.<br>\nHere is a code snippet for you to understand, what is happening in your code -&gt; <a href=\"https://ideone.com/aW5laD\" target=\"_blank\">https://ideone.com/aW5laD</a></p>\n<p>You can concate the prediction results in a single list with this line -&gt; <br>\n<code>submission = pd.DataFrame({'image_id': test_images, 'label':  np.concatenate(y_preds)})</code></p>\n<p>Hope this helps you.</p>",
          "votes": 1,
          "replies": []
        },
        {
          "id": 1154258,
          "author_name": "satya",
          "author_url": "",
          "post_date": "2021-01-15T14:14:42.437000",
          "content": "<p>I got your point now! I will do that! BTW thank you very much for your quick responses.</p>",
          "votes": 0,
          "replies": []
        }
      ]
    },
    {
      "id": 1125777,
      "author_name": "",
      "author_url": "",
      "post_date": "2020-12-25T03:59:55.327000",
      "content": "",
      "votes": 0,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "1125774": "when you train the model, save the weight in the kaggle dataset. Then, submit your code with saved weight. (you upload the weight in the kaggle data tab and then add the dataset in the notebook) \n\n![](https://drive.google.com/uc?export=view&id=1C2a8GkUCyLZO67Shz58sHw0AAm51OZWk)",
    "1125767": "In the notebook，by setting pretrained model ：\n     model = torchvision.models.resnet18(pretrained=True)\nIt works.\nBut when I want to submit my code,it requests me not use the internet,so I can't download model from pytorch.How to solve this problem?",
    "1153862": "I have saved my model like this:\nPATH = './model_18_20.pth'\ntorch.save(model.state_dict(), PATH)\n\nbut when I am running my inference notebook while keeping the internet off and saving the notebook for submission, No output file is showing during submission. But when the internet is on output file is visible, but we cannot submit it because the internet should be off. Can someone help?",
    "1125777": ""
  }
}