Top Related Projects
TensorFlow examples
Android NDK samples with Android Studio
An Open Source Machine Learning Framework for Everyone
mlpack: a fast, header-only C++ machine learning library
Quick Overview
The pytorch/android-demo-app repository is a collection of Android demo applications showcasing the use of PyTorch Mobile for on-device machine learning. It provides examples of how to integrate and use PyTorch models in Android applications, demonstrating various use cases such as image classification, object detection, and natural language processing.
Pros
- Offers practical, ready-to-use examples of PyTorch Mobile integration in Android apps
- Covers a wide range of ML tasks, including computer vision and NLP
- Provides a starting point for developers to build their own ML-powered Android applications
- Regularly updated to keep pace with PyTorch Mobile developments
Cons
- Limited to Android platform, not covering iOS or other mobile operating systems
- May require some understanding of PyTorch and machine learning concepts
- Some examples might not be optimized for production use and may need further refinement
- Documentation could be more comprehensive for beginners
Getting Started
To get started with the pytorch/android-demo-app:
-
Clone the repository:
git clone https://github.com/pytorch/android-demo-app.git
-
Open the project in Android Studio.
-
Choose a specific demo app (e.g., HelloWorldApp, ImageSegmentation, etc.) from the project structure.
-
Build and run the selected demo app on an Android device or emulator.
-
Explore the code to understand how PyTorch Mobile is integrated and used in the application.
For more detailed instructions and requirements, refer to the README.md file in each demo app's directory.
Competitor Comparisons
TensorFlow examples
Pros of examples
- More comprehensive, covering a wider range of TensorFlow applications and platforms
- Better maintained with more frequent updates and contributions
- Includes examples for various TensorFlow features like TensorFlow Lite and TensorFlow.js
Cons of examples
- Larger repository size, potentially overwhelming for beginners
- Less focused on Android-specific implementations
- May require more setup and configuration for Android development
Code Comparison
examples (TensorFlow):
import tensorflow as tf
model = tf.keras.Sequential([
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(10, activation='softmax')
])
android-demo-app (PyTorch):
import org.pytorch.Module
val module: Module = Module.load(assetFilePath(this, "model.pt"))
val inputTensor: Tensor = Tensor.fromBlob(inputArray, inputShape)
val outputTensor: Tensor = module.forward(inputTensor).toTensor()
The TensorFlow example shows a simple model definition using Keras, while the PyTorch example demonstrates loading a pre-trained model and performing inference on Android. The PyTorch code is more specific to Android implementation, whereas the TensorFlow code is more general and applicable to various platforms.
Android NDK samples with Android Studio
Pros of ndk-samples
- Broader scope, covering various NDK features and use cases
- Official Google repository, ensuring up-to-date and best practices
- More extensive documentation and sample variety
Cons of ndk-samples
- Less focused on machine learning applications
- May require more setup and configuration for specific use cases
- Potentially overwhelming for beginners due to its breadth
Code Comparison
ndk-samples (hello-jni):
JNIEXPORT jstring JNICALL
Java_com_example_hellojni_HelloJni_stringFromJNI(JNIEnv *env, jobject thiz) {
return (*env)->NewStringUTF(env, "Hello from JNI!");
}
android-demo-app:
private fun initializeNetwork() {
val module = LiteModuleLoader.load(assetFilePath(this, "model.ptl"))
mModule = module
}
Summary
ndk-samples offers a comprehensive set of examples for Android NDK development, covering various topics and use cases. It's an official Google repository, ensuring best practices and up-to-date content. However, it may be less focused on specific applications like machine learning compared to android-demo-app. The code comparison shows the difference in focus, with ndk-samples demonstrating basic JNI usage and android-demo-app showcasing PyTorch integration.
An Open Source Machine Learning Framework for Everyone
Pros of TensorFlow
- Comprehensive ecosystem with tools like TensorBoard for visualization
- Stronger support for production deployment and mobile/edge devices
- More extensive documentation and community resources
Cons of TensorFlow
- Steeper learning curve, especially for beginners
- Less dynamic and flexible compared to PyTorch's define-by-run approach
- Can be more verbose and complex for simple models
Code Comparison
TensorFlow:
import tensorflow as tf
model = tf.keras.Sequential([
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(10, activation='softmax')
])
PyTorch Android Demo App:
import torch.nn as nn
model = nn.Sequential(
nn.Linear(784, 64),
nn.ReLU(),
nn.Linear(64, 10)
)
Summary
TensorFlow offers a more comprehensive ecosystem and better production support, while the PyTorch Android Demo App showcases a simpler, more intuitive approach for mobile development. TensorFlow's code tends to be more verbose but offers more built-in features, whereas PyTorch's syntax is often more concise and Pythonic. The choice between them depends on specific project requirements and developer preferences.
mlpack: a fast, header-only C++ machine learning library
Pros of mlpack
- Comprehensive C++ machine learning library with a wide range of algorithms
- Designed for both efficiency and ease of use, suitable for production environments
- Extensible architecture allowing for easy addition of new methods
Cons of mlpack
- Steeper learning curve compared to PyTorch's Android demo app
- Less focus on mobile-specific implementations
- May require more setup and configuration for Android development
Code Comparison
mlpack (C++):
#include <mlpack/core.hpp>
#include <mlpack/methods/neighbor_search/neighbor_search.hpp>
using namespace mlpack;
arma::mat data;
data::Load("dataset.csv", data, true);
NeighborSearch<NearestNeighborSort> nn(data);
android-demo-app (Java/Kotlin):
import org.pytorch.IValue
import org.pytorch.Module
import org.pytorch.Tensor
val module = Module.load(assetFilePath(this, "model.pt"))
val inputTensor = Tensor.fromBlob(inputArray, inputShape)
val outputTensor = module.forward(IValue.from(inputTensor)).toTensor()
The mlpack example demonstrates loading data and initializing a nearest neighbor search, while the android-demo-app example shows loading a PyTorch model and performing inference on mobile. mlpack focuses on general machine learning algorithms, while android-demo-app is tailored for deploying PyTorch models on Android devices.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
PyTorch Android Examples
A list of Android demo apps built on the powerful PyTorch Mobile platform.
Note
PyTorch Mobile is no longer actively supported. Please check out ExecuTorch, PyTorchâs all-new on-device inference library. You can learn more about ExecuTorchâs Android demo apps here.
HelloWorld
HelloWorld is a simple image classification application that demonstrates how to use the PyTorch Android API with the latest PyTorch 1.8, MobileNet v3, and MemoryFormat.CHANNELS_LAST.
PyTorch demo app
The PyTorch demo app is a full-fledged app that contains two showcases. A camera app that runs a quantized model to classifiy images in real time. And a text-based app that uses a text classification model to predict the topic from the input text.
D2go
D2Go demonstrates a Python script that creates the much lighter and much faster Facebook D2Go model that is powered by PyTorch 1.8, torchvision 0.9, and Detectron2 with built-in SOTA networks for mobile, and an Android app that uses it to detect objects from pictures in your photos, taken with camera, or with live camera. This demo app also shows how to use the native pre-built torchvision-ops library.
Image Segmentation
Image Segmentation demonstrates a Python script that converts the PyTorch DeepLabV3 model and an Android app that uses the model to segment images.
Object Detection
Object Detection demonstrates how to convert the popular YOLOv5 model and use it in an Android app that detects objects from pictures in your photos, taken with camera, or with live camera.
Neural Machine Translation
Neural Machine Translation demonstrates how to convert a sequence-to-sequence neural machine translation model trained with the code in the PyTorch NMT tutorial and use the model in an Android app to do French-English translation.
Question Answering
Question Answering demonstrates how to convert a powerful transformer QA model and use the model in an Android app to answer questions about PyTorch Mobile and more.
Speech recognition
Speech Recognition demonstrates how to convert Facebook AI's wav2vec 2.0, one of the leading models in speech recognition, to TorchScript and how to use the scripted model in an Android app to perform speech recognition.
Vision Transformer
Vision Transformer demonstrates how to use Facebook's latest Vision Transformer DeiT model to do image classification, and how convert another Vision Transformer model and use it in an Android app to perform handwritten digit recognition.
Speech recognition
Speech Recognition demonstrates how to convert Facebook AI's wav2vec 2.0, one of the leading models in speech recognition, to TorchScript and how to use the scripted model in an Android app to perform speech recognition.
Streaming Speech recognition
Streaming Speech Recognition demonstrates how to how to use a new torchaudio pipeline to perform streaming speech recognition, powered by Java Native Call to a C++ audio processing library for the mel spectrogram transform.
Video Classification
TorchVideo demonstrates how to use a pre-trained video classification model, available at the newly released PyTorchVideo, on Android to see video classification results, updated per second while the video plays, on tested videos, videos from the Photos library, or even real-time videos.
LICENSE
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Top Related Projects
TensorFlow examples
Android NDK samples with Android Studio
An Open Source Machine Learning Framework for Everyone
mlpack: a fast, header-only C++ machine learning library
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot