Welcome back.
Now let's create this function called load model file.
So here private function.
Load model file takes a context object and returns byte buffer.
So the purpose from this function is to load a TensorFlow Lite mode model from assets or from the ML
folder here into a direct byte buffer.
And the input is Android context to access the app context and assets.
And the output would be a buffer, which is a byte buffer containing the model.
Ready for TensorFlow Lite interpreter.
The first step here is to get the model path and name.
So val asset file descriptor equals to context dot assets dot open folder.
And give me the name here which is car mileage prediction.tf light.
Make sure that the name is exactly like you specified here.
Otherwise your your model will not be loaded successfully.
So here car mpg and not mileage.
So you see guys this is a big error.
Car mpg underscore prediction dot df light okay.
And this is good.
We are we get a green highlight.
So it refers to the file.
Now here the context dot asset dot FD gets a file descriptor from the model file.
This is more efficient than just opening a stream because it provides direct file access.
And the model file here which is in the ML car MPG prediction flight should be located in the assets
folder or the ML folder.
Okay, so it's very simple.
Now the second step is to create the input Stream.
So file input stream.
Asset file descriptor.
This is the descriptor disk descriptor dot.
Create input stream.
This.
This line creates the input stream from file descriptor.
This allows you to read the model file as a stream of bytes.
So let me write this note down.
This allows you to read the model file as a stream of bytes.
Then we need to create a file channel.
Or you can simply remove it and use var val bytes equals to input file input stream that we created
before dot read bytes.
Since our function need to return a byte buffer, we need to use bytes.
So file input stream dot read bytes.
This reads all bytes from the stream.
Reads the entire content of the model file into a byte array.
The byte variable now contains the complete binary data of your TensorFlow Lite model.
Now we need to set the model buffer.
So val model buffer equals to byte buffer dot.
Allocate direct and bytes dot size to allocate direct function creates a direct byte buffer outside
the normal Java heat.
This is crucial for TensorFlow Lite because native code C++ TensorFlow Lite Lite engine can access it
directly, avoiding unnecessary memory copies between JVM and native code, also providing better performance
for model inference.
So this is a very important step.
Now we need to continue with model buffer.
So model buffer.org set the byte Order dot native order.
This line of code model buffer dot order sets the byte order to match the device's native byte order.
Little endian and big endian.
The.
This line of code ensures the model data is interpreted correctly by the native TensorFlow Lite code,
and it's very important for cross, cross multiple or cross-platform compatibility or multi-platform
compatibility.
Okay, so we need to use it when we have an interpreter interpreter.
And we need to interpret data correctly from the model.
Then we need to copy the data model data into the buffer.
So copy model data into buffer model buffer dot.
Put the bytes and close the input stream.
This will copy the byte array containing the model into the direct byte buffer.
The buffer now contains the complete TensorFlow Lite model in native memory.
And here returning the model buffer.
So here under the model buffer set return model buffer okay.
Since our application our function need needs the byte buffer as a returning type.
So in this video we created our load model file as we saw it in the previous videos.
And in the previous section, we need to load our prediction model that we created from Colab, and
we need to load it into this file, this this variable and it's of type asset file descriptor.
And here we use the file input stream load loading the asset file descriptor.
Also we transform them into bytes and then buffer and returning as a model buffer.