{
  "id": 20218,
  "title": "MXNET & R",
  "url": "/competitions/state-farm-distracted-driver-detection/discussion/20218",
  "author_name": "",
  "post_date": "2016-04-18T11:05:31.840Z",
  "votes": 1,
  "comment_count": 7,
  "views": 1616,
  "content": "<p>Hi guys,</p>\n\n<p>I want to give a try on the MXNET package which seems quite promising, however I've trouble doing very basic operations.</p>\n\n<p>I've downloaded all the img from the challenge and as seen in a tuto I loaded them within the cimg format with the &quot;imager&quot; package.</p>\n\n<p>For now all the training set is filled in memory inside a list.</p>\n\n<p>Here is my simple question : how to cast this list into a format that can be taken as input with MXNET ?</p>\n\n<p>Thanks for any tips :)</p>",
  "messages": [
    {
      "id": "115357",
      "postDate": "04/18/2016 11:05:31",
      "content": "<p>Hi guys,</p>\n\n<p>I want to give a try on the MXNET package which seems quite promising, however I've trouble doing very basic operations.</p>\n\n<p>I've downloaded all the img from the challenge and as seen in a tuto I loaded them within the cimg format with the &quot;imager&quot; package.</p>\n\n<p>For now all the training set is filled in memory inside a list.</p>\n\n<p>Here is my simple question : how to cast this list into a format that can be taken as input with MXNET ?</p>\n\n<p>Thanks for any tips :)</p>",
      "rawMarkdown": "Hi guys,\r\n\r\nI want to give a try on the MXNET package which seems quite promising, however I've trouble doing very basic operations.\r\n\r\nI've downloaded all the img from the challenge and as seen in a tuto I loaded them within the cimg format with the \"imager\" package.\r\n\r\nFor now all the training set is filled in memory inside a list.\r\n\r\nHere is my simple question : how to cast this list into a format that can be taken as input with MXNET ?\r\n\r\nThanks for any tips :)",
      "votes": null
    },
    {
      "id": "117242",
      "postDate": "04/28/2016 03:29:25",
      "content": "<p>I am experimenting with mxnet too.  The docs are terrible! <br>\nType </p>\n\n<blockquote>\n  <p>?mx.model.FeedForward.create</p>\n</blockquote>\n\n<p>in your console and you will see &quot;X    mx.io.DataIter or R array/matrix The training data.&quot;</p>\n\n<p>So change your X inputs from a list to a matrix.  </p>\n\n<p>At least that worked for me but haven't been getting good results at all during the actual model.  Any pointers?  How did you construct your list?</p>",
      "rawMarkdown": "I am experimenting with mxnet too.  The docs are terrible!  \r\nType \r\n\r\n> ?mx.model.FeedForward.create\r\n\r\n in your console and you will see \"X\tmx.io.DataIter or R array/matrix The training data.\"\r\n\r\nSo change your X inputs from a list to a matrix.  \r\n\r\nAt least that worked for me but haven't been getting good results at all during the actual model.  Any pointers?  How did you construct your list?",
      "votes": null
    },
    {
      "id": "117290",
      "postDate": "04/28/2016 11:29:16",
      "content": "<p>Hi mate, when testing I did the following :</p>\n\n<pre><code>train_img = vector(&quot;list&quot; , length(22424))\ni = 1\nfor (f in file_name) {\n  im = load.image(paste0(src_rep, &quot;train/&quot;, f))\n  im = resize(im, 64, 48)\n  train_img[[i]] = im\n  i = i+1\n}\n</code></pre>\n\n<p>Simply looping over files and adding them into a list. Each element is resized a cimg [64,48,1,3]</p>\n\n<p>Then I did the following :</p>\n\n<pre><code>train_gray = sapply(train_img, grayscale)\n</code></pre>\n\n<p>This grayscale the cimg object and convert into a vector. Note as this is important : <strong>while doing this you lose the 2D structure of the picture</strong> (which is terrible) but it allowed me to train an XGBoost model and a MLP with MXNET (but a crappy one with around 20% accuracy on a validation set).</p>\n\n<p>I have seen the mx.io.DataIter() function however <strong>what is the format of the dataset you pass to this function</strong> ?</p>\n\n<ul>\n<li>Passing a vector of pixels it works fine (but as written above : it is bad)</li>\n<li>Passing an array of dimension [64,48,1,22424] seems to work (the function doesn't rise any error) however I don't think it's the way to do it since the last dimension should be the number of channels and not the number of pictures.</li>\n<li>Passing an array of dimension [64,48,1,1,22424] works too but again I did'nt test it (yet) in the neural net.</li>\n</ul>\n\n<p>Either way, thanks for input.</p>",
      "rawMarkdown": "Hi mate, when testing I did the following :\r\n\r\n    train_img = vector(\"list\" , length(22424))\r\n    i = 1\r\n    for (f in file_name) {\r\n      im = load.image(paste0(src_rep, \"train/\", f))\r\n      im = resize(im, 64, 48)\r\n      train_img[[i]] = im\r\n      i = i+1\r\n    }\r\n\r\nSimply looping over files and adding them into a list. Each element is resized a cimg [64,48,1,3]\r\n\r\nThen I did the following :\r\n\r\n    train_gray = sapply(train_img, grayscale)\r\n\r\nThis grayscale the cimg object and convert into a vector. Note as this is important : **while doing this you lose the 2D structure of the picture** (which is terrible) but it allowed me to train an XGBoost model and a MLP with MXNET (but a crappy one with around 20% accuracy on a validation set).\r\n\r\nI have seen the mx.io.DataIter() function however **what is the format of the dataset you pass to this function** ?\r\n\r\n - Passing a vector of pixels it works fine (but as written above : it is bad)\r\n - Passing an array of dimension [64,48,1,22424] seems to work (the function doesn't rise any error) however I don't think it's the way to do it since the last dimension should be the number of channels and not the number of pictures.\r\n - Passing an array of dimension [64,48,1,1,22424] works too but again I did'nt test it (yet) in the neural net.\r\n\r\nEither way, thanks for input.",
      "votes": null
    },
    {
      "id": "117319",
      "postDate": "04/28/2016 14:05:12",
      "content": "<p>Cool.  My model wasn't even doing that well :(</p>\n\n<p>I could only find <code>mx.io.DataIter</code> in the mxnet python docs so its not all that helpful.  For some reason I didn't load a loop.  I used pbapply functions and originally had different dimensions then added a repeated class vector for each of the classes.</p>\n\n<pre><code>img.preproc&lt;-function(x){\n  x&lt;-resize(x, 64,48) #resize\n  x&lt;-grayscale(x) #make b/w\n  x&lt;-as.vector(x)\n  return(x)\n}\nsetwd(&quot;~/state_farm/new/c0&quot;)\ntemp &lt;- list.files(pattern=&quot;*.jpg&quot;)\npath&lt;-getwd()\npaths&lt;-paste0(path,&quot;/&quot;,temp)\ntr.images&lt;-pblapply(paths[1:500],load.image) #only first 500\ntr.images&lt;-pblapply(tr.images,img.preproc)\ntr.images&lt;-do.call(rbind,tr.images)\n</code></pre>\n\n<p>After tediously getting the images into a matrixI ran the standard nn from the hand written number tutorial.  No luck. didn't get any lift. ah well I will keep at it and if I find a useful piece of info will post it.  Cheers!</p>",
      "rawMarkdown": "Cool.  My model wasn't even doing that well :(\r\n\r\nI could only find `mx.io.DataIter` in the mxnet python docs so its not all that helpful.  For some reason I didn't load a loop.  I used pbapply functions and originally had different dimensions then added a repeated class vector for each of the classes.\r\n\r\n    img.preproc<-function(x){\r\n      x<-resize(x, 64,48) #resize\r\n      x<-grayscale(x) #make b/w\r\n      x<-as.vector(x)\r\n      return(x)\r\n    }\r\n    setwd(\"~/state_farm/new/c0\")\r\n    temp <- list.files(pattern=\"*.jpg\")\r\n    path<-getwd()\r\n    paths<-paste0(path,\"/\",temp)\r\n    tr.images<-pblapply(paths[1:500],load.image) #only first 500\r\n    tr.images<-pblapply(tr.images,img.preproc)\r\n    tr.images<-do.call(rbind,tr.images)\r\nAfter tediously getting the images into a matrixI ran the standard nn from the hand written number tutorial.  No luck. didn't get any lift. ah well I will keep at it and if I find a useful piece of info will post it.  Cheers!",
      "votes": null
    },
    {
      "id": "118730",
      "postDate": "05/05/2016 01:14:47",
      "content": "<p>Did you guys figure out how to iterate from a file? I've had some success with mx.io.CSVIter. MXNET is very sensitive to data shape. It seems to prefer observations in columns instead of rows. Anyway, here is some code for creating csv files and reading with mx.io.CSVIter.</p>\n\n<pre><code>write.table(t(train.x), file = &quot;data/train_images.csv&quot;, row.names = FALSE, col.names = FALSE, sep = &quot;,&quot;)\nwrite.table(train.y, file = &quot;data/train_labels.csv&quot;, row.names = FALSE, col.names = FALSE, sep = &quot;,&quot;)\n\ndtrain = mx.io.CSVIter(\n  data.csv=&quot;data/train_images.csv&quot;,\n  label.csv=&quot;data/train_labels.csv&quot;,\n  data.shape=c(1024),\n  label.shape=c(1),\n  batch.size=100,\n  shuffle=TRUE,\n  flat=TRUE,\n  silent=0,\n  seed=10)\n</code></pre>",
      "rawMarkdown": "Did you guys figure out how to iterate from a file? I've had some success with mx.io.CSVIter. MXNET is very sensitive to data shape. It seems to prefer observations in columns instead of rows. Anyway, here is some code for creating csv files and reading with mx.io.CSVIter.\r\n\r\n    write.table(t(train.x), file = \"data/train_images.csv\", row.names = FALSE, col.names = FALSE, sep = \",\")\r\n    write.table(train.y, file = \"data/train_labels.csv\", row.names = FALSE, col.names = FALSE, sep = \",\")\r\n\r\n    dtrain = mx.io.CSVIter(\r\n      data.csv=\"data/train_images.csv\",\r\n      label.csv=\"data/train_labels.csv\",\r\n      data.shape=c(1024),\r\n      label.shape=c(1),\r\n      batch.size=100,\r\n      shuffle=TRUE,\r\n      flat=TRUE,\r\n      silent=0,\r\n      seed=10)",
      "votes": null
    },
    {
      "id": "119204",
      "postDate": "05/08/2016 01:54:15",
      "content": "<p>Hi JeffH</p>\n\n<p>Thanks for the hints.</p>\n\n<p>Wondering why data.shape = c(1024)? :)</p>\n\n<p>Thanks.</p>",
      "rawMarkdown": "Hi JeffH\r\n\r\nThanks for the hints.\r\n\r\nWondering why data.shape = c(1024)? :)\r\n\r\nThanks.",
      "votes": null
    },
    {
      "id": "119284",
      "postDate": "05/08/2016 20:30:30",
      "content": "<p>Ha ha, this was from a different project. I am just getting started with this data. You do not need to include the number of images in the dimension. Actually, I think that will cause problems. </p>\n\n<p>I think data.shape [64,48,1,22424] should actually be [64,48,1]. That indicates that each row contains a matrix of 64x48 pixels 1 pixel deep. If you add that last dimension, I believe MXnet will expect each observation to include 22424 arrays of dimension 64x48x1. </p>",
      "rawMarkdown": "Ha ha, this was from a different project. I am just getting started with this data. You do not need to include the number of images in the dimension. Actually, I think that will cause problems. \r\n\r\nI think data.shape [64,48,1,22424] should actually be [64,48,1]. That indicates that each row contains a matrix of 64x48 pixels 1 pixel deep. If you add that last dimension, I believe MXnet will expect each observation to include 22424 arrays of dimension 64x48x1.",
      "votes": null
    },
    {
      "id": "119303",
      "postDate": "05/09/2016 01:25:45",
      "content": "<p>Thanks JeffH. \nYou were right, I had problem with  [64,48,1,22424] . Haven't tried [64,48,1]. I didn't quite get it as I already transpose the data to column wise (per image) but still had issue with  [64,48,1,22424]. Using [22424] only does work for CSVIter but not ideal. Will let you know if [64,48,1 works :)</p>",
      "rawMarkdown": "Thanks JeffH. \r\nYou were right, I had problem with  [64,48,1,22424] . Haven't tried [64,48,1]. I didn't quite get it as I already transpose the data to column wise (per image) but still had issue with  [64,48,1,22424]. Using [22424] only does work for CSVIter but not ideal. Will let you know if [64,48,1 works :)",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 117242,
      "author_name": "kwartler",
      "author_url": "",
      "post_date": "04/28/2016 03:29:25",
      "content": "<p>I am experimenting with mxnet too.  The docs are terrible! <br>\nType </p>\n\n<blockquote>\n  <p>?mx.model.FeedForward.create</p>\n</blockquote>\n\n<p>in your console and you will see &quot;X    mx.io.DataIter or R array/matrix The training data.&quot;</p>\n\n<p>So change your X inputs from a list to a matrix.  </p>\n\n<p>At least that worked for me but haven't been getting good results at all during the actual model.  Any pointers?  How did you construct your list?</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 117290,
      "author_name": "laroumagne",
      "author_url": "",
      "post_date": "04/28/2016 11:29:16",
      "content": "<p>Hi mate, when testing I did the following :</p>\n\n<pre><code>train_img = vector(&quot;list&quot; , length(22424))\ni = 1\nfor (f in file_name) {\n  im = load.image(paste0(src_rep, &quot;train/&quot;, f))\n  im = resize(im, 64, 48)\n  train_img[[i]] = im\n  i = i+1\n}\n</code></pre>\n\n<p>Simply looping over files and adding them into a list. Each element is resized a cimg [64,48,1,3]</p>\n\n<p>Then I did the following :</p>\n\n<pre><code>train_gray = sapply(train_img, grayscale)\n</code></pre>\n\n<p>This grayscale the cimg object and convert into a vector. Note as this is important : <strong>while doing this you lose the 2D structure of the picture</strong> (which is terrible) but it allowed me to train an XGBoost model and a MLP with MXNET (but a crappy one with around 20% accuracy on a validation set).</p>\n\n<p>I have seen the mx.io.DataIter() function however <strong>what is the format of the dataset you pass to this function</strong> ?</p>\n\n<ul>\n<li>Passing a vector of pixels it works fine (but as written above : it is bad)</li>\n<li>Passing an array of dimension [64,48,1,22424] seems to work (the function doesn't rise any error) however I don't think it's the way to do it since the last dimension should be the number of channels and not the number of pictures.</li>\n<li>Passing an array of dimension [64,48,1,1,22424] works too but again I did'nt test it (yet) in the neural net.</li>\n</ul>\n\n<p>Either way, thanks for input.</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 117319,
      "author_name": "kwartler",
      "author_url": "",
      "post_date": "04/28/2016 14:05:12",
      "content": "<p>Cool.  My model wasn't even doing that well :(</p>\n\n<p>I could only find <code>mx.io.DataIter</code> in the mxnet python docs so its not all that helpful.  For some reason I didn't load a loop.  I used pbapply functions and originally had different dimensions then added a repeated class vector for each of the classes.</p>\n\n<pre><code>img.preproc&lt;-function(x){\n  x&lt;-resize(x, 64,48) #resize\n  x&lt;-grayscale(x) #make b/w\n  x&lt;-as.vector(x)\n  return(x)\n}\nsetwd(&quot;~/state_farm/new/c0&quot;)\ntemp &lt;- list.files(pattern=&quot;*.jpg&quot;)\npath&lt;-getwd()\npaths&lt;-paste0(path,&quot;/&quot;,temp)\ntr.images&lt;-pblapply(paths[1:500],load.image) #only first 500\ntr.images&lt;-pblapply(tr.images,img.preproc)\ntr.images&lt;-do.call(rbind,tr.images)\n</code></pre>\n\n<p>After tediously getting the images into a matrixI ran the standard nn from the hand written number tutorial.  No luck. didn't get any lift. ah well I will keep at it and if I find a useful piece of info will post it.  Cheers!</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 118730,
      "author_name": "jeffhebert",
      "author_url": "",
      "post_date": "05/05/2016 01:14:47",
      "content": "<p>Did you guys figure out how to iterate from a file? I've had some success with mx.io.CSVIter. MXNET is very sensitive to data shape. It seems to prefer observations in columns instead of rows. Anyway, here is some code for creating csv files and reading with mx.io.CSVIter.</p>\n\n<pre><code>write.table(t(train.x), file = &quot;data/train_images.csv&quot;, row.names = FALSE, col.names = FALSE, sep = &quot;,&quot;)\nwrite.table(train.y, file = &quot;data/train_labels.csv&quot;, row.names = FALSE, col.names = FALSE, sep = &quot;,&quot;)\n\ndtrain = mx.io.CSVIter(\n  data.csv=&quot;data/train_images.csv&quot;,\n  label.csv=&quot;data/train_labels.csv&quot;,\n  data.shape=c(1024),\n  label.shape=c(1),\n  batch.size=100,\n  shuffle=TRUE,\n  flat=TRUE,\n  silent=0,\n  seed=10)\n</code></pre>",
      "votes": null,
      "replies": []
    },
    {
      "id": 119204,
      "author_name": "",
      "author_url": "",
      "post_date": "05/08/2016 01:54:15",
      "content": "<p>Hi JeffH</p>\n\n<p>Thanks for the hints.</p>\n\n<p>Wondering why data.shape = c(1024)? :)</p>\n\n<p>Thanks.</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 119284,
      "author_name": "jeffhebert",
      "author_url": "",
      "post_date": "05/08/2016 20:30:30",
      "content": "<p>Ha ha, this was from a different project. I am just getting started with this data. You do not need to include the number of images in the dimension. Actually, I think that will cause problems. </p>\n\n<p>I think data.shape [64,48,1,22424] should actually be [64,48,1]. That indicates that each row contains a matrix of 64x48 pixels 1 pixel deep. If you add that last dimension, I believe MXnet will expect each observation to include 22424 arrays of dimension 64x48x1. </p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 119303,
      "author_name": "",
      "author_url": "",
      "post_date": "05/09/2016 01:25:45",
      "content": "<p>Thanks JeffH. \nYou were right, I had problem with  [64,48,1,22424] . Haven't tried [64,48,1]. I didn't quite get it as I already transpose the data to column wise (per image) but still had issue with  [64,48,1,22424]. Using [22424] only does work for CSVIter but not ideal. Will let you know if [64,48,1 works :)</p>",
      "votes": null,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "115357": "Hi guys,\r\n\r\nI want to give a try on the MXNET package which seems quite promising, however I've trouble doing very basic operations.\r\n\r\nI've downloaded all the img from the challenge and as seen in a tuto I loaded them within the cimg format with the \"imager\" package.\r\n\r\nFor now all the training set is filled in memory inside a list.\r\n\r\nHere is my simple question : how to cast this list into a format that can be taken as input with MXNET ?\r\n\r\nThanks for any tips :)",
    "117242": "I am experimenting with mxnet too.  The docs are terrible!  \r\nType \r\n\r\n> ?mx.model.FeedForward.create\r\n\r\n in your console and you will see \"X\tmx.io.DataIter or R array/matrix The training data.\"\r\n\r\nSo change your X inputs from a list to a matrix.  \r\n\r\nAt least that worked for me but haven't been getting good results at all during the actual model.  Any pointers?  How did you construct your list?",
    "117290": "Hi mate, when testing I did the following :\r\n\r\n    train_img = vector(\"list\" , length(22424))\r\n    i = 1\r\n    for (f in file_name) {\r\n      im = load.image(paste0(src_rep, \"train/\", f))\r\n      im = resize(im, 64, 48)\r\n      train_img[[i]] = im\r\n      i = i+1\r\n    }\r\n\r\nSimply looping over files and adding them into a list. Each element is resized a cimg [64,48,1,3]\r\n\r\nThen I did the following :\r\n\r\n    train_gray = sapply(train_img, grayscale)\r\n\r\nThis grayscale the cimg object and convert into a vector. Note as this is important : **while doing this you lose the 2D structure of the picture** (which is terrible) but it allowed me to train an XGBoost model and a MLP with MXNET (but a crappy one with around 20% accuracy on a validation set).\r\n\r\nI have seen the mx.io.DataIter() function however **what is the format of the dataset you pass to this function** ?\r\n\r\n - Passing a vector of pixels it works fine (but as written above : it is bad)\r\n - Passing an array of dimension [64,48,1,22424] seems to work (the function doesn't rise any error) however I don't think it's the way to do it since the last dimension should be the number of channels and not the number of pictures.\r\n - Passing an array of dimension [64,48,1,1,22424] works too but again I did'nt test it (yet) in the neural net.\r\n\r\nEither way, thanks for input.",
    "117319": "Cool.  My model wasn't even doing that well :(\r\n\r\nI could only find `mx.io.DataIter` in the mxnet python docs so its not all that helpful.  For some reason I didn't load a loop.  I used pbapply functions and originally had different dimensions then added a repeated class vector for each of the classes.\r\n\r\n    img.preproc<-function(x){\r\n      x<-resize(x, 64,48) #resize\r\n      x<-grayscale(x) #make b/w\r\n      x<-as.vector(x)\r\n      return(x)\r\n    }\r\n    setwd(\"~/state_farm/new/c0\")\r\n    temp <- list.files(pattern=\"*.jpg\")\r\n    path<-getwd()\r\n    paths<-paste0(path,\"/\",temp)\r\n    tr.images<-pblapply(paths[1:500],load.image) #only first 500\r\n    tr.images<-pblapply(tr.images,img.preproc)\r\n    tr.images<-do.call(rbind,tr.images)\r\nAfter tediously getting the images into a matrixI ran the standard nn from the hand written number tutorial.  No luck. didn't get any lift. ah well I will keep at it and if I find a useful piece of info will post it.  Cheers!",
    "118730": "Did you guys figure out how to iterate from a file? I've had some success with mx.io.CSVIter. MXNET is very sensitive to data shape. It seems to prefer observations in columns instead of rows. Anyway, here is some code for creating csv files and reading with mx.io.CSVIter.\r\n\r\n    write.table(t(train.x), file = \"data/train_images.csv\", row.names = FALSE, col.names = FALSE, sep = \",\")\r\n    write.table(train.y, file = \"data/train_labels.csv\", row.names = FALSE, col.names = FALSE, sep = \",\")\r\n\r\n    dtrain = mx.io.CSVIter(\r\n      data.csv=\"data/train_images.csv\",\r\n      label.csv=\"data/train_labels.csv\",\r\n      data.shape=c(1024),\r\n      label.shape=c(1),\r\n      batch.size=100,\r\n      shuffle=TRUE,\r\n      flat=TRUE,\r\n      silent=0,\r\n      seed=10)",
    "119204": "Hi JeffH\r\n\r\nThanks for the hints.\r\n\r\nWondering why data.shape = c(1024)? :)\r\n\r\nThanks.",
    "119284": "Ha ha, this was from a different project. I am just getting started with this data. You do not need to include the number of images in the dimension. Actually, I think that will cause problems. \r\n\r\nI think data.shape [64,48,1,22424] should actually be [64,48,1]. That indicates that each row contains a matrix of 64x48 pixels 1 pixel deep. If you add that last dimension, I believe MXnet will expect each observation to include 22424 arrays of dimension 64x48x1.",
    "119303": "Thanks JeffH. \r\nYou were right, I had problem with  [64,48,1,22424] . Haven't tried [64,48,1]. I didn't quite get it as I already transpose the data to column wise (per image) but still had issue with  [64,48,1,22424]. Using [22424] only does work for CSVIter but not ideal. Will let you know if [64,48,1 works :)"
  },
  "source": "meta"
}