Generating Tesla authentication token – cURL script

I did write a simple Windows (desktop) app called TeslaTokenGenerator, for those who wanted to create authentication tokens for their Tesla, and use with 3rd party apps/data loggers. 

TeslaTokenGenerator can also create a cURL script for you to use, if you prefer not wanting to install anything. It is easy to find this online, but some of you have pinged me to get more details on this. So, I have the script below that you can use. Once you copy this, you will need to update your Tesla account login details (email and password) and run it in a console (command line) and it will all the same API’s to create the token, which then you can save.

curl -X POST -H "Cache-Control: no-cache" -H "Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW" -F "grant_type=password" -F "client_id=81527cff06843c8634fdc09e8ac0abefb46ac849f38fe1e431c2ef2106796384" -F "client_secret=c7257eb71a564034f9419ee651c7d0e5f7aa6bfbd18bafb5c5c033b093bb2fa3" -F "email=YOUR-TESLA-LOGIN-EMAIL@SOMEWHERE.COM" -F "password=YOUR-TESLA-ACCOUNT-PASSWORD" ""

You can see the screenshots of this below too – one in Windows, and another in Linux (well Bash on Windows, but it is real Linux).

My Tesla Model 3 “Keyfob”

Inspired by a few folks on a few forums online, I took the liberty to extend their idea using a IoT Button, that acts as a simple “keyfob” for the Model 3.

The main goal was being to allow my daughter to lock and unlock the car at home. She is too young to have a phone, and without a more traditional fob, this gets a little annoying. 

I extended the original idea, to understand the different presses (Single, Double, and Long press), and accordingly called the appropriate API to lock the car (single press – think of it as a single click), unlock on a double press, and open the charge port on a long press (when one presses and holds the button 2-3 secs).

Model 3 “Keyfob”

For those who aren’t aware, the Amazon IoT buttons calls a Lambda function on AWS and plugging into that, one can extend this. The button needs to be connected, and online for this to work, and in my case, it is on the home wifi network. 

Update: Many of you asked on how to set this up for yourself; I got around to blogging all the step on that; you can read those here.

Windows Tesla Auth Token Generator

If you have a Tesla, and are using (or wanting to use) 3rd party tools or data loggers, the one think they of course need is to authenticate your details with Tesla. A simple, but insecure way is to use your Tesla credentials – and surprisingly many people just happily share and use this.

I wasn’t comfortable doing this – after-all, they have access to your account where you can control a lot of things. Also, there are a few online tools that can generate the auth token, but again I wasn’t comfortable, as I did not know what they saved, or what they did not. 🙂

So, I wrote a simple Windows app that can allow you to generate a auth token that you can save. The application itself is simple. You enter your Tesla credentials, click on Generate Token and can save the generated token. 

Tesla Token Generator Application

To test, if the generated token is working – click on the Test Token button. If everything is working as expected, you will see a list of vehicles that is associated with your account.

If you prefer to use the cURL script, click on the Generate cURL, will generate this and copy it to your clipboard. And it works across operating systems as you can see below (Windows, and Linux), but should also work on Mac.

I do intent to open source this, so folks can have a look at the code, and the Tesla REST APIs. Until then you can download the setup from here.

Leave a comment if you have any issues or any requests.

Update: v1.0.1 Published with minor updates. You can download from the same link above. This adds the revoke screen and some house keeping.

The merits of #AI

Thought of the week:

Artificial Intelligence stands no chance against natural Stupidity.


#ML training data

Seem like my training data for the car – perhaps a hint of #bias. 😂

#GeekyJokes #ML #AIJokes

Neural network basics–Activation functions

Neural networks have a very interesting aspect – they can be viewed as a simple mathematical model that define a function. For a given function f(x) which can take any input value of x, there will be some kind a neural network satisfying that function. This hypothesis was proven almost 20 years ago (“Approximation by Superpositions of a Sigmoidal Function” and “Multilayer feedforward networks are universal approximators”) and forms the basis of much of #AI and #ML use cases possible.

It is this aspect of neural networks that allow us to map any process and generate a corresponding function. Unlike a function in Computer Science, this function isn’t deterministic; instead is confidence score of an approximation (i.e. a probability). The more layers in a neural network, the better this approximation will be.

In a neural network, typically there is one input layer, one output layer, and one or more layers in the middle. To the external system, only the input layer (values of x), and the final output (output of the function f(x)) are visible, and the layers in the middle are not and essentially hidden.

Each layer contains nodes, which is modeled after how the neurons in the brain works. The output of each node gets propagated along to the next layer. This output is the defining character of the node, and activates the node to pass on its value to the next node; this is very similar to how a neuron in the brain fires and works passing on the signal to the next neuron.

Neural Network
Neural Network

