初探Core ML

文 || 張贺

Core ML是iOS11新推出的机器学习使用框架。在此框架之上还有两个集成度较高的框架:Vision 和 NLP(分别是图像处理和文字处理领域的机器学习框架)。

图片来自苹果官方文档

CoreML也可以看做一个模型的转换器,可以将一个MLModel格式的模型文件自动生成一些类和方法,可以直接使用这些类去做分析,让你更简单是在app使用训练好的模型。苹果提供了一些已经训练好的模型供使用,选择一个合适自己需求的下载 。 戳我去下载
下载模型文件
下载之后将文件拖入工程,拖入工程后会根据工程类型(swift工程还是OC工程)生成对应的一些类和方法。
点击箭头会进入MobileNet的头文件

第一行Mchaine Learning Model 是对模型的一些介绍。
第二行Model Class是自动生成的类和方法,点击箭头可以查看头文件。我使用的模型是MobileNet生成的类有3个MobileNetInput MobileNetOutput MobileNet

//
// MobileNet.h
//
// This file was automatically generated and should not be edited.
//

#import <Foundation/Foundation.h>
#import <CoreML/CoreML.h>
#include <stdint.h>

NS_ASSUME_NONNULL_BEGIN


/// Model Prediction Input Type
/// 输入模型
API_AVAILABLE(macos(10.13), ios(11.0), watchos(4.0), tvos(11.0))
@interface MobileNetInput : NSObject<MLFeatureProvider>

/// Input image to be classified as color (kCVPixelFormatType_32BGRA) image buffer, 224 pixels wide by 224 pixels high
/// 输入的是一个CVPixelBufferRef类型的图片 尺寸是224 * 224 尺寸不对会报错
@property (readwrite, nonatomic) CVPixelBufferRef image;
- (instancetype)init NS_UNAVAILABLE;
- (instancetype)initWithImage:(CVPixelBufferRef)image;
@end


/// Model Prediction Output Type
/// 输出模型
API_AVAILABLE(macos(10.13), ios(11.0), watchos(4.0), tvos(11.0))
@interface MobileNetOutput : NSObject<MLFeatureProvider>

/// Probability of each category as dictionary of strings to doubles
/// 所有可能的结果以及每种结果的可能百分比
@property (readwrite, nonatomic) NSDictionary<NSString *, NSNumber *> * classLabelProbs;

/// Most likely image category as string value
/// 最有可能的结果 也就是上面百分比最大的那个
@property (readwrite, nonatomic) NSString * classLabel;
- (instancetype)init NS_UNAVAILABLE;
- (instancetype)initWithClassLabelProbs:(NSDictionary<NSString *, NSNumber *> *)classLabelProbs classLabel:(NSString *)classLabel;
@end


/// Class for model loading and prediction
/// 通过这个类使输入模型 ---> 输出模型
API_AVAILABLE(macos(10.13), ios(11.0), watchos(4.0), tvos(11.0))
@interface MobileNet : NSObject
@property (readonly, nonatomic, nullable) MLModel * model;
- (nullable instancetype)initWithContentsOfURL:(NSURL *)url error:(NSError * _Nullable * _Nullable)error;

/**
    Make a prediction using the standard interface
    @param input an instance of MobileNetInput to predict from
    @param error If an error occurs, upon return contains an NSError object that describes the problem. If you are not interested in possible errors, pass in NULL.
    @return the prediction as MobileNetOutput
*/
- (nullable MobileNetOutput *)predictionFromFeatures:(MobileNetInput *)input error:(NSError * _Nullable * _Nullable)error;

/**
    Make a prediction using the convenience interface
    @param image Input image to be classified as color (kCVPixelFormatType_32BGRA) image buffer, 224 pixels wide by 224 pixels high:
    @param error If an error occurs, upon return contains an NSError object that describes the problem. If you are not interested in possible errors, pass in NULL.
    @return the prediction as MobileNetOutput
*/
- (nullable MobileNetOutput *)predictionFromImage:(CVPixelBufferRef)image error:(NSError * _Nullable * _Nullable)error;
@end

NS_ASSUME_NONNULL_END

MobileNet.h的所有代码都在上面,可以看见使用起来非常简单。

你可能需要用到的方法:
UIImage转换成CVPixelBufferRef

/// 下面这个属性是UIImage的,你只要UIImage的对象 `image.CGImage`传进来就行了
/// @property(nullable, nonatomic,readonly) CGImageRef CGImage; 

