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 everywheremlThen 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.96To port the classifier to MicroPython, just run the following line.
print(clf.to_micropython())This single line will generate valid MicroPython code that you can integrate in your project. Here's what it looks like.
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.0To 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)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!
 
         
            
         
            
         
            
        