Unverified Commit abc1c4a7 authored by Mark Daoust's avatar Mark Daoust Committed by GitHub
Browse files

Merge pull request #5100 from zanarmstrong/master

Adding illustrative charts to basic classification tutorial
parents 909ee1b3 8cbdb869
......@@ -5,8 +5,6 @@
"colab": {
"name": "basic_classification.ipynb",
"version": "0.3.2",
"views": {},
"default_view": {},
"provenance": [],
"private_outputs": true,
"collapsed_sections": [],
......@@ -32,12 +30,7 @@
"metadata": {
"id": "_ckMIh7O7s6D",
"colab_type": "code",
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
}
},
"colab": {},
"cellView": "form"
},
"cell_type": "code",
......@@ -61,12 +54,7 @@
"metadata": {
"id": "vasWnqRgy1H4",
"colab_type": "code",
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
}
},
"colab": {},
"cellView": "form"
},
"cell_type": "code",
......@@ -142,12 +130,7 @@
"metadata": {
"id": "dzLKpmZICaWN",
"colab_type": "code",
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
"colab": {}
},
"cell_type": "code",
"source": [
......@@ -204,12 +187,7 @@
"metadata": {
"id": "7MqDQO0KCaWS",
"colab_type": "code",
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
"colab": {}
},
"cell_type": "code",
"source": [
......@@ -288,12 +266,7 @@
"metadata": {
"id": "IjnLH5S2CaWx",
"colab_type": "code",
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
"colab": {}
},
"cell_type": "code",
"source": [
......@@ -319,12 +292,7 @@
"metadata": {
"id": "zW5k_xz1CaWX",
"colab_type": "code",
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
"colab": {}
},
"cell_type": "code",
"source": [
......@@ -347,12 +315,7 @@
"metadata": {
"id": "TRFYHB2mCaWb",
"colab_type": "code",
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
"colab": {}
},
"cell_type": "code",
"source": [
......@@ -375,12 +338,7 @@
"metadata": {
"id": "XKnCTHz4CaWg",
"colab_type": "code",
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
"colab": {}
},
"cell_type": "code",
"source": [
......@@ -403,12 +361,7 @@
"metadata": {
"id": "2KFnYlcwCaWl",
"colab_type": "code",
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
"colab": {}
},
"cell_type": "code",
"source": [
......@@ -431,12 +384,7 @@
"metadata": {
"id": "iJmPr5-ACaWn",
"colab_type": "code",
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
"colab": {}
},
"cell_type": "code",
"source": [
......@@ -461,19 +409,14 @@
"metadata": {
"id": "m4VEw8Ud9Quh",
"colab_type": "code",
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
"colab": {}
},
"cell_type": "code",
"source": [
"plt.figure()\n",
"plt.imshow(train_images[0])\n",
"plt.colorbar()\n",
"plt.gca().grid(False)"
"plt.grid('off')"
],
"execution_count": 0,
"outputs": []
......@@ -502,12 +445,7 @@
"metadata": {
"id": "bW5WzIPlCaWv",
"colab_type": "code",
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
"colab": {}
},
"cell_type": "code",
"source": [
......@@ -532,18 +470,10 @@
"metadata": {
"id": "oZTImqg_CaW1",
"colab_type": "code",
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
"colab": {}
},
"cell_type": "code",
"source": [
"import matplotlib.pyplot as plt\n",
"%matplotlib inline\n",
"\n",
"plt.figure(figsize=(10,10))\n",
"for i in range(25):\n",
" plt.subplot(5,5,i+1)\n",
......@@ -586,12 +516,7 @@
"metadata": {
"id": "9ODch-OFCaW4",
"colab_type": "code",
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
"colab": {}
},
"cell_type": "code",
"source": [
......@@ -628,12 +553,7 @@
"metadata": {
"id": "Lhan11blCaW7",
"colab_type": "code",
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
"colab": {}
},
"cell_type": "code",
"source": [
......@@ -666,12 +586,7 @@
"metadata": {
"id": "xvwvpA64CaW_",
"colab_type": "code",
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
"colab": {}
},
"cell_type": "code",
"source": [
......@@ -706,12 +621,7 @@
"metadata": {
"id": "VflXLEeECaXC",
"colab_type": "code",
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
"colab": {}
},
"cell_type": "code",
"source": [
......@@ -748,12 +658,7 @@
"metadata": {
"id": "Gl91RPhdCaXI",
"colab_type": "code",
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
"colab": {}
},
"cell_type": "code",
"source": [
......@@ -776,12 +681,7 @@
"metadata": {
"id": "3DmJEUinCaXK",
"colab_type": "code",
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
"colab": {}
},
"cell_type": "code",
"source": [
......@@ -804,12 +704,7 @@
"metadata": {
"id": "qsqenuPnCaXO",
"colab_type": "code",
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
"colab": {}
},
"cell_type": "code",
"source": [
......@@ -832,12 +727,7 @@
"metadata": {
"id": "Sd7Pgsu6CaXP",
"colab_type": "code",
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
"colab": {}
},
"cell_type": "code",
"source": [
......@@ -848,46 +738,131 @@
},
{
"metadata": {
"id": "kgdvGD52CaXR",
"id": "ygh2yYC972ne",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"Let's plot several images with their predictions. Correct prediction labels are green and incorrect prediction labels are red."
"We can graph this to look at the full set of 10 channels"
]
},
{
"metadata": {
"id": "YGBDAiziCaXR",
"id": "DvYmmrpIy6Y1",
"colab_type": "code",
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
"colab": {}
},
"cell_type": "code",
"source": [
"# Plot the first 25 test images, their predicted label, and the true label\n",
"# Color correct predictions in green, incorrect predictions in red\n",
"plt.figure(figsize=(10,10))\n",
"for i in range(25):\n",
" plt.subplot(5,5,i+1)\n",
"def plot_image(i, predictions_array, true_label, img):\n",
" predictions_array, true_label, img = predictions_array[i], true_label[i], img[i]\n",
" plt.grid('off')\n",
" plt.xticks([])\n",
" plt.yticks([])\n",
" plt.grid('off')\n",
" plt.imshow(test_images[i], cmap=plt.cm.binary)\n",
" predicted_label = np.argmax(predictions[i])\n",
" true_label = test_labels[i]\n",
" \n",
" plt.imshow(img, cmap=plt.cm.binary)\n",
"\n",
" predicted_label = np.argmax(predictions_array)\n",
" if predicted_label == true_label:\n",
" color = 'green'\n",
" color = 'blue'\n",
" else:\n",
" color = 'red'\n",
" plt.xlabel(\"{} ({})\".format(class_names[predicted_label], \n",
" \n",
" plt.xlabel(\"{} {:2.0f}% ({})\".format(class_names[predicted_label],\n",
" 100*np.max(predictions_array),\n",
" class_names[true_label]),\n",
" color=color)\n",
" "
"\n",
"def plot_value_array(i, predictions_array, true_label):\n",
" predictions_array, true_label = predictions_array[i], true_label[i]\n",
" plt.grid('off')\n",
" plt.xticks([])\n",
" plt.yticks([])\n",
" thisplot = plt.bar(range(10), predictions_array, color=\"#777777\")\n",
" plt.ylim([0, 1]) \n",
" predicted_label = np.argmax(predictions_array)\n",
" \n",
" thisplot[predicted_label].set_color('red')\n",
" thisplot[true_label].set_color('blue')"
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "d4Ov9OFDMmOD",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"Let's look at the 0th image, predictions, and prediction array. "
]
},
{
"metadata": {
"id": "HV5jw-5HwSmO",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"i = 0\n",
"plt.figure(figsize=(6,3))\n",
"plt.subplot(1,2,1)\n",
"plot_image(i, predictions, test_labels, test_images)\n",
"plt.subplot(1,2,2)\n",
"plot_value_array(i, predictions, test_labels)"
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "Ko-uzOufSCSe",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"i = 12\n",
"plt.figure(figsize=(6,3))\n",
"plt.subplot(1,2,1)\n",
"plot_image(i, predictions, test_labels, test_images)\n",
"plt.subplot(1,2,2)\n",
"plot_value_array(i, predictions, test_labels)"
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "kgdvGD52CaXR",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"Let's plot several images with their predictions. Correct prediction labels are blue and incorrect prediction labels are red. The number gives the percent (out of 100) for the predicted label. Note that it can be wrong even when very confident. "
]
},
{
"metadata": {
"id": "hQlnbqaw2Qu_",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"# Plot the first X test images, their predicted label, and the true label\n",
"# Color correct predictions in blue, incorrect predictions in red\n",
"num_rows = 5\n",
"num_cols = 3\n",
"num_images = num_rows*num_cols\n",
"plt.figure(figsize=(2*2*num_cols, 2*num_rows))\n",
"for i in range(num_images):\n",
" plt.subplot(num_rows, 2*num_cols, 2*i+1)\n",
" plot_image(i, predictions, test_labels, test_images)\n",
" plt.subplot(num_rows, 2*num_cols, 2*i+2)\n",
" plot_value_array(i, predictions, test_labels)\n"
],
"execution_count": 0,
"outputs": []
......@@ -906,12 +881,7 @@
"metadata": {
"id": "yRJ7JU7JCaXT",
"colab_type": "code",
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
"colab": {}
},
"cell_type": "code",
"source": [
......@@ -937,12 +907,7 @@
"metadata": {
"id": "lDFh5yF_CaXW",
"colab_type": "code",
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
"colab": {}
},
"cell_type": "code",
"source": [
......@@ -968,18 +933,27 @@
"metadata": {
"id": "o_rzNSdrCaXY",
"colab_type": "code",
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
"colab": {}
},
"cell_type": "code",
"source": [
"predictions = model.predict(img)\n",
"predictions_single = model.predict(img)\n",
"\n",
"print(predictions)"
"print(predictions_single)"
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "6Ai-cpLjO-3A",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"plot_value_array(0, predictions_single, test_labels)\n",
"_ = plt.xticks(range(10), class_names, rotation=45)"
],
"execution_count": 0,
"outputs": []
......@@ -998,18 +972,11 @@
"metadata": {
"id": "2tRmdq_8CaXb",
"colab_type": "code",
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
"colab": {}
},
"cell_type": "code",
"source": [
"prediction = predictions[0]\n",
"\n",
"np.argmax(prediction)"
"np.argmax(predictions_single[0])"
],
"execution_count": 0,
"outputs": []
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment