-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathliver_test_train_data.py
More file actions
43 lines (31 loc) · 1.56 KB
/
liver_test_train_data.py
File metadata and controls
43 lines (31 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import os
import shutil
from sklearn.model_selection import train_test_split
dataset_path = '7272660'
output_base = 'Data/Liver'
train_dir = os.path.join(output_base, 'train')
test_dir = os.path.join(output_base, 'test')
os.makedirs(train_dir, exist_ok=True)
os.makedirs(test_dir, exist_ok=True)
class_dirs = ['Benign', 'Malignant', 'Normal']
for class_name in class_dirs:
image_folder = os.path.join(dataset_path, class_name, class_name, 'image') # e.g., 7272660/Benign/Benign/image
# Handle Normal class if it doesn't have nested structure
if not os.path.exists(image_folder):
image_folder = os.path.join(dataset_path, class_name, 'image') # fallback
if not os.path.exists(image_folder):
print(f"Image folder not found for class {class_name}, skipping.")
continue
images = [f for f in os.listdir(image_folder) if os.path.isfile(os.path.join(image_folder, f))]
if len(images) < 2:
print(f"Skipping {class_name}: not enough images ({len(images)})")
continue
# Create output folders
os.makedirs(os.path.join(train_dir, class_name), exist_ok=True)
os.makedirs(os.path.join(test_dir, class_name), exist_ok=True)
train_imgs, test_imgs = train_test_split(images, test_size=0.2, random_state=42)
for img in train_imgs:
shutil.copy(os.path.join(image_folder, img), os.path.join(train_dir, class_name, img))
for img in test_imgs:
shutil.copy(os.path.join(image_folder, img), os.path.join(test_dir, class_name, img))
print(f"{class_name}: {len(train_imgs)} train / {len(test_imgs)} test")