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/MobileNetV2/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 MobileNetV2 in tensorflow (without using transfer learning).

Dependancies :

1. Tensorflow module is needed to be installed.
2. Zip file module is needed to be installed.
3. gdown module is needed to be installed.

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,264 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Copy of mobileNetV2_without_transfer_learning.ipynb",
"provenance": [],
"collapsed_sections": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "zkMBooK6itVx"
},
"source": [
"# **Problem: MobileNetV2 in tensorflow. Train on cats and dogs dataset.**\n",
"\n",
"Python program to train cats and dogs dataset using MobileNetV2 in tensorflow (without using 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. Tensorflow module is needed to be installed.\n",
" 2. Zip file module is needed to be installed.\n",
" 3. gdown module is needed to be installed.\n",
" 4. 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": "d9v7TRDIkMmm"
},
"source": [
"# **Import Modules**"
]
},
{
"cell_type": "code",
"metadata": {
"id": "pThT1tW3kPHo"
},
"source": [
"# Import tensorflow\n",
"import tensorflow as tf\n",
"\n",
"# Import keras\n",
"from tensorflow import keras\n",
"\n",
"# Import preprocess_input for ImageDataGenerator class\n",
"from keras.applications.mobilenet import preprocess_input\n",
"\n",
"# Import ImageDataGenerator class for the Data Augmentation\n",
"from keras.preprocessing.image import ImageDataGenerator\n",
"\n",
"# Import gdown module to download files from google drive\n",
"import gdown"
],
"execution_count": 1,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "_eQMxp_RqAXm"
},
"source": [
"# **Get the file location from google drive**"
]
},
{
"cell_type": "code",
"metadata": {
"id": "1szy5fTHp-tZ"
},
"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 = r'cats_and_dogs.zip'\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": "Jjtzusk3qj2X"
},
"source": [
"# **Unzip the zip dataset**"
]
},
{
"cell_type": "code",
"metadata": {
"id": "6bY4IAylqnla"
},
"source": [
"!unzip /content/cats_and_dogs.zip -d \"/content/unzipped_folder/\""
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "K-d0fcsDkUB9"
},
"source": [
"# **Construct train and test datasets**"
]
},
{
"cell_type": "code",
"metadata": {
"id": "v1JaL8hakbOu",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "c8dc7e40-5fd2-42e5-bc0f-82f9a8ac4041"
},
"source": [
"train_path = r\"/content/unzipped_folder/test_set/test_set\"\n",
"test_path = r\"/content/unzipped_folder/training_set/training_set\"\n",
"\n",
"# Give image size and shape\n",
"IMG_SIZE = 224\n",
"IMG_SHAPE = (IMG_SIZE, IMG_SIZE, 3)\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')"
],
"execution_count": 4,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Found 55 images belonging to 2 classes.\n",
"Found 206 images belonging to 2 classes.\n"
]
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "YMnOflqRk2F0"
},
"source": [
"# **Derive the model using MobileNetV2**"
]
},
{
"cell_type": "code",
"metadata": {
"id": "CQUevpmak6JH"
},
"source": [
"# Get the base model using MobileNetV2 without a pre trained moded(weight = None)\n",
"# Do not make trainable as False (Keras is trainable is true by default)\n",
"base_model = keras.applications.MobileNetV2(input_shape=IMG_SHAPE,include_top=False,weights=None)\n",
"\n",
"feature_batch = base_model.output\n",
"global_average_layer = keras.layers.GlobalAveragePooling2D()\n",
"feature_batch_average = global_average_layer(feature_batch)\n",
"prediction_layer = keras.layers.Dense(2)\n",
"prediction_batch = prediction_layer(feature_batch_average)\n",
"\n",
"# Group layers into a keras model\n",
"model = keras.Sequential([\n",
" base_model,\n",
" global_average_layer,\n",
" prediction_layer\n",
"])"
],
"execution_count": 5,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "yC-rnheIk8nT"
},
"source": [
"# **Compile and train the model**"
]
},
{
"cell_type": "code",
"metadata": {
"id": "GacazgGelBen"
},
"source": [
"# Complile the model\n",
"model.compile(optimizer=tf.keras.optimizers.RMSprop(),\n",
" loss='binary_crossentropy',\n",
" metrics=['accuracy'])\n",
"\n",
"# Train the model\n",
"history = model.fit(train_batches,\n",
" steps_per_epoch=24,\n",
" epochs=1, #<-- increase for higher accuracy\n",
" validation_data=test_batches,\n",
" validation_steps=200)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "8N5VvKeslDcL"
},
"source": [
"# **Get accuracy of the model**"
]
},
{
"cell_type": "code",
"metadata": {
"id": "bHLNH7W0lIcL"
},
"source": [
"# Get the accuracy\n",
"loss,accuracy = model.evaluate(test_batches, steps = 20)\n",
"\n",
"print('Accuracy :', accuracy)"
],
"execution_count": null,
"outputs": []
}
]
}
Loading