{"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 matplotlib.pyplot as plt\nimport tensorflow as tf\nimport zipfile\nimport pandas as pd\nimport numpy as np\n\nimport warnings\nwarnings.filterwarnings('ignore')\n\nfrom tensorflow import keras\nfrom keras import layers\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.utils import image_dataset_from_directory\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img\nfrom tensorflow.keras.preprocessing import image_dataset_from_directory","metadata":{"id":"-pl1Th96o0lH","execution":{"iopub.status.busy":"2022-03-20T13:17:02.634884Z","iopub.execute_input":"2022-03-20T13:17:02.635469Z","iopub.status.idle":"2022-03-20T13:17:02.648618Z","shell.execute_reply.started":"2022-03-20T13:17:02.635426Z","shell.execute_reply":"2022-03-20T13:17:02.647197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!nvidia-smi","metadata":{"id":"ekK8Ry0wWG-l","outputId":"6a6e2038-276b-495c-ae81-e6c5f7786909","execution":{"iopub.status.busy":"2022-03-20T13:17:04.655264Z","iopub.execute_input":"2022-03-20T13:17:04.655566Z","iopub.status.idle":"2022-03-20T13:17:05.437519Z","shell.execute_reply.started":"2022-03-20T13:17:04.655536Z","shell.execute_reply":"2022-03-20T13:17:05.436443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_gen = image_dataset_from_directory('../input/dog-vs-cat-classification/train/train',\n                                        labels='inferred',\n                                        color_mode='rgb',\n                                        label_mode='binary', \n                                        batch_size=32,\n                                        image_size=(400, 400),\n                                        shuffle=True,\n                                        seed=1,\n                                        validation_split=0.1,\n                                        subset='training')\n\nval_gen = image_dataset_from_directory('../input/dog-vs-cat-classification/train/train',\n                                       labels='inferred',\n                                       label_mode='binary', \n                                       color_mode='rgb',\n                                       batch_size=32,\n                                       image_size=(400, 400),\n                                       shuffle=True,\n                                       seed=1,\n                                       validation_split=0.1,\n                                       subset='validation')","metadata":{"id":"BRmi2bal-JJw","outputId":"746d5021-57f3-41f8-9437-34155d7561d5","execution":{"iopub.status.busy":"2022-03-20T13:41:26.441123Z","iopub.execute_input":"2022-03-20T13:41:26.441447Z","iopub.status.idle":"2022-03-20T13:41:34.341513Z","shell.execute_reply.started":"2022-03-20T13:41:26.441417Z","shell.execute_reply":"2022-03-20T13:41:34.340515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport matplotlib.image as mpimg \nfig = plt.gcf()\nfig.set_size_inches(16,16)\n\ncat_dir = os.path.join('../input/dog-vs-cat-classification/train/train/cats')\ndog_dir = os.path.join('../input/dog-vs-cat-classification/train/train/dogs')\ncat_names = os.listdir(cat_dir)\ndog_names = os.listdir(dog_dir)\n\npic_index = 210\n\ncat_images = [os.path.join(cat_dir, fname) for fname in cat_names[pic_index-8:pic_index]]\ndog_images = [os.path.join(dog_dir, fname) for fname in dog_names[pic_index-8:pic_index]]\n\nfor i, img_path in enumerate(cat_images + dog_images):\n  sp = plt.subplot(4,4,i+1)\n  sp.axis('Off')\n\n  img = mpimg.imread(img_path)\n  plt.imshow(img)\n\nplt.show()","metadata":{"id":"7HpsJ7AtNDuS","outputId":"90d15569-6f56-491a-f111-db5da7449bf5","execution":{"iopub.status.busy":"2022-03-20T13:41:34.346911Z","iopub.execute_input":"2022-03-20T13:41:34.349276Z","iopub.status.idle":"2022-03-20T13:41:35.752689Z","shell.execute_reply.started":"2022-03-20T13:41:34.349231Z","shell.execute_reply":"2022-03-20T13:41:35.750656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.models.Sequential([\n                                    layers.Conv2D(32,(3,3),activation='relu',input_shape=(400,400,3)),\n                                    layers.MaxPooling2D(2,2),\n                                    layers.Conv2D(64,(3,3),activation='relu'),\n                                    layers.MaxPooling2D(2,2),\n                                    layers.Conv2D(64,(3,3),activation='relu'),\n                                    layers.MaxPooling2D(2,2),\n                                    layers.Conv2D(64,(3,3),activation='relu'),\n                                    layers.MaxPooling2D(2,2),\n\n                                    layers.Flatten(),\n                                    layers.Dense(512,activation='relu'),\n                                    layers.BatchNormalization(),\n                                    layers.Dense(512,activation='relu'),\n                                    layers.Dropout(0.1),\n                                    layers.BatchNormalization(),\n                                    layers.Dense(512,activation='relu'),\n                                    layers.Dropout(0.2),\n                                    layers.BatchNormalization(),\n                                    layers.Dense(1,activation='sigmoid')\n])","metadata":{"id":"y27Q7YIJIrsE","execution":{"iopub.status.busy":"2022-03-20T13:41:35.754044Z","iopub.execute_input":"2022-03-20T13:41:35.754957Z","iopub.status.idle":"2022-03-20T13:41:35.917925Z","shell.execute_reply.started":"2022-03-20T13:41:35.754915Z","shell.execute_reply":"2022-03-20T13:41:35.916991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.build(input_shape=(None,400,400,3))\nmodel.summary()","metadata":{"id":"xp1ASJkOMQLU","outputId":"a97ebe50-d6fe-4abe-e9a7-e4519ba6915a","execution":{"iopub.status.busy":"2022-03-20T13:41:35.920363Z","iopub.execute_input":"2022-03-20T13:41:35.921060Z","iopub.status.idle":"2022-03-20T13:41:35.945502Z","shell.execute_reply.started":"2022-03-20T13:41:35.920997Z","shell.execute_reply":"2022-03-20T13:41:35.943906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(\n    loss='binary_crossentropy',\n    optimizer='adam',\n    metrics=['accuracy']\n)","metadata":{"id":"9pr1cE7howq3","execution":{"iopub.status.busy":"2022-03-20T13:41:35.947183Z","iopub.execute_input":"2022-03-20T13:41:35.947488Z","iopub.status.idle":"2022-03-20T13:41:35.959180Z","shell.execute_reply.started":"2022-03-20T13:41:35.947447Z","shell.execute_reply":"2022-03-20T13:41:35.958206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(\n          train_gen,\n          steps_per_epoch = 704,\n          epochs = 4,\n          verbose = 1,\n          validation_data = val_gen,\n          validation_steps = 79)","metadata":{"id":"YEbMUHpfMSWI","outputId":"1629efd0-bc4d-4709-97f4-8321b3dfdcc5","execution":{"iopub.status.busy":"2022-03-20T13:42:02.661736Z","iopub.execute_input":"2022-03-20T13:42:02.662037Z","iopub.status.idle":"2022-03-20T13:50:58.488186Z","shell.execute_reply.started":"2022-03-20T13:42:02.662006Z","shell.execute_reply":"2022-03-20T13:50:58.487204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_df = pd.DataFrame(history.history)\nhistory_df.loc[:,['loss','val_loss']].plot()\nhistory_df.loc[:,['accuracy','val_accuracy']].plot()\nplt.show()","metadata":{"id":"aFH-H4IGS4Yz","outputId":"63aa296e-dc95-44ac-a9a5-f9981ed82008","execution":{"iopub.status.busy":"2022-03-20T13:50:58.493717Z","iopub.execute_input":"2022-03-20T13:50:58.494654Z","iopub.status.idle":"2022-03-20T13:50:59.040993Z","shell.execute_reply.started":"2022-03-20T13:50:58.494614Z","shell.execute_reply":"2022-03-20T13:50:59.040003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\npath_test = '../input/dog-vs-cat-classification/test/test'\n\nall_test = os.listdir(path_test)\n\ntest_df = pd.DataFrame({'id': all_test})","metadata":{"id":"eFNWP5Tomobj","execution":{"iopub.status.busy":"2022-03-20T13:51:22.423116Z","iopub.execute_input":"2022-03-20T13:51:22.423485Z","iopub.status.idle":"2022-03-20T13:51:22.777888Z","shell.execute_reply.started":"2022-03-20T13:51:22.423455Z","shell.execute_reply":"2022-03-20T13:51:22.776832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_gen = ImageDataGenerator()\ntest_generator = test_gen.flow_from_dataframe(\n    test_df,\n    path_test,\n    x_col='id',\n    y_col=None,\n    class_mode=None,\n    target_size=(400,400),\n    batch_size=128,\n    shuffle=False\n)","metadata":{"id":"jyDARbmamAUY","execution":{"iopub.status.busy":"2022-03-20T13:52:07.591457Z","iopub.execute_input":"2022-03-20T13:52:07.591779Z","iopub.status.idle":"2022-03-20T13:52:22.137571Z","shell.execute_reply.started":"2022-03-20T13:52:07.591748Z","shell.execute_reply":"2022-03-20T13:52:22.136603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict = model.predict(test_generator)\npredict = np.where(predict>0.5,1,0)\ntest_df['labels'] = predict","metadata":{"id":"Q56TCVJvm5il","execution":{"iopub.status.busy":"2022-03-20T13:52:36.139037Z","iopub.execute_input":"2022-03-20T13:52:36.140007Z","iopub.status.idle":"2022-03-20T13:54:08.470673Z","shell.execute_reply.started":"2022-03-20T13:52:36.139942Z","shell.execute_reply":"2022-03-20T13:54:08.469570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = test_df.sort_values(by='id')\ntest_df.to_csv('submission.csv',index=False)\ntest_df.head()","metadata":{"id":"UXPlSCXim7xB","execution":{"iopub.status.busy":"2022-03-20T13:54:12.978621Z","iopub.execute_input":"2022-03-20T13:54:12.979078Z","iopub.status.idle":"2022-03-20T13:54:13.048189Z","shell.execute_reply.started":"2022-03-20T13:54:12.979036Z","shell.execute_reply":"2022-03-20T13:54:13.047187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df['labels'].value_counts()","metadata":{"id":"UCY9sqC_RqZl","execution":{"iopub.status.busy":"2022-03-20T13:54:19.249470Z","iopub.execute_input":"2022-03-20T13:54:19.249770Z","iopub.status.idle":"2022-03-20T13:54:19.261473Z","shell.execute_reply.started":"2022-03-20T13:54:19.249740Z","shell.execute_reply":"2022-03-20T13:54:19.260218Z"},"trusted":true},"execution_count":null,"outputs":[]}]}