Micropython Machine Learning Quickstart

Micropython Machine Learning Quickstart cover

Python is my personal favorite programming language. It is clean, concise and powerful.

Being able to run Python (either MicroPython or CircuitPython) on microcontrollers is such a joyful experience.

Starting from today, we have a new feature to joy for: Machine Learning.

Most TinyML work and projects so far have been developed in C/C++, which is a natual choice since it is math-heavy and C/C++ is the fastest language in terms of execution time.

Nonetheless, not all TinyML projects require microseconds execution time and many tasks can easily handle a bit of slowdown.

Thanks to the everywhereml Python package we can easily port a growing list of classifiers to MicroPython with a single line of code.

Let's see it in action.

Python script

The following script will train a Random Forest classifier on the IRIS toy dataset.

Start by installing the everywhereml library.

pip install everywhereml
1

Then use the following code to train the model.

from everywhereml.sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split

X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)

clf = RandomForestClassifier(n_estimators=7, max_leaf_nodes=20)
clf.fit(X_train, y_train)

print('Score: %.2f' % clf.score(X_test, y_test))
>>> Score: 0.96
1 2 3 4 5 6 7 8 9 10 11 12

To port the classifier to MicroPython, just run the following line.

print(clf.to_micropython())
1

This single line will generate valid MicroPython code that you can integrate in your project. Here's what it looks like.

See source

try:
    from time import ticks_us, ticks_diff
except ImportError:
    from time import time_ns

    def ticks_us(): return int(time_ns() * 1000)
    def ticks_diff(a, b): return a - b

