Answers.Body: <p>This verges on &quot;programming bug&quot; which is not discussed on this site but there's a salient point which likely should be made here.  Without knowing a lot of details, I'll take a guess.</p>
  <p>The model is likely too large for an embedded system.  Especially when we train forests we often train the individual trees to node purity which can be relatively large.</p>
  <p>Attempt to regularize your model (e.g. max depth) and see if you can bring the model size/executable size down.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-12-01T02:02:01.203Z
Answers.LastActivityDate: 2024-12-01T02:02:01.203Z
Answers.UserDisplayName: foreverska
Answers.Body: <p>The emlearn documentation provides some information on <a href="https://emlearn.readthedocs.io/en/latest/tree_based_models.html" rel="nofollow noreferrer">how to optimize tree-based models</a>. The first thing you should do is to reduce the number of trees, and the depth of the trees. The documentation also links example code for how to do this kind of hyper-parameter optimization. It is often possible to reduce from 100 (scikit-learn default) to 10 trees, without much/any reduction in predictive performance. Combined with depth regularization, it is often possible to achieve 100x reduction in model size.</p>
  <p>PS: You should also measure the compiled size and not the text size of the code. The model/code when compiled will generally be considerably smaller than the .c text size.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-12-24T16:56:16.150Z
Answers.LastActivityDate: 2024-12-24T16:56:16.150Z
Answers.UserDisplayName: Jon Nordby
Body: <p>i converted my machine learning model (random forest regressor) into c code using 'emlearn' library but the size of .c file is 3.8 MB which is incompatible for microcontrollers. so i want my c code in KBs what should i do?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-11-30T15:59:58.623Z
Comments.Text: My POV as a computer engineer student about microcontroller is just a data acquisitor, not for the actual computation. It's about buffering real physical data into logical data, and then transfer it to the more advanced computer such as cloud computer through WiFi interface. Why would you perform ML in microcontroller?
Comments.UserDisplayName: Muhammad Ikhwan Perwira
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-12-01T01:46:55.163Z
Comments.Text: @MuhammadIkhwanPerwira How about speed of inference?  Going to the cloud might be much slower than performing the inference locally.  Further, in embedded, one is still not guaranteed internet access at all times.
Comments.UserDisplayName: foreverska
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-12-01T02:24:44.777Z
Comments.Text: @foreverska WiFi interface doesn't have to online, just transfer data into local advance computer, such as Raspi. For the speed inference, well it's basically the same thing when you perform inference with limited KB ram and tradeoff between maximizing MCU to perform data buffering as data bridge.
Comments.UserDisplayName: Muhammad Ikhwan Perwira
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-12-01T03:21:46.983Z
Comments.Text: If one has bigger compute and the time budget to offload data to it, that would certainly be the way to do it.  But reasonably sized microcontrollers can provide faster inference locally, with less power draw and cheaper.  Not every project has unit prices that support a wireless enabled chip but may still want to perform inference from a pre-trained model.
Comments.UserDisplayName: foreverska
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-12-01T06:33:18.320Z
Comments.Text: @foreverska This is why I love Espressif MCU series, it's not high specs neither low specs with low cost. Raspi and AVR on other side are too high and low, respectively. ESP32-S3 supports tensorflow capability if you are still insisting to run model in edge-computing manner instead of cloud-computing.
Comments.UserDisplayName: Muhammad Ikhwan Perwira
Tags: <machine-learning><decision-trees><scikit-learn><random-forests><c>
Title: integrating machine learning models on microcontroller
UserDisplayName: Sakshi Sutar
ThreadText: # integrating machine learning models on microcontroller
  
  (Asked by: username_0 on 2024-11-30)
  
  i converted my machine learning model (random forest regressor) into c code using 'emlearn' library but the size of .c file is 3.8 MB which is incompatible for microcontrollers. so i want my c code in KBs what should i do?
  
  **Tags:** machine-learning, decision-trees, scikit-learn, random-forests, c
  > **Comments:**
  > **username_1**: My POV as a computer engineer student about microcontroller is just a data acquisitor, not for the actual computation. It's about buffering real physical data into logical data, and then transfer it to the more advanced computer such as cloud computer through WiFi interface. Why would you perform ML in microcontroller?  
  > **username_2**: @MuhammadIkhwanPerwira How about speed of inference?  Going to the cloud might be much slower than performing the inference locally.  Further, in embedded, one is still not guaranteed internet access at all times.  
  > **username_1**: @foreverska WiFi interface doesn't have to online, just transfer data into local advance computer, such as Raspi. For the speed inference, well it's basically the same thing when you perform inference with limited KB ram and tradeoff between maximizing MCU to perform data buffering as data bridge.  
  > **username_2**: If one has bigger compute and the time budget to offload data to it, that would certainly be the way to do it.  But reasonably sized microcontrollers can provide faster inference locally, with less power draw and cheaper.  Not every project has unit prices that support a wireless enabled chip but may still want to perform inference from a pre-trained model.  
  > **username_1**: @foreverska This is why I love Espressif MCU series, it's not high specs neither low specs with low cost. Raspi and AVR on other side are too high and low, respectively. ESP32-S3 supports tensorflow capability if you are still insisting to run model in edge-computing manner instead of cloud-computing.  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2024-12-01. Score: 2
  
  This verges on "programming bug" which is not discussed on this site but there's a salient point which likely should be made here. Without knowing a lot of details, I'll take a guess.
  
  The model is likely too large for an embedded system. Especially when we train forests we often train the individual trees to node purity which can be relatively large.
  
  Attempt to regularize your model (e.g. max depth) and see if you can bring the model size/executable size down.
  
  
  ---
  
  ### Answer by username_3 on 2024-12-24. Score: 0
  
  The emlearn documentation provides some information on [how to optimize tree-based models](https://emlearn.readthedocs.io/en/latest/tree_based_models.html). The first thing you should do is to reduce the number of trees, and the depth of the trees. The documentation also links example code for how to do this kind of hyper-parameter optimization. It is often possible to reduce from 100 (scikit-learn default) to 10 trees, without much/any reduction in predictive performance. Combined with depth regularization, it is often possible to achieve 100x reduction in model size.
  
  PS: You should also measure the compiled size and not the text size of the code. The model/code when compiled will generally be considerably smaller than the .c text size.
  
  
  ---
  
  