Every time I open my lab drawer and look at my little collection of microcontrollers, I feel a strange mix of affection and mild panic. There it sits: the trusty ESP32. It costs about the price of a couple of cups of tea, fits neatly on a breadboard, and has wired half the DIY projects in my dorm room.
Yet, people talk about Machine Learning as if it belongs exclusively in air conditioned server racks humming with water-cooled GPUs, or trapped inside massive cloud architectures chewing through gigawatts of power. Asking if machine learning is possible on a simple ESP32 feels a bit like asking if you can run a Formula 1 engine inside a rickshaw.
Spoiler alert: You totally can, but you might need to take off the doors and leave your luggage behind.
Let us dive right into why running AI on the edge, affectionately known as TinyML, is one of the most exciting things you can hack together on a Saturday afternoon when you should probably be studying algorithms.
Why on Earth Would You Want AI on an ESP32?
Let us be honest. If you are building a cloud-connected smart home gadget, streaming data to a server is easy. Your ESP32 reads a temperature sensor, fires an HTTP POST request, and your Node.js backend handles the heavy lifting. So why bother stuffing a neural network onto a chip with roughly the computing power of a vintage graphing calculator?
Latency, privacy, and connectivity.
Imagine you are building a smart agriculture sensor to detect soil stress in a remote rural field where mobile data is flaky at best. If your sensor has to stream twenty audio samples of insect activity or continuous vibration readings to the cloud every second to check for pests, your battery will die before sunset, and your SIM card bill will give you nightmares.
If the ESP32 can run a lightweight classification model locally on the chip, it only wakes up, checks the data, sighs, realizes there is a beetle eating your tomatoes, and sends a single tiny alert message. No cloud round-trips. No waiting for a handshake. Just pure, instant edge intelligence happening right there on the silicon.
And honestly? It is just deeply satisfying to watch a twenty-cent microcontroller make a smart decision without asking the internet for permission first.
Meeting the Silicon: Understanding Your Hardware Limits
Before we start throwing tensors at our poor little ESP32, we need to understand what we are actually working with. Let us look under the hood of a standard ESP32-WROOM-32 module.
You get a dual-core Tensilica Xtensa 32-bit LX6 microprocessor running at 160 or 240 MHz. You have got about 520 KB of internal SRAM. And depending on your board variant, you might have 4 MB or 8 MB of external Flash memory.

