{
  "id": 12481,
  "title": "Code to print out predictions (probs) for Caffe ",
  "url": "/competitions/datasciencebowl/discussion/12481",
  "author_name": "",
  "post_date": "2015-02-11T14:29:34.133Z",
  "votes": 5,
  "comment_count": 13,
  "views": 10751,
  "content": "<p>I wrote a code to print out prediction probabilities. I want to share it for the people who use Caffe, but suffer from printing out predictions.</p>\n<p>More precisely, this code print out the activities of the last layer. If the last layer is softmax, you will get probabilities.&nbsp;</p>\n<p>This code only works with DATA (esp. LMDB) and IMAGE_DATA layer. I thinks this code is somewhat simple, so you can modify it to your data layer.</p>\n\n<p>Put this file to $CAFFE_ROOT/tools/predict.cpp<br>compile it with &#8216;make&#8217; command at $CAFFE_ROOT</p>\n\n<p>I tested the code on Ubuntu 14.04 LTS with nvidia titan black. I&#8217;m not familiar with copyright. If there is any problem sharing this code, please let me know.</p>\n<p>I hope this is helpful to you. :)&nbsp;</p>\n<p>Jaehyun Lim</p>",
  "messages": [
    {
      "id": "64030",
      "postDate": "02/11/2015 14:29:34",
      "content": "<p>I wrote a code to print out prediction probabilities. I want to share it for the people who use Caffe, but suffer from printing out predictions.</p>\n<p>More precisely, this code print out the activities of the last layer. If the last layer is softmax, you will get probabilities.&nbsp;</p>\n<p>This code only works with DATA (esp. LMDB) and IMAGE_DATA layer. I thinks this code is somewhat simple, so you can modify it to your data layer.</p>\n\n<p>Put this file to $CAFFE_ROOT/tools/predict.cpp<br>compile it with &#8216;make&#8217; command at $CAFFE_ROOT</p>\n\n<p>I tested the code on Ubuntu 14.04 LTS with nvidia titan black. I&#8217;m not familiar with copyright. If there is any problem sharing this code, please let me know.</p>\n<p>I hope this is helpful to you. :)&nbsp;</p>\n<p>Jaehyun Lim</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "64777",
      "postDate": "02/23/2015 19:17:09",
      "content": "<p>Can this be used with python? I too am trying to figure out how to output probabilities as opposed to just the assigned class with caffe. Thanks for any help!</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "64783",
      "postDate": "02/23/2015 21:20:09",
      "content": "<p>Check out <a href=\"http://nbviewer.ipython.org/github/BVLC/caffe/blob/master/examples/classification.ipynb\">this link</a> and <a href=\"http://nbviewer.ipython.org/github/BVLC/caffe/blob/master/examples/filter_visualization.ipynb\">this one</a>.</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "64786",
      "postDate": "02/23/2015 22:33:27",
      "content": "<p>Thanks for the links. I've been using those as references so far. I'm having some trouble however with outputting the probabilities as opposed to just class assignments, have asked on the caffe-users mailing list now.</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "64790",
      "postDate": "02/23/2015 22:47:45",
      "content": "<p>They do just that in the second link I posted at In [18]. I.e. just get the output of the final layer.</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "64801",
      "postDate": "02/24/2015 05:47:42",
      "content": "<p>Yeah, I was trying that tutorial out on the MNIST data they provide, and for whatever reason when I extract those probabilities for that example, all the probabilities are 0 or 1 (only one 1). I am just confused as to whether that is because the net in that case very strongly believes in one class, or that it is somehow outputting a binary class assignment, I'm starting to think its the former. Thanks for the help though!</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "65059",
      "postDate": "02/27/2015 18:47:46",
      "content": "<p>Here is the code I use for creating the submission. You might have to modify it according to your needs.</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "65067",
      "postDate": "02/27/2015 20:29:40",
      "content": "<p>Thanks so much for sharing that! Just had a few questions if you wouldn't mind answering them.</p>\n<p>1. How do you set the batch size for your network, is it just supposed to be that the number of iterations * batch size covers the training set size?</p>\n<p>2. Is it faster to do net.forward() for a mini-batch, as opposed to calling net.forward_all([image]) for each test image?</p>\n<p>3. Is the softmax loss not the same as the class probabilties that we are supposed to be outputting? I see the step where you divide the prediction by the sum of the predictions (all exponentiated). Could you explain that part a little bit more?</p>\n\n<p>Thanks again so much!</p>\n\n<p>Best,</p>\n<p>Nitin</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "65106",
      "postDate": "02/28/2015 05:16:56",
      "content": "<p>@nitini</p>\n<p>Hi :)&nbsp;</p>\n<p>1. a simple&nbsp;comment about&nbsp;mini-batch size (https://github.com/BVLC/caffe/issues/218)</p>\n<p>mini-batch size (usually in SGD) is about how many samples are used to calculate gradient among your training data.&nbsp;This&nbsp;means you assume that the data sampled in mini-batch approximately represent the total training data. General tendency of learning w.r.t batchsize is in the comment at the link.</p>\n<p>Choosing batchsize and iterations are irrelevant.&nbsp;&nbsp;</p>\n<p>2. sorry, I have not much idea of implementation details</p>\n<p>3. Softmax loss is a single layer calculating softmax (prob) and log loss at once. This is because derivatives of the combined term is simpler (and computationally stable) than the multiplication of the derivatives of two separate layers (since derivatives of log loss have division?? as I remember)</p>\n<p>That is why some examples of caffe provides different prototxts at train/test/predict</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "65115",
      "postDate": "02/28/2015 09:25:56",
      "content": "<p>Hi Niti&nbsp;</p>\n<p>I just want to add to the answer of Jae Hyun Lim.</p>\n<p>1/. The batch size is set in the file 'train_val.prototype' (see&nbsp;http://caffe.berkeleyvision.org/tutorial/layers.html#data-layers) it defines how many examples are evaluated in 'one go' or one iteration.</p>\n<p>2. I don't now either. I just found&nbsp;net.forward() &nbsp;in the following answer&nbsp;https://github.com/BVLC/caffe/issues/1774</p>\n<p>3. My last layer 'fc8' is an INNER_PRODUCT. To calculate the class-probability from that layer one has to do the calculation as done in the script. I guess I could have also added a&nbsp;SOFTMAX layer and not do the calculation.</p>\n<p>I hope that helps. Good luck in the competition.</p>\n<p>Oliver</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "65301",
      "postDate": "03/02/2015 22:43:19",
      "content": "<p>Thanks for all the updates guys. This is really useful! Batch size makes sense to me now, as does pulling the final probabilities out.</p>\n<p>On an unrelated note, has anyone trained a caffe net for this data set on a GTX 580, I can get&nbsp; access to one but was wondering if its worth trying out?</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "169904",
      "postDate": "03/23/2017 07:10:02",
      "content": "<p>If you had trained lenet mnist with images in the range of 0 to 1 ,ie, you had defined scale in lenet.prototxt then you have to deploy the network on images in the same range,ie, 0 to 1 not from 0 to 255 in the case of grayscale images</p>",
      "rawMarkdown": "If you had trained lenet mnist with images in the range of 0 to 1 ,ie, you had defined scale in lenet.prototxt then you have to deploy the network on images in the same range,ie, 0 to 1 not from 0 to 255 in the case of grayscale images",
      "votes": null
    },
    {
      "id": "169905",
      "postDate": "03/23/2017 07:13:26",
      "content": "<p>then you will get correct probs for a class. I will publish a notebook when I have time for clarity and share the link here.</p>",
      "rawMarkdown": "then you will get correct probs for a class. I will publish a notebook when I have time for clarity and share the link here.",
      "votes": null
    },
    {
      "id": "169910",
      "postDate": "03/23/2017 07:49:15",
      "content": "<p><a href=\"http://stackoverflow.com/questions/40966139/layer-probability-always-1/42969980#42969980\">Here is the link</a></p>",
      "rawMarkdown": "[Here is the link][1]\n\n\n  [1]: http://stackoverflow.com/questions/40966139/layer-probability-always-1/42969980#42969980",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 64777,
      "author_name": "nitini",
      "author_url": "",
      "post_date": "02/23/2015 19:17:09",
      "content": "<p>Can this be used with python? I too am trying to figure out how to output probabilities as opposed to just the assigned class with caffe. Thanks for any help!</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 64783,
      "author_name": "stevenreitsma",
      "author_url": "",
      "post_date": "02/23/2015 21:20:09",
      "content": "<p>Check out <a href=\"http://nbviewer.ipython.org/github/BVLC/caffe/blob/master/examples/classification.ipynb\">this link</a> and <a href=\"http://nbviewer.ipython.org/github/BVLC/caffe/blob/master/examples/filter_visualization.ipynb\">this one</a>.</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 64786,
      "author_name": "nitini",
      "author_url": "",
      "post_date": "02/23/2015 22:33:27",
      "content": "<p>Thanks for the links. I've been using those as references so far. I'm having some trouble however with outputting the probabilities as opposed to just class assignments, have asked on the caffe-users mailing list now.</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 64790,
      "author_name": "stevenreitsma",
      "author_url": "",
      "post_date": "02/23/2015 22:47:45",
      "content": "<p>They do just that in the second link I posted at In [18]. I.e. just get the output of the final layer.</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 64801,
      "author_name": "nitini",
      "author_url": "",
      "post_date": "02/24/2015 05:47:42",
      "content": "<p>Yeah, I was trying that tutorial out on the MNIST data they provide, and for whatever reason when I extract those probabilities for that example, all the probabilities are 0 or 1 (only one 1). I am just confused as to whether that is because the net in that case very strongly believes in one class, or that it is somehow outputting a binary class assignment, I'm starting to think its the former. Thanks for the help though!</p>",
      "votes": null,
      "replies": [
        {
          "id": 169904,
          "author_name": "jeremiah1",
          "author_url": "",
          "post_date": "03/23/2017 07:10:02",
          "content": "<p>If you had trained lenet mnist with images in the range of 0 to 1 ,ie, you had defined scale in lenet.prototxt then you have to deploy the network on images in the same range,ie, 0 to 1 not from 0 to 255 in the case of grayscale images</p>",
          "votes": null,
          "replies": []
        },
        {
          "id": 169905,
          "author_name": "jeremiah1",
          "author_url": "",
          "post_date": "03/23/2017 07:13:26",
          "content": "<p>then you will get correct probs for a class. I will publish a notebook when I have time for clarity and share the link here.</p>",
          "votes": null,
          "replies": []
        }
      ]
    },
    {
      "id": 65059,
      "author_name": "oduerr",
      "author_url": "",
      "post_date": "02/27/2015 18:47:46",
      "content": "<p>Here is the code I use for creating the submission. You might have to modify it according to your needs.</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 65067,
      "author_name": "nitini",
      "author_url": "",
      "post_date": "02/27/2015 20:29:40",
      "content": "<p>Thanks so much for sharing that! Just had a few questions if you wouldn't mind answering them.</p>\n<p>1. How do you set the batch size for your network, is it just supposed to be that the number of iterations * batch size covers the training set size?</p>\n<p>2. Is it faster to do net.forward() for a mini-batch, as opposed to calling net.forward_all([image]) for each test image?</p>\n<p>3. Is the softmax loss not the same as the class probabilties that we are supposed to be outputting? I see the step where you divide the prediction by the sum of the predictions (all exponentiated). Could you explain that part a little bit more?</p>\n\n<p>Thanks again so much!</p>\n\n<p>Best,</p>\n<p>Nitin</p>",
      "votes": null,
      "replies": [
        {
          "id": 169910,
          "author_name": "jeremiah1",
          "author_url": "",
          "post_date": "03/23/2017 07:49:15",
          "content": "<p><a href=\"http://stackoverflow.com/questions/40966139/layer-probability-always-1/42969980#42969980\">Here is the link</a></p>",
          "votes": null,
          "replies": []
        }
      ]
    },
    {
      "id": 65106,
      "author_name": "jaehyunlim",
      "author_url": "",
      "post_date": "02/28/2015 05:16:56",
      "content": "<p>@nitini</p>\n<p>Hi :)&nbsp;</p>\n<p>1. a simple&nbsp;comment about&nbsp;mini-batch size (https://github.com/BVLC/caffe/issues/218)</p>\n<p>mini-batch size (usually in SGD) is about how many samples are used to calculate gradient among your training data.&nbsp;This&nbsp;means you assume that the data sampled in mini-batch approximately represent the total training data. General tendency of learning w.r.t batchsize is in the comment at the link.</p>\n<p>Choosing batchsize and iterations are irrelevant.&nbsp;&nbsp;</p>\n<p>2. sorry, I have not much idea of implementation details</p>\n<p>3. Softmax loss is a single layer calculating softmax (prob) and log loss at once. This is because derivatives of the combined term is simpler (and computationally stable) than the multiplication of the derivatives of two separate layers (since derivatives of log loss have division?? as I remember)</p>\n<p>That is why some examples of caffe provides different prototxts at train/test/predict</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 65115,
      "author_name": "oduerr",
      "author_url": "",
      "post_date": "02/28/2015 09:25:56",
      "content": "<p>Hi Niti&nbsp;</p>\n<p>I just want to add to the answer of Jae Hyun Lim.</p>\n<p>1/. The batch size is set in the file 'train_val.prototype' (see&nbsp;http://caffe.berkeleyvision.org/tutorial/layers.html#data-layers) it defines how many examples are evaluated in 'one go' or one iteration.</p>\n<p>2. I don't now either. I just found&nbsp;net.forward() &nbsp;in the following answer&nbsp;https://github.com/BVLC/caffe/issues/1774</p>\n<p>3. My last layer 'fc8' is an INNER_PRODUCT. To calculate the class-probability from that layer one has to do the calculation as done in the script. I guess I could have also added a&nbsp;SOFTMAX layer and not do the calculation.</p>\n<p>I hope that helps. Good luck in the competition.</p>\n<p>Oliver</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 65301,
      "author_name": "nitini",
      "author_url": "",
      "post_date": "03/02/2015 22:43:19",
      "content": "<p>Thanks for all the updates guys. This is really useful! Batch size makes sense to me now, as does pulling the final probabilities out.</p>\n<p>On an unrelated note, has anyone trained a caffe net for this data set on a GTX 580, I can get&nbsp; access to one but was wondering if its worth trying out?</p>",
      "votes": null,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "64030": "",
    "64777": "",
    "64783": "",
    "64786": "",
    "64790": "",
    "64801": "",
    "65059": "",
    "65067": "",
    "65106": "",
    "65115": "",
    "65301": "",
    "169904": "If you had trained lenet mnist with images in the range of 0 to 1 ,ie, you had defined scale in lenet.prototxt then you have to deploy the network on images in the same range,ie, 0 to 1 not from 0 to 255 in the case of grayscale images",
    "169905": "then you will get correct probs for a class. I will publish a notebook when I have time for clarity and share the link here.",
    "169910": "[Here is the link][1]\n\n\n  [1]: http://stackoverflow.com/questions/40966139/layer-probability-always-1/42969980#42969980"
  },
  "source": "meta"
}