tucan9389 / TFLiteSwift-Vision

Apache License 2.0
38 stars 1 forks source link

TFLiteSwift-Vision

PRs Welcome Version License Platform

Table

Goal

This library is a TensorFlowLiteSwift wrapping library for vision pre/post-processing. You can use TFLiteSwift-Vision, if you want to implemented preprocessing and postprocessing functions in the repository.

image

Here is more detail of this repo's background and goal, and what is diff.

Requirements

Usage

Install the TFLiteSwift-Vision

TFLiteSwift-Vision is available through CocoaPods. To install it, simply add the following line to your Podfile:

target 'MyXcodeProject' do
  use_frameworks!

  # Pods for Your Project
  pod 'TFLiteSwift-Vision', '~> 0.2.6'

end

post_install do |installer|
  installer.pods_project.build_configurations.each do |config|
    config.build_settings["EXCLUDED_ARCHS[sdk=iphonesimulator*]"] = "arm64"
  end
end

And then, run following:

pod install

Add .tflite file in the Xcode Project

importing-tflite-into-xcode

If you have other label file or other meta data file, also import it.

Implement Inference Code

Import TFLiteSwift_Vision framework.

import TFLiteSwift_Vision

Setup interpreter.

let options = TFLiteVisionInterpreter.Options(
  modelName: "mobilenet_v2_1.0_224",
  inputRankType: .bwhc, // if it is pytorch model, use `.bchw`
  normalization: .scaled(from: 0.0, to: 1.0)
)
var visionInterpreter = try? TFLiteVisionInterpreter(options: options)

Inference with an image. The following is an image classification case.

// inference
guard let output: TFLiteFlatArray = try? self.visionInterpreter?.inference(with: uiImage)?.first
    else { fatalError("Cannot inference") }

// postprocess
let predictedIndex: Int = Int(output.argmax())
print("predicted index: \(predictedLabel)")
print(output.dimensions)

Getting Started with Example

Clone and open the Example project

git clone https://github.com/tucan9389/TFLiteSwift-Vision
cd TFLiteSwift-Vision/Example
pod install
open TFLiteSwift-Vision.xcworkspace

Download model and label file

Download tflite model and label txt, and then import the files into Xcode project.

You can also download the following files on here

Build and Run

After build and run the project, you can test the model(mobilenet_v2_1.0_224.tflite) with your own image data.

image classification
demo-tfliteswift-vision-example-001

Architecture

tfliteswift-vision-architecture

Done and TODO

TFLiteSwift-Vision is supporting (or wants to support) follow functions:

Author

License

TFLiteSwift-Vision is available under the Apache license. See the LICENSE file for more info.