To make this generalization of function f(x) outlined above to hold, the that function needs to be continuous function. A continuous function is one where small changes to the input value x, creates small changes to the output of f(x). If these outputs, are not small and the value jumps a lot then it is not continuous and it is difficult for the function to achieve the approximation required for them to be used in a neural network.

For a neural network to ‘learn’ – the network essentially has to use different weights and biases that has a corresponding change to the output, and possibly closer to the result we desire. Ideally small changes to these weights and biases correspond to small changes in the output of the function. But one isn’t sure, until we train and test the result, to see that small changes don’t have bigger shifts that drastically move away from the desired result. It isn’t uncommon to see that one aspect of the result has improved, but others have not and overall skewing the results.

In simple terms, an activation function is a node that attached to the output of a neural network, and maps the resulting value between 0 and 1. It is also used to connect two neural networks together.

An activation function can be linear, or non-linear. A linear isn’t terribly effective as its range is infinity. A non-linear with a finite range is more useful as it can be mapped as a curve; and then changes on this curve can be used to calculate the difference on the curve between two points.

There are many times of activation function, each either their strengths. In this post, we discuss the following six:

  • Sigmoid
  • Tanh
  • ReLU
  • Leaky ReLU
  • ELU
  • Maxout

1. Sigmoid function

A sigmoid function can map any of input values into a probability – i.e., a value between 0 and 1. A sigmoid function is typically shown using a sigma (\sigma). Some also call the (\sigma) a logistic function. For any given input value,  x the official definition of the sigmoid function is as follows:

\sigma(x) \equiv \frac{1}{1+e^{-x}}

If our inputs are x_1, x_2,\ldots, and their corresponding weights are w_1, w_2,\ldots, and a bias b, then the previous sigmoid definition is updated as follows:

\frac{1}{1+\exp(-\sum_j w_j x_j-b)}

When plotted, the sigmoid function, will look plotted looks like this curve below. When we use this, in a neural network, we essentially end up with a smoothed out function, unlike a binary function (also called a step function) – that is either 0, or 1.

For a given function, f(x), as x \rightarrow \infty, f(x) tends towards 1. And, as as x \rightarrow -\infty, f(x) tends towards 0.

Sigmoid function
Sigmoid function

And this smoothness of \sigma is what will create the small changes in the output that we desire – where small changes to the weights (\Delta w_j), and small changes to the bias (\Delta b) will produce a small changes to the output (\Delta output).

Fundamentally, changing these weights and biases, is what can give us either a step function, or small changes. We can show this as follows:

\Delta \mbox{output} \approx \sum_j \frac{\partial \, \mbox{output}}{\partial w_j} \Delta w_j + \frac{\partial \, \mbox{output}}{\partial b} \Delta b


One thing to be aware of is that the sigmoid function suffers from the vanishing gradient problem – the convergence between the various layers is very slow after a certain point – the neurons in previous layers don’t learn fast enough and are much slower than the neurons in later layers. Because of this, generally a sigmoid is avoided.

2. Tanh (hyperbolic tangent function)

Tanh, is a variant of the sigmoid function, but still quite similar – it is a rescaled version and ranges from –1 to 1, instead of 0 and 1. As a result, its optimization is easier and is preferred over the sigmoid function. The formula for tanh, is

\tanh(x) \equiv \frac{e^x-e^{-z}}{e^X+e^{-x}}

Using, this we can show that:

\sigma(x) = \frac{1 + \tanh(x/2)}{2}.

Sigmoid vs Tanh
Sigmoid vs Tanh

Tanh also suffers from the vanishing gradient problem. Both Tanh, and, Sigmoid are used in FNN (Feedforward neural network) – i.e. the information always moves forward and there isn’t any backprop.


3. Rectified Linear Unit (ReLU)

A rectified linear unity (ReLU) is the most popular activation function that is used these days.

\sigma(x) = \begin{cases} x & x > 0\\ 0 & x \leq 0 \end{cases}


ReLU’s are quite popular for a couple of reasons – one, from a computational perspective, these are more efficient and simpler to execute – there isn’t any exponential operations to perform. And two, these doesn’t suffer from the vanishing gradient problem.


The one limitation ReLU’s have, is that their output isn’t in the probability space (i.e. can be >1), and can’t be used in the output layer.

As a result, when we use ReLU’s, we have to use a softmax function in the output layer.  The output of a softmax function sums up to 1; and we can map the output as a probability distribution.

\sum_j a^L_j = \frac{\sum_j e^{z^L_j}}{\sum_k e^{z^L_k}} = 1.


Another issue that can affect ReLU’s is something called a dead neuron problem (also called a dying ReLU). This can happen, when in the training dataset, some features have a negative value. When the ReLU is applied, those negative values become zero (as per definition). If this happens at a large enough scale, the gradient will always be zero – and that node is never adjusted again (its bias. and, weights never get changed) – essentially making it dead! The solution? Use a variation of the ReLU called a Leaky ReLU.

