{
  "id": 11408,
  "title": "Starter Code for R Classification",
  "url": "/competitions/datasciencebowl/discussion/11408",
  "author_name": "",
  "post_date": "2015-01-01T18:51:02.873Z",
  "votes": 10,
  "comment_count": 10,
  "views": 5976,
  "content": "<p>This is basic starter code to create a plankton classification model based on image dimensions and image density. The code uses randomForest, but you can easily change to any classifier you like.&nbsp;</p>\n<p>This code takes about 45 minutes to run on my old laptop. Most of the time is spent reading the thousands of images. You may have better performance with an SSD. I would appreciate any suggestions on how to speed up this code.</p>\n<p>This code should produce a results file with public score of 3.397518. Surely you can come up with a better model!</p>\n<p><a href=\"http://github.com/JeffHebert/Plankton_Classification\">http://github.com/JeffHebert/Plankton_Classification</a></p>\n<p>Enjoy,</p>\n<p>Jeff</p>\n\n<p>Edit: Found and fixed an omission&nbsp;in the prediction code. Log Loss improved from 7.626163 to&nbsp;3.397518.</p>",
  "messages": [
    {
      "id": "62177",
      "postDate": "01/01/2015 18:51:02",
      "content": "<p>This is basic starter code to create a plankton classification model based on image dimensions and image density. The code uses randomForest, but you can easily change to any classifier you like.&nbsp;</p>\n<p>This code takes about 45 minutes to run on my old laptop. Most of the time is spent reading the thousands of images. You may have better performance with an SSD. I would appreciate any suggestions on how to speed up this code.</p>\n<p>This code should produce a results file with public score of 3.397518. Surely you can come up with a better model!</p>\n<p><a href=\"http://github.com/JeffHebert/Plankton_Classification\">http://github.com/JeffHebert/Plankton_Classification</a></p>\n<p>Enjoy,</p>\n<p>Jeff</p>\n\n<p>Edit: Found and fixed an omission&nbsp;in the prediction code. Log Loss improved from 7.626163 to&nbsp;3.397518.</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "62185",
      "postDate": "01/01/2015 20:16:49",
      "content": "<p>If I am reading the leader board correctly, a constant 1/121 prediction for everything gives&nbsp;4.795791.</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "62198",
      "postDate": "01/02/2015 02:33:01",
      "content": "<p>Ha, that is why it is called starter code, not beat the benchmark.&nbsp; The point of this code is to help folks get started.</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "62392",
      "postDate": "01/07/2015 08:47:25",
      "content": "<p>Hi Jeff,</p>\n<p>Thanks for posting the code and sharing with all of us.</p>\n<p>I just started playing with this code and competition so mind you the newbie questions here.</p>\n<p>I'm getting some &quot;errors&quot; or zero counts at these lines in the code:</p>\n<p>train_file_list &lt;- list.files(paste(train_data_dir,&quot;/&quot;,classID,sep=&quot;&quot;))<br> train_cnt &lt;- length(train_file_list)</p>\n<p>Because there are only 1 jpeg file for each type. Unless there was an error on the zip files I downloaded(?).</p>\n<p>Perhaps you or anyone can comment how many files do we have in the train.zip and the test.zip so I can doublecheck(checksum of sorts) that when I extracted it all the files are correct and there.</p>\n<p>Also, train.zip when extracted only has jpeg files no subfolders per each class.</p>\n<p>And test.zip when extracted only has one folder with about ~130K files.</p>\n<p>Please kindly confirm with me that these are what you guys are seeing as well.</p>\n<p>Thanks!</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "62433",
      "postDate": "01/08/2015 01:13:28",
      "content": "<p>I think something went wrong when you unzipped the training and testing data. My original test data only had a few thousand images. Anyway, you should have 121 classes in your training data with 30336 train images and 130400 test images.</p>\n\n<p><code>&gt; nrow(train_data)<br>[1] 30336</code></p>\n<p><code>&gt;<span style=\"font-family: monospace, serif; font-size: 1em; line-height: 1.4;\">nrow(test_data)</span></code></p>\n<p><code>[1] 130400</code></p>\n<p><code>&gt; length(unique(train_data$class)) &nbsp;#Count of unique classes<br>[1] 121<br></code><code><br></code><code><br></code></p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "62434",
      "postDate": "01/08/2015 01:16:52",
      "content": "<p>Here is a list of all the training classes and number of images in each class. These are results of&nbsp;table(train_data$class)</p>\n\n<p>Good luck,</p>\n<p>Jeff</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "62446",
      "postDate": "01/08/2015 07:49:14",
      "content": "<p>For the test data, I get 130,400 files.</p>\n<p>For the train data, I get 30,336 files.</p>\n<p>I think it was the network I was on that was causing the error.</p>\n\n<p>Thanks!</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "62539",
      "postDate": "01/09/2015 23:10:47",
      "content": "<p>Dear Jeff,</p>\n<p>Thanks for sharing a starter code. I'm illiterate in R, so I had to ask explicitly.</p>\n<p>You calculate some features from images and then feed the random forest classifier with those features. Do you also use all pixel values in the input, or do you just use the extracted features?</p>\n<p>Best,</p>\n<p>ugur</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "62549",
      "postDate": "01/10/2015 03:38:09",
      "content": "<p>Hi Ugur,</p>\n\n<p>This code just uses the extracted features. I don't think that random forest would work well&nbsp;with raw image data. You could try resizing all the images and then adding each pixel as a feature. Here are a couple lines of code that can go in the extract_stats() function.</p>\n\n<p><code>image_vector &lt;- as.vector(image)</code></p>\n<p><code>return(c(length=im_length,width=im_width,density=im_density,ratio=im_ratio, image_vector))</code></p>\n\n<p>Just remember that R is very slow with image manipulation. So it is probably best to use some other program to resize the images. I have not done this, but there are a few other threads on the forum about this.</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "62583",
      "postDate": "01/10/2015 19:53:21",
      "content": "<p>Using only the extracted features makes more sense to me. The python tutorial uses all pixels + features, which, I think, makes the analysis space too big. Thanks for the reply!&nbsp;</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "62592",
      "postDate": "01/10/2015 22:45:04",
      "content": "<p>I've started using various filters to identify objects and edges within the image. I am&nbsp;calculating features like area of the largest object, longest width, shortest width, and&nbsp;perimeter.</p>",
      "rawMarkdown": "",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 62185,
      "author_name": "jfkingiii",
      "author_url": "",
      "post_date": "01/01/2015 20:16:49",
      "content": "<p>If I am reading the leader board correctly, a constant 1/121 prediction for everything gives&nbsp;4.795791.</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 62198,
      "author_name": "jeffhebert",
      "author_url": "",
      "post_date": "01/02/2015 02:33:01",
      "content": "<p>Ha, that is why it is called starter code, not beat the benchmark.&nbsp; The point of this code is to help folks get started.</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 62392,
      "author_name": "datailluminator",
      "author_url": "",
      "post_date": "01/07/2015 08:47:25",
      "content": "<p>Hi Jeff,</p>\n<p>Thanks for posting the code and sharing with all of us.</p>\n<p>I just started playing with this code and competition so mind you the newbie questions here.</p>\n<p>I'm getting some &quot;errors&quot; or zero counts at these lines in the code:</p>\n<p>train_file_list &lt;- list.files(paste(train_data_dir,&quot;/&quot;,classID,sep=&quot;&quot;))<br> train_cnt &lt;- length(train_file_list)</p>\n<p>Because there are only 1 jpeg file for each type. Unless there was an error on the zip files I downloaded(?).</p>\n<p>Perhaps you or anyone can comment how many files do we have in the train.zip and the test.zip so I can doublecheck(checksum of sorts) that when I extracted it all the files are correct and there.</p>\n<p>Also, train.zip when extracted only has jpeg files no subfolders per each class.</p>\n<p>And test.zip when extracted only has one folder with about ~130K files.</p>\n<p>Please kindly confirm with me that these are what you guys are seeing as well.</p>\n<p>Thanks!</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 62433,
      "author_name": "jeffhebert",
      "author_url": "",
      "post_date": "01/08/2015 01:13:28",
      "content": "<p>I think something went wrong when you unzipped the training and testing data. My original test data only had a few thousand images. Anyway, you should have 121 classes in your training data with 30336 train images and 130400 test images.</p>\n\n<p><code>&gt; nrow(train_data)<br>[1] 30336</code></p>\n<p><code>&gt;<span style=\"font-family: monospace, serif; font-size: 1em; line-height: 1.4;\">nrow(test_data)</span></code></p>\n<p><code>[1] 130400</code></p>\n<p><code>&gt; length(unique(train_data$class)) &nbsp;#Count of unique classes<br>[1] 121<br></code><code><br></code><code><br></code></p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 62434,
      "author_name": "jeffhebert",
      "author_url": "",
      "post_date": "01/08/2015 01:16:52",
      "content": "<p>Here is a list of all the training classes and number of images in each class. These are results of&nbsp;table(train_data$class)</p>\n\n<p>Good luck,</p>\n<p>Jeff</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 62446,
      "author_name": "datailluminator",
      "author_url": "",
      "post_date": "01/08/2015 07:49:14",
      "content": "<p>For the test data, I get 130,400 files.</p>\n<p>For the train data, I get 30,336 files.</p>\n<p>I think it was the network I was on that was causing the error.</p>\n\n<p>Thanks!</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 62539,
      "author_name": "ugurguney",
      "author_url": "",
      "post_date": "01/09/2015 23:10:47",
      "content": "<p>Dear Jeff,</p>\n<p>Thanks for sharing a starter code. I'm illiterate in R, so I had to ask explicitly.</p>\n<p>You calculate some features from images and then feed the random forest classifier with those features. Do you also use all pixel values in the input, or do you just use the extracted features?</p>\n<p>Best,</p>\n<p>ugur</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 62549,
      "author_name": "jeffhebert",
      "author_url": "",
      "post_date": "01/10/2015 03:38:09",
      "content": "<p>Hi Ugur,</p>\n\n<p>This code just uses the extracted features. I don't think that random forest would work well&nbsp;with raw image data. You could try resizing all the images and then adding each pixel as a feature. Here are a couple lines of code that can go in the extract_stats() function.</p>\n\n<p><code>image_vector &lt;- as.vector(image)</code></p>\n<p><code>return(c(length=im_length,width=im_width,density=im_density,ratio=im_ratio, image_vector))</code></p>\n\n<p>Just remember that R is very slow with image manipulation. So it is probably best to use some other program to resize the images. I have not done this, but there are a few other threads on the forum about this.</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 62583,
      "author_name": "ugurguney",
      "author_url": "",
      "post_date": "01/10/2015 19:53:21",
      "content": "<p>Using only the extracted features makes more sense to me. The python tutorial uses all pixels + features, which, I think, makes the analysis space too big. Thanks for the reply!&nbsp;</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 62592,
      "author_name": "jeffhebert",
      "author_url": "",
      "post_date": "01/10/2015 22:45:04",
      "content": "<p>I've started using various filters to identify objects and edges within the image. I am&nbsp;calculating features like area of the largest object, longest width, shortest width, and&nbsp;perimeter.</p>",
      "votes": null,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "62177": "",
    "62185": "",
    "62198": "",
    "62392": "",
    "62433": "",
    "62434": "",
    "62446": "",
    "62539": "",
    "62549": "",
    "62583": "",
    "62592": ""
  },
  "source": "meta"
}