Giter Club home page Giter Club logo

tfliteclassification's Introduction

TensorFlow Lite Image Classification in Python

This code snipset is heavily based on TensorFlow Lite Image Classification
The segmentation model can be downloaded from above link.
For the realtime implementation on Android look into the Android Image Classification Example
Follow the classification.ipynb to get information about how to use the TFLite model in your Python environment.

Details

The mobilenet_v1_1.0_224_quant.tflite file's input takes normalized 224x224x3 shape image. And the output is 1001x1 where the 1001 denotes labels in below order, contains the probabilty of the image belongs to the class.. The specific labels of the 1001 classes are stored in the labels_mobilenet_quant_v1_224.txt file in below order

lineList = ['background', 'tench', 'goldfish', 'great white shark', 'tiger shark', 'hammerhead', 'electric ray', 'stingray', 'cock', 'hen', 'ostrich', 'brambling', 'goldfinch', 'house finch', 'junco', 'indigo bunting', ...]

For model inference, we need to load, resize, typecast the image.
The mobileNet model uses uint8 format so typecast numpy array to uint8.

Then if you follow the correct instruction provided by Google in load_and_run_a_model_in_python, you would get output in below shape

Now we need to process this output to use it for classification

import pandas as pd
import numpy as np

classification_prob = []
classification_label = []
total = 0
for index,prob in enumerate(output_data[0]):
    if prob != 0:
        classification_prob.append(prob)
        total += prob
        classification_label.append(index)
label_names = [line.rstrip('\n') for line in open("labels_mobilenet_quant_v1_224.txt")]
found_labels = np.array(label_names)[classification_label]

df = pd.DataFrame(classification_prob/total, found_labels)
sorted_df = df.sort_values(by=0,ascending=False)
sorted_df


The other models such as

efficientnet-lite0-fp32.tflite, 
efficientnet-lite0-int8.tflite, 
mobilenet_v1_1.0_224.tflite

are from the Android Image Classification Example Take a look at the github and consider changing the TFLite model if you want.
I believe you can modify the rest of the code as you want by yourself.
Thank you!

tfliteclassification's People

Contributors

joonb14 avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.