For a more detailed guide refer to tensorflow
or pytorch
example or to the documentation on https://clipper.ai.
In [2]:
import mxnet as mx
import numpy as np
In [3]:
mx.__version__
Out[3]:
In [4]:
# random data generation
def get_test_point():
return [np.random.randint(255)/255.0 for _ in range(785)]
In [5]:
data = [np.random.rand(785), np.random.rand(785)]
In [6]:
# MXNet model will read data from a file, so it is saved now
np.savetxt("mxnet.data", data, delimiter=",", fmt='%.3f')
# To see saved data uncomment the lines below
# data_load = np.loadtxt("mxnet.data", delimiter=",")
# print(data_load)
In [7]:
def predict(model, xs):
data_iter = mx.io.NDArrayIter(xs)
preds = model.predict(data_iter)
preds = [preds[0]]
return [str(p) for p in preds]
train_path = "mxnet.data"
data_iter = mx.io.CSVIter(
data_csv=train_path, data_shape=(785, ), batch_size=1)
# Create a MXNet model
# Configure a two layer neural network
data = mx.symbol.Variable('data')
fc1 = mx.symbol.FullyConnected(data, name='fc1', num_hidden=128)
act1 = mx.symbol.Activation(fc1, name='relu1', act_type='relu')
fc2 = mx.symbol.FullyConnected(act1, name='fc2', num_hidden=10)
softmax = mx.symbol.SoftmaxOutput(fc2, name='softmax')
# Initialize the module and fit it
mxnet_model = mx.mod.Module(softmax)
mxnet_model.fit(data_iter, num_epoch=0)
train_data_shape = data_iter.provide_data
In [8]:
from clipper_admin import ClipperConnection, DockerContainerManager
from clipper_admin.deployers.mxnet import deploy_mxnet_model
In [9]:
clipper_conn = ClipperConnection(DockerContainerManager())
In [10]:
clipper_conn.start_clipper()
In [11]:
clipper_conn.connect()
clipper_conn.get_all_apps()
Out[11]:
In [12]:
clipper_conn.get_all_models()
Out[12]:
In [13]:
clipper_conn.register_application(name="mxnet-app", input_type="doubles",
default_output="-1.0", slo_micros=100000)
In [14]:
deploy_mxnet_model(
clipper_conn,
name="mxnet-mod",
version = 1,
input_type="doubles",
func=predict,
mxnet_model=mxnet_model,
mxnet_data_shapes=train_data_shape,
batch_size=1)
In [15]:
clipper_conn.link_model_to_app(
app_name="mxnet-app",
model_name="mxnet-mod")
In [16]:
clipper_conn.get_all_apps()
Out[16]:
In [17]:
# Get query address
query_address = clipper_conn.get_query_addr()
In [18]:
# Run a query
import requests, json, numpy as np
headers = {"Content-type": "application/json"}
requests.post("http://"+query_address+"/mxnet-app/predict", headers=headers,
data=json.dumps({"input": get_test_point()})).json()
Out[18]:
In [19]:
clipper_conn.unlink_model_from_app(model_name="mxnet-mod", app_name="mxnet-app")
In [20]:
clipper_conn.stop_models('mxnet-mod')
In [21]:
clipper_conn.delete_application('mxnet-app')
In [22]:
clipper_conn.stop_all()
In [ ]: