forked from dawidkopczyk/speech_recognition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
56 lines (43 loc) · 2.19 KB
/
main.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
from models import deep, deep_cnn
import numpy as np
from sklearn.metrics import accuracy_score
from keras.callbacks import EarlyStopping
from dataset import DatasetGenerator
DIR = 'input' # unzipped train and test data
INPUT_SHAPE = (177,98,1)
BATCH = 32
EPOCHS = 15
LABELS = 'yes no up'.split()
NUM_CLASSES = len(LABELS)
#==============================================================================
# Prepare data
#==============================================================================
dsGen = DatasetGenerator(label_set=LABELS)
# Load DataFrame with paths/labels for training and validation data
# and paths for testing data
df = dsGen.load_data(DIR)
dsGen.apply_train_test_split(test_size=0.3, random_state=2018)
dsGen.apply_train_val_split(val_size=0.2, random_state=2018)
#==============================================================================
# Train
#==============================================================================
model = deep_cnn(INPUT_SHAPE, NUM_CLASSES)
model.compile(optimizer='Adam', loss='categorical_crossentropy', metrics=['acc'])
callbacks = [EarlyStopping(monitor='val_acc', patience=4, verbose=1, mode='max')]
history = model.fit_generator(generator=dsGen.generator(BATCH, mode='train'),
steps_per_epoch=int(np.ceil(len(dsGen.df_train)/BATCH)),
epochs=EPOCHS,
verbose=1,
callbacks=callbacks,
validation_data=dsGen.generator(BATCH, mode='val'),
validation_steps=int(np.ceil(len(dsGen.df_val)/BATCH)))
#==============================================================================
# Predict
#==============================================================================
y_pred_proba = model.predict_generator(dsGen.generator(BATCH, mode='test'),
int(np.ceil(len(dsGen.df_test)/BATCH)),
verbose=1)
y_pred = np.argmax(y_pred_proba, axis=1)
y_true = dsGen.df_test['label_id'].values
acc_score = accuracy_score(y_true, y_pred)
print(acc_score)