The goal of coreml is to convert models trained/fitted in R to coreml so that you can use them in Apple iOS applications.
This is work in progress and just a proof of concept. Do not use it for production, it barely works and I am still learning and testing.
remotes::install_github("dirkschumacher/coreml")The API can and will change.
coreml_convertconvert a R model to coremlcoreml_predictmake predictions with a coreml model (just a proof of concept)coreml_savesave a coreml model to disk as binary datacoreml_loadload a coreml model from diskconvert_kerasconvert a keras model to coreml using coremltools. This does not work at the moment.
Here we convert a logistic regression model (GLM with family=binomial)
to a coreml model. Save it to disk, read it in again and make
predictions.
library(coreml)
model <- glm(I(mpg >= 20) ~ -1 + hp + cyl + drat, data = mtcars, family = binomial())
coreml_model <- coreml_convert(model)
path <- file.path(tempdir(), "test.mlmodel")
coreml_save(coreml_model, path)
mod <- coreml_load(path)
newdata <- as.matrix(mtcars[, c("hp", "cyl", "drat")])
all.equal(
coreml_predict(mod, newdata),
predict(model, mtcars, type = "response")
)
#> [1] TRUE