{"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":"markdown","source":"<p id=\"part0\"></p>\n\n<p style=\"font-family: Arials; line-height: 2; font-size: 44px; font-weight: bold; letter-spacing: 0px; text-align: center; color: #FF8C00\">Dogs vs Cats</p>\n\n<img src=\"https://editor.analyticsvidhya.com/uploads/999181_BIpRgx5FsEMhr1k2EqBKFg.gif\" width=\"90%\" align=\"center\" hspace=\"20%\" vspace=\"5%\"/>\n\n<p style=\"font-family: Arials; font-size: 40px; font-style: normal; font-weight: bold; letter-spacing: -2px; color: #000000; line-height:2.0\">Table of content:</p>\n\n<p style=\"font-family: Arials; font-size: 16px; font-style: normal; font-weight: bold; letter-spacing: 0px; color: #000000; line-height:.5\"><a href=\"#part1\" style=\"color:#000000\"> 1- Import relevant libraries</a></p>\n\n<p style=\"font-family: Arials; font-size: 16px; font-style: normal; font-weight: bold; letter-spacing: 0px; color: #000000; line-height:.5\"><a href=\"#part2\" style=\"color:#000000\">2- Unzip data</a></p>\n\n<p style=\"font-family: Arials; font-size: 16px; font-style: normal; font-weight: bold; letter-spacing: 0px; color: #000000; line-height:.5\"><a href=\"#part3\" style=\"color:#000000\">3- See some of the pictures</a></p>\n\n<p style=\"font-family: Arials; font-size: 16px; font-style: normal; font-weight: bold; letter-spacing: 0px; color: #000000; line-height:.5\"><a href=\"#part4\" style=\"color:#000000\">4- Create a dataframe from train data</a></p>\n\n<p style=\"font-family: Arials; font-size: 16px; font-style: normal; font-weight: bold; letter-spacing: 0px; color: #000000; line-height:.5\"><a href=\"#part5\" style=\"color:#000000\">5- Create a model </a></p>\n\n<p style=\"text-indent: 1vw; font-family: Arials; font-size: 16px; font-style: normal; font-weight: bold; letter-spacing: 0px; color: #000000; line-height:0.5\">\n<a href=\"#part6\" style=\"color:#000000\">5-1 Model Summary</a></p>\n\n<p style=\"text-indent: 1vw; font-family: Arials; font-size: 16px; font-style: normal; font-weight: bold; letter-spacing: 0px; color: #000000; line-height:0.5\">\n<a href=\"#part7\" style=\"color:#000000\">5-2 Split data to train and test section</a></p>\n\n<p style=\"text-indent: 1vw; font-family: Arials; font-size: 16px; font-style: normal; font-weight: bold; letter-spacing: 0px; color: #000000; line-height:0.5\">\n<a href=\"#part8\" style=\"color:#000000\">5-3 Genereate data by ImageDataGenereator </a></p>\n\n<p style=\"text-indent: 1vw; font-family: Arials; font-size: 16px; font-style: normal; font-weight: bold; letter-spacing: 0px; color: #000000; line-height:0.5\">\n<a href=\"#part9\" style=\"color:#000000\">5-4 Use Callbacks to have better contorl on training</a></p>\n\n<p style=\"text-indent: 1vw; font-family: Arials; font-size: 16px; font-style: normal; font-weight: bold; letter-spacing: 0px; color: #000000; line-height:0.5\">\n<a href=\"#part10\" style=\"color:#000000\">5-5 Fit model</a></p>\n\n<p style=\"font-family: Arials; font-size: 16px; font-style: normal; font-weight: bold; letter-spacing: 0px; color: #000000; line-height:.5\"><a href=\"#part11\" style=\"color:#000000\">6- Plot results </a></p>\n\n<p style=\"font-family: Arials; font-size: 16px; font-style: normal; font-weight: bold; letter-spacing: 0px; color: #000000; line-height:.5\"><a href=\"#part12\" style=\"color:#000000\">7- Evaluation on validation data with best model </a></p>\n\n<p style=\"font-family: Arials; font-size: 16px; font-style: normal; font-weight: bold; letter-spacing: 0px; color: #000000; line-height:.5\"><a href=\"#part13\" style=\"color:#000000\">8- Create a dataframe from test data and prepare it </a></p>\n\n<p style=\"font-family: Arials; font-size: 16px; font-style: normal; font-weight: bold; letter-spacing: 0px; color: #000000; line-height:.5\"><a href=\"#part14\" style=\"color:#000000\">9- Prediction on test data</a></p>\n\n<p style=\"font-family: Arials; font-size: 16px; font-style: normal; font-weight: bold; letter-spacing: 0px; color: #000000; line-height:.5\"><a href=\"#part15\" style=\"color:#000000\">10- Submit the final result</a></p>\n","metadata":{}},{"cell_type":"markdown","source":"<p id=\"part1\"></p>\n\n<p style=\"font-family: Times New Roman; font-size: 20px; font-style: bold; font-weight: bold; letter-spacing: 0px; color: #0000FF\">1- Import relevant libraries</p>\n<hr style=\"height: 1px; border: 1; background-color: #0000FF\">","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport random\nimport tensorflow as tf\nfrom tensorflow import keras\nimport os\nfrom keras import layers\nfrom keras import models\nfrom keras.layers import Dropout\nfrom keras import optimizers\nfrom sklearn.model_selection  import train_test_split\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras import callbacks","metadata":{"execution":{"iopub.status.busy":"2022-10-13T11:18:11.627063Z","iopub.execute_input":"2022-10-13T11:18:11.627499Z","iopub.status.idle":"2022-10-13T11:18:18.674126Z","shell.execute_reply.started":"2022-10-13T11:18:11.627414Z","shell.execute_reply":"2022-10-13T11:18:18.672045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<p id=\"part2\"></p>\n\n<p style=\"font-family: Times New Roman; font-size: 20px; font-style: bold; font-weight: bold; letter-spacing: 0px; color: #0000FF\">2- Unzip data</p>\n<hr style=\"height: 1px; border: 1; background-color: #0000FF\">","metadata":{}},{"cell_type":"code","source":"! unzip -qq \"../input/dogs-vs-cats/test1.zip\" \n! unzip -qq \"../input/dogs-vs-cats/train.zip\"","metadata":{"execution":{"iopub.status.busy":"2022-10-13T11:18:18.675990Z","iopub.execute_input":"2022-10-13T11:18:18.676694Z","iopub.status.idle":"2022-10-13T11:18:35.087384Z","shell.execute_reply.started":"2022-10-13T11:18:18.676652Z","shell.execute_reply":"2022-10-13T11:18:35.086007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<p id=\"part3\"></p>\n\n<p style=\"font-family: Times New Roman; font-size: 20px; font-style: bold; font-weight: bold; letter-spacing: 0px; color: #0000FF\">3- See Some of pictures\n<hr style=\"height: 1px; border: 1; background-color: #0000FF\">","metadata":{}},{"cell_type":"code","source":"filenames = os.listdir('./train/')\nfor i in range(2):\n    picture = np.array(plt.imread(os.path.join('./train',random.choice(filenames))))\n    plt.imshow(picture)\n    plt.show()\n    plt.figure()","metadata":{"execution":{"iopub.status.busy":"2022-10-13T11:18:35.089581Z","iopub.execute_input":"2022-10-13T11:18:35.089971Z","iopub.status.idle":"2022-10-13T11:18:35.599285Z","shell.execute_reply.started":"2022-10-13T11:18:35.089930Z","shell.execute_reply":"2022-10-13T11:18:35.598253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<p id=\"part4\"></p>\n\n<p style=\"font-family: Times New Roman; font-size: 20px; font-style: bold; font-weight: bold; letter-spacing: 0px; color: #0000FF\">4-Create a dataframe from train data\n<hr style=\"height: 1px; border: 1; background-color: #0000FF\">","metadata":{}},{"cell_type":"code","source":"categories = []\nfor filename in filenames:\n    category= filename.split('.')[0]\n    if category == 'cat':\n        categories.append('0')\n    else:\n        categories.append('1')\n\ndf = pd.DataFrame({'filename':filenames,'category':categories})\nprint('df.shape:',df.shape)\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2022-10-13T11:18:35.602204Z","iopub.execute_input":"2022-10-13T11:18:35.603089Z","iopub.status.idle":"2022-10-13T11:18:35.846774Z","shell.execute_reply.started":"2022-10-13T11:18:35.603045Z","shell.execute_reply":"2022-10-13T11:18:35.840903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Number of dogs in the train dataset:',df.category[df.category=='1'].count())\nprint('Number of cats in the train dataset:',df.category[df.category=='0'].count())\ndf['category'].value_counts().plot.bar(color=['green','blue'])","metadata":{"execution":{"iopub.status.busy":"2022-10-13T11:18:35.851759Z","iopub.execute_input":"2022-10-13T11:18:35.852501Z","iopub.status.idle":"2022-10-13T11:18:36.164613Z","shell.execute_reply.started":"2022-10-13T11:18:35.852449Z","shell.execute_reply":"2022-10-13T11:18:36.163614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<p id=\"part5\"></p>\n\n<p style=\"font-family: Times New Roman; font-size: 20px; font-style: bold; font-weight: bold; letter-spacing: 0px; color: #0000FF\">5- Create Model</p>\n<hr style=\"height: 1px; border: 1; background-color: #0000FF\">","metadata":{}},{"cell_type":"code","source":"Image_Size = (256,256)\nChannel_Size = 3\nBatch_Size = 64\n\n# Use CNN Artitechture\ndef create_model():\n    model = models.Sequential()\n    model.add(layers.Conv2D(32, (3, 3), activation='relu',\n                            input_shape=(256,256,3)))\n    model.add(layers.MaxPooling2D((2, 2)))\n    \n    model.add(layers.Conv2D(64, (3, 3), activation='relu'))\n    model.add(layers.MaxPooling2D((2, 2)))\n    \n    model.add(layers.Conv2D(128, (3, 3), activation='relu'))\n    model.add(layers.MaxPooling2D((2, 2)))\n    \n    model.add(layers.Conv2D(256, (3, 3), activation='relu'))\n    model.add(layers.MaxPooling2D((2, 2)))\n    \n    model.add(layers.Flatten())\n    model.add(layers.Dense(128,activation='relu'))\n    model.add(Dropout(0.2))\n    \n    model.add(layers.Dense(256,activation='relu'))\n    model.add(Dropout(0.5))\n    \n    model.add(layers.Dense(2, activation='softmax'))\n    \n    model.compile(loss='categorical_crossentropy',\n              optimizer='rmsprop',\n              metrics=['accuracy'],)\n    return model\n\nmy_model = create_model()","metadata":{"execution":{"iopub.status.busy":"2022-10-13T11:18:36.166136Z","iopub.execute_input":"2022-10-13T11:18:36.167194Z","iopub.status.idle":"2022-10-13T11:18:39.031478Z","shell.execute_reply.started":"2022-10-13T11:18:36.167156Z","shell.execute_reply":"2022-10-13T11:18:39.030446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<p id=\"part6\"></p>\n\n<p style=\"font-family: Times New Roman; font-size: 20px; font-style: bold; font-weight: bold; letter-spacing: 0px; color: #0000FF\">5-1 Model Summary</p>\n<hr style=\"height: 1px; border: 1; background-color: #0000FF\">","metadata":{}},{"cell_type":"code","source":"my_model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-10-13T11:18:39.032961Z","iopub.execute_input":"2022-10-13T11:18:39.033317Z","iopub.status.idle":"2022-10-13T11:18:39.041415Z","shell.execute_reply.started":"2022-10-13T11:18:39.033289Z","shell.execute_reply":"2022-10-13T11:18:39.040483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<p id=\"part7\"></p>\n\n<p style=\"font-family: Times New Roman; font-size: 20px; font-style: bold; font-weight: bold; letter-spacing: 0px; color: #0000FF\">5-2 Split data to train and test section</p>\n<hr style=\"height: 1px; border: 1; background-color: #0000FF\">","metadata":{}},{"cell_type":"code","source":"train_df, validation_df = train_test_split(df, test_size=0.2, random_state=45)\ntrain_df = train_df.reset_index(drop=True)\nvalidation_df = validation_df.reset_index(drop=True)\nprint(f'train_df.shpae:{train_df.shape} & validation_df.shape:{validation_df.shape}')","metadata":{"execution":{"iopub.status.busy":"2022-10-13T11:18:39.043021Z","iopub.execute_input":"2022-10-13T11:18:39.043729Z","iopub.status.idle":"2022-10-13T11:18:39.062888Z","shell.execute_reply.started":"2022-10-13T11:18:39.043692Z","shell.execute_reply":"2022-10-13T11:18:39.062075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<p id=\"part8\"></p>\n\n<p style=\"font-family: Times New Roman; font-size: 20px; font-style: bold; font-weight: bold; letter-spacing: 0px; color: #0000FF\">5-3 Genereate data by ImageDataGenereator</p>\n<hr style=\"height: 1px; border: 1; background-color: #0000FF\">","metadata":{}},{"cell_type":"code","source":"# We generate data and do data agumentation to avoid overfitting\n\n# Train_Section:\ntrain_datagen = ImageDataGenerator(\n    rotation_range=15,\n    rescale=1./255,\n    shear_range=0.1,\n    zoom_range=0.4,\n    horizontal_flip=True)\n\ntrain_generator = train_datagen.flow_from_dataframe(\n    train_df,\n    './train',\n    x_col='filename',\n    y_col='category',\n    target_size=Image_Size,\n    class_mode='categorical',\n    batch_size=Batch_Size\n)\n\n\n#Validation_Section:---> Note: You don not need to do data agumentation on validation data\nvalidation_datagen = ImageDataGenerator(\n    rescale=1./255)\n\nvalidation_generator = train_datagen.flow_from_dataframe(\n    validation_df,\n    './train',\n    x_col='filename',\n    y_col='category',\n    target_size=Image_Size,\n    class_mode='categorical',\n    batch_size=Batch_Size\n)\n","metadata":{"execution":{"iopub.status.busy":"2022-10-13T11:18:39.064155Z","iopub.execute_input":"2022-10-13T11:18:39.064569Z","iopub.status.idle":"2022-10-13T11:18:39.288282Z","shell.execute_reply.started":"2022-10-13T11:18:39.064531Z","shell.execute_reply":"2022-10-13T11:18:39.287241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<p id=\"part9\"></p>\n\n<p style=\"font-family: Times New Roman; font-size: 20px; font-style: bold; font-weight: bold; letter-spacing: 0px; color: #0000FF\">5-4 Use Callbacks to have better contorl on training</p>\n<hr style=\"height: 1px; border: 1; background-color: #0000FF\">","metadata":{}},{"cell_type":"code","source":"# 1st_callbak : EarlyStoping\nMy_EarlyStopping_callback = callbacks.EarlyStopping(monitor=\"val_loss\",\n                                                 patience=3,\n                                                 verbose=1,\n                                                 mode=\"auto\")\n\n\n#2nd_callback : ModelCheckPoint\ncheckpoint_path = './Checkpint_Folder/cp.ckpt' # The path to save wights of the model after training\ncheckpoint_dir =os.path.dirname(checkpoint_path)\nMy_ModelCheckpoint_callback =callbacks.ModelCheckpoint(filepath=checkpoint_path,\n                                          metrics='val_loss',\n                                          verbose=1,\n                                          save_weights_only = True,\n                                          save_best_only=True,\n                                          mode='auto',\n                                          save_freq ='epoch')\n\nMy_callbacks = [My_EarlyStopping_callback, My_ModelCheckpoint_callback]","metadata":{"execution":{"iopub.status.busy":"2022-10-13T11:18:39.291934Z","iopub.execute_input":"2022-10-13T11:18:39.292219Z","iopub.status.idle":"2022-10-13T11:18:39.299223Z","shell.execute_reply.started":"2022-10-13T11:18:39.292193Z","shell.execute_reply":"2022-10-13T11:18:39.298068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<p id=\"part10\"></p>\n\n<p style=\"font-family: Times New Roman; font-size: 20px; font-style: bold; font-weight: bold; letter-spacing: 0px; color: #0000FF\">5-5 Fit model</p>\n<hr style=\"height: 1px; border: 1; background-color: #0000FF\">","metadata":{}},{"cell_type":"code","source":"History = my_model.fit(\n    train_generator,\n    epochs=15,\n    validation_data=(validation_generator),\n    batch_size = Batch_Size,\n    callbacks = My_callbacks\n)","metadata":{"execution":{"iopub.status.busy":"2022-10-13T11:18:39.300876Z","iopub.execute_input":"2022-10-13T11:18:39.301249Z","iopub.status.idle":"2022-10-13T11:19:09.277488Z","shell.execute_reply.started":"2022-10-13T11:18:39.301213Z","shell.execute_reply":"2022-10-13T11:19:09.274002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<p id=\"part11\"></p>\n\n<p style=\"font-family: Times New Roman; font-size: 20px; font-style: bold; font-weight: bold; letter-spacing: 0px; color: #0000FF\">6- Plot results</p>\n<hr style=\"height: 1px; border: 1; background-color: #0000FF\">","metadata":{}},{"cell_type":"code","source":"accuracy = History.history['accuracy']\nval_accuracy = History.history['val_accuracy']\nloss = History.history['loss']\nval_loss = History.history['val_loss']\n\nepochs = range(len(accuracy))\n\nplt.plot(epochs,accuracy, label='accuracy',color='red')\nplt.plot(epochs,val_accuracy, label='val_accuracy',color='blue')\nplt.title('Training and validation loss')\nplt.legend()\nplt.figure()\n\nplt.plot(epochs,loss,label='loss',color='black')\nplt.plot(epochs,val_loss,label='val_loss',color='green')\nplt.title('Training and validation loss')\nplt.legend()\nplt.figure()","metadata":{"execution":{"iopub.status.busy":"2022-10-13T11:19:09.280308Z","iopub.status.idle":"2022-10-13T11:19:09.282887Z","shell.execute_reply.started":"2022-10-13T11:19:09.282606Z","shell.execute_reply":"2022-10-13T11:19:09.282633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<p id=\"part12\"></p>\n\n<p style=\"font-family: Times New Roman; font-size: 20px; font-style: bold; font-weight: bold; letter-spacing: 0px; color: #0000FF\">7- Evaluation on validation data with best model</p>\n<hr style=\"height: 1px; border: 1; background-color: #0000FF\">","metadata":{}},{"cell_type":"code","source":"best_model = create_model()\nbest_model.load_weights(checkpoint_path)\nloss,accuracy = best_model.evaluate(validation_generator)\nprint('loss on validation data:',loss*100)\nprint('accuracy on validation data', accuracy*100)","metadata":{"execution":{"iopub.status.busy":"2022-10-13T11:19:09.286941Z","iopub.status.idle":"2022-10-13T11:19:09.289291Z","shell.execute_reply.started":"2022-10-13T11:19:09.289046Z","shell.execute_reply":"2022-10-13T11:19:09.289072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<p id=\"part13\"></p>\n\n<p style=\"font-family: Times New Roman; font-size: 20px; font-style: bold; font-weight: bold; letter-spacing: 0px; color: #0000FF\">8- Create a dataframe from test data and prepare it</p>\n<hr style=\"height: 1px; border: 1; background-color: #0000FF\">\n","metadata":{}},{"cell_type":"code","source":"test_filenames = os.listdir('./test1/')\ntest_df = pd.DataFrame({'filename':test_filenames})\nprint('test_df.shape:',test_df.shape)\ntest_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-10-13T11:19:09.292014Z","iopub.status.idle":"2022-10-13T11:19:09.292790Z","shell.execute_reply.started":"2022-10-13T11:19:09.292536Z","shell.execute_reply":"2022-10-13T11:19:09.292562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_datagen = ImageDataGenerator(rescale=1./255)\ntest_generator = test_datagen.flow_from_dataframe(\n    test_df, \n    \"./test1/\", \n    x_col='filename',\n    y_col=None,\n    class_mode=None,\n    target_size=Image_Size,\n    batch_size=Batch_Size,\n    shuffle=False\n)","metadata":{"execution":{"iopub.status.busy":"2022-10-13T11:19:09.298941Z","iopub.status.idle":"2022-10-13T11:19:09.299734Z","shell.execute_reply.started":"2022-10-13T11:19:09.299471Z","shell.execute_reply":"2022-10-13T11:19:09.299497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<p id=\"part14\"></p>\n\n<p style=\"font-family: Times New Roman; font-size: 20px; font-style: bold; font-weight: bold; letter-spacing: 0px; color: #0000FF\">9- Prediction on test data</p>\n<hr style=\"height: 1px; border: 1; background-color: #0000FF\">","metadata":{}},{"cell_type":"code","source":"Predict = best_model.predict(test_generator)\n# add the results to test_df as category column\ntest_df['category'] = np.argmax(Predict, axis=-1)","metadata":{"execution":{"iopub.status.busy":"2022-10-13T11:19:09.301160Z","iopub.status.idle":"2022-10-13T11:19:09.301905Z","shell.execute_reply.started":"2022-10-13T11:19:09.301659Z","shell.execute_reply":"2022-10-13T11:19:09.301684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<p id=\"part15\"></p>\n\n<p style=\"font-family: Times New Roman; font-size: 20px; font-style: bold; font-weight: bold; letter-spacing: 0px; color: #0000FF\">10- Submit the final result</p>\n<hr style=\"height: 1px; border: 1; background-color: #0000FF\">","metadata":{}},{"cell_type":"code","source":"submission_df = test_df.copy()\nsubmission_df['id'] = submission_df['filename'].str.split('.').str[0]\nsubmission_df['label'] = submission_df['category']\nsubmission_df.drop(['filename', 'category'], axis=1, inplace=True)\nsubmission_df.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-10-13T11:19:09.303152Z","iopub.status.idle":"2022-10-13T11:19:09.303817Z","shell.execute_reply.started":"2022-10-13T11:19:09.303593Z","shell.execute_reply":"2022-10-13T11:19:09.303616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}