4. Leaky ReLU

A Leaky ReLU will usually allow a small slope \alpha on the negative side; i.e that the value isn’t changed to zero, but rather something like 0.01. You can probably see the ‘leak’ in the image below. This ‘leak’ helps increase the range and we never get into the dying ReLU issue.

ReLU vs. Leaky ReLU
ReLU vs. Leaky ReLU

5. Exponential Linear Unit (ELU)

Sometimes a ReLU isn’t fast enough – over time, a ReLU’s mean output isn’t zero and this positive mean can add a bias for the next layer in the neural network; all this bias adds up and can slow the learning.

Exponential Linear Unit (ELU) can address this, by using an exponential function, which ensure that the mean activation is closer to zero. What this means, is that for a positive value, an ELU acts more like a ReLU and for negative value it is bounded to -1 for \alpha = 1 – which puts the mean activation closer to zero.

\sigma(x) = \begin{cases} x & x \geqslant 0\\ \alpha (e^x - 1) & x < 0\end{cases}


When learning, this derivation of the slope is what is fed back (backprop) – so for this to be efficient, both the function and its derivative need to have a lower computation cost.


And finally, there is another various of that combines with ReLU and a Leaky ReLU called a Maxout function.

So, how do I pick one?

Choosing the ‘right’ activation function would of course depend on the data and problem at hand. My suggestion is to default to a ReLU as a starting step and remember ReLU’s are applied to hidden layers only. Use a simple dataset and see how that performs. If you see dead neurons, than use a leaky ReLU or Maxout instead. It won’t make sense to use Sigmoid or Tanh these days for deep learning models, but are useful for classifiers.

In summary, activation functions are a key aspect that fundamentally influence a neural network’s behavior and output. Having an appreciation and understanding on some of the functions, is key to any successful ML implementation.

Netron – deep learning and machine learning model visualizer

I was looking at something else and happen to stumble across something called Netron, which is a model visualizer for #ML and #DeepLearning models. It is certainly much nicer than for anything else I have seen. The main thing that stood out for me, was that it supports ONNX , and a whole bunch of other formats (Keras, CoreML), TensorFlow (including Lite and JS), Caffe, Caffe2, and MXNet. How awesome is that?

This is essentially a cross platform PWA (progressive web app), essentially using Electron (JavaScript, HTML5, CSS) – which means it can run on most platforms and run-times from just a browser, Linux, Windows, etc. To debug it, best to use Visual Studio Code, along with the Chrome debugger extension.

Below is a couple of examples, of visualizing a ResNet-50 model – you can see both the start and the end of the visualization shown in the two images below to get a feel of things.

Start of ResNet-50 Model

End of ResNet-5o model

And some of the complex model seem very interesting. Here is an example of a TensorFlow Inception (v3) model.


And of course, this can get very complex (below is the same model, just zoomed out more).


I do think it is a brilliant, tool to help understand the flow of things, and what can one do to optimize, or fix. Also very helpful for folks who are just starting to learn and appreciate the nuances.

The code is released under a MIT license and you can download it here.

Machine learning use-cases

Someone recently asked me, what are some of the use cases / examples of machine learning. Whilst, this might seem as an obvious aspect to some of us, it isn’t the case for many businesses and enterprises – despite that they uses elements of #ML (and #AI) in their daily life – as a consumer.

Whilst, the discussion gets more interesting based on the specific domain and the possibly use cases (of course understanding that some might not be sure f the use case – hence the question in the first place). But, this did get me thinking and wanted to share one of the images we use internally as part of our training that outcomes some of the use cases.

Machine Learning Use Cases
Machine Learning Use Cases

These are not 1:1 and many of them can be combined together to address various use cases – for example a #IoT device sending in a sensor data, that triggers a boundary condition (via a #RulesEngine), that in addition to executing one or more business rule, can trigger a alert to a human-in-the-loop (#AugmentingWorkforce) via a #DigitalAssistant (say #Cortana) to make her/him aware, or confirm some corrective action and the likes. The possibilities are endless – but each of these elements triggered by AI/ML and still narrow cases and need to be thought of in the holistic picture.

Synthetic Sound

Trained a model to create a synthetic sound that sounds like me. This is after training it with about 30 sentences – which isn’t a lot.

To create a synthetic voice, you enters some text, using which is then “transcribed” using #AI and your synthetic voice is generated. In my case, at first I had said AI, which was generated also as “aeey” (you can have a listen here). So for the next one, changed the AI to Artificial Intelligence.

One does need to be mindful of #DigitalEthics, as this technology improves further. This is with only a very small sampling of data. Imagine what could happen, with public figures – where their recordings are available quite easily in the public domain. I am thinking the ‘digital twang’ is one of the signatures and ways to stamp this as a generated sound.