Now, compare that to your smartphone. Your phone has gigabytes of RAM and processors that could cook an egg if they tried hard enough. Your ESP32 has a fraction of a megabyte of RAM. That means if you try to load a standard ResNet-50 image classification model onto it, your chip is going to run out of memory faster than a CSE student during a dead-of-night code review right before the semester project deadline.
| ESP32 Hardware Constraints |
|
| Clock Speed |
160 MHz - 240 MHz (Dual Core) |
| SRAM |
~520 KB (Your actual working memory) |
| Flash Memory |
4 MB - 8 MB (Where your code & model live) |
| Power Consumption |
Low (Deep sleep draws microamps) |
RAM is your absolute bottleneck. Every weight, every bias, every activation layer during inference needs to sit comfortably inside that tiny SRAM window. If your model footprint exceeds your available RAM, your program simply crashes or throws an allocation error.
This is why traditional machine learning libraries like standard PyTorch or heavyweight TensorFlow will not compile or run here. We need specialized tools designed from the ground up for resource-constrained hardware. Enter the world of TinyML.
The Magic Trick: How TinyML Makes the Impossible Possible
How do we fit a neural network into a space smaller than a high-resolution JPEG? We cheat. Or rather, we use clever mathematics to shrink models down until they fit into the micro-universe of a microcontroller.
The secret sauce involves a few core techniques:
Quantization: Normally, machine learning models store their weights as 32-bit floating-point numbers. That is a lot of precision. But do we really need decimal places out to the seventh digit to figure out if an audio clip sounds like a clap or a cough? Absolutely not. Quantization converts those 32-bit floats into 8-bit integers. It slashes your model size by up to seventy-five percent while barely making a noticeable dent in your prediction accuracy.
Pruning: During training, neural networks often build complex webs of connections, many of which turn out to be practically useless. Pruning goes through the network and snips out weights that stay close to zero, effectively sparsifying the model so it requires fewer calculations.
Architecture Design: Models like MobileNet or custom micro-architectures are built specifically to have fewer parameters while maintaining high feature extraction capabilities.
Google’s TensorFlow Lite for Microcontrollers (TFLite Micro) is the framework that brings all of this together. It is an interpreter written in pure C++ with zero dynamic memory allocation after initialization, meaning it won't trigger memory fragmentation leaks on your embedded device. It does not require an operating system, and it can run comfortably inside bare-metal microcontroller environments.
What Kinds of Machine Learning Actually Work on an ESP32?
If you are expecting your ESP32 to run a local large language model to write poetry with you, you are going to be severely disappointed. But if you keep your ambitions realistic and focused, you can build genuinely cool applications.
1. Sensor Data and Anomaly Detection
Microcontrollers live in the physical world. They read accelerometers, gyroscopes, temperature probes, gas sensors, and current meters. You can train a tiny model to recognize normal operational patterns in a motor or a factory line. When the vibration signature shifts because a bearing is wearing out, the ESP32 spots the anomaly locally and triggers an immediate shutdown warning.
2. Audio Recognition and Keyword Spotting
With an inexpensive I2S microphone like the INMP441 hooked up to your ESP32, you can build your own offline wake-word detector. It will not understand complex sentences, but it can easily distinguish between background room noise and specific trigger words like a sharp clap, a whistle, or a distinct spoken command.
3. Low-Resolution Computer Vision (ESP32-CAM)
If you step up to an ESP32-CAM module, which includes an OV2640 camera sensor, you can start experimenting with ultra-low-resolution image classification. We are not talking about 4K streaming here. We are usually talking about tiny grayscale matrices like 96x96 pixels. That is enough to run simple face detection, count objects on a moving conveyor belt, or check whether a room light was left on.
Step-by-Step: Building Your First Edge AI Project
Let us walk through the workflow of how you actually get a machine learning model running on an ESP32. We will use the Arduino IDE or ESP-IDF, whichever floats your boat, combined with Python on your PC for the training phase.
Step 1: Train Your Model on a Real Computer
Never try to train a machine learning model on an ESP32. That is like trying to build a skyscraper using a plastic spoon. You use Python, TensorFlow, or Keras on your laptop or a cloud notebook.
Keep your architecture lean. Two or three dense layers or a very small convolutional network are usually plenty for embedded tasks. Train your model using your dataset, evaluate its accuracy, and make sure it performs well.
Step 2: Convert and Quantize
Once your model is trained and saved as a .h5 or SavedModel file, you pass it through the TensorFlow Lite converter in Python:
import tensorflow as tf
# Convert the model to TensorFlow Lite with integer quantization
converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir)
converter.optimizations = [tf.Optimize.DEFAULT]
# Representative dataset generator for full integer quantization
def representative_data_gen():
for input_value in dataset:
yield [input_value]
converter.representative_dataset = representative_data_gen
tflite_model = converter.convert()
# Save the model to disk
with open('model.tflite', 'wb') as f:
f.write(tflite_model)
Step 3: Turn Your Model into a C Header File
Microcontrollers do not load files from a hard drive the way operating systems do. We need to embed our trained model directly into our C++ firmware code as a byte array.
You can use command-line tools like xxd on Linux/macOS or simple Python scripts to convert your .tflite file into a C header file:
xxd -i model.tflite > model.h
This generates a massive C array containing your model weights and architecture graph as raw hex bytes.
Step 4: Write the Inference Firmware
Now, open your Arduino IDE or ESP-IDF workspace, include your generated header file, and write the inference loop using the TFLite Micro library.
#include
#include "tensorflow/lite/micro/all_ops_resolver.h"
#include "tensorflow/lite/micro/micro_interpreter.h"
#include "tensorflow/lite/schema/schema_generated.h"
#include "model.h"
// Globals used for TFLite
tflite::ErrorReporter* error_reporter = nullptr;
const tflite::Model* model = nullptr;
tflite::MicroInterpreter* interpreter = nullptr;
constexpr int kTensorArenaSize = 10 * 1024;
uint8_t tensor_arena[kTensorArenaSize];
void setup() {
Serial.begin(115200);
// Load the model from the C header array
model = tflite::GetModel(model_tflite);
if (model->version() != TFLITE_SCHEMA_VERSION) {
Serial.println("Model schema mismatch!");
return;
}
// Set up an op resolver
static tflite::AllOpsResolver resolver;
// Instantiate interpreter
static tflite::MicroInterpreter static_interpreter(
model, resolver, tensor_arena, kTensorArenaSize, error_reporter);
interpreter = &static_interpreter;
// Allocate memory for model tensors
interpreter->AllocateTensors();
}
void loop() {
// 1. Read data from your sensors
float* input_tensor_buffer = interpreter->input(0)->data.f;
input_tensor_buffer[0] = readMySensorData();
// 2. Run inference
if (interpreter->Invoke() != kTfLiteOk) {
Serial.println("Inference failed!");
return;
}
// 3. Read the output prediction
float* output_tensor_buffer = interpreter->output(0)->data.f;
float prediction = output_tensor_buffer[0];
if (prediction > 0.85) {
Serial.println("Anomaly detected!");
}
delay(1000);
}
Upload that code to your ESP32, open the serial monitor, and watch your little chip run neural network inference in real-time. It is genuinely magical the first time you see those classification scores scrolling across your terminal.
Common Pitfalls and How to Avoid Them
If you jump straight into building your first edge AI project without a roadmap, you are going to run into a few classic walls. Save yourself the headache and keep these tips in mind:
Watch Your Tensor Arena Size: The tensor arena is the chunk of RAM you allocate for intermediate calculations and tensor activations. If you make it too small, TFLite will throw an allocation error. If you make it too large, your ESP32 will run out of heap memory and crash before setup() even finishes. Finding that sweet spot takes a bit of trial and error.
Beware of Floating-Point Overhead: The standard ESP32 does have hardware support for floating-point operations on some variants, but heavy math still eats up clock cycles. Whenever possible, use fixed-point or 8-bit quantized models to keep execution snappy.
Keep Debugging Simple: You do not have a graphical debugger attached to your microcontroller in most hobbyist setups. Lean heavily on your serial output, blink onboard LEDs to indicate execution checkpoints, and test your preprocessing pipeline thoroughly on your PC before flashing code to the board.
The Future Belongs to the Edge
So, is machine learning possible on a simple ESP32? Absolutely. It is not just possible; it is fast, affordable, and opening up an entirely new dimension of smart embedded systems that do not rely on constant cloud connectivity.
We are living in an era where intelligence is no longer locked away behind expensive enterprise APIs or massive data centers. With a few dollars worth of hardware, an open-source toolchain, and a bit of patience, you can embed neural networks into everyday objects sitting right on your desk.
What kind of weird, smart, or slightly over-engineered device are you going to build next? Drop a comment below, or better yet, fire up your IDE and go flash some firmware. Happy hacking!
যখনই আমি আমার ল্যাব ড্রয়ারটা খুলি আর মাইক্রোকন্ট্রোলারের ছোট্ট কালেকশনটার দিকে তাকাই, তখন আমার মধ্যে কেমন যেন একটা নস্টালজিয়া আর হালকা টেনশন কাজ করে। ঠিক মাঝখানে বসে থাকে আমাদের চেনা ইএসপি৩২ (ESP32)। একটা কফির কাপের দামের সমান এটার দাম, ব্রেডবোর্ডে খুব সুন্দরভাবে ফিট হয়ে যায়, আর আমার রুমের অর্ধেক প্রজেক্ট এটার মাধ্যমেই করা।
তবুও, মানুষ এমনভাবে মেশিন লার্নিং নিয়ে কথা বলে যেন এটা শুধু এয়ারকন্ডিশনড সার্ভার র্যাকের জন্যই বরাদ্দ, যেগুলো ওয়াটার-কুলড জিপিইউর গুঞ্জন তোলে কিংবা বিশাল সব ক্লাউড আর্কিটেকচারের পেটে ঢুকে গিগাওয়াট বিদ্যুৎ আর লিটার লিটার পানি গিলে আর চিবিয়ে খায়। একটা সাধারণ ইএসপি৩২-এ মেশিন লার্নিং চালানো সম্ভব কি না, এই প্রশ্ন করাটা অনেকটা একটা রিকশার মধ্যে ফর্মুলা ওয়ান ইঞ্জিন ফিট করার চেষ্টার মতো শোনায়। (করতে পারলে দেখার মতো জিনিস হইতো তাইনা?)
স্পয়লার অ্যালার্ট: আপনি আসলেই এটা করতে পারেন, তবে অটোরিকশার যন্ত্রনায় ঢাকায় টেকা যখন মুশকিল, আপনি তাদের হাতে এফ-ওয়ান এঞ্জিন দেয়া আর নিজের মাথায় নিজে মুগুরপেটা করা একই কথা হবে।
চলুন, সোজা দ ডুব দেওয়া যাক যে কেন এজ-এআই (Edge AI) বা এআই অন দ্য এজ(Edge) রান করা, যাকে খুব আদর করে টাইনিএমেল (TinyML) বলা হয়, শুক্রবার বিকেলে হ্যাক করার মতো সবচেয়ে এক্সাইটিং জিনিসগুলোর একটা, বিশেষ করে যখন আপনার আসলে বসে পড়াশোনা করা উচিত ছিল।
ইএসপি৩২-এ এআই আনতে যাবেন কেন?
একদম সত্যি কথা বলতে কি, আপনি যদি এমন কোনো ক্লাউড-কানেক্টেড স্মার্ট হোম গ্যাজেট বানান, যেখানে সার্ভারে ডেটা স্ট্রিম করা পানির মতো সহজ। আপনার ইএসপি৩২ একটা টেম্পারেচার সেন্সর রিড করবে, একটা এইচটিটিপি পোস্ট (HTTP POST) রিকোয়েস্ট পাঠাবে, আর আপনার নোডজেএস (Node.js) ব্যাকএন্ড বাকি কাজ সামলে নেবে। তাহলে ভিন্টেজ ক্যালকুলেটরের লেভেলের প্রসেসিং পাওয়ারওয়ালা একটা চিপের ভেতর জাস্ট জোর করে একটা নিউরাল নেটওয়ার্ক ঢুকাতে যাবেন কেন?
ল্যাটেন্সি, প্রাইভেসি আর কানেক্টিভিটির জন্য।
ধরুন আপনি এমন একটা স্মার্ট এগ্রিকালচার সেন্সর বানাচ্ছেন যা কোনো রিমোট গ্রামের ক্ষেতে মাটির স্ট্রেস ডিটেক্ট করবে, যেখানে মোবাইল ইন্টারনেট এমনিতেই ভীষণ আনস্টেবল। আপনার সেন্সর যদি পোকামাকড় ধরার জন্য প্রতি সেকেন্ডে পোকার শব্দের বিশটা অডিও স্যাম্পল বা কন্টিনিউয়াস ভাইব্রেশন রিডিং ক্লাউড সার্ভারে পাঠাতে থাকে, তাহলে সূর্য ডোবার আগেই আপনার ব্যাটারি ডেড হয়ে যাবে আর আপনার সিম কার্ডের বিল দেখে রাতে ঘুম আসবে না।
এর বদলে ইএসপি৩২ যদি চিপের ভেতর লোকালি একটা লাইটওয়েট ক্লাসিফিকেশন মডেল রান করতে পারেন, তবে এটা শুধু ঘুম থেকে উঠবে, ডেটা চেক করবে, একটা দীর্ঘশ্বাস ফেলবে, বুঝবে যে একটা পোকা এসে আপনার টমেটো খাচ্ছে, আর সঙ্গে সঙ্গে একটা ছোট্ট অ্যালার্ট মেসেজ পাঠিয়ে দেবে। কোনো ক্লাউড রাউন্ড-ট্রিপ লাগবে না। কোনো হ্যান্ডশেকের অপেক্ষাও করতে হবে না। একদম পিওর, ইনস্ট্যান্ট এজ ইন্টেলিজেন্স সিলিকনের ওপরেই কাজ করবে।
আর সত্যি বলতে কী? ইন্টারনেট বা সার্ভারের কাছে পারমিশন চাওয়ার অপেক্ষা না করে একটা পাঁচশো টাকার মাইক্রোকন্ট্রোলার নিজে নিজে একটা স্মার্ট ডিসিশন নিচ্ছে, এটা দেখতেই তো এক অন্যরকম স্যাটিসফ্যাকশন কাজ করে।
সিলিকনের সাথে পরিচয়: আপনার হার্ডওয়্যার লিমিটগুলো বুঝুন
আমাদের বেচারা ছোটখাটো ইএসপি৩২-এর ওপর Tensor চাপানোর আগে আমাদের বোঝা দরকার যে আমরা আসলে কী নিয়ে কাজ করছি। একটা স্ট্যান্ডার্ড ESP32-WROOM-32 মডিউলের ভেতরটা একটু ঘুরে দেখা যাক।



