finished blog post 1

This commit is contained in:
Abubakar Abid 2019-04-09 21:41:39 -07:00
parent c70235bca8
commit a6ece554f9
8 changed files with 226 additions and 296 deletions

152
Test Keras MNIST.ipynb Normal file
View File

@ -0,0 +1,152 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import tensorflow as tf\n",
"import gradio"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"(x_train, y_train),(x_test, y_test) = tf.keras.datasets.mnist.load_data()\n",
"x_train, x_test = x_train / 255.0, x_test / 255.0"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"model = tf.keras.models.Sequential([\n",
" tf.keras.layers.Flatten(),\n",
" tf.keras.layers.Dense(512, activation=tf.nn.relu),\n",
" tf.keras.layers.Dropout(0.2),\n",
" tf.keras.layers.Dense(10, activation=tf.nn.softmax)\n",
"])\n",
"\n",
"model.compile(optimizer='adam',\n",
" loss='sparse_categorical_crossentropy',\n",
" metrics=['accuracy'])"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch 1/1\n",
"60000/60000 [==============================] - 25s 417us/step - loss: 0.2210 - acc: 0.9351\n"
]
},
{
"data": {
"text/plain": [
"<tensorflow.python.keras.callbacks.History at 0x22d334d8b00>"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"model.fit(x_train, y_train, epochs=1)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"iface = gradio.Interface(inputs=\"sketchpad\", outputs=\"label\", model=model, model_type='keras')"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"No validation samples for this interface... skipping validation.\n",
"NOTE: Gradio is in beta stage, please report all bugs to: contact.gradio@gmail.com\n",
"Model is running locally at: http://localhost:7861/\n",
"To create a public link, set `share=True` in the argument to `launch()`\n"
]
},
{
"data": {
"text/html": [
"\n",
" <iframe\n",
" width=\"1000\"\n",
" height=\"500\"\n",
" src=\"http://localhost:7861/\"\n",
" frameborder=\"0\"\n",
" allowfullscreen\n",
" ></iframe>\n",
" "
],
"text/plain": [
"<IPython.lib.display.IFrame at 0x22d2cf1e710>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(<gradio.networking.serve_files_in_background.<locals>.HTTPServer at 0x22d348a7240>,\n",
" 'http://localhost:7861/',\n",
" None)"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"iface.launch(inline=True, share=False)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.6 (tensorflow)",
"language": "python",
"name": "tensorflow"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.7"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

View File

@ -43,290 +43,29 @@
"metadata": {},
"outputs": [],
"source": [
"from PIL import Image\n",
"import requests\n",
"from io import BytesIO\n",
"# from PIL import Image\n",
"# import requests\n",
"# from io import BytesIO\n",
"\n",
"url = 'https://nationalzoo.si.edu/sites/default/files/animals/cheetah-004.jpg'\n",
"# url = 'https://nationalzoo.si.edu/sites/default/files/animals/cheetah-004.jpg'\n",
"\n",
"response = requests.get(url)\n",
"img = Image.open(BytesIO(response.content))\n",
"# response = requests.get(url)\n",
"# img = Image.open(BytesIO(response.content))\n",
"\n",
"# resize the image into an array that the model can accept\n",
"img = np.array(img.resize((299, 299))).reshape((1, 299, 299, 3))\n",
"# # resize the image into an array that the model can accept\n",
"# img = np.array(img.resize((299, 299))).reshape((1, 299, 299, 3))\n",
"\n",
"# scale the image and do other preprocessing\n",
"img = img/255"
"# # scale the image and do other preprocessing\n",
"# img = img/255"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[2.87348394e-05, 3.31625670e-05, 2.48761153e-05, 2.91672295e-05,\n",
" 1.58476214e-05, 2.04349963e-05, 2.29794769e-05, 2.98258401e-05,\n",
" 7.79210823e-05, 1.14480768e-04, 4.18152449e-05, 5.98102342e-05,\n",
" 8.04319134e-05, 1.35788669e-05, 3.69409063e-05, 2.70363416e-05,\n",
" 4.31931585e-05, 2.60745201e-05, 5.94786252e-05, 4.99271664e-05,\n",
" 2.43047725e-05, 1.30793895e-04, 4.14417882e-05, 2.70148976e-05,\n",
" 4.10587854e-05, 6.58849458e-05, 3.18542261e-05, 2.28214885e-05,\n",
" 4.62773096e-05, 2.48329743e-05, 2.42654005e-05, 2.44440125e-05,\n",
" 4.15933137e-05, 2.09003556e-05, 1.59764550e-05, 1.34925976e-05,\n",
" 5.11476246e-05, 3.04547921e-05, 5.38403292e-05, 2.52995142e-05,\n",
" 2.60678062e-05, 1.34934933e-04, 5.17043627e-05, 1.98286180e-05,\n",
" 1.71139363e-05, 1.33584617e-04, 3.85650528e-05, 4.20200013e-05,\n",
" 4.11127767e-05, 4.81091338e-05, 2.95333430e-05, 1.35418804e-05,\n",
" 2.56965777e-05, 3.57028839e-05, 3.47831883e-05, 1.86437192e-05,\n",
" 2.68215954e-05, 3.62955325e-05, 3.25360379e-05, 2.10378585e-05,\n",
" 5.16176551e-05, 1.74321522e-05, 1.70013354e-05, 3.05602764e-04,\n",
" 2.08724887e-05, 4.20338292e-05, 3.24174725e-05, 5.40788969e-05,\n",
" 3.05910835e-05, 3.09062816e-05, 3.70780981e-05, 5.52072815e-05,\n",
" 5.70952288e-05, 9.20407110e-05, 7.28778541e-05, 7.14581111e-05,\n",
" 1.53447272e-05, 8.25692987e-05, 1.66468253e-05, 3.68028523e-05,\n",
" 3.37601887e-05, 6.55732729e-05, 7.85594093e-05, 1.06584033e-04,\n",
" 2.29202069e-05, 8.09206249e-05, 9.53404306e-05, 3.45181325e-05,\n",
" 4.02693950e-05, 4.56266353e-05, 2.36524411e-05, 3.45649423e-05,\n",
" 2.90476983e-05, 4.14484311e-05, 6.00201092e-05, 2.55004470e-05,\n",
" 2.67037540e-05, 4.19789467e-05, 1.82741569e-05, 3.19312676e-05,\n",
" 2.70542678e-05, 5.47103627e-05, 3.11060867e-05, 1.23055879e-05,\n",
" 2.46065429e-05, 1.89571019e-05, 6.07315133e-06, 2.28815934e-05,\n",
" 2.02425854e-05, 4.30371547e-05, 4.66785350e-05, 2.23741263e-05,\n",
" 5.11738763e-05, 1.96830188e-05, 6.12578588e-05, 3.12303055e-05,\n",
" 2.31823124e-05, 3.49025431e-05, 4.09512031e-05, 3.99867422e-05,\n",
" 2.12125287e-05, 4.49290274e-05, 4.11992005e-05, 2.76286501e-05,\n",
" 5.41073750e-05, 2.77385552e-05, 1.73709468e-05, 7.04166450e-05,\n",
" 4.19379321e-05, 1.01846208e-05, 5.60213484e-05, 1.86260368e-05,\n",
" 2.41570415e-05, 2.32356997e-05, 3.61526509e-05, 3.48804824e-05,\n",
" 3.82888429e-05, 1.91788040e-05, 2.66903371e-05, 2.22696453e-05,\n",
" 8.82827590e-05, 5.88142175e-05, 4.24889695e-05, 1.39571493e-05,\n",
" 2.34105919e-05, 9.14792054e-06, 2.38255616e-05, 2.36884007e-05,\n",
" 4.16137882e-05, 2.61921778e-05, 9.35538628e-06, 4.06456202e-05,\n",
" 5.00514470e-05, 5.93220975e-05, 4.06428662e-05, 4.57445349e-05,\n",
" 9.33669071e-05, 6.87131251e-05, 6.56917036e-05, 1.63057150e-04,\n",
" 2.07711382e-05, 3.40171828e-05, 5.25352189e-05, 7.87169774e-05,\n",
" 2.05707147e-05, 2.78320695e-05, 6.96952338e-05, 5.21129659e-05,\n",
" 7.22707264e-05, 1.92875741e-05, 2.23326842e-05, 1.34510556e-04,\n",
" 2.18944042e-05, 2.92327531e-05, 3.70465386e-05, 5.92614815e-05,\n",
" 1.72934742e-05, 1.91587696e-05, 2.92312779e-05, 2.92381355e-05,\n",
" 3.74670744e-05, 1.88971571e-05, 8.49900607e-05, 4.99997332e-05,\n",
" 1.66820264e-05, 7.92418869e-05, 7.79421171e-05, 3.04292171e-05,\n",
" 1.57136732e-04, 7.85228622e-05, 7.21035758e-05, 1.09024957e-04,\n",
" 4.36982591e-05, 2.41748094e-05, 6.00040876e-05, 2.53325179e-05,\n",
" 1.18568069e-05, 2.16146182e-05, 1.14633331e-05, 2.29929574e-05,\n",
" 4.85876917e-05, 2.81516332e-05, 5.11928120e-05, 2.46606432e-05,\n",
" 2.56997391e-05, 3.24740686e-05, 6.63395913e-05, 5.39563443e-05,\n",
" 7.46768637e-05, 1.44902097e-05, 6.21178260e-05, 7.94990920e-05,\n",
" 5.26915173e-05, 2.44574185e-05, 4.84968186e-05, 5.58190695e-05,\n",
" 6.10985153e-05, 3.27518101e-05, 4.99396410e-05, 4.66030106e-05,\n",
" 4.85124510e-05, 4.44747529e-05, 8.11594291e-05, 5.17867738e-05,\n",
" 3.27908747e-05, 5.72784738e-05, 3.77279248e-05, 2.79495016e-05,\n",
" 1.14093185e-04, 3.22306660e-05, 6.37761405e-05, 4.70271916e-05,\n",
" 5.12053120e-05, 3.55333796e-05, 4.20222823e-05, 7.67037345e-05,\n",
" 6.28583803e-05, 5.06723591e-05, 1.65470137e-05, 8.55832404e-05,\n",
" 9.10326271e-05, 4.44190991e-05, 2.66823718e-05, 4.90651146e-05,\n",
" 3.16314836e-05, 1.70037019e-05, 2.22507952e-05, 3.08026210e-05,\n",
" 2.92006262e-05, 5.21812508e-05, 2.88916035e-05, 3.42814019e-04,\n",
" 3.08394483e-05, 3.21107982e-05, 5.83832407e-05, 3.17412378e-05,\n",
" 4.86168028e-05, 3.69986228e-05, 7.35939539e-05, 1.01651509e-04,\n",
" 6.33892123e-05, 1.16707757e-04, 4.40528347e-05, 7.76190718e-05,\n",
" 4.07124644e-05, 3.60297745e-05, 3.41399682e-05, 3.80674792e-05,\n",
" 2.62926351e-05, 6.99426528e-05, 4.08917695e-05, 6.30973009e-05,\n",
" 1.36613016e-04, 6.11745490e-05, 4.07748266e-05, 4.84085140e-05,\n",
" 3.05555557e-04, 1.16628311e-04, 6.46469707e-05, 8.43134148e-06,\n",
" 5.46161064e-05, 2.23031002e-05, 6.53090974e-05, 2.36686647e-05,\n",
" 1.82996901e-05, 1.40099131e-04, 5.82382090e-05, 4.51421191e-04,\n",
" 2.13040854e-03, 4.35033289e-04, 5.41022397e-04, 1.60870492e-04,\n",
" 4.68313883e-05, 9.54939544e-01, 3.10988798e-05, 3.05654467e-05,\n",
" 3.23688837e-05, 2.17403522e-05, 3.35242439e-05, 5.57901185e-05,\n",
" 3.60432459e-05, 2.85484166e-05, 3.54308868e-05, 2.04342177e-05,\n",
" 5.79034349e-05, 8.77326820e-05, 5.87764771e-05, 7.48893217e-05,\n",
" 3.75710188e-05, 6.65013722e-05, 3.93841146e-05, 4.30815053e-05,\n",
" 3.90250025e-05, 2.49465302e-05, 6.32623924e-05, 2.82751062e-05,\n",
" 4.39944779e-05, 2.91896849e-05, 6.09816307e-05, 3.73154508e-05,\n",
" 2.32816583e-05, 3.47097885e-05, 2.34287490e-05, 3.12690463e-05,\n",
" 3.78116711e-05, 2.44250441e-05, 5.76958992e-05, 1.89158709e-05,\n",
" 3.32920463e-05, 7.67280217e-05, 3.95814968e-05, 1.52310495e-05,\n",
" 2.03246800e-05, 2.73730711e-05, 1.54649297e-05, 4.58700415e-05,\n",
" 9.81626581e-05, 1.53429410e-05, 1.71078354e-05, 2.43297018e-05,\n",
" 7.51380576e-05, 1.96585552e-05, 1.77083912e-05, 3.09253592e-05,\n",
" 2.97734059e-05, 2.21605824e-05, 2.90767166e-05, 1.85748777e-05,\n",
" 4.97450383e-05, 1.88307531e-05, 1.19116285e-05, 7.33295456e-05,\n",
" 6.87958745e-05, 3.93158378e-04, 2.31156846e-05, 2.53631715e-05,\n",
" 1.65625333e-05, 1.65534693e-05, 4.20270917e-05, 3.63402833e-05,\n",
" 4.49393556e-05, 2.52205973e-05, 3.12724478e-05, 2.30503265e-05,\n",
" 2.50303183e-05, 7.02027774e-06, 3.39574144e-05, 2.14463907e-05,\n",
" 6.73558243e-05, 2.93099947e-05, 4.04911734e-05, 1.33556852e-04,\n",
" 3.94189883e-05, 9.10259332e-05, 3.55042503e-05, 1.96132933e-05,\n",
" 2.23557199e-05, 1.78663686e-05, 2.76167684e-05, 2.52928312e-05,\n",
" 2.53802773e-05, 1.75146706e-05, 3.04427449e-05, 3.05658868e-05,\n",
" 2.93310241e-05, 3.47113091e-05, 3.17042395e-05, 4.88488004e-05,\n",
" 5.35136824e-05, 1.63490895e-05, 3.07410519e-05, 2.58094551e-05,\n",
" 1.68334973e-05, 2.69737084e-05, 1.76451395e-05, 8.72271194e-05,\n",
" 4.76380628e-05, 6.58142962e-05, 4.99944399e-05, 3.69260088e-05,\n",
" 1.29387572e-05, 4.35098846e-05, 3.55075354e-05, 5.56325867e-05,\n",
" 3.39463732e-05, 4.18462005e-05, 2.19590602e-05, 2.19191188e-05,\n",
" 1.31938330e-04, 4.50956250e-05, 5.07826589e-05, 4.17550691e-05,\n",
" 7.16164577e-05, 3.72835784e-05, 2.07126468e-05, 4.95999884e-05,\n",
" 2.19804733e-05, 3.38278987e-05, 3.40998122e-05, 2.76599694e-05,\n",
" 5.85454873e-05, 2.18334353e-05, 3.34332472e-05, 4.01523976e-05,\n",
" 4.58525028e-05, 3.80572783e-05, 1.40437096e-05, 2.83117733e-05,\n",
" 4.42011333e-05, 1.78016035e-05, 1.94806598e-05, 1.90080063e-05,\n",
" 6.83374310e-05, 3.57353092e-05, 2.33811606e-05, 2.66535317e-05,\n",
" 5.60822373e-05, 2.42737551e-05, 2.61371079e-05, 2.67774813e-05,\n",
" 1.90776718e-05, 1.29542277e-05, 2.81340526e-05, 1.97947316e-04,\n",
" 4.21368532e-05, 2.00126105e-05, 3.13872697e-05, 5.18013985e-05,\n",
" 4.06897962e-05, 5.17138833e-05, 1.88349750e-05, 4.25781291e-05,\n",
" 2.22635099e-05, 2.73323512e-05, 3.70964553e-05, 1.27892481e-05,\n",
" 2.74279973e-05, 9.07500435e-05, 4.62964235e-05, 2.69962766e-05,\n",
" 2.29480265e-05, 3.62087230e-05, 9.92782880e-05, 7.58480746e-05,\n",
" 2.73580899e-05, 3.78854857e-05, 1.52056227e-05, 4.57414790e-05,\n",
" 2.05655360e-05, 3.76061143e-05, 4.05851133e-05, 4.74572698e-05,\n",
" 5.02792682e-05, 2.73430123e-05, 1.88959839e-05, 3.63449617e-05,\n",
" 8.58449785e-05, 4.83370568e-05, 2.29676662e-05, 1.39783297e-05,\n",
" 3.00153624e-05, 9.30200713e-06, 2.98171090e-05, 4.02061924e-05,\n",
" 4.37480085e-05, 2.40602913e-05, 3.97164113e-05, 3.02438275e-05,\n",
" 3.59209807e-05, 2.26508218e-05, 8.11787104e-05, 4.59786570e-05,\n",
" 3.35251389e-05, 4.65675585e-05, 7.03359765e-05, 3.02757162e-05,\n",
" 1.55114576e-05, 2.08013098e-05, 3.21109837e-05, 3.39043763e-05,\n",
" 3.69621230e-05, 4.77702197e-05, 2.04758471e-05, 4.22765661e-05,\n",
" 1.94308050e-05, 7.30149040e-05, 1.79527942e-05, 2.80337772e-05,\n",
" 4.05040737e-05, 3.21173502e-05, 3.44566943e-05, 4.53288958e-05,\n",
" 1.00638936e-05, 9.34529671e-05, 3.09277821e-05, 2.75657203e-05,\n",
" 7.13371846e-05, 4.78991387e-05, 1.97249592e-05, 2.87434432e-05,\n",
" 3.84232007e-05, 3.48397916e-05, 6.53180105e-05, 4.45334517e-05,\n",
" 4.27828672e-05, 2.16301651e-05, 6.91576715e-05, 7.24355050e-05,\n",
" 1.77990405e-05, 2.19066005e-05, 4.36122064e-05, 1.95597968e-05,\n",
" 1.43805864e-05, 3.36158046e-05, 3.29781979e-05, 2.36990436e-05,\n",
" 3.80291931e-05, 2.94673118e-05, 5.39072244e-05, 1.34538832e-05,\n",
" 4.52095228e-05, 2.56473413e-05, 4.88241676e-05, 3.79534722e-05,\n",
" 9.00277664e-05, 2.71920580e-05, 1.99786864e-05, 4.11888686e-05,\n",
" 2.08715737e-05, 3.21811240e-05, 3.77245779e-05, 3.90776258e-05,\n",
" 5.72720819e-05, 2.28592144e-05, 8.38642154e-05, 7.11378598e-05,\n",
" 7.47661543e-05, 2.76085266e-05, 2.37599397e-05, 2.74038648e-05,\n",
" 1.96829624e-05, 2.75633538e-05, 1.81687465e-05, 1.65650599e-05,\n",
" 1.66132322e-05, 1.77653765e-05, 1.31694123e-05, 4.40269687e-05,\n",
" 5.24848438e-05, 7.23133689e-06, 6.08247465e-05, 4.91629107e-05,\n",
" 2.79622727e-05, 3.83454862e-05, 3.14143108e-05, 7.62736527e-05,\n",
" 7.78079848e-05, 3.67796965e-05, 2.58853524e-05, 2.09938262e-05,\n",
" 6.02522668e-05, 2.05882207e-05, 5.41717600e-05, 2.36419546e-05,\n",
" 2.47464232e-05, 2.56587409e-05, 4.29635875e-05, 2.33233714e-05,\n",
" 2.72860962e-05, 1.83634984e-05, 3.15190737e-05, 3.37215424e-05,\n",
" 4.02502737e-05, 4.96676294e-05, 3.87466462e-05, 4.78446273e-05,\n",
" 1.86023553e-05, 1.04718667e-04, 3.17602207e-05, 9.92937275e-05,\n",
" 2.01554867e-04, 3.02287735e-05, 3.60458944e-05, 2.53110029e-05,\n",
" 2.71016797e-05, 4.33302957e-05, 1.72166110e-05, 2.25411804e-05,\n",
" 2.48068172e-05, 5.14635103e-05, 1.58837247e-05, 1.93799478e-05,\n",
" 1.45588992e-05, 4.06311265e-05, 9.53586550e-06, 4.33395144e-05,\n",
" 6.12365402e-05, 4.95142558e-05, 5.42290290e-05, 3.97067161e-05,\n",
" 9.20145976e-06, 5.19714195e-05, 5.91691532e-05, 7.11168977e-05,\n",
" 1.74283286e-05, 1.11089117e-04, 3.03591587e-05, 6.13862794e-05,\n",
" 4.03964805e-05, 3.48730646e-05, 4.12873851e-05, 3.63344952e-05,\n",
" 9.57763186e-05, 5.08481317e-05, 2.18448913e-05, 2.55160630e-05,\n",
" 2.60871548e-05, 2.94701222e-05, 3.19013780e-05, 4.27702980e-05,\n",
" 3.36178891e-05, 5.91083517e-05, 4.76461501e-05, 2.87710882e-05,\n",
" 7.71013802e-05, 3.33449207e-05, 3.10998585e-05, 2.31554441e-05,\n",
" 7.88360558e-05, 4.88579790e-05, 4.79332739e-05, 6.66515261e-05,\n",
" 2.80267468e-05, 3.95161696e-05, 2.19156900e-05, 4.22459379e-05,\n",
" 2.90575063e-05, 3.46283523e-05, 4.19461721e-05, 4.79287955e-05,\n",
" 4.88870210e-05, 6.30793729e-05, 6.10515781e-05, 7.35698122e-05,\n",
" 4.14108945e-05, 2.09264635e-05, 2.75761595e-05, 2.45826413e-05,\n",
" 1.17834075e-04, 2.33378105e-05, 2.12311697e-05, 2.49118675e-05,\n",
" 1.44527812e-05, 7.47653685e-05, 2.85598799e-05, 4.30836189e-06,\n",
" 4.16856419e-05, 6.70859372e-05, 2.27318233e-05, 2.34566724e-05,\n",
" 2.86041468e-05, 3.12322700e-05, 4.20835640e-05, 2.88782139e-05,\n",
" 2.12479808e-05, 2.67499399e-05, 4.26801307e-05, 3.50373411e-05,\n",
" 1.14289542e-04, 2.39087785e-05, 2.87024377e-05, 5.50144468e-05,\n",
" 1.25675524e-05, 8.60681976e-05, 5.79822372e-05, 2.25609238e-05,\n",
" 1.58922521e-05, 5.59906366e-05, 5.47513882e-05, 3.99525888e-05,\n",
" 4.46637750e-05, 3.12694647e-05, 4.69786464e-05, 7.30282045e-05,\n",
" 3.32598356e-05, 3.93198643e-05, 2.98816431e-05, 4.68274120e-05,\n",
" 3.85413005e-05, 2.63213096e-05, 7.29164458e-05, 1.51059212e-05,\n",
" 1.73022017e-05, 2.24817995e-05, 2.50870999e-05, 2.70464498e-05,\n",
" 6.94527189e-05, 7.03223559e-05, 1.13023976e-04, 1.81350424e-05,\n",
" 1.61756379e-05, 2.27133587e-05, 1.73909539e-05, 3.29188697e-05,\n",
" 3.83688603e-05, 4.63621691e-05, 4.20097022e-05, 2.44141211e-05,\n",
" 2.36812193e-05, 5.37081723e-05, 3.25651235e-05, 6.26961337e-05,\n",
" 4.40348194e-05, 6.08678674e-05, 1.51382401e-05, 4.85360542e-05,\n",
" 2.52648915e-05, 2.13416624e-05, 3.66176173e-05, 2.14315878e-05,\n",
" 2.53550206e-05, 2.49690220e-05, 1.72549426e-05, 1.23161544e-05,\n",
" 4.05697538e-05, 2.80514014e-05, 4.10169851e-05, 1.82738422e-05,\n",
" 2.12066843e-05, 1.92876123e-05, 1.40940101e-05, 2.85765000e-05,\n",
" 1.49180614e-05, 2.17154247e-05, 1.15241521e-04, 6.20267747e-05,\n",
" 3.51752824e-05, 1.84352139e-05, 2.34445524e-05, 6.70253139e-05,\n",
" 3.65042324e-05, 3.79433368e-05, 4.84678712e-05, 3.16102814e-05,\n",
" 4.14562965e-05, 3.48020985e-05, 5.51545527e-05, 1.20085324e-05,\n",
" 4.02397964e-05, 3.69577174e-05, 1.16221108e-05, 1.73307726e-05,\n",
" 2.36364995e-05, 3.68570509e-05, 2.28376211e-05, 1.44234800e-05,\n",
" 4.29613319e-05, 2.90063417e-05, 3.12782940e-05, 3.48059839e-05,\n",
" 3.07464470e-05, 4.45889127e-05, 2.77584841e-05, 2.82693636e-05,\n",
" 3.06526708e-05, 2.07262001e-05, 2.39080709e-05, 3.90869063e-05,\n",
" 1.63033437e-05, 3.13781420e-05, 1.09535986e-05, 2.71046247e-05,\n",
" 1.11349189e-04, 5.54282742e-05, 1.40334141e-05, 4.46840531e-05,\n",
" 3.27371636e-05, 2.36247342e-05, 4.71842868e-05, 2.51329329e-05,\n",
" 1.72788041e-05, 4.25959151e-05, 3.08176059e-05, 2.34919771e-05,\n",
" 5.41002009e-05, 3.36215126e-05, 1.79904982e-05, 2.53810031e-05,\n",
" 2.60098714e-05, 3.03672950e-05, 3.66435743e-05, 1.58947860e-05,\n",
" 1.63827226e-05, 1.00799487e-04, 7.84313306e-05, 3.44231594e-05,\n",
" 2.11487786e-05, 2.59042172e-05, 2.38122284e-05, 4.10612520e-05,\n",
" 7.02113830e-05, 5.71030141e-05, 3.37046913e-05, 5.70804186e-05,\n",
" 4.23736856e-05, 5.22688570e-05, 1.19306824e-05, 5.73656653e-05,\n",
" 7.22504701e-05, 3.07254595e-05, 1.82885069e-05, 3.95821407e-05,\n",
" 3.25709625e-05, 3.28924471e-05, 9.97101160e-05, 2.37495660e-05,\n",
" 4.08896231e-05, 5.16752771e-05, 2.26338507e-05, 3.62301726e-05,\n",
" 3.16428268e-05, 3.80293750e-05, 2.15715372e-05, 4.88352052e-05,\n",
" 5.38927270e-05, 1.52157181e-05, 3.10339638e-05, 7.30824031e-05,\n",
" 5.28455093e-05, 7.30705578e-05, 5.31096957e-05, 2.09516438e-05,\n",
" 2.92397508e-05, 1.90421888e-05, 2.24708656e-05, 5.41521295e-05,\n",
" 4.26290353e-05, 2.68298045e-05, 1.68493905e-04, 7.92833162e-05,\n",
" 2.62088943e-05, 3.02322060e-05, 4.04044986e-05, 3.22642190e-05,\n",
" 3.49984402e-05, 6.04081906e-05, 3.70786656e-05, 3.49358452e-05,\n",
" 5.57010717e-05, 6.00058593e-05, 2.44417952e-05, 5.71263263e-05,\n",
" 3.27215894e-05, 3.40137776e-05, 1.18062126e-05, 6.24499153e-05,\n",
" 6.80528974e-05, 5.41649897e-05, 2.79301566e-05, 6.44374522e-05,\n",
" 2.42756778e-05, 1.69049254e-05, 3.07581759e-05, 3.14246172e-05,\n",
" 4.06918080e-05, 1.29262517e-05, 3.18938037e-05, 2.57563679e-05,\n",
" 4.66018973e-05, 5.63653448e-05, 3.38337704e-05, 2.77759864e-05,\n",
" 2.10913349e-05, 2.39067704e-05, 5.60720728e-05, 3.24895882e-05,\n",
" 3.95797579e-05, 8.02239229e-05, 1.01568221e-05, 2.71663976e-05,\n",
" 4.02033183e-05, 3.46393863e-05, 1.89300026e-05, 6.62385719e-05,\n",
" 2.61362366e-05, 1.68800689e-05, 4.81760289e-05, 2.70115488e-05,\n",
" 6.33779127e-05, 9.80817131e-05, 1.00358353e-04, 1.55335729e-05,\n",
" 6.72744281e-05, 2.67282903e-05, 5.28051060e-05, 2.69585871e-05,\n",
" 4.51873238e-05, 1.50139886e-05, 4.31409971e-05, 2.19486756e-05,\n",
" 2.87472831e-05, 2.18212153e-05, 5.95341808e-05, 4.36046794e-05,\n",
" 2.39972505e-05, 2.03701456e-05, 2.67976375e-05, 1.95541270e-05,\n",
" 4.66474739e-05, 3.60696904e-05, 1.68149654e-05, 2.32476013e-05,\n",
" 2.53057151e-05, 3.79400080e-05, 4.50154475e-05, 4.20643009e-05,\n",
" 2.56692183e-05, 3.46892048e-05, 3.57301287e-05, 3.69169247e-05,\n",
" 3.51629387e-05, 3.76432145e-05, 2.19416434e-05, 2.46851632e-05,\n",
" 6.47292763e-05, 4.90587045e-05, 9.84386497e-05, 4.41858792e-05,\n",
" 1.67457674e-05, 7.27501538e-05, 1.29627551e-05, 4.09351560e-05,\n",
" 4.05014071e-05, 5.26341646e-05, 3.61480306e-05, 4.60584415e-05,\n",
" 6.60331425e-05, 2.30091900e-05, 4.62839744e-05, 2.25598706e-05,\n",
" 3.43657230e-05, 4.64396071e-05, 4.21469849e-05, 4.19451717e-05,\n",
" 1.95988596e-05, 2.81598495e-05, 2.10212929e-05, 2.90198714e-05,\n",
" 3.15838552e-05, 1.42506296e-05, 1.96369892e-05, 1.41922828e-05,\n",
" 2.51494548e-05, 5.29627650e-05, 2.34803319e-05, 2.45093706e-05,\n",
" 5.32276354e-05, 3.60567574e-05, 3.34151773e-05, 4.46611339e-05,\n",
" 1.84819692e-05, 3.09412899e-05, 4.80864292e-05, 4.70178165e-05,\n",
" 7.63339776e-05, 4.71588719e-05, 4.23062920e-05, 4.86267672e-05,\n",
" 2.40010959e-05, 1.92821499e-05, 1.88615959e-05, 5.05874268e-05,\n",
" 4.82907526e-05, 3.97411168e-05, 5.72696772e-05, 2.60267680e-05,\n",
" 5.70021366e-05, 1.14346831e-05, 5.19260393e-05, 6.01843822e-05]],\n",
" dtype=float32)"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"model.predict(img)"
"# model.predict(img)"
]
},
{
@ -343,10 +82,10 @@
"outputs": [],
"source": [
"inp = gradio.inputs.ImageUpload()\n",
"out = gradio.outputs.Label(label_names='imagenet1000')\n",
"out = gradio.outputs.Label(label_names='imagenet1000', max_label_length=8)\n",
"\n",
"io = gradio.Interface(inputs=inp, \n",
" outputs=\"label\",\n",
" outputs=out,\n",
" model=model, \n",
" model_type='keras')"
]
@ -362,9 +101,9 @@
"name": "stdout",
"output_type": "stream",
"text": [
"NOTE: Gradio is in beta stage, please report all bugs to: a12d@stanford.edu\n",
"NOTE: Gradio is in beta stage, please report all bugs to: contact.gradio@gmail.com\n",
"Model is running locally at: http://localhost:7860/\n",
"Model available publicly for 8 hours at: https://d2f02315.gradio.app/\n"
"Model available publicly for 8 hours at: https://2140c179.gradio.app/\n"
]
},
{
@ -381,7 +120,7 @@
" "
],
"text/plain": [
"<IPython.lib.display.IFrame at 0x2837759ea90>"
"<IPython.lib.display.IFrame at 0x1dc825539e8>"
]
},
"metadata": {},

View File

@ -5,6 +5,10 @@
<link href="../style/style.css" rel="stylesheet">
<link href="../style/blog.css" rel="stylesheet">
<link href="../style/gradio.css" rel="stylesheet">
<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.15.6/styles/github.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.15.6/highlight.min.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
</head>
<body>
<nav>
@ -18,9 +22,9 @@
<div class="row">
<div class="leftcolumn">
<div class="card">
<h2>Beginner's Tutorial to Gradio: Creating a Sketchpad for a Keras MNIST Model</h2>
<h2>Beginner's Tutorial: Creating a Sketchpad for a Keras MNIST Model</h2>
<h4>Abubakar Abid, April 9, 2019</h4>
<div class="fakeimg" style="height:200px;">Image</div>
<img src="../img/mnist-sketchpad-screenshot.png"></img>
<p>Gradio is a python library that makes it easy to turn your machine learning models into visual interfaces!
This tutorial shows you how to do that with the "Hello World" of machine learning models: a model that we train
from scratch to classify hand-written digits on the MNIST dataset. By the end, you will create an interface
@ -29,24 +33,54 @@
<a href="https://colab.research.google.com/drive/1DQSuxGARUZ-v4ZOAuw-Hf-8zqegpmes-">Check out the colab notebook here.</a></p>
<h3>Installing Gradio</h3>
<p>If you haven't already installed gradio, go ahead and do so. It's super easy as long as you have Python3 already on your machine:</p>
<pre><code class="bash">pip install gradio</code></pre>
<h3>The MNIST Dataset</h3>
<p>The MNIST dataset consists of images of handwritten digits. We'll be training a model to classify the image
into the digit written, from 0 through 9. Here is a sample of handwritten digits.</p>
into the digit written, from 0 through 9, so let's load the data.
<pre><code class="python">import tensorflow as tf
(x_train, y_train),(x_test, y_test) = tf.keras.datasets.mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0</code></pre>
Here is a sample of handwritten digits.</p>
<img src="../img/mnist-examples.jpg"></img>
<h3>Training a Keras Model</h3>
<p>By using the keras API from the tensorflow package, we can train a model in just a few lines of code. Here,
we're not going to train a very complicated model -- it'll just be a fully connected neural network. Since we're
not really going for record accuracies, let's just train it only for 5 epochs.</p>
<pre><code class="python">model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(512, activation=tf.nn.relu),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10, activation=tf.nn.softmax)
])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.fit(x_train, y_train, epochs=1)</code></pre>
<h3>Launching a Gradio Interface</h3>
<p>Now that we have our keras model trained, we'll want to actually define the interface. What's the appropriate
interface to use? For the input, we can use a sketchpad, so that users can use their cursor to create new </p>
interface to use? For the input, we can use a Sketchpad, so that users can use their cursor to create new digits and
test the model (we call this process <em>interactive inference</em>). The output of the model is simply a label,
so we will use the Label interface. </p>
<pre><code class="python">io = gradio.Interface(
inputs="sketchpad",
outputs="label",
model=model,
model_type='keras')
io.launch(inline=True, share=False)
</code></pre>
And that's it. Try it out <a href="https://colab.research.google.com/drive/1DQSuxGARUZ-v4ZOAuw-Hf-8zqegpmes-">in the colab notebook here.</a>
You'll notice that the interface is embedded directly in the colab notebook!</p>
</div>
<!--<div class="card">-->
<!--<h2>TITLE HEADING</h2>-->
<!--<h5>Title description, Sep 2, 2017</h5>-->
<!--<div class="fakeimg" style="height:200px;">Image</div>-->
<!--<p>Some text..</p>-->
<!--</div>-->
<!--</div>-->
</div>
</div>
<footer>

View File

@ -18,22 +18,22 @@
<div class="row">
<div class="leftcolumn">
<div class="card">
<h2><a href="articles/beginners-tutorial-gradio-mnist-keras.html">Beginner's Tutorial to Gradio: Creating a Sketchpad for a Keras MNIST Model</a></h2>
<h2><a href="articles/beginners-tutorial-gradio-mnist-keras.html">Beginner's Tutorial: Creating a Sketchpad for a Keras MNIST Model</a></h2>
<h4>Abubakar Abid, April 9, 2019</h4>
<div class="fakeimg" style="height:200px;">Image</div>
<img src="img/mnist-sketchpad-screenshot.png"></img>
<p>Gradio is a python library that makes it easy to turn your machine learning models into visual interfaces!
This tutorial shows you how to do that with the "Hello World" of machine learning models: a model that we train
from scratch to classify hand-written digits on the MNIST dataset. By the end, you will create an interface
that allows you to draw handwritten digits and see the results of the classifier.
that allows you to draw handwritten digits and see the results of the classifier in your browser.
<a href="articles/beginners-tutorial-gradio-mnist-keras.html">Read more >>.</a></p>
</div>
<div class="card">
<h2>Why Machine Learning Accessibility is Important</h2>
<h2>Gradio: A Way to Improve Machine Learning Accessibility</h2>
<h4>Gradio Team, April 8, 2019</h4>
<div class="fakeimg" style="height:200px;">Image</div>
<img src="img/biased-google-translate.png"></img>
<p>Why did we create gradio? In this article, we describe the problems we have experienced with machine learning
models and why we think gradio can help solve them.
<a href="articles/beginners-tutorial-gradio-mnist-keras.html">Read more >>.</a></p>
models, in particular that of <em>accessibility</em>, and why we think gradio can help solve them.
<a href="articles/beginners-tutorial-gradio-mnist-keras.html">Coming soon.</a></p>
</div> </div>
</div>
<footer>

Binary file not shown.

After

Width:  |  Height:  |  Size: 56 KiB

BIN
web/img/mnist-examples.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 227 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 72 KiB

View File

@ -1,6 +1,11 @@
.leftcolumn {
width: 75%;
}
.leftcolumn img{
width: 80%;
}
p a{
color: #ed9013;
font-weight: bold;