{"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-10-01T16:05:50.563661Z","iopub.execute_input":"2022-10-01T16:05:50.564317Z","iopub.status.idle":"2022-10-01T16:05:50.573536Z","shell.execute_reply.started":"2022-10-01T16:05:50.56427Z","shell.execute_reply":"2022-10-01T16:05:50.572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport tensorflow as tf\nfrom tensorflow import keras\nimport os\n\nimport matplotlib.pyplot as plt\nimport matplotlib.image as img\nimport seaborn as sns\n\n'''setting seed'''\nseed = 0\nnp.random.seed(seed)\ntf.random.set_seed(3)","metadata":{"execution":{"iopub.status.busy":"2022-10-01T16:05:50.5851Z","iopub.execute_input":"2022-10-01T16:05:50.585529Z","iopub.status.idle":"2022-10-01T16:05:57.057929Z","shell.execute_reply.started":"2022-10-01T16:05:50.585495Z","shell.execute_reply":"2022-10-01T16:05:57.056904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import zipfile\n\nzip_files = ['test1', 'train']\n\nfor zip_file in zip_files:\n    with zipfile.ZipFile(\"../input/dogs-vs-cats/{}.zip\".format(zip_file),\"r\") as z:\n        z.extractall(\".\")\n        print(\"{} unzipped\".format(zip_file))","metadata":{"execution":{"iopub.status.busy":"2022-10-01T16:08:04.775887Z","iopub.execute_input":"2022-10-01T16:08:04.776367Z","iopub.status.idle":"2022-10-01T16:08:21.254458Z","shell.execute_reply.started":"2022-10-01T16:08:04.776329Z","shell.execute_reply":"2022-10-01T16:08:21.253298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TRAIN_DIR_PATH = './train'\nfile_names = os.listdir(TRAIN_DIR_PATH)\nprint('There are {} number of images in directory.'.format(len(file_names)))","metadata":{"execution":{"iopub.status.busy":"2022-10-01T16:38:36.845252Z","iopub.execute_input":"2022-10-01T16:38:36.84569Z","iopub.status.idle":"2022-10-01T16:38:36.867693Z","shell.execute_reply.started":"2022-10-01T16:38:36.845654Z","shell.execute_reply":"2022-10-01T16:38:36.866598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def to_dataframe(file_names):\n    files, labels = list(), list()\n    for file in file_names:\n        files.append(file)\n        labels.append(file[:3])\n    df = pd.DataFrame({'filename':files, 'label':labels})\n    return df\n\ndf = to_dataframe(file_names)","metadata":{"execution":{"iopub.status.busy":"2022-10-01T16:38:37.977278Z","iopub.execute_input":"2022-10-01T16:38:37.977648Z","iopub.status.idle":"2022-10-01T16:38:37.998062Z","shell.execute_reply.started":"2022-10-01T16:38:37.977615Z","shell.execute_reply":"2022-10-01T16:38:37.996852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndog_image_count = 0\ncat_image_count = 0\nfor image_name in os.listdir(TRAIN_DIR_PATH):\n    if image_name.startswith('dog'):\n        dog_image_count += 1\n    else:\n        cat_image_count += 1\nprint(f'Number of dog images in the dataset: {dog_image_count} images')\nprint(f'Number of cat images in the dataset: {cat_image_count} images')","metadata":{"execution":{"iopub.status.busy":"2022-10-01T16:38:39.164579Z","iopub.execute_input":"2022-10-01T16:38:39.164972Z","iopub.status.idle":"2022-10-01T16:38:39.193839Z","shell.execute_reply.started":"2022-10-01T16:38:39.164937Z","shell.execute_reply":"2022-10-01T16:38:39.192975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Info about the data (train)\n","metadata":{"execution":{"iopub.status.busy":"2022-10-01T16:38:40.358143Z","iopub.execute_input":"2022-10-01T16:38:40.359162Z","iopub.status.idle":"2022-10-01T16:38:40.363499Z","shell.execute_reply.started":"2022-10-01T16:38:40.359105Z","shell.execute_reply":"2022-10-01T16:38:40.36241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"raw","source":"\n\n\n\n\n\n\n","metadata":{}},{"cell_type":"code","source":"import random\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\n\ndef show_random_sample(path):\n    \"\"\"\n    Shows random sample of image data from path\n    \"\"\"\n    random_image= random.choice(os.listdir(path))\n    random_image_path = f'{path}/{random_image}'\n    img = mpimg.imread(random_image_path)\n    imgplot = plt.imshow(img)\n    plt.axis(False)\n    plt.title(f'{str(random_image.split(\".\")[0]).capitalize()}\\nShape: {img.shape}')\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-01T16:38:41.923356Z","iopub.execute_input":"2022-10-01T16:38:41.923826Z","iopub.status.idle":"2022-10-01T16:38:41.93514Z","shell.execute_reply.started":"2022-10-01T16:38:41.923786Z","shell.execute_reply":"2022-10-01T16:38:41.934239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_random_sample(TRAIN_DIR_PATH)","metadata":{"execution":{"iopub.status.busy":"2022-10-01T16:39:11.032226Z","iopub.execute_input":"2022-10-01T16:39:11.032608Z","iopub.status.idle":"2022-10-01T16:39:11.25343Z","shell.execute_reply.started":"2022-10-01T16:39:11.032576Z","shell.execute_reply":"2022-10-01T16:39:11.252188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Loading images using cv2 and converting to greayscale\n# Note: We'll also have to record labels, to not get confused between images\nimport cv2\n\ny_labels = []\nimgs_array = []\nfor image_name in os.listdir(TRAIN_DIR_PATH):\n    image_path = f'{data_dir}/{image_name}'\n    image = cv2.imread(image_path)\n    image = cv2.resize(image, (50, 50))\n    imgs_array.append(image)\n    if image_name.startswith('dog'):\n        y_labels.append(1)\n    else:\n        y_labels.append(0)\n        \n                    \n    ","metadata":{"execution":{"iopub.status.busy":"2022-10-01T16:39:36.042984Z","iopub.execute_input":"2022-10-01T16:39:36.043523Z","iopub.status.idle":"2022-10-01T16:40:43.499897Z","shell.execute_reply.started":"2022-10-01T16:39:36.043473Z","shell.execute_reply":"2022-10-01T16:40:43.498786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"Is it a dog? {y_labels[0]}\")\nprint(f\"Image as an array:\\n{imgs_array[0]}\")","metadata":{"execution":{"iopub.status.busy":"2022-10-01T16:40:43.501711Z","iopub.execute_input":"2022-10-01T16:40:43.502029Z","iopub.status.idle":"2022-10-01T16:40:43.509165Z","shell.execute_reply.started":"2022-10-01T16:40:43.502Z","shell.execute_reply":"2022-10-01T16:40:43.507787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(0,9):\n    plt.imshow(imgs_array[i], cmap='gray')\n    plt.axis(False);","metadata":{"execution":{"iopub.status.busy":"2022-10-01T16:40:43.510561Z","iopub.execute_input":"2022-10-01T16:40:43.510868Z","iopub.status.idle":"2022-10-01T16:40:43.705409Z","shell.execute_reply.started":"2022-10-01T16:40:43.51084Z","shell.execute_reply":"2022-10-01T16:40:43.704222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n","metadata":{"execution":{"iopub.status.busy":"2022-10-01T16:07:04.694277Z","iopub.execute_input":"2022-10-01T16:07:04.694616Z","iopub.status.idle":"2022-10-01T16:07:04.706743Z","shell.execute_reply.started":"2022-10-01T16:07:04.694585Z","shell.execute_reply":"2022-10-01T16:07:04.705653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2022-10-01T16:43:01.790883Z","iopub.execute_input":"2022-10-01T16:43:01.791485Z","iopub.status.idle":"2022-10-01T16:43:01.805374Z","shell.execute_reply.started":"2022-10-01T16:43:01.791435Z","shell.execute_reply":"2022-10-01T16:43:01.804079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('data set label distribution:\\n',df['label'].value_counts())\n\nplt.figure(figsize=(7,7))\nsns.countplot(x = df['label'])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-01T16:42:04.734709Z","iopub.execute_input":"2022-10-01T16:42:04.735183Z","iopub.status.idle":"2022-10-01T16:42:04.958054Z","shell.execute_reply.started":"2022-10-01T16:42:04.735121Z","shell.execute_reply":"2022-10-01T16:42:04.957028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat = [file for file in file_names if file[:3]=='cat']\ndog = [file for file in file_names if file[:3] =='dog' ]","metadata":{"execution":{"iopub.status.busy":"2022-10-01T16:44:22.855567Z","iopub.execute_input":"2022-10-01T16:44:22.856039Z","iopub.status.idle":"2022-10-01T16:44:22.871991Z","shell.execute_reply.started":"2022-10-01T16:44:22.855999Z","shell.execute_reply":"2022-10-01T16:44:22.870586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,7))\nfor i, c in enumerate(np.random.randint(0,len(cat),5), start=1):\n    im = img.imread('./train/'+cat[c])\n    plt.subplot(2,5,i)\n    plt.imshow(im)\n    \nfor i, c in enumerate(np.random.randint(0,len(dog),5), start=6):\n    im = img.imread('./train/'+dog[c])\n    plt.subplot(2,5,i)\n    plt.imshow(im)","metadata":{"execution":{"iopub.status.busy":"2022-10-01T16:51:33.66351Z","iopub.execute_input":"2022-10-01T16:51:33.663896Z","iopub.status.idle":"2022-10-01T16:51:35.44892Z","shell.execute_reply.started":"2022-10-01T16:51:33.663863Z","shell.execute_reply":"2022-10-01T16:51:35.44794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\ntrain_set, valid_set = train_test_split(df, test_size=0.2,random_state=seed)","metadata":{"execution":{"iopub.status.busy":"2022-10-01T16:52:24.233864Z","iopub.execute_input":"2022-10-01T16:52:24.234337Z","iopub.status.idle":"2022-10-01T16:52:24.435975Z","shell.execute_reply.started":"2022-10-01T16:52:24.234298Z","shell.execute_reply":"2022-10-01T16:52:24.434835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set.shape, valid_set.shape","metadata":{"execution":{"iopub.status.busy":"2022-10-01T16:53:11.582389Z","iopub.execute_input":"2022-10-01T16:53:11.582781Z","iopub.status.idle":"2022-10-01T16:53:11.590281Z","shell.execute_reply.started":"2022-10-01T16:53:11.582751Z","shell.execute_reply":"2022-10-01T16:53:11.588811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('train set distribution:\\n',train_set['label'].value_counts())\nprint('\\nvalid set distribution:\\n',valid_set['label'].value_counts())\nprint()\n\nplt.figure(figsize=(10,5))\nplt.subplot(1,3,1)\nsns.countplot(x=train_set['label'], order=['dog','cat'])\nplt.title('Train set label distribution')\n\nplt.subplot(1,3,3)\nsns.countplot(x=valid_set['label'], order=['dog','cat'])\nplt.title('valid set label distribution')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-01T16:53:29.777463Z","iopub.execute_input":"2022-10-01T16:53:29.77795Z","iopub.status.idle":"2022-10-01T16:53:30.022591Z","shell.execute_reply.started":"2022-10-01T16:53:29.777907Z","shell.execute_reply":"2022-10-01T16:53:30.021587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n\nWIDTH, HEIGHT = 150, 150\nbatch_size = 32","metadata":{"execution":{"iopub.status.busy":"2022-10-01T17:04:34.673014Z","iopub.execute_input":"2022-10-01T17:04:34.673773Z","iopub.status.idle":"2022-10-01T17:04:35.890825Z","shell.execute_reply.started":"2022-10-01T17:04:34.673736Z","shell.execute_reply":"2022-10-01T17:04:35.889696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagenerator = ImageDataGenerator(rotation_range=15, \n                                  rescale=1./255, \n                                  shear_range=0.1,\n                                  zoom_range=0.2,\n                                  horizontal_flip=True,\n                                  width_shift_range=0.1,\n                                  height_shift_range=0.1)\ntraining_data = train_datagenerator.flow_from_dataframe(dataframe=train_set, \n                                                        directory='./train',\n                                                       x_col='filename',\n                                                       y_col='label',\n                                                       target_size=(WIDTH, HEIGHT),\n                                                       class_mode='categorical',\n                                                       batch_size=batch_size)","metadata":{"execution":{"iopub.status.busy":"2022-10-01T17:08:03.434303Z","iopub.execute_input":"2022-10-01T17:08:03.434733Z","iopub.status.idle":"2022-10-01T17:08:03.669534Z","shell.execute_reply.started":"2022-10-01T17:08:03.434697Z","shell.execute_reply":"2022-10-01T17:08:03.668242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_datagenerator = ImageDataGenerator(rescale=1./255)\nvalidation_data = valid_datagenerator.flow_from_dataframe(dataframe=valid_set,\n                                                         directory='./train',\n                                                         x_col='filename',\n                                                         y_col='label',\n                                                         target_size=(WIDTH,HEIGHT),\n                                                         class_mode='categorical',\n                                                         batch_size=batch_size)","metadata":{"execution":{"iopub.status.busy":"2022-10-01T17:10:20.737742Z","iopub.execute_input":"2022-10-01T17:10:20.738222Z","iopub.status.idle":"2022-10-01T17:10:20.808395Z","shell.execute_reply.started":"2022-10-01T17:10:20.738184Z","shell.execute_reply":"2022-10-01T17:10:20.807165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.layers import *\nfrom functools import partial\n\nDefaultConv2D = partial(keras.layers.Conv2D, kernel_size=3, activation='relu', padding=\"SAME\")\n\nmodel = keras.models.Sequential()\n\nmodel.add(DefaultConv2D(filters=32, kernel_size=5, input_shape=(WIDTH, HEIGHT, 3)))\nmodel.add(MaxPooling2D(pool_size=2))\n\nmodel.add(DefaultConv2D(filters=64))\nmodel.add(DefaultConv2D(filters=64))\nmodel.add(MaxPooling2D(pool_size=2))\nmodel.add(DefaultConv2D(filters=128))\nmodel.add(DefaultConv2D(filters=128))\nmodel.add(MaxPooling2D(pool_size=2))\n\nmodel.add(Flatten())\nmodel.add(Dense(64, activation='relu'))\nmodel.add(Dropout(0.2))\n\nmodel.add(Dense(32, activation='relu'))\nmodel.add(Dropout(0.2))\n\nmodel.add(Dense(2, activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2022-10-01T17:12:26.151034Z","iopub.execute_input":"2022-10-01T17:12:26.151444Z","iopub.status.idle":"2022-10-01T17:12:26.274325Z","shell.execute_reply.started":"2022-10-01T17:12:26.15141Z","shell.execute_reply":"2022-10-01T17:12:26.273379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-10-01T17:12:52.185791Z","iopub.execute_input":"2022-10-01T17:12:52.186255Z","iopub.status.idle":"2022-10-01T17:12:52.193762Z","shell.execute_reply.started":"2022-10-01T17:12:52.186217Z","shell.execute_reply":"2022-10-01T17:12:52.19264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau\n\nearlystop_cb = EarlyStopping(patience=10, restore_best_weights=True)\nreduce_lr_cb = ReduceLROnPlateau(factor=0.5, patience=5, monitor='val_loss', min_lr=0.00001)\ncheckpoint_cb = ModelCheckpoint('model.h5', save_best_only=True)\n\ncallbacks = [earlystop_cb, reduce_lr_cb, checkpoint_cb]","metadata":{"execution":{"iopub.status.busy":"2022-10-01T17:13:54.905883Z","iopub.execute_input":"2022-10-01T17:13:54.906729Z","iopub.status.idle":"2022-10-01T17:13:54.914217Z","shell.execute_reply.started":"2022-10-01T17:13:54.906689Z","shell.execute_reply":"2022-10-01T17:13:54.912969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"opt = keras.optimizers.SGD(learning_rate=0.01, momentum=0.9)\nmodel.compile(loss=\"categorical_crossentropy\", optimizer=opt, metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-10-01T17:14:30.025902Z","iopub.execute_input":"2022-10-01T17:14:30.026336Z","iopub.status.idle":"2022-10-01T17:14:30.042848Z","shell.execute_reply.started":"2022-10-01T17:14:30.026303Z","shell.execute_reply":"2022-10-01T17:14:30.04201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(training_data,\n                   epochs=50,\n                   validation_data=validation_data,\n                   validation_steps=valid_set.shape[0]//batch_size,\n                   steps_per_epoch=train_set.shape[0]//batch_size, \n                   callbacks=callbacks)","metadata":{"execution":{"iopub.status.busy":"2022-10-01T17:14:46.344693Z","iopub.execute_input":"2022-10-01T17:14:46.345731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model1 = keras.models.load_model('model.h5')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_loss, test_acc = model1.evaluate(validation_data, steps=len(validation_data), verbose=1)\nprint('Loss: %.3f' % (test_loss))\nprint('Accuracy: %.3f' % (test_acc * 100.0)) ","metadata":{},"execution_count":null,"outputs":[]}]}