Skip to content

Commit

Permalink
Merge pull request #143 from cliveseldon/mnist_loadtest
Browse files Browse the repository at this point in the history
MNIST loadtest
  • Loading branch information
ukclivecox authored Apr 15, 2018
2 parents af8f432 + 92c7af4 commit 831c391
Show file tree
Hide file tree
Showing 23 changed files with 476 additions and 17 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,6 @@ examples/models/mean_classifier/build/
notebooks/my-ml-deployment/

wrappers/s2i/python/Dockerfile
wrappers/s2i/python/_wrappers
wrappers/s2i/python/_wrappers

.Rhistory
9 changes: 7 additions & 2 deletions examples/models/deep_mnist/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ python ../../../wrappers/testing/tester.py contract.json 0.0.0.0 5000 -p
minikube start --memory 4096
```

[Install seldon core](/readme.md#install)
Install seldon core

```
helm install seldon-core-crd --name seldon-core-crd --repo https://storage.googleapis.com/seldon-charts --set usage_metrics.enabled=true --set rbac.enabled=false
helm install seldon-core --name seldon-core --repo https://storage.googleapis.com/seldon-charts --set rbac.enabled=false
```

Connect to Minikube Docker daemon

Expand All @@ -68,7 +73,7 @@ kubectl create -f deep_mnist.json
Port forward API server

```bash
kubectl port-forward $(kubectl get pods -n seldon -l app=seldon-apiserver-container-app -o jsonpath='{.items[0].metadata.name}') -n seldon 8080:8080
kubectl port-forward $(kubectl get pods -l app=seldon-apiserver-container-app -o jsonpath='{.items[0].metadata.name}') 8080:8080
```

Ensure tester gRPC modules compiled. You will need to install grpc-tools module.
Expand Down
9 changes: 7 additions & 2 deletions examples/models/keras_mnist/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,12 @@ python ../../../wrappers/testing/tester.py contract.json 0.0.0.0 5000 -p
minikube start --memory 4096
```

