1.使用官方最新發(fā)布的CoreML
2.測試object-c swift
3.引用apple案例里使用的機(jī)器學(xué)習(xí)模型,下載地址:https://pan.baidu.com/s/1jHYJW6i 下載后更名為MarsHabitatPricer.mlmodel
4.首先使用Xcode 9 創(chuàng)建新項(xiàng)目腿短,
4.1 導(dǎo)入MarsHabitatPricer.mlmodel包褐鸥,官網(wǎng)說Xcode 9 會自動根據(jù)使用語言生成對應(yīng)類漓穿,但導(dǎo)入后如圖1 :
Model Class 會報:
Model is not part of any target. Add the model to a target to enable generation of the model class.
解決辦法:把此模型添加到項(xiàng)目中泻轰,
如圖 2:
此時再點(diǎn)擊導(dǎo)入的模型莉御,會看到生成的Model Class疙渣,如圖 3
此時在項(xiàng)目中已經(jīng)可以使用此模型的類及函數(shù)了
4.2 代碼
#import "ViewController.h"
#import <CoreML/CoreML.h>
#import "MarsHabitatPricer.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setupCoreML];
}
/** model input */
- (void)setupCoreML
{
MarsHabitatPricer *marsPrice = [[MarsHabitatPricer alloc] init];
double panels = 1.5;
double green = 3.0;
double size = 1500;
MarsHabitatPricerOutput *output = [marsPrice predictionFromSolarPanels:panels greenhouses:green size:size error:nil];
double price = output.price;
NSLog(@"price = %f", price);
}
@end
4.3 swift
let model = MarsHabitatPricer()
let solar = 1.5
let green = 3.0
let acres = 1500.0
let output = try? model.prediction(solarPanels: solar, greenhouses: Double(green), size: acres)
let price = output?.price;
print(price ?? 0)