Commit
·
de41f58
1
Parent(s):
75c2a12
Added gaussian_naive_bayes example
Browse files- scikit-learn/gaussian_nb/client.py +66 -0
- scikit-learn/gaussian_nb/convert2onnx.sh +1 -0
- scikit-learn/gaussian_nb/gaussian_nb.joblib +3 -0
- scikit-learn/gaussian_nb/gaussian_nb.onnx +3 -0
- scikit-learn/gaussian_nb/gaussian_nb.zip +3 -0
- scikit-learn/gaussian_nb/gaussian_nb/1/model.onnx +3 -0
- scikit-learn/gaussian_nb/gaussian_nb/config.pbtxt +18 -0
- scikit-learn/gaussian_nb/model_packaging.sh +1 -0
- scikit-learn/gaussian_nb/predict.py +31 -0
- scikit-learn/gaussian_nb/train.py +25 -0
scikit-learn/gaussian_nb/client.py
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python3
|
2 |
+
# -*- coding: utf-8 -*-
|
3 |
+
|
4 |
+
import sys
|
5 |
+
import argparse
|
6 |
+
import numpy as np
|
7 |
+
|
8 |
+
import tritonclient.grpc as grpcclient
|
9 |
+
|
10 |
+
from sklearn.datasets import load_iris
|
11 |
+
from sklearn.model_selection import train_test_split
|
12 |
+
from sklearn.metrics import accuracy_score
|
13 |
+
|
14 |
+
|
15 |
+
def make_prediction(model_server, model_name, model_version, verbose):
|
16 |
+
try:
|
17 |
+
triton_client = grpcclient.InferenceServerClient(url=model_server, verbose=verbose)
|
18 |
+
except Exception as e:
|
19 |
+
print("channel creation failed: " + str(e))
|
20 |
+
sys.exit(1)
|
21 |
+
# Infer
|
22 |
+
inputs = []
|
23 |
+
outputs = []
|
24 |
+
# Load the California Housing dataset
|
25 |
+
dataset = load_iris()
|
26 |
+
X, y = dataset.data, dataset.target
|
27 |
+
# Split the dataset into training and testing sets
|
28 |
+
_, X_test, _, y_test = train_test_split(X, y, test_size=0.25, random_state=0)
|
29 |
+
input_data = X_test.astype(np.float32)
|
30 |
+
input_label = y_test.astype(np.float32)
|
31 |
+
print(f'input_data:\n{input_data[0]}')
|
32 |
+
print(f'input_label:\n{input_label[0]}')
|
33 |
+
# input_data = np.expand_dims(input_data, axis=0)
|
34 |
+
# Initialize the data
|
35 |
+
inputs.append(grpcclient.InferInput('float_input', [input_data.shape[0], input_data.shape[1]], "FP32"))
|
36 |
+
inputs[0].set_data_from_numpy(input_data)
|
37 |
+
outputs.append(grpcclient.InferRequestedOutput('label'))
|
38 |
+
outputs.append(grpcclient.InferRequestedOutput('probabilities'))
|
39 |
+
# Test with outputs
|
40 |
+
results = triton_client.infer(model_name=model_name, inputs=inputs, outputs=outputs)
|
41 |
+
# print("response:\n", results.get_response())
|
42 |
+
statistics = triton_client.get_inference_statistics(model_name=model_name)
|
43 |
+
# print("statistics:\n", statistics)
|
44 |
+
if len(statistics.model_stats) != 1:
|
45 |
+
print("FAILED: Inference Statistics")
|
46 |
+
sys.exit(1)
|
47 |
+
# Get the output arrays from the results
|
48 |
+
y_pred = results.as_numpy('label').squeeze()
|
49 |
+
y_prob = results.as_numpy('probabilities').squeeze()
|
50 |
+
print(f"y_pred:\n{y_pred[0]}")
|
51 |
+
print(f"y_prob:\n{y_prob[0]}")
|
52 |
+
acc = accuracy_score(y_test, y_pred)
|
53 |
+
print(f'Accuracy classification score: {acc}')
|
54 |
+
|
55 |
+
|
56 |
+
"""
|
57 |
+
python client.py --model_server localhost:8001 --model_name gaussian_nb --model_version 1
|
58 |
+
"""
|
59 |
+
if __name__ == "__main__":
|
60 |
+
parser = argparse.ArgumentParser(description="Make predictions using a specific model.")
|
61 |
+
parser.add_argument("--model_server", default="localhost:8001", help="The address of the model server.")
|
62 |
+
parser.add_argument("--model_name", default="gaussian_nb", help="The name of the model to use.")
|
63 |
+
parser.add_argument("--model_version", default="1", help="The version of the model to use.")
|
64 |
+
parser.add_argument("--verbose", action="store_true", required=False, default=False, help='Enable verbose output')
|
65 |
+
args = parser.parse_args()
|
66 |
+
make_prediction(args.model_server, args.model_name, args.model_version, args.verbose)
|
scikit-learn/gaussian_nb/convert2onnx.sh
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
python ../convert2onnx.py iris gaussian_nb.joblib gaussian_nb.onnx
|
scikit-learn/gaussian_nb/gaussian_nb.joblib
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:8cbef1458f1ed6a1944b6a7dd4a04a8e593c5fa35467433bc85349b03989d66f
|
3 |
+
size 1008
|
scikit-learn/gaussian_nb/gaussian_nb.onnx
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:d88d1c02412905346c118fa7edcbf591b72cada0ece1a5a4769f331a327aa5f6
|
3 |
+
size 1823
|
scikit-learn/gaussian_nb/gaussian_nb.zip
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:d303f012ddaea579f253552b8d5fecd9654c7cb9c5cd58c0f2bafac2ed727adb
|
3 |
+
size 1424
|
scikit-learn/gaussian_nb/gaussian_nb/1/model.onnx
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:d88d1c02412905346c118fa7edcbf591b72cada0ece1a5a4769f331a327aa5f6
|
3 |
+
size 1823
|
scikit-learn/gaussian_nb/gaussian_nb/config.pbtxt
ADDED
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
name: "gaussian_nb"
|
2 |
+
backend: "onnxruntime"
|
3 |
+
max_batch_size: 0
|
4 |
+
input [
|
5 |
+
{
|
6 |
+
name: "float_input"
|
7 |
+
data_type: TYPE_FP32
|
8 |
+
dims: [ 4 ]
|
9 |
+
}
|
10 |
+
]
|
11 |
+
output [
|
12 |
+
]
|
13 |
+
instance_group [
|
14 |
+
{
|
15 |
+
count: 1
|
16 |
+
kind: KIND_CPU
|
17 |
+
}
|
18 |
+
]
|
scikit-learn/gaussian_nb/model_packaging.sh
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
python ../model_packaging.py .
|
scikit-learn/gaussian_nb/predict.py
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python3
|
2 |
+
# -*- coding: utf-8 -*-
|
3 |
+
|
4 |
+
import joblib
|
5 |
+
|
6 |
+
from sklearn.datasets import load_iris
|
7 |
+
from sklearn.model_selection import train_test_split
|
8 |
+
from sklearn.metrics import accuracy_score
|
9 |
+
|
10 |
+
# Load the model from disk
|
11 |
+
loaded_model = joblib.load('gaussian_nb.joblib')
|
12 |
+
|
13 |
+
# Set the random seed
|
14 |
+
random_seed = 0
|
15 |
+
|
16 |
+
# Load the dataset
|
17 |
+
dataset = load_iris()
|
18 |
+
X, y = dataset.data, dataset.target
|
19 |
+
|
20 |
+
# Split the dataset into training and testing sets
|
21 |
+
_, X_test, _, y_test = train_test_split(X, y, test_size=0.25, random_state=random_seed)
|
22 |
+
print(f'X_test:\n{X_test[0]}')
|
23 |
+
print(f'y_test:\n{y_test[0]}')
|
24 |
+
|
25 |
+
# Use the model to make predictions on the test data
|
26 |
+
y_pred = loaded_model.predict(X_test)
|
27 |
+
print(f'y_pred:\n{y_pred[0]}')
|
28 |
+
|
29 |
+
# Score the model using accuracy classification score
|
30 |
+
acc = accuracy_score(y_test, y_pred)
|
31 |
+
print(f'Accuracy classification score: {acc}')
|
scikit-learn/gaussian_nb/train.py
ADDED
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python3
|
2 |
+
# -*- coding: utf-8 -*-
|
3 |
+
|
4 |
+
import joblib
|
5 |
+
|
6 |
+
from sklearn.datasets import load_iris
|
7 |
+
from sklearn.model_selection import train_test_split
|
8 |
+
from sklearn.naive_bayes import GaussianNB
|
9 |
+
|
10 |
+
# Set the random seed
|
11 |
+
random_seed = 0
|
12 |
+
|
13 |
+
# Load the dataset
|
14 |
+
dataset = load_iris()
|
15 |
+
X, y = dataset.data, dataset.target
|
16 |
+
|
17 |
+
# Split the dataset into training and testing sets
|
18 |
+
X_train, _, y_train, _ = train_test_split(X, y, test_size=0.25, random_state=random_seed)
|
19 |
+
|
20 |
+
# Create and train model
|
21 |
+
model = GaussianNB()
|
22 |
+
model.fit(X_train, y_train)
|
23 |
+
|
24 |
+
# Save the model to disk
|
25 |
+
joblib.dump(model, 'gaussian_nb.joblib')
|