micro python + esp8266 + mqtt + RC car = fun project

Hi its been a while since the last post, sorry about that. So in the last few month’s, i was playing with the idea of doing some projects with IoT devices and python. I knew that IOT and electronic hobby projects were a very expensive proposition, so I decided early on that I wouldn’t pour huge sums on the problem and not invest on new kit and more expensive equipment at least that’s the plan.

 enter esp8266 (aka the GOAT chip)

what can I say about this board which hasn’t been said already, To say that its very capable is a gross understatement . I feel its a good litmus test to know if you really want to do tech if you don’t fall in love with this I’m not sure I can recommend a future in the tech industry as a developer. For this project, I am using a wemos d1 mini board.

The esp8266  embodies all things good about programming . its fast, fun, cheap, connected, has great community support and you can do almost anything with it. But great hardware also has a great problem the end user, let me explain if you give me a race car and ask me to go fast around a track most likely I will crash. Similarly, it takes years of practice and training to be competent at IOT and electronics. But every so often there comes a great innovation in software which makes a task like driving a race car as simple as operating a washing machine.

enter MicroPython (aka infinity gauntlet)

Image result for thanos snap

python is powerful(obviously) but ask anyone why c is still popular and so powerful the answer is that nothing else will come as close to bare metal as c does , you can write programs for a server and if you have enough memory cross compile it to some obscure processor the size of a rice grain, okay this wont work always but it is possible. And that is why I feel micro python is a breath of fresh air in the programming language space which is obsessed with more and more features. Its Python in a microcontroller, an appropriate metaphor for this would be like if you could get super serum in your local drug store.

enter MQTT (aka the nerve center)

Image result for brain meme

great now we have the main components of our project but we still need some software to make communicating with the different pieces very easy. Mqtt is an industry standard message queue protocol used in IoT devices and there is lot of support and a large set of community developed tools. And is it so happens newer versions of micropython come baked with a mqtt client library (mind = blown) .

putting it all together

I really love remote cars but they are limited to an analog remote and exist in a sort of isolated space. I wanted to make my RC remote car into a wifi car I bought the cheapest RC car I could find and started looking online for schematics and instruction sets for the ic on the board and as it so happens I came upon this beauty RX2C, the RX2C is found in most if not all cheap RC remote cars . so in true hacky fashion, I just soldered the four gpio pins of the wemos d1 mini to the LEFT, RIGHT, UP and DOWN pins of the.

Some python and mqtt code to put it all together. I have made a GitHub repo for the code here.

Bonus: here is a poster I made for the Chennai python conference PySangamam.

py-sangamam-sub-2.png

debugging in python and unlocking 10x productivity.

python is a beautiful language and dare i say the most useful programming language of the 2010’s . I have been using python as an enthusiast for close to 7 years now, 3 years in college and 4 years professionally. Even though i cant claim to be a pro i sometimes feel like a pro when i use debugging tools. Dare i say it might be the single biggest differentiating factor between a amateur and pro’s , it is common for anyone to make mistakes but usually the pro will fix the code before the amateur can even type the 5th print statement.

The claim of 10x is not completely without some evidence, Even professionally i have met very senior developers who had never heard of python debugging tools and after having used said tools were able to unlock a different gear in their workflow. dare i say every course should have a section devoted to debugging. Incorporating debugging much earlier in development leads to something i like to call Debugger Driven Devlopment ( DDD trade mark pending ) .

PDB

pdb is a package which comes bundled with every distribution of python(docs), Even though it is the standard debugger it comes packed with some awesome features, things which are not possible even in most modern ide debuggers IMO . i will not be covering the basics here , my hope is that you will be tempted to try it for your self. Having said that if you are interested in a deeper step by step please let me know in the comments.

Debugging multi-threaded code

import concurrent.futures
import time
from pdb import set_trace

def thread_function(val):
  problem_val = 10 * val
  if val>3:
    problem_val = 20 * val
    set_trace()
  time.sleep(2)

with concurrent.futures.ThreadPoolExecutor(max_workers=3) as executor:
        executor.map(thread_function, range(10))

Debugging multi-threaded code is possible , this itself is a 100x or 1000x faster compared to print statements. dealing with regular single threaded code with 10 variables itself can be a headache , when threads are involved the headaches increase exponentially. replIt

the above code snip it demonstrates a sample multi-threaded. debugging this simple example with any other tools becomes very difficult very quickly.

Jumping back

from pdb import set_trace
set_trace()
a="working code"
b="some error"
d="some thing you want to check"

when you miss an exit on a free way you usually have to make a big U-turn to come-back and take it again . This also happens quite a lot when you are tweaking a piece of code , you tweak something and it breaks , you change the code it works briefly and breaks , you change it some more it breaks something completely unrelated , you cry and question all life choices you made up to this point. imagine the same freeway scenario but in this scenario your the only one on the road , so you check to see if there any cops and hit reverse and take the correct exit . This is possible in python (and completely legal)

