{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-05-26T15:34:06.422804Z","iopub.execute_input":"2021-05-26T15:34:06.423244Z","iopub.status.idle":"2021-05-26T15:34:06.438306Z","shell.execute_reply.started":"2021-05-26T15:34:06.423145Z","shell.execute_reply":"2021-05-26T15:34:06.437049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os, os.path, shutil\nimport zipfile\nimport tensorflow as tf\nimport h5py\nimport random\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix\nfrom tensorflow.keras import Model\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.optimizers import RMSprop\nfrom tensorflow.keras.applications.inception_v3 import InceptionV3\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img\nfrom tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau\nfrom keras.applications import *\nfrom keras.preprocessing.image import *\nfrom sklearn.utils import shuffle\nfrom keras.models import *\nfrom keras.layers import *\nfrom os import walk\n\n\n\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2021-05-26T15:34:06.440702Z","iopub.execute_input":"2021-05-26T15:34:06.441644Z","iopub.status.idle":"2021-05-26T15:34:13.702669Z","shell.execute_reply.started":"2021-05-26T15:34:06.441583Z","shell.execute_reply":"2021-05-26T15:34:13.701220Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_zip_dir = '/kaggle/input/dogs-vs-cats'\ntrain_zip_dir = os.path.join(data_zip_dir, 'train.zip')\nwith zipfile.ZipFile(train_zip_dir, 'r') as z:\n    z.extractall()\n","metadata":{"execution":{"iopub.status.busy":"2021-05-26T15:35:46.491520Z","iopub.execute_input":"2021-05-26T15:35:46.491937Z","iopub.status.idle":"2021-05-26T15:36:00.236277Z","shell.execute_reply.started":"2021-05-26T15:35:46.491877Z","shell.execute_reply":"2021-05-26T15:36:00.235119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir = os.path.join(os.getcwd(), 'train')\ntest_dir = os.path.join(os.getcwd(), 'test1')\ntrain_set_dir = os.path.join(train_dir, 'train')\nos.mkdir(train_set_dir)\nvalid_set_dir = os.path.join(train_dir, 'valid')\nos.mkdir(valid_set_dir)\ntest_set_dir = os.path.join(train_dir, 'test')\nos.mkdir(test_set_dir)\ndog_files = [f'dog.{i}.jpg' for i in range(12500)]\ncat_files = [f'cat.{i}.jpg' for i in range(12500)]","metadata":{"execution":{"iopub.status.busy":"2021-05-26T15:36:51.907482Z","iopub.execute_input":"2021-05-26T15:36:51.907846Z","iopub.status.idle":"2021-05-26T15:36:51.923797Z","shell.execute_reply.started":"2021-05-26T15:36:51.907816Z","shell.execute_reply":"2021-05-26T15:36:51.922484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd /kaggle/working/test1","metadata":{"execution":{"iopub.status.busy":"2021-05-26T15:36:53.901922Z","iopub.execute_input":"2021-05-26T15:36:53.902422Z","iopub.status.idle":"2021-05-26T15:36:53.909692Z","shell.execute_reply.started":"2021-05-26T15:36:53.902376Z","shell.execute_reply":"2021-05-26T15:36:53.908343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls","metadata":{"execution":{"iopub.status.busy":"2021-05-26T15:36:55.085585Z","iopub.execute_input":"2021-05-26T15:36:55.085993Z","iopub.status.idle":"2021-05-26T15:36:55.869583Z","shell.execute_reply.started":"2021-05-26T15:36:55.085963Z","shell.execute_reply":"2021-05-26T15:36:55.868349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for dog, cat in zip(dog_files[:10000], cat_files[:10000]):\n    src = os.path.join(train_dir, dog)\n    dst = os.path.join(train_set_dir, dog)\n    shutil.move(src, dst)\n    src = os.path.join(train_dir, cat)\n    dst = os.path.join(train_set_dir, cat)\n    shutil.move(src, dst)\nfor dog, cat in zip(dog_files[10000:11250], cat_files[10000:11250]):\n    src = os.path.join(train_dir, dog)\n    dst = os.path.join(valid_set_dir, dog)\n    shutil.move(src, dst)\n    src = os.path.join(train_dir, cat)\n    dst = os.path.join(valid_set_dir, cat)\n    shutil.move(src, dst)\nfor dog, cat in zip(dog_files[11250:12500], cat_files[11250:12500]):\n    src = os.path.join(train_dir, dog)\n    dst = os.path.join(test_set_dir, dog)\n    shutil.move(src, dst)\n    src = os.path.join(train_dir, cat)\n    dst = os.path.join(test_set_dir, cat)\n    shutil.move(src, dst)\n","metadata":{"execution":{"iopub.status.busy":"2021-05-26T15:36:58.001647Z","iopub.execute_input":"2021-05-26T15:36:58.002235Z","iopub.status.idle":"2021-05-26T15:36:59.001649Z","shell.execute_reply.started":"2021-05-26T15:36:58.002150Z","shell.execute_reply":"2021-05-26T15:36:59.000323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(test_set_dir)","metadata":{"execution":{"iopub.status.busy":"2021-05-26T02:39:35.352115Z","iopub.execute_input":"2021-05-26T02:39:35.352461Z","iopub.status.idle":"2021-05-26T02:39:35.357006Z","shell.execute_reply.started":"2021-05-26T02:39:35.352433Z","shell.execute_reply":"2021-05-26T02:39:35.356044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f'the number of train set : {len(os.listdir(train_set_dir))}')\nprint(f'the number of valid set : {len(os.listdir(valid_set_dir))}')\nprint(f'the number of test set : {len(os.listdir(test_set_dir))}')","metadata":{"execution":{"iopub.status.busy":"2021-05-26T15:37:02.765356Z","iopub.execute_input":"2021-05-26T15:37:02.765884Z","iopub.status.idle":"2021-05-26T15:37:02.789792Z","shell.execute_reply.started":"2021-05-26T15:37:02.765853Z","shell.execute_reply":"2021-05-26T15:37:02.788610Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"folder_path = \"/kaggle/working/train/train\"\n\nimages = [f for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f))]\n\nfor image in images:\n    folder_name = image.split('.')[0]\n\n    new_path = os.path.join(folder_path, folder_name)\n    if not os.path.exists(new_path):\n        os.makedirs(new_path)\n\n    old_image_path = os.path.join(folder_path, image)\n    new_image_path = os.path.join(new_path, image)\n    shutil.move(old_image_path, new_image_path)","metadata":{"execution":{"iopub.status.busy":"2021-05-26T15:37:14.914896Z","iopub.execute_input":"2021-05-26T15:37:14.915306Z","iopub.status.idle":"2021-05-26T15:37:15.885512Z","shell.execute_reply.started":"2021-05-26T15:37:14.915275Z","shell.execute_reply":"2021-05-26T15:37:15.884437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"folder_path = \"/kaggle/working/train/valid\"\n\nimages = [f for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f))]\n\nfor image in images:\n    folder_name = image.split('.')[0]\n\n    new_path = os.path.join(folder_path, folder_name)\n    if not os.path.exists(new_path):\n        os.makedirs(new_path)\n\n    old_image_path = os.path.join(folder_path, image)\n    new_image_path = os.path.join(new_path, image)\n    shutil.move(old_image_path, new_image_path)","metadata":{"execution":{"iopub.status.busy":"2021-05-26T15:37:15.887381Z","iopub.execute_input":"2021-05-26T15:37:15.887831Z","iopub.status.idle":"2021-05-26T15:37:16.017932Z","shell.execute_reply.started":"2021-05-26T15:37:15.887787Z","shell.execute_reply":"2021-05-26T15:37:16.016866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"folder_path = \"/kaggle/working/train/test\"\n\nimages = [f for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f))]\n\nfor image in images:\n    folder_name = image.split('.')[0]\n\n    new_path = os.path.join(folder_path, folder_name)\n    if not os.path.exists(new_path):\n        os.makedirs(new_path)\n\n    old_image_path = os.path.join(folder_path, image)\n    new_image_path = os.path.join(new_path, image)\n    shutil.move(old_image_path, new_image_path)","metadata":{"execution":{"iopub.status.busy":"2021-05-26T15:37:17.882576Z","iopub.execute_input":"2021-05-26T15:37:17.882997Z","iopub.status.idle":"2021-05-26T15:37:18.017553Z","shell.execute_reply.started":"2021-05-26T15:37:17.882939Z","shell.execute_reply":"2021-05-26T15:37:18.016465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_zip_dir = os.path.join(data_zip_dir, 'test1.zip')\nwith zipfile.ZipFile(test_zip_dir, 'r') as z:\n    z.extractall(\"test_unzip\")","metadata":{"execution":{"iopub.status.busy":"2021-05-26T15:39:20.041699Z","iopub.execute_input":"2021-05-26T15:39:20.042306Z","iopub.status.idle":"2021-05-26T15:39:25.221570Z","shell.execute_reply.started":"2021-05-26T15:39:20.042255Z","shell.execute_reply":"2021-05-26T15:39:25.220189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"We have total {len(os.listdir('test_unzip/test1'))} images in our training data.\")","metadata":{"execution":{"iopub.status.busy":"2021-05-26T15:41:09.621359Z","iopub.execute_input":"2021-05-26T15:41:09.621703Z","iopub.status.idle":"2021-05-26T15:41:09.637742Z","shell.execute_reply.started":"2021-05-26T15:41:09.621674Z","shell.execute_reply":"2021-05-26T15:41:09.636133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for (dirpath, dirnames, filenames) in walk(\"/kaggle/working/train/train\"):\n    print(\"Directory path: \", dirpath)\n    print(\"Folder name: \", dirnames)\n    print(\"File name: \", filenames)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%time\n\nBATCH_SIZE = 128\nimage_size = (128, 128)\nEPOCHS = 200\n\ndef write_gap(MODEL, image_size, lambda_func=None):\n    width = image_size[0]\n    height = image_size[1]\n    input_tensor = Input((height, width, 3))\n    x = input_tensor\n    if lambda_func:\n        x = Lambda(lambda_func)(x)\n    base_model = MODEL(input_tensor=x, weights='imagenet', include_top=False)\n    # model = Model(base_model.input, GlobalMaxPooling2D()(base_model.output)), 결과값의 acc는 약간 0.1% 수준으로 낮지만, loss는 0.01(50%)수준으로 높아짐\n    model = Model(base_model.input, GlobalAveragePooling2D()(base_model.output))\n\n    \n    gen = ImageDataGenerator()\n    train_generator = gen.flow_from_directory(\"/kaggle/working/train/train\", image_size, shuffle=False, \n                                              batch_size=BATCH_SIZE)\n    valid_generator = gen.flow_from_directory(\"/kaggle/working/train/valid\", image_size, shuffle=False, \n                                             batch_size=BATCH_SIZE)\n    test_generator = gen.flow_from_directory(\"/kaggle/working/train/test\", image_size, shuffle=False, \n                                             batch_size=BATCH_SIZE, class_mode=None)\n    real_test_generator = gen.flow_from_directory(\"test_unzip\", image_size, shuffle=False, \n                                             batch_size=BATCH_SIZE, class_mode=None)\n\n    train = model.predict_generator(train_generator, train_generator.samples)\n    valid = model.predict_generator(valid_generator, valid_generator.samples)\n    test = model.predict_generator(test_generator, test_generator.samples)\n    real_test = model.predict_generator(real_test_generator, real_test_generator.samples)\n\n    with h5py.File(\"gap_%s.h5\"%MODEL.__name__) as h:\n        h.create_dataset(\"train\", data=train)\n        h.create_dataset(\"valid\", data=valid)\n        h.create_dataset(\"test\", data=test)\n        h.create_dataset(\"real_test\", data=real_test)\n        h.create_dataset(\"train_label\", data=train_generator.classes)\n        h.create_dataset(\"valid_label\", data=valid_generator.classes)\n\nwrite_gap(ResNet50, (224, 224))\nwrite_gap(Xception, (299, 299), xception.preprocess_input)\nwrite_gap(InceptionV3, (299, 299), inception_v3.preprocess_input)\n","metadata":{"execution":{"iopub.status.busy":"2021-05-26T15:41:21.900335Z","iopub.execute_input":"2021-05-26T15:41:21.900696Z","iopub.status.idle":"2021-05-26T15:50:34.017392Z","shell.execute_reply.started":"2021-05-26T15:41:21.900667Z","shell.execute_reply":"2021-05-26T15:50:34.016264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#y_train = np.array(h5py.File(\"gap_ResNet50.h5\", 'r')['label'])\n#y_train1 = np.array(h5py.File(\"gap_ResNet50.h5\", 'r')['label'])\n#y_train2 = np.array(h5py.File(\"gap_ResNet50.h5\", 'r')['train'])\n#y_train3 = np.array(h5py.File(\"gap_ResNet50.h5\", 'r')['valid'])\n#y_train4 = np.array(h5py.File(\"gap_ResNet50.h5\", 'r')['test'])\n#len(y_train1),len(y_train2),len(y_train3),len(y_train4)","metadata":{"execution":{"iopub.status.busy":"2021-05-26T04:03:40.670054Z","iopub.execute_input":"2021-05-26T04:03:40.670375Z","iopub.status.idle":"2021-05-26T04:03:40.811842Z","shell.execute_reply.started":"2021-05-26T04:03:40.670339Z","shell.execute_reply":"2021-05-26T04:03:40.810856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = []\nX_valid = []\nX_test = []\nX_real_test = []\n\nfor filename in [\"gap_ResNet50.h5\", \"gap_Xception.h5\", \"gap_InceptionV3.h5\"]:\n    with h5py.File(filename, 'r') as h:\n        X_train.append(np.array(h['train']))\n        X_valid.append(np.array(h['valid']))\n        X_test.append(np.array(h['test']))\n        X_real_test.append(np.array(h['real_test']))\n        y_train = np.array(h['train_label'])\n        y_valid = np.array(h['valid_label'])\n\nX_train = np.concatenate(X_train, axis=1)\nX_valid = np.concatenate(X_valid, axis=1)\nX_test = np.concatenate(X_test, axis=1)\nX_real_test = np.concatenate(X_real_test, axis=1)\n\nX_train, y_train = shuffle(X_train, y_train)\nX_valid, y_valid = shuffle(X_valid, y_valid)","metadata":{"execution":{"iopub.status.busy":"2021-05-26T15:52:51.933033Z","iopub.execute_input":"2021-05-26T15:52:51.933467Z","iopub.status.idle":"2021-05-26T15:52:53.329994Z","shell.execute_reply.started":"2021-05-26T15:52:51.933436Z","shell.execute_reply":"2021-05-26T15:52:53.328853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_tensor = Input(X_train.shape[1:])\nx = input_tensor\nx = Dropout(0.5)(x)\nx = Dense(1, activation='sigmoid')(x)\nmodel = Model(input_tensor, x)\n\nmodel.compile(optimizer=Adam(lr=0.0003, decay=1e-3),\n              loss='binary_crossentropy',\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-05-26T15:52:57.453568Z","iopub.execute_input":"2021-05-26T15:52:57.453905Z","iopub.status.idle":"2021-05-26T15:52:57.491658Z","shell.execute_reply.started":"2021-05-26T15:52:57.453875Z","shell.execute_reply":"2021-05-26T15:52:57.490706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def Callbacks():\n    erl = EarlyStopping(monitor='val_loss', patience=10, verbose=1, mode='min', \n                        restore_best_weights=True)\n    rdc = ReduceLROnPlateau(monitor='val_loss', factor=0.3, patience=3, verbose=1, mode='min')\n    return [erl,rdc]","metadata":{"execution":{"iopub.status.busy":"2021-05-26T15:52:59.412275Z","iopub.execute_input":"2021-05-26T15:52:59.412697Z","iopub.status.idle":"2021-05-26T15:52:59.420156Z","shell.execute_reply.started":"2021-05-26T15:52:59.412668Z","shell.execute_reply":"2021-05-26T15:52:59.418148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(X_train,\n                    y_train, \n                    batch_size=BATCH_SIZE, \n                    epochs=EPOCHS, \n                    callbacks=Callbacks(),\n                    validation_data = (X_valid,y_valid)\n                   )","metadata":{"execution":{"iopub.status.busy":"2021-05-26T15:53:01.595800Z","iopub.execute_input":"2021-05-26T15:53:01.596184Z","iopub.status.idle":"2021-05-26T15:53:29.116886Z","shell.execute_reply.started":"2021-05-26T15:53:01.596142Z","shell.execute_reply":"2021-05-26T15:53:29.115824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_model_history(model_history, acc='accuracy', val_acc='val_accuracy'):\n    fig, axs = plt.subplots(1,2,figsize=(15,5))\n    axs[0].plot(range(1,len(model_history.history[acc])+1),model_history.history[acc])\n    axs[0].plot(range(1,len(model_history.history[val_acc])+1),model_history.history[val_acc])\n    axs[0].set_title('Model Accuracy')\n    axs[0].set_ylabel('Accuracy')\n    axs[0].set_xlabel('Epoch')\n    axs[0].set_xticks(np.arange(1,len(model_history.history[acc])+1),len(model_history.history[acc])/10)\n    axs[0].legend(['train', 'val'], loc='best')\n    axs[1].plot(range(1,len(model_history.history['loss'])+1),model_history.history['loss'])\n    axs[1].plot(range(1,len(model_history.history['val_loss'])+1),model_history.history['val_loss'])\n    axs[1].set_title('Model Loss')\n    axs[1].set_ylabel('Loss')\n    axs[1].set_xlabel('Epoch')\n    axs[1].set_xticks(np.arange(1,len(model_history.history['loss'])+1),len(model_history.history['loss'])/10)\n    axs[1].legend(['train', 'val'], loc='best')\n    plt.show()\n    \nplot_model_history(history)","metadata":{"execution":{"iopub.status.busy":"2021-05-26T15:53:37.228270Z","iopub.execute_input":"2021-05-26T15:53:37.228640Z","iopub.status.idle":"2021-05-26T15:53:38.043282Z","shell.execute_reply.started":"2021-05-26T15:53:37.228611Z","shell.execute_reply":"2021-05-26T15:53:38.039171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#test_generator = gen.flow_from_directory(\"/kaggle/working/test1\", image_size, shuffle=False, \n#                                     batch_size=BATCH_SIZE, class_mode=None)\n\n#X_test = model.predict_generator(test_generator, test_generator.samples)\n#h5py.File(\"gap_%s.h5\"%MODEL.__name__) as h:\n#        h.create_dataset(\"train\", data=train)\n#        h.create_dataset(\"valid\", data=valid)\n#        h.create_dataset(\"test\", data=test)\n#        h.create_dataset(\"train_label\", data=train_generator.classes)\n#        h.create_dataset(\"valid_label\", data=valid_generator.classes)","metadata":{"execution":{"iopub.status.busy":"2021-05-26T04:47:42.908817Z","iopub.execute_input":"2021-05-26T04:47:42.909227Z","iopub.status.idle":"2021-05-26T04:47:43.117596Z","shell.execute_reply.started":"2021-05-26T04:47:42.909184Z","shell.execute_reply":"2021-05-26T04:47:43.115382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model.predict(X_real_test, verbose=1)\ny_pred = y_pred.clip(min=0.005, max=0.995)\nlen(y_pred)","metadata":{"execution":{"iopub.status.busy":"2021-05-26T15:53:44.571914Z","iopub.execute_input":"2021-05-26T15:53:44.572416Z","iopub.status.idle":"2021-05-26T15:53:45.479310Z","shell.execute_reply.started":"2021-05-26T15:53:44.572376Z","shell.execute_reply":"2021-05-26T15:53:45.478267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_filenames = os.listdir('test_unzip/test1')\ndf = pd.DataFrame({'filename': test_filenames})\ndf.head()\ndf.shape","metadata":{"execution":{"iopub.status.busy":"2021-05-26T15:53:48.986056Z","iopub.execute_input":"2021-05-26T15:53:48.986554Z","iopub.status.idle":"2021-05-26T15:53:49.020454Z","shell.execute_reply.started":"2021-05-26T15:53:48.986510Z","shell.execute_reply":"2021-05-26T15:53:49.017922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gen = ImageDataGenerator()\ntest_generator = gen.flow_from_directory(\"test_unzip/\", image_size, shuffle=False, \n                                         batch_size=BATCH_SIZE, class_mode=None)","metadata":{"execution":{"iopub.status.busy":"2021-05-26T15:53:51.691374Z","iopub.execute_input":"2021-05-26T15:53:51.691801Z","iopub.status.idle":"2021-05-26T15:53:52.128847Z","shell.execute_reply.started":"2021-05-26T15:53:51.691744Z","shell.execute_reply":"2021-05-26T15:53:52.127532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['category'] = y_pred\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2021-05-26T15:53:54.351767Z","iopub.execute_input":"2021-05-26T15:53:54.352200Z","iopub.status.idle":"2021-05-26T15:53:54.381180Z","shell.execute_reply.started":"2021-05-26T15:53:54.352167Z","shell.execute_reply":"2021-05-26T15:53:54.379681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"threshold = 0.5\ndf['category'] = np.where(y_pred > threshold, \"Cat\",\"Dog\")\n\n\n\ndf.to_csv('submission.csv', index=False)\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2021-05-26T15:53:57.438745Z","iopub.execute_input":"2021-05-26T15:53:57.439164Z","iopub.status.idle":"2021-05-26T15:53:57.805176Z","shell.execute_reply.started":"2021-05-26T15:53:57.439118Z","shell.execute_reply":"2021-05-26T15:53:57.803693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['category'].value_counts().plot.bar()\n","metadata":{"execution":{"iopub.status.busy":"2021-05-26T15:54:27.396380Z","iopub.execute_input":"2021-05-26T15:54:27.396840Z","iopub.status.idle":"2021-05-26T15:54:27.559389Z","shell.execute_reply.started":"2021-05-26T15:54:27.396793Z","shell.execute_reply":"2021-05-26T15:54:27.558275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_test = df.sample(n=36).reset_index()\nsample_test.head()\n\nplt.figure(figsize=(24, 16))\nfor index, row in sample_test.iterrows():\n    filename = row['filename']\n    category = row['category']\n    img = load_img(\"/kaggle/working/test_unzip/test1/\"+filename, target_size=image_size)\n    plt.subplot(6, 6, index+1)\n    plt.imshow(img)\n    plt.xlabel('It\\'s a ' + \"{}\".format(category) )\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-05-26T15:54:31.934098Z","iopub.execute_input":"2021-05-26T15:54:31.934614Z","iopub.status.idle":"2021-05-26T15:54:38.238817Z","shell.execute_reply.started":"2021-05-26T15:54:31.934584Z","shell.execute_reply":"2021-05-26T15:54:38.237667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}