application_vnd.jupyter.stderr_output_2.png text_plain_output_1.png
import os

import random

import shutil

import numpy as np
import pandas as pd
import os

basedir = '/kaggle/input/5-flower-types-classification-dataset/flower_images'

source_path_orchid = os.path.join(basedir, 'Orchid')
source_path_sunflower = os.path.join(basedir, 'Sunflower')
source_path_tulip = os.path.join(basedir, 'Tulip')
source_path_lotus = os.path.join(basedir, 'Lotus')
source_path_lilly = os.path.join(basedir, 'Lilly')

import shutil
root_dir = '/kaggle/working/fiveflowers'
if os.path.exists(root_dir):
    shutil.rmtree(root_dir)
def create_train_val_dirs(root_path):
    os.makedirs(os.path.join(root_path, 'training'))
    os.makedirs(os.path.join(f'{root_path}/training', 'Lilly'))
    os.makedirs(os.path.join(f'{root_path}/training', 'Lotus'))
    os.makedirs(os.path.join(f'{root_path}/training', 'Orchid'))
    os.makedirs(os.path.join(f'{root_path}/training', 'Sunflower'))
    os.makedirs(os.path.join(f'{root_path}/training', 'Tulip'))
    os.makedirs(os.path.join(root_path, 'validation'))
    os.makedirs(os.path.join(f'{root_path}/validation', 'Lilly'))
    os.makedirs(os.path.join(f'{root_path}/validation', 'Lotus'))
    os.makedirs(os.path.join(f'{root_path}/validation', 'Orchid'))
    os.makedirs(os.path.join(f'{root_path}/validation', 'Sunflower'))
    os.makedirs(os.path.join(f'{root_path}/validation', 'Tulip'))
try:
    create_train_val_dirs(root_path=root_dir)
except FileExistsError:

import random
from shutil import copyfile
def split_data(SOURCE_DIR, TRAINING_DIR, VALIDATION_DIR, SPLIT_SIZE):
    shuffled_source = random.sample(os.listdir(SOURCE_DIR), len(os.listdir(SOURCE_DIR)))
    training_number = int(len(shuffled_source)) * SPLIT_SIZE
    i = 0
    Target = TRAINING_DIR
    for item in shuffled_source:
        item_path = os.path.join(SOURCE_DIR, item)
        shutil.copy(item_path, os.path.join(Target, item))
        i += 1
        if i == training_number:
            Target = VALIDATION_DIR

Lilly_SOURCE_DIR = '/kaggle/input/5-flower-types-classification-dataset/flower_images/Lilly'
Lotus_SOURCE_DIR = '/kaggle/input/5-flower-types-classification-dataset/flower_images/Lotus'
Orchid_SOURCE_DIR = '/kaggle/input/5-flower-types-classification-dataset/flower_images/Orchid'
Sunflower_SOURCE_DIR = '/kaggle/input/5-flower-types-classification-dataset/flower_images/Sunflower'
Tulip_SOURCE_DIR = '/kaggle/input/5-flower-types-classification-dataset/flower_images/Tulip'
TRAINING_DIR = 'kaggle/working/fiveflowers/training/'
VALIDATION_DIR = 'kaggle/working/fiveflowers/validation/'
TRAINING_Lilly_DIR = os.path.join(TRAINING_DIR, 'Lilly')
VALIDATION_Lilly_DIR = os.path.join(VALIDATION_DIR, 'Lilly')
TRAINING_Lotus_DIR = os.path.join(TRAINING_DIR, 'Lotus')
VALIDATION_Lotus_DIR = os.path.join(VALIDATION_DIR, 'Lotus')
TRAINING_Orchid_DIR = os.path.join(TRAINING_DIR, 'Orchid')
VALIDATION_Orchid_DIR = os.path.join(VALIDATION_DIR, 'Orchid')
TRAINING_Sunflower_DIR = os.path.join(TRAINING_DIR, 'Sunflower')
VALIDATION_Sunflower_DIR = os.path.join(VALIDATION_DIR, 'Sunflower')
TRAINING_Tulip_DIR = os.path.join(TRAINING_DIR, 'Tulip')
VALIDATION_Tulip_DIR = os.path.join(VALIDATION_DIR, 'Tulip')
print(TRAINING_Lilly_DIR)
split_size = 0.9
split_data(Lilly_SOURCE_DIR, TRAINING_Lilly_DIR, VALIDATION_Lilly_DIR, split_size)
split_data(Lotus_SOURCE_DIR, TRAINING_Lotus_DIR, VALIDATION_Lotus_DIR, split_size)
split_data(Orchid_SOURCE_DIR, TRAINING_Orchid_DIR, VALIDATION_Orchid_DIR, split_size)
split_data(Sunflower_SOURCE_DIR, TRAINING_Sunflower_DIR, VALIDATION_Sunflower_DIR, split_size)
split_data(Tulip_SOURCE_DIR, TRAINING_Tulip_DIR, VALIDATION_Tulip_DIR, split_size)
print(f"\n\nOriginal lilly's directory has {len(os.listdir(Lilly_SOURCE_DIR))} images")
print(f"Original lotus's directory has {len(os.listdir(Lotus_SOURCE_DIR))} images")
print(f"Original orchid's directory has {len(os.listdir(Orchid_SOURCE_DIR))} images")
print(f"Original sunflower's directory has {len(os.listdir(Sunflower_SOURCE_DIR))} images")
print(f"Original tulip's directory has {len(os.listdir(Tulip_SOURCE_DIR))} images")
print(f'There are {len(os.listdir(TRAINING_Lilly_DIR))} images of lilly for training')
print(f'There are {len(os.listdir(TRAINING_Lotus_DIR))} images of lotus for training')
print(f'There are {len(os.listdir(VALIDATION_Lilly_DIR))} images of lilly for validation')
print(f'There are {len(os.listdir(VALIDATION_Lotus_DIR))} images of lotus for validation')