{"metadata":{"accelerator":"GPU","colab":{"collapsed_sections":[],"provenance":[],"mount_file_id":"1TOoDMcrkkkssw6IBLzunINqd9wpc2QYY","authorship_tag":"ABX9TyOjTPKiYG3hwoaO/SRCta2l"},"gpuClass":"standard","kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":7327,"databundleVersionId":861871,"sourceType":"competition"}],"dockerImageVersionId":30588,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# 🐶 End to End Multi class Dog Breed Identification\n\n## 1. Problem \nIdentifing the breed of a dog given an image of a dog.\n\n## 2. Data \nfrom kaggle,\ndata :- https://www.kaggle.com/c dog-breed-identification\n\n## 3. Evaluation\nThe evaluation is a file with prediction probabilities for each dod breed of each test image \nhttps://www.kaggle.com/competitions/dog-breed-identification/overview/evaluation\n\n\n## 4. Fautures \nSome information about the data:\n* We are dealing with images (unstructured data ) so it's probably best we use deep learning/transfer learning.\n* There  are 120 breeds of dogs (this means there are 120 differnt cases).\n* There are more than 10,000+ images in training set\n* There are around 10,000+ images in the test set (these images have no labels, because, it will be predicted)","metadata":{"id":"00ztuwiuC0UV"}},{"cell_type":"markdown","source":"# Get our workspace ready\n* Import TensorFlow 2.x\n* Import TensorFlow Hub\n* Make sure we're using a GPU","metadata":{"id":"1AyJmXMJHYqP"}},{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        os.path.join(dirname, filename)\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"execution":{"iopub.status.busy":"2024-01-17T06:28:35.009232Z","iopub.execute_input":"2024-01-17T06:28:35.01015Z","iopub.status.idle":"2024-01-17T06:29:01.28706Z","shell.execute_reply.started":"2024-01-17T06:28:35.010118Z","shell.execute_reply":"2024-01-17T06:29:01.286081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2024-01-17T06:29:01.28874Z","iopub.execute_input":"2024-01-17T06:29:01.289155Z","iopub.status.idle":"2024-01-17T06:29:01.293571Z","shell.execute_reply.started":"2024-01-17T06:29:01.289128Z","shell.execute_reply":"2024-01-17T06:29:01.292616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# importing nessasary tools\nimport tensorflow_hub as hub\nimport tensorflow as tf\nprint(\"TF version : \", tf.__version__)\nprint(\"TF Hub version : \", hub.__version__)\n\n# Checking for GPU avaiability - meaning superfast numeric computing\nprint(\"GPU\", \"avilable \" if tf.config.list_physical_devices(\"GPU\") else \"not available\") \n","metadata":{"executionInfo":{"elapsed":3082,"status":"ok","timestamp":1668083831025,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"},"user_tz":-330},"id":"Ekc4UNp4cE1b","outputId":"3cdc9d27-c0df-4c9c-ba3f-e373ddc922c2","execution":{"iopub.status.busy":"2024-01-17T06:29:01.294874Z","iopub.execute_input":"2024-01-17T06:29:01.295153Z","iopub.status.idle":"2024-01-17T06:29:14.42168Z","shell.execute_reply.started":"2024-01-17T06:29:01.295129Z","shell.execute_reply":"2024-01-17T06:29:14.420462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Getting our data ready (turning into Tensors)\n\nWith all machine learning models, our data has to be in numeric format. So thats turning images to Tensors (numeric representation )\n\nLet's start by checking labels ","metadata":{"id":"YyamQ0dpHU1a"}},{"cell_type":"code","source":"# Checkout the labels of data\nimport pandas as pd\nlabels_csv = pd.read_csv(\"/kaggle/input/dog-breed-identification/labels.csv\")\nprint(labels_csv.head())\nprint(labels_csv.describe())","metadata":{"executionInfo":{"elapsed":1274,"status":"ok","timestamp":1668083846034,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"},"user_tz":-330},"id":"M-hZGIzZOJZP","outputId":"ebed8782-2193-4808-f5c5-b01d965d172f","execution":{"iopub.status.busy":"2024-01-17T06:29:14.424099Z","iopub.execute_input":"2024-01-17T06:29:14.424755Z","iopub.status.idle":"2024-01-17T06:29:14.50455Z","shell.execute_reply.started":"2024-01-17T06:29:14.424722Z","shell.execute_reply":"2024-01-17T06:29:14.503424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_csv.head()","metadata":{"executionInfo":{"elapsed":39,"status":"ok","timestamp":1668083846043,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"},"user_tz":-330},"id":"BSrg8wAdOtz6","outputId":"7947e228-e159-4ff1-d9e4-08eac1203506","execution":{"iopub.status.busy":"2024-01-17T06:29:14.505912Z","iopub.execute_input":"2024-01-17T06:29:14.506272Z","iopub.status.idle":"2024-01-17T06:29:14.520763Z","shell.execute_reply.started":"2024-01-17T06:29:14.506245Z","shell.execute_reply":"2024-01-17T06:29:14.519705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(labels_csv)","metadata":{"executionInfo":{"elapsed":36,"status":"ok","timestamp":1668083846045,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"},"user_tz":-330},"id":"iRhcueeEPeIM","outputId":"8c8cb635-4e38-48ff-f2cf-3137a3d43bcd","execution":{"iopub.status.busy":"2024-01-17T06:29:14.522238Z","iopub.execute_input":"2024-01-17T06:29:14.522636Z","iopub.status.idle":"2024-01-17T06:29:14.529452Z","shell.execute_reply.started":"2024-01-17T06:29:14.522599Z","shell.execute_reply":"2024-01-17T06:29:14.528485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" # How many images are there of each breed?\n labels_csv[\"breed\"].value_counts().plot(kind='bar',figsize=(20, 10))","metadata":{"executionInfo":{"elapsed":1889,"status":"ok","timestamp":1668083847905,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"},"user_tz":-330},"id":"uxDYC_2yO-sK","outputId":"05c1ef3e-56f3-4309-f60d-ff62441ebbf9","execution":{"iopub.status.busy":"2024-01-17T06:29:14.531079Z","iopub.execute_input":"2024-01-17T06:29:14.531432Z","iopub.status.idle":"2024-01-17T06:29:16.419395Z","shell.execute_reply.started":"2024-01-17T06:29:14.531404Z","shell.execute_reply":"2024-01-17T06:29:16.418351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_csv[\"breed\"].value_counts()","metadata":{"executionInfo":{"elapsed":42,"status":"ok","timestamp":1668083847912,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"},"user_tz":-330},"id":"MRmcAzvVQVUk","outputId":"1e8dc250-612b-4fc0-ab5d-0c74d72c5493","execution":{"iopub.status.busy":"2024-01-17T06:29:16.420597Z","iopub.execute_input":"2024-01-17T06:29:16.420896Z","iopub.status.idle":"2024-01-17T06:29:16.431738Z","shell.execute_reply.started":"2024-01-17T06:29:16.420872Z","shell.execute_reply":"2024-01-17T06:29:16.43071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_csv[\"breed\"].value_counts().median()","metadata":{"executionInfo":{"elapsed":45,"status":"ok","timestamp":1668083847924,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"},"user_tz":-330},"id":"Jjou-cIQPTwT","outputId":"e2edb18f-d7e6-44fb-846d-c89c3e666901","execution":{"iopub.status.busy":"2024-01-17T06:29:16.433217Z","iopub.execute_input":"2024-01-17T06:29:16.433627Z","iopub.status.idle":"2024-01-17T06:29:16.448073Z","shell.execute_reply.started":"2024-01-17T06:29:16.433593Z","shell.execute_reply":"2024-01-17T06:29:16.447202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Let's view an image\nfrom IPython.display import Image\nImage(\"/kaggle/input/dog-breed-identification/train/000bec180eb18c7604dcecc8fe0dba07.jpg\")","metadata":{"executionInfo":{"elapsed":44205,"status":"ok","timestamp":1668083892098,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"},"user_tz":-330},"id":"UAKxTK43Q0oW","outputId":"356ae64a-e9da-443e-fdcb-acbcb9d5be7c","execution":{"iopub.status.busy":"2024-01-17T06:53:31.560392Z","iopub.execute_input":"2024-01-17T06:53:31.561467Z","iopub.status.idle":"2024-01-17T06:53:31.571463Z","shell.execute_reply.started":"2024-01-17T06:53:31.56143Z","shell.execute_reply":"2024-01-17T06:53:31.570363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Geting images and their labels\n\nLet's get a list of all of our image file pathname.","metadata":{"id":"kgnNHOPpcew8"}},{"cell_type":"code","source":"labels_csv.head()","metadata":{"executionInfo":{"elapsed":63,"status":"ok","timestamp":1668083892100,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"},"user_tz":-330},"id":"UqUHai6hdFfK","outputId":"8abc976b-42dc-4c87-c9fb-3cbd80395f0e","execution":{"iopub.status.busy":"2024-01-17T06:29:17.371874Z","iopub.execute_input":"2024-01-17T06:29:17.372328Z","iopub.status.idle":"2024-01-17T06:29:17.384415Z","shell.execute_reply.started":"2024-01-17T06:29:17.372279Z","shell.execute_reply":"2024-01-17T06:29:17.383263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create pathname from ID's\nfilenames = [fname for fname in labels_csv[\"id\"]]\n\n# Create pathname from image ID's\nfilenames = [\"/kaggle/input/dog-breed-identification/train/\" + fname + \".jpg\" for fname in labels_csv[\"id\"]]\n\n# Check the first 10\nfilenames[:5]\n","metadata":{"executionInfo":{"elapsed":61,"status":"ok","timestamp":1668083892101,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"},"user_tz":-330},"id":"VtBlw8WQdBhH","outputId":"a97d4a35-f432-4d7c-a6f3-9381e419bd9c","execution":{"iopub.status.busy":"2024-01-17T06:29:17.385762Z","iopub.execute_input":"2024-01-17T06:29:17.386077Z","iopub.status.idle":"2024-01-17T06:29:17.40205Z","shell.execute_reply.started":"2024-01-17T06:29:17.38605Z","shell.execute_reply":"2024-01-17T06:29:17.400912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Check whether number of filenames matches number of actual image files\nimport os\nif len(os.listdir(\"/kaggle/input/dog-breed-identification/train\")) == len(filenames):\n  print(\"Filenames maths actual amount of data!!! Proceed\")\nelse: \n  print(\"Filenames do not match actual amount of files, check the target directory\")","metadata":{"executionInfo":{"elapsed":47,"status":"ok","timestamp":1668083892108,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"},"user_tz":-330},"id":"0aJ-RmeRdhXy","outputId":"140a6c55-ea21-4790-8562-943227c3d861","execution":{"iopub.status.busy":"2024-01-17T06:29:17.403382Z","iopub.execute_input":"2024-01-17T06:29:17.404427Z","iopub.status.idle":"2024-01-17T06:29:17.417454Z","shell.execute_reply.started":"2024-01-17T06:29:17.40439Z","shell.execute_reply":"2024-01-17T06:29:17.416243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filenames[9000]","metadata":{"execution":{"iopub.status.busy":"2024-01-17T06:29:17.418656Z","iopub.execute_input":"2024-01-17T06:29:17.418993Z","iopub.status.idle":"2024-01-17T06:29:17.425869Z","shell.execute_reply.started":"2024-01-17T06:29:17.418967Z","shell.execute_reply":"2024-01-17T06:29:17.424774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# One more check \nImage(filenames[9000])","metadata":{"executionInfo":{"elapsed":1996,"status":"ok","timestamp":1668083894073,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"},"user_tz":-330},"id":"L6VQVcC1fvO3","outputId":"ecde2176-b94a-4f13-f3ac-862dea131ad9","execution":{"iopub.status.busy":"2024-01-17T06:29:17.427348Z","iopub.execute_input":"2024-01-17T06:29:17.428Z","iopub.status.idle":"2024-01-17T06:29:17.44401Z","shell.execute_reply.started":"2024-01-17T06:29:17.427963Z","shell.execute_reply":"2024-01-17T06:29:17.443046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_csv[\"breed\"][9000]","metadata":{"executionInfo":{"elapsed":220,"status":"ok","timestamp":1668083894075,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"},"user_tz":-330},"id":"Zig3USvuggwD","outputId":"6618f304-90c9-47fc-dc8a-4db183cbf7f2","execution":{"iopub.status.busy":"2024-01-17T06:29:17.445332Z","iopub.execute_input":"2024-01-17T06:29:17.445649Z","iopub.status.idle":"2024-01-17T06:29:17.452027Z","shell.execute_reply.started":"2024-01-17T06:29:17.445623Z","shell.execute_reply":"2024-01-17T06:29:17.451042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Preparing labels","metadata":{"id":"ujX_vKRdgrkE"}},{"cell_type":"code","source":"import numpy as np\nlabels = labels_csv[\"breed\"]\nlabels = np.array(labels)\nlabels","metadata":{"executionInfo":{"elapsed":213,"status":"ok","timestamp":1668083894077,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"},"user_tz":-330},"id":"6lA1Tuh-g5MP","outputId":"d3a5e026-971c-4403-8b73-b62fb533bdc0","execution":{"iopub.status.busy":"2024-01-17T06:29:17.453423Z","iopub.execute_input":"2024-01-17T06:29:17.453823Z","iopub.status.idle":"2024-01-17T06:29:17.463022Z","shell.execute_reply.started":"2024-01-17T06:29:17.453788Z","shell.execute_reply":"2024-01-17T06:29:17.462075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(labels)","metadata":{"executionInfo":{"elapsed":199,"status":"ok","timestamp":1668083894080,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"},"user_tz":-330},"id":"PHz3l89ohI6g","outputId":"cb066996-2f43-4d5b-94fe-c0135820176b","execution":{"iopub.status.busy":"2024-01-17T06:29:17.464339Z","iopub.execute_input":"2024-01-17T06:29:17.464899Z","iopub.status.idle":"2024-01-17T06:29:17.473382Z","shell.execute_reply.started":"2024-01-17T06:29:17.464854Z","shell.execute_reply":"2024-01-17T06:29:17.472406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# See if number of labels matches the number of filenames\nif len(labels) == len(filenames):\n  print(\"Number of filenames matches number of labels\")\nelse:\n  print(\"Number of filenames do not match number of labels, check directories\")","metadata":{"executionInfo":{"elapsed":192,"status":"ok","timestamp":1668083894084,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"},"user_tz":-330},"id":"40za3TuUhNC_","outputId":"b6732c85-5c6b-42b5-afc6-5dc523b784e5","execution":{"iopub.status.busy":"2024-01-17T06:29:17.474775Z","iopub.execute_input":"2024-01-17T06:29:17.475086Z","iopub.status.idle":"2024-01-17T06:29:17.483462Z","shell.execute_reply.started":"2024-01-17T06:29:17.475059Z","shell.execute_reply":"2024-01-17T06:29:17.48248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Find the unique label values\nunique_breeds = np.unique(labels)\nlen(unique_breeds)","metadata":{"executionInfo":{"elapsed":181,"status":"ok","timestamp":1668083894085,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"},"user_tz":-330},"id":"SfOw0h0zhitX","outputId":"21d8dab6-ba18-45a8-b43d-c438157bbccb","execution":{"iopub.status.busy":"2024-01-17T06:29:17.484706Z","iopub.execute_input":"2024-01-17T06:29:17.484989Z","iopub.status.idle":"2024-01-17T06:29:17.50517Z","shell.execute_reply.started":"2024-01-17T06:29:17.484965Z","shell.execute_reply":"2024-01-17T06:29:17.504162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Turn a single label into a array of booleans\nlabels[0] == unique_breeds","metadata":{"executionInfo":{"elapsed":174,"status":"ok","timestamp":1668083894087,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"},"user_tz":-330},"id":"R8yYw6TRkaL2","outputId":"30615fc0-6008-470e-866a-ec05dcb83aa2","execution":{"iopub.status.busy":"2024-01-17T06:29:17.506434Z","iopub.execute_input":"2024-01-17T06:29:17.506757Z","iopub.status.idle":"2024-01-17T06:29:17.51586Z","shell.execute_reply.started":"2024-01-17T06:29:17.506733Z","shell.execute_reply":"2024-01-17T06:29:17.514854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Turn every label into a boolean array\nboolean_labels = [label == unique_breeds for label in labels]\n\nboolean_labels[:2]","metadata":{"executionInfo":{"elapsed":167,"status":"ok","timestamp":1668083894090,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"},"user_tz":-330},"id":"AJlszEcNkydb","outputId":"76ad0c04-cd18-44a4-b95a-23dfcaf41732","execution":{"iopub.status.busy":"2024-01-17T06:29:17.517112Z","iopub.execute_input":"2024-01-17T06:29:17.517395Z","iopub.status.idle":"2024-01-17T06:29:17.621959Z","shell.execute_reply.started":"2024-01-17T06:29:17.51737Z","shell.execute_reply":"2024-01-17T06:29:17.620931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(boolean_labels), len(labels)","metadata":{"executionInfo":{"elapsed":157,"status":"ok","timestamp":1668083894091,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"},"user_tz":-330},"id":"J4KDB0lmlN7Q","outputId":"b9f676c8-728f-4cc3-a50e-13049a3a464a","execution":{"iopub.status.busy":"2024-01-17T06:29:17.623119Z","iopub.execute_input":"2024-01-17T06:29:17.623418Z","iopub.status.idle":"2024-01-17T06:29:17.629905Z","shell.execute_reply.started":"2024-01-17T06:29:17.623393Z","shell.execute_reply":"2024-01-17T06:29:17.628847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Example : Turning boolean array into integers\nprint(labels[0]) # Orignal breeds\nprint(np.where(unique_breeds == labels[0])) # index where label ocurs\nprint(boolean_labels[0].argmax()) # index where label occurs in boolean array\nprint(boolean_labels[0].astype(int)) # there will be a 1 where the sample label occurs","metadata":{"executionInfo":{"elapsed":148,"status":"ok","timestamp":1668083894093,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"},"user_tz":-330},"id":"dv9cExg2qGW-","outputId":"9373edee-fea7-4427-b2e4-5a37c9617c6b","execution":{"iopub.status.busy":"2024-01-17T06:29:17.631176Z","iopub.execute_input":"2024-01-17T06:29:17.631548Z","iopub.status.idle":"2024-01-17T06:29:17.641282Z","shell.execute_reply.started":"2024-01-17T06:29:17.631493Z","shell.execute_reply":"2024-01-17T06:29:17.639954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filenames[:5]","metadata":{"executionInfo":{"elapsed":140,"status":"ok","timestamp":1668083894096,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"},"user_tz":-330},"id":"2dZ-odLHrWzC","outputId":"b691dbc4-4e57-40a9-fb63-fea9f362a64b","execution":{"iopub.status.busy":"2024-01-17T06:29:17.642636Z","iopub.execute_input":"2024-01-17T06:29:17.64301Z","iopub.status.idle":"2024-01-17T06:29:17.656659Z","shell.execute_reply.started":"2024-01-17T06:29:17.642975Z","shell.execute_reply":"2024-01-17T06:29:17.655608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Creating validation set\n","metadata":{"id":"Pqmnkh1xrvqs"}},{"cell_type":"code","source":"# Setup X, y variables\nX = filenames\ny = boolean_labels","metadata":{"executionInfo":{"elapsed":129,"status":"ok","timestamp":1668083894098,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"},"user_tz":-330},"id":"U6GMD6sSr2ya","execution":{"iopub.status.busy":"2024-01-17T06:29:17.657808Z","iopub.execute_input":"2024-01-17T06:29:17.658127Z","iopub.status.idle":"2024-01-17T06:29:17.665713Z","shell.execute_reply.started":"2024-01-17T06:29:17.658102Z","shell.execute_reply":"2024-01-17T06:29:17.66477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(filenames)","metadata":{"executionInfo":{"elapsed":130,"status":"ok","timestamp":1668083894100,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"},"user_tz":-330},"id":"zzYYJDMksHid","outputId":"afc67a25-e0f0-41cd-d5ca-1a63dcc8185a","execution":{"iopub.status.busy":"2024-01-17T06:29:17.666989Z","iopub.execute_input":"2024-01-17T06:29:17.667404Z","iopub.status.idle":"2024-01-17T06:29:17.678884Z","shell.execute_reply.started":"2024-01-17T06:29:17.66737Z","shell.execute_reply":"2024-01-17T06:29:17.677812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Experimenting with ~1000 images","metadata":{"id":"3F0FfuMAsJZv"}},{"cell_type":"code","source":"# Set number of images to use for experimenting\nNUM_IMAGES = 1000 #@param {type : \"slider\", min:1000, max:10000, step:1000}","metadata":{"executionInfo":{"elapsed":120,"status":"ok","timestamp":1668083894102,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"},"user_tz":-330},"id":"KsM9T5_JsZvt","execution":{"iopub.status.busy":"2024-01-17T06:29:17.687101Z","iopub.execute_input":"2024-01-17T06:29:17.687421Z","iopub.status.idle":"2024-01-17T06:29:17.692026Z","shell.execute_reply.started":"2024-01-17T06:29:17.687394Z","shell.execute_reply":"2024-01-17T06:29:17.691008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Spliting data into training and validation set\nfrom sklearn.model_selection import train_test_split\n\n# Spliting data into training and validation of total size NUM_IMAGES\nX_train, X_val, y_train, y_val = train_test_split(X[:NUM_IMAGES],\n                                                    y[:NUM_IMAGES],\n                                                    test_size=0.2,\n                                                    random_state=42)\n\nlen(X_train), len(y_train), len(X_val), len(y_val)","metadata":{"executionInfo":{"elapsed":119,"status":"ok","timestamp":1668083894104,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"},"user_tz":-330},"id":"rmcHApJgs2GV","outputId":"5d052841-de70-4cb1-e607-a49a1595eb9c","execution":{"iopub.status.busy":"2024-01-17T06:29:17.693401Z","iopub.execute_input":"2024-01-17T06:29:17.693754Z","iopub.status.idle":"2024-01-17T06:29:18.113272Z","shell.execute_reply.started":"2024-01-17T06:29:17.693727Z","shell.execute_reply":"2024-01-17T06:29:18.1121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train[:2], y_train[:2]","metadata":{"executionInfo":{"elapsed":110,"status":"ok","timestamp":1668083894106,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"},"user_tz":-330},"id":"tHQsW6tVuAY8","outputId":"a4ae2415-4676-4042-fbc6-c95af1290141","execution":{"iopub.status.busy":"2024-01-17T06:29:18.114631Z","iopub.execute_input":"2024-01-17T06:29:18.114977Z","iopub.status.idle":"2024-01-17T06:29:18.123293Z","shell.execute_reply.started":"2024-01-17T06:29:18.114948Z","shell.execute_reply":"2024-01-17T06:29:18.122285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Preprocessign Images (turning images into Tensors)\n\nTo preprocess our images into Tensors we're going to write a function which does few things\n1. Take an image filepath as input\n2. Use Tensorflow to read the file and save it to a vaiable, image\n3. Turn our imafe (a jpg) into Tensors\n4. Normalize image (convert color channel values from 0-255 to 0-1 )\n5. Resize the image to be shape of (224, 224)\n6. Return the modified image","metadata":{"id":"cS7l8Fxou8sz"}},{"cell_type":"code","source":"# Convert a image to Nimpy arrray\nfrom matplotlib.pyplot import imread\nimage = imread(filenames[42])\nimage.shape","metadata":{"executionInfo":{"elapsed":100,"status":"ok","timestamp":1668083894107,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"},"user_tz":-330},"id":"k39Ut4-PvN_P","outputId":"3df59c16-6fde-4b74-bfff-e607a9f6bf85","execution":{"iopub.status.busy":"2024-01-17T06:29:18.124624Z","iopub.execute_input":"2024-01-17T06:29:18.124955Z","iopub.status.idle":"2024-01-17T06:29:18.145627Z","shell.execute_reply.started":"2024-01-17T06:29:18.124929Z","shell.execute_reply":"2024-01-17T06:29:18.144548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image.shape","metadata":{"executionInfo":{"elapsed":90,"status":"ok","timestamp":1668083894109,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"},"user_tz":-330},"id":"0aFmA12Uz9b5","outputId":"2db0b01f-7060-4be7-a55f-a1624bef1537","execution":{"iopub.status.busy":"2024-01-17T06:29:18.146773Z","iopub.execute_input":"2024-01-17T06:29:18.147081Z","iopub.status.idle":"2024-01-17T06:29:18.156032Z","shell.execute_reply.started":"2024-01-17T06:29:18.147055Z","shell.execute_reply":"2024-01-17T06:29:18.154829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image.min(), image.max()","metadata":{"executionInfo":{"elapsed":878,"status":"ok","timestamp":1668083894913,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"},"user_tz":-330},"id":"b99xi2-F0GMj","outputId":"804e645c-f890-4634-9c42-208218c0ceb9","execution":{"iopub.status.busy":"2024-01-17T06:29:18.157584Z","iopub.execute_input":"2024-01-17T06:29:18.15793Z","iopub.status.idle":"2024-01-17T06:29:18.166024Z","shell.execute_reply.started":"2024-01-17T06:29:18.157903Z","shell.execute_reply":"2024-01-17T06:29:18.164808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Turning image into a tensor\ntf.constant(image)[:2].shape","metadata":{"executionInfo":{"elapsed":149,"status":"ok","timestamp":1668083896248,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"},"user_tz":-330},"id":"q50HZawp0LaM","outputId":"ff8ef2ba-c3ea-4948-fd91-ef50070d406e","execution":{"iopub.status.busy":"2024-01-17T06:29:18.167235Z","iopub.execute_input":"2024-01-17T06:29:18.167604Z","iopub.status.idle":"2024-01-17T06:29:18.708631Z","shell.execute_reply.started":"2024-01-17T06:29:18.167575Z","shell.execute_reply":"2024-01-17T06:29:18.707569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Making function \n1. Take an image filepath as input\n2. Use Tensorflow to read the file and save it to a vaiable, image\n3. Turn our imafe (a jpg) into Tensors\n4. Normalize image (convert color channel values from 0-255 to 0-1 )\n5. Resize the image to be shape of (224, 224)\n6. Return the modified image","metadata":{"id":"r2mEklpG0Q47"}},{"cell_type":"code","source":"# Define image size\nIMG_SIZE = 224\n\n# Create a function for preprocessing images\ndef process_image(image_path):\n  \"\"\"\n  Take an image file and turns the image into a Tensor\n  \"\"\"\n  # Read in an imafe file\n  image = tf.io.read_file(image_path)\n\n  # Turn the jpeg image into numerical Tensor with 3 color chennels (Red, Gree, Blue)\n  image = tf.image.decode_jpeg(image, channels=3)\n\n  # Convert the color channel values from 0-255 to 0-1 values\n  image = tf.image.convert_image_dtype(image, tf.float32)\n\n  # Resize image to desired value (224, 224)\n  image = tf.image.resize(image, size=[IMG_SIZE, IMG_SIZE])\n\n  return image","metadata":{"executionInfo":{"elapsed":131,"status":"ok","timestamp":1668083896249,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"},"user_tz":-330},"id":"ql-_dcTc0pD-","execution":{"iopub.status.busy":"2024-01-17T06:29:18.709816Z","iopub.execute_input":"2024-01-17T06:29:18.710133Z","iopub.status.idle":"2024-01-17T06:29:18.717711Z","shell.execute_reply.started":"2024-01-17T06:29:18.710107Z","shell.execute_reply":"2024-01-17T06:29:18.716687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tensor = tf.io.read_file(filenames[26])\n# tensor","metadata":{"executionInfo":{"elapsed":133,"status":"ok","timestamp":1668083896252,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"},"user_tz":-330},"id":"L6WbZEID069e","outputId":"ab683f6a-8f1b-4b8a-fc29-d9422104d855","execution":{"iopub.status.busy":"2024-01-17T06:29:18.719156Z","iopub.execute_input":"2024-01-17T06:29:18.719607Z","iopub.status.idle":"2024-01-17T06:29:18.740486Z","shell.execute_reply.started":"2024-01-17T06:29:18.719557Z","shell.execute_reply":"2024-01-17T06:29:18.739619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tensor = tf.image.decode_jpeg(tensor, channels=3)[:2]","metadata":{"executionInfo":{"elapsed":114,"status":"ok","timestamp":1668083896253,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"},"user_tz":-330},"id":"xM4rQmj01U2w","execution":{"iopub.status.busy":"2024-01-17T06:29:18.741693Z","iopub.execute_input":"2024-01-17T06:29:18.741984Z","iopub.status.idle":"2024-01-17T06:29:18.756529Z","shell.execute_reply.started":"2024-01-17T06:29:18.741959Z","shell.execute_reply":"2024-01-17T06:29:18.755548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.image.convert_image_dtype(tensor, tf.float32).shape","metadata":{"executionInfo":{"elapsed":114,"status":"ok","timestamp":1668083896254,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"},"user_tz":-330},"id":"6zaO1FQXMO4X","outputId":"3e90efdf-2039-42e8-946e-17d0001d939c","execution":{"iopub.status.busy":"2024-01-17T06:29:18.758169Z","iopub.execute_input":"2024-01-17T06:29:18.758533Z","iopub.status.idle":"2024-01-17T06:29:18.776852Z","shell.execute_reply.started":"2024-01-17T06:29:18.758478Z","shell.execute_reply":"2024-01-17T06:29:18.775845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Turning our data into batches\n\nIf we try to process 10000+ images in one go, they might not fit into meaning\n\nUsing 32 images at a time\n \nTo use tensor effectively, data should be in form of Tensor tuples which looks like this `(image, label)`\n\n","metadata":{"id":"VgGo8qYgMZ4L"}},{"cell_type":"code","source":"# Create a function to return a tuple of (image, label)\ndef get_image_label(image_path, label):\n  \"\"\"\n  Takes an image file path name and the assosciated label,\n  process the image and return a typle of (image, label)\n  \n  \"\"\"\n  image = process_image(image_path)\n  return image, label","metadata":{"executionInfo":{"elapsed":34,"status":"ok","timestamp":1668083896255,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"},"user_tz":-330},"id":"dPZ97BrXOZhc","execution":{"iopub.status.busy":"2024-01-17T06:29:18.778188Z","iopub.execute_input":"2024-01-17T06:29:18.780475Z","iopub.status.idle":"2024-01-17T06:29:18.785391Z","shell.execute_reply.started":"2024-01-17T06:29:18.780448Z","shell.execute_reply":"2024-01-17T06:29:18.784359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Try above function \nprocess_image(X[42]).shape, tf.constant(y[42]).shape","metadata":{"executionInfo":{"elapsed":33,"status":"ok","timestamp":1668083896255,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"},"user_tz":-330},"id":"mYuMOZp5PN8Y","outputId":"e23e9582-cf92-4c02-a825-11ce7696b6eb","execution":{"iopub.status.busy":"2024-01-17T06:29:18.786673Z","iopub.execute_input":"2024-01-17T06:29:18.786968Z","iopub.status.idle":"2024-01-17T06:29:18.81163Z","shell.execute_reply.started":"2024-01-17T06:29:18.786944Z","shell.execute_reply":"2024-01-17T06:29:18.81069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Now we've got a way to turn data into tuples into Tensors in the form: `(image, label)`\n, let's make a function to turn all of our data (`X` & `Y`) into batches !","metadata":{"id":"yRrDY3cwPb_-"}},{"cell_type":"code","source":"# Define the batch size, 32 is a good start\nBATCH_SIZE = 32\n\n# Create a function to turn data into batches\ndef create_data_batches(X, y=None, batch_size = BATCH_SIZE, valid_data = False, test_data = False):\n  \"\"\"\n  Creates batches of data out of image (X) and label (Y) pairs.\n  Shuffles the data if it's training data but doesn't shuffle it it's validation data\n  Also accepts test data as input (no labels) \n  \"\"\"\n  # If the data is a test datasets, no labels\n  if test_data:\n    print(\"Creating test data batches...\")\n    data = tf.data.Dataset.from_tensor_slices((tf.constant(X))) # only filepaths (no labels)\n    data_batch = data.map(process_image).batch(BATCH_SIZE)\n    return data_batch\n  # If the data is a valid dataset, we don't need to shuffle it\n  elif valid_data:\n    print(\"creating validation data batches.....\")\n    data = tf.data.Dataset.from_tensor_slices((tf.constant(X), # filepaths \n                                               tf.constant(y))) # labels\n    data_batch = data.map(get_image_label).batch(BATCH_SIZE)\n    return data_batch\n\n  else:\n    print(\"Creating training data batches...\")\n    # Turn filepaths and labels into Tensors\n    data = tf.data.Dataset.from_tensor_slices((tf.constant(X),\n                                               tf.constant(y)))\n    # Shuffling pathnammes and labels before mapping image precessor function is faster than shuffling images\n    data = data.shuffle(buffer_size=len(X))\n\n    # Create (image, label) tuples (this also turns the image path into a preprocessed image)\n    data = data.map(get_image_label)\n\n    # Turn the training data into batches\n    data_batch = data.batch(BATCH_SIZE)\n  \n  return data_batch\n","metadata":{"executionInfo":{"elapsed":21,"status":"ok","timestamp":1668083896256,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"},"user_tz":-330},"id":"GridI1AYjFp9","execution":{"iopub.status.busy":"2024-01-17T06:29:18.812696Z","iopub.execute_input":"2024-01-17T06:29:18.812993Z","iopub.status.idle":"2024-01-17T06:29:18.82314Z","shell.execute_reply.started":"2024-01-17T06:29:18.812969Z","shell.execute_reply":"2024-01-17T06:29:18.821923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Creating training and validating data batches\ntrain_data = create_data_batches(X_train, y_train)\nval_data = create_data_batches(X_val, y_val, valid_data = True)\n","metadata":{"executionInfo":{"elapsed":935,"status":"ok","timestamp":1668083897171,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"},"user_tz":-330},"id":"LsLk6RuRmWJd","outputId":"9a250de7-2c5e-4cad-c04e-b7bf84d1dd8a","execution":{"iopub.status.busy":"2024-01-17T06:29:18.824485Z","iopub.execute_input":"2024-01-17T06:29:18.824784Z","iopub.status.idle":"2024-01-17T06:29:19.011948Z","shell.execute_reply.started":"2024-01-17T06:29:18.824759Z","shell.execute_reply":"2024-01-17T06:29:19.010822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Check out the different attributes of our data batches\ntrain_data.element_spec, val_data.element_spec","metadata":{"executionInfo":{"elapsed":60,"status":"ok","timestamp":1668083897173,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"},"user_tz":-330},"id":"cmIo4lYAmvt_","outputId":"7adbcf49-3f77-43f0-e521-676ccad6c968","execution":{"iopub.status.busy":"2024-01-17T06:29:19.013315Z","iopub.execute_input":"2024-01-17T06:29:19.013656Z","iopub.status.idle":"2024-01-17T06:29:19.021194Z","shell.execute_reply.started":"2024-01-17T06:29:19.013628Z","shell.execute_reply":"2024-01-17T06:29:19.020216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Visualizing Data Batches","metadata":{"id":"2fysELs-oLln"}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Create a function for viewing images in a data batch\ndef show_25_images(images, labels):\n  \"\"\"\n  Displays a plot of 25 images and their labels from a data batch\n  \"\"\"\n  # Set the figure\n  plt.figure(figsize=(10, 10))\n  # Loop through 25 (for displaying 25 images)\n  for i in range(25):\n    # Create subplots (5 rows , 5 columns)\n    ax = plt.subplot(5, 5, i+1)\n    # Display an image\n    plt.imshow(images[i])\n    # Add the image image label as the title\n    plt.title(unique_breeds[labels[i].argmax()])\n    # Turn the grid lines off\n    plt.axis(\"off\")","metadata":{"executionInfo":{"elapsed":48,"status":"ok","timestamp":1668083897176,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"},"user_tz":-330},"id":"Xw7oKjNXtdwj","execution":{"iopub.status.busy":"2024-01-17T06:29:19.022484Z","iopub.execute_input":"2024-01-17T06:29:19.022817Z","iopub.status.idle":"2024-01-17T06:29:19.031312Z","shell.execute_reply.started":"2024-01-17T06:29:19.022791Z","shell.execute_reply":"2024-01-17T06:29:19.03025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y[0]","metadata":{"executionInfo":{"elapsed":49,"status":"ok","timestamp":1668083897178,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"},"user_tz":-330},"id":"7BIYZ6Exu2PC","outputId":"2d91d37a-b978-4862-a78b-3f10795f07e6","execution":{"iopub.status.busy":"2024-01-17T06:29:19.032463Z","iopub.execute_input":"2024-01-17T06:29:19.032806Z","iopub.status.idle":"2024-01-17T06:29:19.047253Z","shell.execute_reply.started":"2024-01-17T06:29:19.03278Z","shell.execute_reply":"2024-01-17T06:29:19.046205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unique_breeds[y[0].argmax()]","metadata":{"executionInfo":{"elapsed":37,"status":"ok","timestamp":1668083897179,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"},"user_tz":-330},"id":"2QVwLTkbuRh4","outputId":"e3118fe2-8198-453a-f937-1ce81ec6cad6","execution":{"iopub.status.busy":"2024-01-17T06:29:19.048631Z","iopub.execute_input":"2024-01-17T06:29:19.048989Z","iopub.status.idle":"2024-01-17T06:29:19.05778Z","shell.execute_reply.started":"2024-01-17T06:29:19.048951Z","shell.execute_reply":"2024-01-17T06:29:19.056863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data","metadata":{"executionInfo":{"elapsed":36,"status":"ok","timestamp":1668083897181,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"},"user_tz":-330},"id":"WKEZ-6WwvBQ6","outputId":"6372f6f7-8d44-44dc-c264-df66c8b904c6","execution":{"iopub.status.busy":"2024-01-17T06:29:19.059222Z","iopub.execute_input":"2024-01-17T06:29:19.05965Z","iopub.status.idle":"2024-01-17T06:29:19.069026Z","shell.execute_reply.started":"2024-01-17T06:29:19.059614Z","shell.execute_reply":"2024-01-17T06:29:19.06807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Visualising data in training batches\ntrain_images, train_labels = next(train_data.as_numpy_iterator())\nshow_25_images(train_images, train_labels)","metadata":{"executionInfo":{"elapsed":9504,"status":"ok","timestamp":1668083906661,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"},"user_tz":-330},"id":"tfxCoXZ-u0Tb","outputId":"97e59422-1f5b-4a9d-f1a9-fec719449efb","execution":{"iopub.status.busy":"2024-01-17T06:29:19.070388Z","iopub.execute_input":"2024-01-17T06:29:19.070751Z","iopub.status.idle":"2024-01-17T06:29:21.773814Z","shell.execute_reply.started":"2024-01-17T06:29:19.070723Z","shell.execute_reply":"2024-01-17T06:29:21.772734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# # Visualising data in training batches\n# val_images, val_labels = next(val_data.as_numpy_iterator())\n# show_25_images(val_images, val_labels)","metadata":{"executionInfo":{"elapsed":9442,"status":"ok","timestamp":1668083916091,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"},"user_tz":-330},"id":"7qveWNkawfjO","outputId":"4bf81028-7ed5-4acb-ff8c-11a0ff012d1b","execution":{"iopub.status.busy":"2024-01-17T06:29:21.775188Z","iopub.execute_input":"2024-01-17T06:29:21.775541Z","iopub.status.idle":"2024-01-17T06:29:21.780093Z","shell.execute_reply.started":"2024-01-17T06:29:21.775484Z","shell.execute_reply":"2024-01-17T06:29:21.779031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Building a model\n\nBefore we build a model, there are a few things we need to define:\n* The input shape ( our images shape, in the form of Tensors ) to our model.\n* The output shape (image labels, in the form of Tensors) of our model.\n* The URL of the model we want to use ","metadata":{"id":"WgrXAhraxNLE"}},{"cell_type":"code","source":"IMG_SIZE","metadata":{"executionInfo":{"elapsed":76,"status":"ok","timestamp":1668083916095,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"},"user_tz":-330},"id":"7584sj3e0xr4","outputId":"5403ba88-9826-4249-a93f-93a9cd69d210","execution":{"iopub.status.busy":"2024-01-17T06:29:21.781303Z","iopub.execute_input":"2024-01-17T06:29:21.781649Z","iopub.status.idle":"2024-01-17T06:29:21.793994Z","shell.execute_reply.started":"2024-01-17T06:29:21.78162Z","shell.execute_reply":"2024-01-17T06:29:21.79298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Setup input shape to the model\nINPUT_SHAPE = [None, IMG_SIZE, IMG_SIZE, 3] # batch, height, width, colour channels\n\n# Setup output shape of our model\nOUTPUT_SHAPE = len(unique_breeds)\n\n# setup model URL from TensorFlow Hub\nMODEL_URL = 'https://tfhub.dev/google/imagenet/mobilenet_v2_130_224/classification/5'","metadata":{"executionInfo":{"elapsed":61,"status":"ok","timestamp":1668083916097,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"},"user_tz":-330},"id":"YuGJkNpAyN4_","execution":{"iopub.status.busy":"2024-01-17T06:29:21.79541Z","iopub.execute_input":"2024-01-17T06:29:21.795794Z","iopub.status.idle":"2024-01-17T06:29:21.803168Z","shell.execute_reply.started":"2024-01-17T06:29:21.795758Z","shell.execute_reply":"2024-01-17T06:29:21.802148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Now we've got our inputs, outputs and model ready to go.\nLet's put them together into a Keras deep learning model!\n\nKnowing this, let's create a function which :\n* Takes the input shape, output shape and the model as parameter\n* Defines the layers in a Keras model in sequential fashion( do this first, then this, then that)\n* Compiles the model (says it should be evaluated and imporoved)\n* Builds the model( tells the model the input shape it'll be getting).\n* Return the model\n\n","metadata":{"id":"5r7QqxxF04g5"}},{"cell_type":"code","source":"# Create a function which builds a Keras model \ndef create_model(input_shape=INPUT_SHAPE, output_shape=OUTPUT_SHAPE, model_url = MODEL_URL):\n  print(\"Buuilding model with: \", MODEL_URL)\n\n  # Setup the model alayers \n  model = tf.keras.Sequential([\n      hub.KerasLayer(MODEL_URL), # Layer 1 (input layer)\n      tf.keras.layers.Dense(units=OUTPUT_SHAPE,\n                            activation=\"softmax\") # Layer 2 (output layer)\n  ])\n\n  # Compiler the model\n  model.compile(\n      loss=tf.keras.losses.CategoricalCrossentropy(),\n      optimizer = tf.keras.optimizers.Adam(),\n      metrics = [\"accuracy\"]\n  )\n\n  # Build the model\n  model.build(INPUT_SHAPE)\n\n  return model","metadata":{"executionInfo":{"elapsed":61,"status":"ok","timestamp":1668083916098,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"},"user_tz":-330},"id":"m8UZN9SB7Gf9","execution":{"iopub.status.busy":"2024-01-17T06:29:21.804461Z","iopub.execute_input":"2024-01-17T06:29:21.804821Z","iopub.status.idle":"2024-01-17T06:29:21.81539Z","shell.execute_reply.started":"2024-01-17T06:29:21.804794Z","shell.execute_reply":"2024-01-17T06:29:21.814389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = create_model()\nmodel.summary()","metadata":{"executionInfo":{"elapsed":2561,"status":"ok","timestamp":1668083918601,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"},"user_tz":-330},"id":"C9ZTTXK29AjA","outputId":"939f04eb-46a5-403f-ad2d-dc5f5fb86e2a","execution":{"iopub.status.busy":"2024-01-17T06:29:21.816867Z","iopub.execute_input":"2024-01-17T06:29:21.817195Z","iopub.status.idle":"2024-01-17T06:29:25.576929Z","shell.execute_reply.started":"2024-01-17T06:29:21.817169Z","shell.execute_reply":"2024-01-17T06:29:25.575864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" outputs = np.ones(shape=(1, 1, 1280))\n outputs, outputs.shape","metadata":{"executionInfo":{"elapsed":45,"status":"ok","timestamp":1668083918602,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"},"user_tz":-330},"id":"7jdBG4Gv9Ddl","outputId":"5475fd0a-e014-479e-93c7-184987ea1951","execution":{"iopub.status.busy":"2024-01-17T06:29:25.578453Z","iopub.execute_input":"2024-01-17T06:29:25.578877Z","iopub.status.idle":"2024-01-17T06:29:25.587099Z","shell.execute_reply.started":"2024-01-17T06:29:25.578841Z","shell.execute_reply":"2024-01-17T06:29:25.585953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":" ## Creating callbacks \n Callbacks are helper functions a model can use during traning to do such things as save its progress, check its progress or stops traning early if model stops improving\n\n We'll create two callbacks, one for TensorBord which helps track our models progress and another for early which prevents model from training for too long\n\n ### TensorBord Callback\n To setup a TensorBoard callback, we need to do 3 things:\n 1. Load the TensorBord notebook extension\n 2. Create a TensorBoard callback which is able to save logs to a directory and pass it to our model's `fit()` function\n 3. Visualise our models training logs with the `%tensorbord` modmagic funciton (we'll do this after model training). ","metadata":{"id":"MMLNxh5vHNAE"}},{"cell_type":"code","source":"# Load TensorBord notebook extension\n%load_ext tensorboard","metadata":{"executionInfo":{"elapsed":36,"status":"ok","timestamp":1668083918603,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"},"user_tz":-330},"id":"ZS4PaQODp_Xj","execution":{"iopub.status.busy":"2024-01-17T06:29:25.588465Z","iopub.execute_input":"2024-01-17T06:29:25.588793Z","iopub.status.idle":"2024-01-17T06:29:25.643151Z","shell.execute_reply.started":"2024-01-17T06:29:25.588767Z","shell.execute_reply":"2024-01-17T06:29:25.642077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import datetime\n\n# Create a function to build a TensorBord callback\ndef create_tensorboard_callback():\n  # Create a log directory for storing TensorBord logs\n  logdir = os.path.join(\"/kaggle/working/drive/My Drive/Colab_Dog Vision data/logs\",\n                        # Make it so the logs get tracked whenever we run as experiment\n                        datetime.datetime.now().strftime(\"%Y%m%d-%H%M%S\")\n                        )\n  return tf.keras.callbacks.TensorBoard(logdir)","metadata":{"executionInfo":{"elapsed":36,"status":"ok","timestamp":1668083918604,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"},"user_tz":-330},"id":"ACHL6lg2r8oh","execution":{"iopub.status.busy":"2024-01-17T06:31:23.3608Z","iopub.execute_input":"2024-01-17T06:31:23.361257Z","iopub.status.idle":"2024-01-17T06:31:23.368484Z","shell.execute_reply.started":"2024-01-17T06:31:23.361223Z","shell.execute_reply":"2024-01-17T06:31:23.367059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Early stoping callbacks '\nEarly stopping helps stop our model from overfitting by stopping training if a certain evaluation matric stop imporving \n\nhttps://www.tensorflow.org/api_docs/python/tf/keras/callbacks/EarlyStopping\n\n","metadata":{"id":"pc7JKe0-tkof"}},{"cell_type":"code","source":"# creat early stoping callbacks\nearly_stopping = tf.keras.callbacks.EarlyStopping(monitor=\"val_accuracy\",\n                                                  patience=3)","metadata":{"executionInfo":{"elapsed":35,"status":"ok","timestamp":1668083918605,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"},"user_tz":-330},"id":"9CsstzZyv1Yw","execution":{"iopub.status.busy":"2024-01-17T06:31:25.713642Z","iopub.execute_input":"2024-01-17T06:31:25.714066Z","iopub.status.idle":"2024-01-17T06:31:25.719484Z","shell.execute_reply.started":"2024-01-17T06:31:25.71402Z","shell.execute_reply":"2024-01-17T06:31:25.718239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Training a moel ( on subset of data )\n\nFirst model is only going to train on 1000 images, to make sure everyting is working and to minimise time between training ","metadata":{"id":"R4b7d5XmxhHx"}},{"cell_type":"code","source":"NUM_EPOCHS = 50 #@param {type:\"slider\", min:10, max:100, step:10}","metadata":{"executionInfo":{"elapsed":35,"status":"ok","timestamp":1668083918605,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"},"user_tz":-330},"id":"RiR56o7Nx04c","execution":{"iopub.status.busy":"2024-01-17T06:31:27.633179Z","iopub.execute_input":"2024-01-17T06:31:27.63421Z","iopub.status.idle":"2024-01-17T06:31:27.638608Z","shell.execute_reply.started":"2024-01-17T06:31:27.634175Z","shell.execute_reply":"2024-01-17T06:31:27.637542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Making sure we're still runing on a GPU\nprint(\"GPU available\" if tf.config.list_physical_devices(\"GPU\") else \"not running on GPU :)\")","metadata":{"executionInfo":{"elapsed":34,"status":"ok","timestamp":1668083918605,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"},"user_tz":-330},"id":"PITHx894zPjt","outputId":"5e167339-4328-4880-a607-37f6d23c69ba","execution":{"iopub.status.busy":"2024-01-17T06:31:28.574104Z","iopub.execute_input":"2024-01-17T06:31:28.575094Z","iopub.status.idle":"2024-01-17T06:31:28.581019Z","shell.execute_reply.started":"2024-01-17T06:31:28.575048Z","shell.execute_reply":"2024-01-17T06:31:28.579867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Creating a  funciton which trains a model\n\n* Create a model using `create_model()`\n* Setup a TensorBoard callback using `create_tensorboard_callback()`\n* Call the `fit()` function on our model passing it the training data, training data, validating data, number of epochs to train for (`NUM_EPCHOS`) and the callbacks we'de like to use\n* Return the model","metadata":{"id":"xjtnWlByz4zr"}},{"cell_type":"code","source":"# Build a funciton to train and return a trained model\ndef train_model():\n  \"\"\"\n  Trains a given model and returns the trained version.\n  \"\"\"\n  # Create a model\n  model = create_model()\n\n  # Create a new TensorBord session everything we train a model\n  tensorboard = create_tensorboard_callback()\n\n  # Fit the model to the data passing it the callbacks we created \n  model.fit(x=train_data,\n            epochs=NUM_EPOCHS,\n            validation_data = val_data,\n            validation_freq = 1,\n            callbacks = [tensorboard, early_stopping])\n  # Returning the fitted model\n  return model","metadata":{"executionInfo":{"elapsed":26,"status":"ok","timestamp":1668083918606,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"},"user_tz":-330},"id":"BEmHEwmt0oc2","execution":{"iopub.status.busy":"2024-01-17T06:31:30.516701Z","iopub.execute_input":"2024-01-17T06:31:30.517115Z","iopub.status.idle":"2024-01-17T06:31:30.523766Z","shell.execute_reply.started":"2024-01-17T06:31:30.517084Z","shell.execute_reply":"2024-01-17T06:31:30.522564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Fit the model to the data\nmodel = train_model()","metadata":{"id":"mouBUCjB1ljh","executionInfo":{"status":"ok","timestamp":1668084092539,"user_tz":-330,"elapsed":173959,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"}},"outputId":"4316a0fe-f5d1-4d3a-ed9f-e2c497322df1","execution":{"iopub.status.busy":"2024-01-17T06:31:41.67202Z","iopub.execute_input":"2024-01-17T06:31:41.672426Z","iopub.status.idle":"2024-01-17T06:31:59.040634Z","shell.execute_reply.started":"2024-01-17T06:31:41.672396Z","shell.execute_reply":"2024-01-17T06:31:59.039654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"It looks like model is overfitting because it's performing far better on traning data than the valdation dataset, look for ways to prevernt overfitting in deep learning neural network\n\n** Note ** overfitting to begin whith meaning model is learning something!!!","metadata":{"id":"bqo7p2wx1rjq"}},{"cell_type":"markdown","source":"### Checking the TensorBord logs\n\nThe TensorBord magic function (`%tensorboard`) will access the logs directory we created and visualise it's content","metadata":{"id":"KSa45gfo3UM2"}},{"cell_type":"code","source":"# %pip uninstall -y tensorboard tb-nightly &&\n# %pip install tb-nightly  # must have at least tb-nightly==2.5.0a20210316","metadata":{"execution":{"iopub.status.busy":"2024-01-17T06:35:50.865954Z","iopub.execute_input":"2024-01-17T06:35:50.866381Z","iopub.status.idle":"2024-01-17T06:35:50.870931Z","shell.execute_reply.started":"2024-01-17T06:35:50.866354Z","shell.execute_reply":"2024-01-17T06:35:50.869798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# tensorboard --logdir /path/to/logs --load_fast true","metadata":{"execution":{"iopub.status.busy":"2024-01-17T06:36:10.516441Z","iopub.execute_input":"2024-01-17T06:36:10.516868Z","iopub.status.idle":"2024-01-17T06:36:10.522033Z","shell.execute_reply.started":"2024-01-17T06:36:10.516838Z","shell.execute_reply":"2024-01-17T06:36:10.520561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%tensorboard --logdir /kaggle/working/drive/My\\ Drive/Colab_Dog\\ Vision\\ data/logs --load_fast true","metadata":{"id":"aAbkPEwS8E9J","executionInfo":{"status":"ok","timestamp":1668084096824,"user_tz":-330,"elapsed":4304,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"}},"outputId":"e520e7a9-5647-48e8-9e7c-8eb257253bfb","execution":{"iopub.status.busy":"2024-01-17T06:36:11.516738Z","iopub.execute_input":"2024-01-17T06:36:11.517466Z","iopub.status.idle":"2024-01-17T06:36:11.530317Z","shell.execute_reply.started":"2024-01-17T06:36:11.517431Z","shell.execute_reply":"2024-01-17T06:36:11.529296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Making and evaluating predictions using a trained model","metadata":{"id":"Lo9YVCef8d1b"}},{"cell_type":"code","source":"val_data","metadata":{"id":"zTo30SOBWY3K","executionInfo":{"status":"ok","timestamp":1668084096825,"user_tz":-330,"elapsed":30,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"}},"outputId":"55ec7dc3-6aa9-4bad-84a8-1ee8070a0c6e","execution":{"iopub.status.busy":"2024-01-17T06:36:18.507025Z","iopub.execute_input":"2024-01-17T06:36:18.507422Z","iopub.status.idle":"2024-01-17T06:36:18.514362Z","shell.execute_reply.started":"2024-01-17T06:36:18.507393Z","shell.execute_reply":"2024-01-17T06:36:18.513198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Make prediction on validation data\npredictions = model.predict(val_data, verbose = 1)\npredictions.shape","metadata":{"id":"PzRj_oTmWMjU","executionInfo":{"status":"ok","timestamp":1668084097610,"user_tz":-330,"elapsed":800,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"}},"outputId":"2886b009-941a-4824-ed5f-4c67077ae7f4","execution":{"iopub.status.busy":"2024-01-17T06:36:19.574174Z","iopub.execute_input":"2024-01-17T06:36:19.575074Z","iopub.status.idle":"2024-01-17T06:36:20.438323Z","shell.execute_reply.started":"2024-01-17T06:36:19.575043Z","shell.execute_reply":"2024-01-17T06:36:20.437286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions.shape","metadata":{"id":"fOK5513_Wjqn","executionInfo":{"status":"ok","timestamp":1668084097611,"user_tz":-330,"elapsed":53,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"}},"outputId":"2823a1b7-4fe6-4ae5-bf2c-0a819efeae3b","execution":{"iopub.status.busy":"2024-01-17T06:36:21.921571Z","iopub.execute_input":"2024-01-17T06:36:21.92197Z","iopub.status.idle":"2024-01-17T06:36:21.929979Z","shell.execute_reply.started":"2024-01-17T06:36:21.92194Z","shell.execute_reply":"2024-01-17T06:36:21.928997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(y_val)","metadata":{"id":"654Tqn0sWnwf","executionInfo":{"status":"ok","timestamp":1668084097611,"user_tz":-330,"elapsed":46,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"}},"outputId":"a9785f12-ee3a-47f8-ca20-2e5cbd074ec9","execution":{"iopub.status.busy":"2024-01-17T06:36:23.042754Z","iopub.execute_input":"2024-01-17T06:36:23.043138Z","iopub.status.idle":"2024-01-17T06:36:23.04999Z","shell.execute_reply.started":"2024-01-17T06:36:23.043109Z","shell.execute_reply":"2024-01-17T06:36:23.048955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions[0], predictions.shape","metadata":{"id":"HkNI2E4EWq4W","executionInfo":{"status":"ok","timestamp":1668084097612,"user_tz":-330,"elapsed":40,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"}},"outputId":"1881941c-8c86-4717-e935-6771a2fd6ea1","execution":{"iopub.status.busy":"2024-01-17T06:36:24.950933Z","iopub.execute_input":"2024-01-17T06:36:24.95129Z","iopub.status.idle":"2024-01-17T06:36:24.960095Z","shell.execute_reply.started":"2024-01-17T06:36:24.951264Z","shell.execute_reply":"2024-01-17T06:36:24.959014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.sum(predictions[0])","metadata":{"id":"KwAd-HkyW5ON","executionInfo":{"status":"ok","timestamp":1668084097612,"user_tz":-330,"elapsed":32,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"}},"outputId":"35554c0a-a42b-48a7-b341-9c5cd9f21aa4","execution":{"iopub.status.busy":"2024-01-17T06:36:26.462442Z","iopub.execute_input":"2024-01-17T06:36:26.462852Z","iopub.status.idle":"2024-01-17T06:36:26.470634Z","shell.execute_reply.started":"2024-01-17T06:36:26.462822Z","shell.execute_reply":"2024-01-17T06:36:26.469402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.max(predictions[0])","metadata":{"id":"cl8KtetcXAQZ","executionInfo":{"status":"ok","timestamp":1668084097612,"user_tz":-330,"elapsed":24,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"}},"outputId":"d3feeb44-6a25-459a-abbb-8d6c5f50bfa5","execution":{"iopub.status.busy":"2024-01-17T06:36:27.3614Z","iopub.execute_input":"2024-01-17T06:36:27.361855Z","iopub.status.idle":"2024-01-17T06:36:27.369618Z","shell.execute_reply.started":"2024-01-17T06:36:27.361824Z","shell.execute_reply":"2024-01-17T06:36:27.368336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# First prediction\nindex = 42\nprint(predictions[index])\nprint(f\"Max value (propbabitity of predictions): {np.max(predictions[index])}\")\nprint(f\"Sum: {np.sum(predictions[index])}\")\nprint(f\"Max index: {np.argmax(predictions[index])}, with value {predictions[index][np.argmax(predictions[index])]}\")\nprint(f\"Predicted label : {unique_breeds[np.argmax(predictions[index])]}\")","metadata":{"id":"itVWnngEXFiT","executionInfo":{"status":"ok","timestamp":1668084097613,"user_tz":-330,"elapsed":15,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"}},"outputId":"d84f554a-9bdb-4e4c-e4f6-7f0c6e03b8bf","execution":{"iopub.status.busy":"2024-01-17T06:36:28.638982Z","iopub.execute_input":"2024-01-17T06:36:28.639476Z","iopub.status.idle":"2024-01-17T06:36:28.650807Z","shell.execute_reply.started":"2024-01-17T06:36:28.639439Z","shell.execute_reply":"2024-01-17T06:36:28.649523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unique_breeds[113]","metadata":{"id":"WLm4iRKOYAe0","executionInfo":{"status":"ok","timestamp":1668084098579,"user_tz":-330,"elapsed":26,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"}},"outputId":"a05e1722-5a01-43b2-a2bb-d7985ce11b36","execution":{"iopub.status.busy":"2024-01-17T06:36:30.61531Z","iopub.execute_input":"2024-01-17T06:36:30.615711Z","iopub.status.idle":"2024-01-17T06:36:30.622205Z","shell.execute_reply.started":"2024-01-17T06:36:30.615683Z","shell.execute_reply":"2024-01-17T06:36:30.621161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":" Having the above functinalities is great but we want to do it at scale.\n\n And it would be even better if we could see the image the prediciton is being mode on!\n\n **Node:** Predictions probabilities are also known as confidence levels","metadata":{"id":"BLQok76KYu46"}},{"cell_type":"code","source":"# Turn prediction probabilities into their respective label (easier to understand )\ndef get_pred_label(predictions_probabilities):\n  \"\"\"\n  Turns an array of prediction probabilities into a label\n  \"\"\"\n  return unique_breeds[np.argmax(predictions_probabilities)]\n\n# Get a predicted label based on an array of prediction probability\npred_label = get_pred_label(predictions[81])\npred_label","metadata":{"id":"1Q8hWSz0ZmSo","executionInfo":{"status":"ok","timestamp":1668084098581,"user_tz":-330,"elapsed":26,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"}},"outputId":"4b0ee1d7-824b-47dd-b8f6-0b9c59d4bacc","execution":{"iopub.status.busy":"2024-01-17T06:36:33.729672Z","iopub.execute_input":"2024-01-17T06:36:33.730179Z","iopub.status.idle":"2024-01-17T06:36:33.738513Z","shell.execute_reply.started":"2024-01-17T06:36:33.730146Z","shell.execute_reply":"2024-01-17T06:36:33.737522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Now since our validation data is still in a batch dataset, we'll have to ubbatch it to make predictions on the validation images and then compare those predictions to the validation labels (truth labels)","metadata":{"id":"73x5v4v8asmx"}},{"cell_type":"code","source":"val_data","metadata":{"id":"IRok9ZSMaWoo","executionInfo":{"status":"ok","timestamp":1668084098582,"user_tz":-330,"elapsed":26,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"}},"outputId":"24f96dd1-e8b7-47d3-a26c-b57d1e5efa53","execution":{"iopub.status.busy":"2024-01-17T06:36:37.69321Z","iopub.execute_input":"2024-01-17T06:36:37.694208Z","iopub.status.idle":"2024-01-17T06:36:37.700611Z","shell.execute_reply.started":"2024-01-17T06:36:37.694173Z","shell.execute_reply":"2024-01-17T06:36:37.699551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create a function to unbatch a batch dataset\ndef unbatchify(data):\n  images = []\n  labels = []\n\n  # Loop through unbatched data\n  for image, label in data.unbatch().as_numpy_iterator():\n    images.append(image)\n    labels.append(unique_breeds[np.argmax(label)])\n  return images, labels\n\n# Unbatchify the validation data\nval_images, val_labels = unbatchify(val_data)\nval_images[0].shape, val_labels[0]","metadata":{"id":"zmU4pjd0b_zG","executionInfo":{"status":"ok","timestamp":1668084099981,"user_tz":-330,"elapsed":1415,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"}},"outputId":"0bd2189a-6937-4f22-ee25-48e54161b10c","execution":{"iopub.status.busy":"2024-01-17T06:36:38.706517Z","iopub.execute_input":"2024-01-17T06:36:38.706911Z","iopub.status.idle":"2024-01-17T06:36:39.018619Z","shell.execute_reply.started":"2024-01-17T06:36:38.706882Z","shell.execute_reply":"2024-01-17T06:36:39.017539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# images_ = []\n# labels_ = []\n\n# # Loop through unbatched data\n# for image, label in val_data.unbatch().as_numpy_iterator():\n#   images_.append(image)\n#   labels_.append(label)\n\n# images_[0], labels_[0]","metadata":{"id":"xYPnUZOearSo","executionInfo":{"status":"ok","timestamp":1668084099983,"user_tz":-330,"elapsed":128,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"}},"execution":{"iopub.status.busy":"2024-01-17T06:36:41.962924Z","iopub.execute_input":"2024-01-17T06:36:41.963397Z","iopub.status.idle":"2024-01-17T06:36:41.968457Z","shell.execute_reply.started":"2024-01-17T06:36:41.963361Z","shell.execute_reply":"2024-01-17T06:36:41.967359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"get_pred_label(val_labels[0])","metadata":{"id":"COGaVx62btex","executionInfo":{"status":"ok","timestamp":1668084099985,"user_tz":-330,"elapsed":127,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"}},"outputId":"95a6c7aa-1629-4a66-925c-a3c6cc59abe4","execution":{"iopub.status.busy":"2024-01-17T06:36:43.689482Z","iopub.execute_input":"2024-01-17T06:36:43.690272Z","iopub.status.idle":"2024-01-17T06:36:43.696731Z","shell.execute_reply.started":"2024-01-17T06:36:43.690238Z","shell.execute_reply":"2024-01-17T06:36:43.695651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"get_pred_label(predictions[0])","metadata":{"id":"MxuHBLupb2Xa","executionInfo":{"status":"ok","timestamp":1668084099987,"user_tz":-330,"elapsed":125,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"}},"outputId":"50f8ffd6-1f40-4087-9eb0-0342c2bd826b","execution":{"iopub.status.busy":"2024-01-17T06:36:48.840147Z","iopub.execute_input":"2024-01-17T06:36:48.840617Z","iopub.status.idle":"2024-01-17T06:36:48.847735Z","shell.execute_reply.started":"2024-01-17T06:36:48.840586Z","shell.execute_reply":"2024-01-17T06:36:48.846521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Now we've got ways to get:\n* Predicted labels\n* Validation labels (truth tabel)\n* Validation images\n\nLet's make some function to make these all a bit more visualize\n\nWe'll create a function which:\n* Takes an array of predicted probabilities, an array of truth labels and an array of images and integers\n* Convert the prediction probabilities to a predicted label.\n* Plot the predicted label, its predicted probabilities, the truth label and the target image on a single plot.","metadata":{"id":"cfQkrqGSYSwO"}},{"cell_type":"code","source":"def plot_pred(prediction_preobabilities, labels, images, n = 1):\n  \"\"\"\n  View the prediction, ground truth and image for sample n\n  \"\"\"\n  pred_prob, true_label, image = prediction_preobabilities[n], labels[n], images[n]\n\n  # Get the pred label\n  pred_label = get_pred_label(pred_prob)\n\n  # Plot image # remove ticks \n  plt.imshow(image)\n  plt.xticks([])\n  plt.yticks([])\n\n  # Change to colour of the title depending on if prediction is right or wrong\n  if pred_label == true_label:\n    color = \"green\"\n  else:\n    color = \"red\"\n\n  # Change plot title to be predicted, probabitity of prediction and truth label\n  plt.title(\"{} {:2.0f}% {}\".format(pred_label,\n                                   np.max(pred_prob)*100,\n                                   true_label),\n                                    color = color)\n  pass","metadata":{"id":"1-t7vA6IYuZY","executionInfo":{"status":"ok","timestamp":1668084099989,"user_tz":-330,"elapsed":117,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"}},"execution":{"iopub.status.busy":"2024-01-17T06:36:56.170183Z","iopub.execute_input":"2024-01-17T06:36:56.17058Z","iopub.status.idle":"2024-01-17T06:36:56.178702Z","shell.execute_reply.started":"2024-01-17T06:36:56.170552Z","shell.execute_reply":"2024-01-17T06:36:56.177554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_pred(prediction_preobabilities=predictions,\n          labels=val_labels,\n          images=val_images,\n          n = 77)","metadata":{"id":"4PwuyfZa3dvG","executionInfo":{"status":"ok","timestamp":1668084099991,"user_tz":-330,"elapsed":117,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"}},"outputId":"56ac1ef8-96ae-4e4e-ea7a-9f3d2c6d9db2","execution":{"iopub.status.busy":"2024-01-17T06:36:59.416256Z","iopub.execute_input":"2024-01-17T06:36:59.416663Z","iopub.status.idle":"2024-01-17T06:36:59.679801Z","shell.execute_reply.started":"2024-01-17T06:36:59.416636Z","shell.execute_reply":"2024-01-17T06:36:59.677859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions[1], predictions.shape, len(predictions[1])","metadata":{"id":"nJSgIRWfIgs-","executionInfo":{"status":"ok","timestamp":1668084099991,"user_tz":-330,"elapsed":112,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"}},"outputId":"322cb2fb-6f86-43c3-cc16-b4799c260e8b","execution":{"iopub.status.busy":"2024-01-17T06:37:19.531983Z","iopub.execute_input":"2024-01-17T06:37:19.532368Z","iopub.status.idle":"2024-01-17T06:37:19.541429Z","shell.execute_reply.started":"2024-01-17T06:37:19.532339Z","shell.execute_reply":"2024-01-17T06:37:19.540334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":" Now we've got one functions to visualize our models top prediction, let's make another to view our models top 10 prediction\n\n This function will:\n * Take an input of prediction probabilities array and a gound truth array and an integet\n * Find the top 10:\n  * Prediciton probabilities indexes ✅\n  * Prediction probabilities values ✅\n  * Prediction labels ✅\n* Plot the top 10 prediction probability values and labels, coloring the true label green ✅","metadata":{"id":"9s1jVm1M_4Ct"}},{"cell_type":"code","source":"predictions[0][predictions[0].argsort()[-10:][::-1]]","metadata":{"id":"jGwgFHnBMs6w","executionInfo":{"status":"ok","timestamp":1668084099992,"user_tz":-330,"elapsed":100,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"}},"outputId":"c5f626a5-6b16-471d-eaa6-ee7165a1c676","execution":{"iopub.status.busy":"2024-01-17T06:37:37.253988Z","iopub.execute_input":"2024-01-17T06:37:37.255158Z","iopub.status.idle":"2024-01-17T06:37:37.262499Z","shell.execute_reply.started":"2024-01-17T06:37:37.25512Z","shell.execute_reply":"2024-01-17T06:37:37.261413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions[0].max()","metadata":{"id":"HXsYAZEPOfcZ","executionInfo":{"status":"ok","timestamp":1668084099993,"user_tz":-330,"elapsed":88,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"}},"outputId":"2252064b-15a8-48be-9964-ccbdba585062","execution":{"iopub.status.busy":"2024-01-17T06:37:39.054657Z","iopub.execute_input":"2024-01-17T06:37:39.05508Z","iopub.status.idle":"2024-01-17T06:37:39.061926Z","shell.execute_reply.started":"2024-01-17T06:37:39.055053Z","shell.execute_reply":"2024-01-17T06:37:39.060748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unique_breeds[predictions[0].argsort()[-10:][::-1]]","metadata":{"id":"rOAnk1yuSOJI","executionInfo":{"status":"ok","timestamp":1668084099994,"user_tz":-330,"elapsed":77,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"}},"outputId":"7a0a63db-9513-4c42-9dbe-eb4272c0e063","execution":{"iopub.status.busy":"2024-01-17T06:37:40.20285Z","iopub.execute_input":"2024-01-17T06:37:40.203328Z","iopub.status.idle":"2024-01-17T06:37:40.212572Z","shell.execute_reply.started":"2024-01-17T06:37:40.203282Z","shell.execute_reply":"2024-01-17T06:37:40.211165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_pred_conf(prediction_probabilities, labels, n = 1):\n  \"\"\"\n  Plus the top 10 highest prediction confidances along with the truth for sample n.\n  \"\"\"\n  pred_prob, true_label = prediction_probabilities[n], labels[n]\n\n  # Get the predicted label\n  pred_label = get_pred_label(pred_prob)\n\n  # Find the top 10 prediction confidences indexes\n  top_10_pred_indexes = pred_prob.argsort()[-10:][::-1]\n\n  # Find the top 10 prediction confidence values\n  top_10_pred_values = pred_prob[top_10_pred_indexes]\n\n  # Prediction labels\n  top_10_pred_labels = unique_breeds[top_10_pred_indexes]\n\n  # Setup plot\n  top_plot = plt.bar(np.arange(len(top_10_pred_labels)),\n                     top_10_pred_values,\n                     color=\"grey\")\n  plt.xticks(np.arange(len(top_10_pred_labels)),\n                         labels = top_10_pred_labels,\n                         rotation=\"vertical\")\n  \n  # Change color of true label\n  if np.isin(true_label, top_10_pred_labels):\n    top_plot[np.argmax(top_10_pred_labels == true_label)].set_color(\"green\")\n  else:\n    pass","metadata":{"id":"Sb3RajW9DEIh","executionInfo":{"status":"ok","timestamp":1668084099998,"user_tz":-330,"elapsed":70,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"}},"execution":{"iopub.status.busy":"2024-01-17T06:37:42.025204Z","iopub.execute_input":"2024-01-17T06:37:42.02568Z","iopub.status.idle":"2024-01-17T06:37:42.034473Z","shell.execute_reply.started":"2024-01-17T06:37:42.025647Z","shell.execute_reply":"2024-01-17T06:37:42.033295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_pred_conf(prediction_probabilities=predictions,\n               labels=val_labels,\n               n=9)","metadata":{"id":"FI4hJgLTTgaN","executionInfo":{"status":"ok","timestamp":1668084100000,"user_tz":-330,"elapsed":71,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"}},"outputId":"2a1544c6-5782-433a-d42f-9020e69a44d4","execution":{"iopub.status.busy":"2024-01-17T06:37:43.274143Z","iopub.execute_input":"2024-01-17T06:37:43.274575Z","iopub.status.idle":"2024-01-17T06:37:43.624897Z","shell.execute_reply.started":"2024-01-17T06:37:43.274543Z","shell.execute_reply":"2024-01-17T06:37:43.623833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Now we've got some function to help is visualize our\npredictions and evaluate our model, let's check out a few","metadata":{"id":"jwY_PjpLV0SM"}},{"cell_type":"code","source":"predictions","metadata":{"id":"xa0PT5P1bNKO","executionInfo":{"status":"ok","timestamp":1668084100002,"user_tz":-330,"elapsed":69,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"}},"outputId":"356e6400-5d87-4d98-b862-6c8ccf41ee95","execution":{"iopub.status.busy":"2024-01-17T06:37:46.988777Z","iopub.execute_input":"2024-01-17T06:37:46.98917Z","iopub.status.idle":"2024-01-17T06:37:46.997222Z","shell.execute_reply.started":"2024-01-17T06:37:46.989142Z","shell.execute_reply":"2024-01-17T06:37:46.996119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Let's check out a few predictions and their different values\ni_multiplier = 0\nnum_rows = 3\nnum_cols = 2\nnum_images = num_rows*num_cols\nplt.figure(figsize=(10*num_cols, 5*num_rows))\nfor i in range(num_images):\n  plt.subplot(num_rows, 2*num_cols, 2*i+1)\n  plot_pred(prediction_preobabilities = predictions,\n            labels = val_labels,\n            images = val_images,\n            n = i+i_multiplier)\n  plt.subplot(num_rows, 2*num_cols, 2*i+2)\n  plot_pred_conf(prediction_probabilities=predictions,\n                 labels = val_labels,\n                 n = i+i_multiplier)\n  plt.tight_layout(h_pad=1.0)\nplt.show()","metadata":{"id":"qA_CSVB8X-1S","executionInfo":{"status":"ok","timestamp":1668084103729,"user_tz":-330,"elapsed":3784,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"}},"outputId":"d282878b-addb-4415-ce73-f13dcaceb1ca","execution":{"iopub.status.busy":"2024-01-17T06:37:48.410737Z","iopub.execute_input":"2024-01-17T06:37:48.411151Z","iopub.status.idle":"2024-01-17T06:37:50.960091Z","shell.execute_reply.started":"2024-01-17T06:37:48.41112Z","shell.execute_reply":"2024-01-17T06:37:50.959027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Creating confusion matrix  with our model preditions and true labels","metadata":{"id":"5RGtRPeiZR1v"}},{"cell_type":"markdown","source":"## Saving and reloading a trained model","metadata":{"id":"CJF3y69ThnAu"}},{"cell_type":"code","source":"# Creating  a funcitons to save a model\ndef save_model(model, suffix=None):\n  \"\"\"\n  Saves a given model in a models directory and appends a suffix (string).\n  \"\"\"\n  # Create a model directory pathname with current time\n  modeldir = os.path.join(\"drive/My Drive/Colab_Dog Vision data/models\",\n                          datetime.datetime.now().strftime(\"%Y%m%d-%H%M%s\"))\n  model_path = modeldir + \"-\" + suffix + \".h5\" # Save format of model\n  print(f\"Save model to: {model_path}.....\")\n  model.save(model_path)\n  return model_path","metadata":{"id":"3poyC1J4iE9l","executionInfo":{"status":"ok","timestamp":1668084103731,"user_tz":-330,"elapsed":69,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"}},"execution":{"iopub.status.busy":"2024-01-17T06:38:07.849036Z","iopub.execute_input":"2024-01-17T06:38:07.849469Z","iopub.status.idle":"2024-01-17T06:38:07.857565Z","shell.execute_reply.started":"2024-01-17T06:38:07.849435Z","shell.execute_reply":"2024-01-17T06:38:07.856659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create a funciton to load a trained modedl\ndef load_model(model_path):\n  \"\"\"\n  Loads a saved model from a specified path.\n  \"\"\"\n  print(f\"Loading saved model from: {model_path}\")\n  model = tf.keras.models.load_model(model_path,\n                                    custom_objects = {\"KerasLayer\": hub.KerasLayer})\n  return model","metadata":{"id":"OCMmUhnhjUGk","executionInfo":{"status":"ok","timestamp":1668084103733,"user_tz":-330,"elapsed":68,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"}},"execution":{"iopub.status.busy":"2024-01-17T06:38:09.169091Z","iopub.execute_input":"2024-01-17T06:38:09.169892Z","iopub.status.idle":"2024-01-17T06:38:09.175194Z","shell.execute_reply.started":"2024-01-17T06:38:09.169862Z","shell.execute_reply":"2024-01-17T06:38:09.174105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Checking if save and load functions","metadata":{"id":"iMGlZy2LkSlB"}},{"cell_type":"code","source":"# Save our model trained on 1000 images\nsave_model(model, suffix=\"1000-images-mobilenetv2-Adam\")","metadata":{"id":"-EvpTDNNkYbR","executionInfo":{"status":"ok","timestamp":1668084104468,"user_tz":-330,"elapsed":800,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"}},"outputId":"f5bded24-218e-41f9-a749-8656070c1fa5","execution":{"iopub.status.busy":"2024-01-17T06:38:11.842042Z","iopub.execute_input":"2024-01-17T06:38:11.84286Z","iopub.status.idle":"2024-01-17T06:38:12.133395Z","shell.execute_reply.started":"2024-01-17T06:38:11.842825Z","shell.execute_reply":"2024-01-17T06:38:12.132429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Testing load model\nloaded_1000_image_model = load_model(\"/kaggle/working/drive/My Drive/Colab_Dog Vision data/models/20240117-06381705473491-1000-images-mobilenetv2-Adam.h5\")","metadata":{"id":"cXax9XKmkqNS","executionInfo":{"status":"ok","timestamp":1668084106117,"user_tz":-330,"elapsed":1667,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"}},"outputId":"4911b6e5-d9ae-4176-a23a-579cdeee4510","execution":{"iopub.status.busy":"2024-01-17T06:39:07.956724Z","iopub.execute_input":"2024-01-17T06:39:07.957699Z","iopub.status.idle":"2024-01-17T06:39:10.792629Z","shell.execute_reply.started":"2024-01-17T06:39:07.957654Z","shell.execute_reply":"2024-01-17T06:39:10.79158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Evaluate the pre-saved model\nmodel.evaluate(val_data)","metadata":{"id":"Gf-s9ihFlMjc","executionInfo":{"status":"ok","timestamp":1668084106874,"user_tz":-330,"elapsed":773,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"}},"outputId":"38dafda1-c692-4d72-f072-d6828c9e5ee0","execution":{"iopub.status.busy":"2024-01-17T06:39:12.175675Z","iopub.execute_input":"2024-01-17T06:39:12.176065Z","iopub.status.idle":"2024-01-17T06:39:12.487662Z","shell.execute_reply.started":"2024-01-17T06:39:12.176035Z","shell.execute_reply":"2024-01-17T06:39:12.486676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Evaluate the loaded model\nloaded_1000_image_model.evaluate(val_data)","metadata":{"id":"1U2vAcmgmZES","executionInfo":{"status":"ok","timestamp":1668084109793,"user_tz":-330,"elapsed":2923,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"}},"outputId":"4c988d03-5fbd-4e5b-f801-a2e4fd853270","execution":{"iopub.status.busy":"2024-01-17T06:39:17.314157Z","iopub.execute_input":"2024-01-17T06:39:17.314537Z","iopub.status.idle":"2024-01-17T06:39:18.246082Z","shell.execute_reply.started":"2024-01-17T06:39:17.314508Z","shell.execute_reply":"2024-01-17T06:39:18.24508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Training a big dog model 🐕‍🦺 (on full data)","metadata":{"id":"485t8RVvmiun"}},{"cell_type":"code","source":"len(X), len(y)","metadata":{"id":"K4yuxCjnm8UJ","executionInfo":{"status":"ok","timestamp":1668084109795,"user_tz":-330,"elapsed":61,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"}},"outputId":"b75a1823-af13-472f-ad5b-1312dbb9baa4","execution":{"iopub.status.busy":"2024-01-17T06:39:21.53042Z","iopub.execute_input":"2024-01-17T06:39:21.53117Z","iopub.status.idle":"2024-01-17T06:39:21.537277Z","shell.execute_reply.started":"2024-01-17T06:39:21.531138Z","shell.execute_reply":"2024-01-17T06:39:21.536241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X[:10]","metadata":{"id":"AhizVnvynAAt","executionInfo":{"status":"ok","timestamp":1668084109795,"user_tz":-330,"elapsed":48,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"}},"outputId":"dce3425c-4242-4017-dd6d-3a144c14c11a","execution":{"iopub.status.busy":"2024-01-17T06:39:22.651599Z","iopub.execute_input":"2024-01-17T06:39:22.652412Z","iopub.status.idle":"2024-01-17T06:39:22.658731Z","shell.execute_reply.started":"2024-01-17T06:39:22.652379Z","shell.execute_reply":"2024-01-17T06:39:22.657717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(X_train)","metadata":{"id":"ATiSJkxKnBHt","executionInfo":{"status":"ok","timestamp":1668084109796,"user_tz":-330,"elapsed":40,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"}},"outputId":"864e48a5-6dd5-4f32-8bc6-54178b5c1460","execution":{"iopub.status.busy":"2024-01-17T06:39:23.695432Z","iopub.execute_input":"2024-01-17T06:39:23.696247Z","iopub.status.idle":"2024-01-17T06:39:23.702524Z","shell.execute_reply.started":"2024-01-17T06:39:23.69621Z","shell.execute_reply":"2024-01-17T06:39:23.701485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create a data batch with the full data set\nfull_data = create_data_batches(X, y)","metadata":{"id":"QmoAlxXSnFIr","executionInfo":{"status":"ok","timestamp":1668084109797,"user_tz":-330,"elapsed":34,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"}},"outputId":"0845b057-685e-4ab2-9b38-ccaa7100c417","execution":{"iopub.status.busy":"2024-01-17T06:39:24.665921Z","iopub.execute_input":"2024-01-17T06:39:24.666343Z","iopub.status.idle":"2024-01-17T06:39:24.816514Z","shell.execute_reply.started":"2024-01-17T06:39:24.66631Z","shell.execute_reply":"2024-01-17T06:39:24.815481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"full_data","metadata":{"id":"cD2KLpQsnYEe","executionInfo":{"status":"ok","timestamp":1668084109797,"user_tz":-330,"elapsed":25,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"}},"outputId":"24304a60-010e-4658-9578-d8318192d413","execution":{"iopub.status.busy":"2024-01-17T06:39:25.788354Z","iopub.execute_input":"2024-01-17T06:39:25.788789Z","iopub.status.idle":"2024-01-17T06:39:25.795612Z","shell.execute_reply.started":"2024-01-17T06:39:25.788756Z","shell.execute_reply":"2024-01-17T06:39:25.79455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create a model for full model\nfull_model = create_model()","metadata":{"id":"EWhQKznJnbjb","executionInfo":{"status":"ok","timestamp":1668084110755,"user_tz":-330,"elapsed":974,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"}},"outputId":"df42c51e-4d69-44ea-bfdd-02dd02430531","execution":{"iopub.status.busy":"2024-01-17T06:39:27.051913Z","iopub.execute_input":"2024-01-17T06:39:27.052337Z","iopub.status.idle":"2024-01-17T06:39:29.078934Z","shell.execute_reply.started":"2024-01-17T06:39:27.052306Z","shell.execute_reply":"2024-01-17T06:39:29.078159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create full model callbacks\nfull_model_tensorboard = create_tensorboard_callback()\n# No validation set when when training on all data, so we can't monitor validation accuracy\nfull_model_early_stopping = tf.keras.callbacks.EarlyStopping(monitor = \"accuracy\",\n                                                             patience=3)","metadata":{"id":"eoUWDtXTnmRX","executionInfo":{"status":"ok","timestamp":1668084110755,"user_tz":-330,"elapsed":7,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"}},"execution":{"iopub.status.busy":"2024-01-17T06:39:29.080574Z","iopub.execute_input":"2024-01-17T06:39:29.080892Z","iopub.status.idle":"2024-01-17T06:39:29.086547Z","shell.execute_reply.started":"2024-01-17T06:39:29.080865Z","shell.execute_reply":"2024-01-17T06:39:29.085435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Note:** Running the cell below will take some time(mabe up to 30 minutes) ","metadata":{"id":"MtrQU6lFom6S"}},{"cell_type":"code","source":"# Fit the full model to the full data\nfull_model.fit(x = full_data,\n               epochs=NUM_EPOCHS,\n               callbacks=[full_model_tensorboard, full_model_early_stopping])","metadata":{"id":"0QaVRP2xn2Pf","executionInfo":{"status":"ok","timestamp":1668085137123,"user_tz":-330,"elapsed":1026373,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"}},"outputId":"4f743272-29a3-46cf-8eb0-2fc8707bf57f","execution":{"iopub.status.busy":"2024-01-17T06:42:58.131092Z","iopub.execute_input":"2024-01-17T06:42:58.131558Z","iopub.status.idle":"2024-01-17T06:46:00.727101Z","shell.execute_reply.started":"2024-01-17T06:42:58.131527Z","shell.execute_reply":"2024-01-17T06:46:00.725974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# save_model(full_model, suffix=\"full_image-set-mobilentv2-Adam\")","metadata":{"id":"9xBGWsLC7N0K","executionInfo":{"status":"ok","timestamp":1668085517148,"user_tz":-330,"elapsed":2309,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"}},"outputId":"ed1a17b1-9764-4a13-fda7-a1c9d3adf303","execution":{"iopub.status.busy":"2024-01-17T06:39:31.458772Z","iopub.execute_input":"2024-01-17T06:39:31.459172Z","iopub.status.idle":"2024-01-17T06:39:31.463977Z","shell.execute_reply.started":"2024-01-17T06:39:31.459141Z","shell.execute_reply":"2024-01-17T06:39:31.462859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load full model\n# full_model_loaded = load_model(\"drive/My Drive/Colab_Dog Vision data/models/20221110-13051668085517-full_image-set-mobilentv2-Adam.h5\")","metadata":{"id":"8_JPPLQ9pPlR","executionInfo":{"status":"ok","timestamp":1668085624794,"user_tz":-330,"elapsed":4607,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"}},"outputId":"89ad261e-c2aa-44fc-e506-afc9d6b10887"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"full_model_loaded = full_model","metadata":{"execution":{"iopub.status.busy":"2024-01-17T06:47:53.882529Z","iopub.execute_input":"2024-01-17T06:47:53.882944Z","iopub.status.idle":"2024-01-17T06:47:53.888382Z","shell.execute_reply.started":"2024-01-17T06:47:53.882914Z","shell.execute_reply":"2024-01-17T06:47:53.887077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Making the prediction on test dataset\n\nSince our model has been trained on images in the form  of Tensor batches, to make prediction on the test data, we'll have to get it into the same format.\n\nUse `create_data_batches()` To convert list of filenames as input and convert them into Tensorfloww batches.\n\nTo make predictions on the test data we'll:\n* Get the test image filenames.✅\n* Convert the filenames into test data batches using `create_data_batches()` and setting the `test_data` parameter to `True` (since the test data doesn't have labels).✅\n* Make a predictions array by passing the test batches to the `predict()` method called on our model.","metadata":{"id":"gWUQNBc6ppkH"}},{"cell_type":"code","source":"# Load test image filename\ntest_path = \"/kaggle/input/dog-breed-identification/test/\"\ntest_filenames = [test_path + fname for fname in os.listdir(test_path)]\ntest_filenames[:10]","metadata":{"id":"iG-LxAvnqDzN","executionInfo":{"status":"ok","timestamp":1668087160847,"user_tz":-330,"elapsed":997,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"}},"outputId":"0bc10cfa-6a1d-41eb-b98c-aaa4341a03ed","execution":{"iopub.status.busy":"2024-01-17T06:47:57.301225Z","iopub.execute_input":"2024-01-17T06:47:57.301988Z","iopub.status.idle":"2024-01-17T06:47:57.316449Z","shell.execute_reply.started":"2024-01-17T06:47:57.301953Z","shell.execute_reply":"2024-01-17T06:47:57.315531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(test_filenames)","metadata":{"id":"tP77SScxr7ew","executionInfo":{"status":"ok","timestamp":1668087161590,"user_tz":-330,"elapsed":4,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"}},"outputId":"58730eca-467b-4864-9042-bbc464bd9b0f","execution":{"iopub.status.busy":"2024-01-17T06:47:58.319658Z","iopub.execute_input":"2024-01-17T06:47:58.320519Z","iopub.status.idle":"2024-01-17T06:47:58.32774Z","shell.execute_reply.started":"2024-01-17T06:47:58.320463Z","shell.execute_reply":"2024-01-17T06:47:58.326556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create test data batch \ntest_data = create_data_batches(test_filenames, test_data = True)","metadata":{"id":"g14hOeENsDDP","executionInfo":{"status":"ok","timestamp":1668087197108,"user_tz":-330,"elapsed":34120,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"}},"outputId":"5e298ac0-874e-4aa0-c897-8009f21bd6ca","execution":{"iopub.status.busy":"2024-01-17T06:47:59.003744Z","iopub.execute_input":"2024-01-17T06:47:59.004747Z","iopub.status.idle":"2024-01-17T06:47:59.032427Z","shell.execute_reply.started":"2024-01-17T06:47:59.004706Z","shell.execute_reply":"2024-01-17T06:47:59.031126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data","metadata":{"id":"cliKFmdxsTtu","executionInfo":{"status":"ok","timestamp":1668087197109,"user_tz":-330,"elapsed":15,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"}},"outputId":"633588af-50fa-47be-f46e-4fcb8e5dc49d","execution":{"iopub.status.busy":"2024-01-17T06:47:59.936411Z","iopub.execute_input":"2024-01-17T06:47:59.937451Z","iopub.status.idle":"2024-01-17T06:47:59.943592Z","shell.execute_reply.started":"2024-01-17T06:47:59.937417Z","shell.execute_reply":"2024-01-17T06:47:59.942547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Make prediction on test data batch using the loaded full model\ntest_predictions = full_model_loaded.predict(test_data, verbose = 1 )","metadata":{"id":"9eNTyZ-Ysadj","executionInfo":{"status":"ok","timestamp":1668087404311,"user_tz":-330,"elapsed":202797,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"}},"outputId":"4c132bb5-0fc0-4c46-a810-80c5d8b0d6ea","execution":{"iopub.status.busy":"2024-01-17T06:48:00.864845Z","iopub.execute_input":"2024-01-17T06:48:00.865766Z","iopub.status.idle":"2024-01-17T06:48:30.312895Z","shell.execute_reply.started":"2024-01-17T06:48:00.865732Z","shell.execute_reply":"2024-01-17T06:48:30.311814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Save predictions (Numpy arrary) to csv file (for access later)\nnp.savetxt(\"/kaggle/working/drive/preds_array.csv\", test_predictions, delimiter=\",\")","metadata":{"id":"T4y9b9UWs4C1","executionInfo":{"status":"ok","timestamp":1668087527808,"user_tz":-330,"elapsed":1370,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"}},"execution":{"iopub.status.busy":"2024-01-17T06:48:30.315074Z","iopub.execute_input":"2024-01-17T06:48:30.315973Z","iopub.status.idle":"2024-01-17T06:48:31.775281Z","shell.execute_reply.started":"2024-01-17T06:48:30.315935Z","shell.execute_reply":"2024-01-17T06:48:31.774151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load predictions (Numpy array) from csv file \ntest_predictions = np.loadtxt(\"/kaggle/working/drive/preds_array.csv\", delimiter=\",\")","metadata":{"id":"H8O3IVfuw7Cm","executionInfo":{"status":"ok","timestamp":1668087602755,"user_tz":-330,"elapsed":2369,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"}},"execution":{"iopub.status.busy":"2024-01-17T06:48:31.776616Z","iopub.execute_input":"2024-01-17T06:48:31.777013Z","iopub.status.idle":"2024-01-17T06:48:32.485065Z","shell.execute_reply.started":"2024-01-17T06:48:31.776977Z","shell.execute_reply":"2024-01-17T06:48:32.483882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_predictions[:10]","metadata":{"id":"6JnxFonGxMgW","executionInfo":{"status":"ok","timestamp":1668087621986,"user_tz":-330,"elapsed":552,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"}},"outputId":"8f7c8864-8d96-476a-bb84-2da58a30f3cc","execution":{"iopub.status.busy":"2024-01-17T06:48:32.487367Z","iopub.execute_input":"2024-01-17T06:48:32.48778Z","iopub.status.idle":"2024-01-17T06:48:32.498503Z","shell.execute_reply.started":"2024-01-17T06:48:32.487749Z","shell.execute_reply":"2024-01-17T06:48:32.497413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_predictions.shape","metadata":{"id":"4fetgNK6xRp-","executionInfo":{"status":"ok","timestamp":1668087632548,"user_tz":-330,"elapsed":456,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"}},"outputId":"91946ac2-c822-468b-df96-2e276a9abcb5","execution":{"iopub.status.busy":"2024-01-17T06:48:33.577291Z","iopub.execute_input":"2024-01-17T06:48:33.578059Z","iopub.status.idle":"2024-01-17T06:48:33.584568Z","shell.execute_reply.started":"2024-01-17T06:48:33.578024Z","shell.execute_reply":"2024-01-17T06:48:33.583544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(test_predictions), test_predictions.size","metadata":{"id":"4OZcP2pPxTyG","executionInfo":{"status":"ok","timestamp":1668087657757,"user_tz":-330,"elapsed":580,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"}},"outputId":"4ffa5406-4fe4-4826-8253-ba771c3af5c6","execution":{"iopub.status.busy":"2024-01-17T06:48:35.171223Z","iopub.execute_input":"2024-01-17T06:48:35.172137Z","iopub.status.idle":"2024-01-17T06:48:35.178597Z","shell.execute_reply.started":"2024-01-17T06:48:35.172102Z","shell.execute_reply":"2024-01-17T06:48:35.177354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Preparing test dataset predictions for Kaggle\n\nLooking at the Kaggle sample submission, we find it wants our models prediction probability output in a DataFrame with an ID and a column for each different dog breed\n\nhttps://www.kaggle.com/competitions/dog-breed-identification/overview/evaluation\n\nTO get the data int this format, we'll:\n* Create a pandas  DataFrame wih an ID column as well as a column for each dog breed\n* Add data to the ID coluumn by extreacting the test image ID's from their filepaths\n* Add data (the prediction probabilities) to each of the dog breed columns.\n* Export the DataFrame as a CSV to submit it to kaggle","metadata":{"id":"zq6FY2GFxa5z"}},{"cell_type":"code","source":"# Create a pandas DataFrame with empty columns\npreds_df = pd.DataFrame(columns=[\"id\"] + list(unique_breeds))\npreds_df.head()","metadata":{"id":"YQKt1Py8yU4N","executionInfo":{"status":"ok","timestamp":1668088195936,"user_tz":-330,"elapsed":1421,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"}},"outputId":"c84a3ecc-e136-4e7a-80fa-b86a1ff4d4d3","execution":{"iopub.status.busy":"2024-01-17T06:48:37.537595Z","iopub.execute_input":"2024-01-17T06:48:37.537987Z","iopub.status.idle":"2024-01-17T06:48:37.559932Z","shell.execute_reply.started":"2024-01-17T06:48:37.537959Z","shell.execute_reply":"2024-01-17T06:48:37.558838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Append test image ID's to predictions DataFrame\ntest_ids = [os.path.splitext(path)[0] for path in os.listdir(test_path)]\ntest_ids[:5]","metadata":{"id":"9iBZ4INgzeEN","executionInfo":{"status":"ok","timestamp":1668088355551,"user_tz":-330,"elapsed":773,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"}},"outputId":"6f456f15-b33f-42ed-d1c2-b0100dff86d9","execution":{"iopub.status.busy":"2024-01-17T06:48:57.732928Z","iopub.execute_input":"2024-01-17T06:48:57.733341Z","iopub.status.idle":"2024-01-17T06:48:57.767849Z","shell.execute_reply.started":"2024-01-17T06:48:57.733312Z","shell.execute_reply":"2024-01-17T06:48:57.766843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.path.splitext(test_filenames[0])","metadata":{"id":"epxWQGt3zmiN","executionInfo":{"status":"ok","timestamp":1668088308529,"user_tz":-330,"elapsed":876,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"}},"outputId":"b28c2aa2-3920-42da-dbc3-81639f04a746","execution":{"iopub.status.busy":"2024-01-17T06:48:58.837887Z","iopub.execute_input":"2024-01-17T06:48:58.838263Z","iopub.status.idle":"2024-01-17T06:48:58.844973Z","shell.execute_reply.started":"2024-01-17T06:48:58.838235Z","shell.execute_reply":"2024-01-17T06:48:58.843891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds_df[\"id\"] = test_ids","metadata":{"id":"mAsovZt4z5vJ","executionInfo":{"status":"ok","timestamp":1668088408488,"user_tz":-330,"elapsed":473,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"}},"execution":{"iopub.status.busy":"2024-01-17T06:48:59.318959Z","iopub.execute_input":"2024-01-17T06:48:59.319308Z","iopub.status.idle":"2024-01-17T06:48:59.324858Z","shell.execute_reply.started":"2024-01-17T06:48:59.31928Z","shell.execute_reply":"2024-01-17T06:48:59.323655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds_df.head()","metadata":{"id":"NbDl_bzO0SVd","executionInfo":{"status":"ok","timestamp":1668088822307,"user_tz":-330,"elapsed":783,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"}},"outputId":"2df4ca0a-0d43-477d-d4e9-3dee021f4143","execution":{"iopub.status.busy":"2024-01-17T06:49:00.223177Z","iopub.execute_input":"2024-01-17T06:49:00.224373Z","iopub.status.idle":"2024-01-17T06:49:00.25031Z","shell.execute_reply.started":"2024-01-17T06:49:00.224327Z","shell.execute_reply":"2024-01-17T06:49:00.249095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Add the prediction probability to each dog breed column\npreds_df[list(unique_breeds)] = test_predictions\npreds_df.head()","metadata":{"id":"z2eHBCgQ0U2j","executionInfo":{"status":"ok","timestamp":1668088500937,"user_tz":-330,"elapsed":974,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"}},"outputId":"98d50fa8-3ab3-4a0b-d964-3b8e414b901e","execution":{"iopub.status.busy":"2024-01-17T06:49:02.902661Z","iopub.execute_input":"2024-01-17T06:49:02.903339Z","iopub.status.idle":"2024-01-17T06:49:02.970517Z","shell.execute_reply.started":"2024-01-17T06:49:02.903307Z","shell.execute_reply":"2024-01-17T06:49:02.96942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds_df.to_csv(\"/kaggle/working/drive/My Drive/Colab_Dog Vision data/full_model_submission_1_mobilenetv2.csv\",\n                index = False)","metadata":{"id":"WRBvengv0oyF","executionInfo":{"status":"ok","timestamp":1668088584800,"user_tz":-330,"elapsed":2623,"user":{"displayName":"Rahul Kumar","userId":"06930351542222246632"}},"execution":{"iopub.status.busy":"2024-01-17T06:49:18.431893Z","iopub.execute_input":"2024-01-17T06:49:18.432293Z","iopub.status.idle":"2024-01-17T06:49:21.746581Z","shell.execute_reply.started":"2024-01-17T06:49:18.432263Z","shell.execute_reply":"2024-01-17T06:49:21.745506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"id":"gd_XR1ML08xW"},"execution_count":null,"outputs":[]}]}