{"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-11-30T09:38:36.704672Z","iopub.execute_input":"2022-11-30T09:38:36.705604Z","iopub.status.idle":"2022-11-30T09:38:36.738362Z","shell.execute_reply.started":"2022-11-30T09:38:36.705476Z","shell.execute_reply":"2022-11-30T09:38:36.737376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport zipfile\nimport random\nimport shutil\nimport tensorflow as tf\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom shutil import copyfile\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2022-11-30T10:12:22.138808Z","iopub.execute_input":"2022-11-30T10:12:22.139239Z","iopub.status.idle":"2022-11-30T10:12:22.145992Z","shell.execute_reply.started":"2022-11-30T10:12:22.139205Z","shell.execute_reply":"2022-11-30T10:12:22.144504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nlocal_zip = '/kaggle/input/dogs-vs-cats/train.zip'\nzip_ref   = zipfile.ZipFile(local_zip, 'r')\nzip_ref.extractall('/tmp')\nzip_ref.close()","metadata":{"execution":{"iopub.status.busy":"2022-11-30T09:45:12.568665Z","iopub.execute_input":"2022-11-30T09:45:12.569132Z","iopub.status.idle":"2022-11-30T09:45:28.093147Z","shell.execute_reply.started":"2022-11-30T09:45:12.569098Z","shell.execute_reply":"2022-11-30T09:45:28.091893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"source_path = '/tmp/train'\n\n\n# Deletes all non-image files (there are two .db files bundled into the dataset)\n!find /tmp/train/ -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(source_path))} images of dogs and cats.\")","metadata":{"execution":{"iopub.status.busy":"2022-11-30T09:48:12.368802Z","iopub.execute_input":"2022-11-30T09:48:12.369251Z","iopub.status.idle":"2022-11-30T09:48:13.474548Z","shell.execute_reply.started":"2022-11-30T09:48:12.369214Z","shell.execute_reply":"2022-11-30T09:48:13.472724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"source = '/tmp/datasetfordogs&cats'\nos.makedirs(source, 493)\ndog_dir = os.path.join(source, 'dog')\nos.makedirs(dog_dir, 493 )\ncat_dir = os.path.join(source, 'cat')\nos.makedirs(cat_dir, 493 )\nfor image in os.listdir(source_path):\n    name = image[:3]\n    if(name =='dog'):\n        copyfile((os.path.join(source_path, image)), (os.path.join(dog_dir, image)))\n    else:\n        copyfile((os.path.join(source_path, image)), (os.path.join(cat_dir, image)))\n\nprint(f\"There are {len(os.listdir(dog_dir))} images of dogs.\")\nprint(f\"There are {len(os.listdir(cat_dir))} images of cats.\")","metadata":{"execution":{"iopub.status.busy":"2022-11-30T10:05:08.498428Z","iopub.execute_input":"2022-11-30T10:05:08.498890Z","iopub.status.idle":"2022-11-30T10:05:12.543580Z","shell.execute_reply.started":"2022-11-30T10:05:08.498852Z","shell.execute_reply":"2022-11-30T10:05:12.542177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(cat_dir)\nprint(dog_dir)","metadata":{"execution":{"iopub.status.busy":"2022-11-30T10:07:32.711202Z","iopub.execute_input":"2022-11-30T10:07:32.711638Z","iopub.status.idle":"2022-11-30T10:07:32.718194Z","shell.execute_reply.started":"2022-11-30T10:07:32.711603Z","shell.execute_reply":"2022-11-30T10:07:32.716306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define root directory\nroot_dir = '/tmp/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\ndef create_train_val_dirs(root_path):\n  \"\"\"\n  Creates directories for the train and test sets\n  \n  \"\"\"  \n\n  os.makedirs(root_path, 493 )\n  train_dir = os.path.join(root_path, 'training')\n  os.makedirs(train_dir, 493 )\n  validation_dir = os.path.join(root_path, 'validation')\n  os.makedirs(validation_dir, 493)\n  train_cats_dir = os.path.join(train_dir, 'cats')\n  os.makedirs(train_cats_dir, 493)\n  train_dogs_dir = os.path.join(train_dir, 'dogs')\n  os.makedirs(train_dogs_dir, 493)\n  validation_cats_dir = os.path.join(validation_dir, 'cats')\n  os.makedirs(validation_cats_dir, 493)\n  validation_dogs_dir = os.path.join(validation_dir, 'dogs')\n  os.makedirs(validation_dogs_dir, 493)\n\n\ncreate_train_val_dirs(root_path=root_dir)\n","metadata":{"execution":{"iopub.status.busy":"2022-11-30T09:50:05.737526Z","iopub.execute_input":"2022-11-30T09:50:05.738537Z","iopub.status.idle":"2022-11-30T09:50:05.757829Z","shell.execute_reply.started":"2022-11-30T09:50:05.738488Z","shell.execute_reply":"2022-11-30T09:50:05.756451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Test your create_train_val_dirs function\n\nfor 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":"2022-11-30T09:50:30.648288Z","iopub.execute_input":"2022-11-30T09:50:30.648716Z","iopub.status.idle":"2022-11-30T09:50:30.656938Z","shell.execute_reply.started":"2022-11-30T09:50:30.648667Z","shell.execute_reply":"2022-11-30T09:50:30.655547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def split_data(SOURCE_DIR, TRAINING_DIR, VALIDATION_DIR, SPLIT_SIZE):\n  \"\"\"\n  Splits the data into train and validation sets\n  \"\"\"\n\n  length = len([name for name in os.listdir(SOURCE_DIR)])\n  newlength = int(length*SPLIT_SIZE)\n  count = 1\n  randomshuffle = random.sample([name for name in os.listdir(SOURCE_DIR)], length)\n  for file in randomshuffle:\n    size = os.path.getsize((os.path.join(SOURCE_DIR, file)))\n    if(size>0):\n      if(count<=newlength):\n        copyfile((os.path.join(SOURCE_DIR, file)), (os.path.join(TRAINING_DIR, file)))\n      else:\n        copyfile((os.path.join(SOURCE_DIR, file)), (os.path.join(VALIDATION_DIR, file)))\n    else:\n      print(file+\" is zero length, so ignoring.\")\n    count=count+1","metadata":{"execution":{"iopub.status.busy":"2022-11-30T10:08:57.370343Z","iopub.execute_input":"2022-11-30T10:08:57.370813Z","iopub.status.idle":"2022-11-30T10:08:57.380649Z","shell.execute_reply.started":"2022-11-30T10:08:57.370775Z","shell.execute_reply":"2022-11-30T10:08:57.379277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Test your split_data function\n\n# Define paths\nCAT_SOURCE_DIR = \"/tmp/datasetfordogs&cats/cat\"\nDOG_SOURCE_DIR = \"/tmp/datasetfordogs&cats/dog\"\n\nTRAINING_DIR = \"/tmp/cats-v-dogs/training/\"\nVALIDATION_DIR = \"/tmp/cats-v-dogs/validation/\"\n\nTRAINING_CATS_DIR = os.path.join(TRAINING_DIR, \"cats/\")\nVALIDATION_CATS_DIR = os.path.join(VALIDATION_DIR, \"cats/\")\n\nTRAINING_DOGS_DIR = os.path.join(TRAINING_DIR, \"dogs/\")\nVALIDATION_DOGS_DIR = os.path.join(VALIDATION_DIR, \"dogs/\")\n\n# Empty directories in case you run this cell multiple times\nif len(os.listdir(TRAINING_CATS_DIR)) > 0:\n  for file in os.scandir(TRAINING_CATS_DIR):\n    os.remove(file.path)\nif len(os.listdir(TRAINING_DOGS_DIR)) > 0:\n  for file in os.scandir(TRAINING_DOGS_DIR):\n    os.remove(file.path)\nif len(os.listdir(VALIDATION_CATS_DIR)) > 0:\n  for file in os.scandir(VALIDATION_CATS_DIR):\n    os.remove(file.path)\nif len(os.listdir(VALIDATION_DOGS_DIR)) > 0:\n  for file in os.scandir(VALIDATION_DOGS_DIR):\n    os.remove(file.path)\n\n# Define proportion of images used for training\nsplit_size = .9\n\n# Run the function\n# NOTE: Messages about zero length images should be printed out\nsplit_data(CAT_SOURCE_DIR, TRAINING_CATS_DIR, VALIDATION_CATS_DIR, split_size)\nsplit_data(DOG_SOURCE_DIR, TRAINING_DOGS_DIR, VALIDATION_DOGS_DIR, split_size)\n\n# Check that the number of images matches the expected output\n\n# Your function should perform copies rather than moving images so original directories should contain unchanged images\nprint(f\"\\n\\nOriginal cat's directory has {len(os.listdir(CAT_SOURCE_DIR))} images\")\nprint(f\"Original dog's directory has {len(os.listdir(DOG_SOURCE_DIR))} images\\n\")\n\n# Training and validation splits\nprint(f\"There are {len(os.listdir(TRAINING_CATS_DIR))} images of cats for training\")\nprint(f\"There are {len(os.listdir(TRAINING_DOGS_DIR))} images of dogs for training\")\nprint(f\"There are {len(os.listdir(VALIDATION_CATS_DIR))} images of cats for validation\")\nprint(f\"There are {len(os.listdir(VALIDATION_DOGS_DIR))} images of dogs for validation\")","metadata":{"execution":{"iopub.status.busy":"2022-11-30T10:09:00.834777Z","iopub.execute_input":"2022-11-30T10:09:00.835498Z","iopub.status.idle":"2022-11-30T10:09:04.812595Z","shell.execute_reply.started":"2022-11-30T10:09:00.835458Z","shell.execute_reply":"2022-11-30T10:09:04.811704Z"},"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  \"\"\"\n\n  # Instantiate the ImageDataGenerator class (don't forget to set the rescale argument)\n  train_datagen = ImageDataGenerator(rescale=1/255)\n\n  # Pass in the appropiate 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 appropiate 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  return train_generator, validation_generator","metadata":{"execution":{"iopub.status.busy":"2022-11-30T10:11:31.266350Z","iopub.execute_input":"2022-11-30T10:11:31.266822Z","iopub.status.idle":"2022-11-30T10:11:31.274243Z","shell.execute_reply.started":"2022-11-30T10:11:31.266779Z","shell.execute_reply":"2022-11-30T10:11:31.273286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Test your generators\ntrain_generator, validation_generator = train_val_generators(TRAINING_DIR, VALIDATION_DIR)","metadata":{"execution":{"iopub.status.busy":"2022-11-30T10:11:34.947044Z","iopub.execute_input":"2022-11-30T10:11:34.947483Z","iopub.status.idle":"2022-11-30T10:11:35.618644Z","shell.execute_reply.started":"2022-11-30T10:11:34.947439Z","shell.execute_reply":"2022-11-30T10:11:35.617085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_model():\n  # DEFINE A KERAS MODEL TO CLASSIFY CATS V DOGS\n\n  model = tf.keras.models.Sequential([ \n      tf.keras.layers.Conv2D(16, (3,3), activation='relu', input_shape=(150, 150, 3)),\n      tf.keras.layers.MaxPooling2D(2,2),\n      tf.keras.layers.Conv2D(32, (3,3), activation='relu'),\n      tf.keras.layers.MaxPooling2D(2,2), \n      tf.keras.layers.Conv2D(64, (3,3), activation='relu'), \n      tf.keras.layers.MaxPooling2D(2,2),\n      tf.keras.layers.Conv2D(128, (3,3), activation='relu'), \n      tf.keras.layers.MaxPooling2D(2,2),\n      tf.keras.layers.Flatten(),\n      tf.keras.layers.Dense(512, activation='relu'),\n      tf.keras.layers.Dense(1, activation='sigmoid') \n  ])\n\n  \n  model.compile(optimizer= tf.keras.optimizers.Adam(learning_rate=0.001),\n                loss='binary_crossentropy',\n                metrics=['accuracy']) \n    \n\n  return model\n","metadata":{"execution":{"iopub.status.busy":"2022-11-30T10:12:01.201493Z","iopub.execute_input":"2022-11-30T10:12:01.201920Z","iopub.status.idle":"2022-11-30T10:12:01.212864Z","shell.execute_reply.started":"2022-11-30T10:12:01.201875Z","shell.execute_reply":"2022-11-30T10:12:01.211544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get the untrained model\nmodel = create_model()\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-11-30T10:13:03.835570Z","iopub.execute_input":"2022-11-30T10:13:03.836020Z","iopub.status.idle":"2022-11-30T10:13:04.067348Z","shell.execute_reply.started":"2022-11-30T10:13:03.835980Z","shell.execute_reply":"2022-11-30T10:13:04.066238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train the model\nhistory = model.fit(train_generator,\n                    epochs=15,\n                    verbose=1,\n                    validation_data=validation_generator)","metadata":{"execution":{"iopub.status.busy":"2022-11-30T10:13:19.129413Z","iopub.execute_input":"2022-11-30T10:13:19.129823Z","iopub.status.idle":"2022-11-30T11:02:39.477784Z","shell.execute_reply.started":"2022-11-30T10:13:19.129791Z","shell.execute_reply":"2022-11-30T11:02:39.476597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#-----------------------------------------------------------\n# Retrieve a list of list results on training and test data\n# sets for each training epoch\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)) # Get number of epochs\n\n#------------------------------------------------\n# Plot training and validation accuracy per epoch\n#------------------------------------------------\nplt.plot(epochs, acc, 'r', \"Training Accuracy\")\nplt.plot(epochs, val_acc, 'b', \"Validation Accuracy\")\nplt.title('Training and validation accuracy')\nplt.show()\nprint(\"\")\n\n#------------------------------------------------\n# Plot training and validation loss per epoch\n#------------------------------------------------\nplt.plot(epochs, loss, 'r', \"Training Loss\")\nplt.plot(epochs, val_loss, 'b', \"Validation Loss\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-30T11:03:01.719750Z","iopub.execute_input":"2022-11-30T11:03:01.720946Z","iopub.status.idle":"2022-11-30T11:03:02.250543Z","shell.execute_reply.started":"2022-11-30T11:03:01.720900Z","shell.execute_reply":"2022-11-30T11:03:02.248459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"local_zip_test = '/kaggle/input/dogs-vs-cats/test1.zip'\nzip_ref   = zipfile.ZipFile(local_zip_test, 'r')\nzip_ref.extractall('/tmp')\nzip_ref.close()","metadata":{"execution":{"iopub.status.busy":"2022-11-30T11:04:08.999905Z","iopub.execute_input":"2022-11-30T11:04:09.000318Z","iopub.status.idle":"2022-11-30T11:04:17.427412Z","shell.execute_reply.started":"2022-11-30T11:04:09.000283Z","shell.execute_reply":"2022-11-30T11:04:17.426179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"source_path_test = '/tmp/test1'\n\n\n# Deletes all non-image files (there are two .db files bundled into the dataset)\n!find /tmp/train/ -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(source_path_test))} test images of dogs and cats.\")","metadata":{"execution":{"iopub.status.busy":"2022-11-30T11:05:03.920673Z","iopub.execute_input":"2022-11-30T11:05:03.921432Z","iopub.status.idle":"2022-11-30T11:05:05.037365Z","shell.execute_reply.started":"2022-11-30T11:05:03.921392Z","shell.execute_reply":"2022-11-30T11:05:05.035615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfrom tensorflow.keras.utils import load_img, img_to_array\nimport pandas as pd\nfrom tqdm import tqdm\n\n\nresult = pd.DataFrame(columns=['id','label'])\nids = []\nlabels =[]\nfor file in tqdm(os.listdir(source_path_test)):\n    \n    path = '/tmp/test1/' + file\n    # predicting images\n    img=load_img(path, target_size=(150, 150))\n  \n    x=img_to_array(img)\n    x /= 255\n    x=np.expand_dims(x, axis=0)\n    images = np.vstack([x])\n  \n    classes = model.predict(images, batch_size=10)\n    file = int(file.rsplit( \".\", 1 )[ 0 ])\n    ids.append(file)\n    if classes[0]>0.5:\n        labels.append(1)\n    else:\n        labels.append(0)\n\nresult['id'] = ids\nresult['label']=labels\nresult.to_csv(r'submission.csv')\nFileLink(r'submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-11-30T11:43:33.205198Z","iopub.execute_input":"2022-11-30T11:43:33.205715Z","iopub.status.idle":"2022-11-30T11:56:55.199492Z","shell.execute_reply.started":"2022-11-30T11:43:33.205660Z","shell.execute_reply":"2022-11-30T11:56:55.196210Z"},"trusted":true},"execution_count":null,"outputs":[]}]}