Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/C5 - Sequence Models/Week 3/Trigger word detection/train.py
Views: 4819
import numpy as np1from pydub import AudioSegment2import random3import sys4import io5import os6import glob7import IPython8from td_utils import *910from tensorflow.keras.callbacks import ModelCheckpoint11from tensorflow.keras.models import Model, load_model, Sequential12from tensorflow.keras.layers import Dense, Activation, Dropout, Input, Masking, TimeDistributed, LSTM, Conv1D13from tensorflow.keras.layers import GRU, Bidirectional, BatchNormalization, Reshape14from tensorflow.keras.optimizers import Adam1516Tx = 5511 # The number of time steps input to the model from the spectrogram17n_freq = 101 # Number of frequencies input to the model at each time step of the spectrogram1819Ty = 1375 # The number of time steps in the output of our model2021X = np.load("./XY_train/X0.npy")22Y = np.load("./XY_train/Y0.npy")2324X = np.concatenate((X, np.load("./XY_train/X1.npy")), axis=0)25Y = np.concatenate((Y, np.load("./XY_train/Y1.npy")), axis=0)2627Y = np.swapaxes(Y, 1, 2)2829# Load preprocessed dev set examples30X_dev = np.load("./XY_dev/X_dev.npy")31Y_dev = np.load("./XY_dev/Y_dev.npy")3233# GRADED FUNCTION: model3435def modelf(input_shape):36"""37Function creating the model's graph in Keras.3839Argument:40input_shape -- shape of the model's input data (using Keras conventions)4142Returns:43model -- Keras model instance44"""4546X_input = Input(shape = input_shape)4748### START CODE HERE ###4950# Step 1: CONV layer (≈4 lines)51X = Conv1D(196, kernel_size = 15, strides = 4)(X_input) # CONV1D52X = BatchNormalization()(X) # Batch normalization53X = Activation("relu")(X) # ReLu activation54X = Dropout(0.8)(X) # dropout (use 0.8)5556# Step 2: First GRU Layer (≈4 lines)57X = GRU(units = 128, return_sequences = True)(X) # GRU (use 128 units and return the sequences)58X = Dropout(0.8)(X) # dropout (use 0.8)59X = BatchNormalization()(X) # Batch normalization6061# Step 3: Second GRU Layer (≈4 lines)62X = GRU(units = 128, return_sequences = True)(X) # GRU (use 128 units and return the sequences)63X = Dropout(0.8)(X) # dropout (use 0.8)64X = BatchNormalization()(X) # Batch normalization65X = Dropout(0.8)(X) # dropout (use 0.8)6667# Step 4: Time-distributed dense layer (≈1 line)68X = TimeDistributed(Dense(1, activation = "sigmoid"))(X) # time distributed (sigmoid)6970### END CODE HERE ###7172model = Model(inputs = X_input, outputs = X)7374return model7576model = modelf(input_shape = (Tx, n_freq))77model.summary()7879opt = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, decay=0.01)80model.compile(loss='binary_crossentropy', optimizer=opt, metrics=["accuracy"])818283model.fit(X, Y, batch_size=20, epochs=100)8485loss, acc = model.evaluate(X_dev, Y_dev)86print("Dev set accuracy = ", acc)8788from tensorflow.keras.models import model_from_json8990json_file = open('./models/model_new3.json', 'r')91loaded_model_json = json_file.read()92json_file.close()93model = model_from_json(loaded_model_json)94model.load_weights('./models/model_new3.h5')9596