{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Check for if class names in the txt file matches with the label dir " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Rename to bring the consistency" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Renamed 'all purpose flour' to 'all_purpose_flour'\n", "Renamed 'basmati rice' to 'basmati_rice'\n", "Renamed 'bell pepper' to 'bell_pepper'\n", "Renamed 'black pepper' to 'black_pepper'\n", "Renamed 'bread crumbs' to 'bread_crumbs'\n", "Renamed 'bread flour' to 'bread_flour'\n", "Renamed 'brown rice' to 'brown_rice'\n", "Renamed 'brownie mix' to 'brownie_mix'\n", "Renamed 'chocolate chips' to 'chocolate_chips'\n", "Renamed 'chocolate syrup' to 'chocolate_syrup'\n", "Renamed 'cocoa powder' to 'cocoa_powder'\n", "Renamed 'kidney beans' to 'kidney_beans'\n", "Renamed 'oreo cookies' to 'oreo_cookies'\n", "Renamed 'red chilies' to 'red_chilies'\n", "Renamed 'sweet potato' to 'sweet_potato'\n", "Renamed 'vanilla ice cream' to 'vanilla_ice_cream'\n", "Directory renaming completed.\n" ] } ], "source": [ "import os\n", "\n", "def rename_directories(dataset_directory):\n", " for dirname in os.listdir(dataset_directory):\n", " current_dir_path = os.path.join(dataset_directory, dirname)\n", " if os.path.isdir(current_dir_path):\n", " new_dirname = dirname.replace(' ', '_')\n", " new_dir_path = os.path.join(dataset_directory, new_dirname)\n", " if current_dir_path != new_dir_path: \n", " os.rename(current_dir_path, new_dir_path)\n", " print(f\"Renamed '{dirname}' to '{new_dirname}'\")\n", "dataset_directory = r'C:\\Users\\Kiyo\\Desktop\\DL\\Project\\image_data\\initial_data'\n", "rename_directories(dataset_directory)\n", "\n", "print(\"Directory renaming completed.\")\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "All classes in 'Final_classes.txt' have corresponding directories in the dataset.\n", "No extra directories in the dataset that are not listed in 'Final_classes.txt'.\n" ] } ], "source": [ "\n", "# dataset directory and class names file\n", "dataset_dir = r'C:\\Users\\Kiyo\\Desktop\\DL\\Project\\image_data\\initial_data'\n", "class_names_file = r'C:\\Users\\Kiyo\\Desktop\\DL\\Project\\image_data\\Final_classes.txt'\n", "\n", "# Load class names from file\n", "with open(class_names_file, 'r') as f:\n", " class_names = [line.strip().replace(' ', '_') for line in f]\n", "\n", "# Get a list of actual directory names in the dataset\n", "actual_dirs = [d for d in os.listdir(dataset_dir) if os.path.isdir(os.path.join(dataset_dir, d))]\n", "\n", "# Check for discrepancies\n", "missing_dirs = set(class_names) - set(actual_dirs)\n", "extra_dirs = set(actual_dirs) - set(class_names)\n", "\n", "if missing_dirs:\n", " print(f\"Missing directories for classes in 'Final_classes.txt': {missing_dirs}\")\n", "else:\n", " print(\"All classes in 'Final_classes.txt' have corresponding directories in the dataset.\")\n", "\n", "if extra_dirs:\n", " print(f\"Extra directories in the dataset that are not listed in 'Final_classes.txt': {extra_dirs}\")\n", "else:\n", " print(\"No extra directories in the dataset that are not listed in 'Final_classes.txt'.\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Removing files other than jpg" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "No non-JPG files found in the dataset.\n" ] } ], "source": [ "\n", "def remove_non_jpg_images(dataset_dir):\n", " removed_files = []\n", " for root, dirs, files in os.walk(dataset_dir):\n", " for file in files:\n", " # Check if the file extension is not .jpg\n", " if not file.lower().endswith('.jpg'):\n", " file_path = os.path.join(root, file)\n", " os.remove(file_path) # Remove the non-JPG file\n", " removed_files.append(file_path)\n", " return removed_files\n", "\n", "\n", "dataset_dir = r'C:\\Users\\Kiyo\\Desktop\\DL\\Project\\image_data\\initial_data'\n", "removed_files = remove_non_jpg_images(dataset_dir)\n", "\n", "if removed_files:\n", " print(f\"Removed {len(removed_files)} non-JPG files:\")\n", " for file in removed_files:\n", " print(file)\n", "else:\n", " print(\"No non-JPG files found in the dataset.\")\n" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "all_purpose_flour: 50 images\n", "almonds: 50 images\n", "apple: 50 images\n", "apricot: 50 images\n", "asparagus: 50 images\n", "avocado: 50 images\n", "bacon: 50 images\n", "banana: 50 images\n", "barley: 50 images\n", "basil: 50 images\n", "basmati_rice: 50 images\n", "beans: 50 images\n", "beef: 50 images\n", "beets: 50 images\n", "bell_pepper: 50 images\n", "berries: 50 images\n", "biscuits: 50 images\n", "blackberries: 50 images\n", "black_pepper: 50 images\n", "blueberries: 50 images\n", "bread: 50 images\n", "bread_crumbs: 50 images\n", "bread_flour: 50 images\n", "broccoli: 50 images\n", "brownie_mix: 50 images\n", "brown_rice: 50 images\n", "butter: 50 images\n", "cabbage: 50 images\n", "cake: 50 images\n", "cardamom: 50 images\n", "carrot: 50 images\n", "cashews: 50 images\n", "cauliflower: 50 images\n", "celery: 50 images\n", "cereal: 50 images\n", "cheese: 50 images\n", "cherries: 50 images\n", "chicken: 50 images\n", "chickpeas: 50 images\n", "chocolate: 50 images\n", "chocolate_chips: 50 images\n", "chocolate_syrup: 50 images\n", "cilantro: 50 images\n", "cinnamon: 50 images\n", "clove: 50 images\n", "cocoa_powder: 50 images\n", "coconut: 50 images\n", "cookies: 50 images\n", "corn: 50 images\n", "cucumber: 50 images\n", "dates: 50 images\n", "eggplant: 50 images\n", "eggs: 50 images\n", "fish: 50 images\n", "garlic: 50 images\n", "ginger: 50 images\n", "grapes: 50 images\n", "honey: 50 images\n", "jalapeno: 50 images\n", "kidney_beans: 50 images\n", "lemon: 50 images\n", "mango: 50 images\n", "marshmallows: 50 images\n", "milk: 50 images\n", "mint: 50 images\n", "muffins: 50 images\n", "mushroom: 50 images\n", "noodles: 50 images\n", "nuts: 50 images\n", "oats: 50 images\n", "okra: 50 images\n", "olive: 50 images\n", "onion: 50 images\n", "orange: 50 images\n", "oreo_cookies: 50 images\n", "pasta: 50 images\n", "pear: 50 images\n", "pepper: 50 images\n", "pineapple: 50 images\n", "pistachios: 50 images\n", "pork: 50 images\n", "potato: 50 images\n", "pumpkin: 50 images\n", "radishes: 50 images\n", "raisins: 50 images\n", "red_chilies: 50 images\n", "rice: 50 images\n", "rosemary: 50 images\n", "salmon: 50 images\n", "salt: 50 images\n", "shrimp: 50 images\n", "spinach: 50 images\n", "strawberries: 50 images\n", "sugar: 50 images\n", "sweet_potato: 50 images\n", "tomato: 50 images\n", "vanilla_ice_cream: 50 images\n", "walnuts: 50 images\n", "watermelon: 50 images\n", "yogurt: 50 images\n", "\n", "Total images in dataset: 5000\n" ] } ], "source": [ "def count_images(dataset_dir):\n", " class_image_counts = {}\n", " for class_name in os.listdir(dataset_dir):\n", " class_dir = os.path.join(dataset_dir, class_name)\n", " if os.path.isdir(class_dir):\n", " image_count = sum(1 for file in os.listdir(class_dir) if file.lower().endswith('.jpg'))\n", " class_image_counts[class_name] = image_count\n", " return class_image_counts\n", "\n", "#\n", "dataset_dir = r'C:\\Users\\Kiyo\\Desktop\\DL\\Project\\image_data\\initial_data'\n", "image_counts = count_images(dataset_dir)\n", "\n", "# Print out the counts\n", "for class_name, count in image_counts.items():\n", " print(f\"{class_name}: {count} images\")\n", "\n", "# Optional: total image count\n", "total_images = sum(image_counts.values())\n", "print(f\"\\nTotal images in dataset: {total_images}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- remove images that count is more than 50 per class " ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Class 'all_purpose_flour' meets the maximum image requirement.\n", "Class 'almonds' meets the maximum image requirement.\n", "Class 'apple' meets the maximum image requirement.\n", "Class 'apricot' meets the maximum image requirement.\n", "Class 'asparagus' meets the maximum image requirement.\n", "Class 'avocado' meets the maximum image requirement.\n", "Class 'bacon' meets the maximum image requirement.\n", "Class 'banana' meets the maximum image requirement.\n", "Class 'barley' meets the maximum image requirement.\n", "Class 'basil' meets the maximum image requirement.\n", "Class 'basmati_rice' meets the maximum image requirement.\n", "Class 'beans' meets the maximum image requirement.\n", "Class 'beef' meets the maximum image requirement.\n", "Class 'beets' meets the maximum image requirement.\n", "Class 'bell_pepper' meets the maximum image requirement.\n", "Class 'berries' meets the maximum image requirement.\n", "Class 'biscuits' meets the maximum image requirement.\n", "Class 'blackberries' meets the maximum image requirement.\n", "Class 'black_pepper' meets the maximum image requirement.\n", "Class 'blueberries' meets the maximum image requirement.\n", "Class 'bread' meets the maximum image requirement.\n", "Class 'bread_crumbs' meets the maximum image requirement.\n", "Class 'bread_flour' meets the maximum image requirement.\n", "Class 'broccoli' meets the maximum image requirement.\n", "Class 'brownie_mix' meets the maximum image requirement.\n", "Class 'brown_rice' meets the maximum image requirement.\n", "Class 'butter' meets the maximum image requirement.\n", "Class 'cabbage' meets the maximum image requirement.\n", "Class 'cake' meets the maximum image requirement.\n", "Class 'cardamom' meets the maximum image requirement.\n", "Class 'carrot' meets the maximum image requirement.\n", "Class 'cashews' meets the maximum image requirement.\n", "Class 'cauliflower' meets the maximum image requirement.\n", "Class 'celery' meets the maximum image requirement.\n", "Class 'cereal' meets the maximum image requirement.\n", "Class 'cheese' meets the maximum image requirement.\n", "Class 'cherries' meets the maximum image requirement.\n", "Class 'chicken' meets the maximum image requirement.\n", "Class 'chickpeas' meets the maximum image requirement.\n", "Class 'chocolate' meets the maximum image requirement.\n", "Class 'chocolate_chips' meets the maximum image requirement.\n", "Class 'chocolate_syrup' meets the maximum image requirement.\n", "Class 'cilantro' meets the maximum image requirement.\n", "Class 'cinnamon' meets the maximum image requirement.\n", "Class 'clove' meets the maximum image requirement.\n", "Removed 15 images from 'cocoa_powder' to meet the maximum of 50 images.\n", "Removed 57 images from 'coconut' to meet the maximum of 50 images.\n", "Class 'cookies' meets the maximum image requirement.\n", "Class 'corn' meets the maximum image requirement.\n", "Class 'cucumber' meets the maximum image requirement.\n", "Class 'dates' meets the maximum image requirement.\n", "Class 'eggplant' meets the maximum image requirement.\n", "Class 'eggs' meets the maximum image requirement.\n", "Class 'fish' meets the maximum image requirement.\n", "Class 'garlic' meets the maximum image requirement.\n", "Class 'ginger' meets the maximum image requirement.\n", "Class 'grapes' meets the maximum image requirement.\n", "Class 'honey' meets the maximum image requirement.\n", "Class 'jalapeno' meets the maximum image requirement.\n", "Class 'kidney_beans' meets the maximum image requirement.\n", "Class 'lemon' meets the maximum image requirement.\n", "Class 'mango' meets the maximum image requirement.\n", "Class 'marshmallows' meets the maximum image requirement.\n", "Class 'milk' meets the maximum image requirement.\n", "Class 'mint' meets the maximum image requirement.\n", "Class 'muffins' meets the maximum image requirement.\n", "Class 'mushroom' meets the maximum image requirement.\n", "Class 'noodles' meets the maximum image requirement.\n", "Class 'nuts' meets the maximum image requirement.\n", "Class 'oats' meets the maximum image requirement.\n", "Class 'okra' meets the maximum image requirement.\n", "Class 'olive' meets the maximum image requirement.\n", "Class 'onion' meets the maximum image requirement.\n", "Class 'orange' meets the maximum image requirement.\n", "Class 'oreo_cookies' meets the maximum image requirement.\n", "Class 'pasta' meets the maximum image requirement.\n", "Class 'pear' meets the maximum image requirement.\n", "Class 'pepper' meets the maximum image requirement.\n", "Class 'pineapple' meets the maximum image requirement.\n", "Class 'pistachios' meets the maximum image requirement.\n", "Class 'pork' meets the maximum image requirement.\n", "Class 'potato' meets the maximum image requirement.\n", "Class 'pumpkin' meets the maximum image requirement.\n", "Class 'radishes' meets the maximum image requirement.\n", "Class 'raisins' meets the maximum image requirement.\n", "Class 'red_chilies' meets the maximum image requirement.\n", "Class 'rice' meets the maximum image requirement.\n", "Removed 8 images from 'rosemary' to meet the maximum of 50 images.\n", "Removed 3 images from 'salmon' to meet the maximum of 50 images.\n", "Removed 3 images from 'salt' to meet the maximum of 50 images.\n", "Removed 9 images from 'shrimp' to meet the maximum of 50 images.\n", "Removed 8 images from 'spinach' to meet the maximum of 50 images.\n", "Removed 9 images from 'strawberries' to meet the maximum of 50 images.\n", "Removed 8 images from 'sugar' to meet the maximum of 50 images.\n", "Removed 9 images from 'sweet_potato' to meet the maximum of 50 images.\n", "Removed 4 images from 'tomato' to meet the maximum of 50 images.\n", "Removed 8 images from 'vanilla_ice_cream' to meet the maximum of 50 images.\n", "Class 'walnuts' meets the maximum image requirement.\n", "Class 'watermelon' meets the maximum image requirement.\n", "Class 'yogurt' meets the maximum image requirement.\n", "Image reduction completed.\n" ] } ], "source": [ "import random\n", "\n", "def remove_images_if_more_than(dataset_dir, max_images=50):\n", " for class_name in os.listdir(dataset_dir):\n", " class_dir = os.path.join(dataset_dir, class_name)\n", " if os.path.isdir(class_dir):\n", " images = [file for file in os.listdir(class_dir) if file.lower().endswith('.jpg')]\n", " if len(images) > max_images:\n", " images_to_remove = random.sample(images, len(images) - max_images)\n", " for image in images_to_remove:\n", " image_path = os.path.join(class_dir, image)\n", " os.remove(image_path)\n", " print(f\"Removed {len(images_to_remove)} images from '{class_name}' to meet the maximum of {max_images} images.\")\n", " else:\n", " print(f\"Class '{class_name}' meets the maximum image requirement.\")\n", "\n", "# Specify the path to your dataset directory\n", "dataset_dir = r'C:\\Users\\Kiyo\\Desktop\\DL\\Project\\image_data\\initial_data'\n", "remove_images_if_more_than(dataset_dir, 50)\n", "\n", "print(\"Image reduction completed.\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- augemnet to make every class contains count of 50" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Collecting imgaug\n", " Downloading imgaug-0.4.0-py2.py3-none-any.whl.metadata (1.8 kB)\n", "Requirement already satisfied: six in c:\\users\\kiyo\\appdata\\roaming\\python\\python312\\site-packages (from imgaug) (1.16.0)\n", "Requirement already satisfied: numpy>=1.15 in c:\\users\\kiyo\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from imgaug) (1.26.3)\n", "Requirement already satisfied: scipy in c:\\users\\kiyo\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from imgaug) (1.12.0)\n", "Requirement already satisfied: Pillow in c:\\users\\kiyo\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from imgaug) (10.2.0)\n", "Requirement already satisfied: matplotlib in c:\\users\\kiyo\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from imgaug) (3.8.2)\n", "Requirement already satisfied: scikit-image>=0.14.2 in c:\\users\\kiyo\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from imgaug) (0.22.0)\n", "Requirement already satisfied: opencv-python in c:\\users\\kiyo\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from imgaug) (4.9.0.80)\n", "Requirement already satisfied: imageio in c:\\users\\kiyo\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from imgaug) (2.34.0)\n", "Requirement already satisfied: Shapely in c:\\users\\kiyo\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from imgaug) (2.0.2)\n", "Requirement already satisfied: networkx>=2.8 in c:\\users\\kiyo\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from scikit-image>=0.14.2->imgaug) (3.2.1)\n", "Requirement already satisfied: tifffile>=2022.8.12 in c:\\users\\kiyo\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from scikit-image>=0.14.2->imgaug) (2024.2.12)\n", "Requirement already satisfied: packaging>=21 in c:\\users\\kiyo\\appdata\\roaming\\python\\python312\\site-packages (from scikit-image>=0.14.2->imgaug) (23.2)\n", "Requirement already satisfied: lazy_loader>=0.3 in c:\\users\\kiyo\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from scikit-image>=0.14.2->imgaug) (0.3)\n", "Requirement already satisfied: contourpy>=1.0.1 in c:\\users\\kiyo\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from matplotlib->imgaug) (1.2.0)\n", "Requirement already satisfied: cycler>=0.10 in c:\\users\\kiyo\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from matplotlib->imgaug) (0.12.1)\n", "Requirement already satisfied: fonttools>=4.22.0 in c:\\users\\kiyo\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from matplotlib->imgaug) (4.47.2)\n", "Requirement already satisfied: kiwisolver>=1.3.1 in c:\\users\\kiyo\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from matplotlib->imgaug) (1.4.5)\n", "Requirement already satisfied: pyparsing>=2.3.1 in c:\\users\\kiyo\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from matplotlib->imgaug) (3.1.1)\n", "Requirement already satisfied: python-dateutil>=2.7 in c:\\users\\kiyo\\appdata\\roaming\\python\\python312\\site-packages (from matplotlib->imgaug) (2.8.2)\n", "Downloading imgaug-0.4.0-py2.py3-none-any.whl (948 kB)\n", " ---------------------------------------- 0.0/948.0 kB ? eta -:--:--\n", " ---------------------------------------- 10.2/948.0 kB ? eta -:--:--\n", " - ------------------------------------- 30.7/948.0 kB 660.6 kB/s eta 0:00:02\n", " ------ --------------------------------- 153.6/948.0 kB 1.5 MB/s eta 0:00:01\n", " ------------------------ --------------- 583.7/948.0 kB 4.6 MB/s eta 0:00:01\n", " ---------------------------------------- 948.0/948.0 kB 5.0 MB/s eta 0:00:00\n", "Installing collected packages: imgaug\n", "Successfully installed imgaug-0.4.0\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "DEPRECATION: celery 4.2.0 has a non-standard dependency specifier pytz>dev. pip 24.1 will enforce this behaviour change. A possible replacement is to upgrade to a newer version of celery or contact the author to suggest that they release a version with a conforming dependency specifiers. Discussion can be found at https://github.com/pypa/pip/issues/12063\n" ] } ], "source": [ "!pip install imgaug\n" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Completed augmentation for class 'all_purpose_flour'.\n", "Completed augmentation for class 'almonds'.\n", "Completed augmentation for class 'apple'.\n", "Completed augmentation for class 'apricot'.\n", "Completed augmentation for class 'asparagus'.\n", "Completed augmentation for class 'avocado'.\n", "Completed augmentation for class 'bacon'.\n", "Completed augmentation for class 'banana'.\n", "Completed augmentation for class 'barley'.\n", "Completed augmentation for class 'basil'.\n", "Completed augmentation for class 'basmati_rice'.\n", "Completed augmentation for class 'beans'.\n", "Completed augmentation for class 'beef'.\n", "Completed augmentation for class 'beets'.\n", "Completed augmentation for class 'bell_pepper'.\n", "Completed augmentation for class 'berries'.\n", "Completed augmentation for class 'biscuits'.\n", "Completed augmentation for class 'blackberries'.\n", "Completed augmentation for class 'black_pepper'.\n", "Completed augmentation for class 'blueberries'.\n", "Completed augmentation for class 'bread'.\n", "Completed augmentation for class 'bread_crumbs'.\n", "Completed augmentation for class 'bread_flour'.\n", "Completed augmentation for class 'broccoli'.\n", "Completed augmentation for class 'brownie_mix'.\n", "Completed augmentation for class 'brown_rice'.\n", "Completed augmentation for class 'butter'.\n", "Completed augmentation for class 'cabbage'.\n", "Completed augmentation for class 'cake'.\n", "Completed augmentation for class 'cardamom'.\n", "Completed augmentation for class 'carrot'.\n", "Completed augmentation for class 'cashews'.\n", "Completed augmentation for class 'cauliflower'.\n", "Completed augmentation for class 'celery'.\n", "Completed augmentation for class 'cereal'.\n", "Completed augmentation for class 'cheese'.\n", "Completed augmentation for class 'cherries'.\n", "Completed augmentation for class 'chicken'.\n", "Completed augmentation for class 'chickpeas'.\n", "Completed augmentation for class 'chocolate'.\n", "Completed augmentation for class 'chocolate_chips'.\n", "Completed augmentation for class 'chocolate_syrup'.\n", "Completed augmentation for class 'cilantro'.\n", "Completed augmentation for class 'cinnamon'.\n", "Completed augmentation for class 'clove'.\n", "Completed augmentation for class 'cocoa_powder'.\n", "Completed augmentation for class 'coconut'.\n", "Completed augmentation for class 'cookies'.\n", "Completed augmentation for class 'corn'.\n", "Completed augmentation for class 'cucumber'.\n", "Completed augmentation for class 'dates'.\n", "Completed augmentation for class 'eggplant'.\n", "Completed augmentation for class 'eggs'.\n", "Completed augmentation for class 'fish'.\n", "Completed augmentation for class 'garlic'.\n", "Completed augmentation for class 'ginger'.\n", "Completed augmentation for class 'grapes'.\n", "Completed augmentation for class 'honey'.\n", "Completed augmentation for class 'jalapeno'.\n", "Completed augmentation for class 'kidney_beans'.\n", "Completed augmentation for class 'lemon'.\n", "Completed augmentation for class 'mango'.\n", "Completed augmentation for class 'marshmallows'.\n", "Completed augmentation for class 'milk'.\n", "Completed augmentation for class 'mint'.\n", "Completed augmentation for class 'muffins'.\n", "Completed augmentation for class 'mushroom'.\n", "Completed augmentation for class 'noodles'.\n", "Completed augmentation for class 'nuts'.\n", "Completed augmentation for class 'oats'.\n", "Completed augmentation for class 'okra'.\n", "Completed augmentation for class 'olive'.\n", "Completed augmentation for class 'onion'.\n", "Completed augmentation for class 'orange'.\n", "Completed augmentation for class 'oreo_cookies'.\n", "Completed augmentation for class 'pasta'.\n", "Completed augmentation for class 'pear'.\n", "Completed augmentation for class 'pepper'.\n", "Completed augmentation for class 'pineapple'.\n", "Completed augmentation for class 'pistachios'.\n", "Completed augmentation for class 'pork'.\n", "Completed augmentation for class 'potato'.\n", "Completed augmentation for class 'pumpkin'.\n", "Completed augmentation for class 'radishes'.\n", "Completed augmentation for class 'raisins'.\n", "Completed augmentation for class 'red_chilies'.\n", "Completed augmentation for class 'rice'.\n", "Completed augmentation for class 'rosemary'.\n", "Completed augmentation for class 'salmon'.\n", "Completed augmentation for class 'salt'.\n", "Completed augmentation for class 'shrimp'.\n", "Completed augmentation for class 'spinach'.\n", "Completed augmentation for class 'strawberries'.\n", "Completed augmentation for class 'sugar'.\n", "Completed augmentation for class 'sweet_potato'.\n", "Completed augmentation for class 'tomato'.\n", "Completed augmentation for class 'vanilla_ice_cream'.\n", "Completed augmentation for class 'walnuts'.\n", "Completed augmentation for class 'watermelon'.\n", "Completed augmentation for class 'yogurt'.\n", "Dataset augmentation completed.\n" ] } ], "source": [ "import os\n", "import random\n", "from PIL import Image\n", "import imgaug as ia\n", "import imgaug.augmenters as iaa\n", "import numpy as np\n", "\n", "ia.seed(1)\n", "\n", "def augment_image(image_path, save_dir, augmentation, img_count):\n", " image = Image.open(image_path)\n", " # Convert image to RGB if it's not already in that format\n", " if image.mode != 'RGB':\n", " image = image.convert('RGB')\n", " image_np = np.array(image)\n", " augmented_image_np = augmentation(image=image_np)\n", " augmented_image = Image.fromarray(augmented_image_np)\n", " base_name = os.path.basename(image_path)\n", " new_image_path = os.path.join(save_dir, f\"{os.path.splitext(base_name)[0]}_aug_{img_count}.jpg\")\n", " augmented_image.save(new_image_path)\n", "\n", "\n", "def augment_class_images(class_dir, target_count=50):\n", " images = [file for file in os.listdir(class_dir) if file.lower().endswith('.jpg')]\n", " current_count = len(images)\n", " \n", " augmentation = iaa.Sequential([\n", " iaa.Fliplr(0.5), # Horizontally flip 50% of the images\n", " iaa.Crop(percent=(0, 0.1)), # Perform random crops\n", " \n", " # Apply affine transformations\n", " iaa.Affine(\n", " scale={\"x\": (0.8, 1.2), \"y\": (0.8, 1.2)},\n", " translate_percent={\"x\": (-0.2, 0.2), \"y\": (-0.2, 0.2)},\n", " rotate=(-25, 25),\n", " shear=(-8, 8)\n", " ),\n", " \n", " # Adjust brightness and contrast\n", " iaa.Multiply((0.8, 1.2)), # Change brightness (80-120% of original value)\n", " iaa.LinearContrast((0.75, 1.5)), # Strengthen or weaken the contrast in each image.\n", " \n", " # Apply color temperature changes\n", " iaa.Sequential([\n", " iaa.ChangeColorTemperature((1100, 10000)), # Simulate different color temperatures\n", " iaa.WithChannels(0, iaa.Add((10, 100))) # Optionally, add more red for warmth\n", " ], random_order=True) # Apply these changes in a random order\n", " ])\n", " \n", " if current_count < target_count:\n", " while current_count < target_count:\n", " for img_file in random.sample(images, min(len(images), target_count - current_count)):\n", " augment_image(os.path.join(class_dir, img_file), class_dir, augmentation, current_count)\n", " current_count += 1\n", " if current_count >= target_count:\n", " break\n", "\n", "def augment_dataset_if_needed(dataset_dir, target_count=50):\n", " for class_name in os.listdir(dataset_dir):\n", " class_dir = os.path.join(dataset_dir, class_name)\n", " if os.path.isdir(class_dir):\n", " augment_class_images(class_dir, target_count)\n", " print(f\"Completed augmentation for class '{class_name}'.\")\n", "\n", "# Specify the path to your dataset directory\n", "dataset_dir = r'C:\\Users\\Kiyo\\Desktop\\DL\\Project\\image_data\\initial_data'\n", "augment_dataset_if_needed(dataset_dir, 50)\n", "\n", "print(\"Dataset augmentation completed.\")\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.1" } }, "nbformat": 4, "nbformat_minor": 2 }