{"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":"2022-08-19T19:21:16.59939Z","iopub.execute_input":"2022-08-19T19:21:16.600088Z","iopub.status.idle":"2022-08-19T19:21:20.432401Z","shell.execute_reply.started":"2022-08-19T19:21:16.600014Z","shell.execute_reply":"2022-08-19T19:21:20.431512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nimport cv2\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom keras.layers import Dense, MaxPool2D, Conv2D, Dropout, Flatten, GlobalAveragePooling2D, BatchNormalization, Activation, MaxPooling2D\nfrom keras.models import Sequential\nfrom keras.utils import np_utils\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.callbacks import EarlyStopping\nfrom keras.preprocessing.image import load_img","metadata":{"execution":{"iopub.status.busy":"2022-08-19T19:21:20.437833Z","iopub.execute_input":"2022-08-19T19:21:20.438196Z","iopub.status.idle":"2022-08-19T19:21:22.109953Z","shell.execute_reply.started":"2022-08-19T19:21:20.43816Z","shell.execute_reply":"2022-08-19T19:21:22.108947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"load_img('../input/dog-breed-identification/test/0012a730dfa437f5f3613fb75efcd4ce.jpg')","metadata":{"execution":{"iopub.status.busy":"2022-08-19T19:21:22.111294Z","iopub.execute_input":"2022-08-19T19:21:22.112053Z","iopub.status.idle":"2022-08-19T19:21:22.190397Z","shell.execute_reply.started":"2022-08-19T19:21:22.112013Z","shell.execute_reply":"2022-08-19T19:21:22.189385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"load_img('../input/dog-breed-identification/test/0041940322116ae58c38130f5a6f71f9.jpg',target_size=(128,128))","metadata":{"execution":{"iopub.status.busy":"2022-08-19T19:21:22.193499Z","iopub.execute_input":"2022-08-19T19:21:22.193878Z","iopub.status.idle":"2022-08-19T19:21:22.212913Z","shell.execute_reply.started":"2022-08-19T19:21:22.193844Z","shell.execute_reply":"2022-08-19T19:21:22.211992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = pd.read_csv('../input/dog-breed-identification/labels.csv')\nlabels.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-19T19:21:22.214438Z","iopub.execute_input":"2022-08-19T19:21:22.21512Z","iopub.status.idle":"2022-08-19T19:21:22.241884Z","shell.execute_reply.started":"2022-08-19T19:21:22.215086Z","shell.execute_reply":"2022-08-19T19:21:22.241014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels['breed'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-08-19T19:21:22.242985Z","iopub.execute_input":"2022-08-19T19:21:22.243251Z","iopub.status.idle":"2022-08-19T19:21:22.25369Z","shell.execute_reply.started":"2022-08-19T19:21:22.243226Z","shell.execute_reply":"2022-08-19T19:21:22.252744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels['breed'].value_counts().plot.bar(figsize=(20,10))","metadata":{"execution":{"iopub.status.busy":"2022-08-19T19:21:22.255409Z","iopub.execute_input":"2022-08-19T19:21:22.255817Z","iopub.status.idle":"2022-08-19T19:21:23.908525Z","shell.execute_reply.started":"2022-08-19T19:21:22.255782Z","shell.execute_reply":"2022-08-19T19:21:23.907391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels['id'] = labels['id'].apply(lambda x: x + '.jpg')\nlabels.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-19T19:21:23.910375Z","iopub.execute_input":"2022-08-19T19:21:23.910781Z","iopub.status.idle":"2022-08-19T19:21:23.9269Z","shell.execute_reply.started":"2022-08-19T19:21:23.910745Z","shell.execute_reply":"2022-08-19T19:21:23.925909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen = ImageDataGenerator(featurewise_center=False,\n                             samplewise_center=False,\n                             featurewise_std_normalization=False,\n                             samplewise_std_normalization=False,\n                             zca_whitening=False,\n                             zoom_range=0.1,\n                             width_shift_range=0.2,\n                             height_shift_range=0.2,\n                             vertical_flip=False,\n                             rescale=1/255.0, \n                             validation_split=0.2,\n                             rotation_range=20,\n                             shear_range=0.1, \n                             horizontal_flip=True, \n                             fill_mode=\"nearest\")","metadata":{"execution":{"iopub.status.busy":"2022-08-19T19:21:23.930013Z","iopub.execute_input":"2022-08-19T19:21:23.930785Z","iopub.status.idle":"2022-08-19T19:21:23.937608Z","shell.execute_reply.started":"2022-08-19T19:21:23.930757Z","shell.execute_reply":"2022-08-19T19:21:23.936724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = datagen.flow_from_dataframe(labels, \n                                           directory='../input/dog-breed-identification/train/', \n                                           x_col='id', \n                                           y_col='breed', \n                                           target_size=(224,224), \n                                           class_mode='categorical', \n                                           batch_size=32, \n                                           shuffle=True, \n                                           seed=42, \n                                           subset='training')\n","metadata":{"execution":{"iopub.status.busy":"2022-08-19T19:21:23.939747Z","iopub.execute_input":"2022-08-19T19:21:23.94041Z","iopub.status.idle":"2022-08-19T19:21:24.274266Z","shell.execute_reply.started":"2022-08-19T19:21:23.940374Z","shell.execute_reply":"2022-08-19T19:21:24.273208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid = datagen.flow_from_dataframe(labels, \n                                           directory='../input/dog-breed-identification/train/', \n                                           x_col='id', \n                                           y_col='breed', \n                                           target_size=(224,224), \n                                           class_mode='categorical', \n                                           batch_size=32, \n                                           shuffle=True, \n                                           seed=42, \n                                           subset='validation')","metadata":{"execution":{"iopub.status.busy":"2022-08-19T19:21:24.275765Z","iopub.execute_input":"2022-08-19T19:21:24.276983Z","iopub.status.idle":"2022-08-19T19:21:24.37076Z","shell.execute_reply.started":"2022-08-19T19:21:24.276943Z","shell.execute_reply":"2022-08-19T19:21:24.36966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\n\nmodel.add(Conv2D(32, kernel_size=3,strides=1, activation=None, input_shape=(224,224,3)))\nmodel.add(BatchNormalization())\nmodel.add(Activation('relu'))\nmodel.add(MaxPool2D(2,2))\nmodel.add(Dropout(0.2))\n\nmodel.add(Conv2D(112, kernel_size=3,strides=1, activation=None))\nmodel.add(BatchNormalization())\nmodel.add(Activation('relu'))\n\nmodel.add(Conv2D(84, kernel_size=3,strides=1, activation=None))\nmodel.add(BatchNormalization())\nmodel.add(Activation('relu'))\nmodel.add(MaxPool2D(2,2))\nmodel.add(Dropout(0.3))\n\nmodel.add(Conv2D(64, kernel_size=3,strides=1, activation=None))\nmodel.add(BatchNormalization())\nmodel.add(Activation('relu'))\n\nmodel.add(Conv2D(56, kernel_size=3,strides=1, activation=None))\nmodel.add(BatchNormalization())\nmodel.add(Activation('relu'))\nmodel.add(MaxPool2D(2,2))\nmodel.add(Dropout(0.3))\n\nmodel.add(Conv2D(32, kernel_size=3,strides=1, activation=None))\nmodel.add(BatchNormalization())\nmodel.add(Activation('relu'))\n\nmodel.add(Conv2D(16, kernel_size=3,strides=1, activation=None))\nmodel.add(BatchNormalization())\nmodel.add(Activation('relu'))\n\nmodel.add(Flatten())\nmodel.add(Dense(64,activation='relu'))\nmodel.add(Dropout(0.5))\n\nmodel.add(Dense(units=1))\nmodel.add(Activation('sigmoid'))","metadata":{"execution":{"iopub.status.busy":"2022-08-19T19:21:24.372361Z","iopub.execute_input":"2022-08-19T19:21:24.372862Z","iopub.status.idle":"2022-08-19T19:21:25.610107Z","shell.execute_reply.started":"2022-08-19T19:21:24.372821Z","shell.execute_reply":"2022-08-19T19:21:25.609067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-08-19T19:21:25.613846Z","iopub.execute_input":"2022-08-19T19:21:25.614154Z","iopub.status.idle":"2022-08-19T19:21:25.628474Z","shell.execute_reply.started":"2022-08-19T19:21:25.614127Z","shell.execute_reply":"2022-08-19T19:21:25.627493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"early_stop = EarlyStopping(monitor='val_loss',patience=2)","metadata":{"execution":{"iopub.status.busy":"2022-08-19T19:21:25.629987Z","iopub.execute_input":"2022-08-19T19:21:25.631339Z","iopub.status.idle":"2022-08-19T19:21:25.636056Z","shell.execute_reply.started":"2022-08-19T19:21:25.631303Z","shell.execute_reply":"2022-08-19T19:21:25.635014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(train,epochs =3,validation_data = valid,callbacks=[early_stop])","metadata":{"execution":{"iopub.status.busy":"2022-08-19T19:21:25.637775Z","iopub.execute_input":"2022-08-19T19:21:25.638524Z","iopub.status.idle":"2022-08-19T19:30:30.632288Z","shell.execute_reply.started":"2022-08-19T19:21:25.638488Z","shell.execute_reply":"2022-08-19T19:30:30.631215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['accuracy'], label='training accuracy')\nplt.plot(history.history['val_accuracy'], label='validation accuracy')\nplt.xlabel('Epochs')\nplt.ylabel('Training and Validation Accuracy')\nplt.legend(loc='lower right')","metadata":{"execution":{"iopub.status.busy":"2022-08-19T19:30:42.047472Z","iopub.execute_input":"2022-08-19T19:30:42.047901Z","iopub.status.idle":"2022-08-19T19:30:42.266484Z","shell.execute_reply.started":"2022-08-19T19:30:42.047866Z","shell.execute_reply":"2022-08-19T19:30:42.265559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv('../input/dog-breed-identification/sample_submission.csv')\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-19T19:55:19.464148Z","iopub.execute_input":"2022-08-19T19:55:19.46453Z","iopub.status.idle":"2022-08-19T19:55:19.744024Z","shell.execute_reply.started":"2022-08-19T19:55:19.464496Z","shell.execute_reply":"2022-08-19T19:55:19.742881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-19T19:55:23.672496Z","iopub.execute_input":"2022-08-19T19:55:23.672882Z","iopub.status.idle":"2022-08-19T19:55:23.679557Z","shell.execute_reply.started":"2022-08-19T19:55:23.67285Z","shell.execute_reply":"2022-08-19T19:55:23.678552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_datagen = ImageDataGenerator ( \n    rescale=1./255\n)\n\ntest_set = test_datagen.flow_from_directory(\n    '/kaggle/input/dog-breed-identification',\n    target_size = (224,224),\n    batch_size = 32,\n    seed = 42,\n    classes=['test']\n)","metadata":{"execution":{"iopub.status.busy":"2022-08-19T20:20:33.744068Z","iopub.execute_input":"2022-08-19T20:20:33.74442Z","iopub.status.idle":"2022-08-19T20:20:36.237154Z","shell.execute_reply.started":"2022-08-19T20:20:33.74439Z","shell.execute_reply":"2022-08-19T20:20:36.23591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = model.predict(test_set)","metadata":{"execution":{"iopub.status.busy":"2022-08-19T20:20:38.72204Z","iopub.execute_input":"2022-08-19T20:20:38.722418Z","iopub.status.idle":"2022-08-19T20:21:21.522676Z","shell.execute_reply.started":"2022-08-19T20:20:38.722387Z","shell.execute_reply":"2022-08-19T20:21:21.521655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-19T20:10:02.687328Z","iopub.execute_input":"2022-08-19T20:10:02.688066Z","iopub.status.idle":"2022-08-19T20:10:02.695071Z","shell.execute_reply.started":"2022-08-19T20:10:02.688026Z","shell.execute_reply":"2022-08-19T20:10:02.694201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv('/kaggle/input/dog-breed-identification/sample_submission.csv')\nsubmission.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.iloc[:,1:] =pred","metadata":{"execution":{"iopub.status.busy":"2022-08-19T20:11:46.102055Z","iopub.execute_input":"2022-08-19T20:11:46.102432Z","iopub.status.idle":"2022-08-19T20:11:46.133768Z","shell.execute_reply.started":"2022-08-19T20:11:46.102398Z","shell.execute_reply":"2022-08-19T20:11:46.131901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}