{"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-12T18:30:06.031246Z","iopub.execute_input":"2023-06-12T18:30:06.031611Z","iopub.status.idle":"2023-06-12T18:30:06.041324Z","shell.execute_reply.started":"2023-06-12T18:30:06.03158Z","shell.execute_reply":"2023-06-12T18:30:06.039073Z"},"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-12T18:30:06.044456Z","iopub.execute_input":"2023-06-12T18:30:06.04532Z","iopub.status.idle":"2023-06-12T18:30:17.844195Z","shell.execute_reply.started":"2023-06-12T18:30:06.045276Z","shell.execute_reply":"2023-06-12T18:30:17.843173Z"},"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-12T18:30:17.845569Z","iopub.execute_input":"2023-06-12T18:30:17.845982Z","iopub.status.idle":"2023-06-12T18:30:17.873212Z","shell.execute_reply.started":"2023-06-12T18:30:17.84592Z","shell.execute_reply":"2023-06-12T18:30:17.872345Z"},"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-12T18:30:17.874475Z","iopub.execute_input":"2023-06-12T18:30:17.874881Z","iopub.status.idle":"2023-06-12T18:30:17.986983Z","shell.execute_reply.started":"2023-06-12T18:30:17.874847Z","shell.execute_reply":"2023-06-12T18:30:17.986139Z"},"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-12T18:30:17.990011Z","iopub.execute_input":"2023-06-12T18:30:17.990408Z","iopub.status.idle":"2023-06-12T18:30:18.018196Z","shell.execute_reply.started":"2023-06-12T18:30:17.990374Z","shell.execute_reply":"2023-06-12T18:30:18.017163Z"},"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-12T18:30:18.019876Z","iopub.execute_input":"2023-06-12T18:30:18.020275Z","iopub.status.idle":"2023-06-12T18:30:19.097829Z","shell.execute_reply.started":"2023-06-12T18:30:18.020242Z","shell.execute_reply":"2023-06-12T18:30:19.097027Z"},"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-12T18:30:19.099083Z","iopub.execute_input":"2023-06-12T18:30:19.09958Z","iopub.status.idle":"2023-06-12T18:30:19.112234Z","shell.execute_reply.started":"2023-06-12T18:30:19.09955Z","shell.execute_reply":"2023-06-12T18:30:19.111153Z"},"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)\ntrain_data_generator = train_datagen.flow_from_dataframe(dataframe=train_data,x_col='image_path',y_col='target',\n                                                  target_size=(224,224),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=(224,224),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=(224,224),class_mode=None,batch_size=batch_size,shuffle=False)                                     ","metadata":{"execution":{"iopub.status.busy":"2023-06-12T18:30:19.113602Z","iopub.execute_input":"2023-06-12T18:30:19.11417Z","iopub.status.idle":"2023-06-12T18:30:19.513719Z","shell.execute_reply.started":"2023-06-12T18:30:19.114139Z","shell.execute_reply":"2023-06-12T18:30:19.512766Z"},"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=(224,224,3))\niv3.trainable = False","metadata":{"execution":{"iopub.status.busy":"2023-06-12T18:30:19.515068Z","iopub.execute_input":"2023-06-12T18:30:19.515616Z","iopub.status.idle":"2023-06-12T18:30:21.735411Z","shell.execute_reply.started":"2023-06-12T18:30:19.515583Z","shell.execute_reply":"2023-06-12T18:30:21.734448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential([\n    iv3,\n    Flatten(),\n    Dense(100, activation='relu'),\n    Dense(1, activation='sigmoid'),\n])\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-06-12T18:30:21.736769Z","iopub.execute_input":"2023-06-12T18:30:21.737134Z","iopub.status.idle":"2023-06-12T18:30:22.445748Z","shell.execute_reply.started":"2023-06-12T18:30:21.737086Z","shell.execute_reply":"2023-06-12T18:30:22.444692Z"},"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    validation_data=val_data_generator,\n    epochs=200,\n    callbacks=[early_stopping],verbose=1\n)","metadata":{"execution":{"iopub.status.busy":"2023-06-12T18:30:22.447176Z","iopub.execute_input":"2023-06-12T18:30:22.448163Z","iopub.status.idle":"2023-06-12T18:38:45.74151Z","shell.execute_reply.started":"2023-06-12T18:30:22.448106Z","shell.execute_reply":"2023-06-12T18:38:45.740552Z"},"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-12T18:38:45.742884Z","iopub.execute_input":"2023-06-12T18:38:45.744657Z","iopub.status.idle":"2023-06-12T18:38:46.224823Z","shell.execute_reply.started":"2023-06-12T18:38:45.744624Z","shell.execute_reply":"2023-06-12T18:38:46.223963Z"},"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-12T18:38:46.226359Z","iopub.execute_input":"2023-06-12T18:38:46.227396Z","iopub.status.idle":"2023-06-12T18:39:00.451051Z","shell.execute_reply.started":"2023-06-12T18:38:46.227362Z","shell.execute_reply":"2023-06-12T18:39:00.450164Z"},"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-12T18:39:00.456057Z","iopub.execute_input":"2023-06-12T18:39:00.45637Z","iopub.status.idle":"2023-06-12T18:39:32.03386Z","shell.execute_reply.started":"2023-06-12T18:39:00.456345Z","shell.execute_reply":"2023-06-12T18:39:32.032906Z"},"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_InceptionV3_and_CNN.csv', index=False)\nprint(\"Your submission was successfully saved!\")","metadata":{"execution":{"iopub.status.busy":"2023-06-12T18:39:32.035551Z","iopub.execute_input":"2023-06-12T18:39:32.035899Z","iopub.status.idle":"2023-06-12T18:39:32.071878Z","shell.execute_reply.started":"2023-06-12T18:39:32.035866Z","shell.execute_reply":"2023-06-12T18:39:32.07101Z"},"trusted":true},"execution_count":null,"outputs":[]}]}