-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_dataset.py
More file actions
executable file
·49 lines (36 loc) · 1.64 KB
/
Copy pathbuild_dataset.py
File metadata and controls
executable file
·49 lines (36 loc) · 1.64 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
49
import configparser
import os
import h5py
import pickle
import numpy as np
from sklearn.model_selection import train_test_split
config = configparser.RawConfigParser()
config.read_file(open(r'./configuration.txt'))
def write_hdf5(arr, outfile):
with h5py.File(outfile, "w") as f:
f.create_dataset("image", data=arr, dtype=arr.dtype)
def get_datasets(path, train, label, output_dir):
# Load training images
train_images = np.array(pickle.load(open(os.path.join(path, train), "rb")))
train_images = np.transpose(train_images, (0,3,1,2))
# Load image labels
labels = np.array(pickle.load(open(os.path.join(path, label), "rb")))
labels = labels[..., 0]
labels[labels > 0] = 1
# Split data:
seed = int(config.get('settings', 'seed'))
X, X_test, y, y_test = train_test_split(train_images, labels, test_size=0.1, random_state=seed)
X_train, X_valid, y_train, y_valid = train_test_split(X, y, test_size=0.2, random_state=seed)
# Write data to disk:
write_hdf5(X_train, os.path.join(path, "X_train.hdf5"))
write_hdf5(y_train, os.path.join(path, "y_train.hdf5"))
write_hdf5(X_valid, os.path.join(path, "X_valid.hdf5"))
write_hdf5(y_valid, os.path.join(path, "y_valid.hdf5"))
write_hdf5(X_test, os.path.join(path, "X_test.hdf5"))
write_hdf5(y_test, os.path.join(path, "y_test.hdf5"))
if __name__ == '__main__':
data_dir = config.get('data paths', 'data_dir')
file_train = config.get('data paths', 'file_train')
file_label = config.get('data paths', 'file_label')
output_dir = config.get('data paths', 'output_dir')
get_datasets(data_dir, file_train, file_label, output_dir)