-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset_fixing.py
37 lines (28 loc) · 1.27 KB
/
dataset_fixing.py
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
import os
import shutil
import random
dataset_path= './chest_xray/'
new_dataset_path = './chest_xray_new/'
if not os.path.exists(new_dataset_path):
for split in ['train', 'val', 'test']:
for cls in ['NORMAL', 'PNEUMONIA']:
os.makedirs(f'{new_dataset_path}/{split}/{cls}', exist_ok=True)
for cls in ['NORMAL', 'PNEUMONIA']:
all_files = []
for split in ['train', 'val', 'test']:
source_folder = f'{dataset_path}/{split}/{cls}'
files = os.listdir(source_folder)
all_files.extend([(file, source_folder) for file in files])
random.shuffle(all_files)
train_files = all_files[:int(len(all_files)*0.7)]
val_files = all_files[int(len(all_files)*0.7):int(len(all_files)*0.8)]
test_files = all_files[int(len(all_files)*0.8):]
for file, source_folder in train_files:
dest = f'{new_dataset_path}/train/{cls}/{file}'
shutil.copy(f'{source_folder}/{file}', dest)
for file, source_folder in val_files:
dest = f'{new_dataset_path}/val/{cls}/{file}'
shutil.copy(f'{source_folder}/{file}', dest)
for file, source_folder in test_files:
dest = f'{new_dataset_path}/test/{cls}/{file}'
shutil.copy(f'{source_folder}/{file}', dest)