My self-driving car

Over the last few weeks, I built a self-driving car – which essentially is a remote control Rx car that uses a raspberry pi running Python, TensorFlow implementing a end-to-end convolution neural network (CNN)

Of course other than being  a bit geeky, I do think this is very cool to help understand and get into some of the basic constructs and mechanics around a number of things – web page design, hardware (maker things), and Artificial Intelligence principles.

There are two different models here – they do use the same ASC and controller that can be programmed. My 3D printer, did mess up a little (my supports were a little off) and which is why you see the top not clean.

The sensor and camera are quite basic, and there is provisions to add and do better over time. The Pi isn’t powerful enough to train the model – you need another machine for that (preferably a I7 core with a GPU). Once trained you can run the model on the Pi for inference.

This is the second car, which is a little different hardware, but the ESC to control the motor and actuators are the same.

The code is simple enough; below is an example of the camera (attached) to the Pi, saving the images it is seeing. Tubs is the location where the images are saved; these can then be transferred to another machine for training or inference.

<br />
import donkey as dk</p>
<p>#initialize the vehicle<br />
V = dk.Vehicle()</p>
<p>#add a camera part<br />
cam =<br />
V.add(cam, outputs=['image'], threaded=True)</p>
<p>#add tub part to record images<br />
tub ='~/d2/gettings_started',<br />
                   inputs=['image'],<br />
                   types=['image_array'])<br />
V.add(tub, inputs=inputs)</p>
<p>#start the vehicle's drive loop<br />
V.start(max_loop_count=100)<br />

Below you can see the car driving itself around the track, where it had to be trained first. The reason it is not driving perfectly is because during training (when I was manually driving it around), I crashed a few times and as a result the training data was messed up. Needed more time to clean that up and retrain it.

This is based on donkey car – which is an open source DIY for platform for small-scale self driving cars. I think it is also perfect to get into with those who have teenagers and a little older kids to get in and experiment. You can read up more details on how to go about building this, and the parts needed here.

Cloud and failure

Despite all the cloud talk and where I live, it is like the cloud mecca, for enterprises it is still quite new and many are just starting to think about it. A hard lesson that many of us learn (and partly how we amass our scars) is to design for failures. For those, who run things in their enterprises data center, are quite spoilt I think. Failures are rare, and if machines or state goes down, moving to another one isn’t really a big deal (of course it is a little more complex, and not to say, there isn’t any down time, or business loss, etc.).

When thinking about a cloud migration (hybrid or otherwise) – a key rule is that you are guaranteed to have failures – at many aspects, and those cannot be exceptional conditions, but rather the normal design and expected behavior. As a result, you app/services/API/whatever needs to be designed for failure. And not only how your loosely couple your architecture to be able to handle these situations, but also, how the response isn’t a binary (yay, or a fancy 404); but rather a degraded experience, where your app/service/API/whatever still performs albeit in a deprecated mode.

Things that can throw one off, and is food for thought (not exhaustive, or on any particular order):

  • Managing state (when failures is guaranteed)
  • Latency – cloud is fast, but slower than your internal data center; you know – physics. 🙂 How are your REST API’s handling latency, and are they degrading performance?
  • “Chatiness” – how talkative, are your things on the wire? And how big is the payload?
  • Rollback, or fall forward?
  • Lossy transfers (if data structure sizes are large)
  • DevOps – mashing up of Developers, and Operations (what some call SRE) – you own the stuff you build, and, responsible for it.
  • AutoScale – most think this is to scale up, but it also means to scale down when resources are not needed.
  • Physical deployments – Regional deployment vs. global ones – there isn’t a right or wrong answer, it frankly depends on the service and what you are trying to do. Personally, I would lean towards regional first.
  • Production deployment strategies – various ways to skin a cat and no one is right or wrong per se (except, please don’t do a basic deployment) – that is suicide. I am use to A/B testing, but also what is now called Blue/Green deployment. Read up more here. And of course use some kind of a deployment window (that works for your business) – this allows you and your team to watch what is going on, and take corrective actions if required.
  • Automate everything you can; yes its not free, but you recoup that investment pretty quick; and will still have hair on the scalp!
  • Instrument – if you can’t measure it, you can’t fix it.

Again, not an exhaustive list, but rather meant to get one thinking. There are also some inherent assumptions – e.g. automation and production deployment suggests, there is some automated testing in place; and a CI/CD strategy and supporting tools.

Bottom line – when it comes to cloud (or any other distributed architecture), the best way to avoid failure is to fail constantly!

From managers to leaders

Recently, a few of us went through a workshop where one of the ‘homework’ was to score oneself, on the following 7 aspects – some of these are attributes that allows one to grow from being (hopefully) good managers to great leaders.