[Install seldon core](/readme.md#install)
Install seldon core

```
helm install seldon-core-crd --name seldon-core-crd --repo https://storage.googleapis.com/seldon-charts --set usage_metrics.enabled=true --set rbac.enabled=false
helm install seldon-core --name seldon-core --repo https://storage.googleapis.com/seldon-charts --set rbac.enabled=false
```

Connect to Minikube Docker daemon

Expand All @@ -69,7 +74,7 @@ kubectl create -f keras_mnist_deployment.json
Port forward API server

```bash
kubectl port-forward $(kubectl get pods -n seldon -l app=seldon-apiserver-container-app -o jsonpath='{.items[0].metadata.name}') -n seldon 8080:8080
kubectl port-forward $(kubectl get pods -l app=seldon-apiserver-container-app -o jsonpath='{.items[0].metadata.name}') 8080:8080
```

Ensure tester gRPC modules compiled.
Expand Down
11 changes: 8 additions & 3 deletions examples/models/r_iris/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Deep MNIST
# R model for IRIS dataset
An R Iris model.

## Depenencies
Expand Down Expand Up @@ -43,7 +43,12 @@ python ../../../wrappers/testing/tester.py contract.json 0.0.0.0 5000 -p
minikube start --memory 4096
```

[Install seldon core](/readme.md#install)
Install seldon core

```
helm install seldon-core-crd --name seldon-core-crd --repo https://storage.googleapis.com/seldon-charts --set usage_metrics.enabled=true --set rbac.enabled=false
helm install seldon-core --name seldon-core --repo https://storage.googleapis.com/seldon-charts --set rbac.enabled=false
```

Connect to Minikube Docker daemon

Expand All @@ -66,7 +71,7 @@ kubectl create -f r_iris_deployment.json
Port forward API server

```bash
kubectl port-forward $(kubectl get pods -n seldon -l app=seldon-apiserver-container-app -o jsonpath='{.items[0].metadata.name}') -n seldon 8080:8080
kubectl port-forward $(kubectl get pods -l app=seldon-apiserver-container-app -o jsonpath='{.items[0].metadata.name}') 8080:8080
```

Ensure tester gRPC modules compiled.
Expand Down
4 changes: 4 additions & 0 deletions examples/models/r_mnist/.s2i/environment
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
MODEL_NAME=mnist.R
API_TYPE=REST
SERVICE_TYPE=MODEL
PERSISTENCE=0
9 changes: 9 additions & 0 deletions examples/models/r_mnist/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@


train:
./get_data.sh
Rscript train.R

clean:
rm -f *ubyte
rm -f model.Rds
88 changes: 88 additions & 0 deletions examples/models/r_mnist/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# Deep MNIST
An R MNIST model.

## Depenencies

R

## Train locally

```bash
make train
```

## Wrap using [s2i](https://github.com/openshift/source-to-image#installation).

```bash
s2i build . seldonio/seldon-core-s2i-r r-mnist:0.1
```

## Local Docker Smoke Test

Run under docker.

```bash
docker run --rm -p 5000:5000 r-mnist:0.1
```

Ensure test grpc modules compiled.

```bash
pushd ../../../wrappers/testing ; make build_protos ; popd
```

Send a data request using the wrapper tester.

```bash
python ../../../wrappers/testing/tester.py contract.json 0.0.0.0 5000 -p
```

## Minikube test

```bash
minikube start --memory 4096
```

Install seldon core

```
helm install seldon-core-crd --name seldon-core-crd --repo https://storage.googleapis.com/seldon-charts --set usage_metrics.enabled=true --set rbac.enabled=false
helm install seldon-core --name seldon-core --repo https://storage.googleapis.com/seldon-charts --set rbac.enabled=false
```

Connect to Minikube Docker daemon

```bash
eval $(minikube docker-env)
```

Build image using minikube docker daemon.

```bash
s2i build . seldonio/seldon-core-s2i-r r-mnist:0.1
```

Launch deployment

```bash
kubectl create -f r_mnist_deployment.json -n seldon
```

Port forward API server

```bash
kubectl port-forward $(kubectl get pods -l app=seldon-apiserver-container-app -o jsonpath='{.items[0].metadata.name}') 8080:8080
```

Ensure tester gRPC modules compiled.

```bash
pushd ../../../util/api_tester ; make build_protos ; popd
```

Send test request
```bash
python ../../../util/api_tester/api-tester.py contract.json 0.0.0.0 8080 --oauth-key oauth-key --oauth-secret oauth-secret -p
```


22 changes: 22 additions & 0 deletions examples/models/r_mnist/contract.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"features":[
{
"name":"X",
"dtype":"FLOAT",
"ftype":"continuous",
"range":[0,1],
"repeat":784
}
],
"targets":[
{
"name":"class",
"dtype":"FLOAT",
"ftype":"continuous",
"range":[0,1],
"repeat":10
}
]
}


10 changes: 10 additions & 0 deletions examples/models/r_mnist/get_data.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
wget http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz
wget http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz
wget http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz
wget http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz


gunzip train-images-idx3-ubyte.gz
gunzip train-labels-idx1-ubyte.gz
gunzip t10k-images-idx3-ubyte.gz
gunzip t10k-labels-idx1-ubyte.gz
2 changes: 2 additions & 0 deletions examples/models/r_mnist/install.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
install.packages('pls')

17 changes: 17 additions & 0 deletions examples/models/r_mnist/mnist.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
library(methods)

predict.mnist <- function(mnist,newdata=list()) {
cn <- 1:784
for (i in seq_along(cn)){cn[i] <- paste("X",cn[i],sep = "")}
colnames(newdata) <- cn
predict(mnist$model, newdata = newdata, type='prob')
}

new_mnist <- function(filename) {
model <- readRDS(filename)
structure(list(model=model), class = "mnist")
}

initialise_seldon <- function(params) {
new_mnist("model.Rds")
}
53 changes: 53 additions & 0 deletions examples/models/r_mnist/r_mnist_deployment.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"apiVersion": "machinelearning.seldon.io/v1alpha1",
"kind": "SeldonDeployment",
"metadata": {
"labels": {
"app": "seldon"
},
"name": "seldon-deployment-example"
},
"spec": {
"annotations": {
"project_name": "Mnist classification",
"deployment_version": "0.1"
},
"name": "r-mnist-deployment",
"oauth_key": "oauth-key",
"oauth_secret": "oauth-secret",
"predictors": [
{
"componentSpec": {
"spec": {
"containers": [
{
"image": "r-mnist:0.1",
"imagePullPolicy": "IfNotPresent",
"name": "r-mnist-classifier",
"resources": {
"requests": {
"memory": "1Mi"
}
}
}
],
"terminationGracePeriodSeconds": 20
}
},
"graph": {
"children": [],
"name": "r-mnist-classifier",
"endpoint": {
"type" : "REST"
},
"type": "MODEL"
},
"name": "r-mnist-predictor",
"replicas": 1,
"annotations": {
"predictor_version" : "0.1"
}
}
]
}
}
68 changes: 68 additions & 0 deletions examples/models/r_mnist/train.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
library(caret)
library(doParallel)

# Enable parallel processing.
cl <- makeCluster(detectCores())
registerDoParallel(cl)

# Load the MNIST digit recognition dataset into R
# http://yann.lecun.com/exdb/mnist/
# assume you have all 4 files and gunzip'd them
# creates train$n, train$x, train$y and test$n, test$x, test$y
# e.g. train$x is a 60000 x 784 matrix, each row is one digit (28x28)
# call: show_digit(train$x[5,]) to see a digit.
# brendan o'connor - gist.github.com/39760 - anyall.org
load_mnist <- function() {
load_image_file <- function(filename) {
ret = list()
f = file(filename,'rb')
readBin(f,'integer',n=1,size=4,endian='big')
ret$n = readBin(f,'integer',n=1,size=4,endian='big')
nrow = readBin(f,'integer',n=1,size=4,endian='big')
ncol = readBin(f,'integer',n=1,size=4,endian='big')
x = readBin(f,'integer',n=ret$n*nrow*ncol,size=1,signed=F)
ret$x = matrix(x, ncol=nrow*ncol, byrow=T)
close(f)
ret
}
load_label_file <- function(filename) {
f = file(filename,'rb')
readBin(f,'integer',n=1,size=4,endian='big')
n = readBin(f,'integer',n=1,size=4,endian='big')
y = readBin(f,'integer',n=n,size=1,signed=F)
close(f)
y
}
train <<- load_image_file('train-images-idx3-ubyte')
test <<- load_image_file('t10k-images-idx3-ubyte')

train$y <<- load_label_file('train-labels-idx1-ubyte')
test$y <<- load_label_file('t10k-labels-idx1-ubyte')
}

show_digit <- function(arr784, col=gray(12:1/12), ...) {
image(matrix(arr784, nrow=28)[,28:1], col=col, ...)
}

train <- data.frame()
test <- data.frame()

# Load data.
load_mnist()

# Normalize: X = (X - min) / (max - min) => X = (X - 0) / (255 - 0) => X = X / 255.
train$x <- train$x / 255

# Setup training data with digit and pixel values with 60/40 split for train/cv.
inTrain = data.frame(y=train$y, train$x)
inTrain$y <- as.factor(inTrain$y)
trainIndex = createDataPartition(inTrain$y, p = 0.60,list=FALSE)
training = inTrain[trainIndex,]
cv = inTrain[-trainIndex,]

# SVM. 95/94.
#fit <- train(y ~ ., data = head(training, 1000), method = 'svmRadial', tuneGrid = data.frame(sigma=0.0107249, C=1))
fit <- train(y ~ ., data = head(training, 1000), method = 'pls')
results <- predict(fit, newdata = head(cv, 1000), type='prob')
#confusionMatrix(results, head(cv$y, 1000))
saveRDS(fit, file = "model.Rds", compress = TRUE)
9 changes: 7 additions & 2 deletions examples/models/sklearn_iris/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,12 @@ python ../../../wrappers/testing/tester.py contract.json 0.0.0.0 5000 -p
minikube start --memory 4096
```

[Install seldon core](/readme.md#install)
Install seldon core

```
helm install seldon-core-crd --name seldon-core-crd --repo https://storage.googleapis.com/seldon-charts --set usage_metrics.enabled=true --set rbac.enabled=false
helm install seldon-core --name seldon-core --repo https://storage.googleapis.com/seldon-charts --set rbac.enabled=false
```

Connect to Minikube Docker daemon

Expand All @@ -69,7 +74,7 @@ kubectl create -f sklearn_iris_deployment.json
Port forward API server

```bash
kubectl port-forward $(kubectl get pods -n seldon -l app=seldon-apiserver-container-app -o jsonpath='{.items[0].metadata.name}') -n seldon 8080:8080
kubectl port-forward $(kubectl get pods -l app=seldon-apiserver-container-app -o jsonpath='{.items[0].metadata.name}') 8080:8080
```

Ensure tester gRPC modules compiled.
Expand Down
2 changes: 1 addition & 1 deletion helm-charts/seldon-core-loadtesting/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apiVersion: v1
description: Loadtesting for seldon core
name: seldon-core-loadtesting
version: 0.1.0
version: 0.1.1
keywords:
- kubernetes
- machine-learning
Expand Down
Loading

0 comments on commit 831c391

Please sign in to comment.