in the snip it above we want to check the code but we have a statement in “b” which throws and error and exits either because some dependent service is down or its some transient issue. you want to investigate issue d so you jump directly to said line number. you can also set break points in library methods which are called.

Frame jumping

from pdb import set_trace
set_trace()

def x_block():
    a,b,c = "context","you have", "no clue about"
    d = a+c+b
    def y_block():
      e = d[:2]
      return e+e
    return y_block
print(x_block()())

some times you need some perspective as to how you came to this point , it helps that PDB has good support for looking at variables across the call stack. this means that no part of the code is opaque to you.

in the snip it above the code produces the output “coco” now the word as such has some meaning but to find out the context in which the word was formed is a nightmare. Sadly these types of nightmares are all to common. But with Pdb you have the option to move up and down the stack , to visit and look at each variable individually and to examine the context with which it was declared and the operation that were performed on it .

Debugger Driven Development

its difficult to write code the way it has been written for so long. as soon as you try to write more than 400 lines of code at a stretch you start to balance multiple variables and thousands of states , all of which inevitably leads to large quantity of mistakes. coding best practices help but at some point you still have to check everything and a very common type of mistake programmers make is they run and rerun the code over and over every-time they run into an issue. And if the prior steps involve a lengthy setup it is easy to lose many hours just to get the code to a stable state.

In Debugger Driven Development you start out by typing in comments and overall goals of what you want to achieve. you drop a break point at the first comment and start writing the code in the debugger itself and move the code into IDE. You will restart the script from time to time , But on average the development time is cut in half , yes HALF. You eliminate typos , syntax error , context mismatch and some latency issues in the first go . The code you write already has documentation. you will also get a lot of confidence in the code , when you know that the scenarios which you meant to cover are tested at least once . it forces you to think step by step , this also gives us some time to optimise the solution .

this is not to say that there are no drawbacks , the most noticeable is the lack of multi line support . there are debuggers like epdb which have multi line support which means you can write out whole classes if you like.

In Conclusion PDB and variants open up a whole new gear in the workflow and productivity of programmers dealing with python code , my hope is this article helps you to do some more research on the PDB . I am positive that if not 10X you will atleast be more confident in the code you produce.

The distracted driver and my adventures with auto encoders

after many months of procrastination, i finally decided to compete on kaggle and I decided to skip all the easy training rounds and go after the real challenge.In the distracted driver challenge, you have to predict what the driver is doing based on a pre-classified training set containing ten classes, each class depicts a driver performing an action(texting, drinking coffee, applying makeup, etc). But in case my there were additional challenges since I didn’t have ready access to high-end hardware and I had to attempt the challenge using my laptop an HP246(aka amma laptop) with an Intel Pentium(R) processor and 4gigs of ram.(To clarify my main aim for participation was educational and as such, i did not follow any industry standard style guide (pep8) and the code is not optimized.)

Step 1: processing the data

because of hardware limitations, I had to abandon the default python data structures(list,dict, etc) as they break the ipython kernel. So I had to figure out new ways to batch process through the dataset. Fortunately, i came across an amazing library called h5py. Now all I needed to do was to convert the images to greyscale, resize them to (160×120) and save it as a pdf file.

Step 2: constructing the model

I decided to use an ensemble of autoencoders for dimensionality reduction and random forest regressor for prediction.To construct the autoencoder I settled on using tflearn its one of the most approachable libraries out there and I deeply recommend it to anyone looking to get their feet wet in deep learning.for the randforest regressor, i am using the trusted sklearn library.
autoencoder is unsupervised learning technique which is used as a dimensionality reduction it is a simple feed-forward neural network aimed at predicting its own inputs the middle layer is selected as a reduced input and can be used for classification.
(apologies to any Pythonistas)

import tflearn
encoder = tflearn.input_data(shape=[None, 19200])
encoder = tflearn.fully_connected(encoder, 5000)
encoder = tflearn.fully_connected(encoder, 2000)
encoder = tflearn.fully_connected(encoder, 784)</code>

# Building the decoder

decoder = tflearn.fully_connected(encoder, 100)
decoder = tflearn.fully_connected(decoder, 1000)
decoder = tflearn.fully_connected(encoder, 19200)
net = tflearn.regression(decoder, optimizer='adam', learning_rate=0.005,
loss='mean_square', metric=None)

X=load_imgs()
testX=load_imgs_test()
model = tflearn.DNN(net,tensorboard_verbose=0)
#training the model

model.fit(X, X, n_epoch=10, validation_set=(testX, testX),run_id="auto_encoder", batch_size=200)
#saving the model
model.save("all_image_autencode_model.tf")

step 3: classification based on reduced input

we can use any conventional model for the prediction but from trial and error, i came to the conclusion that the random forest regressor gave the most accuracy. The library of choice is of course sklearn it has a lot of good examples and very good documentation.


