WEBVTT

00:00.080 --> 00:01.040
Welcome back.

00:01.040 --> 00:04.440
Now let's create this function called load model file.

00:04.440 --> 00:07.520
So here private function.

00:07.520 --> 00:15.360
Load model file takes a context object and returns byte buffer.

00:15.480 --> 00:24.760
So the purpose from this function is to load a TensorFlow Lite mode model from assets or from the ML

00:24.800 --> 00:28.600
folder here into a direct byte buffer.

00:28.600 --> 00:34.760
And the input is Android context to access the app context and assets.

00:34.760 --> 00:40.640
And the output would be a buffer, which is a byte buffer containing the model.

00:40.640 --> 00:43.560
Ready for TensorFlow Lite interpreter.

00:43.760 --> 00:49.600
The first step here is to get the model path and name.

00:49.840 --> 00:58.880
So val asset file descriptor equals to context dot assets dot open folder.

00:58.880 --> 01:07.930
And give me the name here which is car mileage prediction.tf light.

01:07.970 --> 01:13.770
Make sure that the name is exactly like you specified here.

01:13.810 --> 01:18.690
Otherwise your your model will not be loaded successfully.

01:18.690 --> 01:21.850
So here car mpg and not mileage.

01:22.130 --> 01:25.050
So you see guys this is a big error.

01:25.170 --> 01:32.090
Car mpg underscore prediction dot df light okay.

01:32.250 --> 01:33.530
And this is good.

01:33.770 --> 01:37.090
We are we get a green highlight.

01:37.090 --> 01:39.370
So it refers to the file.

01:39.410 --> 01:48.050
Now here the context dot asset dot FD gets a file descriptor from the model file.

01:48.250 --> 01:54.690
This is more efficient than just opening a stream because it provides direct file access.

01:54.930 --> 02:04.130
And the model file here which is in the ML car MPG prediction flight should be located in the assets

02:04.130 --> 02:06.770
folder or the ML folder.

02:06.930 --> 02:09.810
Okay, so it's very simple.

02:09.850 --> 02:14.850
Now the second step is to create the input Stream.

02:14.850 --> 02:17.450
So file input stream.

02:17.770 --> 02:19.770
Asset file descriptor.

02:19.810 --> 02:24.090
This is the descriptor disk descriptor dot.

02:24.130 --> 02:26.210
Create input stream.

02:26.370 --> 02:27.170
This.

02:27.370 --> 02:32.330
This line creates the input stream from file descriptor.

02:32.490 --> 02:37.130
This allows you to read the model file as a stream of bytes.

02:37.170 --> 02:40.330
So let me write this note down.

02:40.330 --> 02:46.770
This allows you to read the model file as a stream of bytes.

02:46.930 --> 02:51.170
Then we need to create a file channel.

02:51.170 --> 03:02.690
Or you can simply remove it and use var val bytes equals to input file input stream that we created

03:02.690 --> 03:05.010
before dot read bytes.

03:05.250 --> 03:11.930
Since our function need to return a byte buffer, we need to use bytes.

03:11.930 --> 03:15.610
So file input stream dot read bytes.

03:15.730 --> 03:19.970
This reads all bytes from the stream.

03:20.130 --> 03:24.780
Reads the entire content of the model file into a byte array.

03:25.260 --> 03:31.940
The byte variable now contains the complete binary data of your TensorFlow Lite model.

03:31.980 --> 03:35.100
Now we need to set the model buffer.

03:35.100 --> 03:42.140
So val model buffer equals to byte buffer dot.

03:42.180 --> 03:53.540
Allocate direct and bytes dot size to allocate direct function creates a direct byte buffer outside

03:53.540 --> 03:55.300
the normal Java heat.

03:55.340 --> 04:05.060
This is crucial for TensorFlow Lite because native code C++ TensorFlow Lite Lite engine can access it

04:05.140 --> 04:14.100
directly, avoiding unnecessary memory copies between JVM and native code, also providing better performance

04:14.100 --> 04:16.500
for model inference.

04:16.500 --> 04:18.860
So this is a very important step.

04:19.060 --> 04:22.620
Now we need to continue with model buffer.

04:22.620 --> 04:30.150
So model buffer.org set the byte Order dot native order.

04:30.270 --> 04:38.830
This line of code model buffer dot order sets the byte order to match the device's native byte order.

04:38.870 --> 04:41.630
Little endian and big endian.

04:41.670 --> 04:42.230
The.

04:42.550 --> 04:50.950
This line of code ensures the model data is interpreted correctly by the native TensorFlow Lite code,

04:51.110 --> 05:00.070
and it's very important for cross, cross multiple or cross-platform compatibility or multi-platform

05:00.070 --> 05:01.030
compatibility.

05:01.070 --> 05:07.950
Okay, so we need to use it when we have an interpreter interpreter.

05:08.070 --> 05:11.590
And we need to interpret data correctly from the model.

05:11.630 --> 05:16.710
Then we need to copy the data model data into the buffer.

05:16.710 --> 05:20.990
So copy model data into buffer model buffer dot.

05:21.070 --> 05:25.110
Put the bytes and close the input stream.

05:25.270 --> 05:31.470
This will copy the byte array containing the model into the direct byte buffer.

05:31.710 --> 05:37.910
The buffer now contains the complete TensorFlow Lite model in native memory.

05:37.910 --> 05:42.590
And here returning the model buffer.

05:42.590 --> 05:49.870
So here under the model buffer set return model buffer okay.

05:50.310 --> 05:57.950
Since our application our function need needs the byte buffer as a returning type.

05:57.950 --> 06:05.710
So in this video we created our load model file as we saw it in the previous videos.

06:05.710 --> 06:15.310
And in the previous section, we need to load our prediction model that we created from Colab, and

06:15.310 --> 06:22.790
we need to load it into this file, this this variable and it's of type asset file descriptor.

06:22.790 --> 06:30.070
And here we use the file input stream load loading the asset file descriptor.

06:30.110 --> 06:37.510
Also we transform them into bytes and then buffer and returning as a model buffer.
