{"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, InputLayer, Resizing, Convolution2D, MaxPooling2D, BatchNormalization\nfrom keras.callbacks import EarlyStopping\nfrom keras.applications.inception_v3 import preprocess_input, InceptionV3","metadata":{"execution":{"iopub.status.busy":"2023-06-14T17:19:57.496786Z","iopub.execute_input":"2023-06-14T17:19:57.497201Z","iopub.status.idle":"2023-06-14T17:20:05.242081Z","shell.execute_reply.started":"2023-06-14T17:19:57.497168Z","shell.execute_reply":"2023-06-14T17:20:05.240964Z"},"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-14T17:20:05.244123Z","iopub.execute_input":"2023-06-14T17:20:05.244838Z","iopub.status.idle":"2023-06-14T17:20:22.594203Z","shell.execute_reply.started":"2023-06-14T17:20:05.244798Z","shell.execute_reply":"2023-06-14T17:20:22.593060Z"},"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-14T17:20:22.597933Z","iopub.execute_input":"2023-06-14T17:20:22.598265Z","iopub.status.idle":"2023-06-14T17:20:22.629508Z","shell.execute_reply.started":"2023-06-14T17:20:22.598235Z","shell.execute_reply":"2023-06-14T17:20:22.628463Z"},"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-14T17:20:22.632684Z","iopub.execute_input":"2023-06-14T17:20:22.633089Z","iopub.status.idle":"2023-06-14T17:20:22.772576Z","shell.execute_reply.started":"2023-06-14T17:20:22.633052Z","shell.execute_reply":"2023-06-14T17:20:22.771474Z"},"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-14T17:20:22.773932Z","iopub.execute_input":"2023-06-14T17:20:22.774450Z","iopub.status.idle":"2023-06-14T17:20:22.803952Z","shell.execute_reply.started":"2023-06-14T17:20:22.774410Z","shell.execute_reply":"2023-06-14T17:20:22.802773Z"},"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-14T17:20:22.805829Z","iopub.execute_input":"2023-06-14T17:20:22.806250Z","iopub.status.idle":"2023-06-14T17:20:24.175869Z","shell.execute_reply.started":"2023-06-14T17:20:22.806208Z","shell.execute_reply":"2023-06-14T17:20:24.174877Z"},"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-14T17:20:24.176911Z","iopub.execute_input":"2023-06-14T17:20:24.177232Z","iopub.status.idle":"2023-06-14T17:20:24.191365Z","shell.execute_reply.started":"2023-06-14T17:20:24.177202Z","shell.execute_reply":"2023-06-14T17:20:24.190157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.shape","metadata":{"execution":{"iopub.status.busy":"2023-06-14T17:20:24.192728Z","iopub.execute_input":"2023-06-14T17:20:24.193728Z","iopub.status.idle":"2023-06-14T17:20:24.200458Z","shell.execute_reply.started":"2023-06-14T17:20:24.193692Z","shell.execute_reply":"2023-06-14T17:20:24.199354Z"},"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(preprocessing_function=preprocess_input, zoom_range=.18,rotation_range=10,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=(299,299),class_mode='binary',batch_size=batch_size,shuffle=False)\n\nval_datagen = ImageDataGenerator(preprocessing_function=preprocess_input)\nval_data_generator = val_datagen.flow_from_dataframe(dataframe=val_data,x_col='image_path',y_col='target',\n                                                  target_size=(299,299),class_mode='binary',batch_size=batch_size,shuffle=False)\n\ntest_datagen = ImageDataGenerator(preprocessing_function=preprocess_input)\ntest_data_generator = test_datagen.flow_from_dataframe(dataframe=test_df,x_col='image_path',y_col=None,\n                                                  target_size=(299,299),class_mode=None,batch_size=batch_size,shuffle=False)                                     \n","metadata":{"execution":{"iopub.status.busy":"2023-06-14T17:20:24.202122Z","iopub.execute_input":"2023-06-14T17:20:24.203367Z","iopub.status.idle":"2023-06-14T17:20:24.669632Z","shell.execute_reply.started":"2023-06-14T17:20:24.203242Z","shell.execute_reply":"2023-06-14T17:20:24.668541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Define the model","metadata":{}},{"cell_type":"code","source":"iv3 = InceptionV3(weights=\"imagenet\", include_top=False, input_shape=(299,299,3))\niv3.trainable = False","metadata":{"execution":{"iopub.status.busy":"2023-06-14T17:20:24.673447Z","iopub.execute_input":"2023-06-14T17:20:24.673757Z","iopub.status.idle":"2023-06-14T17:20:31.400538Z","shell.execute_reply.started":"2023-06-14T17:20:24.673729Z","shell.execute_reply":"2023-06-14T17:20:31.399188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential([\n    iv3,\n    Flatten(),\n    Dense(100, activation='leaky_relu'),\n    #Dropout(0.3),\n    Dense(1, activation='sigmoid'),\n])\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-06-14T17:20:31.402247Z","iopub.execute_input":"2023-06-14T17:20:31.402671Z","iopub.status.idle":"2023-06-14T17:20:32.173969Z","shell.execute_reply.started":"2023-06-14T17:20:31.402633Z","shell.execute_reply":"2023-06-14T17:20:32.171388Z"},"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\nhistory = model.fit(\n    train_data_generator,\n    steps_per_epoch=train_data.shape[0]/batch_size,\n    validation_data=val_data_generator,\n    validation_steps=val_data.shape[0]/batch_size,\n    epochs=200,\n    callbacks=[early_stopping],verbose=1\n)","metadata":{"execution":{"iopub.status.busy":"2023-06-14T17:20:32.179280Z","iopub.execute_input":"2023-06-14T17:20:32.180217Z","iopub.status.idle":"2023-06-14T19:18:04.469962Z","shell.execute_reply.started":"2023-06-14T17:20:32.180173Z","shell.execute_reply":"2023-06-14T19:18:04.468629Z"},"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":{"execution":{"iopub.status.busy":"2023-06-14T19:18:04.472005Z","iopub.execute_input":"2023-06-14T19:18:04.472475Z","iopub.status.idle":"2023-06-14T19:18:05.049341Z","shell.execute_reply.started":"2023-06-14T19:18:04.472433Z","shell.execute_reply":"2023-06-14T19:18:05.048108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score=model.evaluate(val_data_generator)\nprint(score[1])","metadata":{"execution":{"iopub.status.busy":"2023-06-14T19:18:05.051063Z","iopub.execute_input":"2023-06-14T19:18:05.051481Z","iopub.status.idle":"2023-06-14T19:18:34.890059Z","shell.execute_reply.started":"2023-06-14T19:18:05.051443Z","shell.execute_reply":"2023-06-14T19:18:34.888930Z"},"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":{"execution":{"iopub.status.busy":"2023-06-14T19:18:34.892255Z","iopub.execute_input":"2023-06-14T19:18:34.893399Z","iopub.status.idle":"2023-06-14T19:19:34.615970Z","shell.execute_reply.started":"2023-06-14T19:18:34.893357Z","shell.execute_reply":"2023-06-14T19:19:34.614800Z"},"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_data_augmented_InceptionV3_and_CNN.csv', index=False)\nprint(\"Your submission was successfully saved!\")","metadata":{"execution":{"iopub.status.busy":"2023-06-14T19:19:34.617571Z","iopub.execute_input":"2023-06-14T19:19:34.618117Z","iopub.status.idle":"2023-06-14T19:19:34.657796Z","shell.execute_reply.started":"2023-06-14T19:19:34.618074Z","shell.execute_reply":"2023-06-14T19:19:34.656681Z"},"trusted":true},"execution_count":null,"outputs":[]}]}