CoreML 是 Apple 今年 WWDC 新推出面向开发者的机器学习框架。数组
Apple 对于 Core ML 的介绍:网络
CoreML 让你将不少机器学习模型集成到你的app中。除了支持层数超过30层的深度学习以外,还支持决策树的融合,SVM(支持向量机),线性模型。因为其底层创建在Metal 和Accelerate等技术上,因此能够最大限度的发挥 CPU 和 GPU 的优点。你能够在移动设备上运行机器学习模型,数据能够不离开设备直接被分析。app
Core ML 让全部的机器学习计算都在iOS设备本地进行,这一点依旧体现出苹果对用户隐私很看重.用苹果的一张图来看看 CoreML 的底层框架框架
图2机器学习
vision:高性能的图像分析和图像识别。这部分应用于人脸追踪,人脸识别,文本识别,区域识别,二维码识别,物体追踪,图像识别等。async
Nattural Language processing:天然语言处理。用于语言识别,分词,词性还原,词性断定等。ide
GamePlayKit:游戏制做,构建游戏。用于常见的游戏行为如随机数生成、人工智能、寻路、和代理行为。工具
Core ML 的底层是 Accelerate and BNNS 和 Metal Performance Shaders,框架集成了神经网络,而且内核优化了图形计算和大规模计算,让App充分使用GPU组件。性能
接下来咱们来体验一下 CoreML,Apple 提供了一些常见的开源模型供你们使用,并且这些模型已经使用了 Core ML 模型格式。您能够自行下载这些模型,而后就能够开始在应用中使用它们了。你也可使用其余第三方机器学习工具来建立和训练模型,将你本身建立的模型使用Core ML Tools 转换成 Core ML 就能够了。学习
这里下载 Apple 提供的 Inception v3 Model,(网址:https://developer.apple.com/machine-learning/).以下图,点击下载.
将下载好的 Model 加入到项目工程中,以下图
从上图能够看到 CoreML Model 分红三部分,第一部分算是基本的描述,第二部分 ModelClass 是对应 Model 生成的 Source 点击 Inception 末尾的小箭头进入Inception.h 文件 能够看到对应 Model的类和方法如图:
一共生成了三个类分别是Inceptionv3,Inceptionv3Input,Inceptionv3Output
具体核心识别代码:
//分析照片
- (void)parsePhotoFromUIImage:(UIImage*)image
{
if (SYSTEM_VERSION < 11.0) {
[SVProgressHUD showErrorWithStatus:@"系统不支持,请更新最新系统"];
return;
}
[SVProgressHUD showWithStatus:@"分析中..."];
if (nil == _inceptionv3) {
_inceptionv3 = [[Inceptionv3 alloc] init];
}
//耗时放子线程
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
//初始化
VNCoreMLModel *vnCoreModel = [VNCoreMLModel modelForMLModel:_inceptionv3.model error:nil];
VNCoreMLRequest *vnCoreMlRequest = [[VNCoreMLRequest alloc] initWithModel:vnCoreModel completionHandler:^(VNRequest * _Nonnull request, NSError * _Nullable error) {
NSString* bestProduction = @""; //最佳预测结果
float bestConfideence = 0; //匹配度
for(VNClassificationObservation *classification in request.results)
{
if(classification.confidence > bestConfideence)
{
bestConfideence = classification.confidence;
bestProduction = classification.identifier;
}
}
dispatch_async(dispatch_get_main_queue(), ^{
[SVProgressHUD dismiss];
_resultLab.text = [NSString stringWithFormat:@"图片是:%@\n识别率:%f", bestProduction,bestConfideence];
CGSize size = [self getLabHeight:_resultLab];
//判断是否到底端
if (size.height > _resultLab.frame.size.height) {
size.height = _resultLab.frame.size.height;
}
CGRect frame = _resultLab.frame;
frame.size.height = size.height;
_resultLab.frame = frame;
});
}];
NSDictionary* dic = NSDictionary.dictionary;
NSError* error = nil;
VNImageRequestHandler *vnImageRequestHandler = [[VNImageRequestHandler alloc] initWithCGImage:image.CGImage options:dic];
[vnImageRequestHandler performRequests:@[vnCoreMlRequest] error:&error];
if (error) {
NSLog(@"error ========= %@",error.localizedDescription);
}
})
}
这里使用Vision库中VNCoreMLModel , VNCoreMLRequest , VNImageRequestHandler
关键开始识别方法
[vnImageRequestHandler performRequests:@[vnCoreMlRequest] error:&error];
识别完成会回调vnCoreMlRequest 的completionHandler,其返回的结果是一个VNClassificationObservation数组,每个VNClassificationObservation都是一个识别的结果,咱们要从里面选出匹配率最高的一个结果出来。具体的Vision库使用能够看看官方文档:https://developer.apple.com/documentation/vision
VNClassificationObservation对象有两个参数
1.confidence 识别率,值越高应该是越接近的
2.identifier 识别结果