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/withoutTransferLearning/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
What it does :

1. This Python program train cats and dogs dataset using Residual Neural Network in tensorflow (without transfer learning).

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,207 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "residual_network_without_transfer_learning.ipynb",
"provenance": [],
"collapsed_sections": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "7_U1SXmzpCWu"
},
"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 (without transfer learning).\n",
"\n",
"Run all the cells. After executing the last cell, you will get the accuracy of the trained model.\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",
" 4. You should have access to the file in the Google Drive.\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "-4cX-Q9fohCP"
},
"source": [
"# **Import Modules**"
]
},
{
"cell_type": "code",
"metadata": {
"id": "4SwdSePtn_L1"
},
"source": [
"# Import Resnet50 model\n",
"from keras.applications.resnet import ResNet50\n",
"\n",
"# Import ImageDataGenerator to generate train and test batches\n",
"from keras.preprocessing.image import ImageDataGenerator \n",
"\n",
"# Import Model to create the final model\n",
"from keras.models import Model\n",
"\n",
"# Import keras layers\n",
"from keras.layers import Dense,GlobalAveragePooling2D\n",
"\n",
"# Import adam optimizer\n",
"from tensorflow.keras.optimizers import Adam\n",
"\n",
"# Import preprocess_input for ImageDataGenerator class\n",
"from keras.applications.resnet_v2 import preprocess_input\n",
"\n",
"# Import gdown module to download files from google drive\n",
"import gdown\n",
"\n",
"# Import zip file module to open the zip file\n",
"from zipfile import ZipFile\n"
],
"execution_count": 1,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "ur1kfcbQoqYu"
},
"source": [
"## **Get the file location from google drive and download** **bold text**"
]
},
{
"cell_type": "code",
"metadata": {
"id": "0U1NBU1kor_e"
},
"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/1fMHrqIY0QYEj9qFUFsDuF949Jo-UWzVX/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 the file from drive to your local machine\n",
"gdown.download(download_url, file_location, quiet=False)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "xzaZ2Akpo0mn"
},
"source": [
"# **Unzip the zip dataset**"
]
},
{
"cell_type": "code",
"metadata": {
"id": "C01agt3Io1tK"
},
"source": [
"!unzip /content/cats_and_dogs.zip -d \"/content/unzipped_folder/\""
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "hKlXid56o4va"
},
"source": [
"# **Start the prediction operation**"
]
},
{
"cell_type": "code",
"metadata": {
"id": "2Cf0BqnNo_UK"
},
"source": [
"# Give image size and shape\n",
"IMG_SIZE = 224\n",
"IMG_SHAPE = (IMG_SIZE, IMG_SIZE, 3)\n",
"\n",
"# Define the number of classes\n",
"num_classes = 2\n",
"\n",
"# Define train path and test path\n",
"train_path = '/content/unzipped_folder/training_set/training_set'\n",
"test_path = '/content/unzipped_folder/test_set/test_set'\n",
"\n",
"# Go through the train directory to obtain cateogries\n",
"train_batches = ImageDataGenerator(preprocessing_function=preprocess_input).flow_from_directory(\n",
" train_path ,target_size=(IMG_SIZE,IMG_SIZE),batch_size=24,class_mode='categorical')\n",
"\n",
"# Go through the test directory to obtain cateogries\n",
"test_batches = ImageDataGenerator(preprocessing_function=preprocess_input).flow_from_directory(\n",
" test_path ,target_size=(IMG_SIZE,IMG_SIZE),batch_size=24,class_mode='categorical')\n",
"\n",
"# Derive the resnet pretrained model without weights\n",
"base_model = ResNet50(weights= None, include_top=False, input_shape=IMG_SHAPE)\n",
"\n",
"# Get the last layer and add a few extra layers to it\n",
"x = base_model.output\n",
"x = GlobalAveragePooling2D()(x)\n",
"\n",
"# Add a layer with softmax activation\n",
"predictions = Dense(num_classes, activation= 'softmax')(x)\n",
"\n",
"# Get the final model\n",
"model = Model(inputs = base_model.input, outputs = predictions)\n",
"\n",
"# Define the base learning rate\n",
"base_learning_rate = 0.0001\n",
"\n",
"# Use Adam optimizer as the optimizer of the model\n",
"adam = Adam(lr=base_learning_rate)\n",
"\n",
"# Loss function IS categorical cross entropy\n",
"# Accuracy is the evaluation metric\n",
"model.compile(optimizer= adam, loss='categorical_crossentropy', metrics=['accuracy'])\n",
"\n",
"# Fit the training data\n",
"model.fit(train_batches, steps_per_epoch=2, epochs=2, validation_data=test_batches)\n",
"\n",
"# Evaluate the model\n",
"preds = model.evaluate(test_batches, steps = 20)\n",
"\n",
"# Print the accuracy of the model\n",
"print (\"Accuracy = \" + str(preds[1]))"
],
"execution_count": null,
"outputs": []
}
]
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,117 @@
# TODO: Create a Residual Neural Network in tensorflow. Train on cats and dogs dataset. (no 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 model
from keras.applications.resnet import ResNet50

# Import ImageDataGenerator to generate train and test batches
from keras.preprocessing.image import ImageDataGenerator

# Import Model to create the final model
from keras.models import Model

# Import keras layers
from keras.layers import Dense,GlobalAveragePooling2D

# Import adam optimizer
from tensorflow.keras.optimizers import Adam

# Import preprocess_input for ImageDataGenerator class
from keras.applications.resnet_v2 import preprocess_input

# 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/1fMHrqIY0QYEj9qFUFsDuF949Jo-UWzVX/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()
# Read train and test datasets
train_path = r"test_set\test_set"
test_path = r"training_set\training_set"

#--------------------------------------------- Begin the training operation using Resnet ----------------------------------------------

# Give image size and shape
IMG_SIZE = 224
IMG_SHAPE = (IMG_SIZE, IMG_SIZE, 3)

# Define the number of classes
num_classes = 2

# Go through the train directory to obtain cateogries
train_batches = ImageDataGenerator(preprocessing_function=preprocess_input).flow_from_directory(
train_path ,target_size=(IMG_SIZE,IMG_SIZE),batch_size=24,class_mode='categorical')

# Go through the test directory to obtain cateogries
test_batches = ImageDataGenerator(preprocessing_function=preprocess_input).flow_from_directory(
test_path ,target_size=(IMG_SIZE,IMG_SIZE),batch_size=24,class_mode='categorical')

# Derive the resnet pretrained model without weights
base_model = ResNet50(weights= None, include_top=False, input_shape=IMG_SHAPE)

# Get the last layer and add a few extra layers to it
x = base_model.output
x = GlobalAveragePooling2D()(x)

# Add a layer with softmax activation
predictions = Dense(num_classes, activation= 'softmax')(x)

# Get the final model
model = Model(inputs = base_model.input, outputs = predictions)

# Define the base learning rate
base_learning_rate = 0.0001

# Use Adam optimizer as the optimizer of the model
adam = Adam(lr=base_learning_rate)

# Loss function IS categorical cross entropy
# Accuracy is the evaluation metric
# Compile the model
model.compile(optimizer= adam, loss='categorical_crossentropy', metrics=['accuracy'])

# Fit the training data
model.fit(train_batches, steps_per_epoch=2, epochs=2, validation_data=test_batches)

# Evaluate the model
preds = model.evaluate(test_batches, steps = 20)

# Print the accuracy
print ("Accuracy = " + str(preds[1]))