iOS 测试官网机器学习模型

1.使用官方最新发布的CoreML
2.测试object-c swift
3.引用apple案例里使用的机器学习模型,下载地址:https://pan.baidu.com/s/1jHYJW6i 下载后更名为MarsHabitatPricer.mlmodel
4.首先使用Xcode 9 创建新项目,

4.1 导入MarsHabitatPricer.mlmodel包,官网说Xcode 9 会自动根据使用语言生成对应类,但导入后如图1 :

2287217-8c2b45924a693df7.png
图 1.png

Model Class 会报:

Model is not part of any target. Add the model to a target to enable generation of the model class.

解决办法:把此模型添加到项目中,
如图 2:

2287217-9b5e69aaebcb18b9.png
图 2.png

此时再点击导入的模型,会看到生成的Model Class,如图 3

2287217-e0f0250a9222a737.png
图 3.png

此时在项目中已经可以使用此模型的类及函数了

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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值