{"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":"import os\nfrom tensorflow.keras.applications.vgg16 import VGG16\nfrom tensorflow.keras.applications.resnet import preprocess_input, decode_predictions\nfrom tensorflow.keras.preprocessing import image\nimport numpy as np\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nimport matplotlib.image as mpimg\nimport matplotlib.pyplot as plt\nimport random\nimport shutil\nimport cv2\nfrom keras.preprocessing.image import ImageDataGenerator","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-11-05T12:00:17.033084Z","iopub.execute_input":"2023-11-05T12:00:17.033368Z","iopub.status.idle":"2023-11-05T12:00:20.068942Z","shell.execute_reply.started":"2023-11-05T12:00:17.033345Z","shell.execute_reply":"2023-11-05T12:00:20.068090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Unzipping Training Data","metadata":{}},{"cell_type":"code","source":"\n\nif not os.path.exists(\"/kaggle/working/train\"):\n    !unzip -q /kaggle/input/dogs-vs-cats/train.zip\n\n","metadata":{"execution":{"iopub.status.busy":"2023-11-05T12:00:20.070442Z","iopub.execute_input":"2023-11-05T12:00:20.070942Z","iopub.status.idle":"2023-11-05T12:00:20.075352Z","shell.execute_reply.started":"2023-11-05T12:00:20.070919Z","shell.execute_reply":"2023-11-05T12:00:20.074468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = '/kaggle/working/train'\nlen(os.listdir(train_path))","metadata":{"execution":{"iopub.status.busy":"2023-11-05T12:00:20.076783Z","iopub.execute_input":"2023-11-05T12:00:20.077338Z","iopub.status.idle":"2023-11-05T12:00:20.100188Z","shell.execute_reply.started":"2023-11-05T12:00:20.077314Z","shell.execute_reply":"2023-11-05T12:00:20.099266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(16,16))\n\nimages = os.listdir(train_path)\nfor i in range(8):\n    img = random.choice([x for x in images])\n    fig = plt.subplot(4,4,i+1)\n    fig.axis('off')\n    img = mpimg.imread(os.path.join(train_path, img)) \n    fig.imshow(img) ","metadata":{"execution":{"iopub.status.busy":"2023-11-05T12:00:20.101270Z","iopub.execute_input":"2023-11-05T12:00:20.101530Z","iopub.status.idle":"2023-11-05T12:00:20.843595Z","shell.execute_reply.started":"2023-11-05T12:00:20.101509Z","shell.execute_reply":"2023-11-05T12:00:20.842834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir(train_path)[:10]","metadata":{"execution":{"iopub.status.busy":"2023-11-05T12:00:20.845618Z","iopub.execute_input":"2023-11-05T12:00:20.846565Z","iopub.status.idle":"2023-11-05T12:00:20.864099Z","shell.execute_reply.started":"2023-11-05T12:00:20.846531Z","shell.execute_reply":"2023-11-05T12:00:20.863182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Creating Directories to organize our Data\n\ncreate a directory structure for a cats-vs-dogs project. You create a parent directory named \"cats-v-dogs\" and inside that, you create two directories named \"training\" and \"validation\". Within each of these directories, you create two more directories named \"cats\" and \"dogs\".\n\nIf the directories already exist, the code will print \"Existed\". Otherwise, it will create the directories.","metadata":{}},{"cell_type":"code","source":"try:\n    parent_dir = '/kaggle/working/cats-v-dogs'\n    os.mkdir(parent_dir)\n\n    for dir in ['training', 'validation']:\n        path = os.path.join(parent_dir , dir)\n        os.mkdir(path)\n        for dir in ['cats', 'dogs']:\n            n_path = os.path.join(path , dir)\n            os.mkdir(n_path)\nexcept OSError:\n    print('Existed')","metadata":{"execution":{"iopub.status.busy":"2023-11-05T12:00:20.865398Z","iopub.execute_input":"2023-11-05T12:00:20.865629Z","iopub.status.idle":"2023-11-05T12:00:20.872535Z","shell.execute_reply.started":"2023-11-05T12:00:20.865609Z","shell.execute_reply":"2023-11-05T12:00:20.871532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"src_dir = train_path\n\ncats_train_dir = '/kaggle/working/cats-v-dogs/training/cats'\ncats_valid_dir = '/kaggle/working/cats-v-dogs/validation/cats'\n\ndogs_train_dir = '/kaggle/working/cats-v-dogs/training/dogs'\ndogs_valid_dir = '/kaggle/working/cats-v-dogs/validation/dogs'\n\n# List of cats names in all training images\ncats =[img for img in os.listdir(src_dir) if img[:3] == 'cat']\n\n# List of dogs names in all training images\ndogs =[img for img in os.listdir(src_dir) if img[:3] == 'dog']\n\n# Take 10% of training images for validation    \ncats_valid_sample = random.sample(cats, int(0.1*len(cats))) \ndogs_valid_sample = random.sample(dogs, int(0.1*len(dogs)))","metadata":{"execution":{"iopub.status.busy":"2023-11-05T12:00:20.874052Z","iopub.execute_input":"2023-11-05T12:00:20.874443Z","iopub.status.idle":"2023-11-05T12:00:20.910459Z","shell.execute_reply.started":"2023-11-05T12:00:20.874417Z","shell.execute_reply":"2023-11-05T12:00:20.909611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for img in os.listdir(src_dir):\n    src_img = os.path.join(src_dir, img)\n\n    if img[:3] == 'cat':\n        if img in cats_valid_sample:\n            dest_img = os.path.join(cats_valid_dir, img)\n        else:\n            dest_img = os.path.join(cats_train_dir, img)\n\n    elif img[:3] == 'dog':\n        if img in dogs_valid_sample:\n            dest_img = os.path.join(dogs_valid_dir, img)\n        else:\n            dest_img = os.path.join(dogs_train_dir, img)\n    if not os.path.exists(dest_img):\n        shutil.copyfile(src_img, dest_img)","metadata":{"execution":{"iopub.status.busy":"2023-11-05T12:00:20.911555Z","iopub.execute_input":"2023-11-05T12:00:20.911814Z","iopub.status.idle":"2023-11-05T12:00:22.098338Z","shell.execute_reply.started":"2023-11-05T12:00:20.911792Z","shell.execute_reply":"2023-11-05T12:00:22.097411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Number of ALL cats : ', len(cats))\nprint('Number of cats for training: ', len(os.listdir(cats_train_dir)))\nprint('Number of cats for validation: ', len(os.listdir(cats_valid_dir)))\n\nprint('\\n Number of ALL dogs : ', len(dogs))\nprint('Number of dogs for training: ', len(os.listdir(dogs_train_dir)))\nprint('Number of dogs for validation: ', len(os.listdir(dogs_valid_dir)))","metadata":{"execution":{"iopub.status.busy":"2023-11-05T12:00:22.101292Z","iopub.execute_input":"2023-11-05T12:00:22.101536Z","iopub.status.idle":"2023-11-05T12:00:22.118604Z","shell.execute_reply.started":"2023-11-05T12:00:22.101516Z","shell.execute_reply":"2023-11-05T12:00:22.117751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def apply_all_augmentations(file_path, rain_intensity=0.2, snow_intensity=0.2, mist_intensity=0.2):\n    # Load the image\n    image = cv2.imread(file_path)\n    \n    # Rain filter\n    height, width, _ = image.shape\n    rain_image = np.zeros_like(image)\n    num_raindrops = int(rain_intensity * height * width)  \n    for _ in range(num_raindrops):\n        x = random.randint(0, width - 1)\n        y = random.randint(0, height - 1)\n        rain_image[y, x] = (255, 255, 255)  # White raindrop\n    rain_image = cv2.GaussianBlur(rain_image, (7, 7), 0)\n    image_with_rain = cv2.addWeighted(image, 1, rain_image, 0.5, 0)\n\n    # Snow filter\n    snow_image = np.zeros_like(image)\n    num_snowflakes = int(snow_intensity * height * width) \n    for _ in range(num_snowflakes):\n        x = random.randint(0, width - 1)\n        y = random.randint(0, height - 1)\n        snow_image[y, x] = (255, 255, 255)  # White snowflake\n    image_with_snow = cv2.addWeighted(image, 1, snow_image, 0.5, 0)\n\n    # Mist filter\n    mist_color = (200, 200, 200)\n    mist_image = np.full(image.shape, mist_color, dtype=np.uint8)\n    image_with_mist = cv2.addWeighted(image, 1 - mist_intensity, mist_image, mist_intensity, 0)\n\n    # Specify the directory to save the augmented images\n    output_dir = \"/kaggle/working/cats-v-dogs\"\n    \n    # Create the output directory if it doesn't exist\n    os.makedirs(output_dir, exist_ok=True)\n\n    # Construct the output file paths within the specified directory\n    output_file_rain = os.path.join(output_dir, os.path.basename(file_path).replace('.jpg', '_rainy.jpg'))\n    output_file_snow = os.path.join(output_dir, os.path.basename(file_path).replace('.jpg', '_snowy.jpg'))\n    output_file_mist = os.path.join(output_dir, os.path.basename(file_path).replace('.jpg', '_mist.jpg'))\n    \n    # Save the augmented images\n    cv2.imwrite(output_file_rain, image_with_rain)\n    cv2.imwrite(output_file_snow, image_with_snow)\n    cv2.imwrite(output_file_mist, image_with_mist)","metadata":{"execution":{"iopub.status.busy":"2023-11-05T12:00:51.333373Z","iopub.execute_input":"2023-11-05T12:00:51.333739Z","iopub.status.idle":"2023-11-05T12:00:51.342890Z","shell.execute_reply.started":"2023-11-05T12:00:51.333712Z","shell.execute_reply":"2023-11-05T12:00:51.342191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define an ImageDataGenerator with the custom augmentation functions\ntrain_datagen = ImageDataGenerator(\n    rescale=1./255,\n    rotation_range=40,\n    width_shift_range=0.2,\n    height_shift_range=0.2,\n    shear_range=0.2,\n    zoom_range=0.2,\n    horizontal_flip=True,\n    fill_mode='nearest',\n    preprocessing_function=apply_all_augmentations,  # Apply all effect\n)\nvalid_datagen = ImageDataGenerator(\n    rescale=1./255,\n    rotation_range=40,\n    width_shift_range=0.2,\n    height_shift_range=0.2,\n    shear_range=0.2,\n    zoom_range=0.2,\n    horizontal_flip=True,\n    fill_mode='nearest',\n    preprocessing_function=apply_all_augmentations,  # Apply all effect\n)\n# Specify the directory containing your training and validation images\ntrain_dir = '/kaggle/working/cats-v-dogs/training'\nvalid_dir = '/kaggle/working/cats-v-dogs/validation'\n# Create a data generator using flow_from_directory\ntrain_generator = train_datagen.flow_from_directory(\n    train_dir,\n    target_size=(150, 150),\n    batch_size=32,\n    class_mode='binary'\n)\n\nvalid_generator=valid_datagen.flow_from_directory(\n    valid_dir,\n    target_size=(150, 150),\n    batch_size=32,\n    class_mode='binary' )\n","metadata":{"execution":{"iopub.status.busy":"2023-11-05T12:01:20.026180Z","iopub.execute_input":"2023-11-05T12:01:20.026490Z","iopub.status.idle":"2023-11-05T12:01:20.568275Z","shell.execute_reply.started":"2023-11-05T12:01:20.026469Z","shell.execute_reply":"2023-11-05T12:01:20.567434Z"},"trusted":true},"execution_count":null,"outputs":[]}]}