Skip to content

Latest commit

 

History

History

object_classifier

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

General object classifier


Classifies 3 types of bears: bronw, black and teddy bear.

Bear classifier

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:

  1. 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.
  2. 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.
  3. 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.
  4. Build the mobile app. Connect your Flutter app to the web app from the previous step.
  5. Sign up for Firebase and Stripe. This step is not finished.

There are 3 main components:

  1. model training script - Jupyter (Colab) script to train a classification model.
  2. web app - starter project to deploy a trained classification model to the web.
  3. 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.


Web app

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 classifier pkl file. Update the model_file_url variable with the URL copied above. Update export_file_name to bears_trained_model.pkl. Update classes 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!

Bear classifier

  • Install Flutter. Run flutter doctor in console. Resolve problems with certificates and plugins.

    flutter doctor

  • 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 error Because 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.

    Flutter SDK path

  • 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.

    Replace Render link

    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 and user_login in the main.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:

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.