{
  "id": 11421,
  "title": "Converting images to standard scale code",
  "url": "/competitions/datasciencebowl/discussion/11421",
  "author_name": "",
  "post_date": "2015-01-04T18:56:36.943Z",
  "votes": 3,
  "comment_count": 14,
  "views": 5017,
  "content": "<p>Hi all,</p>\n<p>So for my first attempt at this problem, I've decided to perform image rescaling, and using the rescaled images as feature spaces. In the case of the example, I rescale all image to 72 x 72, and use the 5,184 pixel vector as a training example.</p>\n<p>Unfortunately, this code is somewhat slow; it takes about 0.44 seconds to perform the image rescale and updates to the matrix and category vector (even after using preallocation in R). I suspect that on the training set alone, this task will take ~ 4-5 hours. The one positive is that thanks to preallocation, the run time doesn't increase with object growth. Even using a rescale to a smaller image isn't much faster (20 x 20 ran at around 0.38 seconds per image).</p>\n<p>Has anyone else tried to perform a similar operation, especially in R? My code is attached for reference (with directory paths masked).&nbsp;</p>",
  "messages": [
    {
      "id": "62259",
      "postDate": "01/04/2015 18:56:36",
      "content": "<p>Hi all,</p>\n<p>So for my first attempt at this problem, I've decided to perform image rescaling, and using the rescaled images as feature spaces. In the case of the example, I rescale all image to 72 x 72, and use the 5,184 pixel vector as a training example.</p>\n<p>Unfortunately, this code is somewhat slow; it takes about 0.44 seconds to perform the image rescale and updates to the matrix and category vector (even after using preallocation in R). I suspect that on the training set alone, this task will take ~ 4-5 hours. The one positive is that thanks to preallocation, the run time doesn't increase with object growth. Even using a rescale to a smaller image isn't much faster (20 x 20 ran at around 0.38 seconds per image).</p>\n<p>Has anyone else tried to perform a similar operation, especially in R? My code is attached for reference (with directory paths masked).&nbsp;</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "62261",
      "postDate": "01/04/2015 19:26:06",
      "content": "<p>I have been running the data transformations with imagemagick; I don't have an exact timing but it's orders of magnitude faster than the numbers you're quoting. &nbsp;</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "62262",
      "postDate": "01/04/2015 19:34:35",
      "content": "<p>You can try to run the conversions in parallel. There is a package multicore that has this handy function mclapply (multi core lapply).&nbsp;</p>\n<p><code>mclapply(list_of_images,FUN=function(img_path){</code></p>\n<p><code></code><code>convert_image(img_path)</code></p>\n<p><code>})<br></code></p>\n<p>I hate R as a language but its support for parallelism is quite good.</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "62263",
      "postDate": "01/04/2015 19:46:55",
      "content": "<p>[quote=James King;62261]</p>\n<p>I have been running the data transformations with imagemagick; I don't have an exact timing but it's orders of magnitude faster than the numbers you're quoting. &nbsp;</p>\n<p>[/quote]</p>\n<p>Would you be able to provide an example shell/BASH script that performs this? I'm now trying to learn this :)</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "62264",
      "postDate": "01/04/2015 19:50:33",
      "content": "<p>[quote=Pawe&#322;;62262]</p>\n<p>You can try to run the conversions in parallel. There is a package multicore that has this handy function mclapply (multi core lapply).&nbsp;</p>\n<p><code>mclapply(list_of_images,FUN=function(img_path){</code></p>\n<p><code></code><code>convert_image(img_path)</code></p>\n<p><code>})<br></code></p>\n<p>I hate R as a language but its support for parallelism is quite good.</p>\n<p>[/quote]</p>\n<p>I'd love to perform these tasks in Python, but unfortunately it's a learning curve that I simply don't have in R (I've been using R since college and scripting in R for a good 5+ years).&nbsp;</p>\n<p>I'll look into parallelizing&nbsp;the process; my machine has just 2 cores, but a near 2x speedup is nothing to sneeze at.&nbsp;</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "62266",
      "postDate": "01/04/2015 19:53:16",
      "content": "<p>You can find an example in the gen_train.py script from cxxnet, e.g.</p>\n<p>https://github.com/antinucleon/cxxnet/blob/master/example/kaggle_bowl/gen_train.py</p>\n<p>for example</p>\n<p><code>convert -resize 48x48\\! infile outfile</code></p>\n<p>The gen_train script just calls imagemagick from python; it would be easy to create an R program or shell script that does the same.</p>\n\n<p><code>for name in /path/to/data/*.jpg; do<br> convert -resize 48x48\\! $name $name<br>done</code></p>\n<p>I've found cxxnet to be a good starting point for this problem.</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "62271",
      "postDate": "01/04/2015 21:42:42",
      "content": "<p>[quote=James King;62266]</p>\n<p>You can find an example in the gen_train.py script from cxxnet, e.g.</p>\n<p>https://github.com/antinucleon/cxxnet/blob/master/example/kaggle_bowl/gen_train.py</p>\n<p>for example</p>\n<p><code>convert -resize 48x48\\! infile outfile</code></p>\n<p>The gen_train script just calls imagemagick from python; it would be easy to create an R program or shell script that does the same.</p>\n<p><code>for name in /path/to/data/*.jpg; do<br> convert -resize 48x48\\! $name $name<br>done</code></p>\n<p>I've found cxxnet to be a good starting point for this problem.</p>\n<p>[/quote]</p>\n<p>This is definitely helpful; could be a 4x speedup or more. I've opted for 72 x 72 images :).&nbsp;</p>\n<p>Addendum: Looks like it's processing ~ 475 images per minute.&nbsp;</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "62330",
      "postDate": "01/06/2015 05:32:13",
      "content": "<p><code>for name in /path/to/data/*.jpg; do<br>&nbsp; &nbsp; convert -resize 48x48\\! $name $name<br>done</code></p>\n<p>This&nbsp;code breaks aspect ratio.</p>\n<p>I think the following code is even&nbsp;better.</p>\n<p><code>for name in /path/to/data/*.jpg; do<br>&nbsp; &nbsp; convert $name -resize 48x48 -gravity center -background white -extent 48x48 $name<br>done</code></p>\n<p><em>Warning: This example will overwrite the original dataset.</em></p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "62338",
      "postDate": "01/06/2015 11:54:45",
      "content": "<p>[quote=nagadomi;62330]</p>\n<p><code>for name in /path/to/data/*.jpg; do<br>&nbsp; &nbsp; convert -resize 48x48\\! $name $name<br>done</code></p>\n<p>This&nbsp;code breaks aspect ratio.</p>\n<p>I think the following code is even&nbsp;better.</p>\n<p><code>for name in /path/to/data/*.jpg; do<br>&nbsp; &nbsp; convert $name -resize 48x48 -gravity center -background white -extent 48x48 $name<br>done</code></p>\n<p><em>Warning: This example will overwrite the original dataset.</em></p>\n<p>[/quote]</p>\n<p>I have been doing something like this.&nbsp; The only issue I envisaged was that in the FAQs it states that due to the imaging method used the size of the objects of interest will be the same no matter the distance to the camera.&nbsp; I interpreted this to mean that &quot;size matters&quot;, i.e. the size of the object in the image could be an important discriminating factor.&nbsp; This method of resizing has the potential to change the relative sizes of different samples of the same object class if they appear in images of different sizes.&nbsp; I don't have a better solution though!</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "62357",
      "postDate": "01/06/2015 15:49:20",
      "content": "<p>the object in the image has too many deformations. (shift, rotation, scale, pose, ...)<br>a simple solution is to use data augmentation methods. (random shift, random rotate, random scale, ...)</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "62405",
      "postDate": "01/07/2015 16:03:57",
      "content": "<p>[quote=nagadomi;62330]</p>\n<p>This&nbsp;code breaks aspect ratio...</p>\n<p>[/quote]</p>\n<p>Indeed it does, and I would think preserving aspect ratio should obviously work better. But when I do so I can't get as good a score. Maybe my convnet is not configured correctly to handle the white space you get when you square a long thin image.</p>\n<p>Or maybe I need one network to handle thin images and another to handle approximately square images...</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "62408",
      "postDate": "01/07/2015 17:01:58",
      "content": "<p>[quote=James King;62405]</p>\n<p>[quote=nagadomi;62330]</p>\n<p>This&nbsp;code breaks aspect ratio...</p>\n<p>[/quote]</p>\n<p>Indeed it does, and I would think preserving aspect ratio should obviously work better. But when I do so I can't get as good a score. Maybe my convnet is not configured correctly to handle the white space you get when you square a long thin image.</p>\n<p>Or maybe I need one network to handle thin images and another to handle approximately square images...</p>\n<p>[/quote]</p>\n<p>I am inverting image brightness (background_color=0), it may be reason.</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "62435",
      "postDate": "01/08/2015 04:02:19",
      "content": "<p>[quote=James King;62405]</p>\n<p>[quote=nagadomi;62330]</p>\n<p>This&nbsp;code breaks aspect ratio...</p>\n<p>[/quote]</p>\n<p>Indeed it does, and I would think preserving aspect ratio should obviously work better. But when I do so I can't get as good a score. Maybe my convnet is not configured correctly to handle the white space you get when you square a long thin image.</p>\n<p>Or maybe I need one network to handle thin images and another to handle approximately square images...</p>\n<p>[/quote]</p>\n<p>Same thing happened on my side. Maybe Spatially-sparse CNN (https://www.kaggle.com/c/cifar-10/forums/t/10493/train-you-very-own-deep-convolutional-network/56310) can handle such padding more appropriately but I haven't tested it out.</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "62436",
      "postDate": "01/08/2015 04:11:21",
      "content": "<p>[quote=nagadomi;62408]</p>\n<p>[quote=James King;62405]</p>\n<p>[quote=nagadomi;62330]</p>\n<p>This&nbsp;code breaks aspect ratio...</p>\n<p>[/quote]</p>\n<p>Indeed it does, and I would think preserving aspect ratio should obviously work better. But when I do so I can't get as good a score. Maybe my convnet is not configured correctly to handle the white space you get when you square a long thin image.</p>\n<p>Or maybe I need one network to handle thin images and another to handle approximately square images...</p>\n<p>[/quote]</p>\n<p>I am inverting image brightness (background_color=0), it may be reason.</p>\n<p>[/quote]</p>\n<p>As we usually subtract out the mean image, inverting only causes a &quot;-&quot; sign to the input. The result should be similar if NN was able to learn negative weights.</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "62444",
      "postDate": "01/08/2015 06:01:55",
      "content": "<p>[quote=yr;62436]</p>\n<p>[quote=nagadomi;62408]</p>\n<p>[quote=James King;62405]</p>\n<p>[quote=nagadomi;62330]</p>\n<p>This&nbsp;code breaks aspect ratio...</p>\n<p>[/quote]</p>\n<p>Indeed it does, and I would think preserving aspect ratio should obviously work better. But when I do so I can't get as good a score. Maybe my convnet is not configured correctly to handle the white space you get when you square a long thin image.</p>\n<p>Or maybe I need one network to handle thin images and another to handle approximately square images...</p>\n<p>[/quote]</p>\n<p>I am inverting image brightness (background_color=0), it may be reason.</p>\n<p>[/quote]</p>\n<p>As we usually subtract out the mean image, inverting only causes a &quot;-&quot; sign to the input. The result should be similar if NN was able to learn negative weights.</p>\n<p>[/quote]</p>\n<p>oh you are right.</p>",
      "rawMarkdown": "",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 62261,
      "author_name": "jfkingiii",
      "author_url": "",
      "post_date": "01/04/2015 19:26:06",
      "content": "<p>I have been running the data transformations with imagemagick; I don't have an exact timing but it's orders of magnitude faster than the numbers you're quoting. &nbsp;</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 62262,
      "author_name": "paweljankiewicz",
      "author_url": "",
      "post_date": "01/04/2015 19:34:35",
      "content": "<p>You can try to run the conversions in parallel. There is a package multicore that has this handy function mclapply (multi core lapply).&nbsp;</p>\n<p><code>mclapply(list_of_images,FUN=function(img_path){</code></p>\n<p><code></code><code>convert_image(img_path)</code></p>\n<p><code>})<br></code></p>\n<p>I hate R as a language but its support for parallelism is quite good.</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 62263,
      "author_name": "jdr0317",
      "author_url": "",
      "post_date": "01/04/2015 19:46:55",
      "content": "<p>[quote=James King;62261]</p>\n<p>I have been running the data transformations with imagemagick; I don't have an exact timing but it's orders of magnitude faster than the numbers you're quoting. &nbsp;</p>\n<p>[/quote]</p>\n<p>Would you be able to provide an example shell/BASH script that performs this? I'm now trying to learn this :)</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 62264,
      "author_name": "jdr0317",
      "author_url": "",
      "post_date": "01/04/2015 19:50:33",
      "content": "<p>[quote=Pawe&#322;;62262]</p>\n<p>You can try to run the conversions in parallel. There is a package multicore that has this handy function mclapply (multi core lapply).&nbsp;</p>\n<p><code>mclapply(list_of_images,FUN=function(img_path){</code></p>\n<p><code></code><code>convert_image(img_path)</code></p>\n<p><code>})<br></code></p>\n<p>I hate R as a language but its support for parallelism is quite good.</p>\n<p>[/quote]</p>\n<p>I'd love to perform these tasks in Python, but unfortunately it's a learning curve that I simply don't have in R (I've been using R since college and scripting in R for a good 5+ years).&nbsp;</p>\n<p>I'll look into parallelizing&nbsp;the process; my machine has just 2 cores, but a near 2x speedup is nothing to sneeze at.&nbsp;</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 62266,
      "author_name": "jfkingiii",
      "author_url": "",
      "post_date": "01/04/2015 19:53:16",
      "content": "<p>You can find an example in the gen_train.py script from cxxnet, e.g.</p>\n<p>https://github.com/antinucleon/cxxnet/blob/master/example/kaggle_bowl/gen_train.py</p>\n<p>for example</p>\n<p><code>convert -resize 48x48\\! infile outfile</code></p>\n<p>The gen_train script just calls imagemagick from python; it would be easy to create an R program or shell script that does the same.</p>\n\n<p><code>for name in /path/to/data/*.jpg; do<br> convert -resize 48x48\\! $name $name<br>done</code></p>\n<p>I've found cxxnet to be a good starting point for this problem.</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 62271,
      "author_name": "jdr0317",
      "author_url": "",
      "post_date": "01/04/2015 21:42:42",
      "content": "<p>[quote=James King;62266]</p>\n<p>You can find an example in the gen_train.py script from cxxnet, e.g.</p>\n<p>https://github.com/antinucleon/cxxnet/blob/master/example/kaggle_bowl/gen_train.py</p>\n<p>for example</p>\n<p><code>convert -resize 48x48\\! infile outfile</code></p>\n<p>The gen_train script just calls imagemagick from python; it would be easy to create an R program or shell script that does the same.</p>\n<p><code>for name in /path/to/data/*.jpg; do<br> convert -resize 48x48\\! $name $name<br>done</code></p>\n<p>I've found cxxnet to be a good starting point for this problem.</p>\n<p>[/quote]</p>\n<p>This is definitely helpful; could be a 4x speedup or more. I've opted for 72 x 72 images :).&nbsp;</p>\n<p>Addendum: Looks like it's processing ~ 475 images per minute.&nbsp;</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 62330,
      "author_name": "nagadomi",
      "author_url": "",
      "post_date": "01/06/2015 05:32:13",
      "content": "<p><code>for name in /path/to/data/*.jpg; do<br>&nbsp; &nbsp; convert -resize 48x48\\! $name $name<br>done</code></p>\n<p>This&nbsp;code breaks aspect ratio.</p>\n<p>I think the following code is even&nbsp;better.</p>\n<p><code>for name in /path/to/data/*.jpg; do<br>&nbsp; &nbsp; convert $name -resize 48x48 -gravity center -background white -extent 48x48 $name<br>done</code></p>\n<p><em>Warning: This example will overwrite the original dataset.</em></p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 62338,
      "author_name": "senecaur",
      "author_url": "",
      "post_date": "01/06/2015 11:54:45",
      "content": "<p>[quote=nagadomi;62330]</p>\n<p><code>for name in /path/to/data/*.jpg; do<br>&nbsp; &nbsp; convert -resize 48x48\\! $name $name<br>done</code></p>\n<p>This&nbsp;code breaks aspect ratio.</p>\n<p>I think the following code is even&nbsp;better.</p>\n<p><code>for name in /path/to/data/*.jpg; do<br>&nbsp; &nbsp; convert $name -resize 48x48 -gravity center -background white -extent 48x48 $name<br>done</code></p>\n<p><em>Warning: This example will overwrite the original dataset.</em></p>\n<p>[/quote]</p>\n<p>I have been doing something like this.&nbsp; The only issue I envisaged was that in the FAQs it states that due to the imaging method used the size of the objects of interest will be the same no matter the distance to the camera.&nbsp; I interpreted this to mean that &quot;size matters&quot;, i.e. the size of the object in the image could be an important discriminating factor.&nbsp; This method of resizing has the potential to change the relative sizes of different samples of the same object class if they appear in images of different sizes.&nbsp; I don't have a better solution though!</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 62357,
      "author_name": "nagadomi",
      "author_url": "",
      "post_date": "01/06/2015 15:49:20",
      "content": "<p>the object in the image has too many deformations. (shift, rotation, scale, pose, ...)<br>a simple solution is to use data augmentation methods. (random shift, random rotate, random scale, ...)</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 62405,
      "author_name": "jfkingiii",
      "author_url": "",
      "post_date": "01/07/2015 16:03:57",
      "content": "<p>[quote=nagadomi;62330]</p>\n<p>This&nbsp;code breaks aspect ratio...</p>\n<p>[/quote]</p>\n<p>Indeed it does, and I would think preserving aspect ratio should obviously work better. But when I do so I can't get as good a score. Maybe my convnet is not configured correctly to handle the white space you get when you square a long thin image.</p>\n<p>Or maybe I need one network to handle thin images and another to handle approximately square images...</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 62408,
      "author_name": "nagadomi",
      "author_url": "",
      "post_date": "01/07/2015 17:01:58",
      "content": "<p>[quote=James King;62405]</p>\n<p>[quote=nagadomi;62330]</p>\n<p>This&nbsp;code breaks aspect ratio...</p>\n<p>[/quote]</p>\n<p>Indeed it does, and I would think preserving aspect ratio should obviously work better. But when I do so I can't get as good a score. Maybe my convnet is not configured correctly to handle the white space you get when you square a long thin image.</p>\n<p>Or maybe I need one network to handle thin images and another to handle approximately square images...</p>\n<p>[/quote]</p>\n<p>I am inverting image brightness (background_color=0), it may be reason.</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 62435,
      "author_name": "chenglongchen",
      "author_url": "",
      "post_date": "01/08/2015 04:02:19",
      "content": "<p>[quote=James King;62405]</p>\n<p>[quote=nagadomi;62330]</p>\n<p>This&nbsp;code breaks aspect ratio...</p>\n<p>[/quote]</p>\n<p>Indeed it does, and I would think preserving aspect ratio should obviously work better. But when I do so I can't get as good a score. Maybe my convnet is not configured correctly to handle the white space you get when you square a long thin image.</p>\n<p>Or maybe I need one network to handle thin images and another to handle approximately square images...</p>\n<p>[/quote]</p>\n<p>Same thing happened on my side. Maybe Spatially-sparse CNN (https://www.kaggle.com/c/cifar-10/forums/t/10493/train-you-very-own-deep-convolutional-network/56310) can handle such padding more appropriately but I haven't tested it out.</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 62436,
      "author_name": "chenglongchen",
      "author_url": "",
      "post_date": "01/08/2015 04:11:21",
      "content": "<p>[quote=nagadomi;62408]</p>\n<p>[quote=James King;62405]</p>\n<p>[quote=nagadomi;62330]</p>\n<p>This&nbsp;code breaks aspect ratio...</p>\n<p>[/quote]</p>\n<p>Indeed it does, and I would think preserving aspect ratio should obviously work better. But when I do so I can't get as good a score. Maybe my convnet is not configured correctly to handle the white space you get when you square a long thin image.</p>\n<p>Or maybe I need one network to handle thin images and another to handle approximately square images...</p>\n<p>[/quote]</p>\n<p>I am inverting image brightness (background_color=0), it may be reason.</p>\n<p>[/quote]</p>\n<p>As we usually subtract out the mean image, inverting only causes a &quot;-&quot; sign to the input. The result should be similar if NN was able to learn negative weights.</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 62444,
      "author_name": "nagadomi",
      "author_url": "",
      "post_date": "01/08/2015 06:01:55",
      "content": "<p>[quote=yr;62436]</p>\n<p>[quote=nagadomi;62408]</p>\n<p>[quote=James King;62405]</p>\n<p>[quote=nagadomi;62330]</p>\n<p>This&nbsp;code breaks aspect ratio...</p>\n<p>[/quote]</p>\n<p>Indeed it does, and I would think preserving aspect ratio should obviously work better. But when I do so I can't get as good a score. Maybe my convnet is not configured correctly to handle the white space you get when you square a long thin image.</p>\n<p>Or maybe I need one network to handle thin images and another to handle approximately square images...</p>\n<p>[/quote]</p>\n<p>I am inverting image brightness (background_color=0), it may be reason.</p>\n<p>[/quote]</p>\n<p>As we usually subtract out the mean image, inverting only causes a &quot;-&quot; sign to the input. The result should be similar if NN was able to learn negative weights.</p>\n<p>[/quote]</p>\n<p>oh you are right.</p>",
      "votes": null,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "62259": "",
    "62261": "",
    "62262": "",
    "62263": "",
    "62264": "",
    "62266": "",
    "62271": "",
    "62330": "",
    "62338": "",
    "62357": "",
    "62405": "",
    "62408": "",
    "62435": "",
    "62436": "",
    "62444": ""
  },
  "source": "meta"
}