Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library(keras)
- library(mrsdeploy)
- #Keras objects cannot be saved and restored across R sessions, so load a keras model and convert it to a r model
- kerasModel = load_model_hdf5('my_model.h5')
- rModel = serialize_model(kerasModel, include_optimizer = TRUE)
- kerasFunc <- function(x) {
- library(keras)
- kerasModel = unserialize_model(rModel, custom_objects = NULL, compile = TRUE)
- prediction <- predict_classes(kerasModel, x)
- answer = as.list(prediction)
- }
- serviceName <- paste0("kerasService", round(as.numeric(Sys.time()), 0))
- api <- publishService(
- serviceName,
- code = kerasFunc,
- model = rModel,
- inputs = list(x="matrix"),
- outputs = list(answer="vector"),
- v = "v1.0.0"
- )
- swagger <- api$swagger()
- cat(swagger, file = "swagger.json", append = FALSE)
- # Load test data
- mnist <- dataset_mnist()
- x_test <- mnist$test$x
- x_test <- array_reshape(x_test, c(nrow(x_test), 784))
- x_test <- x_test / 255
- x = matrix(c(x_test[1,]), nrow=1, ncol=784)
- #consume the keras model api
- result<-api$kerasFunc(x)
- #output is the predicted number
- print(result$output("answer"))
Add Comment
Please, Sign In to add comment