Skip to content

Commit

Permalink
Moved TFLite build module into its own file. Worked on README.
Browse files Browse the repository at this point in the history
  • Loading branch information
albert-tomanek committed Apr 29, 2021
1 parent 47ec758 commit a5f319e
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 27 deletions.
15 changes: 10 additions & 5 deletions README → README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#
![](screenshot.png)

# Using a neural network in your app
This small app aims to demonstrate how machine learning can be integrated into desktop Linux applications.

### Using a neural network in your app

Neural networks are numerical models which take an array of numbers as an input, and produce an array of numbers as an output.
In machine learning, the technical term for such an array is a 'tensor'. Tensors usually have more than 1 dimensions.
Expand All @@ -10,11 +12,14 @@ A tensor is a technical term for a multi-dimentional array.
Any network you find will have one or more input tensors and one or more output tensors.
Before .., the input tensor of a network needs to be filled with data. When invoked, the data is processed by the neural network and the output tensor is filled with the result. This can then be copied out and used.

# Converting h5 files for use with TFLite
### Using pre-trained models
Models for doing common tasks already exist online and you can download and use them directly in your program. In cases where models work on something else than numerical data, you may have to do some preprocessing yourself.

### Converting h5 files for use with TFLite

Many pre-trained models are available on sites like http://www.modelzoo.co/.
Many pre-trained models are available on sites like [ModelZoo](http://www.modelzoo.co/) or the [TensorFlow Hub](https://tfhub.dev/).

To convert a model saved as a .h5 file to TfLite's format, enter the following commands into Python:
To convert a model saved as a .h5 file to TFLite's format, enter the following commands into Python:

import tensorflow as tf
m = tf.keras.models.load_model('mnist.h5')
Expand Down
8 changes: 8 additions & 0 deletions org.tensorflow.lite.examples.mnist.metainfo.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<application>
<id>org.tensorflow.lite.examples.mnist</id>
<name>TensorFlow Lite demo</name>
<summary>Example application using the TensorFlow Lite machine learning library.</summary>
<url type="homepage">https://github.com/albert-tomanek/tflite-demo</url>
<metadata_license>CC0-1.0</metadata_license>
</application>
36 changes: 14 additions & 22 deletions org.tensorflow.lite.examples.mnist.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,27 @@ add-build-extensions:
build-options:
append-path: /usr/lib/sdk/bazel/bin
modules:
- name: tflite
build-options:
build-args:
- --share=network # Bazel downloads some things when building tflite. Also, build.sh uses `git clone` to download the latest tensorflow.
buildsystem: simple
build-commands:
- pip3 install pip numpy wheel --prefix=/app # Dunno why these are required by bazel to build a C binary 🤷 https://www.tensorflow.org/install/source
- pip3 install keras_preprocessing --no-deps --prefix=/app
- ./build.sh
- rm -r package/DEBIAN # The build script is intended to create a debian package. We don't need package metadata so we remove it.
- mv package/usr/lib package/ # Flatpak apps seem to have libraries in /app/lib rather than /app/user/lib
- cp -RT package/ ${FLATPAK_DEST}/ # Merge directory structure from the debian package into the runtime (and build..? :-/) environment (`package/` contains directories like usr/, lib/, etc.)
sources:
- type: git
url: https://github.com/albert-tomanek/tflite-package
- tflite.yml
- name: demo
buildsystem: simple
build-commands:
- valac main.vala --pkg gtk+-3.0 --pkg tflite --vapidir ${FLATPAK_DEST}/usr/share/vala/vapi -X -I/app/usr/include -X -L/app/lib
- valac main.vala --pkg gtk+-3.0 --pkg tflite --vapidir /app/usr/share/vala/vapi -X -I/app/usr/include -X -L/app/lib

- install -D main /app/bin/main
- install -D mnist.tflite /app/bin/mnist.tflite
- echo "#!/bin/sh " >> /app/bin/run_main.sh
- echo "main /app/bin/mnist.tflite" >> /app/bin/run_main.sh
- chmod +x /app/bin/run_main.sh
- ls -l /app/bin

sources:
- type: file
path: main.vala
- type: file
path: mnist.tflite
# - type: file
# path: meson.build
# We need to create a shell script to start our app because we need to pass it a commandline argument and flatpak-builder doesn't support this.
- name: run_script
sources:
- type: script
commands:
- main /app/bin/mnist.tflite
dest-filename: run_main.sh
buildsystem: simple
build-commands:
- cp run_main.sh /app/bin/
Binary file added screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions tflite.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# When using this module, the following needs to be added to the manifest:
#
# add-build-extensions:
# - org.freedesktop.Sdk.Extension.bazel
# build-options:
# append-path: /usr/lib/sdk/bazel/bin
#

name: tflite
build-options:
build-args:
- --share=network # Bazel downloads some things when building tflite. Also, build.sh uses `git clone` to download the latest tensorflow.
buildsystem: simple
build-commands:
- pip3 install pip numpy wheel --prefix=/app # Dunno why these are required by bazel to build a C binary 🤷 https://www.tensorflow.org/install/source
- pip3 install keras_preprocessing --no-deps --prefix=/app
- ./build.sh
- rm -r package/DEBIAN # The build script is intended to create a debian package. We don't need package metadata so we remove it.
- mv package/usr/lib package/ # Flatpak apps seem to have libraries in /app/lib rather than /app/user/lib
- cp -RT package/ ${FLATPAK_DEST}/ # Merge directory structure from the debian package into the runtime (and build..? :-/) environment (`package/` contains directories like usr/, lib/, etc.)
sources:
- type: git
url: https://github.com/albert-tomanek/tflite-package

0 comments on commit a5f319e

Please sign in to comment.