初探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 
#import 
#include 

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

/// 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

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

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

最后附一张识别截图


最后附一张识别截图

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

你可能感兴趣的:(初探Core ML)