{"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":"2023-02-16T17:06:25.341694Z","iopub.execute_input":"2023-02-16T17:06:25.342653Z","iopub.status.idle":"2023-02-16T17:06:25.377096Z","shell.execute_reply.started":"2023-02-16T17:06:25.342563Z","shell.execute_reply":"2023-02-16T17:06:25.376046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pwd","metadata":{"execution":{"iopub.status.busy":"2023-02-16T17:06:33.523012Z","iopub.execute_input":"2023-02-16T17:06:33.523873Z","iopub.status.idle":"2023-02-16T17:06:33.534473Z","shell.execute_reply.started":"2023-02-16T17:06:33.523817Z","shell.execute_reply":"2023-02-16T17:06:33.531803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!unzip -q \"/kaggle/input/dogs-vs-cats/train.zip\"\n!unzip -q \"/kaggle/input/dogs-vs-cats/test1.zip\"","metadata":{"execution":{"iopub.status.busy":"2023-02-16T17:06:34.461553Z","iopub.execute_input":"2023-02-16T17:06:34.46235Z","iopub.status.idle":"2023-02-16T17:06:54.609783Z","shell.execute_reply.started":"2023-02-16T17:06:34.462306Z","shell.execute_reply":"2023-02-16T17:06:54.608577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ls","metadata":{"execution":{"iopub.status.busy":"2023-02-16T17:07:01.647165Z","iopub.execute_input":"2023-02-16T17:07:01.647778Z","iopub.status.idle":"2023-02-16T17:07:02.598377Z","shell.execute_reply.started":"2023-02-16T17:07:01.64773Z","shell.execute_reply":"2023-02-16T17:07:02.597096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\nimport matplotlib.pyplot as plt\n%matplotlib inline\n\nimg = np.asarray(Image.open('./test1/2500.jpg'))\n\nplt.imshow(img)\nplt.xticks([])\nplt.yticks([])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-16T17:07:02.60104Z","iopub.execute_input":"2023-02-16T17:07:02.601547Z","iopub.status.idle":"2023-02-16T17:07:02.8981Z","shell.execute_reply.started":"2023-02-16T17:07:02.601494Z","shell.execute_reply":"2023-02-16T17:07:02.89719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport glob\nimport shutil\n\ntrain_files = glob.glob('./train/*.jpg') \ntest_files = glob.glob('./test1/*.jpg')\nprint(\"Number of train files\", len(train_files))\nprint(\"Number of test files\", len(test_files))","metadata":{"execution":{"iopub.status.busy":"2023-02-16T17:07:04.976109Z","iopub.execute_input":"2023-02-16T17:07:04.977213Z","iopub.status.idle":"2023-02-16T17:07:05.081972Z","shell.execute_reply.started":"2023-02-16T17:07:04.977167Z","shell.execute_reply":"2023-02-16T17:07:05.080768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_dir = os.getcwd()\ntrain_path = os.path.join(base_dir, 'train_labeled')\nif not os.path.exists(train_path):\n    os.makedirs(train_path)\n    \nclasses = ['cat', 'dog']\n\nfor cl in classes:\n    cl_path = os.path.join(train_path, cl)\n    if not os.path.exists(cl_path):\n        os.makedirs(cl_path)\n        \n    cl_files = glob.glob(os.path.join(base_dir, 'train') + '/' + cl +'*.jpg')\n    \n    for file_name in cl_files:\n        shutil.move(file_name, cl_path)","metadata":{"execution":{"iopub.status.busy":"2023-02-16T17:07:05.855696Z","iopub.execute_input":"2023-02-16T17:07:05.856357Z","iopub.status.idle":"2023-02-16T17:07:07.06336Z","shell.execute_reply.started":"2023-02-16T17:07:05.85632Z","shell.execute_reply":"2023-02-16T17:07:07.062336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for cl in classes:\n    cl_path = os.path.join(train_path, cl)\n    num_files = len(os.listdir(cl_path))\n    print(\"Number of images in \", cl_path, \": \", num_files)","metadata":{"execution":{"iopub.status.busy":"2023-02-16T17:07:07.065021Z","iopub.execute_input":"2023-02-16T17:07:07.065389Z","iopub.status.idle":"2023-02-16T17:07:07.085224Z","shell.execute_reply.started":"2023-02-16T17:07:07.06536Z","shell.execute_reply":"2023-02-16T17:07:07.084248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nkeras = tf.keras\nfrom keras.preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2023-02-16T17:07:07.93644Z","iopub.execute_input":"2023-02-16T17:07:07.937239Z","iopub.status.idle":"2023-02-16T17:07:14.502037Z","shell.execute_reply.started":"2023-02-16T17:07:07.93715Z","shell.execute_reply":"2023-02-16T17:07:14.501042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BATCH_SIZE = 128\nIMG_SHAPE = (150, 150)\naug_image_gen = ImageDataGenerator(rescale = 1./255,\n                              zoom_range = 0.5,\n                              width_shift_range = 0.15,\n                              height_shift_range = 0.15,\n                              rotation_range = 30,\n                              fill_mode = 'nearest',\n                              validation_split=0.2\n                              )\n\ntrain_gen = aug_image_gen.flow_from_directory(directory = train_path,\n                                          batch_size = BATCH_SIZE,\n                                          target_size = IMG_SHAPE,\n                                          shuffle = True,\n                                          subset = 'training',\n                                          class_mode = 'sparse'\n                                         )\n\nval_gen = aug_image_gen.flow_from_directory(directory = train_path,\n                                          batch_size = BATCH_SIZE,\n                                          target_size = IMG_SHAPE,\n                                          shuffle = True,\n                                          subset = 'validation',\n                                          class_mode = 'sparse'\n                                         )","metadata":{"execution":{"iopub.status.busy":"2023-02-16T18:14:07.146462Z","iopub.execute_input":"2023-02-16T18:14:07.146831Z","iopub.status.idle":"2023-02-16T18:14:07.807238Z","shell.execute_reply.started":"2023-02-16T18:14:07.146799Z","shell.execute_reply":"2023-02-16T18:14:07.806147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.layers import Conv2D, Dense, Flatten, BatchNormalization, Dropout, MaxPool2D, Input\n\nimg_input = Input(shape = (IMG_SHAPE[0], IMG_SHAPE[1], 3))\noutput = Conv2D(32, (3, 3), padding = 'same', activation = 'relu')(img_input)\noutput = MaxPool2D((2, 2))(output)\noutput = BatchNormalization()(output)\n\noutput = Conv2D(64, (3, 3), padding = 'same', activation = 'relu')(output)\noutput = MaxPool2D((2, 2))(output)\noutput = BatchNormalization()(output)\n\noutput = Conv2D(128, (3, 3), padding = 'same', activation = 'relu')(output)\noutput = MaxPool2D((2, 2))(output)\noutput = BatchNormalization()(output)\n\noutput = Conv2D(128, (3, 3), padding = 'same', activation = 'relu')(output)\noutput = MaxPool2D((2, 2))(output)\noutput = BatchNormalization()(output)\n\noutput = Flatten()(output)\noutput = Dropout(rate=0.4)(output)\noutput = Dense(512, activation = 'relu')(output)\noutput = Dropout(rate=0.2)(output)\noutput = Dense(2, activation = 'softmax')(output)\n\nmodel = keras.Model(inputs = img_input, outputs = output)","metadata":{"execution":{"iopub.status.busy":"2023-02-16T18:14:07.808995Z","iopub.execute_input":"2023-02-16T18:14:07.809978Z","iopub.status.idle":"2023-02-16T18:14:07.922803Z","shell.execute_reply.started":"2023-02-16T18:14:07.809937Z","shell.execute_reply":"2023-02-16T18:14:07.9219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-02-16T18:14:08.192794Z","iopub.execute_input":"2023-02-16T18:14:08.193198Z","iopub.status.idle":"2023-02-16T18:14:08.235921Z","shell.execute_reply.started":"2023-02-16T18:14:08.193166Z","shell.execute_reply":"2023-02-16T18:14:08.235181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_dir = os.path.join(base_dir, 'CAT_DOG_MODEL')\nif not os.path.exists(model_dir):\n    os.makedirs(model_dir)","metadata":{"execution":{"iopub.status.busy":"2023-02-16T18:14:08.721223Z","iopub.execute_input":"2023-02-16T18:14:08.722079Z","iopub.status.idle":"2023-02-16T18:14:08.727393Z","shell.execute_reply.started":"2023-02-16T18:14:08.722003Z","shell.execute_reply":"2023-02-16T18:14:08.726162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer = 'adam', loss = 'sparse_categorical_crossentropy', metrics = ['accuracy'])\n\nfrom keras.callbacks import EarlyStopping, ReduceLROnPlateau, ModelCheckpoint\n\nckpt_saver = ModelCheckpoint(model_dir,\n                            monitor = 'val_accuracy',\n                            mode = 'max',\n                            save_best_only = True,\n                            save_freq = 'epoch',\n                            verbose = 2)\n\nreduce_lr = ReduceLROnPlateau(monitor = 'val_loss', factor = 0.2, min_lr = 1e-5, patience=7)\nearly_stop = EarlyStopping(monitor = 'val_loss', patience = 14)","metadata":{"execution":{"iopub.status.busy":"2023-02-16T18:14:09.765278Z","iopub.execute_input":"2023-02-16T18:14:09.765639Z","iopub.status.idle":"2023-02-16T18:14:09.778701Z","shell.execute_reply.started":"2023-02-16T18:14:09.765609Z","shell.execute_reply":"2023-02-16T18:14:09.77752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"EPOCHS = 60\nhistory = model.fit(train_gen, \n          validation_data = val_gen, \n          epochs = EPOCHS,\n          callbacks=[ckpt_saver, reduce_lr, early_stop])","metadata":{"execution":{"iopub.status.busy":"2023-02-16T18:14:12.010119Z","iopub.execute_input":"2023-02-16T18:14:12.010913Z","iopub.status.idle":"2023-02-16T20:49:07.376243Z","shell.execute_reply.started":"2023-02-16T18:14:12.010869Z","shell.execute_reply":"2023-02-16T20:49:07.375301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_loss = history.history['loss']\nval_loss = history.history['val_loss']\n\ntrain_accuracy = history.history['accuracy']\nval_accuracy = history.history['val_accuracy']\n\nepoch_range = np.arange(1, len(train_loss)+1)\nplt.figure()\nplt.subplot(1, 2, 1)\nplt.plot(epoch_range, train_loss, label = 'train')\nplt.plot(epoch_range, val_loss, label = 'val')\nplt.subplot(1, 2, 2)\nplt.plot(epoch_range, train_accuracy, label = 'train')\nplt.plot(epoch_range, val_accuracy, label = 'val')\n","metadata":{"execution":{"iopub.status.busy":"2023-02-16T20:50:01.246529Z","iopub.execute_input":"2023-02-16T20:50:01.246906Z","iopub.status.idle":"2023-02-16T20:50:01.540497Z","shell.execute_reply.started":"2023-02-16T20:50:01.246873Z","shell.execute_reply":"2023-02-16T20:50:01.539498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_path = os.path.join(base_dir, 'test1')\ntest_files = glob.glob(test_path + '/*.jpg')\ntest_df = pd.DataFrame({'file': test_files})\ntest_df_gen = ImageDataGenerator(rescale = 1./255 )\n\ntest_gen = test_df_gen.flow_from_dataframe(test_df,\n                                           x_col = 'file',\n                                           y_col = None,\n                                          batch_size = 100,\n                                          target_size = IMG_SHAPE,\n                                          shuffle = False,\n                                           class_mode = None\n                                         )\n\nbest_model = keras.models.load_model(model_dir)","metadata":{"execution":{"iopub.status.busy":"2023-02-16T21:27:35.378396Z","iopub.execute_input":"2023-02-16T21:27:35.378754Z","iopub.status.idle":"2023-02-16T21:27:36.390995Z","shell.execute_reply.started":"2023-02-16T21:27:35.378725Z","shell.execute_reply":"2023-02-16T21:27:36.390026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def predictions_gallery(model, gen, dims = (5, 5)):\n    \n    \n    samples = dims[0]*dims[1]\n    \n    images = [gen[0][i] for i in np.random.randint(0, 100, size = samples)]\n    reversed_class_indices = {0: 'cat', 1: 'dog'}\n    \n    plt.figure(figsize = (12, 12))\n    for i in range(samples):\n        image = images[i]\n        plt.subplot(dims[0], dims[1], i+1)\n        plt.imshow(image)\n        plt.xticks([])\n        plt.yticks([])\n        \n        output = model.predict(tf.expand_dims(image, axis=0))\n        pred = reversed_class_indices[np.argmax(output)]\n        plt.title(pred)\n        \n    plt.tight_layout()\n    plt.show()\n    \n    \npredictions_gallery(best_model, test_gen)","metadata":{"execution":{"iopub.status.busy":"2023-02-16T21:27:47.169809Z","iopub.execute_input":"2023-02-16T21:27:47.170183Z","iopub.status.idle":"2023-02-16T21:27:57.200712Z","shell.execute_reply.started":"2023-02-16T21:27:47.170152Z","shell.execute_reply":"2023-02-16T21:27:57.199678Z"},"trusted":true},"execution_count":null,"outputs":[]}]}