Tensor/IO
简介
Tensor/IO iOS是针对TensorFlow和TensorFlow Lite的Objective-C包装器,它抽象了将字节复制到张量和从张量中复制字节的工作,并允许您与原生类型进行交互,例如数字、数组、字典和像素缓冲区。Tensor/IO for iOS支持打包和部署、推理和训练。该实现是Tensor/IO项目的一部分,支持iOS、Android和React Native上的机器学习。
有关更完整的文档,请参阅Tensor/IO文档。
示例
使用Tensor/IO,您只需几行代码即可执行推理
UIImage *image = [UIImage imageNamed:@"example-image"];
TIOPixelBuffer *buffer = [[TIOPixelBuffer alloc] initWithPixelBuffer:image.pixelBuffer orientation:kCGImagePropertyOrientationUp];
TIOTFLiteModel *model = [TIOTFLiteModel modelWithBundleAtPath:path];
NSDictionary *inference = (NSDictionary *)[model runOn:buffer];
NSDictionary *classification = [inference[@"classification"] topN:5 threshold:0.1];
在Swift中
let image = UIImage(named: "example-image")!
let pixels = image.pixelBuffer()!
let value = pixels.takeUnretainedValue() as CVPixelBuffer
let buffer = TIOPixelBuffer(pixelBuffer:value, orientation: .up)
let model = TIOTFLiteModel.withBundleAtPath(path)!
let inference = model.run(on: buffer)
let classification = ((inference as! NSDictionary)["classification"] as! NSDictionary).topN(5, threshold: 0.1)
示例项目
要运行示例项目,请克隆仓库,然后首先从Example目录运行pod install
。
- 请参阅MainViewController.mm以获取示例代码。
- 请参阅TensorIOTFLiteModelIntegrationTests.mm以获取更复杂的模型。
- 测试模型可能在repo中的notebooks目录中找到。
我们包括四个示例项目,展示了如何使用Tensor/IO与TF Lite和TensorFlow后端,在Objective-C和Swift中应用。
需求
Tensor/IO需要iOS 12.0及以上版本
将Tensor/IO添加到您的项目
Tensor/IO可以通过CocoaPods获得。在您的Podfile中添加以下内容
pod 'TensorIO/TFLite'
然后运行pod install
。
如果您想使用TensorFlow后端,请添加以下内容代替
pod 'TensorIO/TensorFlow'
请注意,截至v2.0.6版本的TensorIO/TensorFlow依赖项,该依赖项提供的TensorFlow只支持arm64架构。
objective-c
因为Tensor/IO的总头文件导入了具有C++语法的头文件,任何使用Tensor/IO的文件都必须有Obj-C++扩展。将您的.m
文件重命名为.mm
。
然后在您想要使用Tensor/IO的地方,只需导入它即可
@import TensorIO;
Swift
确保在Podfile中取消注释use_frameworks!
,然后在您想要使用Tensor/IO的地方,只需导入它即可
import TensorIO
有关使用Tensor/IO的更多信息,请参阅完整文档或查看此仓库中包含的四个示例项目。
我们还维护一个包含如何使用Tensor/IO构建适用于设备推理和训练的模型的示例jupyter笔记本的仓库,并包括在Swift中运行这些模型的示例iOS代码。有关更多信息,请参阅tensorio/examples。
MIT 协议
Tensor/IO 在 Apache 2 许可证下可用。更多信息请参见 LICENSE。