{"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-08T18:13:41.086345Z","iopub.execute_input":"2023-05-08T18:13:41.086767Z","iopub.status.idle":"2023-05-08T18:14:09.258097Z","shell.execute_reply.started":"2023-05-08T18:13:41.086734Z","shell.execute_reply":"2023-05-08T18:14:09.256918Z"},"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-08T18:14:09.266118Z","iopub.execute_input":"2023-05-08T18:14:09.266392Z","iopub.status.idle":"2023-05-08T18:14:16.887790Z","shell.execute_reply.started":"2023-05-08T18:14:09.266369Z","shell.execute_reply":"2023-05-08T18:14:16.886914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = '/kaggle/input/dog-breed-identification/train'\ntest_path = '/kaggle/input/dog-breed-identification/test'\n\n#reading dataset labels\ntrain_labels = pd.read_csv('/kaggle/input/dog-breed-identification/labels.csv')\ntest_labels = pd.read_csv('/kaggle/input/dog-breed-identification/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2023-05-08T18:14:16.888902Z","iopub.execute_input":"2023-05-08T18:14:16.889428Z","iopub.status.idle":"2023-05-08T18:14:17.499223Z","shell.execute_reply.started":"2023-05-08T18:14:16.889401Z","shell.execute_reply":"2023-05-08T18:14:17.498143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels.head(20)","metadata":{"execution":{"iopub.status.busy":"2023-05-08T18:14:17.500744Z","iopub.execute_input":"2023-05-08T18:14:17.501145Z","iopub.status.idle":"2023-05-08T18:14:17.531161Z","shell.execute_reply.started":"2023-05-08T18:14:17.501112Z","shell.execute_reply":"2023-05-08T18:14:17.529899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_labels.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-08T18:14:17.532490Z","iopub.execute_input":"2023-05-08T18:14:17.533059Z","iopub.status.idle":"2023-05-08T18:14:17.561141Z","shell.execute_reply.started":"2023-05-08T18:14:17.533031Z","shell.execute_reply":"2023-05-08T18:14:17.559636Z"},"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-08T18:14:38.353789Z","iopub.execute_input":"2023-05-08T18:14:38.354186Z","iopub.status.idle":"2023-05-08T18:14:38.367799Z","shell.execute_reply.started":"2023-05-08T18:14:38.354146Z","shell.execute_reply":"2023-05-08T18:14:38.366526Z"},"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-08T18:14:38.684894Z","iopub.execute_input":"2023-05-08T18:14:38.685295Z","iopub.status.idle":"2023-05-08T18:14:38.846631Z","shell.execute_reply.started":"2023-05-08T18:14:38.685261Z","shell.execute_reply":"2023-05-08T18:14:38.845586Z"},"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-08T18:14:39.056321Z","iopub.execute_input":"2023-05-08T18:14:39.057357Z","iopub.status.idle":"2023-05-08T18:14:39.372967Z","shell.execute_reply.started":"2023-05-08T18:14:39.057313Z","shell.execute_reply":"2023-05-08T18:14:39.371974Z"},"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)","metadata":{"execution":{"iopub.status.busy":"2023-05-08T18:14:42.009052Z","iopub.execute_input":"2023-05-08T18:14:42.009415Z","iopub.status.idle":"2023-05-08T18:14:42.411893Z","shell.execute_reply.started":"2023-05-08T18:14:42.009386Z","shell.execute_reply":"2023-05-08T18:14:42.410938Z"},"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)","metadata":{"execution":{"iopub.status.busy":"2023-05-08T18:14:42.413520Z","iopub.execute_input":"2023-05-08T18:14:42.413807Z","iopub.status.idle":"2023-05-08T18:14:42.433079Z","shell.execute_reply.started":"2023-05-08T18:14:42.413783Z","shell.execute_reply":"2023-05-08T18:14:42.432137Z"},"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-08T18:14:45.456969Z","iopub.execute_input":"2023-05-08T18:14:45.457873Z","iopub.status.idle":"2023-05-08T18:14:49.611599Z","shell.execute_reply.started":"2023-05-08T18:14:45.457838Z","shell.execute_reply":"2023-05-08T18:14:49.610671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set.batch_size","metadata":{"execution":{"iopub.status.busy":"2023-05-08T18:14:54.436443Z","iopub.execute_input":"2023-05-08T18:14:54.436846Z","iopub.status.idle":"2023-05-08T18:14:54.443722Z","shell.execute_reply.started":"2023-05-08T18:14:54.436818Z","shell.execute_reply":"2023-05-08T18:14:54.442439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resnet = ResNet50V2(input_shape = [224,224,3], weights = 'imagenet', include_top = False)","metadata":{"execution":{"iopub.status.busy":"2023-05-08T18:15:02.501368Z","iopub.execute_input":"2023-05-08T18:15:02.501740Z","iopub.status.idle":"2023-05-08T18:15:08.880755Z","shell.execute_reply.started":"2023-05-08T18:15:02.501709Z","shell.execute_reply":"2023-05-08T18:15:08.879742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for 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-08T18:15:14.810833Z","iopub.execute_input":"2023-05-08T18:15:14.811181Z","iopub.status.idle":"2023-05-08T18:54:12.344623Z","shell.execute_reply.started":"2023-05-08T18:15:14.811154Z","shell.execute_reply":"2023-05-08T18:54:12.343570Z"},"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-08T18:54:12.346944Z","iopub.execute_input":"2023-05-08T18:54:12.347655Z"},"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":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\n\npredicted_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":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}