In most enterprises, as one grows in their career, managers need to acquire new capabilities – and quickly. What they have, in terms of skills and capabilities and got her or him to this place, won’t be enough for the next step – as the scope and complexity increases it can leave executives underwhelmed. At the core, new executives need support on these seven dimensions that will help them make this transition.

    • Specialist to generalist – Understand the mental models, tools, and terms used in key business functions and develop templates for evaluating the leaders of those functions.
    • Analyst to Integrator – Integrate the collective knowledge of cross-functional teams and make appropriate trade-offs to solve complex organizational problems.
    • Tactician to Strategist – Shift fluidly between the details and the larger picture, perceive important patterns in complex environments, and anticipate and influence the reactions of key external players.
    • Bricklayer to Architect – Understand how to analyze and design organizational systems so that strategy, structure, operating models, and skill bases fit together effectively and efficiently, and harness this understanding to make needed organizational changes.
    • Problem Solver to Agenda Setter – Define the problems the organization should focus on, and spot issues that don’t fall neatly into any one function but are still important.
    • Warrior to Diplomat – Proactively shape the environment in which the business operates by influencing key external constituencies, including the government, NGOs, the media, and investors.
    • Supporting Cast Member to Lead Role – Exhibit the right behaviors as a role model for the organization and learn to communicate with and inspire large groups of people both directly and, increasingly, indirectly.

I was surprised on how few people talk about this. These come from an awesome HBR article called How Managers become Leaders, which if you haven’t read, I would highly recommend.

So, what can one do? The suggestions outlined are not rocket science, but something to think about. And fundamentally not that much different on how the armed forces trains new officers.

  • Give potential leaders:
    • Experience on cross-functional projects
    • An international assignment
    • Exposure to a broad range of business situations – accelerated growth, sustaining success, realignment, turnaround.
  • When a high potentials’ leadership promise becomes evident give them:
    • A position on a senior management team
    • Experience with external stakeholders
    • An assignment as chief of staff for an experienced enterprise leader
    • An appointment to lead an acquisition integration or a substantial restructuring
  • Just before their first leadership promotion:
    • Send them to an executive program that addresses capabilities like – organizational design, business process improvement, and transition management.
  • When promoted, place new enterprise leaders in business units:
    • That are small, distinct, and thriving
    • And are staffed with an experienced and assertive team that they can learn from.

Download Build deck and video (2018)

Just as last year, I wrote a PowerShell script using which you can download the PowerPoint decks, and, videos from Microsoft Build’s conference, instead of streaming it (or manually download it one by one). You can choose if you want the decks, or the videos, or both. For the videos you can choose the desired resolution (Low, Medium, High) – of course the higher the resolution, the more space is needed. The script also downloads the description and if there is a session image (if there is one).

A few points to note:

  • The slides only once downloaded is ~10GB and with videos (high-resolution), the size goes up to 90.5 GB. So make sure you have enough space.
  • By default the download location is “C:\build-2018\”; you can change this to whatever you want, but make sure there is a trailing backslash. Think of this as the ‘base’ folder.
  • For each session a sub-folder with the session name will be created in the ‘base’ folder setup in the previous step.
  • If a file already exists, it will be skipped.
  • As each file is downloaded, it save it in the root folder and once the download is complete, only then moves it in the relevant subfolder.
  • If a download fails for some reason to retry it, delete the ‘left over’ file(s) in the base folder and then run the script again. The script itself will ‘eat’ the exception and move on to the next file.
  • The video quality parameter is 1 for Low, 2 for Medium, and 3 for High (default).

And if you read through, the script is quite self-explanatory.

