WEBVTT

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

00:00.840 --> 00:06.080
In this video we're going to create the load model file function under the init block.

00:06.200 --> 00:09.960
Start with private function load model file.

00:10.080 --> 00:18.920
Taking into the parameters a model file name of type string and returning map byte buffer.

00:18.960 --> 00:23.840
Let me explain what is the signature of this function?

00:23.840 --> 00:30.280
Mapped byte buffer is a memory mapped file containing the model file.

00:30.320 --> 00:39.360
Here we need to start with val file descriptor context dot assets dot open folder and we specify the

00:39.360 --> 00:41.200
model file name parameter.

00:41.440 --> 00:51.280
This appends the model file from apps folder, returning an asset file descriptor containing file metadata.

00:51.400 --> 00:57.640
Then we need to create the input stream from the file descriptor.

00:57.840 --> 01:05.910
This provides access to read the files raw bytes so val input stream file descriptor.

01:06.190 --> 01:07.910
Create input stream.

01:08.070 --> 01:17.790
The third step is to get file channel from the input stream val file channel equals to input stream

01:17.790 --> 01:18.950
dot channel.

01:18.990 --> 01:25.550
File channel provides memory mapped capabilities to access the files data.

01:25.590 --> 01:31.710
Then we need to create and get file position information.

01:31.710 --> 01:42.150
Start with val start offset equals to file descriptor dot start offset and val declared length file

01:42.150 --> 01:50.830
descriptor dot declare length the start offset position where the actual file data begins, which is

01:50.830 --> 01:56.790
usually zero, and the declared length total size of the file in bytes.

01:56.790 --> 02:05.150
I want from you to write those notes down, because they are very important and help you to, uh, to

02:05.190 --> 02:07.510
memorize what we've done before.

02:07.870 --> 02:13.550
So offset from the beginning and declared length length of the file in the bytes.

02:13.590 --> 02:16.990
The fifth step is memory mapping.

02:17.190 --> 02:23.550
We use memory mapping for efficient loading file isn't fully loaded into heap memory.

02:23.710 --> 02:25.430
Fast access.

02:25.710 --> 02:29.390
Direct memory access to file content.

02:29.430 --> 02:34.950
Low memory usage only needed portions are loaded on demand.

02:35.070 --> 02:37.150
Ideal for large files.

02:37.150 --> 02:44.670
Perfect for machine learning models that can be several memory consuming without memory mapping.

02:44.870 --> 02:46.270
It's inefficient.

02:46.350 --> 02:55.190
This would load entire file into memory, which is less efficient, so we use memory mapping for efficient

02:55.190 --> 03:02.550
loading, fast access, low memory usage, and ideal for large files.

03:02.550 --> 03:10.830
So here returning the map buffer using return file channel dot map.

03:11.070 --> 03:19.900
And here we use the file channel Alt Plus enter to import file channel map mode read only and parameter

03:19.900 --> 03:25.940
number two start offset parameter number three declared length okay.

03:26.180 --> 03:28.660
So this is the memory mapping.

03:28.940 --> 03:35.020
We use it for efficient loading, fast access, low memory usage and idle for large files.

03:35.020 --> 03:43.060
Without it, it will load directly all the file, all the resources, all files into memory.

03:43.060 --> 03:44.460
And this is inefficient.

03:44.700 --> 03:54.580
So here we are creating map byte buffer that directly reference the file data in memory using this approach.

03:54.740 --> 04:04.420
As I showed you here in this video, providing the most efficient way to load TensorFlow Lite models

04:04.420 --> 04:12.100
on Android, minimizing memory usage while ensuring fast model access during inference.
