{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Build_TensorFlow_Lite_Support_Libraries_with_Bazel_and_Colab.ipynb",
"provenance": [],
"collapsed_sections": []
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "bFF0wwGKvyxw"
},
"source": [
"Licensed under the Apache License, Version 2.0 (the \"License\");\n",
"you may not use this file except in compliance with the License.\n",
"You may obtain a copy of the License at\n",
" http://www.apache.org/licenses/LICENSE-2.0\n",
"Unless required by applicable law or agreed to in writing, software\n",
"distributed under the License is distributed on an \"AS IS\" BASIS,\n",
"WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
"See the License for the specific language governing permissions and\n",
"limitations under the License."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "_u2bOkHzv4F4"
},
"source": [
"# Build TensorFlow Lite Support libraries with Bazel "
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "sh4xCk3Ygq6p"
},
"source": [
"## Set up Android environment"
]
},
{
"cell_type": "code",
"metadata": {
"id": "S-NOtaam7Ebn"
},
"source": [
"# Create folders\n",
"!mkdir -p '/android/sdk'\n",
"\n",
"# Download and move android SDK tools to specific folders\n",
"!wget -q 'https://dl.google.com/android/repository/tools_r25.2.5-linux.zip'\n",
"\n",
"!unzip 'tools_r25.2.5-linux.zip'\n",
"!mv '/content/tools' '/android/sdk'\n",
"# Copy paste the folder\n",
"!cp -r /android/sdk/tools /android/android-sdk-linux\n",
"\n",
"# Download NDK, unzip and move contents\n",
"!wget 'https://dl.google.com/android/repository/android-ndk-r19c-linux-x86_64.zip'\n",
"\n",
"!unzip 'android-ndk-r19c-linux-x86_64.zip'\n",
"!mv /content/android-ndk-r19c /content/ndk\n",
"!mv '/content/ndk' '/android'\n",
"# Copy paste the folder\n",
"!cp -r /android/ndk /android/android-ndk-r19c\n",
"\n",
"# Remove .zip files\n",
"!rm 'tools_r25.2.5-linux.zip'\n",
"!rm 'android-ndk-r19c-linux-x86_64.zip'\n",
"\n",
"# Make android ndk executable to all users\n",
"!chmod -R go=u '/android'"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "dTZrWYNwhd7G"
},
"source": [
"# Set and view environment variables\n",
"%env PATH = /usr/local/nvidia/bin:/usr/local/cuda/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/tools/node/bin:/tools/google-cloud-sdk/bin:/opt/bin:/android/sdk/tools:/android/sdk/platform-tools:/android/ndk\n",
"%env ANDROID_SDK_API_LEVEL=29\n",
"%env ANDROID_API_LEVEL=29\n",
"%env ANDROID_BUILD_TOOLS_VERSION=30.0.0\n",
"%env ANDROID_DEV_HOME=/android\n",
"%env ANDROID_NDK_API_LEVEL=21\n",
"%env ANDROID_NDK_FILENAME=android-ndk-r19c-linux-x86_64.zip\n",
"%env ANDROID_NDK_HOME=/android/ndk\n",
"%env ANDROID_NDK_URL=https://dl.google.com/android/repository/android-ndk-r19c-linux-x86_64.zip\n",
"%env ANDROID_SDK_FILENAME=tools_r25.2.5-linux.zip\n",
"%env ANDROID_SDK_HOME=/android/sdk\n",
"#%env ANDROID_HOME=/android/sdk\n",
"%env ANDROID_SDK_URL=https://dl.google.com/android/repository/tools_r25.2.5-linux.zip\n",
"\n",
"#!echo $PATH\n",
"!export -p"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "e26VrdXdL2U6"
},
"source": [
"# Install specific versions of sdk, tools etc.\n",
"!android update sdk --no-ui -a \\\n",
" --filter tools,platform-tools,android-29,build-tools-29.0.2"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "3eAfl3MmySzO"
},
"source": [
"## Install BAZEL with Baselisk"
]
},
{
"cell_type": "code",
"metadata": {
"id": "Dj_P2Fb-yTqq"
},
"source": [
"# Download Latest version of Bazelisk\n",
"!wget https://github.com/bazelbuild/bazelisk/releases/latest/download/bazelisk-linux-amd64\n",
"\n",
"# Make script executable\n",
"!chmod +x bazelisk-linux-amd64\n",
"\n",
"# Adding to the path\n",
"!sudo mv bazelisk-linux-amd64 /usr/local/bin/bazel\n",
"\n",
"# Extract bazel info\n",
"!bazel"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "AMPL9u6tzOMJ"
},
"source": [
"# Clone TensorFlow Lite Support repository OR upload your custom folder to build\n",
"!git clone https://github.com/tensorflow/tflite-support.git"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "9KXH-1So0VOd"
},
"source": [
"# Move into tflite-support folder\n",
"%cd /content/tflite-support/\n",
"\n",
"!ls"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "2D2tsSFs7Hrq"
},
"source": [
"## Build .aar files"
]
},
{
"cell_type": "code",
"metadata": {
"id": "8XM37u827jD_"
},
"source": [
"#@title Select library. { display-mode: \"form\" }\n",
"\n",
"library = 'Support library' #@param [\"Support library\", \"Task Vision library\", \"Task Text library\", \"Task Audio library\",\"Metadata library\",\"C++ image_classifier\",\"C++ image_objector\",\"C++ image_segmenter\",\"C++ image_embedder\",\"C++ nl_classifier\",\"C++ bert_nl_classifier\", \"C++ bert_question_answerer\", \"C++ metadata_extractor\"]\n",
"\n",
"print('You selected:', library)\n",
"\n",
"if library == 'Support library':\n",
" library = '//tensorflow_lite_support/java:tensorflowlite_support.aar'\n",
"elif library == 'Task Vision library':\n",
" library = '//tensorflow_lite_support/java/src/java/org/tensorflow/lite/task/vision:task-library-vision'\n",
"elif library == 'Task Text library':\n",
" library = '//tensorflow_lite_support/java/src/java/org/tensorflow/lite/task/text:task-library-text'\n",
"elif library == 'Task Audio library':\n",
" library = '//tensorflow_lite_support/java/src/java/org/tensorflow/lite/task/audio:task-library-audio'\n",
"elif library == 'Metadata library':\n",
" library = '//tensorflow_lite_support/metadata/java:tensorflow-lite-support-metadata-lib'\n",
"elif library == 'C++ image_classifier':\n",
" library = '//tensorflow_lite_support/cc/task/vision:image_classifier'\n",
"elif library == 'C++ image_objector':\n",
" library = '//tensorflow_lite_support/cc/task/vision:image_objector'\n",
"elif library == 'C++ image_segmenter':\n",
" library = '//tensorflow_lite_support/cc/task/vision:image_segmenter'\n",
"elif library == 'C++ image_embedder':\n",
" library = '//tensorflow_lite_support/cc/task/vision:image_embedder'\n",
"elif library == 'C++ nl_classifier':\n",
" library = '//tensorflow_lite_support/cc/task/text/nlclassifier:nl_classifier'\n",
"elif library == 'C++ bert_nl_classifier':\n",
" library = '//tensorflow_lite_support/cc/task/text/nlclassifier:bert_nl_classifier'\n",
"elif library == 'C++ bert_question_answerer':\n",
" library = '//tensorflow_lite_support/cc/task/text/qa:bert_question_answerer'\n",
"elif library == 'C++ metadata_extractor':\n",
" library = '//tensorflow_lite_support/metadata/cc:metadata_extractor'\n",
"\n"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "WwanGSg-FE-0"
},
"source": [
"#@title Select platform(s). { display-mode: \"form\" }\n",
"\n",
"platforms = 'arm64-v8a,armeabi-v7a' #@param [\"arm64-v8a,armeabi-v7a\",\"x86\", \"x86_64\", \"arm64-v8a\", \"armeabi-v7a\",\"x86,x86_64,arm64-v8a,armeabi-v7a\"]\n",
"print('You selected:', platforms)\n"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "FBw-vFrw0hdc"
},
"source": [
"# Build library\n",
"!bazel build \\\n",
" --fat_apk_cpu='{platforms}' \\\n",
" '{library}'"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "vRCanN-W5Av7"
},
"source": [
""
],
"execution_count": null,
"outputs": []
}
]
}