1
00:00:00,630 --> 00:00:02,550
Lesson 11, Project five.

2
00:00:02,760 --> 00:00:10,020
Intelligent, mature station with BME 280 specifically for model training and development.

3
00:00:10,650 --> 00:00:17,190
So the model you trained in the previous phase was transformed to a binary, including a model structure

4
00:00:17,490 --> 00:00:25,230
and weights, and it can be now imported into wheel terminal together with the C++ code, which I believe

5
00:00:25,230 --> 00:00:27,450
is somehow familiar to you, right?

6
00:00:28,200 --> 00:00:35,280
Basically, TensorFlow Lite for Microcontrollers offers a model interpreter that is minimal and quick

7
00:00:35,760 --> 00:00:39,450
to achieve low load start up and execution time.

8
00:00:39,870 --> 00:00:46,740
The interpreter employs static, ordering any specialized or less dynamic memory allocator.

9
00:00:47,220 --> 00:00:54,960
The data in the output buffers is fed into the model graph and the results are deposited in the output

10
00:00:54,960 --> 00:00:55,440
buffer.

11
00:00:55,740 --> 00:01:02,730
After the inference is completed and in order to reduce the size of the model and decrease inference

12
00:01:02,730 --> 00:01:06,690
time, we perform two important optimizations.

13
00:01:06,870 --> 00:01:13,110
First is that perform full integer quantization changing model, which inputs and outputs from floating

14
00:01:13,110 --> 00:01:21,870
point thirty two numbers and each one occupying 32 bits of memory to integer eight numbers, each occupying

15
00:01:21,870 --> 00:01:25,620
only eight bits, thus reducing size by a factor of four.

16
00:01:25,620 --> 00:01:33,210
Right now, use micrometer table opera solver and specify operations that we have in neural network

17
00:01:33,570 --> 00:01:39,990
to compile our code only with the operations needed to run the model, as opposed to using all ops resolver,

18
00:01:40,380 --> 00:01:46,110
which includes all operations supported by current TensorFlow Lite for microcontrollers interpreter.

19
00:01:46,620 --> 00:01:53,040
So in preparation, you need to make sure you have libraries for Wi-Fi and LV installed because we will

20
00:01:53,040 --> 00:01:55,320
need them for our expansion tasks.

21
00:01:55,800 --> 00:02:02,640
As we discussed further along now to start the practice, once the model training is finished, it says

22
00:02:02,640 --> 00:02:05,430
here that create an M.D. sketch and then save it.

23
00:02:06,120 --> 00:02:12,630
Then after that, copied the model, you train to the sketch folder and then re-open the sketch right

24
00:02:12,630 --> 00:02:13,200
after that.

25
00:02:13,530 --> 00:02:21,180
Change the variable name of model to something short term, and as this year used to code from real

26
00:02:21,180 --> 00:02:28,740
terminal def, make your weather prediction static that enum, which you can find in the course materials

27
00:02:28,740 --> 00:02:29,550
for testing.

28
00:02:30,120 --> 00:02:37,260
Now, go over the main steps we have in the C++ code, and then we'll also include the headers for Tensor

29
00:02:37,260 --> 00:02:40,200
Library and the file with model flood buffer.

30
00:02:41,100 --> 00:02:43,740
So now notice how different things look.

31
00:02:44,130 --> 00:02:50,730
It's better to use micrometer mid-table opera solver not h after you finish testing because it will

32
00:02:50,730 --> 00:02:55,360
use less memory on your device and it's easier to use than all.

33
00:02:55,400 --> 00:03:00,150
Obsess over that, each for testing, but it's better to use micro, mutable up.

34
00:03:00,180 --> 00:03:01,860
Resolver that each when you're done.

35
00:03:02,580 --> 00:03:09,390
And after that, set up the pointers for the error reporter and the model, the input and output, then

36
00:03:09,390 --> 00:03:10,710
source and interpreter.

37
00:03:11,160 --> 00:03:18,420
It's easy to see that our model has two outputs one for precipitation amount and one for the type of

38
00:03:18,420 --> 00:03:19,320
weather we're having.

39
00:03:19,830 --> 00:03:26,880
We'll also talk about the Tensor Arena, which you can think as a scratch board that wants input output

40
00:03:26,880 --> 00:03:28,560
and intermediate arrays.

41
00:03:29,070 --> 00:03:36,480
The size of the arena will depend on the model you are using and may need to be determined by testing.

