feat: add extraction script (#4)
Browse files- feat: add extraction script (59e608f626de5a2c8f7c9265caf84b6867bdac7c)
- extract.py +124 -0
extract.py
ADDED
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import random
|
3 |
+
from pathlib import Path
|
4 |
+
import shutil
|
5 |
+
import zipfile
|
6 |
+
import argparse
|
7 |
+
import tempfile
|
8 |
+
|
9 |
+
|
10 |
+
def reorganize_dataset(
|
11 |
+
zip_file: str,
|
12 |
+
*,
|
13 |
+
destination_dir: str = "./data",
|
14 |
+
split_ratio: float = 0.8,
|
15 |
+
random_seed: int = 42,
|
16 |
+
remove_zip: bool = False,
|
17 |
+
) -> None:
|
18 |
+
"""Reorganize dataset into train and test directories.
|
19 |
+
|
20 |
+
Args:
|
21 |
+
zip_file (str): Path to the zip file.
|
22 |
+
dest_dir (str, optional): Path to the destination directory. Defaults to './data'.
|
23 |
+
train_ratio (float, optional): Ratio of data to be used for training. Defaults to 0.7.
|
24 |
+
random_seed (int, optional): Random seed for reproducibility. Defaults to 42.
|
25 |
+
remove_zip (bool, optional): Whether to remove the zip file after extraction. Defaults to False.
|
26 |
+
|
27 |
+
Raises:
|
28 |
+
ValueError: If the source directory or destination directory does not exist.
|
29 |
+
ValueError: If the destination directory is not empty.
|
30 |
+
"""
|
31 |
+
# Convert the destination directory to a Path object
|
32 |
+
dest_dir = Path(destination_dir)
|
33 |
+
|
34 |
+
# Check if the source directory exists
|
35 |
+
if not Path(zip_file).is_file():
|
36 |
+
raise ValueError(f"Source directory '{zip_file}' does not exist.")
|
37 |
+
|
38 |
+
# Check if the destination directory is empty
|
39 |
+
if dest_dir.exists() and any(dest_dir.iterdir()):
|
40 |
+
raise ValueError(f"Destination directory '{dest_dir}' is not empty.")
|
41 |
+
|
42 |
+
# Set the random seed for reproducibility
|
43 |
+
random.seed(random_seed)
|
44 |
+
|
45 |
+
# Create train and test directories in the destination directory
|
46 |
+
train_dir = dest_dir / "train"
|
47 |
+
test_dir = dest_dir / "test"
|
48 |
+
train_dir.mkdir(parents=True, exist_ok=True)
|
49 |
+
test_dir.mkdir(parents=True, exist_ok=True)
|
50 |
+
|
51 |
+
# Extract the zip file to a temporary directory
|
52 |
+
with tempfile.TemporaryDirectory() as temp_dir:
|
53 |
+
with zipfile.ZipFile(zip_file, "r") as zip_ref:
|
54 |
+
zip_ref.extractall(temp_dir)
|
55 |
+
|
56 |
+
# Navigate to the animals directory inside the extracted files
|
57 |
+
root_dir = Path(temp_dir) / "animals" / "animals"
|
58 |
+
|
59 |
+
# Iterate through each animal directory
|
60 |
+
for animal_path in root_dir.iterdir():
|
61 |
+
if animal_path.is_dir() and animal_path.name not in ["train", "test"]:
|
62 |
+
# Create corresponding directories in train and test
|
63 |
+
(train_dir / animal_path.name).mkdir(parents=True, exist_ok=True)
|
64 |
+
(test_dir / animal_path.name).mkdir(parents=True, exist_ok=True)
|
65 |
+
|
66 |
+
# Get all files in the animal directory
|
67 |
+
files = [file for file in animal_path.iterdir() if file.is_file()]
|
68 |
+
random.shuffle(files)
|
69 |
+
|
70 |
+
# Split files into train and test sets
|
71 |
+
split_index = int(len(files) * split_ratio)
|
72 |
+
train_files = files[:split_index]
|
73 |
+
test_files = files[split_index:]
|
74 |
+
|
75 |
+
# Move files to train directory
|
76 |
+
for file in train_files:
|
77 |
+
dst = train_dir / animal_path.name / file.name
|
78 |
+
shutil.move(str(file), str(dst))
|
79 |
+
|
80 |
+
# Move files to test directory
|
81 |
+
for file in test_files:
|
82 |
+
dst = test_dir / animal_path.name / file.name
|
83 |
+
shutil.move(str(file), str(dst))
|
84 |
+
|
85 |
+
# Remove the zip file if the flag is set to True
|
86 |
+
if remove_zip:
|
87 |
+
os.remove(zip_file)
|
88 |
+
|
89 |
+
print(f"Dataset reorganization complete! (Seed: {random_seed})")
|
90 |
+
|
91 |
+
|
92 |
+
if __name__ == "__main__":
|
93 |
+
parser = argparse.ArgumentParser(description="Reorganize dataset.")
|
94 |
+
parser.add_argument("zip_file", type=str, help="Path to the zip file.")
|
95 |
+
parser.add_argument(
|
96 |
+
"--destination-dir",
|
97 |
+
type=str,
|
98 |
+
default="./data",
|
99 |
+
help="Path to the destination directory.",
|
100 |
+
)
|
101 |
+
parser.add_argument(
|
102 |
+
"--split-ratio",
|
103 |
+
type=float,
|
104 |
+
default=0.8,
|
105 |
+
help="Ratio of data to be used for training.",
|
106 |
+
)
|
107 |
+
parser.add_argument(
|
108 |
+
"--random-seed", type=int, default=42, help="Random seed for reproducibility."
|
109 |
+
)
|
110 |
+
parser.add_argument(
|
111 |
+
"--remove-zip",
|
112 |
+
action="store_true",
|
113 |
+
help="Whether to remove the source zip archive file after extraction.",
|
114 |
+
)
|
115 |
+
|
116 |
+
args = parser.parse_args()
|
117 |
+
|
118 |
+
reorganize_dataset(
|
119 |
+
args.zip_file,
|
120 |
+
destination_dir=args.destination_dir,
|
121 |
+
split_ratio=args.split_ratio,
|
122 |
+
random_seed=args.random_seed,
|
123 |
+
remove_zip=args.remove_zip,
|
124 |
+
)
|