Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Chapter11/residualNeuralNetwork/withTransferLearning/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
What it does :

1. This Python program predict the category of the image using ResNet50 pretrained model.

Dependancies :

1. Keras module is needed to be installed in the local machine to run this program.
2. gdown module is needed to be installed in the local machine to run this program.
3. zipfile module is needed to be installed in the local machine to run this program.

Things to check before running :

1. Check whether you have given the correct location of your dataset file.
2. You should have access to the file in the Google Drive.



Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "residual_network_with_transfer_learning.ipynb",
"provenance": [],
"collapsed_sections": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "Fm15BsTFkiR2"
},
"source": [
"# **Problem: Residual Neural Network in tensorflow. Train on cats and dogs dataset.**\n",
"\n",
"Python program to train cats and dogs dataset using Residual Neural Network in tensorflow (with transfer learning).\n",
"\n",
"Run all the cells. After executing the last cell, you will get the predictions.\n",
"\n",
"**Notes:**\n",
"\n",
"Following things are needed to be checked before running the program.\n",
" 1. Keras module is needed to be installed.\n",
" 2. gdown module is needed to be installed.\n",
" 3. Check whether you have given the correct location of your dataset file.\n",
" 5. You should have access to the file in the Google Drive.\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ypdiISGNBNBX"
},
"source": [
"# **Import Modules**"
]
},
{
"cell_type": "code",
"metadata": {
"id": "rvTOp-uUAqtl"
},
"source": [
"# Import resnet50 pretrained model\n",
"from keras.applications.resnet import ResNet50\n",
"\n",
"# Import image module\n",
"from keras.preprocessing import image\n",
"\n",
"# Import preprocess_input and decode_predictions modules to evaluate the input\n",
"from keras.applications.resnet import preprocess_input, decode_predictions\n",
"\n",
"# Import numpy module\n",
"import numpy as np\n",
"\n",
"# Import gdown module to download files from google drive\n",
"import gdown\n"
],
"execution_count": 1,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "J9sgnIraBU86"
},
"source": [
"# **Get the file location from google drive and download**"
]
},
{
"cell_type": "code",
"metadata": {
"id": "lkTPUtD5BaBr"
},
"source": [
"# Please change the URL as needed (make sure you have the access to the file)\n",
"\n",
"url = 'https://drive.google.com/file/d/1-FTayQ4HNMpWg9BSUE17-gbreaSsdtCf/view?usp=sharing'\n",
"\n",
"# Derive the file id from the URL\n",
"file_id = url.split('/')[-2]\n",
"\n",
"# Derive the download url of the the file\n",
"download_url = 'https://drive.google.com/uc?id=' + file_id\n",
"\n",
"# Give the location you want to save it in your local machine\n",
"file_location = 'cats_and_dogs.zip'\n",
"\n",
"#--------------------------------------------- Download and extract the zip file -----------------------------------------------------------\n",
"\n",
"# Download the file from drive to your local machine\n",
"gdown.download(download_url, file_location, quiet=False)\n"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "KrMC9ovZBdmr"
},
"source": [
"# **Unzip the zip dataset**"
]
},
{
"cell_type": "code",
"metadata": {
"id": "EGCAPKdvBg9x"
},
"source": [
"!unzip /content/cats_and_dogs.zip -d \"/content/unzipped_folder/\""
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "ALN2RKuXB1nQ"
},
"source": [
"# **Start the prediction operation**"
]
},
{
"cell_type": "code",
"metadata": {
"id": "1ULa2X0GB27Z"
},
"source": [
"# Use ResNet50 pretrained model with imagenet training\n",
"model = ResNet50(weights='imagenet')\n",
"\n",
"def evaluate(img_fname):\n",
" # Load the image\n",
" img = image.load_img(img_fname, target_size=(224, 224))\n",
"\n",
" # Conver image into a Numpy array\n",
" x = image.img_to_array(img)\n",
"\n",
" # Expand the array \n",
" x = np.expand_dims(x, axis=0)\n",
"\n",
" # Adequate your image to the format the model requires\n",
" x = preprocess_input(x)\n",
"\n",
" # Get the predictions\n",
" preds = model.predict(x)\n",
"\n",
" # Print the probability and category name for the 5 categories \n",
" # With highest probability: \n",
" print('Predicted:', decode_predictions(preds, top=5)[0])"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "x8d84ch6B6jV"
},
"source": [
"# **Get the predictions for one image**"
]
},
{
"cell_type": "code",
"metadata": {
"id": "ZK9nKtGUB94t"
},
"source": [
"# Give the path of the image you want to predict to the evaluate function as a input parameter\n",
"evaluate('/content/unzipped_folder/Cats and Dogs (New)/Cats/Cat_1.jpg')"
],
"execution_count": null,
"outputs": []
}
]
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,86 @@
# TODO: Create a Residual Neural Network in tensorflow. Train on cats and dogs dataset. (with transfer learning)
# TODO: Code should be well commented.
'''Copyright (c) 2021 AIClub

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without
limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
OR OTHER DEALINGS IN THE SOFTWARE.'''

# Import resnet50 pretrained model
from keras.applications.resnet import ResNet50

# Import image module
from keras.preprocessing import image

# Import preprocess_input and decode_predictions modules to evaluate the input
from keras.applications.resnet import preprocess_input, decode_predictions

# Import numpy module
import numpy as np

# Import gdown module to download files from google drive
import gdown

# Import zip file module to open the zip file
from zipfile import ZipFile

#--------------------------------------------- Get the file location from google drive ----------------------------------------------------

# Please change the URL as needed (make sure you have the access to the file)

url = 'https://drive.google.com/file/d/1-FTayQ4HNMpWg9BSUE17-gbreaSsdtCf/view?usp=sharing'

# Derive the file id from the URL
file_id = url.split('/')[-2]

# Derive the download url of the the file
download_url = 'https://drive.google.com/uc?id=' + file_id

# Give the location you want to save it in your local machine
file_location = 'cats_and_dogs.zip'

#--------------------------------------------- Download and extract the zip file -----------------------------------------------------------

# Download the file from drive to your local machine
gdown.download(download_url, file_location)

# Open the downloaded zip file and extract its contents
with ZipFile(file_location, "r") as zip_file:
filepath = zip_file.extractall()

#--------------------------------------------- Start the prediction operation --------------------------------------------------------------

# Use ResNet50 pretrained model with imagenet training
model = ResNet50(weights='imagenet')

def evaluate(img_fname):
# Load the image
img = image.load_img(img_fname, target_size=(224, 224))

# Conver image into a Numpy array
x = image.img_to_array(img)

# Expand the array
x = np.expand_dims(x, axis=0)

# Adequate your image to the format the model requires
x = preprocess_input(x)

# Get the predictions
preds = model.predict(x)

# Print the probability and category name for the 5 categories
# With highest probability:
print('Predicted:', decode_predictions(preds, top=5)[0])

# Give the path of the image you want to predict to the evaluate function as a input parameter
evaluate('Cats and Dogs (New)\Cats\Cat_1.jpg')