42
00:03:37,170 --> 00:03:44,970
Then, in setup function, it does more boring things like setting up error reporters, opera solvers,

43
00:03:44,970 --> 00:03:47,010
interpreters and mapping the model.

44
00:03:47,370 --> 00:03:51,900
It also allocate benzos and checks their shapes after allocating them.

45
00:03:52,410 --> 00:03:57,600
This is when the code might not work at all because some of the model operations are not supported by

46
00:03:57,600 --> 00:04:00,270
the current version of the TensorFlow Micro Library.

47
00:04:00,720 --> 00:04:07,200
Changing the model's architecture or adding the support for the unsupported operation yourself, usually

48
00:04:07,200 --> 00:04:13,230
by porting it from TensorFlow Lite, is actually the best way to get the support for the unsupported

49
00:04:13,230 --> 00:04:13,920
operation.

50
00:04:14,370 --> 00:04:22,620
And now finally, loop function we define as a placeholder for a quantized AI, anti-hate values and

51
00:04:22,920 --> 00:04:29,940
an array with float values, which you can copy paste from the book for comparison of model inference

52
00:04:29,940 --> 00:04:32,300
on device versus in column.

53
00:04:32,970 --> 00:04:39,870
And now on this step, we quantized of float values into the AI into eight in four loop and then placed

54
00:04:39,870 --> 00:04:42,230
them in the input tensor one by one.

55
00:04:42,240 --> 00:04:43,890
So you can see there in the codes.

56
00:04:44,490 --> 00:04:51,960
Then inference is performed by TensorFlow micro interpreter and if no errors are reported, values are

57
00:04:51,960 --> 00:04:52,990
placed in the output.

58
00:04:52,990 --> 00:04:57,270
10 Source check and compare the values for the same data point.

59
00:04:57,720 --> 00:04:59,880
They should be the same for quantized TensorFlow.

60
00:05:00,010 --> 00:05:03,610
Eight model in CoLab notebook and TensorFlow micro model.

61
00:05:03,670 --> 00:05:11,050
Running on your real terminal, meaning there should be no difference in values between quantized TensorFlow

62
00:05:11,050 --> 00:05:18,160
Lite in CoLab Notebook and TensorFlow Micro running on your real terminal when it comes to the same

63
00:05:18,160 --> 00:05:18,790
data point.

64
00:05:19,400 --> 00:05:26,060
Now the next step here is that open to sketch real terminal def micro weather prediction static that

65
00:05:26,080 --> 00:05:30,340
we know from course materials and have a look at its contents.

66
00:05:31,240 --> 00:05:34,150
Parts of the code are broken down into the main sketch.

67
00:05:34,360 --> 00:05:38,860
We have get historical data and UI parts.

68
00:05:39,520 --> 00:05:45,670
The first inference will have to wait for 24 hours because our model needs data from the last 24 hours

69
00:05:46,000 --> 00:05:47,350
to solve this problem.

70
00:05:48,310 --> 00:05:54,820
We get the weather from the last 24 hours from open weather that come and can do the first inference

71
00:05:54,880 --> 00:05:56,650
right after the device starts up.

72
00:05:57,130 --> 00:06:04,560
Then we replace the values in a circular buffer with the temperature, humidity and pressure from BME

73
00:06:04,570 --> 00:06:06,940
to 80 sensor connected to the real thing.

74
00:06:07,120 --> 00:06:10,750
Now I do see Groves suck in a library called LV.

75
00:06:10,750 --> 00:06:12,970
Gale was used to make the UI.

76
00:06:13,480 --> 00:06:19,540
It's a small and useful graphic library, so we'll have to build and upload the code.

77
00:06:19,810 --> 00:06:25,480
Make sure you make changes to your Wi-Fi credentials and location in sketch before you upload it.

78
00:06:25,480 --> 00:06:27,520
OK, connect to the internet.

79
00:06:27,520 --> 00:06:34,020
After you upload, the device will get the data about your location for the last 24 hours and makes

80
00:06:34,030 --> 00:06:38,890
its first guess before getting data from the BME to sorry.

81
00:06:39,280 --> 00:06:42,520
It will wait for an hour if there is no sensor connected.

82
00:06:42,670 --> 00:06:44,410
The program won't start up at all.

83
00:06:44,920 --> 00:06:46,600
Now this is how it works.
