{"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":"The model I have used here are :\n* ResNet50\n* Xception\n* Inception","metadata":{}},{"cell_type":"markdown","source":"# Import Libraries","metadata":{}},{"cell_type":"code","source":"import os, os.path, shutil\nimport zipfile\nimport numpy as np \nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nimport h5py\nimport random\n\nfrom tensorflow.keras.optimizers import Adam\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix\nfrom tensorflow.keras import layers\nfrom tensorflow.keras import Model\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\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-05-24T08:44:20.603475Z","iopub.execute_input":"2021-05-24T08:44:20.604075Z","iopub.status.idle":"2021-05-24T08:44:26.842329Z","shell.execute_reply.started":"2021-05-24T08:44:20.604026Z","shell.execute_reply":"2021-05-24T08:44:26.841455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Seed to maintain reproducibility","metadata":{}},{"cell_type":"code","source":"def seed_everything(seed=13):\n    np.random.seed(seed)\n    tf.random.set_seed(seed)\n    os.environ['PYTHONHASHSEED'] = str(seed)\n    os.environ['TF_DETERMINISTIC_OPS'] = '1'\n    os.environ['TF_KERAS'] = '1'\n    random.seed(seed)\n    \nseed_everything(419)","metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","execution":{"iopub.status.busy":"2021-05-24T08:44:26.846528Z","iopub.execute_input":"2021-05-24T08:44:26.846784Z","iopub.status.idle":"2021-05-24T08:44:26.854434Z","shell.execute_reply.started":"2021-05-24T08:44:26.846759Z","shell.execute_reply":"2021-05-24T08:44:26.853822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n","metadata":{"execution":{"iopub.status.busy":"2021-05-24T08:44:26.857222Z","iopub.execute_input":"2021-05-24T08:44:26.857476Z","iopub.status.idle":"2021-05-24T08:44:26.875417Z","shell.execute_reply.started":"2021-05-24T08:44:26.857451Z","shell.execute_reply":"2021-05-24T08:44:26.874619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Unzip Files","metadata":{}},{"cell_type":"code","source":"with zipfile.ZipFile(\"../input/dogs-vs-cats/train.zip\",\"r\") as z:\n    z.extractall(\"../kaggle/working/train_unzip\")\n    \nprint(f\"We have total {len(os.listdir('../kaggle/working/train_unzip/train'))} images in our training data.\")","metadata":{"execution":{"iopub.status.busy":"2021-05-24T08:44:26.878452Z","iopub.execute_input":"2021-05-24T08:44:26.878713Z","iopub.status.idle":"2021-05-24T08:44:39.287334Z","shell.execute_reply.started":"2021-05-24T08:44:26.878687Z","shell.execute_reply":"2021-05-24T08:44:39.286447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with zipfile.ZipFile(\"../input/dogs-vs-cats/test1.zip\",\"r\") as z:\n    z.extractall(\"../kaggle/working/test_unzip\")\n    \nprint(f\"We have total {len(os.listdir('../kaggle/working/test_unzip/test1'))} images in our training data.\")","metadata":{"execution":{"iopub.status.busy":"2021-05-24T08:44:39.290554Z","iopub.execute_input":"2021-05-24T08:44:39.290893Z","iopub.status.idle":"2021-05-24T08:44:44.594828Z","shell.execute_reply.started":"2021-05-24T08:44:39.290857Z","shell.execute_reply":"2021-05-24T08:44:44.593897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"folder_path = \"../kaggle/working/train_unzip/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-24T08:44:44.597068Z","iopub.execute_input":"2021-05-24T08:44:44.597595Z","iopub.status.idle":"2021-05-24T08:44:45.840712Z","shell.execute_reply.started":"2021-05-24T08:44:44.597553Z","shell.execute_reply":"2021-05-24T08:44:45.839969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* I can see the directory,folder name and file name residing inside through this tiny code snippet","metadata":{}},{"cell_type":"code","source":"for (dirpath, dirnames, filenames) in walk(\"../kaggle/working/test_unzip/test1\"):\n    print(\"Directory path: \", dirpath)\n    print(\"Folder name: \", dirnames)\n    print(\"File name: \", filenames)","metadata":{"execution":{"iopub.status.busy":"2021-05-24T08:44:45.841988Z","iopub.execute_input":"2021-05-24T08:44:45.842344Z","iopub.status.idle":"2021-05-24T08:44:45.886243Z","shell.execute_reply.started":"2021-05-24T08:44:45.842306Z","shell.execute_reply":"2021-05-24T08:44:45.883466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* 3 different models are used to generate their individual weight. Later on we'll merge these weights.","metadata":{}},{"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))\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_unzip/train\", image_size, shuffle=False, \n                                              batch_size=BATCH_SIZE)\n    test_generator = gen.flow_from_directory(\"../kaggle/working/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    test = model.predict_generator(test_generator, test_generator.samples)\n    with h5py.File(\"gap_%s.h5\"%MODEL.__name__) as h:\n        h.create_dataset(\"train\", data=train)\n        h.create_dataset(\"test\", data=test)\n        h.create_dataset(\"label\", data=train_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-24T08:44:45.887350Z","iopub.execute_input":"2021-05-24T08:44:45.887690Z","iopub.status.idle":"2021-05-24T08:52:47.529406Z","shell.execute_reply.started":"2021-05-24T08:44:45.887648Z","shell.execute_reply":"2021-05-24T08:52:47.528391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Merge All Of The Weights","metadata":{}},{"cell_type":"code","source":"X_train = []\nX_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_test.append(np.array(h['test']))\n        y_train = np.array(h['label'])\n\nX_train = np.concatenate(X_train, axis=1)\nX_test = np.concatenate(X_test, axis=1)\n\nX_train, y_train = shuffle(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2021-05-24T08:52:47.530931Z","iopub.execute_input":"2021-05-24T08:52:47.531295Z","iopub.status.idle":"2021-05-24T08:52:48.659923Z","shell.execute_reply.started":"2021-05-24T08:52:47.531255Z","shell.execute_reply":"2021-05-24T08:52:48.659095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Build The Model","metadata":{}},{"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-24T08:52:48.661245Z","iopub.execute_input":"2021-05-24T08:52:48.661649Z","iopub.status.idle":"2021-05-24T08:52:48.703963Z","shell.execute_reply.started":"2021-05-24T08:52:48.661606Z","shell.execute_reply":"2021-05-24T08:52:48.703318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-05-24T08:52:48.705110Z","iopub.execute_input":"2021-05-24T08:52:48.705463Z","iopub.status.idle":"2021-05-24T08:52:48.716167Z","shell.execute_reply.started":"2021-05-24T08:52:48.705429Z","shell.execute_reply":"2021-05-24T08:52:48.712225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Define Callback Method\n* to setup earlystopping and specify the value in plateau","metadata":{}},{"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-24T08:52:48.717570Z","iopub.execute_input":"2021-05-24T08:52:48.718084Z","iopub.status.idle":"2021-05-24T08:52:48.724328Z","shell.execute_reply.started":"2021-05-24T08:52:48.718046Z","shell.execute_reply":"2021-05-24T08:52:48.723328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training","metadata":{}},{"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_split=0.2)","metadata":{"execution":{"iopub.status.busy":"2021-05-24T08:52:48.725936Z","iopub.execute_input":"2021-05-24T08:52:48.726317Z","iopub.status.idle":"2021-05-24T08:53:12.286533Z","shell.execute_reply.started":"2021-05-24T08:52:48.726278Z","shell.execute_reply":"2021-05-24T08:53:12.285885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('bit_mini_model_res50+Xcep+Incep.h5')","metadata":{"execution":{"iopub.status.busy":"2021-05-24T08:53:12.289471Z","iopub.execute_input":"2021-05-24T08:53:12.289726Z","iopub.status.idle":"2021-05-24T08:53:12.306940Z","shell.execute_reply.started":"2021-05-24T08:53:12.289699Z","shell.execute_reply":"2021-05-24T08:53:12.306281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Evaluation Through Plot","metadata":{}},{"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-24T08:53:12.308258Z","iopub.execute_input":"2021-05-24T08:53:12.308602Z","iopub.status.idle":"2021-05-24T08:53:12.777719Z","shell.execute_reply.started":"2021-05-24T08:53:12.308567Z","shell.execute_reply":"2021-05-24T08:53:12.777045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Inference","metadata":{}},{"cell_type":"code","source":"y_pred = model.predict(X_test, verbose=1)\ny_pred = y_pred.clip(min=0.005, max=0.995)\ny_pred","metadata":{"execution":{"iopub.status.busy":"2021-05-24T08:53:12.779040Z","iopub.execute_input":"2021-05-24T08:53:12.779390Z","iopub.status.idle":"2021-05-24T08:53:13.537857Z","shell.execute_reply.started":"2021-05-24T08:53:12.779354Z","shell.execute_reply":"2021-05-24T08:53:13.537045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_filenames = os.listdir('../kaggle/working/test_unzip/test1')\ndf = pd.DataFrame({'filename': test_filenames})\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2021-05-24T08:53:13.540619Z","iopub.execute_input":"2021-05-24T08:53:13.540893Z","iopub.status.idle":"2021-05-24T08:53:13.565606Z","shell.execute_reply.started":"2021-05-24T08:53:13.540856Z","shell.execute_reply":"2021-05-24T08:53:13.564559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gen = ImageDataGenerator()\ntest_generator = gen.flow_from_directory(\"../kaggle/working/test_unzip/\", image_size, shuffle=False, \n                                         batch_size=BATCH_SIZE, class_mode=None)","metadata":{"execution":{"iopub.status.busy":"2021-05-24T08:53:13.566996Z","iopub.execute_input":"2021-05-24T08:53:13.567349Z","iopub.status.idle":"2021-05-24T08:53:14.007596Z","shell.execute_reply.started":"2021-05-24T08:53:13.567311Z","shell.execute_reply":"2021-05-24T08:53:14.006809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['category'] = y_pred\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2021-05-24T08:53:14.010962Z","iopub.execute_input":"2021-05-24T08:53:14.011257Z","iopub.status.idle":"2021-05-24T08:53:14.028848Z","shell.execute_reply.started":"2021-05-24T08:53:14.011227Z","shell.execute_reply":"2021-05-24T08:53:14.027659Z"},"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-24T08:53:14.030443Z","iopub.execute_input":"2021-05-24T08:53:14.030927Z","iopub.status.idle":"2021-05-24T08:53:14.414064Z","shell.execute_reply.started":"2021-05-24T08:53:14.030872Z","shell.execute_reply":"2021-05-24T08:53:14.413238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['category'].value_counts().plot.bar()","metadata":{"execution":{"iopub.status.busy":"2021-05-24T08:53:14.415232Z","iopub.execute_input":"2021-05-24T08:53:14.415558Z","iopub.status.idle":"2021-05-24T08:53:14.535720Z","shell.execute_reply.started":"2021-05-24T08:53:14.415520Z","shell.execute_reply":"2021-05-24T08:53:14.534534Z"},"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-24T08:53:14.537415Z","iopub.execute_input":"2021-05-24T08:53:14.537824Z","iopub.status.idle":"2021-05-24T08:53:19.431026Z","shell.execute_reply.started":"2021-05-24T08:53:14.537764Z","shell.execute_reply":"2021-05-24T08:53:19.430176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}