- Introduction
- Step 1. Create dataset
- Step 2. Transfer learning
- Step 3. Deploy web app
- Step 4. Deploy mobile app
- Step 5. Make app commercial (not finished)
Classifies 3 types of bears: bronw, black and teddy bear.
Watch original video of Siraj Raval first. And review the original code of an image classification startup.
This is a back-end solution for image classification on Android. For the front-end solution, please, visit my project for object detection on Android.
General object classifier is based on:
- Python and fast.ai for model training through deep learning;
- Flutter mobile development framework with a single code base for Android and iOS applications;
- Firebase for Google Analytics and user authentication;
- Stripe as the payment processor.
and consists of five steps:
- Find an image dataset via Google Dataset Search or Awesome Public Datasets list. Also you can download images via google_images_download Python script or Fatkun Batch Download Image add-on for Chrome browser.
- Transfer learning. Take some trained model and retrain a part of it on your relatively small image dataset. To perform transfer learning install fast.ai vision library (version 1.0.57 or later). Fast.ai is built on top of PyTorch. During transfer training most weights of neural network are frozen in place except for the last layer.
- Deploy the web app. Fast.ai made this impressively simple starter application for deploying fast.ai models on Render, for instantly creating a web app and mobile app for your classifier.
- Build the mobile app. Connect your Flutter app to the web app from the previous step.
- Sign up for Firebase and Stripe. This step is not finished.
There are 3 main components:
- model training script - Jupyter (Colab) script to train a classification model.
- web app - starter project to deploy a trained classification model to the web.
- mobile app - mobile application which connect your web app with mobile phone (tested for Android).
Additionaly some more TensorFlow examples and links here and here.
Create your own directories with images, download data using google_images_download or Fatkun Batch Download Image add-on. Find some datasets to train via Google Dataset Search and Awesome Public Datasets. Or continue with existing bears classification.
Install fastai if you need to train on your local computer:
# Install PyTorch and Torchvision first, because fast.ai is built on top of PyTorch.
# Installation video - https://deeplizard.com/learn/video/UWlFM0R_x6I
# Be patient, it is 750.2 MB for now :-)
# For example: Stable 1.2 -> Windows -> Conda or pip -> Python 3.7 -> CUDA 10.0
pip3 install torch===1.2.0 torchvision===0.4.0 -f https://download.pytorch.org/whl/torch_stable.html
conda install pytorch torchvision cudatoolkit=10.0 -c pytorch
# Check it
pip list | grep torch
conda list torch
# PyPI Install
pip install fastai
# Conda Install
conda install fastai pytorch -c fastai -c pytorch -c conda-forge
conda install -c fastai fastai
# Check it
python -m fastai.utils.show_install
# Activate Anaconda environment and check it
conda activate
python -m fastai.utils.show_install
conda deactivate
This is the original bear_classifier.ipynb
file of the Bear Classifier project.
Run in Jupyter Notebook file called
bear_classifier.ipynb
if you have computer with modern GPU or upload and run it in
Google Colab
if you do not have modern GPU.
Replace the bear dataset with your own image dataset.
Retrain a resnet34
image classification model.
Such retraining is called transfer learning.
Save the resulting model pkl
file to Google Drive and save the download link.
This is the original project Starter for deploying fast.ai models on Render.
Render is a modern cloud provider that makes it effortless and instant to deploy your code in production. You can deploy anything on Render, from simple static sites and cron jobs to databases and Docker-ized private services.
Render deploys your services directly from GitHub or GitLab. All that's needed is to push your code like you normally do. Render automatically updates your services and keeps them up and running at all times.
- Fork the web app repository.
- Follow the instructions in its readme.md to deploy it to Render.
- Once deployed, check that it works.
- Then replace line 12 in
server.py
of the web example with a link to your own classifierpkl
file. Update themodel_file_url
variable with the URL copied above. Updateexport_file_name
tobears_trained_model.pkl
. Updateclasses
if you have other classes. Re-deploy. - Make any cosmetic changes to the front-end interface that you'd like.
- Review (after 6 min) and test the web-site!
- Install Flutter.
Run
flutter doctor
in console. Resolve problems with certificates and plugins.
- Download and open this code in Android Studio as a new Flutter project.
- It will ask you to 'get' all dependencies, say yes and it'll will all be installed automatically.
If you get errorBecause flutter_app11 depends on flutter_test any from sdk which doesn't exist (the Flutter SDK is not available), version solving failed
. Then you need to configure path to Flutter in project settings:File --> Settings --> Languages & Frameworks --> Flutter --> Flutter SDK path
. Enter your path to Flutter directory.
- Run
flutter packages get
in console in project directory. It'll update.packages
file. - Replace the default render link in
main.dart
to the link to your deployed render app.
Dart is a client-optimized programming language for fast apps on multiple platforms. It is developed by Google and is used to build mobile, desktop, backend and web applications. - Notice the 2 functions
user_signup
anduser_login
in themain.dart
file. This is where your stripe and firebase authentication code will be placed to make your app commercial.
Signup for Firebase and Stripe. Confirm your e-mail during signup.
Connect the user login flow to Firebase:
- create a Firebase project to connect to your app;
- add Firebase to your Android app with the package name
com.example.flutter_app11
; - download
google-services.json
file in to android/app folder; - modify android/build.gradle and android/app/build.gradle files if necessary according to instructions;
- change
gradle-5.6.2-all.zip
to actual version indistributionUrl
of gradle-wrapper.properties file.
Configure a Flutter to integrate the Stripe plugin for iOS and Android if you like, but I did not.
Prepare your Android application for release to the Google Play Store if you like, but I did not.
I excluded (commented out) user authentication code in the main.dart.