এখানে আপনি পাচ্ছেন একটা ডুয়াল-কোর Tensilica Xtensa 32-bit LX6 মাইক্রোপ্রসেসর, আপনি সরাসরি এটা দেখতে পাবেন না, এটা ESP32 চিপের মাঝে SoC বা সিস্টেম অন চিপ অবস্থায় আছে, যা ১৬০ বা ২৪০ মেগাহার্জে রান করে। আপনার কাছে আছে প্রায় ৫২০ কিলোবাইট ইন্টারনাল এস্র্যাম (SRAM)। আর আপনার বোর্ডের ভ্যারিয়েন্ট অনুযায়ী ৪ মেগাবাইট বা ৮ মেগাবাইট এক্সটার্নাল ফ্লাশ মেমোরি পেতে পারেন, যেটাকে আপনি বাম পাশে দেখতে পাবেন, ৮টা পিন ওয়ালা আইসি।
এবার এটার সাথে আপনার স্মার্টফোনের তুলনা করুন। আপনার ফোনে গিগাবাইট গিগাবাইট র্যাম আছে আর এমন প্রসেসর আছে যা দিয়ে চাইলে ডিম ভাজা যায়। আর আপনার ইএসপি৩২-এর আছে এক মেগাবাইটেরও কম র্যাম। এর মানে হলো আপনি যদি এতে স্ট্যান্ডার্ড কোনো রেজনেট-৫০ (ResNet-50) ইমেজ ক্লাসিফিকেশন মডেল লোড করতে যান, তবে সেটার মেমোরি ফুরিয়ে যাবে CSE স্টুডেন্টের সেমিস্টার প্রজেক্টের ডেডলাইনের রাতের অ্যাসাইনমেন্টের সময় এক্সপায়ার হবার থেকেও দ্রুত গতিতে।
| ESP32 হার্ডওয়্যার |
|
| ক্লক স্পিড |
১৬০ মেগাহার্জ - ২৪০ মেগাহার্জ (ডুয়াল কোর) |
| এস্র্যাম (SRAM) |
প্রায় ৫২০ কিলোবাইট (আপনার আসল ওয়ার্কিং মেমোরি) |
| ফ্লাশ মেমোরি |
৪ মেগাবাইট - ৮ মেগাবাইট (যেখানে আপনার কোড এবং মডেল থাকে) |
| পাওয়ার কনজাম্পশন |
লো (ডিপ স্লিপ মোডে মাইক্রোঅ্যাম্পিয়ার টানে) |
র্যাম হলো আপনার মেইন বটলনেক বা বাধা। এআই ইনফারেন্সের সময় প্রতিটা ওয়েট, প্রতিটা বায়াস এবং প্রতিটা অ্যাক্টিভেশন লেয়ারকে ওই ছোট্ট এস্র্যাম উইন্ডোর ভেতর আরাম করে বসে থাকতে হবে। আপনার মডেলের সাইজ যদি অ্যাভেইলেবল র্যামকে ছাড়িয়ে যায়, তবে আপনার প্রোগ্রাম ক্র্যাশ করবে অথবা সরাসরি অ্যালোকেশন এরর দিয়ে বসে থাকবে।
এই কারণেই স্ট্যান্ডার্ড পাইটর্চ (PyTorch) বা হেভিওয়েট টেনসরফ্লোর মতো ট্র্যাডিশনাল মেশিন লার্নিং লাইব্রেরিগুলো এখানে কাজ করবে না বা কম্পাইলই হবে না। এমন কম র্যাম, কম প্রসেসরের রিসোর্স-কনস্ট্রেইন্ড হার্ডওয়্যারের জন্য একদম নিচ থেকে ডিজাইন করা স্পেশাল টুলস আমাদের লাগবে। আর এখানেই এন্ট্রি নেয় টাইনিএমএল-এর (TinyML)-এর জগত।
ম্যাজিক ট্রিক: টিনিএমএল যেভাবে অসম্ভবকে সম্ভব করে
একটা হাই-রেজোলিউশন ছবির থেকেও ছোট জায়গার ভেতর একটা নিউরাল নেটওয়ার্ক কীভাবে ফিট করবেন? আমরা একটা চিটিং করি এই যায়গায়, বা বলা ভালো, আমরা দারুণ কিছু গণিত বা অ্যালগরিদম ব্যবহার করে মডেলগুলোকে ছোট করে ফেলি যাতে সেগুলো একটা মাইক্রোকন্ট্রোলারের মাইক্রো-মেমরিতে ফিট হয়ে যায়।
এর পেছনে রয়েছে কিছু কোর টেকনিক:
কোয়ান্টাইজেশন (Quantization): নরমালি, মেশিন লার্নিং মডেলগুলো তাদের ওয়েটগুলোকে ৩২-বিট ফ্লটিং-পয়েন্ট নম্বর হিসেবে রাখে। এটা অনেক বড় একটা প্রিসিশন, ৩২ বিটের নম্বর মানে অনেক লম্বা একটা নাম্বার। কিন্তু কোনো অডিও ক্লিপ শুনে সেটা তালি নাকি কাশির শব্দ, সেটা বের করার জন্য কি আমাদের দশমিকের পর সাত ঘর পর্যন্ত নিখুঁত ডেটা দরকার? একদমই না। কোয়ান্টাইজেশন ওই ৩২-বিট ফ্লোট নাম্বারগুলোকে ৮-বিট রাউন্ট ইন্টিজার নাম্বারে কনভার্ট করে ফেলে। এটা আপনার মডেলের সাইজ প্রায় ৭৫ পার্সেন্ট পর্যন্ত কমিয়ে দেয় এবং প্রেডিকশনের অ্যাক্যুরেসিতেও খুব একটা বড় প্রভাব ফেলে না।
প্রুনিং (Pruning): ট্রেনিংয়ের সময় নিউরাল নেটওয়ার্কগুলো প্রায়ই কানেকশনের একটা জটিল জাল তৈরি করে, যার মধ্যে একটা বড় অংশই কাজের কাজ কিচ্ছু করে না। প্রুনিং এই নেটওয়ার্কের ভেতর দিয়ে গিয়ে সেই ওয়েটগুলোকে ছেঁটে ফেলে যেগুলো প্রায় শূন্যের কাছাকাছি থাকে, ফলে মডেলটা অনেক বেশি হালকা হয়ে যায় এবং কম ক্যালকুলেশনের দরকার হয়।
আর্কিটেকচার ডিজাইন: মোবাইলনেট (MobileNet) বা কাস্টম মাইক্রো-আর্কিটেকচারের মতো মডেলগুলো বিশেষভাবে তৈরি করা হয় যাতে হাই ফিচার এক্সট্রাকশন ক্যাপাবিলিটি ঠিক রেখে প্যারামিটার অনেক কম রাখা যায়।
গুগলের টেনসরফ্লো লাইট ফর মাইক্রোকন্ট্রোলার্স (TFLite Micro) হলো সেই ফ্রেমওয়ার্ক যা এই পুরো জিনিসটাকে এক সুতোয় বাঁধে। এটি পিওর সি প্লাস প্লাস (C++) এ লেখা একটা ইন্টারপ্রেটার যার ইনিশিয়ালাইজেশনের পর কোনো ডায়নামিক মেমোরি অ্যালোকেশন হয় না, যার মানে হলো এটা আপনার ছোটো ডিভাইসে কোনো মেমোরি ফ্র্যাগমেন্টেশন লিক করবে না। এটার জন্য কোনো অপারেটিং সিস্টেমের দরকার হয় না এবং এটা খুব আরামসে বেয়ার-মেটাল মাইক্রোকন্ট্রোলার এনভায়রনমেন্টে রান করতে পারে।
ইএসপি৩২-এ আসলে কোন ধরণের মেশিন লার্নিং কাজ করে?
আপনি যদি আশা করেন যে আপনার ইএসপি৩২ আপনার সাথে লোকালি কোনো লার্জ ল্যাঙ্গুয়েজ মডেল, যেমন Gemma অথবা GLM রান করে কবিতা লিখবে, তবে আপনি মারাত্মকভাবে হতাশ হবেন। কিন্তু আপনি যদি আপনার চাহিদাগুলো রিয়েলিস্টিক এবং ফোকাসড রাখেন, তাহলে বেশ দারুণ কিছু অ্যাপ্লিকেশন বানাতে পারবেন।
১. সেন্সর ডেটা এবং অ্যানোমালি ডিটেকশন
মাইক্রোকন্ট্রোলারগুলো ফিজিক্যাল ওয়ার্ল্ড বা বাস্তবের সাথে মিশে থাকে। এরা অ্যাকসেলেরোমিটার, জাইরোস্কোপ, টেম্পারেচার প্রোব, গ্যাস সেন্সর এবং কারেন্ট মিটার রিড করে। আপনি একটা ছোট্ট মডেল ট্রেন করতে পারেন যা কোনো মোটর বা ফ্যাক্টরি লাইনের নরমাল অপারেশনাল প্যাটার্ন চিনে রাখতে পারে। যখন বিয়ারিং ক্ষয়ে যাওয়ার কারণে ভাইব্রেশন সিগনেচার বদলে যাবে, তখন ইএসপি৩২ লোকালি সেই অ্যানোমালি বা গড়মিল ধরে ফেলবে এবং সাথে সাথে শাটডাউন ওয়ার্নিং দিয়ে দেবে।
২. অডিও রেকগনিশন এবং কিওয়ার্ড স্পটিং
আপনার ইএসপি৩২-এর সাথে (INMP441)-এর মতো সস্তা একটা আইটুএস (I2S) মাইক্রোফোন কানেক্ট করে আপনি নিজের একটা অফলাইন ওয়েক ওয়ার্ড ডিটেক্টর বানিয়ে ফেলতে পারবেন। এটা খুব জটিল বাক্য বুঝতে পারবে না, কিন্তু ব্যাকগ্রাউন্ড রুম নয়েজ আর নির্দিষ্ট কিছু ট্রিগার ওয়ার্ড যেমন একটা শার্প তালি, বাঁশির আওয়াজ বা ক্লিয়ার কোনো স্পোকেন কমান্ড খুব সহজেই আলাদা করতে পারবে।
৩. লো-রেজোলিউশন কম্পিউটার ভিশন (ESP32-CAM)
আপনি যদি একটু আপডেট করে একটা ইএসপি৩২-ক্যাম (ESP32-CAM) মডিউলে যান, যেটায় ওভি২৬৪০ (OV2640) ক্যামেরা সেন্সর থাকে, তবে আপনি আল্ট্রা-লো-রেজোলিউশন ইমেজ ক্লাসিফিকেশন নিয়ে এক্সপেরিমেন্ট শুরু করতে পারবেন। এখানে আমরা ফোরকে স্ট্রিমিং নিয়ে কথা বলছি না। আমরা সাধারণত ৯৬ বাই ৯৬ পিক্সেলের মতো ছোট্ট গ্রেস্কেল ম্যাট্রিক্স নিয়ে কথা বলি। সিম্পল ফেস ডিটেকশন, চলমান কনভেয়র বেল্টে অবজেক্ট গোনা বা রুমের লাইট জ্বালানো অবস্থায় আছে কি না তা চেক করার জন্য এটুকুই এনাফ।
স্টেপ-বাই-স্টেপ: আপনার ফার্স্ট এজ এআই প্রজেক্ট বানানো
চলুন ওয়ার্কফ্লোটার ওপর দিয়ে একবার চোখ বুলিয়ে নেওয়া যাক যে কীভাবে আপনি আসলে একটা মেশিন লার্নিং মডেল ইএসপি৩২-এ রান করাবেন। আমরা ট্রেনিং ফেজের জন্য পিসিতে পাইথনের সাথে কম্বাইন করে আরডুইনো আইডিই (Arduino IDE) বা ইএসপি-আইডিএফ (ESP-IDF) ব্যবহার করব, যেটা আপনার সুবিধা হয়।
স্টেপ ১: রিয়েল কম্পিউটারে আপনার মডেল ট্রেন করুন
কখনোই ইএসপি৩২-এর ওপর মেশিন লার্নিং মডেল ট্রেন করার চেষ্টা করবেন না। এটা অনেকটা প্লাস্টিকের চামচ দিয়ে স্কাইস্ক্র্যাপার বা বহুতল ভবন বানানোর চেষ্টার মতো। আপনার ল্যাপটপ বা ক্লাউড নোটবুকে পাইথন, টেনসরফ্লো বা কেরাস (Keras) ব্যবহার করবেন।
আপনার আর্কিটেকচার হালকা-পাতলা রাখবেন। দুই-তিনটে ডেন্স লেয়ার বা একটা খুব ছোট কনভোলিউশনাল নেটওয়ার্কই এমবেডেড কাজের জন্য এনাফ। আপনার ডেটাসেট দিয়ে মডেলটি ট্রেন করুন, এর অ্যাক্যুরেসি ইভালুয়েট করুন এবং নিশ্চিত করুন যে এটি ঠিকঠাক কাজ করছে।
স্টেপ ২: কনভার্ট এবং কোয়ান্টাইজ করুন
আপনার মডেল যখন ট্রেন করা শেষ এবং .h5 বা সেভড-মডেল ফাইল হিসেবে সেভ করা হয়ে যাবে, তখন পাইথনে টেনসরফ্লো লাইট কনভার্টারের ভেতর দিয়ে এটাকে পাস করাবেন:
import tensorflow as tf
# কনভার্ট দ্য মডেল টু টেনসরফ্লো লাইট উইথ ইন্টিজার কোয়ান্টাইজেশন
converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir)
converter.optimizations = [tf.Optimize.DEFAULT]
# রিপ্রেজেন্টেটিভ ডেটাসেট জেনারেটর ফর ফুল ইন্টিজার কোয়ান্টাইজেশন
def representative_data_gen():
for input_value in dataset:
yield [input_value]
converter.representative_dataset = representative_data_gen
tflite_model = converter.convert()
# সেভ দ্য মডেল টু ডিস্ক
with open('model.tflite', 'wb') as f:
f.write(tflite_model)
স্টেপ ৩: আপনার মডেলকে একটা সি হেডার ফাইলে রূপান্তর করুন
অপারেটিং সিস্টেম যেভাবে হার্ড ড্রাইভ থেকে ফাইল লোড করে, মাইক্রোকন্ট্রোলার সেভাবে ফাইল লোড করে না। আমাদের ট্রেন করা মডেলটিকে সরাসরি আমাদের সি প্লাস প্লাস (C++) ফার্মওয়্যার কোডের ভেতরে একটা বাইট অ্যারে হিসেবে এমবেড করতে হবে।
লিনাক্স বা ম্যাকওএসে xxd এর মতো কমান্ড-লাইন টুলস অথবা সিম্পল পাইথন স্ক্রিপ্ট ব্যবহার করে আপনার .tflite ফাইলটিকে একটা সি হেডার ফাইলে কনভার্ট করে নিতে পারেন:
xxd -i model.tflite > model.h
এটা আপনার মডেলের ওয়েট এবং আর্কিটেকচার গ্রাফের র হেক্স বাইট সম্বলিত একটা বিশাল সি অ্যারে জেনারেট করে দেবে।
স্টেপ ৪: ইনফারেন্স ফার্মওয়্যার লিখুন
এবার আপনার আরডুইনো আইডিই বা ইএসপি-আইডিএফ ওয়ার্কস্পেস ওপেন করুন, জেনারেট করা হেডার ফাইলটি ইনক্লুড করুন এবং টিফ্লাইট মাইক্রো (TFLite Micro) লাইব্রেরি ব্যবহার করে ইনফারেন্স লুপটি লিখে ফেলুন।
#include
#include "tensorflow/lite/micro/all_ops_resolver.h"
#include "tensorflow/lite/micro/micro_interpreter.h"
#include "tensorflow/lite/schema/schema_generated.h"
#include "model.h"
// গ্লোবালস ইউজড ফর টিফ্লাইট
tflite::ErrorReporter* error_reporter = nullptr;
const tflite::Model* model = nullptr;
tflite::MicroInterpreter* interpreter = nullptr;
constexpr int kTensorArenaSize = 10 * 1024;
uint8_t tensor_arena[kTensorArenaSize];
void setup() {
Serial.begin(115200);
// লোড দ্য মডেল ফ্রম দ্য সি হেডার অ্যারে
model = tflite::GetModel(model_tflite);
if (model->version() != TFLITE_SCHEMA_VERSION) {
Serial.println("মডেল স্কিমা মিসম্যাচ!");
return;
}
// সেট আপ অ্যান অপ রিজলভার
static tflite::AllOpsResolver resolver;
// ইন্সট্যান্টিয়েট ইন্টারপ্রেটার
static tflite::MicroInterpreter static_interpreter(
model, resolver, tensor_arena, kTensorArenaSize, error_reporter);
interpreter = &static_interpreter;
// অ্যালোকেট মেমোরি ফর মডেল টেনসরস
interpreter->AllocateTensors();
}
void loop() {
// ১. রিড ডেটা ফ্রম ইউর সেন্সরস্
float* input_tensor_buffer = interpreter->input(0)->data.f;
input_tensor_buffer[0] = readMySensorData();
// ২. রান ইনফারেন্স
if (interpreter->Invoke() != kTfLiteOk) {
Serial.println("ইনফারেন্স ফেইলড!");
return;
}
// ৩. রিড দ্য আউটপুট প্রেডিকশন
float* output_tensor_buffer = interpreter->output(0)->data.f;
float prediction = output_tensor_buffer[0];
if (prediction > 0.85) {
Serial.println("অ্যানোমালি ডিটেক্টেড!");
}
delay(1000);
}
এই কোডটি আপনার ইএসপি৩২-এ আপলোড করুন, সিরিয়াল মনিটর ওপেন করুন এবং রিয়েল-টাইমে আপনার ছোট চিপটিকে নিউরাল নেটওয়ার্ক ইনফারেন্স রান করতে দেখুন। টার্মিনালে যখন ফার্স্ট টাইম ওই ক্লাসিফিকেশন স্কোরগুলো স্ক্রোল করতে দেখবেন, তখন এটা দেখতে সত্যিই জাদুর মতো মনে হবে।
কমন পিটফলস এবং এগুলো যেভাবে এড়িয়ে চলবেন
আপনি যদি কোনো রোডম্যাপ ছাড়া সরাসরি আপনার ফার্স্ট এজ এআই প্রজেক্ট বানাতে নেমে পড়েন, তবে আপনি বেশ কিছু ক্লাসিক দেয়ালে গিয়ে ধাক্কা খাবেন। মাথা ব্যথা থেকে বাঁচতে এই টিপসগুলো মাথায় রাখবেন:
আপনার টেনসর এরিনা সাইজের দিকে নজর রাখুন: টেনসর এরিনা হলো র্যামের সেই অংশ যা আপনি ইন্টারমিডিয়েট ক্যালকুলেশন এবং টেনসর অ্যাক্টিভেশনের জন্য অ্যালোকেট করেন। আপনি যদি এটা খুব ছোট করে ফেলেন, তবে TFLite একটা অ্যালোকেশন এরর থ্রো করবে। আর যদি খুব বড় করে ফেলেন, তবে setup() রান হওয়ার আগেই আপনার ইএসপি৩২ এর হিপ মেমোরি ফুরিয়ে ক্র্যাশ করবে। এই সুইট স্পটটা খুঁজে বের করতে একটু ট্রায়াল অ্যান্ড এরর করতে হবে।
ফ্লটিং-পয়েন্ট ওভারহেড নিয়ে সাবধান থাকুন: স্ট্যান্ডার্ড ইএসপি৩২-এর কিছু ভ্যারিয়েন্টে ফ্লটিং-পয়েন্ট অপারেশনের জন্য হার্ডওয়্যার সাপোর্ট থাকে, তা সত্ত্বেও হেভি ম্যাথ বেশ ভালোই ক্লক সাইকেল খেয়ে ফেলে। যখনই সম্ভব, এক্সিকিউশন ফাস্ট রাখার জন্য ফিক্সড-পয়েন্ট বা ৮-বিট কোয়ান্টাইজড মডেল ব্যবহার করবেন।
ডিবাগিং সিম্পল রাখুন: বেশিরভাগ হবিইস্ট সেটআপে আপনার মাইক্রোকন্ট্রোলারের সাথে কোনো গ্রাফিক্যাল ডিবাগার কানেক্ট করা থাকে না। সিরিয়াল আউটপুটের ওপর ভরসা রাখুন, এক্সিকিউশন চেকপয়েন্ট বোঝানোর জন্য অনবোর্ড এলইডি ব্লিংক করান এবং বোর্ডে কোড ফ্ল্যাশ করার আগে পিসিতে আপনার প্রিপ্রসেসিং পাইপলাইন ভালোভাবে টেস্ট করে নিন।
ফিউচার বিলংস টু দ্য এজ
তাহলে, একটা সিম্পল ইএসপি৩২-এ কি মেশিন লার্নিং পসিবল? ডেফিনিটলি পসিবল। এটা শুধু পসিবলই না; এটা ফাস্ট, সাশ্রয়ী এবং স্মার্ট এমবেডেড সিস্টেমের এক নতুন ডাইমেনশন ওপেন করে দিচ্ছে যেগুলোর জন্য কন্টিনিউয়াস ক্লাউড কানেক্টিভিটির ওপর ডিপেন্ড করে থাকতে হয় না।
আমরা এমন একটা যুগে বাস করছি যেখানে ইন্টেলিজেন্স বা বুদ্ধিমত্তা আর দামি এন্টারপ্রাইজ এপিআই বা বিশাল সব ডেটা সেন্টারের বন্দি খাঁচায় আটকে নেই। কয়েক ডলারের হার্ডওয়্যার, একটা ওপেন-সোর্স টুলচেইন আর একটুখানি ধৈর্য নিয়ে আপনি আপনার ডেস্কে বসে থাকা সাধারণ সব জিনিসের ভেতরেই নিউরাল নেটওয়ার্ক এমবেড করে দিতে পারবেন।
পরবর্তীতে আপনি কী ধরণের অদ্ভুত, স্মার্ট বা একটু বেশি ওভার-ইঞ্জিনিয়ার্ড ডিভাইস বানাতে যাচ্ছেন? নিচে একটা কমেন্ট করে ফেলুন, কিংবা তার চেয়েও ভালো হয় যদি আপনি এখনই আপনার আইডিই ওপেন করে কিছু ফার্মওয়্যার ফ্ল্যাশ করে ফেলেন। হ্যাপি হ্যাকিং!
Apu, I'm so much interested in Machine Learning, but I'm just starting, ami kivabe shuru korle easy hobe shekhata?