class RandomForestClassifier:
    """
    # RandomForestClassifier(base_estimator=deprecated, bootstrap=True, ccp_alpha=0.0, class_name=RandomForestClassifier, class_weight=None, criterion=gini, estimator=DecisionTreeClassifier(), estimator_params=('criterion', 'max_depth', 'min_samples_split', 'min_samples_leaf', 'min_weight_fraction_leaf', 'max_features', 'max_leaf_nodes', 'min_impurity_decrease', 'random_state', 'ccp_alpha'), max_depth=None, max_features=sqrt, max_leaf_nodes=20, max_samples=None, min_impurity_decrease=0.0, min_samples_leaf=1, min_samples_split=2, min_weight_fraction_leaf=0.0, n_estimators=7, n_jobs=None, num_outputs=3, oob_score=False, package_name=everywhereml.sklearn.ensemble, random_state=None, template_folder=everywhereml/sklearn/ensemble, verbose=0, warm_start=False)
    """

    def __init__(self):
        """
        Constructor
        """
        self.latency = 0
        self.predicted_value = -1

        self.votes = [0.00000000000, 0.00000000000, 0.00000000000]

    def predict(self, x):
        """
        Predict output from input vector
        """
        self.predicted_value = -1
        started_at = ticks_us()

        self.votes = [0.00000000000, 0.00000000000, 0.00000000000]

        idx, score = self.tree0(x)
        self.votes[idx] += score
        
        idx, score = self.tree1(x)
        self.votes[idx] += score
        
        idx, score = self.tree2(x)
        self.votes[idx] += score
        
        idx, score = self.tree3(x)
        self.votes[idx] += score
        
        idx, score = self.tree4(x)
        self.votes[idx] += score
        
        idx, score = self.tree5(x)
        self.votes[idx] += score
        
        idx, score = self.tree6(x)
        self.votes[idx] += score

        # get argmax of votes
        max_vote = max(self.votes)
        self.predicted_value = next(i for i, v in enumerate(self.votes) if v == max_vote)

        self.latency = ticks_diff(ticks_us(), started_at)
        return self.predicted_value

    def latencyInMicros(self):
        """
        Get latency in micros
        """
        return self.latency

    def latencyInMillis(self):
        """
        Get latency in millis
        """
        return self.latency // 1000

    def tree0(self, x):
        """
        Random forest's tree #0
        """
        if x[2] < 2.449999988079071:
            return 0, 37.0
        else:
            if x[3] < 1.649999976158142:
                if x[1] < 2.350000023841858:
                    if x[2] < 4.5:
                        return 1, 36.0
                    else:
                        return 2, 32.0
                else:
                    return 1, 36.0
            else:
                return 2, 32.0

    def tree1(self, x):
        """
        Random forest's tree #1
        """
        if x[0] < 5.349999904632568:
            if x[3] < 0.7000000029802322:
                return 0, 25.0
            else:
                if x[3] < 1.550000011920929:
                    return 1, 46.0
                else:
                    return 2, 34.0
        else:
            if x[3] < 1.649999976158142:
                if x[1] < 3.6999999284744263:
                    if x[2] < 4.950000047683716:
                        return 1, 46.0
                    else:
                        return 2, 34.0
                else:
                    return 0, 25.0
            else:
                return 2, 34.0

    def tree2(self, x):
        """
        Random forest's tree #2
        """
        if x[2] < 2.599999964237213:
            return 0, 32.0
        else:
            if x[3] < 1.600000023841858:
                if x[3] < 1.449999988079071:
                    return 1, 38.0
                else:
                    if x[2] < 4.950000047683716:
                        return 1, 38.0
                    else:
                        return 2, 35.0
            else:
                if x[2] < 4.8500001430511475:
                    if x[0] < 5.400000095367432:
                        return 2, 35.0
                    else:
                        if x[0] < 5.950000047683716:
                            return 1, 38.0
                        else:
                            return 2, 35.0
                else:
                    return 2, 35.0

    def tree3(self, x):
        """
        Random forest's tree #3
        """
        if x[2] < 2.350000023841858:
            return 0, 30.0
        else:
            if x[2] < 4.75:
                if x[3] < 1.600000023841858:
                    return 1, 41.0
                else:
                    return 2, 34.0
            else:
                if x[1] < 3.049999952316284:
                    if x[3] < 1.649999976158142:
                        if x[1] < 2.350000023841858:
                            return 2, 34.0
                        else:
                            return 1, 41.0
                    else:
                        return 2, 34.0
                else:
                    if x[0] < 6.150000095367432:
                        return 1, 41.0
                    else:
                        if x[2] < 5.0:
                            return 1, 41.0
                        else:
                            return 2, 34.0

    def tree4(self, x):
        """
        Random forest's tree #4
        """
        if x[2] < 2.449999988079071:
            return 0, 33.0
        else:
            if x[3] < 1.649999976158142:
                if x[1] < 2.25:
                    if x[2] < 4.25:
                        return 1, 44.0
                    else:
                        return 2, 28.0
                else:
                    return 1, 44.0
            else:
                return 2, 28.0

    def tree5(self, x):
        """
        Random forest's tree #5
        """
        if x[3] < 0.75:
            return 0, 37.0
        else:
            if x[3] < 1.600000023841858:
                return 1, 35.0
            else:
                if x[2] < 4.8500001430511475:
                    if x[2] < 4.650000095367432:
                        return 2, 33.0
                    else:
                        if x[1] < 3.100000023841858:
                            return 2, 33.0
                        else:
                            return 1, 35.0
                else:
                    return 2, 33.0

    def tree6(self, x):
        """
        Random forest's tree #6
        """
        if x[2] < 2.449999988079071:
            return 0, 35.0
        else:
            if x[0] < 6.3500001430511475:
                if x[3] < 1.600000023841858:
                    return 1, 37.0
                else:
                    return 2, 33.0
            else:
                if x[2] < 5.0:
                    return 1, 37.0
                else:
                    return 2, 33.0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226

To execute the model inside your own project, refer to the following skeleton.

from time import sleep
from random_forest import RandomForestClassifier

clf = RandomForestClassifier()
x = [5.1, 3.5, 1.4, 0.2]
y = clf.predict(x)
1 2 3 4 5 6

Pretty easy, right?

You only have to call clf.predict(x) to get the predicted value back.

Random Forest is only the first of a few classifiers that will be implemented in the next weeks, but it's one of the most performant and you can go a long way with it already!

Subscribe to my newsletter

Join 1007 businesses and hobbysts skyrocketing their Arduino + ESP32 skills twice a month