CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
amanchadha

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: amanchadha/coursera-deep-learning-specialization
Path: blob/master/C5 - Sequence Models/Week 3/Trigger word detection/train.py
Views: 4819
1
import numpy as np
2
from pydub import AudioSegment
3
import random
4
import sys
5
import io
6
import os
7
import glob
8
import IPython
9
from td_utils import *
10
11
from tensorflow.keras.callbacks import ModelCheckpoint
12
from tensorflow.keras.models import Model, load_model, Sequential
13
from tensorflow.keras.layers import Dense, Activation, Dropout, Input, Masking, TimeDistributed, LSTM, Conv1D
14
from tensorflow.keras.layers import GRU, Bidirectional, BatchNormalization, Reshape
15
from tensorflow.keras.optimizers import Adam
16
17
Tx = 5511 # The number of time steps input to the model from the spectrogram
18
n_freq = 101 # Number of frequencies input to the model at each time step of the spectrogram
19
20
Ty = 1375 # The number of time steps in the output of our model
21
22
X = np.load("./XY_train/X0.npy")
23
Y = np.load("./XY_train/Y0.npy")
24
25
X = np.concatenate((X, np.load("./XY_train/X1.npy")), axis=0)
26
Y = np.concatenate((Y, np.load("./XY_train/Y1.npy")), axis=0)
27
28
Y = np.swapaxes(Y, 1, 2)
29
30
# Load preprocessed dev set examples
31
X_dev = np.load("./XY_dev/X_dev.npy")
32
Y_dev = np.load("./XY_dev/Y_dev.npy")
33
34
# GRADED FUNCTION: model
35
36
def modelf(input_shape):
37
"""
38
Function creating the model's graph in Keras.
39
40
Argument:
41
input_shape -- shape of the model's input data (using Keras conventions)
42
43
Returns:
44
model -- Keras model instance
45
"""
46
47
X_input = Input(shape = input_shape)
48
49
### START CODE HERE ###
50
51
# Step 1: CONV layer (≈4 lines)
52
X = Conv1D(196, kernel_size = 15, strides = 4)(X_input) # CONV1D
53
X = BatchNormalization()(X) # Batch normalization
54
X = Activation("relu")(X) # ReLu activation
55
X = Dropout(0.8)(X) # dropout (use 0.8)
56
57
# Step 2: First GRU Layer (≈4 lines)
58
X = GRU(units = 128, return_sequences = True)(X) # GRU (use 128 units and return the sequences)
59
X = Dropout(0.8)(X) # dropout (use 0.8)
60
X = BatchNormalization()(X) # Batch normalization
61
62
# Step 3: Second GRU Layer (≈4 lines)
63
X = GRU(units = 128, return_sequences = True)(X) # GRU (use 128 units and return the sequences)
64
X = Dropout(0.8)(X) # dropout (use 0.8)
65
X = BatchNormalization()(X) # Batch normalization
66
X = Dropout(0.8)(X) # dropout (use 0.8)
67
68
# Step 4: Time-distributed dense layer (≈1 line)
69
X = TimeDistributed(Dense(1, activation = "sigmoid"))(X) # time distributed (sigmoid)
70
71
### END CODE HERE ###
72
73
model = Model(inputs = X_input, outputs = X)
74
75
return model
76
77
model = modelf(input_shape = (Tx, n_freq))
78
model.summary()
79
80
opt = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, decay=0.01)
81
model.compile(loss='binary_crossentropy', optimizer=opt, metrics=["accuracy"])
82
83
84
model.fit(X, Y, batch_size=20, epochs=100)
85
86
loss, acc = model.evaluate(X_dev, Y_dev)
87
print("Dev set accuracy = ", acc)
88
89
from tensorflow.keras.models import model_from_json
90
91
json_file = open('./models/model_new3.json', 'r')
92
loaded_model_json = json_file.read()
93
json_file.close()
94
model = model_from_json(loaded_model_json)
95
model.load_weights('./models/model_new3.h5')
96