{"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":"2022-08-23T15:44:58.323126Z","iopub.execute_input":"2022-08-23T15:44:58.323392Z","iopub.status.idle":"2022-08-23T15:44:58.331265Z","shell.execute_reply.started":"2022-08-23T15:44:58.323366Z","shell.execute_reply":"2022-08-23T15:44:58.330028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport os\nimport random\nimport zipfile\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import ConfusionMatrixDisplay, confusion_matrix, classification_report \nimport tensorflow as tf\nfrom keras.preprocessing.image import load_img, ImageDataGenerator\nfrom keras.models import Sequential, Model\nfrom keras.layers import Conv2D, MaxPool2D, Flatten, Dense, GlobalAveragePooling2D, Dropout, BatchNormalization, Input\nfrom keras.callbacks import ReduceLROnPlateau, EarlyStopping, ModelCheckpoint\nfrom keras.applications.vgg16 import VGG16, preprocess_input\n\nbatch_size = 128","metadata":{"execution":{"iopub.status.busy":"2022-08-23T15:44:58.378073Z","iopub.execute_input":"2022-08-23T15:44:58.378545Z","iopub.status.idle":"2022-08-23T15:45:05.570053Z","shell.execute_reply.started":"2022-08-23T15:44:58.378497Z","shell.execute_reply":"2022-08-23T15:45:05.569052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"seed = 666\ntf.random.set_seed(seed)\nnp.random.seed(seed)\nos.environ[\"PYTHONHASHSEED\"] = str(seed)                      \nrandom.seed(666)","metadata":{"execution":{"iopub.status.busy":"2022-08-23T15:45:05.572824Z","iopub.execute_input":"2022-08-23T15:45:05.574190Z","iopub.status.idle":"2022-08-23T15:45:05.581147Z","shell.execute_reply.started":"2022-08-23T15:45:05.574139Z","shell.execute_reply":"2022-08-23T15:45:05.579918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir(\"../input/dogs-vs-cats/\")\n\n","metadata":{"execution":{"iopub.status.busy":"2022-08-23T15:45:05.582857Z","iopub.execute_input":"2022-08-23T15:45:05.583206Z","iopub.status.idle":"2022-08-23T15:45:05.599864Z","shell.execute_reply.started":"2022-08-23T15:45:05.583171Z","shell.execute_reply":"2022-08-23T15:45:05.598953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TRAIN_PATH = \"../input/dogs-vs-cats/train.zip\"\nTEST_PATH = \"../input/dogs-vs-cats/test1.zip\"\n\nFILES = \"/kaggle/files/unzipped/\"\n\nwith zipfile.ZipFile(TRAIN_PATH, 'r') as zipp:\n    zipp.extractall(FILES)\n    \nwith zipfile.ZipFile(TEST_PATH, 'r') as zipp:\n    zipp.extractall(FILES)","metadata":{"execution":{"iopub.status.busy":"2022-08-23T15:45:05.602194Z","iopub.execute_input":"2022-08-23T15:45:05.602561Z","iopub.status.idle":"2022-08-23T15:45:24.179697Z","shell.execute_reply.started":"2022-08-23T15:45:05.602509Z","shell.execute_reply":"2022-08-23T15:45:24.178727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.DataFrame({\"file\": os.listdir(\"/kaggle/files/unzipped/train\")})\ntrain_df[\"label\"] = train_df[\"file\"].apply(lambda x: x.split(\".\")[0])\n\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-23T15:45:41.365143Z","iopub.execute_input":"2022-08-23T15:45:41.366157Z","iopub.status.idle":"2022-08-23T15:45:41.411857Z","shell.execute_reply.started":"2022-08-23T15:45:41.366101Z","shell.execute_reply":"2022-08-23T15:45:41.410789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.DataFrame({\"file\": os.listdir(\"/kaggle/files/unzipped/test1\")})\n\ntest_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-23T15:45:45.703543Z","iopub.execute_input":"2022-08-23T15:45:45.704468Z","iopub.status.idle":"2022-08-23T15:45:45.729562Z","shell.execute_reply.started":"2022-08-23T15:45:45.704434Z","shell.execute_reply":"2022-08-23T15:45:45.728503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(figsize = (6, 6), facecolor = \"#e5e5e5\")\nax.set_facecolor(\"#e5e5e5\")\n\nsns.countplot(x = \"label\", data = train_df, ax = ax)\n\nax.set_title(\"Distribution of Class Labels\")\nsns.despine()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-23T15:45:53.750352Z","iopub.execute_input":"2022-08-23T15:45:53.750716Z","iopub.status.idle":"2022-08-23T15:45:53.966772Z","shell.execute_reply.started":"2022-08-23T15:45:53.750684Z","shell.execute_reply":"2022-08-23T15:45:53.965890Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(1, figsize = (8, 8))\nfig.suptitle(\"Training Set Images (Sample)\")\n\nfor i in range(25):\n\n    plt.subplot(5, 5, i + 1)\n    image = load_img(FILES + \"train/\" + train_df[\"file\"][i])\n    plt.imshow(image)\n    plt.axis(\"off\")\n    \nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-23T15:46:51.259495Z","iopub.execute_input":"2022-08-23T15:46:51.260534Z","iopub.status.idle":"2022-08-23T15:46:52.691258Z","shell.execute_reply.started":"2022-08-23T15:46:51.260474Z","shell.execute_reply":"2022-08-23T15:46:52.690331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(1, figsize = (8, 8))\nfig.suptitle(\"Sample Dog images from Training Set\")\n\nfor i in range(25):\n    \n    plt.subplot(5, 5, i + 1)\n    image = load_img(FILES + \"train/\" + train_df.query(\"label == 'dog'\").file.values[i])\n    plt.imshow(image)\n    plt.axis(\"off\")\n    \nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-23T15:46:53.478901Z","iopub.execute_input":"2022-08-23T15:46:53.479268Z","iopub.status.idle":"2022-08-23T15:46:55.225816Z","shell.execute_reply.started":"2022-08-23T15:46:53.479237Z","shell.execute_reply":"2022-08-23T15:46:55.224766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(1, figsize = (8, 8))\nfig.suptitle(\"Sample Cat images from Training Set\")\n\nfor i in range(25):\n    \n    plt.subplot(5, 5, i + 1)\n    image = load_img(FILES + \"train/\" + train_df.query(\"label == 'cat'\").file.values[i])\n    plt.imshow(image)\n    plt.axis(\"off\")\n    \nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-23T15:47:02.273973Z","iopub.execute_input":"2022-08-23T15:47:02.274347Z","iopub.status.idle":"2022-08-23T15:47:03.772869Z","shell.execute_reply.started":"2022-08-23T15:47:02.274314Z","shell.execute_reply":"2022-08-23T15:47:03.771622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data, val_data = train_test_split(train_df, \n                                        test_size = 0.2, \n                                        stratify = train_df[\"label\"], \n                                        random_state = 666)","metadata":{"execution":{"iopub.status.busy":"2022-08-23T15:47:06.085433Z","iopub.execute_input":"2022-08-23T15:47:06.086023Z","iopub.status.idle":"2022-08-23T15:47:06.115892Z","shell.execute_reply.started":"2022-08-23T15:47:06.085987Z","shell.execute_reply":"2022-08-23T15:47:06.115006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen = ImageDataGenerator(\n    rotation_range = 30, \n    width_shift_range = 0.1,\n    height_shift_range = 0.1, \n    brightness_range = (0.5, 1), \n    zoom_range = 0.2,\n    horizontal_flip = True, \n    rescale = 1./255,\n)\n\nsample_df = train_data.sample(1)\n\nsample_generator = datagen.flow_from_dataframe(\n    dataframe = sample_df,\n    directory = FILES + \"train/\",\n    x_col = \"file\",\n    y_col = \"label\",\n    class_mode = \"categorical\",\n    target_size = (224, 224),\n    seed = 666\n)\n\nplt.figure(figsize = (14, 8))\n\nfor i in range(50):\n    \n    plt.subplot(5, 10, i + 1)\n    \n    for X, y in sample_generator:\n\n        plt.imshow(X[0])\n        plt.axis(\"off\")\n        break\n        \nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-23T15:47:44.204329Z","iopub.execute_input":"2022-08-23T15:47:44.204707Z","iopub.status.idle":"2022-08-23T15:47:47.761691Z","shell.execute_reply.started":"2022-08-23T15:47:44.204675Z","shell.execute_reply":"2022-08-23T15:47:47.760503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(\n    rotation_range = 15, \n#     width_shift_range = 0.1,\n#     height_shift_range = 0.1, \n#     brightness_range = (0.5, 1), \n#     zoom_range = 0.1,\n    horizontal_flip = True,\n    preprocessing_function = preprocess_input\n)\n\nval_datagen = ImageDataGenerator(preprocessing_function = preprocess_input)","metadata":{"execution":{"iopub.status.busy":"2022-08-23T15:47:47.764058Z","iopub.execute_input":"2022-08-23T15:47:47.764404Z","iopub.status.idle":"2022-08-23T15:47:47.772878Z","shell.execute_reply.started":"2022-08-23T15:47:47.764371Z","shell.execute_reply":"2022-08-23T15:47:47.769328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator = train_datagen.flow_from_dataframe(\n    dataframe = train_data,\n    directory = FILES + \"train/\",\n    x_col = \"file\",\n    y_col = \"label\",\n    class_mode = \"categorical\",\n    target_size = (224, 224),\n    batch_size = batch_size,\n    seed = 666,\n)\n\nval_generator = val_datagen.flow_from_dataframe(\n    dataframe = val_data,\n    directory = FILES + \"train/\",\n    x_col = \"file\",\n    y_col = \"label\",\n    class_mode = \"categorical\",\n    target_size = (224, 224),\n    batch_size = batch_size,\n    seed = 666,\n    shuffle = False\n)","metadata":{"execution":{"iopub.status.busy":"2022-08-23T15:47:49.094267Z","iopub.execute_input":"2022-08-23T15:47:49.094652Z","iopub.status.idle":"2022-08-23T15:47:49.347391Z","shell.execute_reply.started":"2022-08-23T15:47:49.094618Z","shell.execute_reply":"2022-08-23T15:47:49.346491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.applications.vgg19 import VGG19\nfrom keras.models import Model\n\n\ninput_shape=(224,224,3)\nbatch_size= 128\n\nbase_model = VGG19(\n    weights = \"imagenet\", \n    input_shape = (224, 224, 3),\n    include_top = False\n)\n\nfor layer in base_model.layers:\n    layer.trainable = False\n    \ndef vgg19_pretrained():\n    \n    model= Sequential([\n        base_model,\n        GlobalAveragePooling2D(),\n        Dense(100,activation='relu'),\n        Dropout(0.4),\n        Dense(64,activation='relu'),\n        Dense(2,activation='softmax')\n    ])\n    \n    return model\n\n\ntf.keras.backend.clear_session()","metadata":{"execution":{"iopub.status.busy":"2022-08-23T15:49:18.660964Z","iopub.execute_input":"2022-08-23T15:49:18.661355Z","iopub.status.idle":"2022-08-23T15:49:22.957404Z","shell.execute_reply.started":"2022-08-23T15:49:18.661322Z","shell.execute_reply":"2022-08-23T15:49:22.956080Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = vgg19_pretrained()\n\nmodel.compile(loss = \"categorical_crossentropy\", optimizer = \"adam\", metrics = \"accuracy\")\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-08-23T15:49:30.237799Z","iopub.execute_input":"2022-08-23T15:49:30.238467Z","iopub.status.idle":"2022-08-23T15:49:30.340029Z","shell.execute_reply.started":"2022-08-23T15:49:30.238429Z","shell.execute_reply":"2022-08-23T15:49:30.338933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"reduce_lr = ReduceLROnPlateau(\n    monitor = \"val_accuracy\", \n    patience = 2,\n    verbose = 1, \n    factor = 0.5, \n    min_lr = 0.000000001\n)\n\nearly_stopping = EarlyStopping(\n    monitor = \"val_accuracy\",\n    patience = 5,\n    verbose = 1,\n    mode = \"max\",\n)\n\ncheckpoint = ModelCheckpoint(\n    monitor = \"val_accuracy\",\n    filepath = \"catdog_vgg19_.{epoch:02d}-{val_accuracy:.6f}.hdf5\",\n    verbose = 1,\n    save_best_only = True, \n    save_weights_only = True\n)","metadata":{"execution":{"iopub.status.busy":"2022-08-23T15:49:32.318930Z","iopub.execute_input":"2022-08-23T15:49:32.320034Z","iopub.status.idle":"2022-08-23T15:49:32.326490Z","shell.execute_reply.started":"2022-08-23T15:49:32.319989Z","shell.execute_reply":"2022-08-23T15:49:32.325349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(\n    train_generator,\n    epochs = 5, \n    validation_data = val_generator,\n    validation_steps = val_data.shape[0] // batch_size,\n    steps_per_epoch = train_data.shape[0] // batch_size,\n    callbacks = [reduce_lr, early_stopping, checkpoint]\n)","metadata":{"execution":{"iopub.status.busy":"2022-08-23T15:57:25.349131Z","iopub.execute_input":"2022-08-23T15:57:25.349498Z","iopub.status.idle":"2022-08-23T16:22:47.160387Z","shell.execute_reply.started":"2022-08-23T15:57:25.349466Z","shell.execute_reply":"2022-08-23T16:22:47.159340Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.keras.backend.clear_session()\n\nmodel = vgg19_pretrained()\n\nmodel.load_weights(\"./catdog_vgg19_.10-0.983774.hdf5\")","metadata":{"execution":{"iopub.status.busy":"2022-08-23T16:23:41.043104Z","iopub.execute_input":"2022-08-23T16:23:41.043848Z","iopub.status.idle":"2022-08-23T16:23:41.182387Z","shell.execute_reply.started":"2022-08-23T16:23:41.043808Z","shell.execute_reply":"2022-08-23T16:23:41.180996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axes = plt.subplots(1, 2, figsize = (12, 4))\n\nsns.lineplot(x = range(len(history.history[\"loss\"])), y = history.history[\"loss\"], ax = axes[0], label = \"Training Loss\")\nsns.lineplot(x = range(len(history.history[\"loss\"])), y = history.history[\"val_loss\"], ax = axes[0], label = \"Validation Loss\")\n\nsns.lineplot(x = range(len(history.history[\"accuracy\"])), y = history.history[\"accuracy\"], ax = axes[1], label = \"Training Accuracy\")\nsns.lineplot(x = range(len(history.history[\"accuracy\"])), y = history.history[\"val_accuracy\"], ax = axes[1], label = \"Validation Accuracy\")\naxes[0].set_title(\"Loss\"); axes[1].set_title(\"Accuracy\")\n\nsns.despine()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-23T16:24:28.087773Z","iopub.execute_input":"2022-08-23T16:24:28.088160Z","iopub.status.idle":"2022-08-23T16:24:28.474992Z","shell.execute_reply.started":"2022-08-23T16:24:28.088122Z","shell.execute_reply":"2022-08-23T16:24:28.474086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_pred = model.predict(val_generator, steps = np.ceil(val_data.shape[0] / batch_size))\nval_data.loc[:, \"val_pred\"] = np.argmax(val_pred, axis = 1)\n\nlabels = dict((v, k) for k, v in val_generator.class_indices.items())\n\nval_data.loc[:, \"val_pred\"] = val_data.loc[:, \"val_pred\"].map(labels)","metadata":{"execution":{"iopub.status.busy":"2022-08-23T16:24:29.582251Z","iopub.execute_input":"2022-08-23T16:24:29.582633Z","iopub.status.idle":"2022-08-23T16:24:52.707225Z","shell.execute_reply.started":"2022-08-23T16:24:29.582601Z","shell.execute_reply":"2022-08-23T16:24:52.706160Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(figsize = (9, 6))\n\ncm = confusion_matrix(val_data[\"label\"], val_data[\"val_pred\"])\n\ndisp = ConfusionMatrixDisplay(confusion_matrix = cm, display_labels = [\"cat\", \"dog\"])\ndisp.plot(cmap = plt.cm.Blues, ax = ax)\n\nax.set_title(\"Validation Set\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-23T16:24:52.709217Z","iopub.execute_input":"2022-08-23T16:24:52.709937Z","iopub.status.idle":"2022-08-23T16:24:52.962280Z","shell.execute_reply.started":"2022-08-23T16:24:52.709894Z","shell.execute_reply":"2022-08-23T16:24:52.961204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}