Android app for implementing vision transformer(computationally heavy) in production.

Overview

Vision Transformer on Android

Introduction

We use 2 well known vision models:

  1. Facebook DeiT model, a ViT model pre-trained on ImageNet, for image classification on Android;

  2. ViT model on MNIST and convert it to TorchScript to use on Android for handwritten digit recognition.

Prerequisites

  • PyTorch 1.7 or later (Optional)
  • Python 3.8 (Optional)
  • Android Pytorch library 1.7 or later
  • Android Studio 4.0.1 or later

Quick Start on Using Facebook DeiT

1. Prepare the Model (Optional)

To use a pre-trained Facebook DeiT model and convert it to TorchScript, first install PyTorch 1.7 or later, then install timm using pip install timm==0.3.2, and finally run the following script:

python convert_deit.py

This will generate the quantized scripted model named fbdeit.pt, which can also be downloaded here. Note that the quantization code in the script reduces the model size from 346MB to 89MB.

To train and convert your own DeiT model on ImageNet, first follow the instructions under Data Preparation and Training at the DeiT repo, then simply run the following code after model is trained:

from torch.utils.mobile_optimizer import optimize_for_mobile
ts_model = torch.jit.script(model)
optimized_torchscript_model = optimize_for_mobile(ts_model)
optimized_torchscript_model.save("fbdeit.pt")

2. Run the Model on Android

Changes in MainActivity.java file from:

module = Module.load(assetFilePath(this, "model.pt"));
```py
to
```py
module = Module.load(assetFilePath(this, "fbdeit.pt"));

Run the app in Android Studio and you'll see the same image classification result.

Quick Start on Using ViT for MNIST

To Test Run the Android ViT4MNIST demo app, follow the steps below:

1. Prepare the Model (Optional)

On a Terminal, with PyTorch 1.7.0 and einops installed, run :

python mnist_vit.py

The model definition in vit_pytorch.py and training code in mnist_vit.py are mostly taken from the blog here.

2. Build and run with Android Studio

Run on your AVD or real Android device.

Screenshot 1 Screenshot 2 Screenshot 3

You might also like...
An android image compression library.
An android image compression library.

Compressor Compressor is a lightweight and powerful android image compression library. Compressor will allow you to compress large photos into smaller

Custom shaped android imageview components
Custom shaped android imageview components

Shape Image View Provides a set of custom shaped android imageview components, and a framework to define more shapes. Implements both shader and bitma

Android widget for cropping and rotating an image.
Android widget for cropping and rotating an image.

Cropper The Cropper is an image cropping tool. It provides a way to set an image in XML and programmatically, and displays a resizable crop window on

A simple image cropping library for Android.
A simple image cropping library for Android.

SimpleCropView The SimpleCropView is an image cropping library for Android. It simplifies your code for cropping image and provides an easily customiz

Customizable Android full screen image viewer for Fresco library supporting
Customizable Android full screen image viewer for Fresco library supporting "pinch to zoom" and "swipe to dismiss" gestures. Made by Stfalcon

This project is no longer supported. If you're able to switch from Fresco to any other library that works with the Android's ImageView, please migrate

Dali is an image blur library for Android. It contains several modules for static blurring, live blurring and animations.
Dali is an image blur library for Android. It contains several modules for static blurring, live blurring and animations.

Dali Dali is an image blur library for Android. It is easy to use, fast and extensible. Dali contains several modules for either static blurring, live

An image resizing library for Android
An image resizing library for Android

Resizer Inspired by zetbaitsu's Compressor, Resizer is a lightweight and easy-to-use Android library for image scaling. It allows you to resize an ima

Photo picker library for android. Let's you pick photos directly from files, or navigate to camera or gallery.
Photo picker library for android. Let's you pick photos directly from files, or navigate to camera or gallery.

ChiliPhotoPicker Made with ❤️ by Chili Labs. Library made without DataBinding, RxJava and image loading libraries, to give you opportunity to use it w

Simple android image popup Library
Simple android image popup Library

Android Image Popup Show image as a popup on a click event or any event. Simply set the image as drawable and thats it!!!. And also you can set width,

Owner
Mann Patel
Deep Learning Developer/Researcher | SOTA AI | MLOps | CSE '22
Mann Patel
[Android] Screenshot detection while user using your app

[Android] Screenshot detection while user using your app

Akexorcist 137 Dec 30, 2022
Image classifier app build in Android Studio.

Android Image Classifier App Strongly based on https://github.com/microsoft/onnxruntime-inference-examples/tree/main/mobile/examples/image_classificat

Luiz Santos 3 Feb 17, 2022
A small customizable library useful to handle an gallery image pick action built-in your app. :sunrise_over_mountains::stars:

Louvre A small customizable image picker. Useful to handle an gallery image pick action built-in your app. *Images from Google Image Search Installati

André Mion 640 Nov 19, 2022
Note saver app which can save images as well with customization.

Note-With-Images-App Note saver app which can save images as well with customization. Demo final.demo.mp4 What i used? Kotlin Paging 3 library flow li

agam koradiya 9 Oct 31, 2022
🐶 Simple app which shows random dog image from Dog API

?? Doggy App ?? ?? Simple app which shows random dog image from Dog API with Retrofit2 and Glide ?? ❤️ Retrofit @GET Request YouTube Tutorial! (Stevdz

Yağmur Erdoğan 10 Nov 1, 2022
Opencv Android SDK application for Deep Neural Networks to run on Android.

This application allows you to deploy deep nets in Android environment using OpenCV Android SDK. With OpenCV, images are taken from the camera on your phone, and then these images are passed through the neural network and visualized on the front side. In this application, we will search for faces in the images taken and draw the found faces on the screen.

Ahmet Furkan DEMIR 10 Nov 1, 2022
Android filters based on OpenGL (idea from GPUImage for iOS)

GPUImage for Android Idea from: iOS GPUImage framework Goal is to have something as similar to GPUImage as possible. Vertex and fragment shaders are e

CATS Open Source Softwares 8.6k Dec 28, 2022
Android library project for cropping images

I guess people are just cropping out all the sadness An Android library project that provides a simple image cropping Activity, based on code from AOS

Jamie McDonald 4.5k Jan 7, 2023
Custom view for circular images in Android while maintaining the best draw performance

CircularImageView Custom view for circular images in Android while maintaining the best draw performance Usage To make a circular ImageView, add this

Pkmmte Xeleon 1.2k Dec 28, 2022
some android image filters

android-image-filter some android image filters in some filter, I use NDK to implement to make it more efficient Setup Install Android NDK and properl

RagnarokStack 643 Dec 27, 2022