{"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":"!pip install visualkeras --quiet\nimport pandas as pd\nimport numpy as np\nfrom keras.preprocessing.image import ImageDataGenerator\nimport tensorflow as tf\nimport matplotlib.pyplot as plt\nimport matplotlib.image as img\nfrom sklearn.model_selection import train_test_split\nfrom keras.utils import to_categorical\nimport visualkeras\nfrom sklearn.metrics import classification_report\nimport keras\nimport zipfile\nimport os\nfrom tensorflow.keras.callbacks import EarlyStopping\n\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-05-28T22:58:32.780455Z","iopub.execute_input":"2023-05-28T22:58:32.781092Z","iopub.status.idle":"2023-05-28T22:58:43.794922Z","shell.execute_reply.started":"2023-05-28T22:58:32.781017Z","shell.execute_reply":"2023-05-28T22:58:43.793609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Examine the data","metadata":{}},{"cell_type":"code","source":"os.listdir('../input/dogs-vs-cats-redux-kernels-edition')","metadata":{"execution":{"iopub.status.busy":"2023-05-28T22:15:08.524437Z","iopub.execute_input":"2023-05-28T22:15:08.524834Z","iopub.status.idle":"2023-05-28T22:15:08.534458Z","shell.execute_reply.started":"2023-05-28T22:15:08.524796Z","shell.execute_reply":"2023-05-28T22:15:08.533413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zip_files = ['test', 'train']\n\nfor zip_file in zip_files:\n    with zipfile.ZipFile(\"../input/dogs-vs-cats-redux-kernels-edition/{}.zip\".format(zip_file),\"r\") as z:\n        z.extractall(\".\")\n        print(\"{} unzipped\".format(zip_file))\n        ","metadata":{"execution":{"iopub.status.busy":"2023-05-28T22:15:09.726868Z","iopub.execute_input":"2023-05-28T22:15:09.727260Z","iopub.status.idle":"2023-05-28T22:15:28.315653Z","shell.execute_reply.started":"2023-05-28T22:15:09.727229Z","shell.execute_reply":"2023-05-28T22:15:28.314622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_file_names = os.listdir(\"../working/train\")\ntest_file_names = os.listdir(\"../working/test\")","metadata":{"execution":{"iopub.status.busy":"2023-05-28T22:15:47.910277Z","iopub.execute_input":"2023-05-28T22:15:47.910661Z","iopub.status.idle":"2023-05-28T22:15:47.939638Z","shell.execute_reply.started":"2023-05-28T22:15:47.910629Z","shell.execute_reply":"2023-05-28T22:15:47.938756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"targets = []\nfull_paths = []\nfor file_name in train_file_names:\n    target = file_name.split(\".\")[0]\n    full_path = os.path.join(\"../working/train\",file_name)\n    full_paths.append(full_path)\n    targets.append(target)\ntrain_df = pd.DataFrame()\ntrain_df['image_path'] = full_paths\ntrain_df['target'] = targets","metadata":{"execution":{"iopub.status.busy":"2023-05-28T22:17:23.937588Z","iopub.execute_input":"2023-05-28T22:17:23.938482Z","iopub.status.idle":"2023-05-28T22:17:24.019817Z","shell.execute_reply.started":"2023-05-28T22:17:23.938448Z","shell.execute_reply":"2023-05-28T22:17:24.018927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"targets = []\nfull_paths = []\nfor file_name in test_file_names:\n    target = file_name.split(\".\")[0]\n    full_path = os.path.join(\"../working/test\", file_name)\n    full_paths.append(full_path)\n    targets.append(target)\ntest_df = pd.DataFrame()\ntest_df['image_path'] = full_paths","metadata":{"execution":{"iopub.status.busy":"2023-05-28T22:17:24.255073Z","iopub.execute_input":"2023-05-28T22:17:24.255406Z","iopub.status.idle":"2023-05-28T22:17:24.298600Z","shell.execute_reply.started":"2023-05-28T22:17:24.255379Z","shell.execute_reply":"2023-05-28T22:17:24.297758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig,axes=plt.subplots(5,5,figsize=(10,10))\naxes = axes.ravel()\nfor i in range(25):\n    axes[i].imshow(img.imread(train_df.iloc[i,0]))\n    axes[i].axis('off')\nfig.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-28T22:17:25.268742Z","iopub.execute_input":"2023-05-28T22:17:25.269428Z","iopub.status.idle":"2023-05-28T22:17:27.024697Z","shell.execute_reply.started":"2023-05-28T22:17:25.269393Z","shell.execute_reply":"2023-05-28T22:17:27.023515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Pre-Process","metadata":{}},{"cell_type":"code","source":"train_data, val_data = train_test_split(train_df,train_size=0.80,random_state=42)\ntrain_data.shape, val_data.shape, test_df.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-28T22:18:32.998180Z","iopub.execute_input":"2023-05-28T22:18:32.998671Z","iopub.status.idle":"2023-05-28T22:18:33.018260Z","shell.execute_reply.started":"2023-05-28T22:18:32.998631Z","shell.execute_reply":"2023-05-28T22:18:33.017292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = ImageDataGenerator( rescale=1./255,\n        rotation_range=3,  # randomly rotate images in the range (degrees, 0 to 180)\n        zoom_range = 0.01, # Randomly zoom image \n        width_shift_range=0.01,  # randomly shift images horizontally (fraction of total width)\n        height_shift_range=0.01,  # randomly shift images vertically (fraction of total height)\n        horizontal_flip=True)  # randomly flip images\n        # validation_split=0.2)  # validation split\n\ntrain_data_generator = train_datagen.flow_from_dataframe(dataframe=train_data,x_col='image_path',y_col='target',\n                                                  target_size=(150,150),class_mode='binary',batch_size=64,shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2023-05-28T22:20:25.422706Z","iopub.execute_input":"2023-05-28T22:20:25.423531Z","iopub.status.idle":"2023-05-28T22:20:25.663524Z","shell.execute_reply.started":"2023-05-28T22:20:25.423496Z","shell.execute_reply":"2023-05-28T22:20:25.662646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_datagen = ImageDataGenerator( rescale=1./255,\n        rotation_range=3,  # randomly rotate images in the range (degrees, 0 to 180)\n        zoom_range = 0.01, # Randomly zoom image \n        width_shift_range=0.01,  # randomly shift images horizontally (fraction of total width)\n        height_shift_range=0.01,  # randomly shift images vertically (fraction of total height)\n        horizontal_flip=True)  # randomly flip images\n        # validation_split=0.2)  # validation split\n\nval_data_generator = val_datagen.flow_from_dataframe(dataframe=val_data,x_col='image_path',y_col='target',\n                                                  target_size=(150,150),class_mode='binary',batch_size=64,shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2023-05-28T22:20:26.316360Z","iopub.execute_input":"2023-05-28T22:20:26.316760Z","iopub.status.idle":"2023-05-28T22:20:26.385110Z","shell.execute_reply.started":"2023-05-28T22:20:26.316690Z","shell.execute_reply":"2023-05-28T22:20:26.384045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_datagen = ImageDataGenerator(rescale=1./255)\ntest_data_generator = test_datagen.flow_from_dataframe(dataframe=test_df,x_col='image_path',y_col=None,\n                                                  target_size=(150,150),class_mode=None,batch_size=64,shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2023-05-28T22:20:27.057918Z","iopub.execute_input":"2023-05-28T22:20:27.058568Z","iopub.status.idle":"2023-05-28T22:20:27.179726Z","shell.execute_reply.started":"2023-05-28T22:20:27.058537Z","shell.execute_reply":"2023-05-28T22:20:27.178620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model","metadata":{}},{"cell_type":"code","source":"from keras.layers import Input, Dense, Flatten, Dropout, BatchNormalization, Conv2D, MaxPooling2D \nfrom keras.models import Model\n\n# Define the model architecture\ninp = Input(shape=(150, 150, 3))\nx = Conv2D(32, (5, 5), activation='relu')(inp)\nx = BatchNormalization()(x)\nx = MaxPooling2D()(x)\nx = Dropout(0.2)(x)\nx = Conv2D(32, (3, 3), activation='relu')(x)\nx = BatchNormalization()(x)\nx = MaxPooling2D()(x)\nx = Dropout(0.2)(x)\nx = Conv2D(16, (3, 3), activation='relu')(x)\nx = BatchNormalization()(x)\nx = MaxPooling2D()(x)\nx = Dropout(0.2)(x)\nx = Flatten()(x)\nx = Dense(128, activation='relu')(x)\nx = BatchNormalization()(x)\nx = Dropout(0.3)(x)\nout = Dense(1, activation='sigmoid')(x)  # Binary classification with sigmoid activation\nmodel = Model(inputs=inp, outputs=out)\nmodel.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-05-28T22:21:32.073952Z","iopub.execute_input":"2023-05-28T22:21:32.074422Z","iopub.status.idle":"2023-05-28T22:21:32.311181Z","shell.execute_reply.started":"2023-05-28T22:21:32.074382Z","shell.execute_reply":"2023-05-28T22:21:32.310344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"visualkeras.layered_view(model, legend = 3)","metadata":{"execution":{"iopub.status.busy":"2023-05-28T22:21:37.187777Z","iopub.execute_input":"2023-05-28T22:21:37.188214Z","iopub.status.idle":"2023-05-28T22:21:37.313841Z","shell.execute_reply.started":"2023-05-28T22:21:37.188177Z","shell.execute_reply":"2023-05-28T22:21:37.312825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# Fit the model with early stopping\nhistory = model.fit(x=train_data_generator, validation_data=val_data_generator, epochs=15)","metadata":{"execution":{"iopub.status.busy":"2023-05-28T22:58:53.221945Z","iopub.execute_input":"2023-05-28T22:58:53.222342Z","iopub.status.idle":"2023-05-28T23:14:06.950341Z","shell.execute_reply.started":"2023-05-28T22:58:53.222305Z","shell.execute_reply":"2023-05-28T23:14:06.949291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Results & Submission","metadata":{}},{"cell_type":"code","source":"fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(6, 3))\nhistory_df = pd.DataFrame(history.history)\nhistory_df.loc[:, ['loss', 'val_loss']].plot(ax=axes[0],title=\"Cross-entropy\")\nhistory_df.loc[:, ['accuracy', 'val_accuracy']].plot(ax=axes[1],title=\"Accuracy\")\nplt.tight_layout()\nplt.show","metadata":{"execution":{"iopub.status.busy":"2023-05-28T23:15:11.516284Z","iopub.execute_input":"2023-05-28T23:15:11.516663Z","iopub.status.idle":"2023-05-28T23:15:31.924596Z","shell.execute_reply.started":"2023-05-28T23:15:11.516632Z","shell.execute_reply":"2023-05-28T23:15:31.923629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_label = []\na = os.listdir(\"test\")\nfor i in range(len(a)):\n    test_label.append(a[i].split(\".\")[0])\ntest_label[0:5] # check labels ","metadata":{"execution":{"iopub.status.busy":"2023-05-28T23:16:33.992384Z","iopub.execute_input":"2023-05-28T23:16:33.992807Z","iopub.status.idle":"2023-05-28T23:16:34.019544Z","shell.execute_reply.started":"2023-05-28T23:16:33.992771Z","shell.execute_reply":"2023-05-28T23:16:34.018589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Make predictions\npredictions = model.predict(test_data_generator)\n\n# Convert predictions to binary labels\npredictions = np.round(predictions.flatten()).astype(int)\n\nsubmission = pd.DataFrame({'id':test_label,'label':predictions})\nsubmission.to_csv(\"submission.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2023-05-28T23:16:36.686118Z","iopub.execute_input":"2023-05-28T23:16:36.686481Z","iopub.status.idle":"2023-05-28T23:16:56.273475Z","shell.execute_reply.started":"2023-05-28T23:16:36.686451Z","shell.execute_reply":"2023-05-28T23:16:56.272449Z"},"trusted":true},"execution_count":null,"outputs":[]}]}