{"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-11-06T12:59:07.098627Z","iopub.execute_input":"2023-11-06T12:59:07.099536Z","iopub.status.idle":"2023-11-06T12:59:07.554548Z","shell.execute_reply.started":"2023-11-06T12:59:07.099497Z","shell.execute_reply":"2023-11-06T12:59:07.55322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow import keras\nfrom tensorflow.keras import layers\ninputs = keras.Input(shape=(28, 28, 1))\nx = layers.Conv2D(filters=32, kernel_size=3, activation=\"relu\")(inputs)\nx = layers.MaxPooling2D(pool_size=2)(x)\nx = layers.Conv2D(filters=64, kernel_size=3, activation=\"relu\")(x)\nx = layers.MaxPooling2D(pool_size=2)(x)\nx = layers.Conv2D(filters=128, kernel_size=3, activation=\"relu\")(x)\nx = layers.Flatten()(x)\noutputs = layers.Dense(10, activation=\"softmax\")(x)\nmodel = keras.Model(inputs=inputs, outputs=outputs)","metadata":{"execution":{"iopub.status.busy":"2023-11-06T13:07:53.485699Z","iopub.execute_input":"2023-11-06T13:07:53.486078Z","iopub.status.idle":"2023-11-06T13:07:53.546337Z","shell.execute_reply.started":"2023-11-06T13:07:53.486049Z","shell.execute_reply":"2023-11-06T13:07:53.545583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inputs = keras.Input(shape=(28, 28, 1))\nx = layers.Conv2D(filters=32, kernel_size=3, activation=\"relu\")(inputs)\nx = layers.Conv2D(filters=64, kernel_size=3, activation=\"relu\")(x)\nx = layers.Conv2D(filters=128, kernel_size=3, activation=\"relu\")(x)\nx = layers.Flatten()(x)\noutputs = layers.Dense(10, activation=\"softmax\")(x)\nmodel_no_max_pool = keras.Model(inputs=inputs, outputs=outputs)","metadata":{"execution":{"iopub.status.busy":"2023-11-06T13:08:03.656595Z","iopub.execute_input":"2023-11-06T13:08:03.657248Z","iopub.status.idle":"2023-11-06T13:08:03.711542Z","shell.execute_reply.started":"2023-11-06T13:08:03.657218Z","shell.execute_reply":"2023-11-06T13:08:03.710773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-11-06T13:08:11.391211Z","iopub.execute_input":"2023-11-06T13:08:11.391975Z","iopub.status.idle":"2023-11-06T13:08:11.421214Z","shell.execute_reply.started":"2023-11-06T13:08:11.391944Z","shell.execute_reply":"2023-11-06T13:08:11.419931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!unzip -qq /kaggle/input/dogs-vs-cats-redux-kernels-edition/train.zip\n","metadata":{"execution":{"iopub.status.busy":"2023-11-06T13:09:17.508157Z","iopub.execute_input":"2023-11-06T13:09:17.509111Z","iopub.status.idle":"2023-11-06T13:09:29.845411Z","shell.execute_reply.started":"2023-11-06T13:09:17.50908Z","shell.execute_reply":"2023-11-06T13:09:29.844315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os, shutil, pathlib\n\noriginal_dir = pathlib.Path(\"/kaggle/working/train\")\nnew_base_dir = pathlib.Path(\"/kaggle/working/cats_vs_dogs_small\")\n\ndef make_subset(subset_name, start_index, end_index):\n    for category in (\"cat\", \"dog\"):\n        dir = new_base_dir / subset_name / category\n        os.makedirs(dir)\n        fnames = [f\"{category}.{i}.jpg\" for i in range(start_index, end_index)]\n        for fname in fnames:\n            shutil.copyfile(src=original_dir / fname,\n                            dst=dir / fname)\n\nmake_subset(\"train\", start_index=0, end_index=1000)\nmake_subset(\"validation\", start_index=1000, end_index=1500)\nmake_subset(\"test\", start_index=1500, end_index=2500)","metadata":{"execution":{"iopub.status.busy":"2023-11-06T13:13:32.282632Z","iopub.execute_input":"2023-11-06T13:13:32.283013Z","iopub.status.idle":"2023-11-06T13:13:32.910361Z","shell.execute_reply.started":"2023-11-06T13:13:32.282979Z","shell.execute_reply":"2023-11-06T13:13:32.909532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow import keras\nfrom tensorflow.keras import layers\n\ninputs = keras.Input(shape=(180, 180, 3))\nx = layers.Rescaling(1./255)(inputs)\nx = layers.Conv2D(filters=32, kernel_size=3, activation=\"relu\")(x)\nx = layers.MaxPooling2D(pool_size=2)(x)\nx = layers.Conv2D(filters=64, kernel_size=3, activation=\"relu\")(x)\nx = layers.MaxPooling2D(pool_size=2)(x)\nx = layers.Conv2D(filters=128, kernel_size=3, activation=\"relu\")(x)\nx = layers.MaxPooling2D(pool_size=2)(x)\nx = layers.Conv2D(filters=256, kernel_size=3, activation=\"relu\")(x)\nx = layers.MaxPooling2D(pool_size=2)(x)\nx = layers.Conv2D(filters=256, kernel_size=3, activation=\"relu\")(x)\nx = layers.Flatten()(x)\noutputs = layers.Dense(1, activation=\"sigmoid\")(x)\nmodel = keras.Model(inputs=inputs, outputs=outputs)","metadata":{"execution":{"iopub.status.busy":"2023-11-06T13:14:18.965829Z","iopub.execute_input":"2023-11-06T13:14:18.966195Z","iopub.status.idle":"2023-11-06T13:14:19.079801Z","shell.execute_reply.started":"2023-11-06T13:14:18.96617Z","shell.execute_reply":"2023-11-06T13:14:19.078976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-11-06T13:14:35.727274Z","iopub.execute_input":"2023-11-06T13:14:35.727656Z","iopub.status.idle":"2023-11-06T13:14:35.766999Z","shell.execute_reply.started":"2023-11-06T13:14:35.727619Z","shell.execute_reply":"2023-11-06T13:14:35.766051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss=\"binary_crossentropy\",\n              optimizer=\"rmsprop\",\n              metrics=[\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2023-11-06T13:15:01.388051Z","iopub.execute_input":"2023-11-06T13:15:01.388408Z","iopub.status.idle":"2023-11-06T13:15:01.405935Z","shell.execute_reply.started":"2023-11-06T13:15:01.388381Z","shell.execute_reply":"2023-11-06T13:15:01.404993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.utils import image_dataset_from_directory\n\ntrain_dataset = image_dataset_from_directory(\n    new_base_dir / \"train\",\n    image_size=(180, 180),\n    batch_size=32)\nvalidation_dataset = image_dataset_from_directory(\n    new_base_dir / \"validation\",\n    image_size=(180, 180),\n    batch_size=32)\ntest_dataset = image_dataset_from_directory(\n    new_base_dir / \"test\",\n    image_size=(180, 180),\n    batch_size=32)","metadata":{"execution":{"iopub.status.busy":"2023-11-06T13:15:47.164301Z","iopub.execute_input":"2023-11-06T13:15:47.164689Z","iopub.status.idle":"2023-11-06T13:15:47.581424Z","shell.execute_reply.started":"2023-11-06T13:15:47.164659Z","shell.execute_reply":"2023-11-06T13:15:47.580561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport tensorflow as tf\nrandom_numbers = np.random.normal(size=(1000, 16))\ndataset = tf.data.Dataset.from_tensor_slices(random_numbers)","metadata":{"execution":{"iopub.status.busy":"2023-11-06T13:16:17.643156Z","iopub.execute_input":"2023-11-06T13:16:17.644191Z","iopub.status.idle":"2023-11-06T13:16:17.653215Z","shell.execute_reply.started":"2023-11-06T13:16:17.644142Z","shell.execute_reply":"2023-11-06T13:16:17.652409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i, element in enumerate(dataset):\n    print(element.shape)\n    if i >= 2:\n        break","metadata":{"execution":{"iopub.status.busy":"2023-11-06T13:16:29.915306Z","iopub.execute_input":"2023-11-06T13:16:29.915684Z","iopub.status.idle":"2023-11-06T13:16:29.97601Z","shell.execute_reply.started":"2023-11-06T13:16:29.915657Z","shell.execute_reply":"2023-11-06T13:16:29.974991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"atched_dataset = dataset.batch(32)\nfor i, element in enumerate(batched_dataset):\n    print(element.shape)\n    if i >= 2:\n        break","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"reshaped_dataset = dataset.map(lambda x: tf.reshape(x, (4, 4)))\nfor i, element in enumerate(reshaped_dataset):\n    print(element.shape)\n    if i >= 2:\n        break","metadata":{"execution":{"iopub.status.busy":"2023-11-06T13:17:09.674557Z","iopub.execute_input":"2023-11-06T13:17:09.674929Z","iopub.status.idle":"2023-11-06T13:17:09.739477Z","shell.execute_reply.started":"2023-11-06T13:17:09.674901Z","shell.execute_reply":"2023-11-06T13:17:09.73836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for data_batch, labels_batch in train_dataset:\n    print(\"data batch shape:\", data_batch.shape)\n    print(\"labels batch shape:\", labels_batch.shape)\n    break","metadata":{"execution":{"iopub.status.busy":"2023-11-06T13:17:26.522427Z","iopub.execute_input":"2023-11-06T13:17:26.523195Z","iopub.status.idle":"2023-11-06T13:17:26.743301Z","shell.execute_reply.started":"2023-11-06T13:17:26.523152Z","shell.execute_reply":"2023-11-06T13:17:26.742291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"callbacks = [\n    keras.callbacks.ModelCheckpoint(\n        filepath=\"convnet_from_scratch.keras\",\n        save_best_only=True,\n        monitor=\"val_loss\")\n]\nhistory = model.fit(\n    train_dataset,\n    epochs=30,\n    validation_data=validation_dataset,\n    callbacks=callbacks)","metadata":{"execution":{"iopub.status.busy":"2023-11-06T13:17:45.866043Z","iopub.execute_input":"2023-11-06T13:17:45.866896Z","iopub.status.idle":"2023-11-06T13:19:26.998518Z","shell.execute_reply.started":"2023-11-06T13:17:45.866861Z","shell.execute_reply":"2023-11-06T13:19:26.99769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\naccuracy = history.history[\"accuracy\"]\nval_accuracy = history.history[\"val_accuracy\"]\nloss = history.history[\"loss\"]\nval_loss = history.history[\"val_loss\"]\nepochs = range(1, len(accuracy) + 1)\nplt.plot(epochs, accuracy, \"bo\", label=\"Training accuracy\")\nplt.plot(epochs, val_accuracy, \"b\", label=\"Validation accuracy\")\nplt.title(\"Training and validation accuracy\")\nplt.legend()\nplt.figure()\nplt.plot(epochs, loss, \"bo\", label=\"Training loss\")\nplt.plot(epochs, val_loss, \"b\", label=\"Validation loss\")\nplt.title(\"Training and validation loss\")\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-06T13:19:36.049574Z","iopub.execute_input":"2023-11-06T13:19:36.050402Z","iopub.status.idle":"2023-11-06T13:19:36.640257Z","shell.execute_reply.started":"2023-11-06T13:19:36.050366Z","shell.execute_reply":"2023-11-06T13:19:36.639296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}