-
Notifications
You must be signed in to change notification settings - Fork 28
/
gen_train_data_test_data.py
107 lines (88 loc) · 2.64 KB
/
gen_train_data_test_data.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
import cv2
import numpy as np
from os.path import join
from os import listdir
from keras.utils import np_utils
species = [
"blasti",
"bonegl",
"brhkyt",
"cbrtsh",
"cmnmyn",
"gretit",
"hilpig",
"himbul",
"himgri",
"hsparo",
"indvul",
"jglowl",
"lbicrw",
"mgprob",
"rebimg",
"wcrsrt",
]
datapath = "./"
N_CLASSES = 16 # Number of classes
def gen_data():
"""Generate numpy files for training, validation and
testing.
"""
X_train = []
Y_train = []
X_valid = []
Y_valid = []
X_test = []
Y_test = []
count = 0
for bird_specie in species:
# Samples Location
train_data = join(datapath, "train/" + bird_specie)
val_data = join(datapath, "valid/" + bird_specie)
test_data = join(datapath, "test/" + bird_specie)
# Samples Files
train_files = listdir(train_data)
valid_files = listdir(val_data)
test_files = listdir(test_data)
for img_file in train_files:
im = join(train_data, img_file)
img = cv2.imread(im)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = cv2.resize(img, (416, 416))
X_train.append(img)
Y_train += [count]
for img_file in test_files:
im = join(test_data, img_file)
img = cv2.imread(im)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = cv2.resize(img, (416, 416))
X_test.append(img)
Y_test += [count]
for img_file in valid_files:
im = join(val_data, img_file)
img = cv2.imread(im)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = cv2.resize(img, (416, 416))
X_valid.append(img)
Y_valid += [count]
count += 1
X_train = np.asarray(X_train).astype("float32")
X_train /= 255
Y_train = np.asarray(Y_train)
X_valid = np.asarray(X_valid).astype("float32")
X_valid /= 255
Y_valid = np.asarray(Y_valid)
X_test = np.asarray(X_test).astype("float32")
X_test /= 255
Y_test = np.asarray(Y_test)
return X_train, Y_train, X_valid, Y_valid, X_test, Y_test
if __name__ == "__main__":
x_train, y_train, x_valid, y_valid, x_test, y_test = gen_data()
y_train = np_utils.to_categorical(y_train, N_CLASSES)
y_valid = np_utils.to_categorical(y_train, N_CLASSES)
y_test = np_utils.to_categorical(y_test, N_CLASSES)
np.save("X_train.npy", x_train)
np.save("Y_train.npy", y_train)
np.save("X_valid.npy", x_valid)
np.save("Y_valid.npy", y_valid)
np.save("X_test.npy", x_test)
np.save("Y_test.npy", y_test)