Cosmin Cătălin Sanda
Data Scientist & Engineer
Cosmin Catalin Sanda
th
4 April 2019
Cosmin Catalin Sanda
Data Scientist and Engineer at AudienceProject
Blogging at https://cosminsanda.com
In early 2018 Microsoft open-sourced ML.NET
Open source interchange format between ML models
Unfortunately there's a but
ONNX scoring in ML.NET works only on Windows X64 platforms (currently)
from mxnet.gluon.nn import HybridSequential, Dense, Dropout
net = HybridSequential()
with net.name_scope():
net.add(Dense(9))
net.add(Dropout(.25))
net.add(Dense(16))
net.add(Dropout(.25))
net.add(Dense(1))
Kaggle's New York City Taxi Fare Prediction
from mxnet.contrib import onnx as onnx_mxnet
def save(net, model_dir):
net.export("model", epoch=4)
onnx_mxnet.export_model(sym="model-symbol.json",
params="model-0004.params",
input_shape=[(1, 4)],
input_type=np.float32,
onnx_file_path="{}/model.onnx".format(model_dir),
verbose=True)
Model artifact (archive) is uploaded to S3 by Sagemaker
MLContext _env = env;
string _onnxFilePath = onnxFilePath;
var pipeline = new ColumnConcatenatingEstimator(_env, "Features", "RateCode", "PassengerCount", "TripTime", "TripDistance")
.Append(new ColumnSelectingEstimator(_env, "Features"))
.Append(new OnnxScoringEstimator(_env, _onnxFilePath, "Features", "Estimate"))
.Append(new ColumnSelectingEstimator(_env, "Estimate"))
.Append(new CustomMappingEstimator<RawPrediction, FlatPrediction>(_env, contractName: "OnnxPredictionExtractor",
mapAction: (input, output) =>
{
output.Estimate = input.Estimate[0];
}));
var transformer = pipeline.Fit(data);
FROM microsoft/dotnet:sdk AS build-env
COPY inference/*.csproj /src/
COPY inference/*.cs /src/
WORKDIR /src
RUN dotnet restore --verbosity normal
RUN dotnet publish -c Release -o /app/ -r win10-x64
FROM microsoft/dotnet:aspnetcore-runtime
COPY --from=build-env /app/ /app/
COPY models/model.onnx /models/
COPY lib/* /app/
WORKDIR /app
CMD ["dotnet", "inference.dll", "--onnx", "/models/model.onnx"]
By Cosmin Cătălin Sanda
Learn how to use the power of MXNet modes when doing inference in ML.NET