Hello.
Welcome back.
So in this lesson we should begin deploying our more.
So what we're going to do is we going to let a wah wah wah e I give us two predictions.
It's first choice and it's second choice.
And basically what we're going to do is we're going to check the probabilities that the um the A.I.
is going to produce and we're going to select the first two highest probabilities and we're gonna make
that prediction number one and Prediction Number two because sometimes the um the number three and number
eight looks similar.
So we want to see what the E I can when you write 3-D.
I can give you less a choice number one 3 and choice number two US eight because of the similarities
in the I mean the two numbers.
So um one other thing we're going to do for our game is we're going to use a push button to reset the
game.
So whenever you whenever the A.I. does its prediction the push button can be pressed to start over.
So if you have no other that you can initialize the push button from the um from the PSP what is the
result.
There is a function in the PSP broad support package for initialize it.
It simply BSP underscore P4 push button and then it.
And over here you simply have to pass to argument button key and then zero as its initial state.
Okay.
Right.
So that the um after doing that we are going to create a structure to hold the um um the resort form
out a structure screen.
You have two members.
One is going to be the probability and then another one is gonna be the label which is the number which
is produce that the 0 1 2 3 4 5 2 was at a resort essentially.
So I'm gonna come up here and I'm gonna create a structure C struct C struct over here and then I'll
call this prediction underscore
underscore probability
and then open and close over here the first member is going to be of type A float and these data types
are defined um these are defined in Iowa a library.
So this is just a float but a float.
It s been renamed and the second type is gonna be a U int 8.
And we can use the data type of it into a library which we can see.
E I underscore you 8 4 on signed 8 bits integer.
We're gonna call this label right.
Once that is done I'm going to come here and see when I type if this to shorten its name so I'll say
typedef I've copied it paste it over here and then we're gonna call this print prop for prediction probability
type pred prop type that such a name.
Okay so we've got this this structure.
Yeah it's great to have our you know the resort basically.
Right.
And once this is done we can um we can we can create some data types we've got gotta create a buffer
to hold the input as well as another buffer to hold to the output.
So I'm gonna create an input buffer
um let's see.
I'm gonna I'm not gonna make this global.
I'm gonna keep in the main function.
So come over here and then I'm going to see a I underscore float I call this an end for neural network
input data and there's just gonna be a two by two.
It's first gonna take a number of rows which is the N N number of rows is the is the same as the image
height so see n n underscore input image height.
We've created a symbolic name for this sentence already and then number of columns is going to be the
with so n n underscore input image with like this.
Okay.
And um I'm gonna create I'm gonna create an instance of this structure to hold a resort.
I'm gonna copy this right prop type I'm gonna come over here and then I'm gonna have first prediction
1 s t print
and then I'm gonna have second prediction.
Let's go so I can print like this.
Okay.
Perhaps you can rename it to a better look in variable and then I'm going to initialize the probabilities
to 0.
So say first prediction
first on the score parade door to prop 8 cause.
Second prediction
I can parade to probe cause probably some member into.
Because your point is you're okay.
Yes.
Yeah.
Probably some member and the next thing I'm gonna do is I'm going to create a character buffer so that
we can send a predictions to our serial ports as well so that we'll be able to view it.
I'm gonna come over here
and see a car.
Uh uh.
Call this
first period string SDR and we could start with a size of ten and then we'll have second
the size of 10 as well.
Okay.
Once that is done I'm gonna have a buffer for the um for the output.
Remember the output is going to is going gonna be um the size of the output buffer should be equal to
the size of the number of labels or the number of classes we have.
And we have 0 1 2 3 4 5 6 7 8 9.
So those are 10 classes for Eddie created a symbolic name to hold that number of classes exist over
here.
So come over here and say I float on this go float and then I'll create a buffer called an end output
underscored data to mean output buffer and the size of this is going to be number of classes like this.
Right.
So yeah um we've created these next.
Well we have to do is we have to grab what we draw on the on the LCD screen into a wire into what input
buffer.
Here we have to be able to grab what it what I use user draws on the screen into the input buffer.
So we do down next.
I'm gonna come over here to the part where we do what we're drawing.
So we said this but here this bit's over here's what reproduces the drawing this bit here gives us that
drawing.
So over here we're simply using a function that reproduces Detroit over here is the scaled down version
of the type that our neural network needs.
The 28 by 28 version is held in small x small access more y.
And we've been able to shift and redraw that in our small square here.
So we're going to use the same method that we used to draw the pixels to arrange the pixels in Iowa
into our data buffer.
And that's what I mean.
So to collect the pixels when I call a what input buffer here it's called and an input data come over
here and then 0 2 x plus 1 index x plus 1 and by X plus 1 I mean small x plus 1 and then small y plus
1 and then I'm going to pass because we are dealing with floats I cannot pass to Phi Phi so I'm going
to pass the pixel set which I created a symbolic name for over here I'm gonna pass that over here like
this and then all I'm gonna do is copy and paste this and just change the index next I'm gonna do X
minus one Y minus one just following the same sequence or just following the same arrangement we used
for drawing the pixels up here and after that I'm gonna do a whole ball for got X minus one here we've
got X plus one I'm gonna do X plus 1 is simply y over here
then for the fourth row I'm gonna do X minus 1 and Y X minus one over here is simply y
and then after that I'm going to do X X plus one X Y plus one come over here X over here.
No y plus 1 and then I'm gonna do X Y minus 1
Is this over here we have X and then we have Y minus 1 right now I'm gonna do X plus 1 y minus one x
plus 1 y minus 1.
Now I'm gonna do X minus one Y plus 1 x minus 1
Um do we have the x minus one Y plus 1.
Okay.
We don't have it so X minus 1 y plus 1.
And then finally I'm gonna do x y you're right.
So they should be able to give us all the pixels.
Okay.
So after this we would have the the input the input buffer populate it.
And what we're going to do is we're going to print the um we're gonna print the position off your finger
or your stylus or your pointer.
Not serial port we're going to print that in 0 0 1 and so I should say so say x percentage to you over
here and then Y
percentage T.
And then I'm simply gonna pass you I on this score state DOT X and then you uh on that score state the
Y over here.
I'm gonna put a carriage return a new line here.
Okay so this was gonna happen if if you are drawing in the um in Detroit area we're going to first reproduce
exactly what you are drawing.
Scale it into a train eight by twenty eight and put it in the twenty eight by twenty eight square and
take that twenty eight by twenty eight and put it in a what neural network input data buffer like this.
So this was going to happen.
However if you are at the other side which is this part the ball the reset if you are the in the area
with regards to the reset button.
In other words when you press the reset button we want to clean the screen.
We want to reset the control panel or reset control panel reset this sentence or this function if it
should be you I reset cos it's reset the entire screen not just the control panel.
So I'm gonna change this plus when I refactor roots I'll come up here and um to refactor you simply
highlight this right click code to refactor rename and I'll call this user
interface on this code reset and then hit enter.
It should be able to change the name at all instances.
Okay so it's changed a name here.
Okay let's change the name here.
Okay.
So now our control panel said this quote you say interface reset.
So apart from resetting now you say interface.
What we also want to do is we want to be able to reset the entire game and then we have to create a
function to just sort of do that for us right.
And to reset we simply want to set let's say the labels and the prediction is back to zero so that they
can be repopulated to run your resource.
So I'm gonna create a function down here I call this function like void M next game.
Reset
like this and the first comment is going to be the input data.
Oh the uh pointer to the input data buffer.
I see a float stars the
data over here.
And then when I say print prop type prediction probability type a pointer to that type I call this first
prediction course we're going to our first print and second print first print like this.
Then we're going to have
print prop type again
second prediction like this.
Right.
And what we want to do is set the um sets the input buffer to zero.
We going to use memes sets to do this.
I'm gonna see a memo set
and then I'm going to see in data that zero point zero here and then we pass size off
in data like zero like this.
Multiply by the the input image with an input image height which we've got.
Symbolic names represent and already input image height API just paste this and then rename this
like this.
All right.
So I've got a pool close the brackets over here.
Dennis call on over here once we've set once we've said the entire input buffer to zero zero point zero.
We've got to reset the label and disarm the probability values.
So come down here and see the second prediction.
Second prayed that we're using a pointer here.
So we have to use the operator label
because
the second prediction
probability and of course
the first prediction label it calls first prediction
probability all of this because you have points you like this.
So this will initialize or reset everything for us as well.
Okay.
So I'm going to take the prototype of this put out a top as well.
I'll put it over here and we're going to call this function in a way.
You raise a button such that whenever one presses the reset button to the entire game is reset as well
okay.
So I'm gonna go to the um the while one loop over here we said when you press t you raise we reset the
U.S. to face but now we want to reset the game as well.
So come over here and then the first argument is going to be addressed to inputs buffer.
So out take the numbers on sine and then copy the name and input data.
The second argument is going to be the address to the um to the first prediction type we're going to
have a local variable called first prediction where we're going to store the first prediction.
So I'll call this 0 0 past the M percent to indicate it s it s address and then the third argument of
course is the second prediction.
So like this this will sort of resets the game for us.
Okay.
Right.
So that is done.
So the next thing we want to do for our API application is when we press that push button which is the
blue button we want to run to you phrase we want it in French.
Okay.
When we press the push button and we want the results to be produce on the screen when we press the
push button the um the data should be taken put it through our new uh put through on your network and
then to resort to produced the results taken and then printed on the screen.
So we've got to implement that here and we've got to implement it over here in the same place in our
while one loop.
So while one starts by checking by trying to detect whether um the screen has been touched after we've
checked for touch detection on this screen we can check for the push button as well.
So that's what we're gonna do next.
So I'm gonna say if if PSP underscore P B for push button we use to get state to function get state
and then let states guess its function takes one argument which is the button whose state you want to
get so button key here.
This is already defined in our PSP and this corresponds to the pin of the arm of the blue push button
on our microcontroller board.
So if this is true if it gets the state and it's true
I'm gonna we're gonna implement this we're gonna change this a bit and implement it.
Okay.
If this is true what we want to do is run this over here.
This we would change it a bit rather than take no argument.
We're going to pass a what inputs data buffer as well as our output data buffer.
So um the first argument is going to be the input buffer.
So we're going to pass this to and makes a process and we're going to change the prototype of this function
as well.
Later on in other files you'll see an underscore input underscored data.
Does the first I comment and then the second argument is gonna be an underscore output data.
Right.
So once we've done this once with passed that what input buffer no triple buffer to our neural network.
We create a for loop to determine the prediction.
I'm gonna say we're going to find out which is the number one prediction and then number two prediction.
We can just use a loop over here for int for int i equals zero.
I is less than number of classes
I plus plus now we see
if
first prediction probability by doing first pred dot probe
it first prediction probability is greater than an output data index.
I
we open this block and then we see second prediction that s probability because first prediction that
s probability of course.
Uh second prediction label.
Sorry April you cause first prediction label
and then second prediction probability of first prediction probability.
Yeah.
Or should we do that.
Yeah no problem because we want to view that probability as well I was thinking let's just viewed a
label but we want to know what probability it is.
Right.
So we can get the label we can print out the the probability as well as the label not just a label.
So I see second second prediction.
DOT's probe
because it's called first prediction probe.
I was hoping to process just the m the label but let's process both prob like this and then we see first
prediction
probe equals output
data index.
I like this.
And then my first prediction label is simply I
okay.
Once that is done we can print out the first and second prediction as well as the um.
The prediction labels as well as the probabilities both first and second so simply to a print off over
here and then I'll say percentage.
Percentage F for the probability and then New Line courage return.
What I'm gonna do here is a first prediction
Dutch label and then first prediction
probability like this
okay we solve this later.
She
do not change it is the second prediction.
Actually I'm gonna bring this out of the for loop.
Come this over here.
The for loop this over here and go on a bit of context to it's also a first case of order and then a
second guess
okay.
Let's see what is a process.
Says go to project properties settings to settings and odd print of project properties.
C++ beauty etc..
Okay.
Right click properties C++ build settings to settings.
Okay.
And then we take this and then we take this applying close.
Okay it looks good.
Okay.
Right.
So once we've done this what we're gonna do is we're going to print only the first prediction on to
a while until our LCD screen and to do that we do s print f
what I'm going to do here is pass first prediction first prediction dotcom string.
We've got a kid like this and put it into a string and then we will pass the string to this.
So
so we agreed to take the first prediction put it into a strength so that we'll be able to pass the string
onto our LCD screen so to do this I'm gonna call our buffer that we created over here first prediction.
Uh prediction SDR put us put this over here and then it's off type percentage D.
And then comma is first prediction label.
That is what we want to store here.
So copy this and then dot label over here.
Once this is done we can print it on our LCD screen and we're gonna print it with text color red.
So do BSP on the score LCD on the score set to text color and then I'm going to pass right over here
LCD
color red and then
PSP and the score LCD and a score set font.
Gonna set a font size of 20 bypassing the address font 20 over here once that is done I'm gonna set
the background color to the same color as the um as the LCD as everything else I should say PSP OCD
on a score set back a color so that the texture wouldn't stand out.
You know there's a function called set back color and a color we're using is generally LCD color light
cyan like this and watch all of this is done.
One can print the um the text on the screen by using the display screen got function.
So see PSP on the score LCD display string at
we gonna put it a position exposition one thirty and one uh y position two five five if it doesn't fit
properly we're going to readjust it what I'm gonna do is typecast this to you in eight on a score t
pointer
and what we want to place here is this string here which we've assembled
and I'm gonna set it to left mode over here on a scroll mode right so to read more about this function
you can certainly check it out here okay or double click go to declaration to get more information with
regards to this LCD function once we've done this we can reset the game I'm gonna call this a research
function from here I'm gonna put it over here like this right.
So this what we have so far so in the next lesson what we're going to do is go and implement what we
need to implement in our up underscore X cube a I don't see file after that is done we can test out
our deployment but that's all there is for this lesson.
It's getting a bit long so we should continue in the next lesson.