{"cells":[{"metadata":{},"cell_type":"markdown","source":"### Importing packages"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\nimport tensorflow as tf","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Load data"},{"metadata":{"trusted":true},"cell_type":"code","source":"df_labels = pd.read_csv(\"/kaggle/input/dog-breed-identification/labels.csv\")\ndf_labels['id'] = df_labels['id'] + '.jpg'\ndf_labels.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Splitting data"},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\ndf_train, df_validation = train_test_split(df_labels, test_size=0.2, shuffle=True)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Verifying data and generating images for testing"},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\n\ntrain_datagen = ImageDataGenerator(\n        rotation_range=40,\n        width_shift_range=0.2,\n        height_shift_range=0.2,\n        rescale=1./255,\n        shear_range=0.2,\n        zoom_range=0.2,\n        horizontal_flip=True,\n        fill_mode='nearest'\n)\n\nvalidation_datagen = ImageDataGenerator(\n        rescale=1./255)\n\ntraining_set = train_datagen.flow_from_dataframe(\n    dataframe=df_train,\n    directory='/kaggle/input/dog-breed-identification/train',\n    x_col=\"id\",\n    y_col=\"breed\",\n    target_size=(150, 150),\n    class_mode=\"categorical\",\n    batch_size=128\n)\n\nvalidation_set = validation_datagen.flow_from_dataframe(\n    dataframe=df_validation,\n    directory='/kaggle/input/dog-breed-identification/train',\n    x_col=\"id\",\n    y_col=\"breed\",\n    target_size=(150, 150),\n    class_mode=\"categorical\",\n    batch_size=128\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"classes = df_labels['breed'].unique().shape[0]\nprint(\"Total breeds: \", classes)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Model"},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.layers import Conv2D, MaxPooling2D, GlobalAveragePooling2D\nfrom keras.layers import Dropout, Flatten, Dense\nfrom keras.models import Sequential\n\nmodel = Sequential()\n\n\nmodel.add(Conv2D(32, (3, 3),input_shape=(150,150,3), activation = 'relu'))\n\nmodel.add(MaxPooling2D(pool_size=(2,2)))\n\nmodel.add(Conv2D(32, (3, 3), activation = 'relu'))\n\nmodel.add(Flatten())\nmodel.add(Dropout(0.25))\n\nmodel.add(Dense(units=128, activation='relu'))\nmodel.add(Dense(units=classes, activation='softmax'))\n\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.compile(optimizer='adam',\n             loss='categorical_crossentropy',\n             metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.fit(x = training_set, validation_data = validation_set, epochs = 20)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"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 = (150,150),\n    batch_size = 128,\n    classes=['test']\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_pred = model.predict(test_set)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_submission = pd.read_csv('/kaggle/input/dog-breed-identification/sample_submission.csv')\ndf_submission","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import re\nfile_list = test_set.filenames\nid_list = []\nfor name in file_list:\n    m = re.sub('test/', '', name)\n    m = re.sub('.jpg', '', m)\n    id_list.append(m)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_submission['id'] = id_list\ndf_submission.iloc[:,1:] = y_pred\ndf_submission","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"final_df = df_submission.set_index('id')\nfinal_df.to_csv('Submission.csv')","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}