{"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":"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        print(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":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-05-04T12:15:21.083300Z","iopub.execute_input":"2023-05-04T12:15:21.083843Z","iopub.status.idle":"2023-05-04T12:15:25.585074Z","shell.execute_reply.started":"2023-05-04T12:15:21.083803Z","shell.execute_reply":"2023-05-04T12:15:25.582494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom keras.applications.resnet_v2 import ResNet50V2\nfrom tensorflow import keras\nfrom keras.preprocessing.image import ImageDataGenerator\nimport seaborn as sns\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2023-05-04T12:20:53.214782Z","iopub.execute_input":"2023-05-04T12:20:53.215182Z","iopub.status.idle":"2023-05-04T12:20:53.221003Z","shell.execute_reply.started":"2023-05-04T12:20:53.215151Z","shell.execute_reply":"2023-05-04T12:20:53.219765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = '../input/dog-breed-identification/train'\ntest_path = '../input/dog-breed-identification/test'\n\n#reading dataset labels\ntrain_labels = pd.read_csv('../input/dog-breed-identification/labels.csv')\ntest_labels = pd.read_csv('../input/dog-breed-identification/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2023-05-04T12:20:41.510170Z","iopub.execute_input":"2023-05-04T12:20:41.511246Z","iopub.status.idle":"2023-05-04T12:20:42.348533Z","shell.execute_reply.started":"2023-05-04T12:20:41.511200Z","shell.execute_reply":"2023-05-04T12:20:42.347263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-04T12:20:56.687885Z","iopub.execute_input":"2023-05-04T12:20:56.688275Z","iopub.status.idle":"2023-05-04T12:20:56.718530Z","shell.execute_reply.started":"2023-05-04T12:20:56.688248Z","shell.execute_reply":"2023-05-04T12:20:56.717344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_labels.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-04T12:20:59.341438Z","iopub.execute_input":"2023-05-04T12:20:59.341814Z","iopub.status.idle":"2023-05-04T12:20:59.372318Z","shell.execute_reply.started":"2023-05-04T12:20:59.341785Z","shell.execute_reply":"2023-05-04T12:20:59.370733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels['id'] = train_labels['id'].apply(lambda x: x + \".jpg\")\ntest_labels['id'] = test_labels['id'].apply(lambda x: x + \".jpg\")","metadata":{"execution":{"iopub.status.busy":"2023-05-04T12:21:03.853773Z","iopub.execute_input":"2023-05-04T12:21:03.854993Z","iopub.status.idle":"2023-05-04T12:21:03.868127Z","shell.execute_reply.started":"2023-05-04T12:21:03.854956Z","shell.execute_reply":"2023-05-04T12:21:03.866896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = train_labels.breed.unique()\ninclude = ['beagle', 'chihuahua', 'doberman','french_bulldog', 'golden_retriever', 'malamute', 'pug', 'saint_bernard', 'scottish_deerhound','tibetan_mastiff']\nfor i in x :\n    if i not in include:\n        train_labels = train_labels.drop(train_labels[ train_labels['breed'] == i ].index)\nprint(train_labels.shape)\ntrain_labels.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-04T12:21:06.128288Z","iopub.execute_input":"2023-05-04T12:21:06.128711Z","iopub.status.idle":"2023-05-04T12:21:06.309868Z","shell.execute_reply.started":"2023-05-04T12:21:06.128677Z","shell.execute_reply":"2023-05-04T12:21:06.308614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(18,4))\ncp = sns.countplot(x = 'breed', data = train_labels)","metadata":{"execution":{"iopub.status.busy":"2023-05-04T12:21:09.236941Z","iopub.execute_input":"2023-05-04T12:21:09.237308Z","iopub.status.idle":"2023-05-04T12:21:09.555157Z","shell.execute_reply.started":"2023-05-04T12:21:09.237281Z","shell.execute_reply":"2023-05-04T12:21:09.554291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(\n                                   rescale = 1./255, \n                                   validation_split = 0.2,\n                                   shear_range = 0.2, \n                                   zoom_range = 0.2, horizontal_flip = True, \n                                   width_shift_range = 0.2,\n                                   height_shift_range = 0.2,\n                                   rotation_range = 20,\n                                   brightness_range=[0.2,1.0])\n\ntrain_set = train_datagen.flow_from_dataframe(dataframe = train_labels,\n                                                 directory = train_path,\n                                                 x_col = \"id\",\n                                                 y_col = \"breed\",\n                                                 batch_size = 16,\n                                                 subset=\"training\",\n                                                 class_mode = \"categorical\",\n                                                 target_size = (224,224),\n                                                 seed = 42,\n                                                 shuffle = True)\n","metadata":{"execution":{"iopub.status.busy":"2023-05-04T12:21:13.614102Z","iopub.execute_input":"2023-05-04T12:21:13.614905Z","iopub.status.idle":"2023-05-04T12:21:14.046237Z","shell.execute_reply.started":"2023-05-04T12:21:13.614859Z","shell.execute_reply":"2023-05-04T12:21:14.045113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validate_set = train_datagen.flow_from_dataframe(dataframe = train_labels,\n                                                 directory = train_path,\n                                                 x_col = \"id\",\n                                                 y_col = \"breed\",\n                                                 batch_size = 16,\n                                                 subset=\"validation\",\n                                                 class_mode = \"categorical\",\n                                                 target_size = (224,224),\n                                                 seed = 42,\n                                                 shuffle = True)\n","metadata":{"execution":{"iopub.status.busy":"2023-05-04T12:21:22.631700Z","iopub.execute_input":"2023-05-04T12:21:22.632501Z","iopub.status.idle":"2023-05-04T12:21:22.690789Z","shell.execute_reply.started":"2023-05-04T12:21:22.632461Z","shell.execute_reply":"2023-05-04T12:21:22.689358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_datagen = ImageDataGenerator(rescale=1./255)\n\n#defining test set\ntest_set = test_datagen.flow_from_dataframe(dataframe = test_labels,\n                                                 directory = test_path,\n                                                 x_col = \"id\",\n                                                 y_col = None,\n                                                 batch_size = 16,\n                                                 class_mode = None,\n                                                 seed = 42,\n                                                 shuffle=False,\n                                                 target_size = (224,224))","metadata":{"execution":{"iopub.status.busy":"2023-05-04T12:21:24.959362Z","iopub.execute_input":"2023-05-04T12:21:24.959739Z","iopub.status.idle":"2023-05-04T12:21:29.729260Z","shell.execute_reply.started":"2023-05-04T12:21:24.959709Z","shell.execute_reply":"2023-05-04T12:21:29.728189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set.batch_size","metadata":{"execution":{"iopub.status.busy":"2023-05-04T12:21:29.730712Z","iopub.execute_input":"2023-05-04T12:21:29.731010Z","iopub.status.idle":"2023-05-04T12:21:29.737945Z","shell.execute_reply.started":"2023-05-04T12:21:29.730985Z","shell.execute_reply":"2023-05-04T12:21:29.736393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resnet = ResNet50V2(input_shape = [224,224,3], weights = 'imagenet', include_top = False)\n\nfor layer in resnet.layers:\n    layer.trainable = False\n\nx = keras.layers.Flatten()(resnet.output)\n\nx = keras.layers.Dropout(0.4)(x)\n\npred = keras.layers.Dense(10, activation='softmax')(x)\n\nmodel = tf.keras.models.Model(inputs=resnet.input, outputs=pred)\n\nopt = tf.keras.optimizers.Adam(learning_rate = 1e-5)\nmodel.compile(optimizer=opt,loss='categorical_crossentropy',metrics=['accuracy'])\n\ntrain_step = train_set.n//train_set.batch_size\nvalidate_step = validate_set.n//validate_set.batch_size\n\nresnet50 = model.fit(train_set,validation_data = validate_set,epochs = 30,steps_per_epoch = train_step, validation_steps = validate_step)","metadata":{"execution":{"iopub.status.busy":"2023-05-04T12:21:31.408928Z","iopub.execute_input":"2023-05-04T12:21:31.409572Z","iopub.status.idle":"2023-05-04T13:01:06.646477Z","shell.execute_reply.started":"2023-05-04T12:21:31.409528Z","shell.execute_reply":"2023-05-04T13:01:06.645198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"STEP_SIZE_TEST=test_set.n//test_set.batch_size\n\ntest_set.reset()\n\npred=model.predict(test_set,steps=STEP_SIZE_TEST,verbose=1)","metadata":{"execution":{"iopub.status.busy":"2023-05-04T13:06:25.598393Z","iopub.execute_input":"2023-05-04T13:06:25.598814Z","iopub.status.idle":"2023-05-04T13:18:58.289713Z","shell.execute_reply.started":"2023-05-04T13:06:25.598780Z","shell.execute_reply":"2023-05-04T13:18:58.288502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(resnet50.history['accuracy'])\nplt.plot(resnet50.history['val_accuracy'])\nplt.title('Model accuracy')\nplt.ylabel('Accuracy')\nplt.xlabel('Epoch')\nplt.legend(['Train', 'Test'], loc='upper left')\nplt.show()\n\nplt.plot(resnet50.history['loss'])\nplt.plot(resnet50.history['val_loss'])\nplt.title('Model loss')\nplt.ylabel('Loss')\nplt.xlabel('Epoch')\nplt.legend(['Train', 'Test'], loc='upper left')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-04T13:20:18.154472Z","iopub.execute_input":"2023-05-04T13:20:18.155021Z","iopub.status.idle":"2023-05-04T13:20:18.674867Z","shell.execute_reply.started":"2023-05-04T13:20:18.154985Z","shell.execute_reply":"2023-05-04T13:20:18.673282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_class_indices=np.argmax(pred,axis=1)\n\nlabels = (train_set.class_indices)\n\nlabels = dict((v,k) for k,v in labels.items())\n\npredictions = [labels[k] for k in predicted_class_indices]","metadata":{"execution":{"iopub.status.busy":"2023-05-04T13:20:32.956187Z","iopub.execute_input":"2023-05-04T13:20:32.957270Z","iopub.status.idle":"2023-05-04T13:20:32.965114Z","shell.execute_reply.started":"2023-05-04T13:20:32.957225Z","shell.execute_reply":"2023-05-04T13:20:32.963526Z"},"trusted":true},"execution_count":null,"outputs":[]}]}