WEBVTT

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

00:01.040 --> 00:04.160
Now let's create the Loadmodel function.

00:04.200 --> 00:10.200
Outside the initialize function, create a new function named as load mode.

00:10.200 --> 00:12.840
This function takes a string.

00:13.040 --> 00:18.880
Name it as file name as a parameter and returns byte buffer.

00:18.920 --> 00:26.000
This function loads a file from assets and returns it as a byte buffer.

00:26.040 --> 00:27.840
Let me write this comment below.

00:28.000 --> 00:33.200
Returns null if loading fails, so I use this question mark.

00:33.400 --> 00:39.280
So here we need to load the model and returns it as byte buffer.

00:39.520 --> 00:44.120
Uses memory mapping for efficient file access.

00:44.160 --> 00:48.600
Let me start by opening the model file.

00:48.760 --> 00:52.200
So here return return try.

00:52.240 --> 00:58.480
Let me try to open the file model file or my model whatever you want.

00:58.600 --> 01:04.200
Context dot dot open folder.

01:04.240 --> 01:05.720
What's the name of the folder?

01:05.720 --> 01:13.740
Is the file name parameter that it's best to do the conversion of the file to bytebuffer.

01:14.020 --> 01:25.380
We need to start with the input stream equals to file input stream and set the model file dot file descriptor.

01:25.420 --> 01:31.060
The first line appends the file from the app's assets folder.

01:31.060 --> 01:40.460
Using assets file descriptor provides access to the file descriptor, start offset, and declared length,

01:40.460 --> 01:47.500
while the second line creates a file input stream from the file descriptor.

01:47.780 --> 01:52.180
This allows reading the file as a stream of bytes.

01:52.180 --> 01:53.060
So here.

01:53.220 --> 02:00.180
Step number two reading the model file as stream byte stream of bytes.

02:00.300 --> 02:12.460
The third step is the file channel val file channel equals to input stream dot channel file channel

02:12.580 --> 02:13.980
dot map.

02:14.140 --> 02:23.250
We need to pass three parameters the file channel, the start of set and the declared length or size

02:23.250 --> 02:24.490
of the file channel.

02:24.530 --> 02:27.090
Alt+ enter to import the file channel.

02:27.090 --> 02:36.330
Obtains a file channel from the input stream, and the file channel provides methods for file operations,

02:36.330 --> 02:38.530
including memory mapping.

02:38.650 --> 02:45.890
I want from you to write those those comments below because they are very important okay.

02:45.930 --> 02:51.210
And helps you to understand the code, the file channel.

02:51.250 --> 02:52.250
Here we are.

02:53.410 --> 02:56.810
We are making a memory map of the file.

02:57.170 --> 03:00.330
Mapping the file to the memory okay.

03:00.450 --> 03:10.530
File channel dot map mode read only maps the file as read only model file dot start offset starting

03:10.570 --> 03:16.130
position in the file usually zero and the declared length.

03:16.170 --> 03:19.450
The length of the file to the map.

03:19.450 --> 03:29.110
Returns a map byte buffer that directly represents the file content in the Memory.

03:29.150 --> 03:37.150
Why we need memory mapping we need for file content is loaded on demand.

03:37.150 --> 03:46.590
Lazy loading, which is efficient memory mapping is used for fast access direct memory access without

03:46.590 --> 03:56.150
copying data and OS managed because operating system handles paging and caching.

03:56.310 --> 04:02.230
So those are the advantages of mapping and memory mapping.

04:02.390 --> 04:11.630
Okay, now we need to catch any errors that may result from this mapping and this conversion.

04:11.630 --> 04:18.990
So for that we need to start with catch here E exception.

04:18.990 --> 04:27.950
And we need to log e tag and load it model print the message and return null okay.

04:28.150 --> 04:32.270
So this is the load model function.

04:32.270 --> 04:37.270
This is how we load the model and convert it as byte buffer.