# Comments that you should read, before you kick this off. Yes, seriously. :)<br />
# 1. Setup the folder where to download using the parameters outlined below<br />
# 2. Loop through and get the slides first<br />
# 3. Finally, loop through and get the videos last</p>
<p> param (<br />
    [string]$path = &quot;C:\build-2018\&quot;,<br />
    [switch]$sessionvideo = $true,<br />
    [int][ValidateRange(1,3)]$videoquality = 3,<br />
    [switch]$sessiondeck = $true<br />
<p>[Environment]::CurrentDirectory=(Get-Location -PSProvider FileSystem).ProviderPath<br />
$rss = (new-object net.webclient)</p>
<p>#Filenames might get long, so keep this short!<br />
#$downloadlocation = &quot;D:\build-2018&quot;</p>
<p>if (-not (Test-Path $path)) {<br />
	Write-Host &quot;Folder $fpath dosen't exist. Creating it...&quot;<br />
    New-Item $path -type directory<br />
<p>set-location $path</p>
<p>if($sessiondeck) {<br />
# Grab the Slides RSS feed - Build 2018<br />
$slides = ($rss.downloadstring(&quot;;)) </p>
<p>    # ********** download the decks **********<br />
    try {<br />
        foreach($item in $ {<br />
            $code = $item.comments.split(&quot;/&quot;) | select -last 1     </p>
<p>            # Get the url for the pptx file<br />
            $urlpptx = New-Object System.Uri($item.enclosure.url)</p>
<p>            # make the filename readable<br />
            $filepptx = $code + &quot; - &quot; + $item.title.Replace(&quot;:&quot;, &quot;-&quot;).Replace(&quot;?&quot;, &quot;&quot;).Replace(&quot;/&quot;, &quot;-&quot;).Replace(&quot;&amp;lt;&quot;, &quot;&quot;).Replace(&quot;|&quot;, &quot;&quot;).Replace('&quot;',&quot;&quot;).Replace(&quot;*&quot;,&quot;&quot;).Replace(&quot;’&quot;,&quot;'&quot;).Replace(&quot;â€&quot;,&quot;&quot;).Replace(&quot;'NEW SESSION'&quot;,&quot;&quot;).Replace(&quot;™&quot;,&quot;&quot;).Replace(&quot;œ&quot;,&quot;&quot;)<br />
            $filepptx = $filepptx.substring(0, [System.Math]::Min(120, $filepptx.Length))<br />
            $filepptx = $filepptx.trim()<br />
            $filejpg = $filepptx</p>
<p>            $filepptx = $filepptx + &quot;.pptx&quot;<br />
            $filejpg = $filejpg + &quot;_960.png&quot;</p>
<p>            $folder = $item.title.Replace(&quot;:&quot;, &quot;-&quot;).Replace(&quot;?&quot;, &quot;&quot;).Replace(&quot;/&quot;, &quot;-&quot;).Replace(&quot;&amp;lt;&quot;, &quot;&quot;).Replace(&quot;|&quot;, &quot;&quot;).Replace('&quot;',&quot;&quot;).Replace(&quot;*&quot;,&quot;&quot;).Replace(&quot;’&quot;,&quot;'&quot;).Replace(&quot;â€&quot;,&quot;&quot;).Replace(&quot;'NEW SESSION'&quot;,&quot;&quot;).Replace(&quot;™&quot;,&quot;&quot;).Replace(&quot;œ&quot;,&quot;&quot;)<br />
            $folder = $folder.substring(0, [System.Math]::Min(100, $folder.Length))<br />
            $folder = $folder.trim()</p>
<p>            if (-not (Test-Path $folder)) {<br />
                Write-Host &quot;Folder $folder doesnt exist. Creating it...&quot;<br />
                New-Item $folder -type directory<br />
<p>            # Make sure the PowerPoint file doesn't already exist<br />
            if (!(test-path &quot;$path\$folder\$filepptx&quot;)) {<br />
                # Echo out the file that's being downloaded<br />
                $filepptx<br />
                $wc = (New-Object System.Net.WebClient)  </p>
<p>                # Download the pptx file<br />
                Invoke-WebRequest $urlpptx -OutFile $path\$filepptx</p>
<p>                # download the jpg but don't want to break if this doesn't exist; hence the nested try blocks<br />
                try {<br />
                    if($item.thumbnail -ne $null) {<br />
                        $urljpg = New-Object System.Uri($item.thumbnail.url)</p>
<p>                        if (!(test-path &quot;$path\$filejpg&quot;)) {<br />
                            $wc.DownloadFile($urljpg, &quot;$path\$folder\$filejpg&quot;)<br />
                        }<br />
                    }<br />
                }<br />
                catch {<br />
                    Write-Host &quot;Image (jpeg) $filejpg doesn't exist ... eating the exception and moving on ...&quot;<br />
<p>                mv $filepptx $folder<br />
                #mv $filejpg $folder<br />
            } #endif<br />
            else {<br />
                Write-Host &quot;PPTX: $filepptx exist; skipping download.&quot;<br />
<p>            #try and get the sessions details<br />
            try {<br />
                $descriptionFileName = &quot;$($path)\$($folder)\$($Code.trim()).txt&quot;</p>
<p>                if (!(test-path &quot;$descriptionFileName&quot;)) {<br />
                    $OutFile = New-Item -type file $descriptionFileName -Force<br />
                    $Content = &quot;Title: &quot; + $item.title.ToString().trim() + &quot;`r`n&quot; + &quot;`r`n&quot; + &quot;Presenter: &quot; + $item.creator + &quot;`r`n&quot; + &quot;`r`n&quot; + &quot;Summary: &quot; + $item.summary.ToString().trim() + &quot;`r`n&quot; + &quot;`r`n&quot; + &quot;Link: &quot; + $item.comments.ToString().trim() </p>
<p>                    #some categories are missing; so need to eat the exception<br />
                    #this is a hack and not very elegant<br />
                    try {<br />
                        if($item.category -ne $null) {<br />
                            $Content = $Content + &quot;`r`n&quot; + &quot;`r`n&quot; + &quot;Category: &quot; + $item.category.ToString().trim().Replace(&quot;+&quot;,&quot; &quot;)<br />
                        }<br />
                    }<br />
                    catch {<br />
                        #do nothing; eat the exception<br />
<p>                    add-content $OutFile $Content<br />
                }<br />
            }<br />
            catch {<br />
                $ErrorMessage = $_.Exception.Message<br />
                $FailedItem = $_.Exception.ItemName<br />
                Write-host &quot;\t&quot; $ErrorMessage + &quot;\n&quot; + $FailedItem<br />
            }<br />
        } #end-loop foreach</p>
<p>        Write-host &quot;*************** Downloading all the decks complete ***************&quot;<br />
    }<br />
    catch<br />
    {<br />
        Write-host &quot;Oops, could not find any slides.&quot;<br />
        $ErrorMessage = $_.Exception.Message<br />
        $FailedItem = $_.Exception.ItemName<br />
        Write-host &quot;\t&quot; $ErrorMessage + &quot;\n&quot; + $FailedItem<br />
    }<br />
} #download session-deck </p>
<p># ********** download the videos **********<br />
# if you don't want the video but only the slides just comment all the code below in the foreach loop<br />
try {<br />
	# check for video download<br />
	if($sessionvideo) {<br />
        switch ($videoquality) {<br />
            1 {$video = ($rss.downloadstring(&quot;;)); break}<br />
            2 {$video = ($rss.downloadstring(&quot;;)); break}<br />
            default {$video = ($rss.downloadstring(&quot;;)); break}<br />
<p>        foreach($item in $ {<br />
            # Grab the URL for the MP4 file<br />
            $url = New-Object System.Uri($item.enclosure.url)  </p>
<p>            # Create the local file name for the video download<br />
            $file = $item.title.Replace(&quot;:&quot;, &quot;-&quot;).Replace(&quot;?&quot;, &quot;&quot;).Replace(&quot;/&quot;, &quot;-&quot;).Replace(&quot;&amp;lt;&quot;, &quot;&quot;).Replace(&quot;|&quot;, &quot;&quot;).Replace('&quot;',&quot;&quot;).Replace(&quot;*&quot;,&quot;&quot;).Replace(&quot;’&quot;,&quot;'&quot;).Replace(&quot;â€&quot;,&quot;&quot;).Replace(&quot;'NEW SESSION'&quot;,&quot;&quot;).Replace(&quot;™&quot;,&quot;&quot;).Replace(&quot;œ&quot;,&quot;&quot;)<br />
            $file = $file.substring(0, [System.Math]::Min(120, $file.Length))<br />
            $file = $file.trim()<br />
            $file = $file + &quot;.mp4&quot;  </p>
<p>            $folder = $item.title.Replace(&quot;:&quot;, &quot;-&quot;).Replace(&quot;?&quot;, &quot;&quot;).Replace(&quot;/&quot;, &quot;-&quot;).Replace(&quot;&amp;lt;&quot;, &quot;&quot;).Replace(&quot;|&quot;, &quot;&quot;).Replace('&quot;',&quot;&quot;).Replace(&quot;*&quot;,&quot;&quot;).Replace(&quot;’&quot;,&quot;'&quot;).Replace(&quot;â€&quot;,&quot;&quot;).Replace(&quot;'NEW SESSION'&quot;,&quot;&quot;).Replace(&quot;™&quot;,&quot;&quot;).Replace(&quot;œ&quot;,&quot;&quot;)<br />
            $folder = $folder.substring(0, [System.Math]::Min(100, $folder.Length))<br />
            $folder = $folder.trim()</p>
<p>            if (-not (Test-Path $folder)) {<br />
                Write-Host &quot;Folder $folder doesn't exist. Creating it...&quot;<br />
                New-Item $folder -type directory<br />
<p>            # Make sure the video file doesn't already exist<br />
            if (!(test-path &quot;$folder\$file&quot;))<br />
            {<br />
                # Echo out the  file that's being downloaded<br />
<p>                # Download the video file<br />
                try{<br />
                    if (!(test-path &quot;$path\$file&quot;))<br />
                    {<br />
                        Invoke-WebRequest $url -OutFile $path\$file</p>
<p>                        #move it from the current working folder to the target<br />
                        mv $file $folder<br />
                    }<br />
                    else {<br />
                        Write-Host &quot;Video: $file - anoter process possibly working on this; skipping download.&quot;<br />
                    }<br />
                }<br />
                catch {<br />
                    $ErrorMessage = $_.Exception.Message<br />
                    $FailedItem = $_.Exception.ItemName<br />
                    Write-host &quot;\t&quot; $ErrorMessage + &quot;\n&quot; + $FailedItem<br />
                }<br />
            }<br />
            else {<br />
                Write-Host &quot;Video: $file exist; skipping download.&quot;<br />
<p>        } #end-loop foreach<br />
    } #end - video check<br />
}<br />
catch<br />
{<br />
    Write-host &quot;Oops, could not find any videos or some other error happened.&quot;<br />
    $ErrorMessage = $_.Exception.Message<br />
    $FailedItem = $_.Exception.ItemName<br />
    Write-host &quot;\t&quot; $ErrorMessage + &quot;\n&quot; + $FailedItem<br />
} # ********** End - download the videos section **********</p>
<p>Write-host &quot;*************** All Done! Woot! ***************&quot;

Certificate error with git and Donkey Car

If you were trying to pull the latest source code on your Raspberry Pi for donkeycar, and get the following error, then probably your clock is off (and I guess some nonce is failing). This can happen if your pi had been powered off for a while (as in my case), and it’s clock is off (clock drift is a real thing) :).

fatal: unable to access '': server certificate verification failed. CAfile: /etc/ssl/certs/ca-certificates.crt CRLfile: none

To fix this, the following commands works. It seems the Raspberry Pi 3, by default has NTP disabled and this would enable it. I also had to check the result status with the second command, and force it with the third one.

<br />
sudo timedatectl set-ntp True<br />
timedatectl status<br />
sudo timedatectl set-local-rtc true<br />

And that should do it; you might need to reboot the pi just to get it back on and then you should be able to pull the code off git and deploy your autonomous car.

AI photos–style transfer

Can #AI make me look (more) presentable? The jury is out I think. Smile

This is called style transfer, where the style/technique from a kind of painting (could be a photos too) is applied to an image, to create a new image. I took this using the built-in camera on my machine sitting at my desk and then applying the different kind of ‘styles’ on it. Each of these styles are is a separate #deeplearning model  that has learned how to apply the relevant style to a source image.

Style – Candy

Style – Feathers

Style – Mosaic

Style – Robert

Specifically, this uses a Neural Network (#DeepLearning) model called VGG19, which is a 19 layer model running on TensorFlow. Of course you can export this to a ONNX model, that then can be used in most other run-times and libraries.


This is inspired from Cornell universities paper – Perceptual Losses for Real-Time Style Transfer and Super-Resolution. Below is a snapshot of the VGG code that.

<br />
def net(data_path, input_image):<br />
layers = (<br />
'conv1_1', 'relu1_1', 'conv1_2', 'relu1_2', 'pool1',<br />
'conv2_1', 'relu2_1', 'conv2_2', 'relu2_2', 'pool2',<br />
'conv3_1', 'relu3_1', 'conv3_2', 'relu3_2', 'conv3_3',<br />
'relu3_3', 'conv3_4', 'relu3_4', 'pool3',<br />
'conv4_1', 'relu4_1', 'conv4_2', 'relu4_2', 'conv4_3',<br />
'relu4_3', 'conv4_4', 'relu4_4', 'pool4',<br />
'conv5_1', 'relu5_1', 'conv5_2', 'relu5_2', 'conv5_3',<br />
'relu5_3', 'conv5_4', 'relu5_4'<br />
)<br />
data =<br />
mean = data['normalization'][0][0][0]<br />
mean_pixel = np.mean(mean, axis=(0, 1))<br />
weights = data['layers'][0]</p>
<p>net = {}<br />
current = input_image<br />
for i, name in enumerate(layers):<br />
kind = name[:4]<br />
if kind == 'conv':<br />
kernels, bias = weights[i][0][0][0][0]<br />
# matconvnet: weights are [width, height, in_channels, out_channels]<br />
# tensorflow: weights are [height, width, in_channels, out_channels]<br />
kernels = np.transpose(kernels, (1, 0, 2, 3))<br />
bias = bias.reshape(-1)<br />
current = _conv_layer(current, kernels, bias)<br />
elif kind == 'relu':<br />
current = tf.nn.relu(current)<br />
elif kind == 'pool':<br />
current = _pool_layer(current)<br />
net[name] = current</p>
<p>assert len(net) == len(layers)<br />
return net</p>
<p>def _conv_layer(input, weights, bias):<br />
conv = tf.nn.conv2d(input, tf.constant(weights), strides=(1, 1, 1, 1),<br />
padding='SAME')<br />
return tf.nn.bias_add(conv, bias)</p>
<p>def _pool_layer(input):<br />
return tf.nn.max_pool(input, ksize=(1, 2, 2, 1), strides=(1, 2, 2, 1),<br />
padding='SAME')<br />

If you have interest to play with this, you can download the code. Personally, I like Mosaic style the best.

Artificial Intelligence (AI)

Yesterday it worked
Today it is not working
#AI is like that

#Haiku #GeekyHaiku #GeekyJokes

Error messages

Rather than a beep
Or a rude error message,
These words: “File not found.”

#Haiku #GeekyHaiku #GeekyJokes