From 55fd791d95f5887f59c151dc1732246d0e47eed8 Mon Sep 17 00:00:00 2001 From: pallavi-pannu-oc Date: Mon, 9 Nov 2020 16:38:08 +0000 Subject: [PATCH 1/6] r-serving --- R/classification/mnist/transformer/Dockerfile | 23 ++ .../mnist/transformer/microservice.R | 328 ++++++++++++++++++ .../mnist/transformer/transformer.R | 21 ++ 3 files changed, 372 insertions(+) create mode 100644 R/classification/mnist/transformer/Dockerfile create mode 100644 R/classification/mnist/transformer/microservice.R create mode 100644 R/classification/mnist/transformer/transformer.R diff --git a/R/classification/mnist/transformer/Dockerfile b/R/classification/mnist/transformer/Dockerfile new file mode 100644 index 00000000..5a7f5bf6 --- /dev/null +++ b/R/classification/mnist/transformer/Dockerfile @@ -0,0 +1,23 @@ +FROM rocker/r-apt:bionic + +RUN apt-get update && \ + apt-get install -y -qq \ + r-cran-plumber \ + r-cran-jsonlite \ + r-cran-optparse \ + r-cran-stringr \ + r-cran-urltools \ + r-cran-caret \ + r-cran-randomforest \ + r-cran-pls \ + curl + +RUN mkdir microservice +COPY . /microservice +WORKDIR /microservice + +#dkube-kfserving port must be 8080 +EXPOSE 8080 + +#dkube-kfserving use ENTRYPOINT in the below format instead of CMD +ENTRYPOINT ["Rscript", "microservice.R", "--transformer=transformer.R", "--api=REST", "--service=MODEL", "--persistence=0"] diff --git a/R/classification/mnist/transformer/microservice.R b/R/classification/mnist/transformer/microservice.R new file mode 100644 index 00000000..85e5ccba --- /dev/null +++ b/R/classification/mnist/transformer/microservice.R @@ -0,0 +1,328 @@ +library(plumber) +library(jsonlite) +library(optparse) +library(methods) +library(urltools) +library(stringi) + +parseQS <- function(qs){ + if (is.null(qs) || length(qs) == 0 || qs == "") { + return(list()) + } + if (stri_startswith_fixed(qs, "?")) { + qs <- substr(qs, 2, nchar(qs)) + } + + parts <- strsplit(qs, "&", fixed = TRUE)[[1]] + kv <- strsplit(parts, "=", fixed = TRUE) + kv <- kv[sapply(kv, length) == 2] # Ignore incompletes + + keys <- sapply(kv, "[[", 1) + keys <- unname(sapply(keys, url_decode)) + + vals <- sapply(kv, "[[", 2) + vals[is.na(vals)] <- "" + vals <- unname(sapply(vals, url_decode)) + + ret <- as.list(vals) + names(ret) <- keys + + # If duplicates, combine + combine_elements <- function(name){ + unname(unlist(ret[names(ret)==name])) + } + + unique_names <- unique(names(ret)) + + ret <- lapply(unique_names, combine_elements) + names(ret) <- unique_names + + ret +} + + +v <- function(...) cat(sprintf(...), sep='', file=stdout()) + +validate_json <- function(jdf) { + if (!"data" %in% names(jdf)) { + return("data field is missing") + } + else if (!("ndarray" %in% names(jdf$data) || "tensor" %in% names(jdf$data)) ) { + return("data field must contain ndarray or tensor field") + } + else{ + return("OK") + } +} + +validate_feedback <- function(jdf) { + if (!"request" %in% names(jdf)) + { + return("request field is missing") + } + else if (!"reward" %in% names(jdf)) + { + return("reward field is missing") + } + else if (!"data" %in% names(jdf$request)) { + return("data request field is missing") + } + else if (!("ndarray" %in% names(jdf$request$data) || "tensor" %in% names(jdf$request$data)) ) { + return("data field must contain ndarray or tensor field") + } + else{ + return("OK") + } +} + + + +create_response <- function(req_df,res_df){ + if ("ndarray" %in% names(req_df$data)){ + templ <- '{"data":{"names":%s,"ndarray":%s}}' + names <- toJSON(colnames(res_df)) + values <- toJSON(as.matrix(res_df)) + sprintf(templ,names,values) + } else { + templ <- '{"data":{"names":%s,"tensor":{"shape":%s,"values":%s}}}' + names <- toJSON(colnames(res_df)) + values <- toJSON(c(res_df)) + dims <- toJSON(dim(res_df)) + sprintf(templ,names,dims,values) + } +} + + + +# See https://github.com/trestletech/plumber/issues/105 +parse_data <- function(req){ + parsed <- req$postBody + class(parsed) <- "json" + print(req$postBody) + #parsed$json + parsed +} + +predict_endpoint <- function(req,res,json=NULL,isDefault=NULL) { + #for ( obj in ls(req) ) { + #print(c(obj,get(obj,envir = req))) + #} + json <- parse_data(req) # Hack as Plumber using URLDecode which doesn't decode + + jdf <- fromJSON(json) + valid_input <- validate_json(jdf) + if (valid_input[1] == "OK") { + df <- preprocess(jdf) # transformer preprocess function call + scores <- predict(user_model,newdata=df) # predict function call from mnist.R + scores <- postprocess(scores) # postprocess function call + res_json = create_response(jdf,scores) + res$body <- res_json + res + } else { + res$status <- 400 # Bad request + list(error=jsonlite::unbox(valid_input)) + } +} + +send_feedback_endpoint <- function(req,res,json=NULL,isDefault=NULL) { + json <- parse_data(req) + jdf <- fromJSON(json) + valid_input <- validate_feedback(jdf) + if (valid_input[1] == "OK") { + request <- create_dataframe(jdf$request) + if ("truth" %in% names(jdf)){ + truth <- create_dataframe(jdf$truth) + } else { + truth <- NULL + } + #reward <- jdf$reward + send_feedback(user_model,request=request,reward=1,truth=truth) + res$body <- "{}" + res + } else { + res$status <- 400 # Bad request + list(error=jsonlite::unbox(valid_input)) + } +} + + +transform_input_endpoint <- function(req,res,json=NULL,isDefault=NULL) { + json <- parse_data(req) + jdf <- fromJSON(json) + valid_input <- validate_json(jdf) + if (valid_input[1] == "OK") { + df <- create_dataframe(jdf) + trans <- transform_input(user_model,newdata=df) + res_json = create_response(jdf,trans) + res$body <- res_json + res + } else { + res$status <- 400 # Bad request + list(error=jsonlite::unbox(valid_input)) + } +} + +transform_output_endpoint <- function(req,res,json=NULL,isDefault=NULL) { + json <- parse_data(req) + jdf <- fromJSON(json) + valid_input <- validate_json(jdf) + if (valid_input[1] == "OK") { + df <- create_dataframe(jdf) + trans <- transform_output(user_model,newdata=df) + res_json = create_response(jdf,trans) + res$body <- res_json + res + } else { + res$status <- 400 # Bad request + list(error=jsonlite::unbox(valid_input)) + } +} + +route_endpoint <- function(req,res,json=NULL,isDefault=NULL) { + json <- parse_data(req) + jdf <- fromJSON(json) + valid_input <- validate_json(jdf) + if (valid_input[1] == "OK") { + df <- create_dataframe(jdf) + routing <- route(user_model,data=df) + res_json = create_response(jdf,data.frame(list(routing))) + res$body <- res_json + res + } else { + res$status <- 400 # Bad request + list(error=jsonlite::unbox(valid_input)) + } +} + +parse_commandline <- function() { + parser <- OptionParser() + parser <- add_option(parser, c("-p", "--parameters"), type="character", + help="Parameters for component", metavar = "parameters") + parser <- add_option(parser, c("-m", "--model"), type="character", + help="Model file", metavar = "model") + parser <- add_option(parser, c("-t", "--transformer"), type="character", + help="Transformer file", metavar = "transformer") + parser <- add_option(parser, c("-s", "--service"), type="character", + help="Service type", metavar = "service", default = "MODEL") + parser <- add_option(parser, c("-a", "--api"), type="character", + help="API type - REST", metavar = "api", default = "REST") + parser <- add_option(parser, c("-e", "--persistence"), type="integer", + help="Persistence", metavar = "persistence", default = 0) + #dkube-kfserving - this option must be parsed + parser <- add_option(parser, c("-n", "--model_name"), type="character", + help="Name of the model", metavar = "model_name") + #dkube-kfserving - this option must be parsed + parser <- add_option(parser, c("-b", "--model_base_path"), type="character", + help="Model file base path", metavar = "model_base_path") + args <- parse_args(parser, args = commandArgs(trailingOnly = TRUE), + convert_hyphens_to_underscores = TRUE) + + if (is.null(args$parameters)){ + args$parameters <- Sys.getenv("PREDICTIVE_UNIT_PARAMETERS") + } + + if (args$parameters == ''){ + args$parameters = "[]" + } + + args +} + + +extract_parmeters <- function(params) { + j = fromJSON(params) + values <- list() + names <- list() + for (i in seq_along(j)) + { + name <- j[i,"name"] + value <- j[i,"value"] + type <- j[i,"type"] + if (type == "INT") + value <- as.integer(value) + else if (type == "FLOAT") + value <- as.double(value) + else if (type == "BOOL") + value <- as.logical(type.convert(value)) + values <- c(values,value) + names <- c(names,name) + } + names(values) <- names + values +} + +validate_commandline <- function(args) { + if (!is.element(args$service,c("MODEL","ROUTER","COMBINER","TRANSFORMER"))) { + v("Invalid service type [%s]\n",args$service) + 1 + }else if (!is.element(args$api,c("REST"))) { + v("Invalid API type [%s]\n",args$api) + 1 + } + else{ + 0 + } +} + +# Parse command line and validate +args <- parse_commandline() +if (validate_commandline(args) > 0){ + quit(status=1) +} +params <- extract_parmeters(args$parameters) + +# Check user model exists +if(!file.exists(args$model)){ + v("Model file does not exist [%s]\n",args$model) + quit(status=1) +} + +#Load user model +#dkube-kfserving - load file from specified base path +model_base_path <- args$model_base_path +model_file <- sprintf("%s/model.Rds", model_base_path) +#dkube-kfserving - load file from specified base path + +source(args$model) +source(args$transformer) +#dkube-kfserving - pass the model_file to this function +user_model <- initialise_seldon(model_file, params) + +# Setup generics +# Predict already exists in base R +send_feedback <- function(x,...) UseMethod("send_feedback", x) +route <- function(x,...) UseMethod("route",x) +transform_input <- function(x,...) UseMethod("transform_input",x) +transform_output <- function(x,...) UseMethod("transform_output",x) + +#dkube-kfserving - read model name from args +modelname <- args$model_name +serve_model <- plumber$new() +if (args$service == "MODEL") { + #dkube-kfserving - serve at this url + url <- sprintf("/v1/models/%s:predict",modelname) + serve_model$handle("POST", url,predict_endpoint) +} else if (args$service == "ROUTER") { + #serve_model$handle("POST", "/route",route_endpoint) + #serve_model$handle("GET", "/route",route_endpoint) + #serve_model$handle("POST", "/send-feedback",send_feedback_endpoint) + #serve_model$handle("GET", "/send-feedback",send_feedback_endpoint) +} else if (args$service == "TRANSFORMER") { + #serve_model$handle("POST", "/transform-output",transform_output_endpoint) + #serve_model$handle("GET", "/transform-output",transform_output_endpoint) + #serve_model$handle("POST", "/transform-input",transform_input_endpoint) + #serve_model$handle("GET", "/transform-input",transform_input_endpoint) + +} else +{ + v("Unknown service type [%s]\n",args$service) + quit(status=1) +} + +port <- Sys.getenv("PREDICTIVE_UNIT_SERVICE_PORT") +if (port == ''){ + #dkube-kfserving - port has to be 8080 + port <- 8080 +} else { + port <- as.integer(port) +} +serve_model$run(host="0.0.0.0", port = 8080) diff --git a/R/classification/mnist/transformer/transformer.R b/R/classification/mnist/transformer/transformer.R new file mode 100644 index 00000000..5f0e1a9f --- /dev/null +++ b/R/classification/mnist/transformer/transformer.R @@ -0,0 +1,21 @@ +library(methods) +library(randomForest) +library(plumber) +library(jsonlite) +library(optparse) +library(urltools) +library(stringi) + + + +preprocess <- function(json_data) { +result <- fromJSON(json_data) +df<-data.frame(result$inputs) +return df +} + +postprocess <- function(df){ +df +} + + From 05ee84c149ff24ebd5b1c849cb8b40dbe53de61b Mon Sep 17 00:00:00 2001 From: pallavi-pannu-oc Date: Tue, 17 Nov 2020 05:14:27 +0000 Subject: [PATCH 2/6] r-transformer --- R/classification/mnist/transformer/Dockerfile | 2 +- .../mnist/transformer/transformer.R | 23 ++++++++++++++++--- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/R/classification/mnist/transformer/Dockerfile b/R/classification/mnist/transformer/Dockerfile index 5a7f5bf6..84c1f943 100644 --- a/R/classification/mnist/transformer/Dockerfile +++ b/R/classification/mnist/transformer/Dockerfile @@ -20,4 +20,4 @@ WORKDIR /microservice EXPOSE 8080 #dkube-kfserving use ENTRYPOINT in the below format instead of CMD -ENTRYPOINT ["Rscript", "microservice.R", "--transformer=transformer.R", "--api=REST", "--service=MODEL", "--persistence=0"] +ENTRYPOINT ["Rscript", "microservice.R","--model=mnist.R", "--transformer=transformer.R", "--api=REST", "--service=MODEL", "--persistence=0"] diff --git a/R/classification/mnist/transformer/transformer.R b/R/classification/mnist/transformer/transformer.R index 5f0e1a9f..86aac99d 100644 --- a/R/classification/mnist/transformer/transformer.R +++ b/R/classification/mnist/transformer/transformer.R @@ -6,14 +6,31 @@ library(optparse) library(urltools) library(stringi) +library(hash) +library(float) +library(utf8) +library(OpenImage) preprocess <- function(json_data) { -result <- fromJSON(json_data) -df<-data.frame(result$inputs) -return df + json_data$instances<-NULL + data=json_data$signatures$inputs[[1]][[3]] + json_data=utf8_encode(data) + image <- readImage("image.png") + image <- resizeImage(image, width = 28, height = 28, method = 'nearest') + image <- array(image[1:28,1:28], dim = c(1,1,28,28)) + image <- dbl(image) + instances<-list(image) + token <- json_data["token"] + payload<-hash() + payload[["instances"]]=instances + payload[["token"]]=token + payload } + + + postprocess <- function(df){ df } From a85f8e5aac3c98e551ed795f8ace2d0b9c1485fa Mon Sep 17 00:00:00 2001 From: pallavi-pannu-oc Date: Tue, 17 Nov 2020 09:19:21 +0000 Subject: [PATCH 3/6] r-transformer --- .../mnist/transformer/microservice.R | 26 +++++++++---------- .../mnist/transformer/transformer.R | 6 +---- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/R/classification/mnist/transformer/microservice.R b/R/classification/mnist/transformer/microservice.R index 85e5ccba..1b0f7776 100644 --- a/R/classification/mnist/transformer/microservice.R +++ b/R/classification/mnist/transformer/microservice.R @@ -112,9 +112,9 @@ predict_endpoint <- function(req,res,json=NULL,isDefault=NULL) { valid_input <- validate_json(jdf) if (valid_input[1] == "OK") { df <- preprocess(jdf) # transformer preprocess function call - scores <- predict(user_model,newdata=df) # predict function call from mnist.R - scores <- postprocess(scores) # postprocess function call - res_json = create_response(jdf,scores) + class <- predict(user_model,newdata=df$instances) # predict function call from mnist.R + class <- postprocess(class) # postprocess function call + res_json = create_response(jdf,class) res$body <- res_json res } else { @@ -208,11 +208,9 @@ parse_commandline <- function() { parser <- add_option(parser, c("-e", "--persistence"), type="integer", help="Persistence", metavar = "persistence", default = 0) #dkube-kfserving - this option must be parsed - parser <- add_option(parser, c("-n", "--model_name"), type="character", - help="Name of the model", metavar = "model_name") + #parser <- add_option(parser, c("-n", "--model_name"), type="character",help="Name of the model", metavar = "model_name") #dkube-kfserving - this option must be parsed - parser <- add_option(parser, c("-b", "--model_base_path"), type="character", - help="Model file base path", metavar = "model_base_path") + #parser <- add_option(parser, c("-b", "--model_url"), type="character",help="serving model url", metavar = "model_base_path") args <- parse_args(parser, args = commandArgs(trailingOnly = TRUE), convert_hyphens_to_underscores = TRUE) @@ -278,14 +276,15 @@ if(!file.exists(args$model)){ #Load user model #dkube-kfserving - load file from specified base path -model_base_path <- args$model_base_path -model_file <- sprintf("%s/model.Rds", model_base_path) +#model_base_path <- args$model_base_path +#model_file <- sprintf("%s/model.Rds", model_base_path) #dkube-kfserving - load file from specified base path + source(args$model) -source(args$transformer) +user_model <- model #dkube-kfserving - pass the model_file to this function -user_model <- initialise_seldon(model_file, params) + # Setup generics # Predict already exists in base R @@ -306,8 +305,8 @@ if (args$service == "MODEL") { #serve_model$handle("GET", "/route",route_endpoint) #serve_model$handle("POST", "/send-feedback",send_feedback_endpoint) #serve_model$handle("GET", "/send-feedback",send_feedback_endpoint) -} else if (args$service == "TRANSFORMER") { - #serve_model$handle("POST", "/transform-output",transform_output_endpoint) +} else if (args$service == "TRANSFORMER") { + serve_model$handle("POST",/transform-output,predict_endpoint) #serve_model$handle("GET", "/transform-output",transform_output_endpoint) #serve_model$handle("POST", "/transform-input",transform_input_endpoint) #serve_model$handle("GET", "/transform-input",transform_input_endpoint) @@ -326,3 +325,4 @@ if (port == ''){ port <- as.integer(port) } serve_model$run(host="0.0.0.0", port = 8080) + diff --git a/R/classification/mnist/transformer/transformer.R b/R/classification/mnist/transformer/transformer.R index 86aac99d..3bb3e3ff 100644 --- a/R/classification/mnist/transformer/transformer.R +++ b/R/classification/mnist/transformer/transformer.R @@ -15,11 +15,9 @@ library(OpenImage) preprocess <- function(json_data) { json_data$instances<-NULL data=json_data$signatures$inputs[[1]][[3]] - json_data=utf8_encode(data) image <- readImage("image.png") image <- resizeImage(image, width = 28, height = 28, method = 'nearest') - image <- array(image[1:28,1:28], dim = c(1,1,28,28)) - image <- dbl(image) + image <- array(image[1:28,1:28], dim = c(1,784)) instances<-list(image) token <- json_data["token"] payload<-hash() @@ -29,8 +27,6 @@ preprocess <- function(json_data) { } - - postprocess <- function(df){ df } From 0292b5726ad86f2947a2a8bc132eec3f17127d14 Mon Sep 17 00:00:00 2001 From: pallavi-pannu-oc Date: Tue, 17 Nov 2020 09:41:37 +0000 Subject: [PATCH 4/6] fix-rserving --- R/classification/mnist/transformer/microservice.R | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/R/classification/mnist/transformer/microservice.R b/R/classification/mnist/transformer/microservice.R index 1b0f7776..ac985ee5 100644 --- a/R/classification/mnist/transformer/microservice.R +++ b/R/classification/mnist/transformer/microservice.R @@ -112,10 +112,10 @@ predict_endpoint <- function(req,res,json=NULL,isDefault=NULL) { valid_input <- validate_json(jdf) if (valid_input[1] == "OK") { df <- preprocess(jdf) # transformer preprocess function call - class <- predict(user_model,newdata=df$instances) # predict function call from mnist.R + class <- user_model %>% predict_classes(df$instances)# predict function call from mnist.R class <- postprocess(class) # postprocess function call - res_json = create_response(jdf,class) - res$body <- res_json + #res_json = create_response(jdf,class) + res$body <- class res } else { res$status <- 400 # Bad request From 88a82cb81b4e6e8c26b6960d1071838fa1ac923c Mon Sep 17 00:00:00 2001 From: pallavi-pannu-oc Date: Wed, 18 Nov 2020 08:03:45 +0000 Subject: [PATCH 5/6] r-transformer --- R/classification/mnist/transformer/Dockerfile | 7 +++++- .../mnist/transformer/get-image.py | 5 +++++ .../mnist/transformer/microservice.R | 22 +++++++++---------- .../mnist/transformer/transformer.R | 11 +++++----- 4 files changed, 28 insertions(+), 17 deletions(-) create mode 100644 R/classification/mnist/transformer/get-image.py diff --git a/R/classification/mnist/transformer/Dockerfile b/R/classification/mnist/transformer/Dockerfile index 84c1f943..5b74aeb4 100644 --- a/R/classification/mnist/transformer/Dockerfile +++ b/R/classification/mnist/transformer/Dockerfile @@ -12,12 +12,17 @@ RUN apt-get update && \ r-cran-pls \ curl +RUN R -e 'install.packages("OpenImageR")' +RUN R -e 'install.packages("hash")' +RUN R -e 'install.packages("reticulate")' + RUN mkdir microservice COPY . /microservice +COPY https://github.com/oneconvergence/dkube-examples/blob/2.1.5/R/classification/mnist/mnist.R /microservice WORKDIR /microservice #dkube-kfserving port must be 8080 EXPOSE 8080 #dkube-kfserving use ENTRYPOINT in the below format instead of CMD -ENTRYPOINT ["Rscript", "microservice.R","--model=mnist.R", "--transformer=transformer.R", "--api=REST", "--service=MODEL", "--persistence=0"] +ENTRYPOINT ["Rscript", "microservice.R","--model=mnist.R", "--transformer=transformer.R", "--api=REST", "--service=TRANSFORMER", "--persistence=0"] diff --git a/R/classification/mnist/transformer/get-image.py b/R/classification/mnist/transformer/get-image.py new file mode 100644 index 00000000..674007f9 --- /dev/null +++ b/R/classification/mnist/transformer/get-image.py @@ -0,0 +1,5 @@ +import base64 +def get_image(data): + data=data.encode() + with open("image.png", "wb") as fh: + fh.write(base64.decodebytes(data)) diff --git a/R/classification/mnist/transformer/microservice.R b/R/classification/mnist/transformer/microservice.R index ac985ee5..5e235f72 100644 --- a/R/classification/mnist/transformer/microservice.R +++ b/R/classification/mnist/transformer/microservice.R @@ -111,11 +111,10 @@ predict_endpoint <- function(req,res,json=NULL,isDefault=NULL) { jdf <- fromJSON(json) valid_input <- validate_json(jdf) if (valid_input[1] == "OK") { - df <- preprocess(jdf) # transformer preprocess function call - class <- user_model %>% predict_classes(df$instances)# predict function call from mnist.R - class <- postprocess(class) # postprocess function call - #res_json = create_response(jdf,class) - res$body <- class + scores <- predict(user_model,newdata=df) # predict function call from mnist.R + scores <- postprocess(scores) # postprocess function call + res_json = create_response(jdf,scores) + res$body <- res_json res } else { res$status <- 400 # Bad request @@ -166,10 +165,11 @@ transform_output_endpoint <- function(req,res,json=NULL,isDefault=NULL) { jdf <- fromJSON(json) valid_input <- validate_json(jdf) if (valid_input[1] == "OK") { - df <- create_dataframe(jdf) - trans <- transform_output(user_model,newdata=df) - res_json = create_response(jdf,trans) - res$body <- res_json + df <- preprocess(jdf) # transformer preprocess function call + class <- user_model %>% predict_classes(df$instances)# predict function call from mnist.R + class <- postprocess(class) # postprocess function call + #res_json = create_response(jdf,class) + res$body <- class res } else { res$status <- 400 # Bad request @@ -282,6 +282,7 @@ if(!file.exists(args$model)){ source(args$model) +source(args$transformer) user_model <- model #dkube-kfserving - pass the model_file to this function @@ -306,7 +307,7 @@ if (args$service == "MODEL") { #serve_model$handle("POST", "/send-feedback",send_feedback_endpoint) #serve_model$handle("GET", "/send-feedback",send_feedback_endpoint) } else if (args$service == "TRANSFORMER") { - serve_model$handle("POST",/transform-output,predict_endpoint) + serve_model$handle("POST",/transform-output,transform_output_endpoint) #serve_model$handle("GET", "/transform-output",transform_output_endpoint) #serve_model$handle("POST", "/transform-input",transform_input_endpoint) #serve_model$handle("GET", "/transform-input",transform_input_endpoint) @@ -325,4 +326,3 @@ if (port == ''){ port <- as.integer(port) } serve_model$run(host="0.0.0.0", port = 8080) - diff --git a/R/classification/mnist/transformer/transformer.R b/R/classification/mnist/transformer/transformer.R index 3bb3e3ff..3dd9c67d 100644 --- a/R/classification/mnist/transformer/transformer.R +++ b/R/classification/mnist/transformer/transformer.R @@ -7,17 +7,18 @@ library(urltools) library(stringi) library(hash) -library(float) -library(utf8) -library(OpenImage) +library(OpenImageR +library(reticulate) preprocess <- function(json_data) { json_data$instances<-NULL data=json_data$signatures$inputs[[1]][[3]] + source_python("get-image.py") + get_image(data) image <- readImage("image.png") - image <- resizeImage(image, width = 28, height = 28, method = 'nearest') - image <- array(image[1:28,1:28], dim = c(1,784)) + image <- array(image, dim = c(1,784)) + image <- image/255 instances<-list(image) token <- json_data["token"] payload<-hash() From 2a19461ba7daef177b709d204293499f3ca3d5c8 Mon Sep 17 00:00:00 2001 From: pallavi-pannu-oc Date: Wed, 18 Nov 2020 08:10:50 +0000 Subject: [PATCH 6/6] transformer --- R/classification/mnist/transformer/transformer.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/classification/mnist/transformer/transformer.R b/R/classification/mnist/transformer/transformer.R index 3dd9c67d..e1c4b918 100644 --- a/R/classification/mnist/transformer/transformer.R +++ b/R/classification/mnist/transformer/transformer.R @@ -7,7 +7,7 @@ library(urltools) library(stringi) library(hash) -library(OpenImageR +library(OpenImageR) library(reticulate)