-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset_processing.py
150 lines (113 loc) · 4.7 KB
/
dataset_processing.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import os, re
import numpy as np
import cv2 as cv
import tensorflow as tf
from sklearn.model_selection import train_test_split
from preprocessing import ImageTransformer
def preprocess_data_tf(im, label):
im = tf.cast(im, tf.float32)
im = im/127.5
im = im - 1
return im, label
def preprocess_data(im, label):
im = im.astype(np.float32)
im = im/127.5
im = im - 1
return im, label
def standardize_image_size(X, img_dims):
m = X.shape[0]
X_resized = np.zeros((m,img_dims[1],img_dims[0],3),dtype='uint8')
for i in range(m):
if X[i].shape[0:2] != (img_dims[1],img_dims[0]):
X_resized[i] = ImageTransformer.resize(X[i],img_dims)
else:
X_resized[i] = X[i]
return X_resized
def set_dataset(case_dir, img_dims, dataset_foldername):
datasets_dir = os.path.join(case_dir,'Datasets',dataset_foldername)
x_cont = []
y_cont = []
for folder in os.listdir(datasets_dir): # for each dataset contained in the Dataset folder
f = open(os.path.join(datasets_dir,folder,'labels.dat'))
data = f.read()
# Read labels
labels = [int(label) for label in re.findall('\n*.*(\d)\n*',data)]
# Read samples
samples = re.findall('\n*(.*),\d\n*',data)
samples_path = [os.path.join(datasets_dir,folder,sample) for sample in samples]
# Generate X,y datasets
m = len(samples_path)
x = np.zeros((m,img_dims[1],img_dims[0],3),dtype='uint8')
y = np.zeros((m,),dtype='uint8')
for i,sample in enumerate(samples_path):
# X-array storage
img = cv.imread(sample)
x[i,:,:,:] = ImageTransformer.resize(img,img_dims)
# Label storage
y[i] = labels[i] - 1
x_cont.append(x)
y_cont.append(y)
m = [item.shape[0] for item in x_cont]
X = np.zeros([sum(m),*x.shape[1:]],dtype='uint8')
Y = np.zeros([sum(m),],dtype='uint8')
for i in range(len(m)):
X[sum(m[0:i]):sum(m[0:i+1]),:,:,:] = x_cont[i]
Y[sum(m[0:i]):sum(m[0:i+1]),] = y_cont[i]
return X, Y
def read_test_datasets(case_dir, img_dims, dataset_ID=None, return_filepaths=False):
if dataset_ID == None:
dataset_dir = [case_dir]
else:
dataset_dir = [os.path.join(case_dir,'Dataset_{}'.format(i)) for i in dataset_ID]
X = []
y = []
for folder in dataset_dir:
f = open(os.path.join(folder,'labels_test.dat'))
data = f.read()
# Read labels
labels = [int(label) for label in re.findall('\n*.*(\d)\n*',data)]
# Read samples
samples = re.findall('\n*(.*),\d\n*', data)
samples_path = [os.path.join(folder,sample) for sample in samples]
# Generate X,y datasets
m = len(samples_path)
X = np.zeros((m,img_dims[1],img_dims[0],3),dtype='uint8')
y = np.zeros((m,), dtype=int)
for i, sample in enumerate(samples_path):
# X-array storage
img = cv.imread(sample)
X[i,:,:,:] = ImageTransformer.resize(img,img_dims)
# Label storage
y[i] = labels[i] - 1
if return_filepaths:
return X, y, samples
else:
return X, y
def get_test_dataset(case_dir, img_dims):
# Read original datasets
X, y = set_dataset(case_dir,img_dims)
return (X,y)
def get_datasets(case_dir, img_dims, train_size):
# Read original datasets
X, y = set_dataset(case_dir,img_dims,dataset_foldername='Training')
X_train, X_val, y_train, y_val = train_test_split(X,y,train_size=train_size,shuffle=True)
X_cv, X_test, y_cv, y_test = train_test_split(X_val,y_val,train_size=0.75,shuffle=True)
data_train = (X_train, y_train)
data_cv = (X_cv, y_cv)
data_test = (X_test, y_test)
return data_train, data_cv, data_test
def create_dataset_pipeline(dataset, is_train=True, num_threads=8, prefetch_buffer=100, batch_size=32):
X, y = dataset
y_oh = tf.one_hot(y,depth=9)
dataset_tensor = tf.data.Dataset.from_tensor_slices((X, y_oh))
if is_train:
dataset_tensor = dataset_tensor.shuffle(buffer_size=X.shape[0]).repeat()
dataset_tensor = dataset_tensor.map(preprocess_data_tf, num_parallel_calls=num_threads)
dataset_tensor = dataset_tensor.batch(batch_size)
dataset_tensor = dataset_tensor.prefetch(prefetch_buffer)
return dataset_tensor
def get_tensorflow_datasets(data_train, data_cv, data_test, batch_size=32):
dataset_train = create_dataset_pipeline(data_train,is_train=True,batch_size=batch_size)
dataset_cv = create_dataset_pipeline(data_cv,is_train=False,batch_size=1)
dataset_test = preprocess_data_tf(data_test[0],tf.one_hot(data_test[1],depth=9))
return dataset_train, dataset_cv, dataset_test