Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save XingLiangLondon/a7df300a8592a0f057166081deaa690c to your computer and use it in GitHub Desktop.
Save XingLiangLondon/a7df300a8592a0f057166081deaa690c to your computer and use it in GitHub Desktop.
Created on Skills Network Labs
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a href=\"https://cocl.us/DL0320EN_TOP_IMAGE\">\n",
" <img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/DL0320EN/Assets/Images/Top.png\" width=\"750\" alt=\"IBM 10TB Storage\" />\n",
"</a>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h1>Classifying European Money Denominations: Comparing Two Models</h1>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2>Table of Contents</h2>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<p>In this lab you will compare the <code>ResNet50</code> and <code>VGG16</code></p>\n",
"<ul>\n",
" <li><a href=\"#dataset\">Create Dataset Class and Object</a></li>\n",
" <li><a href=\"#pre\">Load Pre-trained Model</a></li>\n",
" <li><a href=\"#analyze\">Analyze Models</a></li>\n",
"</ul>\n",
"\n",
"<p>Estimated Time Needed: <b>25 min</b></p>\n",
"<hr>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2>Preparation</h2>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a href=\"https://cocl.us/DL0320EN_storage\">\n",
" <img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/DL0320EN/Assets/Images/ObjectStorage.png\" width=\"750\" alt=\"cognitive class\" />\n",
"</a>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Download the datasets you needed for this lab."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# You can comment out this box when you already have the dataset\n",
"# Step 1: Ctrl + A : Select all\n",
"# Step 2: Ctrl + / : Comment out all; if everything selected has been comment out alreaday, then uncomment all\n",
"\n",
"# Download test dataset\n",
"!wget --quiet -O /resources/data/test_data_keras.tar.gz https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/DL0320EN/Datasets/Keras/test_data_keras.tar.gz\n",
"!tar -xzf /resources/data/test_data_keras.tar.gz -C /resources/data --exclude '.*'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Import the Keras Modules needed in the lab."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Using TensorFlow backend.\n"
]
}
],
"source": [
"# Import Keras Modules\n",
"\n",
"import keras\n",
"from keras.preprocessing.image import ImageDataGenerator\n",
"from keras.models import load_model"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Import Non-Keras Modules "
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# Import Non-Keras Modules\n",
"\n",
"import os\n",
"from matplotlib.pyplot import imshow\n",
"import matplotlib.pylab as plt\n",
"import pandas as pd\n",
"from PIL import Image\n",
"import numpy as np "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<hr>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"dataset\">Create Dataset Class and Object</h2>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In this section, you use the dataset class from the last section."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The denomination, the folder path for the testing data is as below."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"# The path for accessing image dataset folder\n",
"\n",
"test_data_dir = '/resources/data/test_data_keras/'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>Try</h3>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Try to construct a <code>test_generator</code> using <code>test_data_dir</code> as directory path"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"# Create Dataset Class\n",
"\n",
"CLASSES = ['5', '10', '20', '50', '100', '200', '500']\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Found 70 images belonging to 7 classes.\n"
]
}
],
"source": [
"test_generator = ImageDataGenerator().flow_from_directory(test_data_dir\n",
" , target_size=(224, 224)\n",
" , batch_size=5\n",
" , classes=CLASSES\n",
" , seed=0)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Double-click <b>here</b> for the solution.\n",
"<!--\n",
"test_generator = ImageDataGenerator().flow_from_directory(test_data_dir\n",
" , target_size=(224, 224)\n",
" , batch_size=5\n",
" , classes=CLASSES\n",
" , seed=0)\n",
"-->"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<hr>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"pre\">Load Pre-trained Model</h2>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Load the <code>ResNet50</code> and <code>VGG16</code> model you created from the last section "
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"# Load pre-trained model\n",
"model = load_model(\"resnet50_keras.pt\")\n",
"model_vgg = load_model(\"vgg16_keras.pt\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Print the structure of two models. You need to answer the questions in quiz based on the output here."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"=================================== ResNet50 =========================================\n",
"__________________________________________________________________________________________________\n",
"Layer (type) Output Shape Param # Connected to \n",
"==================================================================================================\n",
"input_1 (InputLayer) (None, 224, 224, 3) 0 \n",
"__________________________________________________________________________________________________\n",
"conv1_pad (ZeroPadding2D) (None, 230, 230, 3) 0 input_1[0][0] \n",
"__________________________________________________________________________________________________\n",
"conv1 (Conv2D) (None, 112, 112, 64) 9472 conv1_pad[0][0] \n",
"__________________________________________________________________________________________________\n",
"bn_conv1 (BatchNormalization) (None, 112, 112, 64) 256 conv1[0][0] \n",
"__________________________________________________________________________________________________\n",
"activation_1 (Activation) (None, 112, 112, 64) 0 bn_conv1[0][0] \n",
"__________________________________________________________________________________________________\n",
"max_pooling2d_1 (MaxPooling2D) (None, 55, 55, 64) 0 activation_1[0][0] \n",
"__________________________________________________________________________________________________\n",
"res2a_branch2a (Conv2D) (None, 55, 55, 64) 4160 max_pooling2d_1[0][0] \n",
"__________________________________________________________________________________________________\n",
"bn2a_branch2a (BatchNormalizati (None, 55, 55, 64) 256 res2a_branch2a[0][0] \n",
"__________________________________________________________________________________________________\n",
"activation_2 (Activation) (None, 55, 55, 64) 0 bn2a_branch2a[0][0] \n",
"__________________________________________________________________________________________________\n",
"res2a_branch2b (Conv2D) (None, 55, 55, 64) 36928 activation_2[0][0] \n",
"__________________________________________________________________________________________________\n",
"bn2a_branch2b (BatchNormalizati (None, 55, 55, 64) 256 res2a_branch2b[0][0] \n",
"__________________________________________________________________________________________________\n",
"activation_3 (Activation) (None, 55, 55, 64) 0 bn2a_branch2b[0][0] \n",
"__________________________________________________________________________________________________\n",
"res2a_branch2c (Conv2D) (None, 55, 55, 256) 16640 activation_3[0][0] \n",
"__________________________________________________________________________________________________\n",
"res2a_branch1 (Conv2D) (None, 55, 55, 256) 16640 max_pooling2d_1[0][0] \n",
"__________________________________________________________________________________________________\n",
"bn2a_branch2c (BatchNormalizati (None, 55, 55, 256) 1024 res2a_branch2c[0][0] \n",
"__________________________________________________________________________________________________\n",
"bn2a_branch1 (BatchNormalizatio (None, 55, 55, 256) 1024 res2a_branch1[0][0] \n",
"__________________________________________________________________________________________________\n",
"add_1 (Add) (None, 55, 55, 256) 0 bn2a_branch2c[0][0] \n",
" bn2a_branch1[0][0] \n",
"__________________________________________________________________________________________________\n",
"activation_4 (Activation) (None, 55, 55, 256) 0 add_1[0][0] \n",
"__________________________________________________________________________________________________\n",
"res2b_branch2a (Conv2D) (None, 55, 55, 64) 16448 activation_4[0][0] \n",
"__________________________________________________________________________________________________\n",
"bn2b_branch2a (BatchNormalizati (None, 55, 55, 64) 256 res2b_branch2a[0][0] \n",
"__________________________________________________________________________________________________\n",
"activation_5 (Activation) (None, 55, 55, 64) 0 bn2b_branch2a[0][0] \n",
"__________________________________________________________________________________________________\n",
"res2b_branch2b (Conv2D) (None, 55, 55, 64) 36928 activation_5[0][0] \n",
"__________________________________________________________________________________________________\n",
"bn2b_branch2b (BatchNormalizati (None, 55, 55, 64) 256 res2b_branch2b[0][0] \n",
"__________________________________________________________________________________________________\n",
"activation_6 (Activation) (None, 55, 55, 64) 0 bn2b_branch2b[0][0] \n",
"__________________________________________________________________________________________________\n",
"res2b_branch2c (Conv2D) (None, 55, 55, 256) 16640 activation_6[0][0] \n",
"__________________________________________________________________________________________________\n",
"bn2b_branch2c (BatchNormalizati (None, 55, 55, 256) 1024 res2b_branch2c[0][0] \n",
"__________________________________________________________________________________________________\n",
"add_2 (Add) (None, 55, 55, 256) 0 bn2b_branch2c[0][0] \n",
" activation_4[0][0] \n",
"__________________________________________________________________________________________________\n",
"activation_7 (Activation) (None, 55, 55, 256) 0 add_2[0][0] \n",
"__________________________________________________________________________________________________\n",
"res2c_branch2a (Conv2D) (None, 55, 55, 64) 16448 activation_7[0][0] \n",
"__________________________________________________________________________________________________\n",
"bn2c_branch2a (BatchNormalizati (None, 55, 55, 64) 256 res2c_branch2a[0][0] \n",
"__________________________________________________________________________________________________\n",
"activation_8 (Activation) (None, 55, 55, 64) 0 bn2c_branch2a[0][0] \n",
"__________________________________________________________________________________________________\n",
"res2c_branch2b (Conv2D) (None, 55, 55, 64) 36928 activation_8[0][0] \n",
"__________________________________________________________________________________________________\n",
"bn2c_branch2b (BatchNormalizati (None, 55, 55, 64) 256 res2c_branch2b[0][0] \n",
"__________________________________________________________________________________________________\n",
"activation_9 (Activation) (None, 55, 55, 64) 0 bn2c_branch2b[0][0] \n",
"__________________________________________________________________________________________________\n",
"res2c_branch2c (Conv2D) (None, 55, 55, 256) 16640 activation_9[0][0] \n",
"__________________________________________________________________________________________________\n",
"bn2c_branch2c (BatchNormalizati (None, 55, 55, 256) 1024 res2c_branch2c[0][0] \n",
"__________________________________________________________________________________________________\n",
"add_3 (Add) (None, 55, 55, 256) 0 bn2c_branch2c[0][0] \n",
" activation_7[0][0] \n",
"__________________________________________________________________________________________________\n",
"activation_10 (Activation) (None, 55, 55, 256) 0 add_3[0][0] \n",
"__________________________________________________________________________________________________\n",
"res3a_branch2a (Conv2D) (None, 28, 28, 128) 32896 activation_10[0][0] \n",
"__________________________________________________________________________________________________\n",
"bn3a_branch2a (BatchNormalizati (None, 28, 28, 128) 512 res3a_branch2a[0][0] \n",
"__________________________________________________________________________________________________\n",
"activation_11 (Activation) (None, 28, 28, 128) 0 bn3a_branch2a[0][0] \n",
"__________________________________________________________________________________________________\n",
"res3a_branch2b (Conv2D) (None, 28, 28, 128) 147584 activation_11[0][0] \n",
"__________________________________________________________________________________________________\n",
"bn3a_branch2b (BatchNormalizati (None, 28, 28, 128) 512 res3a_branch2b[0][0] \n",
"__________________________________________________________________________________________________\n",
"activation_12 (Activation) (None, 28, 28, 128) 0 bn3a_branch2b[0][0] \n",
"__________________________________________________________________________________________________\n",
"res3a_branch2c (Conv2D) (None, 28, 28, 512) 66048 activation_12[0][0] \n",
"__________________________________________________________________________________________________\n",
"res3a_branch1 (Conv2D) (None, 28, 28, 512) 131584 activation_10[0][0] \n",
"__________________________________________________________________________________________________\n",
"bn3a_branch2c (BatchNormalizati (None, 28, 28, 512) 2048 res3a_branch2c[0][0] \n",
"__________________________________________________________________________________________________\n",
"bn3a_branch1 (BatchNormalizatio (None, 28, 28, 512) 2048 res3a_branch1[0][0] \n",
"__________________________________________________________________________________________________\n",
"add_4 (Add) (None, 28, 28, 512) 0 bn3a_branch2c[0][0] \n",
" bn3a_branch1[0][0] \n",
"__________________________________________________________________________________________________\n",
"activation_13 (Activation) (None, 28, 28, 512) 0 add_4[0][0] \n",
"__________________________________________________________________________________________________\n",
"res3b_branch2a (Conv2D) (None, 28, 28, 128) 65664 activation_13[0][0] \n",
"__________________________________________________________________________________________________\n",
"bn3b_branch2a (BatchNormalizati (None, 28, 28, 128) 512 res3b_branch2a[0][0] \n",
"__________________________________________________________________________________________________\n",
"activation_14 (Activation) (None, 28, 28, 128) 0 bn3b_branch2a[0][0] \n",
"__________________________________________________________________________________________________\n",
"res3b_branch2b (Conv2D) (None, 28, 28, 128) 147584 activation_14[0][0] \n",
"__________________________________________________________________________________________________\n",
"bn3b_branch2b (BatchNormalizati (None, 28, 28, 128) 512 res3b_branch2b[0][0] \n",
"__________________________________________________________________________________________________\n",
"activation_15 (Activation) (None, 28, 28, 128) 0 bn3b_branch2b[0][0] \n",
"__________________________________________________________________________________________________\n",
"res3b_branch2c (Conv2D) (None, 28, 28, 512) 66048 activation_15[0][0] \n",
"__________________________________________________________________________________________________\n",
"bn3b_branch2c (BatchNormalizati (None, 28, 28, 512) 2048 res3b_branch2c[0][0] \n",
"__________________________________________________________________________________________________\n",
"add_5 (Add) (None, 28, 28, 512) 0 bn3b_branch2c[0][0] \n",
" activation_13[0][0] \n",
"__________________________________________________________________________________________________\n",
"activation_16 (Activation) (None, 28, 28, 512) 0 add_5[0][0] \n",
"__________________________________________________________________________________________________\n",
"res3c_branch2a (Conv2D) (None, 28, 28, 128) 65664 activation_16[0][0] \n",
"__________________________________________________________________________________________________\n",
"bn3c_branch2a (BatchNormalizati (None, 28, 28, 128) 512 res3c_branch2a[0][0] \n",
"__________________________________________________________________________________________________\n",
"activation_17 (Activation) (None, 28, 28, 128) 0 bn3c_branch2a[0][0] \n",
"__________________________________________________________________________________________________\n",
"res3c_branch2b (Conv2D) (None, 28, 28, 128) 147584 activation_17[0][0] \n",
"__________________________________________________________________________________________________\n",
"bn3c_branch2b (BatchNormalizati (None, 28, 28, 128) 512 res3c_branch2b[0][0] \n",
"__________________________________________________________________________________________________\n",
"activation_18 (Activation) (None, 28, 28, 128) 0 bn3c_branch2b[0][0] \n",
"__________________________________________________________________________________________________\n",
"res3c_branch2c (Conv2D) (None, 28, 28, 512) 66048 activation_18[0][0] \n",
"__________________________________________________________________________________________________\n",
"bn3c_branch2c (BatchNormalizati (None, 28, 28, 512) 2048 res3c_branch2c[0][0] \n",
"__________________________________________________________________________________________________\n",
"add_6 (Add) (None, 28, 28, 512) 0 bn3c_branch2c[0][0] \n",
" activation_16[0][0] \n",
"__________________________________________________________________________________________________\n",
"activation_19 (Activation) (None, 28, 28, 512) 0 add_6[0][0] \n",
"__________________________________________________________________________________________________\n",
"res3d_branch2a (Conv2D) (None, 28, 28, 128) 65664 activation_19[0][0] \n",
"__________________________________________________________________________________________________\n",
"bn3d_branch2a (BatchNormalizati (None, 28, 28, 128) 512 res3d_branch2a[0][0] \n",
"__________________________________________________________________________________________________\n",
"activation_20 (Activation) (None, 28, 28, 128) 0 bn3d_branch2a[0][0] \n",
"__________________________________________________________________________________________________\n",
"res3d_branch2b (Conv2D) (None, 28, 28, 128) 147584 activation_20[0][0] \n",
"__________________________________________________________________________________________________\n",
"bn3d_branch2b (BatchNormalizati (None, 28, 28, 128) 512 res3d_branch2b[0][0] \n",
"__________________________________________________________________________________________________\n",
"activation_21 (Activation) (None, 28, 28, 128) 0 bn3d_branch2b[0][0] \n",
"__________________________________________________________________________________________________\n",
"res3d_branch2c (Conv2D) (None, 28, 28, 512) 66048 activation_21[0][0] \n",
"__________________________________________________________________________________________________\n",
"bn3d_branch2c (BatchNormalizati (None, 28, 28, 512) 2048 res3d_branch2c[0][0] \n",
"__________________________________________________________________________________________________\n",
"add_7 (Add) (None, 28, 28, 512) 0 bn3d_branch2c[0][0] \n",
" activation_19[0][0] \n",
"__________________________________________________________________________________________________\n",
"activation_22 (Activation) (None, 28, 28, 512) 0 add_7[0][0] \n",
"__________________________________________________________________________________________________\n",
"res4a_branch2a (Conv2D) (None, 14, 14, 256) 131328 activation_22[0][0] \n",
"__________________________________________________________________________________________________\n",
"bn4a_branch2a (BatchNormalizati (None, 14, 14, 256) 1024 res4a_branch2a[0][0] \n",
"__________________________________________________________________________________________________\n",
"activation_23 (Activation) (None, 14, 14, 256) 0 bn4a_branch2a[0][0] \n",
"__________________________________________________________________________________________________\n",
"res4a_branch2b (Conv2D) (None, 14, 14, 256) 590080 activation_23[0][0] \n",
"__________________________________________________________________________________________________\n",
"bn4a_branch2b (BatchNormalizati (None, 14, 14, 256) 1024 res4a_branch2b[0][0] \n",
"__________________________________________________________________________________________________\n",
"activation_24 (Activation) (None, 14, 14, 256) 0 bn4a_branch2b[0][0] \n",
"__________________________________________________________________________________________________\n",
"res4a_branch2c (Conv2D) (None, 14, 14, 1024) 263168 activation_24[0][0] \n",
"__________________________________________________________________________________________________\n",
"res4a_branch1 (Conv2D) (None, 14, 14, 1024) 525312 activation_22[0][0] \n",
"__________________________________________________________________________________________________\n",
"bn4a_branch2c (BatchNormalizati (None, 14, 14, 1024) 4096 res4a_branch2c[0][0] \n",
"__________________________________________________________________________________________________\n",
"bn4a_branch1 (BatchNormalizatio (None, 14, 14, 1024) 4096 res4a_branch1[0][0] \n",
"__________________________________________________________________________________________________\n",
"add_8 (Add) (None, 14, 14, 1024) 0 bn4a_branch2c[0][0] \n",
" bn4a_branch1[0][0] \n",
"__________________________________________________________________________________________________\n",
"activation_25 (Activation) (None, 14, 14, 1024) 0 add_8[0][0] \n",
"__________________________________________________________________________________________________\n",
"res4b_branch2a (Conv2D) (None, 14, 14, 256) 262400 activation_25[0][0] \n",
"__________________________________________________________________________________________________\n",
"bn4b_branch2a (BatchNormalizati (None, 14, 14, 256) 1024 res4b_branch2a[0][0] \n",
"__________________________________________________________________________________________________\n",
"activation_26 (Activation) (None, 14, 14, 256) 0 bn4b_branch2a[0][0] \n",
"__________________________________________________________________________________________________\n",
"res4b_branch2b (Conv2D) (None, 14, 14, 256) 590080 activation_26[0][0] \n",
"__________________________________________________________________________________________________\n",
"bn4b_branch2b (BatchNormalizati (None, 14, 14, 256) 1024 res4b_branch2b[0][0] \n",
"__________________________________________________________________________________________________\n",
"activation_27 (Activation) (None, 14, 14, 256) 0 bn4b_branch2b[0][0] \n",
"__________________________________________________________________________________________________\n",
"res4b_branch2c (Conv2D) (None, 14, 14, 1024) 263168 activation_27[0][0] \n",
"__________________________________________________________________________________________________\n",
"bn4b_branch2c (BatchNormalizati (None, 14, 14, 1024) 4096 res4b_branch2c[0][0] \n",
"__________________________________________________________________________________________________\n",
"add_9 (Add) (None, 14, 14, 1024) 0 bn4b_branch2c[0][0] \n",
" activation_25[0][0] \n",
"__________________________________________________________________________________________________\n",
"activation_28 (Activation) (None, 14, 14, 1024) 0 add_9[0][0] \n",
"__________________________________________________________________________________________________\n",
"res4c_branch2a (Conv2D) (None, 14, 14, 256) 262400 activation_28[0][0] \n",
"__________________________________________________________________________________________________\n",
"bn4c_branch2a (BatchNormalizati (None, 14, 14, 256) 1024 res4c_branch2a[0][0] \n",
"__________________________________________________________________________________________________\n",
"activation_29 (Activation) (None, 14, 14, 256) 0 bn4c_branch2a[0][0] \n",
"__________________________________________________________________________________________________\n",
"res4c_branch2b (Conv2D) (None, 14, 14, 256) 590080 activation_29[0][0] \n",
"__________________________________________________________________________________________________\n",
"bn4c_branch2b (BatchNormalizati (None, 14, 14, 256) 1024 res4c_branch2b[0][0] \n",
"__________________________________________________________________________________________________\n",
"activation_30 (Activation) (None, 14, 14, 256) 0 bn4c_branch2b[0][0] \n",
"__________________________________________________________________________________________________\n",
"res4c_branch2c (Conv2D) (None, 14, 14, 1024) 263168 activation_30[0][0] \n",
"__________________________________________________________________________________________________\n",
"bn4c_branch2c (BatchNormalizati (None, 14, 14, 1024) 4096 res4c_branch2c[0][0] \n",
"__________________________________________________________________________________________________\n",
"add_10 (Add) (None, 14, 14, 1024) 0 bn4c_branch2c[0][0] \n",
" activation_28[0][0] \n",
"__________________________________________________________________________________________________\n",
"activation_31 (Activation) (None, 14, 14, 1024) 0 add_10[0][0] \n",
"__________________________________________________________________________________________________\n",
"res4d_branch2a (Conv2D) (None, 14, 14, 256) 262400 activation_31[0][0] \n",
"__________________________________________________________________________________________________\n",
"bn4d_branch2a (BatchNormalizati (None, 14, 14, 256) 1024 res4d_branch2a[0][0] \n",
"__________________________________________________________________________________________________\n",
"activation_32 (Activation) (None, 14, 14, 256) 0 bn4d_branch2a[0][0] \n",
"__________________________________________________________________________________________________\n",
"res4d_branch2b (Conv2D) (None, 14, 14, 256) 590080 activation_32[0][0] \n",
"__________________________________________________________________________________________________\n",
"bn4d_branch2b (BatchNormalizati (None, 14, 14, 256) 1024 res4d_branch2b[0][0] \n",
"__________________________________________________________________________________________________\n",
"activation_33 (Activation) (None, 14, 14, 256) 0 bn4d_branch2b[0][0] \n",
"__________________________________________________________________________________________________\n",
"res4d_branch2c (Conv2D) (None, 14, 14, 1024) 263168 activation_33[0][0] \n",
"__________________________________________________________________________________________________\n",
"bn4d_branch2c (BatchNormalizati (None, 14, 14, 1024) 4096 res4d_branch2c[0][0] \n",
"__________________________________________________________________________________________________\n",
"add_11 (Add) (None, 14, 14, 1024) 0 bn4d_branch2c[0][0] \n",
" activation_31[0][0] \n",
"__________________________________________________________________________________________________\n",
"activation_34 (Activation) (None, 14, 14, 1024) 0 add_11[0][0] \n",
"__________________________________________________________________________________________________\n",
"res4e_branch2a (Conv2D) (None, 14, 14, 256) 262400 activation_34[0][0] \n",
"__________________________________________________________________________________________________\n",
"bn4e_branch2a (BatchNormalizati (None, 14, 14, 256) 1024 res4e_branch2a[0][0] \n",
"__________________________________________________________________________________________________\n",
"activation_35 (Activation) (None, 14, 14, 256) 0 bn4e_branch2a[0][0] \n",
"__________________________________________________________________________________________________\n",
"res4e_branch2b (Conv2D) (None, 14, 14, 256) 590080 activation_35[0][0] \n",
"__________________________________________________________________________________________________\n",
"bn4e_branch2b (BatchNormalizati (None, 14, 14, 256) 1024 res4e_branch2b[0][0] \n",
"__________________________________________________________________________________________________\n",
"activation_36 (Activation) (None, 14, 14, 256) 0 bn4e_branch2b[0][0] \n",
"__________________________________________________________________________________________________\n",
"res4e_branch2c (Conv2D) (None, 14, 14, 1024) 263168 activation_36[0][0] \n",
"__________________________________________________________________________________________________\n",
"bn4e_branch2c (BatchNormalizati (None, 14, 14, 1024) 4096 res4e_branch2c[0][0] \n",
"__________________________________________________________________________________________________\n",
"add_12 (Add) (None, 14, 14, 1024) 0 bn4e_branch2c[0][0] \n",
" activation_34[0][0] \n",
"__________________________________________________________________________________________________\n",
"activation_37 (Activation) (None, 14, 14, 1024) 0 add_12[0][0] \n",
"__________________________________________________________________________________________________\n",
"res4f_branch2a (Conv2D) (None, 14, 14, 256) 262400 activation_37[0][0] \n",
"__________________________________________________________________________________________________\n",
"bn4f_branch2a (BatchNormalizati (None, 14, 14, 256) 1024 res4f_branch2a[0][0] \n",
"__________________________________________________________________________________________________\n",
"activation_38 (Activation) (None, 14, 14, 256) 0 bn4f_branch2a[0][0] \n",
"__________________________________________________________________________________________________\n",
"res4f_branch2b (Conv2D) (None, 14, 14, 256) 590080 activation_38[0][0] \n",
"__________________________________________________________________________________________________\n",
"bn4f_branch2b (BatchNormalizati (None, 14, 14, 256) 1024 res4f_branch2b[0][0] \n",
"__________________________________________________________________________________________________\n",
"activation_39 (Activation) (None, 14, 14, 256) 0 bn4f_branch2b[0][0] \n",
"__________________________________________________________________________________________________\n",
"res4f_branch2c (Conv2D) (None, 14, 14, 1024) 263168 activation_39[0][0] \n",
"__________________________________________________________________________________________________\n",
"bn4f_branch2c (BatchNormalizati (None, 14, 14, 1024) 4096 res4f_branch2c[0][0] \n",
"__________________________________________________________________________________________________\n",
"add_13 (Add) (None, 14, 14, 1024) 0 bn4f_branch2c[0][0] \n",
" activation_37[0][0] \n",
"__________________________________________________________________________________________________\n",
"activation_40 (Activation) (None, 14, 14, 1024) 0 add_13[0][0] \n",
"__________________________________________________________________________________________________\n",
"res5a_branch2a (Conv2D) (None, 7, 7, 512) 524800 activation_40[0][0] \n",
"__________________________________________________________________________________________________\n",
"bn5a_branch2a (BatchNormalizati (None, 7, 7, 512) 2048 res5a_branch2a[0][0] \n",
"__________________________________________________________________________________________________\n",
"activation_41 (Activation) (None, 7, 7, 512) 0 bn5a_branch2a[0][0] \n",
"__________________________________________________________________________________________________\n",
"res5a_branch2b (Conv2D) (None, 7, 7, 512) 2359808 activation_41[0][0] \n",
"__________________________________________________________________________________________________\n",
"bn5a_branch2b (BatchNormalizati (None, 7, 7, 512) 2048 res5a_branch2b[0][0] \n",
"__________________________________________________________________________________________________\n",
"activation_42 (Activation) (None, 7, 7, 512) 0 bn5a_branch2b[0][0] \n",
"__________________________________________________________________________________________________\n",
"res5a_branch2c (Conv2D) (None, 7, 7, 2048) 1050624 activation_42[0][0] \n",
"__________________________________________________________________________________________________\n",
"res5a_branch1 (Conv2D) (None, 7, 7, 2048) 2099200 activation_40[0][0] \n",
"__________________________________________________________________________________________________\n",
"bn5a_branch2c (BatchNormalizati (None, 7, 7, 2048) 8192 res5a_branch2c[0][0] \n",
"__________________________________________________________________________________________________\n",
"bn5a_branch1 (BatchNormalizatio (None, 7, 7, 2048) 8192 res5a_branch1[0][0] \n",
"__________________________________________________________________________________________________\n",
"add_14 (Add) (None, 7, 7, 2048) 0 bn5a_branch2c[0][0] \n",
" bn5a_branch1[0][0] \n",
"__________________________________________________________________________________________________\n",
"activation_43 (Activation) (None, 7, 7, 2048) 0 add_14[0][0] \n",
"__________________________________________________________________________________________________\n",
"res5b_branch2a (Conv2D) (None, 7, 7, 512) 1049088 activation_43[0][0] \n",
"__________________________________________________________________________________________________\n",
"bn5b_branch2a (BatchNormalizati (None, 7, 7, 512) 2048 res5b_branch2a[0][0] \n",
"__________________________________________________________________________________________________\n",
"activation_44 (Activation) (None, 7, 7, 512) 0 bn5b_branch2a[0][0] \n",
"__________________________________________________________________________________________________\n",
"res5b_branch2b (Conv2D) (None, 7, 7, 512) 2359808 activation_44[0][0] \n",
"__________________________________________________________________________________________________\n",
"bn5b_branch2b (BatchNormalizati (None, 7, 7, 512) 2048 res5b_branch2b[0][0] \n",
"__________________________________________________________________________________________________\n",
"activation_45 (Activation) (None, 7, 7, 512) 0 bn5b_branch2b[0][0] \n",
"__________________________________________________________________________________________________\n",
"res5b_branch2c (Conv2D) (None, 7, 7, 2048) 1050624 activation_45[0][0] \n",
"__________________________________________________________________________________________________\n",
"bn5b_branch2c (BatchNormalizati (None, 7, 7, 2048) 8192 res5b_branch2c[0][0] \n",
"__________________________________________________________________________________________________\n",
"add_15 (Add) (None, 7, 7, 2048) 0 bn5b_branch2c[0][0] \n",
" activation_43[0][0] \n",
"__________________________________________________________________________________________________\n",
"activation_46 (Activation) (None, 7, 7, 2048) 0 add_15[0][0] \n",
"__________________________________________________________________________________________________\n",
"res5c_branch2a (Conv2D) (None, 7, 7, 512) 1049088 activation_46[0][0] \n",
"__________________________________________________________________________________________________\n",
"bn5c_branch2a (BatchNormalizati (None, 7, 7, 512) 2048 res5c_branch2a[0][0] \n",
"__________________________________________________________________________________________________\n",
"activation_47 (Activation) (None, 7, 7, 512) 0 bn5c_branch2a[0][0] \n",
"__________________________________________________________________________________________________\n",
"res5c_branch2b (Conv2D) (None, 7, 7, 512) 2359808 activation_47[0][0] \n",
"__________________________________________________________________________________________________\n",
"bn5c_branch2b (BatchNormalizati (None, 7, 7, 512) 2048 res5c_branch2b[0][0] \n",
"__________________________________________________________________________________________________\n",
"activation_48 (Activation) (None, 7, 7, 512) 0 bn5c_branch2b[0][0] \n",
"__________________________________________________________________________________________________\n",
"res5c_branch2c (Conv2D) (None, 7, 7, 2048) 1050624 activation_48[0][0] \n",
"__________________________________________________________________________________________________\n",
"bn5c_branch2c (BatchNormalizati (None, 7, 7, 2048) 8192 res5c_branch2c[0][0] \n",
"__________________________________________________________________________________________________\n",
"add_16 (Add) (None, 7, 7, 2048) 0 bn5c_branch2c[0][0] \n",
" activation_46[0][0] \n",
"__________________________________________________________________________________________________\n",
"activation_49 (Activation) (None, 7, 7, 2048) 0 add_16[0][0] \n",
"__________________________________________________________________________________________________\n",
"avg_pool (AveragePooling2D) (None, 1, 1, 2048) 0 activation_49[0][0] \n",
"__________________________________________________________________________________________________\n",
"flatten_1 (Flatten) (None, 2048) 0 avg_pool[0][0] \n",
"__________________________________________________________________________________________________\n",
"dense_1 (Dense) (None, 7) 14343 flatten_1[0][0] \n",
"==================================================================================================\n",
"Total params: 23,602,055\n",
"Trainable params: 14,343\n",
"Non-trainable params: 23,587,712\n",
"__________________________________________________________________________________________________\n",
"ResNet5O:\n",
" None\n",
"=================================== VGG16 ============================================\n",
"_________________________________________________________________\n",
"Layer (type) Output Shape Param # \n",
"=================================================================\n",
"input_3 (InputLayer) (None, 224, 224, 3) 0 \n",
"_________________________________________________________________\n",
"block1_conv1 (Conv2D) (None, 224, 224, 64) 1792 \n",
"_________________________________________________________________\n",
"block1_conv2 (Conv2D) (None, 224, 224, 64) 36928 \n",
"_________________________________________________________________\n",
"block1_pool (MaxPooling2D) (None, 112, 112, 64) 0 \n",
"_________________________________________________________________\n",
"block2_conv1 (Conv2D) (None, 112, 112, 128) 73856 \n",
"_________________________________________________________________\n",
"block2_conv2 (Conv2D) (None, 112, 112, 128) 147584 \n",
"_________________________________________________________________\n",
"block2_pool (MaxPooling2D) (None, 56, 56, 128) 0 \n",
"_________________________________________________________________\n",
"block3_conv1 (Conv2D) (None, 56, 56, 256) 295168 \n",
"_________________________________________________________________\n",
"block3_conv2 (Conv2D) (None, 56, 56, 256) 590080 \n",
"_________________________________________________________________\n",
"block3_conv3 (Conv2D) (None, 56, 56, 256) 590080 \n",
"_________________________________________________________________\n",
"block3_pool (MaxPooling2D) (None, 28, 28, 256) 0 \n",
"_________________________________________________________________\n",
"block4_conv1 (Conv2D) (None, 28, 28, 512) 1180160 \n",
"_________________________________________________________________\n",
"block4_conv2 (Conv2D) (None, 28, 28, 512) 2359808 \n",
"_________________________________________________________________\n",
"block4_conv3 (Conv2D) (None, 28, 28, 512) 2359808 \n",
"_________________________________________________________________\n",
"block4_pool (MaxPooling2D) (None, 14, 14, 512) 0 \n",
"_________________________________________________________________\n",
"block5_conv1 (Conv2D) (None, 14, 14, 512) 2359808 \n",
"_________________________________________________________________\n",
"block5_conv2 (Conv2D) (None, 14, 14, 512) 2359808 \n",
"_________________________________________________________________\n",
"block5_conv3 (Conv2D) (None, 14, 14, 512) 2359808 \n",
"_________________________________________________________________\n",
"block5_pool (MaxPooling2D) (None, 7, 7, 512) 0 \n",
"_________________________________________________________________\n",
"flatten (Flatten) (None, 25088) 0 \n",
"_________________________________________________________________\n",
"fc1 (Dense) (None, 4096) 102764544 \n",
"_________________________________________________________________\n",
"fc2 (Dense) (None, 4096) 16781312 \n",
"_________________________________________________________________\n",
"dense_2 (Dense) (None, 7) 28679 \n",
"=================================================================\n",
"Total params: 134,289,223\n",
"Trainable params: 28,679\n",
"Non-trainable params: 134,260,544\n",
"_________________________________________________________________\n",
"VGG16:\n",
" None\n"
]
}
],
"source": [
"# Print model structure\n",
"\n",
"print(\"=================================== ResNet50 =========================================\")\n",
"print(\"ResNet5O:\\n\", model.summary())\n",
"print(\"=================================== VGG16 ============================================\")\n",
"print(\"VGG16:\\n\", model_vgg.summary())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<hr>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"analyze\">Analyze Models</h2>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>Try</h3>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Find the error on the test data using the <code>ResNet50</code> model"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Accuracy using ResNet50: 0.6428571577583041\n"
]
}
],
"source": [
"# Predict the data using ResNet50 model and print out accuracy\n",
"score = model.evaluate_generator(test_generator, test_generator.n//test_generator.batch_size)\n",
"print(\"Accuracy using ResNet50: \", score[1]) \n",
"# Type your code here"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Double-click <b>here</b> for the solution.\n",
"<!--\n",
"score = model.evaluate_generator(test_generator, test_generator.n//test_generator.batch_size)\n",
"print(\"Accuracy using ResNet50: \", score[1]) \n",
"-->"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>Try</h3>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Find the error on the test data using the <code>VGG16</code> model"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Accuracy using VGG16: 1.0\n"
]
}
],
"source": [
"# Predict the data using VGG16 model and print out accuracy\n",
"score = model_vgg.evaluate_generator(test_generator, test_generator.n//test_generator.batch_size)\n",
"print(\"Accuracy using VGG16: \", score[1]) \n",
"# Type your code here"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Double-click <b>here</b> for the solution.\n",
"<!--\n",
"score = model_vgg.evaluate_generator(test_generator, test_generator.n//test_generator.batch_size)\n",
"print(\"Accuracy using VGG16: \", score[1]) \n",
"-->"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>Try</h3>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"What model performed better on the test data"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"# Write your answer here: VGG16"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a href=\"https://cocl.us/DLO0320EN_notebook_bott\">\n",
" <img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/DL0320EN/Assets/Images/Bottom.png\" width=\"750\" alt=\"cognitive class\" />\n",
"</a>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2>About the Authors:</h2> \n",
"\n",
"<a href=\"https://www.linkedin.com/in/joseph-s-50398b136/\">Joseph Santarcangelo</a> has a PhD in Electrical Engineering, his research focused on using machine learning, signal processing, and computer vision to determine how videos impact human cognition. Joseph has been working for IBM since he completed his PhD."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Other contributors: <a href=\"https://www.linkedin.com/in/michelleccarey/\">Michelle Carey</a>, <a href=\"www.linkedin.com/in/jiahui-mavis-zhou-a4537814a\">Mavis Zhou</a>, <a href=\"https://www.linkedin.com/in/yi-leng-yao-84451275/\">Yi Leng Yao</a>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<hr>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Copyright &copy; 2018 <a href=\"cognitiveclass.ai?utm_source=bducopyrightlink&utm_medium=dswb&utm_campaign=bdu\">cognitiveclass.ai</a>. This notebook and its source code are released under the terms of the <a href=\"https://bigdatauniversity.com/mit-license/\">MIT License</a>."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"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.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment