"git@developer.sourcefind.cn:modelzoo/resnet50_tensorflow.git" did not exist on "d1f25acc58303c0cbf4bbac2c6c210b3039bc1f5"
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 @@ ...@@ -5,8 +5,6 @@
"colab": { "colab": {
"name": "basic_classification.ipynb", "name": "basic_classification.ipynb",
"version": "0.3.2", "version": "0.3.2",
"views": {},
"default_view": {},
"provenance": [], "provenance": [],
"private_outputs": true, "private_outputs": true,
"collapsed_sections": [], "collapsed_sections": [],
...@@ -32,12 +30,7 @@ ...@@ -32,12 +30,7 @@
"metadata": { "metadata": {
"id": "_ckMIh7O7s6D", "id": "_ckMIh7O7s6D",
"colab_type": "code", "colab_type": "code",
"colab": { "colab": {},
"autoexec": {
"startup": false,
"wait_interval": 0
}
},
"cellView": "form" "cellView": "form"
}, },
"cell_type": "code", "cell_type": "code",
...@@ -61,12 +54,7 @@ ...@@ -61,12 +54,7 @@
"metadata": { "metadata": {
"id": "vasWnqRgy1H4", "id": "vasWnqRgy1H4",
"colab_type": "code", "colab_type": "code",
"colab": { "colab": {},
"autoexec": {
"startup": false,
"wait_interval": 0
}
},
"cellView": "form" "cellView": "form"
}, },
"cell_type": "code", "cell_type": "code",
...@@ -142,12 +130,7 @@ ...@@ -142,12 +130,7 @@
"metadata": { "metadata": {
"id": "dzLKpmZICaWN", "id": "dzLKpmZICaWN",
"colab_type": "code", "colab_type": "code",
"colab": { "colab": {}
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
}, },
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
...@@ -204,12 +187,7 @@ ...@@ -204,12 +187,7 @@
"metadata": { "metadata": {
"id": "7MqDQO0KCaWS", "id": "7MqDQO0KCaWS",
"colab_type": "code", "colab_type": "code",
"colab": { "colab": {}
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
}, },
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
...@@ -288,12 +266,7 @@ ...@@ -288,12 +266,7 @@
"metadata": { "metadata": {
"id": "IjnLH5S2CaWx", "id": "IjnLH5S2CaWx",
"colab_type": "code", "colab_type": "code",
"colab": { "colab": {}
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
}, },
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
...@@ -319,12 +292,7 @@ ...@@ -319,12 +292,7 @@
"metadata": { "metadata": {
"id": "zW5k_xz1CaWX", "id": "zW5k_xz1CaWX",
"colab_type": "code", "colab_type": "code",
"colab": { "colab": {}
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
}, },
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
...@@ -347,12 +315,7 @@ ...@@ -347,12 +315,7 @@
"metadata": { "metadata": {
"id": "TRFYHB2mCaWb", "id": "TRFYHB2mCaWb",
"colab_type": "code", "colab_type": "code",
"colab": { "colab": {}
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
}, },
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
...@@ -375,12 +338,7 @@ ...@@ -375,12 +338,7 @@
"metadata": { "metadata": {
"id": "XKnCTHz4CaWg", "id": "XKnCTHz4CaWg",
"colab_type": "code", "colab_type": "code",
"colab": { "colab": {}
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
}, },
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
...@@ -403,12 +361,7 @@ ...@@ -403,12 +361,7 @@
"metadata": { "metadata": {
"id": "2KFnYlcwCaWl", "id": "2KFnYlcwCaWl",
"colab_type": "code", "colab_type": "code",
"colab": { "colab": {}
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
}, },
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
...@@ -431,12 +384,7 @@ ...@@ -431,12 +384,7 @@
"metadata": { "metadata": {
"id": "iJmPr5-ACaWn", "id": "iJmPr5-ACaWn",
"colab_type": "code", "colab_type": "code",
"colab": { "colab": {}
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
}, },
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
...@@ -461,19 +409,14 @@ ...@@ -461,19 +409,14 @@
"metadata": { "metadata": {
"id": "m4VEw8Ud9Quh", "id": "m4VEw8Ud9Quh",
"colab_type": "code", "colab_type": "code",
"colab": { "colab": {}
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
}, },
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"plt.figure()\n", "plt.figure()\n",
"plt.imshow(train_images[0])\n", "plt.imshow(train_images[0])\n",
"plt.colorbar()\n", "plt.colorbar()\n",
"plt.gca().grid(False)" "plt.grid('off')"
], ],
"execution_count": 0, "execution_count": 0,
"outputs": [] "outputs": []
...@@ -502,12 +445,7 @@ ...@@ -502,12 +445,7 @@
"metadata": { "metadata": {
"id": "bW5WzIPlCaWv", "id": "bW5WzIPlCaWv",
"colab_type": "code", "colab_type": "code",
"colab": { "colab": {}
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
}, },
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
...@@ -532,18 +470,10 @@ ...@@ -532,18 +470,10 @@
"metadata": { "metadata": {
"id": "oZTImqg_CaW1", "id": "oZTImqg_CaW1",
"colab_type": "code", "colab_type": "code",
"colab": { "colab": {}
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
}, },
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"import matplotlib.pyplot as plt\n",
"%matplotlib inline\n",
"\n",
"plt.figure(figsize=(10,10))\n", "plt.figure(figsize=(10,10))\n",
"for i in range(25):\n", "for i in range(25):\n",
" plt.subplot(5,5,i+1)\n", " plt.subplot(5,5,i+1)\n",
...@@ -586,12 +516,7 @@ ...@@ -586,12 +516,7 @@
"metadata": { "metadata": {
"id": "9ODch-OFCaW4", "id": "9ODch-OFCaW4",
"colab_type": "code", "colab_type": "code",
"colab": { "colab": {}
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
}, },
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
...@@ -628,12 +553,7 @@ ...@@ -628,12 +553,7 @@
"metadata": { "metadata": {
"id": "Lhan11blCaW7", "id": "Lhan11blCaW7",
"colab_type": "code", "colab_type": "code",
"colab": { "colab": {}
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
}, },
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
...@@ -666,12 +586,7 @@ ...@@ -666,12 +586,7 @@
"metadata": { "metadata": {
"id": "xvwvpA64CaW_", "id": "xvwvpA64CaW_",
"colab_type": "code", "colab_type": "code",
"colab": { "colab": {}
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
}, },
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
...@@ -706,12 +621,7 @@ ...@@ -706,12 +621,7 @@
"metadata": { "metadata": {
"id": "VflXLEeECaXC", "id": "VflXLEeECaXC",
"colab_type": "code", "colab_type": "code",
"colab": { "colab": {}
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
}, },
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
...@@ -748,12 +658,7 @@ ...@@ -748,12 +658,7 @@
"metadata": { "metadata": {
"id": "Gl91RPhdCaXI", "id": "Gl91RPhdCaXI",
"colab_type": "code", "colab_type": "code",
"colab": { "colab": {}
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
}, },
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
...@@ -776,12 +681,7 @@ ...@@ -776,12 +681,7 @@
"metadata": { "metadata": {
"id": "3DmJEUinCaXK", "id": "3DmJEUinCaXK",
"colab_type": "code", "colab_type": "code",
"colab": { "colab": {}
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
}, },
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
...@@ -804,12 +704,7 @@ ...@@ -804,12 +704,7 @@
"metadata": { "metadata": {
"id": "qsqenuPnCaXO", "id": "qsqenuPnCaXO",
"colab_type": "code", "colab_type": "code",
"colab": { "colab": {}
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
}, },
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
...@@ -832,12 +727,7 @@ ...@@ -832,12 +727,7 @@
"metadata": { "metadata": {
"id": "Sd7Pgsu6CaXP", "id": "Sd7Pgsu6CaXP",
"colab_type": "code", "colab_type": "code",
"colab": { "colab": {}
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
}, },
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
...@@ -846,6 +736,104 @@ ...@@ -846,6 +736,104 @@
"execution_count": 0, "execution_count": 0,
"outputs": [] "outputs": []
}, },
{
"metadata": {
"id": "ygh2yYC972ne",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"We can graph this to look at the full set of 10 channels"
]
},
{
"metadata": {
"id": "DvYmmrpIy6Y1",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"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",
" \n",
" plt.imshow(img, cmap=plt.cm.binary)\n",
"\n",
" predicted_label = np.argmax(predictions_array)\n",
" if predicted_label == true_label:\n",
" color = 'blue'\n",
" else:\n",
" color = 'red'\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": { "metadata": {
"id": "kgdvGD52CaXR", "id": "kgdvGD52CaXR",
...@@ -853,41 +841,28 @@ ...@@ -853,41 +841,28 @@
}, },
"cell_type": "markdown", "cell_type": "markdown",
"source": [ "source": [
"Let's plot several images with their predictions. Correct prediction labels are green and incorrect prediction labels are red." "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": { "metadata": {
"id": "YGBDAiziCaXR", "id": "hQlnbqaw2Qu_",
"colab_type": "code", "colab_type": "code",
"colab": { "colab": {}
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
}, },
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"# Plot the first 25 test images, their predicted label, and the true label\n", "# Plot the first X test images, their predicted label, and the true label\n",
"# Color correct predictions in green, incorrect predictions in red\n", "# Color correct predictions in blue, incorrect predictions in red\n",
"plt.figure(figsize=(10,10))\n", "num_rows = 5\n",
"for i in range(25):\n", "num_cols = 3\n",
" plt.subplot(5,5,i+1)\n", "num_images = num_rows*num_cols\n",
" plt.xticks([])\n", "plt.figure(figsize=(2*2*num_cols, 2*num_rows))\n",
" plt.yticks([])\n", "for i in range(num_images):\n",
" plt.grid('off')\n", " plt.subplot(num_rows, 2*num_cols, 2*i+1)\n",
" plt.imshow(test_images[i], cmap=plt.cm.binary)\n", " plot_image(i, predictions, test_labels, test_images)\n",
" predicted_label = np.argmax(predictions[i])\n", " plt.subplot(num_rows, 2*num_cols, 2*i+2)\n",
" true_label = test_labels[i]\n", " plot_value_array(i, predictions, test_labels)\n"
" if predicted_label == true_label:\n",
" color = 'green'\n",
" else:\n",
" color = 'red'\n",
" plt.xlabel(\"{} ({})\".format(class_names[predicted_label], \n",
" class_names[true_label]),\n",
" color=color)\n",
" "
], ],
"execution_count": 0, "execution_count": 0,
"outputs": [] "outputs": []
...@@ -906,12 +881,7 @@ ...@@ -906,12 +881,7 @@
"metadata": { "metadata": {
"id": "yRJ7JU7JCaXT", "id": "yRJ7JU7JCaXT",
"colab_type": "code", "colab_type": "code",
"colab": { "colab": {}
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
}, },
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
...@@ -937,12 +907,7 @@ ...@@ -937,12 +907,7 @@
"metadata": { "metadata": {
"id": "lDFh5yF_CaXW", "id": "lDFh5yF_CaXW",
"colab_type": "code", "colab_type": "code",
"colab": { "colab": {}
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
}, },
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
...@@ -968,18 +933,27 @@ ...@@ -968,18 +933,27 @@
"metadata": { "metadata": {
"id": "o_rzNSdrCaXY", "id": "o_rzNSdrCaXY",
"colab_type": "code", "colab_type": "code",
"colab": { "colab": {}
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
}, },
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"predictions = model.predict(img)\n", "predictions_single = model.predict(img)\n",
"\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, "execution_count": 0,
"outputs": [] "outputs": []
...@@ -998,18 +972,11 @@ ...@@ -998,18 +972,11 @@
"metadata": { "metadata": {
"id": "2tRmdq_8CaXb", "id": "2tRmdq_8CaXb",
"colab_type": "code", "colab_type": "code",
"colab": { "colab": {}
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
}, },
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"prediction = predictions[0]\n", "np.argmax(predictions_single[0])"
"\n",
"np.argmax(prediction)"
], ],
"execution_count": 0, "execution_count": 0,
"outputs": [] "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