chuyun923 / tensorflow-hotornot

This is an example of how to use TensorFlow library to classify images.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

TensorFlow - Hot or Not?

This is an example of how to use TensorFlow library on Android to classify images. You can find the code explanation in details on my blog post. If you're interested how the model was created, you can read about it on my other blog post.


Presentation

The example was created for the presentation purpose which you can find here.

Usage

Flow of the app is pretty simple:

  1. Take a photo.
  2. Classify if it's hot or not.
  3. Show the results.

Code structure

The app consists of two main components:

  1. MainActivity which is responsible for taking a photo.
  2. ImageClassifier which classifies the photo.

Classifier

ImageClassifier properties:

  • inputName - the name of the classifier's input (the photo pixels goes in there),
  • outputName - the name of the classifier's output (the results can be found there),
  • imageSize - the size of the photo,
  • labels - the list of the labels (in our case "hot" and "not"),
  • imageBitmapPixels - the array with bitmap pixels (int values before normalization),
  • imageNormalizedPixels - the array with normalized pixels,
  • results - the list with the results,
  • tensorFlowInference - the TensorFlow API object (which is used for inference).

Classification process

For classifying photos the app is using retrained MobileNet model. The model can be found inside the assets folder together with the labels file.

Before classification the photo needs to be prepared to fit the input of the classifier which is 224x224 pixels. Because of that the photo is resized and cropped which is happening inside the ImageUtils.

Prepared photo is passed to the ImageClassifier. The class responsibilities are as follows:

  1. Nomalizing pixels of the photo - preprocessImageToNormalizedFloats() method.
  2. Classifying - classifyImageToOutputs() method.
  3. Getting the results - getResults() method.

For the classification process the instance of the TensorFlowInferenceInterface is used. The classification looks as follows:

  1. Put the data to the classifier:
    tensorFlowInference.feed(inputName, imageNormalizedPixels, 1L, imageSize, imageSize, COLOR_CHANNELS.toLong())
  2. Run the classifier:
    tensorFlowInference.run(arrayOf(outputName), ENABLE_LOG_STATS)
  3. Get the results from the output:
    tensorFlowInference.fetch(outputName, results)

The results are then passed to the MainActivity and shown on the screen.

License

Apache 2.0

About

This is an example of how to use TensorFlow library to classify images.

License:Other


Languages

Language:Kotlin 100.0%