{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"tpu1vmV38","dataSources":[{"sourceId":3362,"databundleVersionId":31148,"sourceType":"competition"}],"dockerImageVersionId":30626,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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":"2024-01-08T19:55:19.846025Z","iopub.execute_input":"2024-01-08T19:55:19.846805Z","iopub.status.idle":"2024-01-08T19:55:20.209541Z","shell.execute_reply.started":"2024-01-08T19:55:19.846766Z","shell.execute_reply":"2024-01-08T19:55:20.208848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os \nfrom tensorflow.keras.optimizers import RMSprop\nimport zipfile\nimport random\nimport shutil\nimport tensorflow as tf\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom shutil import copyfile\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2024-01-08T19:55:20.210882Z","iopub.execute_input":"2024-01-08T19:55:20.211175Z","iopub.status.idle":"2024-01-08T19:55:33.536039Z","shell.execute_reply.started":"2024-01-08T19:55:20.211149Z","shell.execute_reply":"2024-01-08T19:55:33.535152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_local_zip = '/kaggle/input/dogs-vs-cats/train.zip'\ntest_local_zip = '/kaggle/input/dogs-vs-cats/test1.zip'\n\ntrain_zip_ref = zipfile.ZipFile(train_local_zip, 'r')\ntest_zip_ref = zipfile.ZipFile(test_local_zip, 'r')\n\ntrain_zip_ref.extractall('/kaggle/working')\ntest_zip_ref.extractall('/kaggle/working')\n\ntrain_zip_ref.close()\ntest_zip_ref.close()","metadata":{"execution":{"iopub.status.busy":"2024-01-08T19:55:33.536947Z","iopub.execute_input":"2024-01-08T19:55:33.537454Z","iopub.status.idle":"2024-01-08T19:55:46.462304Z","shell.execute_reply.started":"2024-01-08T19:55:33.537424Z","shell.execute_reply":"2024-01-08T19:55:46.461467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"source_path = '/kaggle/working'\n\ntrain_source_path = os.path.join(source_path, 'train')\ntest_source_path = os.path.join(source_path, 'test1')\n\n#delete non-image files\n!find /'/kaggle/working/train' -type f ! -name \"*.jpg\" -exec rm {} +\n!find /'/kaggle/working/test1' -type f ! -name \"*.jpg\" -exec rm {} +\n\n# os.listdir returns a list containing all files under the given path\nprint(f'there are {len(os.listdir(train_source_path))} images of train.')\nprint(f'there are {len(os.listdir(test_source_path))} images of test.')","metadata":{"execution":{"iopub.status.busy":"2024-01-08T19:55:46.463337Z","iopub.execute_input":"2024-01-08T19:55:46.463619Z","iopub.status.idle":"2024-01-08T19:55:46.791318Z","shell.execute_reply.started":"2024-01-08T19:55:46.463591Z","shell.execute_reply":"2024-01-08T19:55:46.790454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"root_dir = os.path.join(source_path, 'cats-v-dogs')\n\n# Empty directory to prevent FileExistsError is the function is run several times\nif os.path.exists(root_dir):\n  shutil.rmtree(root_dir)\n\n#Create Directory\nos.mkdir(root_dir)\n\n# GRADED FUNCTION: create_train_val_dirs\ndef create_train_val_dirs(root_path):\n    \n    \"\"\"\n    Creates directories for the train and test sets\n\n    Args:\n    root_path (string) - the base directory path to create subdirectories from\n\n    Returns:\n    None\n    \"\"\"\n    for i in ['training', 'validation']:\n        os.mkdir(os.path.join(root_path, i))\n        for t in ['cats', 'dogs']:\n            os.mkdir(os.path.join(os.path.join(root_path, i), t))\n    pass\n\ntry:\n  create_train_val_dirs(root_path=root_dir)\nexcept FileExistsError:\n  print(\"You should not be seeing this since the upper directory is removed beforehand\")","metadata":{"execution":{"iopub.status.busy":"2024-01-08T19:55:46.793873Z","iopub.execute_input":"2024-01-08T19:55:46.794183Z","iopub.status.idle":"2024-01-08T19:55:46.801191Z","shell.execute_reply.started":"2024-01-08T19:55:46.794151Z","shell.execute_reply":"2024-01-08T19:55:46.800536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for rootdir, dirs, files in os.walk(root_dir):\n    for subdir in dirs:\n        print(os.path.join(rootdir, subdir))","metadata":{"execution":{"iopub.status.busy":"2024-01-08T19:55:46.801983Z","iopub.execute_input":"2024-01-08T19:55:46.802200Z","iopub.status.idle":"2024-01-08T19:55:46.814281Z","shell.execute_reply.started":"2024-01-08T19:55:46.802177Z","shell.execute_reply":"2024-01-08T19:55:46.813602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def split_data(SOURCE_DIR, SPLIT_SIZE):\n    \"\"\"\n    Splits the data into train and test sets\n\n    Args:\n    SOURCE_DIR (string): directory path containing the images\n    TRAINING_DIR (string): directory path to be used for training\n    VALIDATION_DIR (string): directory path to be used for validation\n    SPLIT_SIZE (float): proportion of the dataset to be used for training\n\n    Returns:\n    None\n    \"\"\"\n\n    ### START CODE HERE\n    data_list = os.listdir(SOURCE_DIR)\n    \n    #cat_list = list(filter(lambda x:x.beginwith('cat'), data_list))\n    \n    train_rand = random.sample(data_list, int(len(data_list)*SPLIT_SIZE))\n    \n    for i in data_list:\n        if os.path.getsize(os.path.join(SOURCE_DIR, i)) != 0:\n            if i in train_rand:\n                if i.startswith('cat'):\n                    shutil.copy(os.path.join(SOURCE_DIR, i), '/kaggle/working/cats-v-dogs/training/cats')\n                else:\n                    shutil.copy(os.path.join(SOURCE_DIR, i), '/kaggle/working/cats-v-dogs/training/dogs')\n            else:\n                if i.startswith('cat'):\n                    shutil.copy(os.path.join(SOURCE_DIR, i), '/kaggle/working/cats-v-dogs/validation/cats')\n                else:\n                    shutil.copy(os.path.join(SOURCE_DIR, i), '/kaggle/working/cats-v-dogs/validation/dogs')\n        else:\n            print(i, \"  is zero length, so ignoring.\")\n    pass","metadata":{"execution":{"iopub.status.busy":"2024-01-08T19:55:46.815174Z","iopub.execute_input":"2024-01-08T19:55:46.815420Z","iopub.status.idle":"2024-01-08T19:55:46.828031Z","shell.execute_reply.started":"2024-01-08T19:55:46.815394Z","shell.execute_reply":"2024-01-08T19:55:46.827427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"SOURCE_DIR = train_source_path\n\nsplit_size = .9\n\nsplit_data(SOURCE_DIR, split_size)\n\nprint(f\"\\n\\nOriginal cat's directory has {len(os.listdir(SOURCE_DIR))} images\")\n\n# Training and validation splits\nprint(f\"There are {len(os.listdir('/kaggle/working/cats-v-dogs/training/cats'))} images of cats for training\")\nprint(f\"There are {len(os.listdir('/kaggle/working/cats-v-dogs/training/dogs'))} images of dogs for training\")\nprint(f\"There are {len(os.listdir('/kaggle/working/cats-v-dogs/validation/cats'))} images of cats for validation\")\nprint(f\"There are {len(os.listdir('/kaggle/working/cats-v-dogs/validation/dogs'))} images of dogs for validation\")","metadata":{"execution":{"iopub.status.busy":"2024-01-08T19:55:46.828785Z","iopub.execute_input":"2024-01-08T19:55:46.829007Z","iopub.status.idle":"2024-01-08T19:55:58.377398Z","shell.execute_reply.started":"2024-01-08T19:55:46.828983Z","shell.execute_reply":"2024-01-08T19:55:58.376710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train_val_generators(TRAINING_DIR, VALIDATION_DIR):\n  \"\"\"\n  Creates the training and validation data generators\n\n  Args:\n    TRAINING_DIR (string): directory path containing the training images\n    VALIDATION_DIR (string): directory path containing the testing/validation images\n\n  Returns:\n    train_generator, validation_generator - tuple containing the generators\n  \"\"\"\n  \n  # Instantiate the ImageDataGenerator class \n  train_datagen = ImageDataGenerator(rescale=1./255,\n                                     width_shift_range=0.2,\n                                     height_shift_range = 0.2,\n                                     rotation_range=0.2,\n                                     zoom_range=0.2,\n                                     shear_range=0.2\n                                     \n                                    )\n\n  # Pass in the appropriate arguments to the flow_from_directory method\n  train_generator = train_datagen.flow_from_directory(directory=TRAINING_DIR,\n                                                      batch_size=20,\n                                                      class_mode='binary',\n                                                      target_size=(150, 150))\n\n  # Instantiate the ImageDataGenerator class (don't forget to set the rescale argument)\n  validation_datagen = ImageDataGenerator(rescale=1./255)\n\n  # Pass in the appropriate arguments to the flow_from_directory method\n  validation_generator = validation_datagen.flow_from_directory(directory=VALIDATION_DIR,\n                                                                batch_size=20,\n                                                                class_mode='binary',\n                                                                target_size=(150, 150))\n  \n  return train_generator, validation_generator","metadata":{"execution":{"iopub.status.busy":"2024-01-08T19:55:58.378297Z","iopub.execute_input":"2024-01-08T19:55:58.378532Z","iopub.status.idle":"2024-01-08T19:55:58.384038Z","shell.execute_reply.started":"2024-01-08T19:55:58.378506Z","shell.execute_reply":"2024-01-08T19:55:58.383442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_model():\n  model = tf.keras.models.Sequential([\n      tf.keras.layers.Conv2D(16,(5,5), activation='relu',input_shape=(150,150,3)),\n      tf.keras.layers.MaxPooling2D(3,3),\n      tf.keras.layers.Conv2D(32, (5,5), activation='relu'),\n      tf.keras.layers.MaxPooling2D(3,3),\n      tf.keras.layers.Conv2D(64, (5,5), activation='relu'),\n      tf.keras.layers.MaxPool2D(3,3),\n      tf.keras.layers.Flatten(),\n      tf.keras.layers.Dropout(0.2),\n      tf.keras.layers.Dense(512, activation='relu'),\n      tf.keras.layers.Dense(1, activation='sigmoid')\n  ])\n\n\n  model.compile(optimizer=RMSprop(lr=0.001),\n                loss='binary_crossentropy',\n                metrics=['accuracy'])\n\n  return model","metadata":{"execution":{"iopub.status.busy":"2024-01-08T19:55:58.384907Z","iopub.execute_input":"2024-01-08T19:55:58.385158Z","iopub.status.idle":"2024-01-08T19:55:58.401863Z","shell.execute_reply.started":"2024-01-08T19:55:58.385132Z","shell.execute_reply":"2024-01-08T19:55:58.401236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator, validation_generator = train_val_generators('/kaggle/working/cats-v-dogs/training/', '/kaggle/working/cats-v-dogs/validation/')","metadata":{"execution":{"iopub.status.busy":"2024-01-08T19:55:58.402719Z","iopub.execute_input":"2024-01-08T19:55:58.402953Z","iopub.status.idle":"2024-01-08T19:55:58.857608Z","shell.execute_reply.started":"2024-01-08T19:55:58.402929Z","shell.execute_reply":"2024-01-08T19:55:58.856888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = create_model()\n\nhistory = model.fit(train_generator,\n                    epochs=30,\n                    verbose=1,\n                    validation_data=validation_generator)","metadata":{"execution":{"iopub.status.busy":"2024-01-08T19:55:58.858623Z","iopub.execute_input":"2024-01-08T19:55:58.858901Z","iopub.status.idle":"2024-01-08T21:08:29.397634Z","shell.execute_reply.started":"2024-01-08T19:55:58.858873Z","shell.execute_reply":"2024-01-08T21:08:29.396522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nacc = history.history['accuracy']\nval_acc = history.history['val_accuracy']\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\nepochs = range(len(acc))\n\nplt.plot(epochs, acc, 'r', label='Training accuracy')\nplt.plot(epochs, val_acc, 'b', label='validation_accuracy')\nplt.title('Training and validation accuracy')\n\nplt.figure()\n\nplt.plot(epochs, loss, 'r', label='Training Loss')\nplt.plot(epochs, val_loss, 'b', label='Validation Loss')\nplt.title('Training and Validation loss')\nplt.legend()\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-08T21:08:29.399088Z","iopub.execute_input":"2024-01-08T21:08:29.399507Z","iopub.status.idle":"2024-01-08T21:08:29.854847Z","shell.execute_reply.started":"2024-01-08T21:08:29.399472Z","shell.execute_reply":"2024-01-08T21:08:29.853994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}