from sklearn.ensemble import RandomForestClassifier as tree
model=tree(n_estimators=500,verbose=1,n_jobs=10,min_weight_fraction_leaf=0.05)
model.fit(x_train,y_train)
with f3,f2 as h5.File("result_rfr_estim","w"), h5.File("set_with_reduced","r") :
dset=f3.create_dataset("class",(79727,10),dtype='float16',maxshape=(None,None))
dset1=f3.create_dataset("name",(79727 ,1),dtype='S20',maxshape=(None,None))
count=0
while(count !=79727):
dset[count]=model.predict(f2['reduced'][count])
dset1[count]=f2['name'][count]
count+=1

step 4: repackage to csv

now all we have to do is repack our results into CSV file which kaggle will accept.


import csv
import h5py as h5<code>
with f3 as h5.File("result_rfr_estim_300","r"):
result=open("knn_result+11.csv","wb")
write=csv.writer(result)
write.writerow(["img","c0","c1","c2","c3","c4","c5","c6","c7","c8","c9"])
count=0
while 1:
c=f3['class'][count]
write.writerow([f3['name'][count][0],c[0],c[1],c[2],c[3],c[4],c[5],c[6],c[7],c[8],c[9]])
if count==len(f3['name'])-2:
break
count+=1</code>

<code>

conclusion :

well, the final result on google was a multiclass log loss score of ~ 3.0 which is kinda bad since the example input data score was 2.7 still i learnt a lot of useful things on how to do stuff. And it was a great amount of fun. You can check out my .ipynb file for the project over here.

Stock Market prediction using Neural Nets

here I present my undergraduate project the basic idea is to examine how neural networks can perform a stock market analysis.

introduction

In general, a time series refers to a series of data points which are measured
at successive points in time spaced at uniform time intervals. This concept
is heavily used in scientific fields like statistics and signal processing, but
also in the context of financial analysis.The price that a particular security is traded at can be viewed as a time series, where the value at a given point in time is the price of the last
observed trade at that time. In essence, the time series is then just a simple
price curve for the security.

Artificial Neural networks

Artificial neural networks (ANNs) are a class of machine learning algo-
rithms that draw inspiration from biological neural systems. They are
generally implemented in computer software with the aim of enabling au-
tomatic learning and subsequently autonomous problem solving.

neural nets for stock markets

A big challenge in making an automated stock market analysis system is finding
a way to reliably identify trend reversal patterns such. While the presence of such patterns are often obvious for a person looking at a chart, this type of classification is notoriously
tricky to implement heuristically in a computer algorithm. to overcome these challenges we use the superior ability f neural networks in the domain of pattern recognition and apply it in predicting various trends which other statistical models are likely to miss.

the code

my solution to the problem is implemented in python and is available for free and to be modified and reused by all. the project is hosted on Github so feel free to clone and make your own modifications please let me know if you have any suggestions to improve it (it’s currently 20% glue code so feel free to apply glue wherever necessary).

screen cap:

 

 

big thanks to Rune Aamodt(NTNU) for making a great report on the subject.

 

Simple Game With RNN – Lstm

Introduction

there has been a lot of talks recently about Ann( artificial neural network) what is it and why should you care, I will try to answer these questions in the least boring way possible. An ANN is an electronic approximation of biological neural networks. It offers many interesting insights into how our own brains work. artificial neural networks have recently made great advances in AI by offering new models for pattern recognition, audio and video analysis.

artificial neural networks are aimed at making computers that are more suitable for real-world scenarios which involve learning from the environment based on past inputs. this allows us to make highly adaptable systems which can either understand new requirement(unsupervised learning) or predict the future outputs of the system based on past inputs(supervised learning).

rnn-lstm

recurrent neural networks are a little bit better approximation of our brain they have a feedback loop which feeds the past inputs of the system to itself ,this allows it to have memory of past trends and behaviours.this kind of behaviour is of paramount importance in applications such as language translators, where memory of context is very important.

yawn ….said something about some game

but today we are going to look at the fun side of AI. Every child in India has played hand-cricket in the classroom, to those unfamiliar with this game the basic idea is to keep the opponent guessing by choosing random number in the range of 1 to 6 this will be your RUNS(score) if by chance your opponent has the same number as you are OUT then it is the opponent’s turn to make a total greater than your score .

the script for such a program is small and indeed very easily codable using a random number generator, but what if we replaced the soul-less random-number generator with a neural network which learns your moves and predicts your next move. the proverbial plot thickens …

code

the coding is done in python and the python library for our purpose is pybrain, is perfect if you are starting out (like me) and just want something that works(most of the time), pybrain is slow because it’s mostly made in python and does not use optimizations. it took me an average of 30-40 seconds to get a prediction per round.

the code for all this can be found in on Github feel free to try it out and mess around with it