- (CVPixelBufferRef)GetpixelBufferWithCGImage:(CGImageRef)cgimage
{
    NSDictionary *options = @{
                              (NSString*)kCVPixelBufferCGImageCompatibilityKey : @YES,
                              (NSString*)kCVPixelBufferCGBitmapContextCompatibilityKey : @YES,
                              (NSString*)kCVPixelBufferIOSurfacePropertiesKey: [NSDictionary dictionary]
                              };
    CVPixelBufferRef pxbuffer = NULL;
    
    CGFloat frameWidth = CGImageGetWidth(cgimage);
    CGFloat frameHeight = CGImageGetHeight(cgimage);
    
    CVReturn status = CVPixelBufferCreate(kCFAllocatorDefault,
                                          frameWidth,
                                          frameHeight,
                                          kCVPixelFormatType_32BGRA,
                                          (__bridge CFDictionaryRef) options,
                                          &pxbuffer);
    
    NSParameterAssert(status == kCVReturnSuccess && pxbuffer != NULL);
    
    CVPixelBufferLockBaseAddress(pxbuffer, 0);
    void *pxdata = CVPixelBufferGetBaseAddress(pxbuffer);
    NSParameterAssert(pxdata != NULL);
    
    CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
    
    CGContextRef context = CGBitmapContextCreate(pxdata,
                                                 frameWidth,
                                                 frameHeight,
                                                 8,
                                                 CVPixelBufferGetBytesPerRow(pxbuffer),
                                                 rgbColorSpace,
                                                 (CGBitmapInfo)kCGImageAlphaNoneSkipFirst);
    NSParameterAssert(context);
    CGContextConcatCTM(context, CGAffineTransformIdentity);
    CGContextDrawImage(context, CGRectMake(0,
                                           0,
                                           frameWidth,
                                           frameHeight),
                       cgimage);
    CGColorSpaceRelease(rgbColorSpace);
    CGContextRelease(context);
    
    CVPixelBufferUnlockBaseAddress(pxbuffer, 0);
    
    return pxbuffer;
}

你可能用到的方法,裁剪图片。将图片裁剪成224 * 224 的。

#pragma mark -裁剪图片
//需要传过来的参数有 : 图片 image 和 自定的尺寸
- (UIImage *)image:(UIImage*)image byScalingToSize:(CGSize)targetSize {
    //原始 iamge
    UIImage *sourceImage = image;
    //新的image 用来接收裁剪后的image 开始时为 nil 
    UIImage *newImage = nil;
    UIGraphicsBeginImageContext(targetSize);
    CGRect thumbnailRect = CGRectZero;
    //裁剪后的image 的原点和 裁剪前 的 image 的 原点相同
    thumbnailRect.origin = CGPointZero;
    //裁剪后的image 的宽和 指定的宽 相同
    thumbnailRect.size.width  = targetSize.width;
    //裁剪后的image 的长和 指定的长 相同
    thumbnailRect.size.height = targetSize.height;
    //将原始image 在设定的 位置上绘制(裁剪)
    [sourceImage drawInRect:thumbnailRect];
    //把裁剪好的 image 放在 新的image 上
    newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage ;
}

调用系统相机或从相册中选择照片,选择完成之后

#pragma mark - 图片识别
/// 我里面的方法就是上面的2个方法做了一点封装
- (void)RecognizeWithImage:(UIImage *)pImage
{
    MobileNet *mobileNet = [[MobileNet alloc]init];
    //只需要调用predictionFromImage:error:这一个方法就可以得到结果了
    MobileNetOutput *output = [mobileNet predictionFromImage:[[pImage ScalingToSize:CGSizeMake(224, 224)] GetpixelBuffer] error:nil];
    _photoName.text = output.classLabel;
    //所有可能的结果 以及每种结果的可能性百分比
    NSLog(@"classLabelProbs----------------%@",output.classLabelProbs);
    //可能性最大的那个结果
    NSLog(@"classLabel----------------%@",output.classLabel);
}

最后附一张识别截图


最后附一张识别截图

此文章待更....一些细节和理论部分有待深入学习和理解。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,826评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,968评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,234评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,562评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,611评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,482评论 1 302
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,271评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,166评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,608评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,814评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,926评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,644评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,249评论 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,866评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,991评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,063评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,871评论 2 354

推荐阅读更多精彩内容