-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit.py
More file actions
48 lines (38 loc) · 1.47 KB
/
Copy pathsplit.py
File metadata and controls
48 lines (38 loc) · 1.47 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
44
45
46
47
48
# Script para divisão do dataset em treino, validação e teste.
import h5py
import numpy as np
from sklearn.model_selection import train_test_split
original_file_path = "database/Binary_2_5_dataset.h5"
new_file_path = "Binary_2_5_dataset.h5"
train_ratio = 0.8
val_ratio = 0.1
test_ratio = 0.1
with h5py.File(original_file_path, 'r') as original_file:
labels = original_file['labels'][:]
images = original_file['images'][:]
num_samples = len(labels)
indices = np.arange(num_samples)
train_indices, temp_indices, train_labels, temp_labels = train_test_split(
indices, labels,
test_size=(1 - train_ratio),
stratify=labels,
random_state=42
)
val_indices, test_indices, val_labels, test_labels = train_test_split(
temp_indices, temp_labels,
test_size=(test_ratio / (val_ratio + test_ratio)),
stratify=temp_labels,
random_state=42
)
split = np.zeros(num_samples, dtype=np.uint8)
split[train_indices] = 0 # Train
split[val_indices] = 1 # Val
split[test_indices] = 2 # Test
with h5py.File(new_file_path, 'w') as new_file:
new_file.create_dataset('images', data=images)
new_file.create_dataset('labels', data=labels)
new_file.create_dataset('split', data=split)
print(f"Novo arquivo salvo como {new_file_path}")
print(f"Divisão: {len(train_indices)} treino, "
f"{len(val_indices)} validação, "
f"{len(test_indices)} teste.")