{"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":"<h1> Dogs vs Cats Competiton </h1>\nCreating a model to distinguish dogs from cats in images using Keras and Tensorflow","metadata":{}},{"cell_type":"markdown","source":"<a id=\"toc\"></a>\n<h2> Table of Contents </h2>\n<div class = \"alert alert-block alert-info\">\n    <ol>\n        <li><a href=\"#import\"> Importing libraries </a></li>\n        <li><a href=\"#download\"> Downloading data </a></li>        \n        <li><a href=\"#analyse\"> Analyse the data </a></li>        \n        <li><a href=\"#model\"> Modeling </a></li>\n        <li><a href=\"#reference\"> Reference </a></li>        \n    </ol>\n</div>","metadata":{}},{"cell_type":"markdown","source":"<a id=\"import\"></a>\n<h2> Importing libraries </h2>","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd \nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nimport seaborn as sns\n%matplotlib inline\n\nimport os\nimport zipfile\nfrom tqdm import tqdm\n\n#from PIL import Image\n\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-03-23T07:49:47.431043Z","iopub.execute_input":"2022-03-23T07:49:47.431675Z","iopub.status.idle":"2022-03-23T07:49:53.279853Z","shell.execute_reply.started":"2022-03-23T07:49:47.431555Z","shell.execute_reply":"2022-03-23T07:49:53.279071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a href=\"#toc\"> Back to top </a>\n<hr>","metadata":{}},{"cell_type":"markdown","source":"<a id=\"download\"></a>\n<h2> Downloading data </h2>","metadata":{}},{"cell_type":"markdown","source":"We have the option of using either `os.walk()` or `os.listdir()` to get all the files or images. In this project, we will use `os.lisdir()` ","metadata":{}},{"cell_type":"code","source":"#help(os.walk)\n# help(os.listdir)","metadata":{"execution":{"iopub.status.busy":"2022-03-23T07:49:53.283277Z","iopub.execute_input":"2022-03-23T07:49:53.283475Z","iopub.status.idle":"2022-03-23T07:49:53.291037Z","shell.execute_reply.started":"2022-03-23T07:49:53.283452Z","shell.execute_reply":"2022-03-23T07:49:53.289547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(os.listdir('../'))\nprint(os.listdir('../input'))\nprint(os.listdir('../input/dogs-vs-cats'))","metadata":{"execution":{"iopub.status.busy":"2022-03-23T07:49:53.292337Z","iopub.execute_input":"2022-03-23T07:49:53.292581Z","iopub.status.idle":"2022-03-23T07:49:53.305498Z","shell.execute_reply.started":"2022-03-23T07:49:53.292547Z","shell.execute_reply":"2022-03-23T07:49:53.304772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_path = '/kaggle/input/dogs-vs-cats/'\nos.listdir(input_path)","metadata":{"execution":{"iopub.status.busy":"2022-03-23T07:49:53.308421Z","iopub.execute_input":"2022-03-23T07:49:53.308924Z","iopub.status.idle":"2022-03-23T07:49:53.317427Z","shell.execute_reply.started":"2022-03-23T07:49:53.308896Z","shell.execute_reply":"2022-03-23T07:49:53.316692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_zip = input_path + os.listdir(input_path)[0]\ntrain_zip = input_path + os.listdir(input_path)[1]\nsample_sub_path = input_path + os.listdir(input_path)[2]\n# sanity check\n#print(train_zip)\n#print(test_zip)","metadata":{"execution":{"iopub.status.busy":"2022-03-23T07:49:53.318400Z","iopub.execute_input":"2022-03-23T07:49:53.318708Z","iopub.status.idle":"2022-03-23T07:49:53.324898Z","shell.execute_reply.started":"2022-03-23T07:49:53.318675Z","shell.execute_reply":"2022-03-23T07:49:53.323836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Taking a look at the format of submission required. ","metadata":{}},{"cell_type":"code","source":"sample_sub = pd.read_csv(sample_sub_path)\nsample_sub.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-23T07:49:53.326416Z","iopub.execute_input":"2022-03-23T07:49:53.326662Z","iopub.status.idle":"2022-03-23T07:49:53.354007Z","shell.execute_reply.started":"2022-03-23T07:49:53.326631Z","shell.execute_reply":"2022-03-23T07:49:53.353226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The images are contained in zip files and so we will need to use `zipfile.ZipFile()` to extract the images.","metadata":{}},{"cell_type":"code","source":"# help(zipfile.ZipFile)","metadata":{"execution":{"iopub.status.busy":"2022-03-23T07:49:53.355283Z","iopub.execute_input":"2022-03-23T07:49:53.355529Z","iopub.status.idle":"2022-03-23T07:49:53.359003Z","shell.execute_reply.started":"2022-03-23T07:49:53.355496Z","shell.execute_reply":"2022-03-23T07:49:53.358316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nimage_path = '/kaggle/temp/'\nwith zipfile.ZipFile(test_zip, 'r') as myzip:\n    myzip.extractall(image_path)\n    \nwith zipfile.ZipFile(train_zip, 'r') as myzip:\n    myzip.extractall(image_path)","metadata":{"execution":{"iopub.status.busy":"2022-03-23T07:49:53.360300Z","iopub.execute_input":"2022-03-23T07:49:53.360710Z","iopub.status.idle":"2022-03-23T07:50:10.079464Z","shell.execute_reply.started":"2022-03-23T07:49:53.360677Z","shell.execute_reply":"2022-03-23T07:50:10.078706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = image_path + 'train/'\nprint(train_path)\nd = {'image': os.listdir(train_path)}\ntrain_df = pd.DataFrame(data=d)\ntrain_df['label'] = train_df['image'].apply(lambda x: x.split('.')[0])\ntrain_df['path'] = train_df['image'].apply(lambda x: train_path+x)\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-23T08:06:01.538573Z","iopub.execute_input":"2022-03-23T08:06:01.538846Z","iopub.status.idle":"2022-03-23T08:06:01.587629Z","shell.execute_reply.started":"2022-03-23T08:06:01.538816Z","shell.execute_reply":"2022-03-23T08:06:01.586828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_path = image_path + 'test1/'\nprint(test_path)\nd = {'image': os.listdir(test_path)}\ntest_df = pd.DataFrame(data=d)\ntest_df['label'] = None\ntest_df['path'] = test_df['image'].apply(lambda x: test_path+x)\ntest_df['id'] = test_df['image'].apply(lambda x: int(x.split('.')[0]))\n#test_df.sort_values(by='id', inplace=True)\ntest_df.set_index('id', inplace=True)\ntest_df.sort_index(inplace=True)\n#test_df.reset_index(inplace=True)\ntest_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-23T08:05:24.295984Z","iopub.execute_input":"2022-03-23T08:05:24.296873Z","iopub.status.idle":"2022-03-23T08:05:24.338086Z","shell.execute_reply.started":"2022-03-23T08:05:24.296829Z","shell.execute_reply":"2022-03-23T08:05:24.337342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a href=\"#toc\"> Back to top </a>","metadata":{}},{"cell_type":"markdown","source":"<a id=\"analyse\"></a>\n<h2> Analysing the data </h2>","metadata":{}},{"cell_type":"code","source":"print(f'Number of training images: {len(train_df)}')\nprint(f'Number of test images: {len(test_df)}')","metadata":{"execution":{"iopub.status.busy":"2022-03-23T07:50:18.933373Z","iopub.execute_input":"2022-03-23T07:50:18.933651Z","iopub.status.idle":"2022-03-23T07:50:18.939077Z","shell.execute_reply.started":"2022-03-23T07:50:18.933620Z","shell.execute_reply":"2022-03-23T07:50:18.938219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(x='label', data=train_df)\nplt.title('Number of training images of dogs and cats')\nplt.show()\n\nprint(train_df['label'].value_counts())","metadata":{"execution":{"iopub.status.busy":"2022-03-23T07:50:21.768055Z","iopub.execute_input":"2022-03-23T07:50:21.768837Z","iopub.status.idle":"2022-03-23T07:50:21.977314Z","shell.execute_reply.started":"2022-03-23T07:50:21.768791Z","shell.execute_reply":"2022-03-23T07:50:21.976208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We will print out some images for quick check","metadata":{}},{"cell_type":"code","source":"def print_images(n, df, ncols=5):\n    '''\n    helper function to print images and labels\n    n: number of images to print\n    df: dataframe from which image is chosen\n    '''\n    nrows = int(np.ceil(n / ncols))\n\n    plt.figure(figsize=(10, 6))\n    for i in range(n):\n        img_sample = df.sample()\n        img_name = img_sample.iloc[0].image\n        img_path = img_sample.iloc[0].path\n        img_label = img_sample.iloc[0].label\n        img = mpimg.imread(img_path)\n        ax = plt.subplot(nrows, ncols, i+1)\n        ax.imshow(img)\n        ax.set_title(f'{img_name}\\n, label: {img_label}')\n    plt.tight_layout()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-23T09:24:49.216732Z","iopub.execute_input":"2022-03-23T09:24:49.217438Z","iopub.status.idle":"2022-03-23T09:24:49.223923Z","shell.execute_reply.started":"2022-03-23T09:24:49.217400Z","shell.execute_reply":"2022-03-23T09:24:49.223077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print_images(10, train_df)","metadata":{"execution":{"iopub.status.busy":"2022-03-23T09:24:49.969858Z","iopub.execute_input":"2022-03-23T09:24:49.970145Z","iopub.status.idle":"2022-03-23T09:24:51.344246Z","shell.execute_reply.started":"2022-03-23T09:24:49.970112Z","shell.execute_reply":"2022-03-23T09:24:51.343633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print_images(10, test_df)","metadata":{"execution":{"iopub.status.busy":"2022-03-23T09:24:54.036611Z","iopub.execute_input":"2022-03-23T09:24:54.037195Z","iopub.status.idle":"2022-03-23T09:24:55.569250Z","shell.execute_reply.started":"2022-03-23T09:24:54.037152Z","shell.execute_reply":"2022-03-23T09:24:55.568620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The images are of different shapes and sizes. We will do a simple visualisation of the minimum, maximum as well as the distribution of the sizes. ","metadata":{}},{"cell_type":"code","source":"train_df['shape'] = train_df['path'].apply(lambda x: mpimg.imread(x).shape)\ntrain_df['size'] = train_df['shape'].apply(lambda x: x[0]*x[1]*x[2])\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-23T08:41:30.613991Z","iopub.execute_input":"2022-03-23T08:41:30.614534Z","iopub.status.idle":"2022-03-23T08:42:27.474884Z","shell.execute_reply.started":"2022-03-23T08:41:30.614489Z","shell.execute_reply":"2022-03-23T08:42:27.474072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df['shape'] = test_df['path'].apply(lambda x: mpimg.imread(x).shape)\ntest_df['size'] = test_df['shape'].apply(lambda x: x[0]*x[1]*x[2])\ntest_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-23T08:42:27.476592Z","iopub.execute_input":"2022-03-23T08:42:27.476870Z","iopub.status.idle":"2022-03-23T08:42:56.163279Z","shell.execute_reply.started":"2022-03-23T08:42:27.476833Z","shell.execute_reply":"2022-03-23T08:42:56.162446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"temp_img_df = train_df[train_df['size'] == train_df['size'].min()]\nprint(temp_img_df)\nprint_images(1, temp_img_df)\nprint('-' * 80)\ntemp_img_df = train_df[train_df['size'] == train_df['size'].max()]\nprint(temp_img_df)\nprint_images(1, temp_img_df)","metadata":{"execution":{"iopub.status.busy":"2022-03-23T09:25:01.804622Z","iopub.execute_input":"2022-03-23T09:25:01.804939Z","iopub.status.idle":"2022-03-23T09:25:02.273395Z","shell.execute_reply.started":"2022-03-23T09:25:01.804904Z","shell.execute_reply":"2022-03-23T09:25:02.272634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"temp_img_df = test_df[test_df['size'] == test_df['size'].min()]\nprint(temp_img_df)\nprint_images(1, temp_img_df)\nprint('-'*20)\ntemp_img_df = test_df[test_df['size'] == test_df['size'].max()]\nprint(temp_img_df)\nprint_images(1, temp_img_df)\n","metadata":{"execution":{"iopub.status.busy":"2022-03-23T09:25:38.452640Z","iopub.execute_input":"2022-03-23T09:25:38.453207Z","iopub.status.idle":"2022-03-23T09:25:38.836014Z","shell.execute_reply.started":"2022-03-23T09:25:38.453167Z","shell.execute_reply":"2022-03-23T09:25:38.835375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(1, 2, figsize=(8, 4))\nsns.histplot(x='size', data=train_df, bins=20, ax=ax[0])\nsns.histplot(x='size', data=test_df, bins=20, ax=ax[1])\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-23T09:30:48.637404Z","iopub.execute_input":"2022-03-23T09:30:48.637659Z","iopub.status.idle":"2022-03-23T09:30:49.046097Z","shell.execute_reply.started":"2022-03-23T09:30:48.637630Z","shell.execute_reply":"2022-03-23T09:30:49.045448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id='housekeeping'></a>\n<h2>Housekeeping</h2>","metadata":{}},{"cell_type":"markdown","source":"In order to use keras' data generator, we will need to move the files into respective labeled folders. This is to achieve an end-to-end model. The moving of images can be achieved by different methods, `os.rename()`, `os.replace()` and `shutil.move()`. We will be using `os.rename()`","metadata":{}},{"cell_type":"code","source":"dog_df = train_df[train_df.label=='dog']\ncat_df = train_df[train_df.label=='cat']","metadata":{"execution":{"iopub.status.busy":"2022-03-23T09:33:55.022899Z","iopub.execute_input":"2022-03-23T09:33:55.023188Z","iopub.status.idle":"2022-03-23T09:33:55.041427Z","shell.execute_reply.started":"2022-03-23T09:33:55.023156Z","shell.execute_reply":"2022-03-23T09:33:55.040740Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dog_path = train_path + 'dog/'\ncat_path = train_path + 'cat/'\n\nos.mkdir(dog_path)\nos.mkdir(cat_path)\n\nfor name in dog_df.image:\n    os.rename((train_path+name), (dog_path+name))\n    \nfor name in cat_df.image:\n    os.rename((train_path+name), (cat_path+name))","metadata":{"execution":{"iopub.status.busy":"2022-03-23T09:33:56.420411Z","iopub.execute_input":"2022-03-23T09:33:56.420675Z","iopub.status.idle":"2022-03-23T09:33:57.015618Z","shell.execute_reply.started":"2022-03-23T09:33:56.420647Z","shell.execute_reply":"2022-03-23T09:33:57.014766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a href='#toc'> Back to Top </a>","metadata":{}},{"cell_type":"markdown","source":"<a id='model'></a>\n<h2> Modeling with Keras </h2>","metadata":{}},{"cell_type":"code","source":"image_size = (128, 128)\nbatch_size = 32\nseed = 2\n\ntrain_ds = keras.preprocessing.image_dataset_from_directory(\n    train_path,\n    batch_size=32,\n    image_size=image_size,\n    seed=seed,\n    validation_split=.2,\n    subset='training'\n)\n\nval_ds = keras.preprocessing.image_dataset_from_directory(\n    train_path,\n    batch_size=32,\n    image_size=image_size,\n    seed=seed,\n    validation_split=.2,\n    subset='validation'\n)","metadata":{"execution":{"iopub.status.busy":"2022-03-23T09:34:19.594892Z","iopub.execute_input":"2022-03-23T09:34:19.595549Z","iopub.status.idle":"2022-03-23T09:34:23.519068Z","shell.execute_reply.started":"2022-03-23T09:34:19.595511Z","shell.execute_reply":"2022-03-23T09:34:23.518308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#help(train_ds.take)","metadata":{"execution":{"iopub.status.busy":"2022-03-16T08:59:21.124428Z","iopub.execute_input":"2022-03-16T08:59:21.125337Z","iopub.status.idle":"2022-03-16T08:59:21.129237Z","shell.execute_reply.started":"2022-03-16T08:59:21.125292Z","shell.execute_reply":"2022-03-16T08:59:21.128309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"`train_ds.take(count)` creates a `Dataset` with at most `count` elements (or batch if you will). We can try running the code below with `range(33)` and it will return an error because each `batch_size` has maximum 32 images","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(12, 12))\n\nfor images, labels in train_ds.take(1):\n    for i in range(32):\n        ax = plt.subplot(7, 5, i + 1)\n        plt.imshow(images[i].numpy().astype(\"uint8\"))\n        plt.title(int(labels[i]))\n        plt.axis(\"off\")\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-23T09:34:52.645502Z","iopub.execute_input":"2022-03-23T09:34:52.645761Z","iopub.status.idle":"2022-03-23T09:34:55.227161Z","shell.execute_reply.started":"2022-03-23T09:34:52.645731Z","shell.execute_reply":"2022-03-23T09:34:55.226362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Creating a data augmentation layer. We will have 2 basic layers in the augmentation layer `RandomFlip()` and `RandomRotation()`.","metadata":{}},{"cell_type":"code","source":"#help(layers.RandomFlip)\n#help(layers.RandomRotation)","metadata":{"execution":{"iopub.status.busy":"2022-03-16T08:59:23.713829Z","iopub.execute_input":"2022-03-16T08:59:23.714745Z","iopub.status.idle":"2022-03-16T08:59:23.72081Z","shell.execute_reply.started":"2022-03-16T08:59:23.714706Z","shell.execute_reply":"2022-03-16T08:59:23.719428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# data_augmentation\ndata_augmentation = keras.Sequential(\n    [layers.RandomFlip(\"horizontal\"),\n     layers.RandomRotation(0.1)]\n)","metadata":{"execution":{"iopub.status.busy":"2022-03-23T09:35:18.693512Z","iopub.execute_input":"2022-03-23T09:35:18.693770Z","iopub.status.idle":"2022-03-23T09:35:18.706756Z","shell.execute_reply.started":"2022-03-23T09:35:18.693742Z","shell.execute_reply":"2022-03-23T09:35:18.706014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10, 10))\n\nfor images, _ in train_ds.take(1):\n    for i in range(9):\n        augmented_images = data_augmentation(images)\n        ax = plt.subplot(3, 3, i + 1)\n        plt.imshow(augmented_images[0].numpy().astype(\"uint8\"))\n        plt.axis(\"off\")","metadata":{"execution":{"iopub.status.busy":"2022-03-23T09:35:31.057235Z","iopub.execute_input":"2022-03-23T09:35:31.057706Z","iopub.status.idle":"2022-03-23T09:35:32.415182Z","shell.execute_reply.started":"2022-03-23T09:35:31.057668Z","shell.execute_reply":"2022-03-23T09:35:32.414497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Our images have RGB channel values in the range [0, 255]. We will have standardise them to the range [0, 1]. ","metadata":{}},{"cell_type":"markdown","source":"<h2>Building the model</h2>","metadata":{"execution":{"iopub.status.busy":"2021-12-16T02:35:25.070029Z","iopub.execute_input":"2021-12-16T02:35:25.070373Z","iopub.status.idle":"2021-12-16T02:35:25.077344Z","shell.execute_reply.started":"2021-12-16T02:35:25.070331Z","shell.execute_reply":"2021-12-16T02:35:25.076132Z"}}},{"cell_type":"code","source":"model = keras.Sequential()\nmodel.add(keras.Input(shape=image_size+(3,)))\nmodel.add(data_augmentation)\nmodel.add(layers.Rescaling(1.0/255))\nmodel.add(layers.Conv2D(32, 3, activation='relu'))\nmodel.add(layers.MaxPool2D(pool_size=2))\nmodel.add(layers.Conv2D(64, 3, activation='relu'))\nmodel.add(layers.MaxPool2D(pool_size=2))\nmodel.add(layers.Conv2D(64, 3, activation='relu'))\nmodel.add(layers.MaxPool2D(pool_size=2))\nmodel.add(layers.Conv2D(128, 3, activation='relu'))\nmodel.add(layers.MaxPool2D(pool_size=2))\nmodel.add(layers.Flatten())\nmodel.add(layers.Dense(512, activation='relu'))\nmodel.add(layers.Dropout(.5))\nmodel.add(layers.Dense(1, activation='sigmoid'))\n#model.add(layers.Flatten())","metadata":{"execution":{"iopub.status.busy":"2022-03-23T09:35:41.572096Z","iopub.execute_input":"2022-03-23T09:35:41.572348Z","iopub.status.idle":"2022-03-23T09:35:41.784869Z","shell.execute_reply.started":"2022-03-23T09:35:41.572321Z","shell.execute_reply":"2022-03-23T09:35:41.784162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.output","metadata":{"execution":{"iopub.status.busy":"2022-03-23T09:35:48.343670Z","iopub.execute_input":"2022-03-23T09:35:48.344362Z","iopub.status.idle":"2022-03-23T09:35:48.349303Z","shell.execute_reply.started":"2022-03-23T09:35:48.344325Z","shell.execute_reply":"2022-03-23T09:35:48.348661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-03-23T09:36:11.010907Z","iopub.execute_input":"2022-03-23T09:36:11.011186Z","iopub.status.idle":"2022-03-23T09:36:11.026344Z","shell.execute_reply.started":"2022-03-23T09:36:11.011155Z","shell.execute_reply":"2022-03-23T09:36:11.025603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 50\n\ncallbacks = [\n    keras.callbacks.ModelCheckpoint(\"save_at_{epoch}.h5\"),\n]\n\nmodel.compile(\n    optimizer='adam',\n    loss=\"binary_crossentropy\",\n    metrics=[\"accuracy\"],\n)\n\nhist = model.fit(\n    train_ds, epochs=epochs, validation_data=val_ds, verbose=1\n)","metadata":{"execution":{"iopub.status.busy":"2022-03-23T09:36:15.881725Z","iopub.execute_input":"2022-03-23T09:36:15.882309Z","iopub.status.idle":"2022-03-23T10:03:16.198732Z","shell.execute_reply.started":"2022-03-23T09:36:15.882269Z","shell.execute_reply":"2022-03-23T10:03:16.197984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fit_df = pd.DataFrame.from_dict(hist.history)\nfit_df['epoch'] = fit_df.index + 1\nfit_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-23T10:23:48.697018Z","iopub.execute_input":"2022-03-23T10:23:48.697506Z","iopub.status.idle":"2022-03-23T10:23:48.713435Z","shell.execute_reply.started":"2022-03-23T10:23:48.697470Z","shell.execute_reply":"2022-03-23T10:23:48.710851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12, 5))\nplt.subplot(121)\nsns.lineplot(x='epoch', y='loss', data=fit_df)\nsns.lineplot(x='epoch', y='val_loss', data=fit_df)\nplt.legend(['loss', 'val_loss'])\n\nplt.subplot(122)\nsns.lineplot(x='epoch', y='accuracy', data=fit_df)\nsns.lineplot(x='epoch', y='val_accuracy', data=fit_df)\nplt.legend(['accuracy', 'val_accuracy'])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-23T10:23:56.215638Z","iopub.execute_input":"2022-03-23T10:23:56.216332Z","iopub.status.idle":"2022-03-23T10:23:56.617373Z","shell.execute_reply.started":"2022-03-23T10:23:56.216280Z","shell.execute_reply":"2022-03-23T10:23:56.616632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a href=\"#toc\"> Back to top </a>","metadata":{}},{"cell_type":"markdown","source":"<h2>Predictions</h2>","metadata":{}},{"cell_type":"code","source":"test_path","metadata":{"execution":{"iopub.status.busy":"2022-03-23T10:26:22.358767Z","iopub.execute_input":"2022-03-23T10:26:22.359067Z","iopub.status.idle":"2022-03-23T10:26:22.364424Z","shell.execute_reply.started":"2022-03-23T10:26:22.359035Z","shell.execute_reply":"2022-03-23T10:26:22.363796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_ds = keras.preprocessing.image_dataset_from_directory(\n    test_path,\n    label_mode=None,\n    image_size=image_size,\n)","metadata":{"execution":{"iopub.status.busy":"2022-03-23T10:26:30.435207Z","iopub.execute_input":"2022-03-23T10:26:30.435460Z","iopub.status.idle":"2022-03-23T10:26:30.819910Z","shell.execute_reply.started":"2022-03-23T10:26:30.435433Z","shell.execute_reply":"2022-03-23T10:26:30.818690Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(test_ds)","metadata":{"execution":{"iopub.status.busy":"2022-03-23T10:26:33.478858Z","iopub.execute_input":"2022-03-23T10:26:33.479114Z","iopub.status.idle":"2022-03-23T10:26:43.449704Z","shell.execute_reply.started":"2022-03-23T10:26:33.479086Z","shell.execute_reply":"2022-03-23T10:26:43.448986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = np.array([1 if x>=0.5 else 0 for x in predictions])","metadata":{"execution":{"iopub.status.busy":"2022-03-23T10:34:15.679537Z","iopub.execute_input":"2022-03-23T10:34:15.679890Z","iopub.status.idle":"2022-03-23T10:34:15.709751Z","shell.execute_reply.started":"2022-03-23T10:34:15.679854Z","shell.execute_reply":"2022-03-23T10:34:15.708840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.histplot(predictions)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-23T10:30:56.631098Z","iopub.execute_input":"2022-03-23T10:30:56.631361Z","iopub.status.idle":"2022-03-23T10:30:56.859352Z","shell.execute_reply.started":"2022-03-23T10:30:56.631333Z","shell.execute_reply":"2022-03-23T10:30:56.858628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df['label'] = pred\ntest_df","metadata":{"execution":{"iopub.status.busy":"2022-03-23T10:34:20.064123Z","iopub.execute_input":"2022-03-23T10:34:20.064419Z","iopub.status.idle":"2022-03-23T10:34:20.086286Z","shell.execute_reply.started":"2022-03-23T10:34:20.064385Z","shell.execute_reply":"2022-03-23T10:34:20.085599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df['label'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-03-23T10:34:27.279139Z","iopub.execute_input":"2022-03-23T10:34:27.279588Z","iopub.status.idle":"2022-03-23T10:34:27.286108Z","shell.execute_reply.started":"2022-03-23T10:34:27.279551Z","shell.execute_reply":"2022-03-23T10:34:27.285135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print_images(20, test_df)","metadata":{"execution":{"iopub.status.busy":"2022-03-23T10:39:51.332706Z","iopub.execute_input":"2022-03-23T10:39:51.333119Z","iopub.status.idle":"2022-03-23T10:39:53.356849Z","shell.execute_reply.started":"2022-03-23T10:39:51.333087Z","shell.execute_reply":"2022-03-23T10:39:53.356142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a href=\"#toc\"> Back to top </a>","metadata":{}},{"cell_type":"markdown","source":"<a id=\"reference\"></a>\n<h2> Reference </h2>","metadata":{}},{"cell_type":"markdown","source":"1. [Understanding os.walk](https://docs.python.org/3/library/os.html)\n2. [Understanding zipfile.ZipFile](https://docs.python.org/3/library/zipfile.html)\n3. [Introduction to Keras for Engineers](https://keras.io/getting_started/intro_to_keras_for_engineers/)\n4. [Image classification from scratch](https://keras.io/examples/vision/image_classification_from_scratch/)\n5. [How to use Kaggle TPU](https://www.kaggle.com/docs/tpu)","metadata":{}},{"cell_type":"markdown","source":"<a href=\"#toc\"> Back to top </a>","metadata":{"execution":{"iopub.status.busy":"2021-12-15T03:33:41.977898Z","iopub.execute_input":"2021-12-15T03:33:41.978232Z","iopub.status.idle":"2021-12-15T03:33:41.984363Z","shell.execute_reply.started":"2021-12-15T03:33:41.978199Z","shell.execute_reply":"2021-12-15T03:33:41.983422Z"}}}]}