{"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":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-08-03T03:51:24.654351Z","iopub.execute_input":"2021-08-03T03:51:24.654662Z","iopub.status.idle":"2021-08-03T03:51:24.662719Z","shell.execute_reply.started":"2021-08-03T03:51:24.654632Z","shell.execute_reply":"2021-08-03T03:51:24.661562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import package\nimport numpy as np\nimport pandas as pd \nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nimport seaborn as sns\nimport random\nimport os\nfrom zipfile import ZipFile\nfrom skimage.io import imread, imshow\nfrom sklearn.model_selection import train_test_split\nimport tensorflow as tf\nfrom tensorflow.keras.models import Sequential,load_model \nfrom tensorflow.keras.layers import Conv2D,MaxPooling2D,Flatten,Dense, Dropout, Activation, BatchNormalization\n\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint,ReduceLROnPlateau\nfrom tensorflow.keras.preprocessing.image import load_img, img_to_array\n\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2021-08-03T03:51:24.665288Z","iopub.execute_input":"2021-08-03T03:51:24.666034Z","iopub.status.idle":"2021-08-03T03:51:29.502806Z","shell.execute_reply.started":"2021-08-03T03:51:24.665993Z","shell.execute_reply":"2021-08-03T03:51:29.501953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import train and test data\ntrainDataPath = \"/kaggle/input/dogs-vs-cats/train.zip\"\ntestDataPath = \"/kaggle/input/dogs-vs-cats/test1.zip\"\nZipFile(trainDataPath,mode = \"r\").extractall()\nZipFile(testDataPath,mode =  \"r\").extractall()\n\ntrain_path = './train'\ntest_path = './test1'","metadata":{"execution":{"iopub.status.busy":"2021-08-03T03:51:29.504179Z","iopub.execute_input":"2021-08-03T03:51:29.504481Z","iopub.status.idle":"2021-08-03T03:51:51.856891Z","shell.execute_reply.started":"2021-08-03T03:51:29.504450Z","shell.execute_reply":"2021-08-03T03:51:51.856002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# set parameter\nSIZE  = 128\nCHANNELS = 3\nEPOCHS = 50\nBATCH_SIZE = 150\nIMG_SHAPE = 128","metadata":{"execution":{"iopub.status.busy":"2021-08-03T03:51:51.858618Z","iopub.execute_input":"2021-08-03T03:51:51.858964Z","iopub.status.idle":"2021-08-03T03:51:51.866302Z","shell.execute_reply.started":"2021-08-03T03:51:51.858936Z","shell.execute_reply":"2021-08-03T03:51:51.865420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# preparing training data\n\nfilenames = os.listdir(train_path)\ncategories = []\nfor filename in filenames:\n    category = filename.split('.')[0]\n    if category == 'cat':\n        categories.append(category)\n    else:\n        categories.append(category)\n\ndf = pd.DataFrame({\n    'Image': filenames,\n    'Category': categories})\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-03T03:51:51.867865Z","iopub.execute_input":"2021-08-03T03:51:51.868206Z","iopub.status.idle":"2021-08-03T03:51:51.925010Z","shell.execute_reply.started":"2021-08-03T03:51:51.868175Z","shell.execute_reply":"2021-08-03T03:51:51.924107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# preparing test data\ntest_files = os.listdir(test_path)\ndf_test = pd.DataFrame({'Image':test_files})\n\ndf_test.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-03T03:51:51.926249Z","iopub.execute_input":"2021-08-03T03:51:51.926598Z","iopub.status.idle":"2021-08-03T03:51:51.947108Z","shell.execute_reply.started":"2021-08-03T03:51:51.926563Z","shell.execute_reply":"2021-08-03T03:51:51.946390Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# check the total data\nsns.countplot(x='Category',data=df)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-08-03T03:51:51.948117Z","iopub.execute_input":"2021-08-03T03:51:51.948415Z","iopub.status.idle":"2021-08-03T03:51:52.095465Z","shell.execute_reply.started":"2021-08-03T03:51:51.948391Z","shell.execute_reply":"2021-08-03T03:51:52.094540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# slipt trainning data and validate data\ntrain_df, validation_df = train_test_split(df, test_size=0.20, random_state=50)\ntrain_df = train_df.reset_index(drop=True)\nvalidation_df = validation_df.reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2021-08-03T03:51:52.096908Z","iopub.execute_input":"2021-08-03T03:51:52.097270Z","iopub.status.idle":"2021-08-03T03:51:52.107957Z","shell.execute_reply.started":"2021-08-03T03:51:52.097232Z","shell.execute_reply":"2021-08-03T03:51:52.106736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Cat files\ncat_files = [file for file in filenames if file.split('.')[0] == 'cat']\nprint(len(cat_files))\n# Dog file \ndog_files = [file for file in filenames if file.split('.')[0] == 'dog']\nprint(len(dog_files))","metadata":{"execution":{"iopub.status.busy":"2021-08-03T03:51:52.110432Z","iopub.execute_input":"2021-08-03T03:51:52.110818Z","iopub.status.idle":"2021-08-03T03:51:52.131380Z","shell.execute_reply.started":"2021-08-03T03:51:52.110780Z","shell.execute_reply":"2021-08-03T03:51:52.130131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# data Augmentaion - trainning data\nimage_gen_train = ImageDataGenerator( \n    rescale=1./255, \n    rotation_range=45, \n    width_shift_range=0.3, \n    height_shift_range=0.3,\n    shear_range=0.1, \n    zoom_range=0.3, \n    horizontal_flip=True, \n    fill_mode='nearest'\n)\n\ntrain_dataset = image_gen_train.flow_from_dataframe(\n    train_df,\n    directory = train_path, \n    x_col='Image',\n    y_col='Category',\n    target_size=(IMG_SHAPE,IMG_SHAPE),\n    class_mode='categorical',\n    batch_size=BATCH_SIZE\n)","metadata":{"execution":{"iopub.status.busy":"2021-08-03T03:51:52.133274Z","iopub.execute_input":"2021-08-03T03:51:52.133802Z","iopub.status.idle":"2021-08-03T03:51:52.339588Z","shell.execute_reply.started":"2021-08-03T03:51:52.133762Z","shell.execute_reply":"2021-08-03T03:51:52.338825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# data Augmentaion - validation data\nimage_gen_val = ImageDataGenerator(rescale=1./255)\n\nvalidation_dataset = image_gen_val.flow_from_dataframe(\n    validation_df, \n    directory = train_path, \n    x_col = 'Image',\n    y_col = 'Category',\n    target_size= (IMG_SHAPE,IMG_SHAPE),\n    class_mode = 'categorical',\n    batch_size = BATCH_SIZE\n)","metadata":{"execution":{"iopub.status.busy":"2021-08-03T03:51:52.340949Z","iopub.execute_input":"2021-08-03T03:51:52.341300Z","iopub.status.idle":"2021-08-03T03:51:52.396981Z","shell.execute_reply.started":"2021-08-03T03:51:52.341263Z","shell.execute_reply":"2021-08-03T03:51:52.396127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# data Augmentaion - test data\ntest_datagen = ImageDataGenerator(rescale=1./255)\n\ntest_dataset = test_datagen.flow_from_dataframe(\n    df_test, \n    directory=test_path, \n    x_col='Image',\n    y_col=None,\n    class_mode=None,\n    target_size= (IMG_SHAPE,IMG_SHAPE),\n    batch_size=BATCH_SIZE,\n    shuffle=False\n)","metadata":{"execution":{"iopub.status.busy":"2021-08-03T03:51:52.398152Z","iopub.execute_input":"2021-08-03T03:51:52.398491Z","iopub.status.idle":"2021-08-03T03:51:52.499368Z","shell.execute_reply.started":"2021-08-03T03:51:52.398454Z","shell.execute_reply":"2021-08-03T03:51:52.498423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Init Model\nmodel = Sequential()\n\nmodel.add(Conv2D(32, (3, 3),input_shape=(IMG_SHAPE, IMG_SHAPE, 3), activation = 'relu'))   \nmodel.add(MaxPooling2D(pool_size = (2,2)))  \nmodel.add(Dropout(rate = 0.2))\n\nmodel.add(Conv2D(32, (3, 3), activation = 'relu')) \nmodel.add(MaxPooling2D(pool_size = (2,2)))         \nmodel.add(Dropout(rate = 0.3))\n\nmodel.add(Conv2D(64, (3, 3), activation = 'relu'))    \nmodel.add(MaxPooling2D(pool_size = (2,2)))            \nmodel.add(Dropout(rate = 0.4))\n\nmodel.add(Conv2D(128, (3, 3), activation = 'relu'))    \nmodel.add(MaxPooling2D(pool_size = (2,2)))           \nmodel.add(Dropout(rate = 0.5))\n\nmodel.add(Flatten())\nmodel.add(Dense(activation = 'relu', units = 128))\nmodel.add(Dense(2, activation='softmax'))\n\nmodel.summary()\n","metadata":{"execution":{"iopub.status.busy":"2021-08-03T03:51:52.500670Z","iopub.execute_input":"2021-08-03T03:51:52.501026Z","iopub.status.idle":"2021-08-03T03:51:54.950501Z","shell.execute_reply.started":"2021-08-03T03:51:52.500990Z","shell.execute_reply":"2021-08-03T03:51:54.949712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model compile\nmodel.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])\n","metadata":{"execution":{"iopub.status.busy":"2021-08-03T03:51:54.954402Z","iopub.execute_input":"2021-08-03T03:51:54.956624Z","iopub.status.idle":"2021-08-03T03:51:54.978431Z","shell.execute_reply.started":"2021-08-03T03:51:54.956583Z","shell.execute_reply":"2021-08-03T03:51:54.977105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#call backs\ncheckpoint_filepath = 'best_weights.hdf5'\n\nearlyStop = EarlyStopping(monitor='val_accuracy', patience=50, \n            verbose=1, mode='auto',restore_best_weights=True)\n\ncheckpoint = ModelCheckpoint(filepath=checkpoint_filepath, \n                                      save_weights_only=False, \n                                      monitor='val_accuracy',\n                                      mode='auto', \n                                      save_best_only=True)\ncallbacks = [earlyStop, checkpoint]","metadata":{"execution":{"iopub.status.busy":"2021-08-03T03:54:41.909318Z","iopub.execute_input":"2021-08-03T03:54:41.909674Z","iopub.status.idle":"2021-08-03T03:54:41.915944Z","shell.execute_reply.started":"2021-08-03T03:54:41.909644Z","shell.execute_reply":"2021-08-03T03:54:41.914929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(earlyStop)","metadata":{"execution":{"iopub.status.busy":"2021-08-03T03:54:42.635061Z","iopub.execute_input":"2021-08-03T03:54:42.635372Z","iopub.status.idle":"2021-08-03T03:54:42.642645Z","shell.execute_reply.started":"2021-08-03T03:54:42.635344Z","shell.execute_reply":"2021-08-03T03:54:42.640699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Model Fitting\nhistory = model.fit(train_dataset, \n                    validation_data = validation_dataset,\n                    epochs = EPOCHS,\n                    callbacks = callbacks)","metadata":{"execution":{"iopub.status.busy":"2021-08-03T03:54:43.342991Z","iopub.execute_input":"2021-08-03T03:54:43.343316Z","iopub.status.idle":"2021-08-03T05:44:47.042088Z","shell.execute_reply.started":"2021-08-03T03:54:43.343288Z","shell.execute_reply":"2021-08-03T05:44:47.041079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = history.history['accuracy']\nval_acc = history.history['val_accuracy']\nloss = history.history['loss']\nval_loss = history.history['val_loss']\nepochs_range = range(len(acc))\n\nplt.figure(figsize=(15, 15))\nplt.subplot(2, 2, 1)\nplt.plot(epochs_range, acc, label='Training Accuracy')\nplt.plot(epochs_range, val_acc, label='Validation Accuracy')\nplt.legend(loc='lower right')\nplt.title('Training and Validation Accuracy')\n\nplt.subplot(2, 2, 2)\nplt.plot(epochs_range, loss, label='Training Loss')\nplt.plot(epochs_range, val_loss, label='Validation Loss')\nplt.legend(loc='upper right')\nplt.title('Training and Validation Loss')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-08-03T05:44:47.044115Z","iopub.execute_input":"2021-08-03T05:44:47.044463Z","iopub.status.idle":"2021-08-03T05:44:47.325320Z","shell.execute_reply.started":"2021-08-03T05:44:47.044423Z","shell.execute_reply":"2021-08-03T05:44:47.324532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# test model\n\npredict = model.predict(test_dataset)\n\ndf_test['category'] = np.argmax(predict, axis=-1)\ndf_test.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-03T05:44:47.326981Z","iopub.execute_input":"2021-08-03T05:44:47.327476Z","iopub.status.idle":"2021-08-03T05:45:18.947817Z","shell.execute_reply.started":"2021-08-03T05:44:47.327437Z","shell.execute_reply":"2021-08-03T05:45:18.947027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_map = dict((v,k) for k,v in train_dataset.class_indices.items())\ndf_test['category'] = df_test['category'].replace(label_map)","metadata":{"execution":{"iopub.status.busy":"2021-08-03T05:45:18.949448Z","iopub.execute_input":"2021-08-03T05:45:18.950034Z","iopub.status.idle":"2021-08-03T05:45:18.957078Z","shell.execute_reply.started":"2021-08-03T05:45:18.949986Z","shell.execute_reply":"2021-08-03T05:45:18.956280Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#show result of predicted\nsample_test = df_test.head(15)\nsample_test.head()\nplt.figure(figsize=(12, 24))\nfor index, row in sample_test.iterrows():\n    filename = row['Image']\n    category = row['category']\n    img = load_img(test_path+'/' + filename, target_size=(SIZE,SIZE))\n    plt.subplot(6, 3, index+1)\n    plt.imshow(img)\n    plt.xlabel(filename + '(' + \"{}\".format(category) + ')' )\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-08-03T05:45:18.958366Z","iopub.execute_input":"2021-08-03T05:45:18.959027Z","iopub.status.idle":"2021-08-03T05:45:21.419480Z","shell.execute_reply.started":"2021-08-03T05:45:18.958981Z","shell.execute_reply":"2021-08-03T05:45:21.418452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#submit\n\nsubmission_df = df_test.copy()\nsubmission_df['id'] = submission_df['Image'].str.split('.').str[0]\nsubmission_df['label'] = submission_df['category']\nsubmission_df.drop(['Image', 'category'], axis=1, inplace=True)\nsubmission_df.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-08-03T05:45:21.420733Z","iopub.execute_input":"2021-08-03T05:45:21.421143Z","iopub.status.idle":"2021-08-03T05:45:21.490802Z","shell.execute_reply.started":"2021-08-03T05:45:21.421101Z","shell.execute_reply":"2021-08-03T05:45:21.490001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}