Datasets:
Download preprocessing.py from Man1103/PetImages: direct link, hf CLI and curl.
- Browser
- Download file 2.27 kB
-
https://huggingface.co/datasets/Man1103/PetImages/resolve/main/preprocessing.py
- Command line
-
hf download hf://datasets/Man1103/PetImages/preprocessing.py
-
curl -L -o preprocessing.py https://huggingface.co/datasets/Man1103/PetImages/resolve/main/preprocessing.py
2.27 kB
| import os | |
| from PIL import Image | |
| def fetching_selected_data(test_split: int, max_len: int = 1000000000): | |
| cats_images_path = r"/content/data/PetImages/Cat" | |
| dogs_images_path = r"/content/data/PetImages/Dog" | |
| total_len = min(len(os.listdir(cats_images_path)), len(os.listdir(dogs_images_path)), max_len) | |
| training_set_count = int(total_len * (1 - (test_split/100))) | |
| testing_set_count = total_len - training_set_count | |
| def _get_image_files(parent_dir: str, first_count: int, max_count: int): | |
| all_files = [] | |
| count = 0 | |
| for child in os.listdir(parent_dir): | |
| grandchild = os.path.join(parent_dir, child) | |
| if not os.path.isdir(grandchild): | |
| count += 1 | |
| if os.path.isdir(grandchild): | |
| continue | |
| final_full_path = os.path.join(parent_dir, grandchild) | |
| all_files.append(final_full_path) | |
| else: | |
| _get_image_files(grandchild) | |
| last_count = first_count + max_count | |
| return all_files[first_count:last_count] | |
| def _get_input_labels(first_count: int, max_count: int): | |
| cats_list = _get_image_files(cats_images_path, first_count=first_count, max_count=max_count) | |
| dogs_list = _get_image_files(dogs_images_path, first_count=first_count, max_count=max_count) | |
| img_files_list = cats_list + dogs_list | |
| X, y = [], [] | |
| for file_path in img_files_list: | |
| img_file_data = Image.open(file_path).convert("RGB") | |
| X.append(img_file_data) | |
| if 'cat' in file_path.lower(): | |
| y.append(0) | |
| elif 'dog' in file_path.lower(): | |
| y.append(1) | |
| return X, y | |
| train_first_count = 0 | |
| train_max_count = training_set_count | |
| X_train, y_train = _get_input_labels(train_first_count, train_max_count) | |
| test_first_count = training_set_count | |
| test_max_count = testing_set_count | |
| X_test, y_test = _get_input_labels(test_first_count, test_max_count) | |
| return X_train, X_test, y_train, y_test | |
| (fetched_X_train, fetched_X_test, | |
| fetched_y_train, fetched_y_test) = fetching_selected_data(test_split=0.25, max_len=5000) |