{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"<h1>Dog Breed Identification</h1>\n\n[https://www.kaggle.com/c/dog-breed-identification](https://www.kaggle.com/c/dog-breed-identification/)\n\nAuthor: Vaasudevan Srinivasan [(Portfolio)](https://vaasudevans.github.io) <br>\nCreated on: July 06, 2021 <br>","metadata":{"id":"ECfLzbQLc-6P"}},{"cell_type":"markdown","source":"# Import all the Modules","metadata":{"id":"FvlvnbBXdisu"}},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\nimport matplotlib.pyplot as plt\nimport tensorflow_hub as hub\nimport tensorflow as tf\nimport seaborn as sns\nimport pandas as pd\nimport numpy as np\n\nsns.set_theme()\ntf.__version__","metadata":{"id":"VSaiS6Svoo2n","executionInfo":{"status":"ok","timestamp":1625620239857,"user_tz":180,"elapsed":429,"user":{"displayName":"Vaasudevan Srinivasan","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gi0dXpSGlii6JEMDrq2vVeVdafnHzP4rCVssUb6Mg=s64","userId":"15487097235806161418"}},"outputId":"eb5a65b6-5c99-499b-efb2-a205503b1b2f","execution":{"iopub.status.busy":"2021-07-08T12:47:10.318660Z","iopub.execute_input":"2021-07-08T12:47:10.319030Z","iopub.status.idle":"2021-07-08T12:47:10.327058Z","shell.execute_reply.started":"2021-07-08T12:47:10.318976Z","shell.execute_reply":"2021-07-08T12:47:10.326056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Preparation","metadata":{"id":"VNtKpYRpo_pN"}},{"cell_type":"code","source":"labels_df = pd.read_csv('/kaggle/input/dog-breed-identification/labels.csv')\nlabels_df['id'] = labels_df['id'] + '.jpg'\nlabels_df.breed.value_counts().plot.bar(figsize=(25, 5));","metadata":{"id":"5rlWJwPyop0X","executionInfo":{"status":"ok","timestamp":1625620247296,"user_tz":180,"elapsed":4704,"user":{"displayName":"Vaasudevan Srinivasan","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gi0dXpSGlii6JEMDrq2vVeVdafnHzP4rCVssUb6Mg=s64","userId":"15487097235806161418"}},"outputId":"02cc1b07-a85a-4bf4-edb5-bd94e30e8d3f","execution":{"iopub.status.busy":"2021-07-08T12:47:12.672255Z","iopub.execute_input":"2021-07-08T12:47:12.672573Z","iopub.status.idle":"2021-07-08T12:47:18.224620Z","shell.execute_reply.started":"2021-07-08T12:47:12.672544Z","shell.execute_reply":"2021-07-08T12:47:18.223761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen = ImageDataGenerator(rescale=1.0 / 255,\n                             horizontal_flip=True,\n                             vertical_flip=True,\n                             shear_range=0.2,\n                             validation_split=0.1)\n\ntrain = datagen.flow_from_dataframe(labels_df,\n                                    directory='/kaggle/input/dog-breed-identification/train',\n                                    x_col='id',\n                                    y_col='breed',\n                                    subset='training',\n                                    target_size=(224, 224),\n                                    batch_size=64,\n                                    seed=123)\n\nval = datagen.flow_from_dataframe(labels_df,\n                                  directory='/kaggle/input/dog-breed-identification/train',\n                                  x_col='id',\n                                  y_col='breed',\n                                  subset='validation',\n                                  target_size=(224, 224),\n                                  batch_size=64,\n                                  seed=123)","metadata":{"id":"DTKiYnlqV9wt","executionInfo":{"status":"ok","timestamp":1625620250017,"user_tz":180,"elapsed":211,"user":{"displayName":"Vaasudevan Srinivasan","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gi0dXpSGlii6JEMDrq2vVeVdafnHzP4rCVssUb6Mg=s64","userId":"15487097235806161418"}},"outputId":"606b8b88-5463-4819-e559-b497b4c51ab2","execution":{"iopub.status.busy":"2021-07-08T12:47:18.225888Z","iopub.execute_input":"2021-07-08T12:47:18.226261Z","iopub.status.idle":"2021-07-08T12:47:21.689728Z","shell.execute_reply.started":"2021-07-08T12:47:18.226220Z","shell.execute_reply":"2021-07-08T12:47:21.688753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get the images from a val batch\nimgs = val.next()\nlabels = list(val.class_indices.keys())\n\nplt.figure(figsize=(20, 6))\ncols, rows = 15, 4\nfor i in range(cols * rows):\n    plt.subplot(rows, cols, i + 1)\n    plt.imshow(imgs[0][i])\n    plt.title(np.argmax(imgs[1][i]))\n    plt.axis('off')\n\nplt.tight_layout();","metadata":{"id":"knhkp1E2u7cF","executionInfo":{"status":"ok","timestamp":1625621867318,"user_tz":180,"elapsed":11658,"user":{"displayName":"Vaasudevan Srinivasan","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gi0dXpSGlii6JEMDrq2vVeVdafnHzP4rCVssUb6Mg=s64","userId":"15487097235806161418"}},"outputId":"766fb8ec-01c5-4060-e5cc-141f8e00454c","execution":{"iopub.status.busy":"2021-07-08T12:47:21.691610Z","iopub.execute_input":"2021-07-08T12:47:21.691871Z","iopub.status.idle":"2021-07-08T12:47:28.533584Z","shell.execute_reply.started":"2021-07-08T12:47:21.691846Z","shell.execute_reply":"2021-07-08T12:47:28.532659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Transfer Learning using Inception ResNet V2","metadata":{"id":"wo6UhI8avJIN"}},{"cell_type":"code","source":"url = 'https://tfhub.dev/google/imagenet/inception_resnet_v2/classification/5'\ninception_resnet = tf.keras.Sequential([\n    hub.KerasLayer(url, trainable=False),\n    tf.keras.layers.Dense(120, activation='softmax'),\n])\ninception_resnet.build((None, 224, 224, 3))\ninception_resnet.compile(loss='categorical_crossentropy',\n                         optimizer='adam', metrics=['accuracy'])\nh = inception_resnet.fit(train, validation_data=val, epochs=10,\n                         steps_per_epoch=len(train),\n                         validation_steps=len(val))","metadata":{"id":"U9jDWuyq_InQ","executionInfo":{"status":"ok","timestamp":1625621553098,"user_tz":180,"elapsed":1291886,"user":{"displayName":"Vaasudevan Srinivasan","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gi0dXpSGlii6JEMDrq2vVeVdafnHzP4rCVssUb6Mg=s64","userId":"15487097235806161418"}},"outputId":"2f0e029a-1bff-4562-d405-e81165bc114e","execution":{"iopub.status.busy":"2021-07-08T12:47:28.535216Z","iopub.execute_input":"2021-07-08T12:47:28.535548Z","iopub.status.idle":"2021-07-08T13:12:06.553744Z","shell.execute_reply.started":"2021-07-08T12:47:28.535516Z","shell.execute_reply":"2021-07-08T13:12:06.552881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Prediction and Submission","metadata":{"id":"czSwYgzC0ow3"}},{"cell_type":"code","source":"test_df = pd.read_csv('/kaggle/input/dog-breed-identification/sample_submission.csv')\nbreeds = test_df.columns[1:].tolist()\ntest_df['file_name'] = test_df['id'] + '.jpg'","metadata":{"id":"UhRyuprP1puB","executionInfo":{"status":"ok","timestamp":1625621559589,"user_tz":180,"elapsed":456,"user":{"displayName":"Vaasudevan Srinivasan","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gi0dXpSGlii6JEMDrq2vVeVdafnHzP4rCVssUb6Mg=s64","userId":"15487097235806161418"}},"execution":{"iopub.status.busy":"2021-07-08T13:12:24.482845Z","iopub.execute_input":"2021-07-08T13:12:24.483191Z","iopub.status.idle":"2021-07-08T13:12:25.067770Z","shell.execute_reply.started":"2021-07-08T13:12:24.483160Z","shell.execute_reply":"2021-07-08T13:12:25.066975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_gen = ImageDataGenerator(rescale=1.0 / 255)\n\ntest = test_gen.flow_from_dataframe(test_df,\n                                    x_col='file_name',\n                                    y_col=None,\n                                    directory='/kaggle/input/dog-breed-identification/test',\n                                    class_mode=None,\n                                    target=(224, 224))","metadata":{"id":"JXEQlNhUQpTE","executionInfo":{"status":"ok","timestamp":1625621562596,"user_tz":180,"elapsed":237,"user":{"displayName":"Vaasudevan Srinivasan","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gi0dXpSGlii6JEMDrq2vVeVdafnHzP4rCVssUb6Mg=s64","userId":"15487097235806161418"}},"outputId":"0f8f053c-f735-46c0-8e2b-a94cfae289eb","execution":{"iopub.status.busy":"2021-07-08T13:12:26.235319Z","iopub.execute_input":"2021-07-08T13:12:26.235645Z","iopub.status.idle":"2021-07-08T13:12:44.431397Z","shell.execute_reply.started":"2021-07-08T13:12:26.235616Z","shell.execute_reply":"2021-07-08T13:12:44.430498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Predict\ntest_df.loc[:, breeds] = inception_resnet.predict(test)\ntest_df[breeds].head()","metadata":{"id":"qe8qIXEaQpU8","executionInfo":{"status":"ok","timestamp":1625621643793,"user_tz":180,"elapsed":77655,"user":{"displayName":"Vaasudevan Srinivasan","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gi0dXpSGlii6JEMDrq2vVeVdafnHzP4rCVssUb6Mg=s64","userId":"15487097235806161418"}},"outputId":"11d30845-bda6-4718-92f8-20582031fc39","execution":{"iopub.status.busy":"2021-07-08T13:12:46.564066Z","iopub.execute_input":"2021-07-08T13:12:46.564389Z","iopub.status.idle":"2021-07-08T13:14:22.165198Z","shell.execute_reply.started":"2021-07-08T13:12:46.564361Z","shell.execute_reply":"2021-07-08T13:14:22.164416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.drop('file_name', axis=1).to_csv('submission.csv', index=False)","metadata":{"id":"a0vn6VW_JQey","executionInfo":{"status":"ok","timestamp":1625621672075,"user_tz":180,"elapsed":2156,"user":{"displayName":"Vaasudevan Srinivasan","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gi0dXpSGlii6JEMDrq2vVeVdafnHzP4rCVssUb6Mg=s64","userId":"15487097235806161418"}},"execution":{"iopub.status.busy":"2021-07-08T13:14:22.166571Z","iopub.execute_input":"2021-07-08T13:14:22.166954Z","iopub.status.idle":"2021-07-08T13:14:24.732807Z","shell.execute_reply.started":"2021-07-08T13:14:22.166918Z","shell.execute_reply":"2021-07-08T13:14:24.731976Z"},"trusted":true},"execution_count":null,"outputs":[]}]}