初试iOS coreML
来源:互联网 发布:java xml转化成pdf 编辑:程序博客网 时间:2024/06/05 11:44
2017年7月 暑假
之前一直被各种事缠着,今天终于静下心来学习了苹果的Core ML 。
WWDC2017,苹果推出了Core ML, 初入IOS开发,硬生生看完了苹果关于Core ML的介绍视频,地址https://developer.apple.com/videos/play/wwdc2017/703/
被视频中的那个识别图片Demo吸引住,就去苹果官网https://developer.apple.com/machine-learning/下载了第四个机器学习模型VGG16。
在Xcode -beta中新建一个项目MyCoreMLDemo,把下载好的模型直接拖入项目中。
在这里说一下自己对机器学习模型的理解:我们可以把训练好的模型看作一个函数,关注模型的输入参数类型和输出参数类型。
VGG16的输入参数类型:image<RGB,224,224>. ----一张224*224的图片(这个要特别注意,不然会报错:
Input image feature image does not match model description);
输出类型:classLabelProbs Dictionary<String,Double> 和 classLable String
只要简单的三行代码,我们就可以使用VGG16模型:
一、建立模型对象
let model =VGG16.init()
二、输入数据
三、输出预测
研究VGG16的代码后发现,VGG16提供一个接口:
func prediction(image: CVPixelBuffer) throws -> VGG16Output {
let input_ = VGG16Input(image: image)
return tryself.prediction(input: input_)
}
这个函数封装了输入,并返回预测结果。这样更简便,像这样调用:
let outPut = try? model.prediction(image:buffer(from: imageView.image!)!)
但要注意的是,输入参数类型为 CVPixeBuffer(像素数据),而我提供的是照片,因此需要将图片转换成像素数据,google了一下,找到了下面的转换函数:
func buffer(from image: UIImage) -> CVPixelBuffer? {
let attrs = [kCVPixelBufferCGImageCompatibilityKey:kCFBooleanTrue, kCVPixelBufferCGBitmapContextCompatibilityKey:kCFBooleanTrue] asCFDictionary
var pixelBuffer : CVPixelBuffer?
let status =CVPixelBufferCreate(kCFAllocatorDefault,Int(image.size.width),Int(image.size.height),kCVPixelFormatType_32ARGB, attrs, &pixelBuffer)
guard (status == kCVReturnSuccess) else {
return nil
}
CVPixelBufferLockBaseAddress(pixelBuffer!,CVPixelBufferLockFlags(rawValue: 0))
let pixelData = CVPixelBufferGetBaseAddress(pixelBuffer!)
let rgbColorSpace =CGColorSpaceCreateDeviceRGB()
let context = CGContext(data: pixelData, width: Int(image.size.width), height:Int(image.size.height), bitsPerComponent:8, bytesPerRow: CVPixelBufferGetBytesPerRow(pixelBuffer!), space: rgbColorSpace, bitmapInfo:CGImageAlphaInfo.noneSkipFirst.rawValue)
context?.translateBy(x: 0, y: image.size.height)
context?.scaleBy(x: 1.0, y: -1.0)
UIGraphicsPushContext(context!)
image.draw(in: CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height))
UIGraphicsPopContext()
CVPixelBufferUnlockBaseAddress(pixelBuffer!,CVPixelBufferLockFlags(rawValue: 0))
return pixelBuffer
}
- 初试iOS coreML
- iOS-CoreML-初探
- iOS 调戏 CoreML —这是花瓶?
- iOS CoreML 模型转换工具coremltools(一)
- iOS CoreML 模型转换工具coremltools(二)
- iOS CoreML 模型转换工具coremltools(三)
- iOS 图片风格转换(CoreML)
- iOS 图片风格转换(CoreML)
- iOS SQLite3初试用
- iOS 开发者账号初试
- iOS 开发者账号初试
- coreML 初窥
- 初试swift语言ios开发
- CoreML学习——转换caffe模型并应用到 iOS App中
- CoreML尝鲜:将自己训练的 caffe 模型移植到 IOS 上
- 初试
- 初试
- 初试
- 深入理解linux内核——内存寻址
- Windows 10右键点击Word EXCEL PPT PDF等,系统资源管理器立即重启解决方法
- 虚拟机CentOS7 配置固定IP地址
- 微信小程序列表的上拉加载和下拉刷新的实现
- Cardiologist-LevelArrhythmiaDetectionwithConvolutionalNeuralNetworks
- 初试iOS coreML
- Error creating bean with name 'mvcContentNegotiationManager'
- TCP
- POJ 2983 Is the Information Reliable (差分约束 bell/spfa判断负环)
- PAT (Advanced Level) Practise 1029 Median (25)
- poj 3352 Road Construction 【边双连通分量缩点 + 加边定理】
- 【JavaScript的关系操作符】
- 数据库中间件 MyCAT 源码分析 —— SQL ON MongoDB
- PAT (Advanced Level) Practise 1030 Travel Plan (30)