lmob 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,374 @@
1
+ Metadata-Version: 2.4
2
+ Name: lmob
3
+ Version: 1.0.0
4
+ Summary: Automated PyTorch-to-Android conversion and benchmarking on physical mobile devices
5
+ Author-email: ABrain One and contributors <AI@ABrain.one>
6
+ License: MIT License
7
+
8
+ Copyright (c) 2025- ABrain One and contributors; PyTorch to TensorFlow Lite neural network model converter (c) 2025 Andrey Ignatov
9
+ All rights reserved.
10
+
11
+ Permission is hereby granted, free of charge, to any person obtaining a copy
12
+ of this software and associated documentation files (the "Software"), to deal
13
+ in the Software without restriction, including without limitation the rights
14
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15
+ copies of the Software, and to permit persons to whom the Software is
16
+ furnished to do so, subject to the following conditions:
17
+
18
+ The above copyright notice and this permission notice shall be included in all
19
+ copies or substantial portions of the Software.
20
+
21
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27
+ SOFTWARE.
28
+
29
+ Project-URL: Homepage, https://ABrain.one
30
+ Project-URL: Repository, https://github.com/ABrain-One/nn-lite
31
+ Project-URL: Bug Tracker, https://github.com/ABrain-One/nn-lite/issues
32
+ Keywords: on-device inference,Android,mobile AI,benchmarking,TensorFlow Lite,LiteRT,quantization,NNAPI,PyTorch,edge AI
33
+ Classifier: Programming Language :: Python :: 3
34
+ Classifier: License :: OSI Approved :: MIT License
35
+ Classifier: Operating System :: OS Independent
36
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
37
+ Classifier: Topic :: System :: Benchmark
38
+ Classifier: Intended Audience :: Developers
39
+ Classifier: Intended Audience :: Science/Research
40
+ Requires-Python: >=3.10
41
+ Description-Content-Type: text/markdown
42
+ License-File: LICENSE
43
+ Requires-Dist: nn-lit==1.0.0
44
+ Provides-Extra: emulator
45
+ Requires-Dist: nn-dataset; extra == "emulator"
46
+ Dynamic: license-file
47
+
48
+ # Mobile-Ready AI: Verification and Deployment on Edge Devices
49
+
50
+ <img src='https://abrain.one/img/nnlite-logo.png' width='25%'/>
51
+
52
+ The original open-source version of the <a href='https://github.com/ABrain-One/NN-Lite/'>NN Lite</a> was developed by <strong>Faraz Kayani</strong>, <strong>Saif U Din</strong> and <strong>Muhammad Ahsan Hussain</strong> at the Computer Vision Laboratory, University of Würzburg, Germany, under the supervision and technical guidance of <strong>Dr. Dmitry Ignatov</strong>, whose foundational work established the basis for the project.
53
+
54
+ NN-Lite measures how fast PyTorch models run on real Android phones. For every model in the
55
+ [LEMUR / NN Dataset](https://github.com/ABrain-One/nn-dataset) it:
56
+
57
+ 1. rebuilds the network and loads its trained weights,
58
+ 2. converts it to LiteRT (TensorFlow Lite) in **FP32** and full-integer **INT8** (calibrated on real
59
+ CIFAR-10 training images, prepared with the model's own input transform),
60
+ 3. copies it to a phone over USB and times it with the official `benchmark_model` tool on the **CPU**, **GPU** and **NPU (NNAPI)**,
61
+ 4. saves one JSON record per model, precision and device, in the same layout as the dataset.
62
+
63
+ Runs are unattended and resumable: NN-Lite waits if the USB cable is unplugged, lets the phone
64
+ cool down between models, restarts itself every 50 models (`--restart-every`), and records
65
+ failures instead of skipping them. An optional emulator path (Android Studio) is also included.
66
+
67
+ ## Requirements
68
+
69
+ - Linux (tested on Ubuntu) or macOS on Apple Silicon, with Python 3.10 or newer
70
+ - `adb` (Android platform tools): `sudo apt install adb` on Linux, `brew install --cask android-platform-tools`
71
+ on macOS, or the [SDK platform tools](https://developer.android.com/tools/releases/platform-tools)
72
+ - An Android phone with USB debugging enabled (see [Connect a phone](#connect-a-phone)); no root is needed
73
+ - An internet connection and about 2 GB of free disk space: the first run downloads the LEMUR
74
+ database (about 1.2 GB unpacked) and the CIFAR-10 training set used for INT8 calibration;
75
+ model weights are downloaded from Hugging Face as they are needed
76
+
77
+ ## Installation
78
+
79
+ Create and activate a virtual environment (recommended).
80
+
81
+ For Linux/Mac:
82
+ ```bash
83
+ python3 -m venv .venv
84
+ source .venv/bin/activate
85
+ python -m pip install --upgrade pip
86
+ ```
87
+ For Windows:
88
+ ```bash
89
+ python3 -m venv .venv
90
+ .venv\Scripts\activate
91
+ python -m pip install --upgrade pip
92
+ ```
93
+
94
+ Install NN-Lite from PyPI:
95
+ ```bash
96
+ pip install nn-lit
97
+ ```
98
+ This also installs the [NN Dataset](https://github.com/ABrain-One/nn-dataset) package, from
99
+ which NN-Lite reads the models, so nothing else needs to be cloned. Results are written to
100
+ `nn-lite-results/` in the folder you run NN-Lite from; see
101
+ [Contributing results to the dataset](#contributing-results-to-the-dataset) to add them to LEMUR.
102
+
103
+ Or install it from source:
104
+ ```bash
105
+ git clone https://github.com/ABrain-One/nn-lite.git
106
+ cd nn-lite
107
+ pip install -e .
108
+ ```
109
+
110
+ If `nn-lite-bench` stops with `ModuleNotFoundError: No module named 'ab.lite'`, your `PYTHONPATH`
111
+ includes a checkout of the NN Dataset, whose `ab` folder then hides the installed one
112
+ (`python -c "import ab; print(ab.__path__)"` shows which is used). Remove that folder from
113
+ `PYTHONPATH`, or run `unset PYTHONPATH`, and try again.
114
+
115
+ ### Working with an nn-dataset checkout
116
+
117
+ If you commit results to the NN Dataset, or benchmark models that are newer than the installed
118
+ package, NN-Lite can read the models from a git checkout of the dataset and write the results
119
+ straight into it. When NN-Lite is installed from source, a checkout next to it is found
120
+ automatically:
121
+ ```bash
122
+ cd ..
123
+ git clone https://github.com/ABrain-One/nn-dataset.git
124
+ ```
125
+ ```
126
+ your-folder/
127
+ ├── nn-lite/
128
+ └── nn-dataset/
129
+ ```
130
+ An `nn-dataset` checkout in the folder NN-Lite is run from is found automatically as well. A
131
+ checkout elsewhere is chosen with `--dataset-root /path/to/nn-dataset` or the `NN_DATASET_ROOT`
132
+ environment variable. NN-Lite prints at start-up where it reads the models from and where it
133
+ writes the results.
134
+
135
+ ## Connect a phone
136
+
137
+ 1. On the phone, open **Settings → About phone** and tap **Build number** seven times to enable developer options.
138
+ 2. Open **Settings → Developer options** (on some phones under **System**) and turn on **USB debugging**.
139
+ 3. Connect the phone to the computer with a USB cable and accept the **Allow USB debugging?** prompt on the phone.
140
+ 4. Check that the phone is visible:
141
+ ```bash
142
+ adb devices
143
+ ```
144
+ It should be listed with the state `device` (not `unauthorized`). With several phones
145
+ connected, see [Several phones](#several-phones).
146
+
147
+ Keep the phone charging during long runs. NN-Lite keeps the screen awake and copies the
148
+ `benchmark_model` binary to `/data/local/tmp` on the phone automatically.
149
+
150
+ ## Quick start
151
+
152
+ Benchmark a single model (a few minutes):
153
+ ```bash
154
+ nn-lite-bench --models AirNet
155
+ ```
156
+ From a source checkout, the same command is `python -m ab.lite.torch2tflite --models AirNet`.
157
+
158
+ NN-Lite converts `AirNet` to FP32 and INT8, times each file on the CPU, GPU and NPU with
159
+ 20 runs per backend, and writes (into the nn-dataset checkout instead, if one is used):
160
+ ```
161
+ nn-lite-results/ab/nn/stat/run/tflite/fp32/img-classification_cifar-10_acc_AirNet/android_<device>.json
162
+ nn-lite-results/ab/nn/stat/run/tflite/int8/img-classification_cifar-10_acc_AirNet/android_<device>.json
163
+ ```
164
+ An abridged record (latencies are in nanoseconds; `unit` is the fastest backend):
165
+ ```json
166
+ {
167
+ "model_name": "AirNet",
168
+ "device_type": "STK-L21",
169
+ "os_version": "10 | HUAWEISTK-L21",
170
+ "valid": true,
171
+ "emulator": false,
172
+ "iterations": 20,
173
+ "duration": 55200000,
174
+ "unit": "GPU",
175
+ "cpu_duration": 329480000, "cpu_min_duration": 310111000, "cpu_max_duration": 344513000, "cpu_std_dev": 9657000.0,
176
+ "gpu_duration": 55200000, "gpu_min_duration": 53553000, "gpu_max_duration": 60863000, "gpu_std_dev": 2064000.0,
177
+ "npu_duration": 364514000, "npu_min_duration": 357856000, "npu_max_duration": 371171000, "npu_std_dev": 6657000.0,
178
+ "total_ram_kb": 3775716, "free_ram_kb": 189496, "available_ram_kb": 1617764, "cached_kb": 1638264,
179
+ "in_dim_0": 1, "in_dim_1": 128, "in_dim_2": 128, "in_dim_3": 3,
180
+ "device_analytics": { "...": "CPU cores, SoC and ARM architecture of the phone" }
181
+ }
182
+ ```
183
+ If a backend fails, its error message is stored in `cpu_error`, `gpu_error` or `npu_error`; if
184
+ all three fail, the record is kept with `"valid": false`. The full output of every failure is
185
+ appended to `_work/benchmark_errors_<device>.log` in the results folder.
186
+
187
+ Benchmark every model (runs for hours; safe to stop and restart at any time):
188
+ ```bash
189
+ nn-lite-bench
190
+ ```
191
+
192
+ | Option | Meaning |
193
+ |---|---|
194
+ | `--models NAME [NAME ...]` | Only process these models |
195
+ | `--android-runs N` | Timed runs per backend (default 20) |
196
+ | `--dataset-root PATH` | Read models from this `nn-dataset` checkout and write results into it |
197
+ | `--out PATH` | Write results to this folder instead (default: the checkout, or `./nn-lite-results`) |
198
+ | `--model-path PATH [PATH ...]` | Benchmark your own models instead (see [Your own models](#your-own-models)) |
199
+ | `--serial SERIAL` | Benchmark this phone, as listed by `adb devices` (needed when several are connected) |
200
+ | `--restart-every N` | Restart the process after every N models to free memory (default 50; 0 never restarts) |
201
+ | `--force` | Forget the connected phone's progress and start from the beginning |
202
+ | `--reinstall-bench` | Copy `benchmark_model` to the phone again |
203
+
204
+ Progress is stored per phone model in `_work/processing_state_<model>.json` of the results folder; models
205
+ listed there as processed or failed are skipped when that phone model is benchmarked again,
206
+ while a phone of another model starts from the beginning. `--force` resets the progress of the
207
+ connected phone model only. Like the result files, progress is identified by the phone model, so
208
+ a second phone of the same model continues where the first one left off.
209
+
210
+ ### Several phones
211
+
212
+ Several phones can be benchmarked at the same time from one computer, each by its own run
213
+ of NN-Lite. Choose the phone of each run with `--serial` and the serial number that
214
+ `adb devices` lists for it:
215
+ ```bash
216
+ adb devices
217
+ # List of devices attached
218
+ # R58M12ABCDE device
219
+ # 2A281FDH300 device
220
+ nn-lite-bench --serial R58M12ABCDE # in one terminal
221
+ nn-lite-bench --serial 2A281FDH300 # in another terminal
222
+ ```
223
+ Without `--serial` (or the `ANDROID_SERIAL` environment variable), NN-Lite uses the only
224
+ phone connected, and stops with the list of phones if there are several. A run only ever
225
+ talks to its own phone, even if other phones are connected or reconnected during the run.
226
+
227
+ The runs can write into the same results folder: the downloads they share are made once,
228
+ and each phone has its own temporary folder. Phones of the same model share one progress
229
+ file and one result file per model, so they are benchmarked one after the other; a second
230
+ run for a phone model that is already being benchmarked stops with an error.
231
+
232
+ ## Your own models
233
+
234
+ NN-Lite also benchmarks models that are not part of the NN Dataset, without using the dataset at
235
+ all. Give the model files, or folders containing them, with `--model-path`:
236
+ ```bash
237
+ nn-lite-bench --model-path my_models/ --calib-dir sample_images/
238
+ ```
239
+ Each model is either
240
+
241
+ - a **`.pt2` file** saved with [`torch.export`](https://docs.pytorch.org/docs/stable/export.html):
242
+ it needs no Python code, and its input shape is stored in the file. Export the model in
243
+ evaluation mode (`model.eval()`), as the exported graph keeps the mode it was exported in:
244
+ ```python
245
+ torch.export.save(torch.export.export(model.eval(), (torch.randn(1, 3, 224, 224),)), "mymodel.pt2")
246
+ ```
247
+ - or a **`.py` file with a `.pt` or `.pth` file of the same name** next to it (`mymodel.py` and
248
+ `mymodel.pth`), holding either the weights (`torch.save(model.state_dict(), ...)`) or the whole
249
+ model (`torch.save(model, ...)`). NN-Lite builds the network with `create_model()` if the `.py`
250
+ file defines one, otherwise with `Net()` or the file's only model class. The input is
251
+ `--input-size` pixels square (default 224).
252
+
253
+ A `.pt` file alone is not enough: it holds the weights but not the code that defines the network,
254
+ so PyTorch cannot rebuild the model from it. Load whole-model files only from sources you trust,
255
+ as loading them can run code stored in the file.
256
+
257
+ FP32 is always benchmarked. INT8 needs sample inputs for calibration: pass a folder of images with
258
+ `--calib-dir` (up to 50 are used). They are resized to the model's input and normalised with the
259
+ ImageNet statistics, unless the `.py` file defines `input_transform`, a function that turns a PIL
260
+ image into a tensor. Results are written to `nn-lite-results/custom/{fp32,int8}/<model>/`, in the
261
+ same format as the dataset's records.
262
+
263
+ ## Contributing results to the dataset
264
+
265
+ The results folder has the same layout as the NN Dataset, so adding your measurements to LEMUR
266
+ takes three steps:
267
+ ```bash
268
+ git clone https://github.com/ABrain-One/nn-dataset.git
269
+ cp -r nn-lite-results/ab nn-dataset/
270
+ cd nn-dataset && git add ab/nn/stat/run/tflite && git commit -m "Add LiteRT benchmarks for <device>"
271
+ ```
272
+ Then open a pull request on the [NN Dataset](https://github.com/ABrain-One/nn-dataset) repository.
273
+ The `_work` folder (downloads, progress and logs) is not part of the dataset and is not copied.
274
+ From then on, NN-Lite run from the same folder finds this checkout and writes new results
275
+ straight into it.
276
+
277
+ ## Optional: emulator path (Android Studio)
278
+
279
+ The earlier version of NN-Lite runs models inside an Android emulator through the Android app in
280
+ `App/`. It uses the NN Dataset Python package installed with NN-Lite; to use its latest
281
+ development version instead:
282
+ ```bash
283
+ rm -rf db
284
+ pip uninstall -y nn-dataset
285
+ pip install --no-cache-dir git+https://github.com/ABrain-One/nn-dataset
286
+ ```
287
+
288
+ Install Android Studio 'Android Studio Narwhal 3 Feature Drop | 2025.1.3' (outside of the virtual environment) with the ready-made script (Linux):
289
+ ```bash
290
+ chmod +x install-android-studio.sh
291
+ ./install-android-studio.sh
292
+ ```
293
+
294
+ Or install it manually from the [Android Studio archive](https://developer.android.com/studio/archive):
295
+ ```bash
296
+ sudo apt update
297
+ sudo apt install openjdk-17-jdk
298
+ cd ~/Downloads
299
+ unzip android-studio-*.zip
300
+ sudo mv android-studio /opt/
301
+ /opt/android-studio/bin/studio.sh
302
+ ```
303
+ In Android Studio, select `App` and import it as a project, then go to
304
+ **Tools → Device Manager** and add a new device with the **+** symbol (e.g. Pixel 5).
305
+
306
+ Set up the Android SDK environment variables by adding these lines to the end of `~/.bashrc`,
307
+ using your own paths (shown under **Tools → Device Manager → Android SDK Location**):
308
+ ```bash
309
+ export ANDROID_SDK_ROOT="$HOME/Android/Sdk"
310
+ export ANDROID_HOME="$HOME/Android/Sdk"
311
+ export PATH="$PATH:$HOME/Android/Sdk/cmdline-tools/latest/bin:$HOME/.local/bin"
312
+ ```
313
+
314
+ Run all models, a single model, or several models:
315
+ ```bash
316
+ python -m ab.lite.torch2tflite-all
317
+ python -m ab.lite.torch2tflite-all AirNet
318
+ python -m ab.lite.torch2tflite-all AirNet ga-196 ga-197 ga-198
319
+ ```
320
+
321
+ ## Running the tests
322
+
323
+ The unit tests cover output parsing, error extraction, the result schema, the choice of the
324
+ model source and of the phone, the locks between runs and the options kept across restarts.
325
+ They need neither a phone nor PyTorch, TensorFlow or the NN Dataset:
326
+ ```bash
327
+ pip install pytest filelock
328
+ python -m pytest tests
329
+ ```
330
+
331
+ ## Contributing and support
332
+
333
+ Bug reports, questions and feature requests are welcome in the
334
+ [issue tracker](https://github.com/ABrain-One/nn-lite/issues). See [CONTRIBUTING.md](https://github.com/ABrain-One/nn-lite/blob/main/CONTRIBUTING.md)
335
+ for how to propose changes and how the project is maintained.
336
+
337
+ ## Citation
338
+
339
+ If you find this project to be useful for your research, please consider citing our articles:
340
+ ```bibtex
341
+ @article{ABrain.NN-Lite,
342
+ title = {AI on the Edge: An Automated Pipeline for PyTorch-to-Android Deployment and Benchmarking},
343
+ author = {Saif U Din and Muhammad Ahsan Hussain and Mohsin Ikram and Faraz Kayani and Dmitry Ignatov and Radu Timofte},
344
+ doi = {10.20944/preprints202511.1831.v2},
345
+ url = {https://doi.org/10.20944/preprints202511.1831.v2},
346
+ year = 2026,
347
+ month = {July},
348
+ publisher = {Preprints},
349
+ journal = {Preprints}
350
+ }
351
+
352
+ @InProceedings{ABrain.MobileDenoising,
353
+ title = {Real Image Denoising with Knowledge Distillation for High-Performance Mobile {NPUs}},
354
+ author = {Faraz Kayani and Sarmad Kayani and Asad Ahmed and Radu Timofte and Dmitry Ignatov},
355
+ booktitle={Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition Workshops (CVPRW)},
356
+ pages = {3792--3800},
357
+ year={2026}
358
+ }
359
+
360
+ @InProceedings{ABrain.MobileAgeNet,
361
+ title = {{MobileAgeNet}: Lightweight Facial Age Estimation for Mobile Deployment},
362
+ author = {Arun Kumar and Aswathy Baiju and Radu Timofte and Dmitry Ignatov},
363
+ booktitle={Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition Workshops (CVPRW)},
364
+ pages = {3810--3818},
365
+ year={2026}
366
+ }
367
+
368
+ ```
369
+
370
+ ## License
371
+
372
+ NN-Lite is released under the [MIT License](https://github.com/ABrain-One/nn-lite/blob/main/LICENSE).
373
+
374
+ #### The idea and leadership of Dr. Ignatov
@@ -0,0 +1,6 @@
1
+ lmob-1.0.0.dist-info/licenses/LICENSE,sha256=JU9ZHLckSwIWSbuWZtdtDFQ6j4nNqwoaiMTaIo3aPD8,1189
2
+ lmob-1.0.0.dist-info/METADATA,sha256=a4WuGBjQugITkE0kWc1aySfa3ZYU3BYw1RRBsY4u6s8,17587
3
+ lmob-1.0.0.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
4
+ lmob-1.0.0.dist-info/entry_points.txt,sha256=b5j0lbmxZaRGbVykxu9fbKc68bqtXovdtOjQ9lEbFU0,60
5
+ lmob-1.0.0.dist-info/top_level.txt,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
6
+ lmob-1.0.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (84.0.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ nn-lite-bench = ab.lite.torch2tflite:main
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025- ABrain One and contributors; PyTorch to TensorFlow Lite neural network model converter (c) 2025 Andrey Ignatov
4
+ All rights reserved.
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
@@ -0,0 +1 @@
1
+