Skip to content

Commit

Permalink
run nbqa-black and nbqa-isort
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGorelli committed Jun 1, 2021
1 parent 6c506e7 commit a661972
Show file tree
Hide file tree
Showing 75 changed files with 5,134 additions and 2,682 deletions.
83 changes: 54 additions & 29 deletions components/drift-detection/cifar10/cifar10_drift.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@
"metadata": {},
"outputs": [],
"source": [
"#CLUSTER_IP=\"localhost:8004\""
"# CLUSTER_IP=\"localhost:8004\""
]
},
{
Expand All @@ -366,56 +366,79 @@
"metadata": {},
"outputs": [],
"source": [
"import json\n",
"\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"import requests\n",
"import json\n",
"import tensorflow as tf\n",
"\n",
"tf.keras.backend.clear_session()\n",
"\n",
"train, test = tf.keras.datasets.cifar10.load_data()\n",
"X_train, y_train = train\n",
"X_test, y_test = test\n",
"\n",
"X_train = X_train.astype('float32') / 255\n",
"X_test = X_test.astype('float32') / 255\n",
"X_train = X_train.astype(\"float32\") / 255\n",
"X_test = X_test.astype(\"float32\") / 255\n",
"print(X_train.shape, y_train.shape, X_test.shape, y_test.shape)\n",
"classes = ('plane', 'car', 'bird', 'cat',\n",
" 'deer', 'dog', 'frog', 'horse', 'ship', 'truck')\n",
"classes = (\n",
" \"plane\",\n",
" \"car\",\n",
" \"bird\",\n",
" \"cat\",\n",
" \"deer\",\n",
" \"dog\",\n",
" \"frog\",\n",
" \"horse\",\n",
" \"ship\",\n",
" \"truck\",\n",
")\n",
"\n",
"\n",
"def show(X):\n",
" plt.imshow(X.reshape(32, 32, 3))\n",
" plt.axis('off')\n",
" plt.axis(\"off\")\n",
" plt.show()\n",
"\n",
"\n",
"def predict(X):\n",
" formData = {\n",
" 'instances': X.tolist()\n",
" }\n",
" formData = {\"instances\": X.tolist()}\n",
" headers = {}\n",
" res = requests.post('http://'+CLUSTER_IP+'/seldon/cifar10drift/tfserving-cifar10/v1/models/resnet32/:predict', json=formData, headers=headers)\n",
" res = requests.post(\n",
" \"http://\"\n",
" + CLUSTER_IP\n",
" + \"/seldon/cifar10drift/tfserving-cifar10/v1/models/resnet32/:predict\",\n",
" json=formData,\n",
" headers=headers,\n",
" )\n",
" if res.status_code == 200:\n",
" j = res.json()\n",
" if len(j[\"predictions\"]) == 1:\n",
" return classes[np.array(j[\"predictions\"])[0].argmax()]\n",
" else:\n",
" print(\"Failed with \",res.status_code)\n",
" print(\"Failed with \", res.status_code)\n",
" return []\n",
" \n",
"\n",
"\n",
"def drift(X):\n",
" formData = {\n",
" 'instances': X.tolist()\n",
" }\n",
" formData = {\"instances\": X.tolist()}\n",
" headers = {}\n",
" headers = { \"ce-namespace\": \"default\",\"ce-modelid\":\"cifar10drift\",\"ce-type\":\"io.seldon.serving.inference.request\", \\\n",
" \"ce-id\":\"1234\",\"ce-source\":\"localhost\",\"ce-specversion\":\"1.0\"}\n",
" headers = {\n",
" \"ce-namespace\": \"default\",\n",
" \"ce-modelid\": \"cifar10drift\",\n",
" \"ce-type\": \"io.seldon.serving.inference.request\",\n",
" \"ce-id\": \"1234\",\n",
" \"ce-source\": \"localhost\",\n",
" \"ce-specversion\": \"1.0\",\n",
" }\n",
" headers[\"Host\"] = SERVICE_HOSTNAME_CD\n",
" res = requests.post('http://'+CLUSTER_IP+'/', json=formData, headers=headers)\n",
" res = requests.post(\"http://\" + CLUSTER_IP + \"/\", json=formData, headers=headers)\n",
" if res.status_code == 200:\n",
" od = res.json()\n",
" return od\n",
" else:\n",
" print(\"Failed with \",res.status_code)\n",
" print(\"Failed with \", res.status_code)\n",
" return []"
]
},
Expand All @@ -435,7 +458,7 @@
"outputs": [],
"source": [
"idx = 1\n",
"X = X_train[idx:idx+1]\n",
"X = X_train[idx : idx + 1]\n",
"show(X)\n",
"predict(X)"
]
Expand All @@ -460,7 +483,7 @@
"metadata": {},
"outputs": [],
"source": [
"!kubectl logs -n cifar10drift $(kubectl get pod -n cifar10drift -l app=hello-display -o jsonpath='{.items[0].metadata.name}') "
"!kubectl logs -n cifar10drift $(kubectl get pod -n cifar10drift -l app=hello-display -o jsonpath='{.items[0].metadata.name}')"
]
},
{
Expand All @@ -477,8 +500,9 @@
"outputs": [],
"source": [
"from tqdm import tqdm\n",
"for i in tqdm(range(0,5000,100)):\n",
" X = X_train[i:i+100]\n",
"\n",
"for i in tqdm(range(0, 5000, 100)):\n",
" X = X_train[i : i + 100]\n",
" predict(X)"
]
},
Expand Down Expand Up @@ -517,10 +541,11 @@
"metadata": {},
"outputs": [],
"source": [
"from alibi_detect.datasets import fetch_cifar10c, corruption_types_cifar10c\n",
"corruption = ['motion_blur']\n",
"from alibi_detect.datasets import corruption_types_cifar10c, fetch_cifar10c\n",
"\n",
"corruption = [\"motion_blur\"]\n",
"X_corr, y_corr = fetch_cifar10c(corruption=corruption, severity=5, return_X_y=True)\n",
"X_corr = X_corr.astype('float32') / 255"
"X_corr = X_corr.astype(\"float32\") / 255"
]
},
{
Expand All @@ -547,8 +572,8 @@
"metadata": {},
"outputs": [],
"source": [
"for i in tqdm(range(0,5000,100)):\n",
" X = X_corr[i:i+100]\n",
"for i in tqdm(range(0, 5000, 100)):\n",
" X = X_corr[i : i + 100]\n",
" predict(X)"
]
},
Expand Down
114 changes: 74 additions & 40 deletions components/drift-detection/nvidia-triton-cifar10/cifar10_drift.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@
"metadata": {},
"outputs": [],
"source": [
"#CLUSTER_IP=\"localhost:8004\""
"# CLUSTER_IP=\"localhost:8004\""
]
},
{
Expand All @@ -445,7 +445,7 @@
"metadata": {},
"outputs": [],
"source": [
"TOKEN=\"Bearer <token>\""
"TOKEN = \"Bearer <token>\""
]
},
{
Expand Down Expand Up @@ -481,73 +481,104 @@
}
],
"source": [
"import json\n",
"\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"import requests\n",
"import json\n",
"import tensorflow as tf\n",
"\n",
"tf.keras.backend.clear_session()\n",
"\n",
"train, test = tf.keras.datasets.cifar10.load_data()\n",
"X_train, y_train = train\n",
"X_test, y_test = test\n",
"\n",
"X_train = X_train.astype('float32') / 255\n",
"X_test = X_test.astype('float32') / 255\n",
"X_train = X_train.astype(\"float32\") / 255\n",
"X_test = X_test.astype(\"float32\") / 255\n",
"print(X_train.shape, y_train.shape, X_test.shape, y_test.shape)\n",
"classes = ('plane', 'car', 'bird', 'cat',\n",
" 'deer', 'dog', 'frog', 'horse', 'ship', 'truck')\n",
"classes = (\n",
" \"plane\",\n",
" \"car\",\n",
" \"bird\",\n",
" \"cat\",\n",
" \"deer\",\n",
" \"dog\",\n",
" \"frog\",\n",
" \"horse\",\n",
" \"ship\",\n",
" \"truck\",\n",
")\n",
"\n",
"\n",
"def show(X):\n",
" plt.imshow(X.reshape(32, 32, 3))\n",
" plt.axis('off')\n",
" plt.axis(\"off\")\n",
" plt.show()\n",
"\n",
"\n",
"def predict(X):\n",
" formData = {\n",
" 'inputs': [{\n",
" \"name\":\"input_1\",\n",
" \"datatype\": \"FP32\",\n",
" \"shape\": [X.shape[0], 32, 32, 3],\n",
" \"data\": X.flatten().tolist()\n",
" }]\n",
" \"inputs\": [\n",
" {\n",
" \"name\": \"input_1\",\n",
" \"datatype\": \"FP32\",\n",
" \"shape\": [X.shape[0], 32, 32, 3],\n",
" \"data\": X.flatten().tolist(),\n",
" }\n",
" ]\n",
" }\n",
" headers = {\n",
" \"Authorization\":\"Bearer \"+TOKEN,\n",
" \"X-Auth-Token\":TOKEN,\n",
" \"Content-Type\": \"application/json\"\n",
" }\n",
" res = requests.post('http://'+CLUSTER_IP+'/seldon/cifar10drift/triton-cifar10/v2/models/cifar10/infer', json=formData, headers=headers)\n",
" \"Authorization\": \"Bearer \" + TOKEN,\n",
" \"X-Auth-Token\": TOKEN,\n",
" \"Content-Type\": \"application/json\",\n",
" }\n",
" res = requests.post(\n",
" \"http://\"\n",
" + CLUSTER_IP\n",
" + \"/seldon/cifar10drift/triton-cifar10/v2/models/cifar10/infer\",\n",
" json=formData,\n",
" headers=headers,\n",
" )\n",
" if res.status_code == 200:\n",
" j = res.json()\n",
" y = np.array(j[\"outputs\"][0][\"data\"])\n",
" y.shape = tuple(j[\"outputs\"][0][\"shape\"])\n",
" return [classes[x.argmax()] for x in y]\n",
" else:\n",
" print(\"Failed with \",res.status_code)\n",
" print(\"Failed with \", res.status_code)\n",
" return []\n",
" \n",
"\n",
"\n",
"def drift(X):\n",
" formData = {\n",
" 'inputs': [{\n",
" \"name\":\"input_1\",\n",
" \"datatype\": \"FP32\",\n",
" \"shape\": [1, 32, 32, 3],\n",
" \"data\": X.flatten().tolist()\n",
" }]\n",
" \"inputs\": [\n",
" {\n",
" \"name\": \"input_1\",\n",
" \"datatype\": \"FP32\",\n",
" \"shape\": [1, 32, 32, 3],\n",
" \"data\": X.flatten().tolist(),\n",
" }\n",
" ]\n",
" }\n",
" headers = {}\n",
" headers = { \"ce-namespace\": \"default\",\"ce-modelid\":\"cifar10drift\",\"ce-type\":\"io.seldon.serving.inference.request\", \\\n",
" \"ce-id\":\"1234\",\"ce-source\":\"localhost\",\"ce-specversion\":\"1.0\"}\n",
" headers = {\n",
" \"ce-namespace\": \"default\",\n",
" \"ce-modelid\": \"cifar10drift\",\n",
" \"ce-type\": \"io.seldon.serving.inference.request\",\n",
" \"ce-id\": \"1234\",\n",
" \"ce-source\": \"localhost\",\n",
" \"ce-specversion\": \"1.0\",\n",
" }\n",
" headers[\"Host\"] = SERVICE_HOSTNAME_CD\n",
" headers[\"X-Auth-Token\"] = TOKEN\n",
" headers[\"Authorization\"] = \"Bearer \"+TOKEN\n",
" res = requests.post('http://'+CLUSTER_IP+'/', json=formData, headers=headers)\n",
" headers[\"Authorization\"] = \"Bearer \" + TOKEN\n",
" res = requests.post(\"http://\" + CLUSTER_IP + \"/\", json=formData, headers=headers)\n",
" if res.status_code == 200:\n",
" od = res.json()\n",
" return od\n",
" else:\n",
" print(\"Failed with \",res.status_code)\n",
" print(\"Failed with \", res.status_code)\n",
" return []"
]
},
Expand Down Expand Up @@ -590,7 +621,7 @@
],
"source": [
"idx = 1\n",
"X = X_train[idx:idx+1]\n",
"X = X_train[idx : idx + 1]\n",
"show(X)\n",
"predict(X)"
]
Expand Down Expand Up @@ -638,8 +669,9 @@
],
"source": [
"from tqdm.notebook import tqdm\n",
"for i in tqdm(range(1,5000,500)):\n",
" X = X_train[i:i+500]\n",
"\n",
"for i in tqdm(range(1, 5000, 500)):\n",
" X = X_train[i : i + 500]\n",
" predict(X)"
]
},
Expand Down Expand Up @@ -686,10 +718,11 @@
"metadata": {},
"outputs": [],
"source": [
"from alibi_detect.datasets import fetch_cifar10c, corruption_types_cifar10c\n",
"corruption = ['motion_blur']\n",
"from alibi_detect.datasets import corruption_types_cifar10c, fetch_cifar10c\n",
"\n",
"corruption = [\"motion_blur\"]\n",
"X_corr, y_corr = fetch_cifar10c(corruption=corruption, severity=5, return_X_y=True)\n",
"X_corr = X_corr.astype('float32') / 255"
"X_corr = X_corr.astype(\"float32\") / 255"
]
},
{
Expand Down Expand Up @@ -769,8 +802,9 @@
],
"source": [
"from tqdm.notebook import tqdm\n",
"for i in tqdm(range(0,5000,500)):\n",
" X = X_corr[i:i+500]\n",
"\n",
"for i in tqdm(range(0, 5000, 500)):\n",
" X = X_corr[i : i + 500]\n",
" predict(X)"
]
},
Expand Down
Loading

0 comments on commit a661972

Please sign in to comment.