{"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":"## Import the required libraries","metadata":{}},{"cell_type":"code","source":"import os\nimport zipfile\nimport os, warnings\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport matplotlib.image as img\nfrom matplotlib import gridspec\n\nimport random\nimport numpy as np\nimport tensorflow as tf\n\n#from random import randint\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom sklearn.model_selection import train_test_split\nfrom keras.utils import image_dataset_from_directory, load_img, img_to_array\nfrom keras.models import Sequential\nfrom keras.layers import Dense , Flatten, Dropout, BatchNormalization\nfrom keras.callbacks import EarlyStopping","metadata":{"execution":{"iopub.status.busy":"2023-06-06T13:38:30.232461Z","iopub.execute_input":"2023-06-06T13:38:30.232821Z","iopub.status.idle":"2023-06-06T13:38:30.241856Z","shell.execute_reply.started":"2023-06-06T13:38:30.232792Z","shell.execute_reply":"2023-06-06T13:38:30.239865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Unsip the files","metadata":{}},{"cell_type":"code","source":"import zipfile\n\nzip_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))","metadata":{"execution":{"iopub.status.busy":"2023-06-06T13:38:30.246113Z","iopub.execute_input":"2023-06-06T13:38:30.246981Z","iopub.status.idle":"2023-06-06T13:38:42.189729Z","shell.execute_reply.started":"2023-06-06T13:38:30.246931Z","shell.execute_reply":"2023-06-06T13:38:42.188678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Train_Folder_Path = \"../working/train\"\nTest_Folder_Path = \"../working/test\"\ntrain_file_names = os.listdir(Train_Folder_Path)\ntest_file_names = os.listdir(Test_Folder_Path)\n","metadata":{"execution":{"iopub.status.busy":"2023-06-06T13:38:42.191126Z","iopub.execute_input":"2023-06-06T13:38:42.191922Z","iopub.status.idle":"2023-06-06T13:38:42.219414Z","shell.execute_reply.started":"2023-06-06T13:38:42.191878Z","shell.execute_reply":"2023-06-06T13:38:42.218597Z"},"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(Train_Folder_Path, 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\n\nfull_paths = []\nfor file_name in test_file_names:\n    target = file_name.split(\".\")[0]\n    full_path = os.path.join(Test_Folder_Path, file_name)\n    full_paths.append(full_path)\ntest_df = pd.DataFrame()\ntest_df['image_path'] = full_paths","metadata":{"execution":{"iopub.status.busy":"2023-06-06T13:38:42.222006Z","iopub.execute_input":"2023-06-06T13:38:42.222415Z","iopub.status.idle":"2023-06-06T13:38:42.333156Z","shell.execute_reply.started":"2023-06-06T13:38:42.222382Z","shell.execute_reply":"2023-06-06T13:38:42.33223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_label = []\na = os.listdir(\"../working/test\")\nfor i in range(len(a)):\n    test_label.append(a[i].split(\".\")[0])\ntest_label[0:5]","metadata":{"execution":{"iopub.status.busy":"2023-06-06T13:38:42.334848Z","iopub.execute_input":"2023-06-06T13:38:42.335252Z","iopub.status.idle":"2023-06-06T13:38:42.35856Z","shell.execute_reply.started":"2023-06-06T13:38:42.335211Z","shell.execute_reply":"2023-06-06T13:38:42.35751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Visualize the data","metadata":{}},{"cell_type":"code","source":"class_names = [\"cat\", \"dog\"]\n\nplt.figure(figsize=(10, 10))\nfor i in range(9):\n    ax = plt.subplot(3, 3, i + 1)\n    j = random.randint(0, 25000)\n    plt.imshow(img.imread(train_df.iloc[j,0]))\n    plt.title(train_df.iloc[j,1])\n    plt.axis('off')\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-06-06T13:38:42.360199Z","iopub.execute_input":"2023-06-06T13:38:42.36055Z","iopub.status.idle":"2023-06-06T13:38:43.284908Z","shell.execute_reply.started":"2023-06-06T13:38:42.360517Z","shell.execute_reply":"2023-06-06T13:38:43.284119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Split the data","metadata":{}},{"cell_type":"code","source":"train_data,val_data = train_test_split(train_df,random_state=234)","metadata":{"execution":{"iopub.status.busy":"2023-06-06T13:38:43.286067Z","iopub.execute_input":"2023-06-06T13:38:43.286614Z","iopub.status.idle":"2023-06-06T13:38:43.299453Z","shell.execute_reply.started":"2023-06-06T13:38:43.286581Z","shell.execute_reply":"2023-06-06T13:38:43.298252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.random.seed(1234)\n\n# If you run into memory errors, try reducing this\nbatch_size = 32\n\ntrain_datagen = ImageDataGenerator(zoom_range=.18,rotation_range=10,rescale=1./255,shear_range=0.1,\n                            horizontal_flip=True,width_shift_range=0.1,height_shift_range=0.1)\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=batch_size,shuffle=False)\n\nval_datagen = ImageDataGenerator(rescale=1./255)\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=batch_size,shuffle=False)\n\ntest_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=batch_size,shuffle=False)                                     ","metadata":{"execution":{"iopub.status.busy":"2023-06-06T13:38:43.301132Z","iopub.execute_input":"2023-06-06T13:38:43.30146Z","iopub.status.idle":"2023-06-06T13:38:43.71783Z","shell.execute_reply.started":"2023-06-06T13:38:43.30143Z","shell.execute_reply":"2023-06-06T13:38:43.716906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Define the model","metadata":{}},{"cell_type":"code","source":"model = Sequential([  \n    Flatten(input_shape=(150,150,3)),\n    BatchNormalization(),\n    Dense(100, activation='leaky_relu'),\n    BatchNormalization(),\n    Dense(20, activation='leaky_relu'),\n    BatchNormalization(),    \n    Dense(1, activation='sigmoid')\n    ])\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-06-06T13:38:43.719418Z","iopub.execute_input":"2023-06-06T13:38:43.719762Z","iopub.status.idle":"2023-06-06T13:38:43.829842Z","shell.execute_reply.started":"2023-06-06T13:38:43.719729Z","shell.execute_reply":"2023-06-06T13:38:43.829129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Train the model","metadata":{}},{"cell_type":"code","source":"model.compile(\n    optimizer='adam',\n    loss='binary_crossentropy',\n    metrics=['binary_accuracy'],\n)\n\nearly_stopping = EarlyStopping(\n    patience=5,\n    min_delta=0.001,\n    restore_best_weights=True\n)\n\n#history = model.fit(\n#    train_data_generator,\n#    validation_data=val_data_generator,\n#    epochs=200,\n#    callbacks=[early_stopping],verbose=1\n#)\n\nhistory = model.fit(train_data_generator,epochs=200,validation_data=val_data_generator,\n                 validation_steps=val_data.shape[0]/batch_size,\n                 steps_per_epoch=train_data.shape[0]/batch_size,\n                 callbacks=[early_stopping],verbose=1\n)","metadata":{"execution":{"iopub.status.busy":"2023-06-06T13:38:43.833091Z","iopub.execute_input":"2023-06-06T13:38:43.833433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\n\nhistory_frame = pd.DataFrame(history.history)\nhistory_frame.loc[:, ['loss', 'val_loss']].plot()\nhistory_frame.loc[:, ['binary_accuracy', 'val_binary_accuracy']].plot();","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score=model.evaluate(val_data_generator)\nprint(score[1])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Make the predictions","metadata":{}},{"cell_type":"code","source":"predictions = model.predict(test_data_generator)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = pd.DataFrame({'id': test_label, 'label': np.round(predictions).flatten().astype(int)})\noutput.to_csv('submission_MLP.csv', index=False)\nprint(\"Your submission was successfully saved!\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}