Upload folder using huggingface_hub
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- .DS_Store +0 -0
- data_splitting.py +89 -0
- test/Apple/Apple (1133).jpeg +3 -0
- test/Apple/Apple (1153).jpeg +3 -0
- test/Apple/Apple (12).png +3 -0
- test/Apple/Apple (1348).jpeg +3 -0
- test/Apple/Apple (1413).jpeg +3 -0
- test/Apple/Apple (1415).jpeg +3 -0
- test/Apple/Apple (1423).jpeg +3 -0
- test/Apple/Apple (1526).jpeg +3 -0
- test/Apple/Apple (1555).jpeg +3 -0
- test/Apple/Apple (1624).jpeg +3 -0
- test/Apple/Apple (1729).jpeg +3 -0
- test/Apple/Apple (1750).jpeg +3 -0
- test/Apple/Apple (1789).jpeg +3 -0
- test/Apple/Apple (1809).jpeg +3 -0
- test/Apple/Apple (1976).jpeg +3 -0
- test/Apple/Apple (233).jpeg +3 -0
- test/Apple/Apple (27).jpeg +3 -0
- test/Apple/Apple (28).jpeg +3 -0
- test/Apple/Apple (420).jpeg +3 -0
- test/Apple/Apple (545).jpeg +3 -0
- test/Banana/Banana (2044).jpeg +3 -0
- test/Banana/Banana (2067).jpeg +3 -0
- test/Banana/Banana (2120).jpeg +3 -0
- test/Banana/Banana (2121).jpeg +3 -0
- test/Banana/Banana (2179).jpeg +3 -0
- test/Banana/Banana (2279).jpeg +3 -0
- test/Banana/Banana (2291).jpeg +3 -0
- test/Banana/Banana (2448).jpeg +3 -0
- test/Banana/Banana (2580).jpeg +3 -0
- test/Banana/Banana (2712).jpeg +3 -0
- test/Banana/Banana (2727).jpeg +3 -0
- test/Banana/Banana (2845).jpeg +3 -0
- test/Banana/Banana (2928).jpeg +3 -0
- test/Banana/Banana (3362).jpeg +3 -0
- test/Banana/Banana (3449).jpeg +3 -0
- test/Banana/Banana (3597).jpeg +3 -0
- test/Banana/Banana (3652).jpeg +3 -0
- test/Banana/Banana (3813).jpeg +3 -0
- test/Banana/Banana (3901).jpeg +3 -0
- test/Banana/Banana (3939).jpeg +3 -0
- test/Grape/Grape (1073).jpeg +3 -0
- test/Grape/Grape (1090).jpeg +3 -0
- test/Grape/Grape (1222).jpeg +3 -0
- test/Grape/Grape (1266).jpeg +3 -0
- test/Grape/Grape (1335).jpeg +3 -0
- test/Grape/Grape (1470).jpeg +3 -0
- test/Grape/Grape (1521).jpeg +3 -0
- test/Grape/Grape (1575).jpeg +3 -0
.DS_Store
ADDED
Binary file (6.15 kB). View file
|
|
data_splitting.py
ADDED
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
'''
|
2 |
+
|
3 |
+
This code is a data preprocessing script that is used for creating a dataset to train, validate and test an image classification model. Specifically, it creates a dataset of fruit images, including Apples, Bananas, Grapes, Mangos, and Strawberries.
|
4 |
+
|
5 |
+
The script first defines the class names and number of images per class by counting the number of files in the directory for each fruit. It then creates three directories, one for each dataset split, i.e., training, validation, and testing. These directories are created if they don't exist.
|
6 |
+
|
7 |
+
The script then creates subdirectories in each of the three main directories for each fruit class. This step creates a hierarchical structure for the dataset, where each fruit class has a folder in each dataset split directory.
|
8 |
+
|
9 |
+
Next, the script collects all the image paths for each fruit class, storing them in a list. It then shuffles the paths randomly to ensure that the data is not biased in any way.
|
10 |
+
|
11 |
+
The script then calculates the size of each dataset split based on the total number of images and predefined ratios. The training dataset is the largest, comprising 97% of the total data, while the validation and testing datasets comprise 2% and 1%, respectively.
|
12 |
+
|
13 |
+
The script then creates three separate lists, one for each dataset split, containing tuples with the old and new paths for each image. The new paths are generated by concatenating the class folder, the image filename, and the corresponding dataset split directory.
|
14 |
+
|
15 |
+
Finally, the script moves each image from its old path to its new path in the appropriate dataset split directory using the os.rename() function. It then removes the old directories for each fruit class, as they are no longer needed.
|
16 |
+
|
17 |
+
The script outputs the total size of the data, as well as the size of each dataset split. Once the script finishes running, the dataset is ready to use for training, validating, and testing an image classification model.
|
18 |
+
|
19 |
+
'''
|
20 |
+
|
21 |
+
|
22 |
+
import os
|
23 |
+
import numpy as np
|
24 |
+
from glob import glob
|
25 |
+
from tqdm import tqdm
|
26 |
+
|
27 |
+
# Define class names and number of images per class
|
28 |
+
class_names = ['Apple', 'Banana', 'Grape', 'Mango', 'Strawberry']
|
29 |
+
n_images_per_class = len(os.listdir(f"./{class_names[0]}"))
|
30 |
+
|
31 |
+
# Define train, valid, and test directories and create them if they don't exist
|
32 |
+
train_dir = "./train"
|
33 |
+
valid_dir = "./valid"
|
34 |
+
test_dir = "./test"
|
35 |
+
|
36 |
+
for directory in [train_dir, valid_dir, test_dir]:
|
37 |
+
if not os.path.exists(directory):
|
38 |
+
os.makedirs(directory)
|
39 |
+
|
40 |
+
# Create subdirectories for each class in train, valid, and test directories
|
41 |
+
for name in class_names:
|
42 |
+
for directory in [train_dir, valid_dir, test_dir]:
|
43 |
+
class_path = os.path.join(directory, name)
|
44 |
+
if not os.path.exists(class_path):
|
45 |
+
os.makedirs(class_path)
|
46 |
+
|
47 |
+
# Collect all image paths for each class
|
48 |
+
all_class_paths = [glob(f"./{name}/*") for name in class_names]
|
49 |
+
|
50 |
+
# Define training, validation, and testing size
|
51 |
+
total_size = sum([len(paths) for paths in all_class_paths])
|
52 |
+
|
53 |
+
train_ratio = 0.97
|
54 |
+
valid_ratio = 0.02
|
55 |
+
test_ratio = 0.01
|
56 |
+
|
57 |
+
train_size = int(total_size * train_ratio)
|
58 |
+
valid_size = int(total_size * valid_ratio)
|
59 |
+
test_size = int(total_size * test_ratio)
|
60 |
+
|
61 |
+
train_images_per_class = int(n_images_per_class * train_ratio)
|
62 |
+
valid_images_per_class = int(n_images_per_class * valid_ratio)
|
63 |
+
test_images_per_class = int(n_images_per_class * test_ratio)
|
64 |
+
|
65 |
+
print("Total Data Size : {}".format(total_size))
|
66 |
+
print("Training Size : {}".format(train_size))
|
67 |
+
print("Validation Size : {}".format(valid_size))
|
68 |
+
print("Testing Size : {}\n".format(test_size))
|
69 |
+
|
70 |
+
# Shuffle image paths for each class
|
71 |
+
for paths in all_class_paths:
|
72 |
+
np.random.shuffle(paths)
|
73 |
+
|
74 |
+
# Define lists of (old_path, new_path) tuples for training, validation, and testing images
|
75 |
+
train_images = [(path, os.path.join(train_dir, path.split('/')[-2], path.split('/')[-1])) for paths in all_class_paths for path in paths[:train_images_per_class]]
|
76 |
+
valid_images = [(path, os.path.join(valid_dir, path.split('/')[-2], path.split('/')[-1])) for paths in all_class_paths for path in paths[train_images_per_class: train_images_per_class + valid_images_per_class]]
|
77 |
+
test_images = [(path, os.path.join(test_dir, path.split('/')[-2], path.split('/')[-1])) for paths in all_class_paths for path in paths[train_images_per_class+valid_images_per_class: train_images_per_class + valid_images_per_class + test_images_per_class]]
|
78 |
+
|
79 |
+
# Move images to their new directories
|
80 |
+
for images, data_type in [(train_images, "Training"), (valid_images, "Validation"), (test_images, "Testing")]:
|
81 |
+
for (old_path, new_path) in tqdm(images, desc=data_type + " Data"):
|
82 |
+
os.rename(old_path, new_path)
|
83 |
+
|
84 |
+
# Remove the old directories
|
85 |
+
for directory in class_names:
|
86 |
+
os.rmdir("./" + directory)
|
87 |
+
|
88 |
+
# Print confirmation message
|
89 |
+
print("ALL DONE!!")
|
test/Apple/Apple (1133).jpeg
ADDED
![]() |
Git LFS Details
|
test/Apple/Apple (1153).jpeg
ADDED
![]() |
Git LFS Details
|
test/Apple/Apple (12).png
ADDED
![]() |
Git LFS Details
|
test/Apple/Apple (1348).jpeg
ADDED
![]() |
Git LFS Details
|
test/Apple/Apple (1413).jpeg
ADDED
![]() |
Git LFS Details
|
test/Apple/Apple (1415).jpeg
ADDED
![]() |
Git LFS Details
|
test/Apple/Apple (1423).jpeg
ADDED
![]() |
Git LFS Details
|
test/Apple/Apple (1526).jpeg
ADDED
![]() |
Git LFS Details
|
test/Apple/Apple (1555).jpeg
ADDED
![]() |
Git LFS Details
|
test/Apple/Apple (1624).jpeg
ADDED
![]() |
Git LFS Details
|
test/Apple/Apple (1729).jpeg
ADDED
![]() |
Git LFS Details
|
test/Apple/Apple (1750).jpeg
ADDED
![]() |
Git LFS Details
|
test/Apple/Apple (1789).jpeg
ADDED
![]() |
Git LFS Details
|
test/Apple/Apple (1809).jpeg
ADDED
![]() |
Git LFS Details
|
test/Apple/Apple (1976).jpeg
ADDED
![]() |
Git LFS Details
|
test/Apple/Apple (233).jpeg
ADDED
![]() |
Git LFS Details
|
test/Apple/Apple (27).jpeg
ADDED
![]() |
Git LFS Details
|
test/Apple/Apple (28).jpeg
ADDED
![]() |
Git LFS Details
|
test/Apple/Apple (420).jpeg
ADDED
![]() |
Git LFS Details
|
test/Apple/Apple (545).jpeg
ADDED
![]() |
Git LFS Details
|
test/Banana/Banana (2044).jpeg
ADDED
![]() |
Git LFS Details
|
test/Banana/Banana (2067).jpeg
ADDED
![]() |
Git LFS Details
|
test/Banana/Banana (2120).jpeg
ADDED
![]() |
Git LFS Details
|
test/Banana/Banana (2121).jpeg
ADDED
![]() |
Git LFS Details
|
test/Banana/Banana (2179).jpeg
ADDED
![]() |
Git LFS Details
|
test/Banana/Banana (2279).jpeg
ADDED
![]() |
Git LFS Details
|
test/Banana/Banana (2291).jpeg
ADDED
![]() |
Git LFS Details
|
test/Banana/Banana (2448).jpeg
ADDED
![]() |
Git LFS Details
|
test/Banana/Banana (2580).jpeg
ADDED
![]() |
Git LFS Details
|
test/Banana/Banana (2712).jpeg
ADDED
![]() |
Git LFS Details
|
test/Banana/Banana (2727).jpeg
ADDED
![]() |
Git LFS Details
|
test/Banana/Banana (2845).jpeg
ADDED
![]() |
Git LFS Details
|
test/Banana/Banana (2928).jpeg
ADDED
![]() |
Git LFS Details
|
test/Banana/Banana (3362).jpeg
ADDED
![]() |
Git LFS Details
|
test/Banana/Banana (3449).jpeg
ADDED
![]() |
Git LFS Details
|
test/Banana/Banana (3597).jpeg
ADDED
![]() |
Git LFS Details
|
test/Banana/Banana (3652).jpeg
ADDED
![]() |
Git LFS Details
|
test/Banana/Banana (3813).jpeg
ADDED
![]() |
Git LFS Details
|
test/Banana/Banana (3901).jpeg
ADDED
![]() |
Git LFS Details
|
test/Banana/Banana (3939).jpeg
ADDED
![]() |
Git LFS Details
|
test/Grape/Grape (1073).jpeg
ADDED
![]() |
Git LFS Details
|
test/Grape/Grape (1090).jpeg
ADDED
![]() |
Git LFS Details
|
test/Grape/Grape (1222).jpeg
ADDED
![]() |
Git LFS Details
|
test/Grape/Grape (1266).jpeg
ADDED
![]() |
Git LFS Details
|
test/Grape/Grape (1335).jpeg
ADDED
![]() |
Git LFS Details
|
test/Grape/Grape (1470).jpeg
ADDED
![]() |
Git LFS Details
|
test/Grape/Grape (1521).jpeg
ADDED
![]() |
Git LFS Details
|
test/Grape/Grape (1575).jpeg
ADDED
![]() |
Git LFS Details
|