{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"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)\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\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","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_labels = pd.read_csv(\"/kaggle/input/dog-breed-identification/labels.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_labels","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_labels['id'] = df_labels['id'] + '.jpg'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_labels","execution_count":null,"outputs":[]},{"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":{"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]\nclasses","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.layers import Dense, Activation, Flatten, Conv2D, MaxPooling2D, Dropout       \n\nfrom keras.models import Sequential\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model=Sequential()\n\n\nmodel.add(Conv2D(32, (3, 3),input_shape=(150,150,3)))   #32 filters, each filter 3x3\nmodel.add(Activation('relu'))\n\n\nmodel.add(MaxPooling2D(pool_size=(2,2)))\n#reduces dimensions of image- (12,12,64)\n#4 pixelels merged into 1, channels remains the same\n\nmodel.add(Conv2D(32, (3, 3)) )  #16 filters, each filter 3x3\nmodel.add(Activation('relu'))\n\n\nmodel.add(Flatten())   #After flattening.. total features will be 28*28=784\n\nmodel.add(Dropout(0.25))  #25% of random weights will be reduced to zero\n\nmodel.add(Dense(units=128, activation='relu'))\n\n# Step 5 - Output Layer\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":"id_list[:10]","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":{"trusted":true},"cell_type":"code","source":"","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}