Skip to content

Commit

Permalink
Keyword spotting network and test-benches created
Browse files Browse the repository at this point in the history
  • Loading branch information
Anirudh0707 committed Oct 14, 2020
1 parent fd38187 commit b85cda2
Show file tree
Hide file tree
Showing 13 changed files with 418 additions and 267 deletions.
79 changes: 52 additions & 27 deletions c_reference/src/dscnn.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,28 @@
int DSCNN_LR(float* output_signal, float* input_signal, unsigned in_T, unsigned in_channels, float* mean, float* var,
unsigned affine, float* gamma, float* beta, unsigned in_place, unsigned cnn_hidden, int cnn_padding, unsigned cnn_kernel_size,
const void* cnn_params, int cnn_activations){
unsigned out_T;
// BatchNorm
float* norm_out = (float*)malloc(in_T*in_channels*sizeof(float));
BatchNorm1d(norm_out, input_signal, in_T, in_channels,
mean, var, affine, gamma, beta, in_place, 0.00001);

// CNN
out_T = in_T - cnn_kernel_size + 2*cnn_padding + 1;
Conv1D_LR(output_signal, out_T, cnn_hidden, norm_out,
in_T, in_channels, cnn_padding, cnn_kernel_size,
cnn_params, cnn_activations);
free(norm_out);


unsigned out_T = in_T - cnn_kernel_size + 2*cnn_padding + 1;
if(in_place){
// BatchNorm
BatchNorm1d(0, input_signal, in_T, in_channels,
mean, var, affine, gamma, beta, in_place, 0.00001);
// CNN
Conv1D_LR(output_signal, out_T, cnn_hidden, input_signal,
in_T, in_channels, cnn_padding, cnn_kernel_size,
cnn_params, cnn_activations);
}
else{
// BatchNorm
float* norm_out = (float*)malloc(in_T*in_channels*sizeof(float));
BatchNorm1d(norm_out, input_signal, in_T, in_channels,
mean, var, affine, gamma, beta, in_place, 0.00001);
// CNN
Conv1D_LR(output_signal, out_T, cnn_hidden, norm_out,
in_T, in_channels, cnn_padding, cnn_kernel_size,
cnn_params, cnn_activations);
free(norm_out);
}
return 0;
}

Expand All @@ -36,33 +45,49 @@ int DSCNN_LR_Point_Depth(float* output_signal, float* input_signal, unsigned in_
float* act_out= (float*)malloc(in_T * (in_channels>>1) * sizeof(float));
TanhGate(act_out, input_signal, in_T, in_channels);

// Norm
in_channels >>= 1;
BatchNorm1d(0, act_out, in_T, in_channels,
mean, var, affine, gamma, beta, in_place, 0.00001);

// Depth CNN
out_T = in_T - depth_cnn_kernel_size + 2*depth_cnn_padding + 1;
float* depth_out = (float*)malloc(out_T * depth_cnn_hidden * sizeof(float));
Conv1D_Depth(depth_out, out_T, act_out,
in_T, in_channels, depth_cnn_padding, depth_cnn_kernel_size,
depth_cnn_params, depth_cnn_activations);
free(act_out);
float* depth_out;
if(in_place){
// Norm
BatchNorm1d(0, act_out, in_T, in_channels,
mean, var, affine, gamma, beta, in_place, 0.00001);
// Depth CNN
out_T = in_T - depth_cnn_kernel_size + 2*depth_cnn_padding + 1;
depth_out = (float*)malloc(out_T * depth_cnn_hidden * sizeof(float));
Conv1D_Depth(depth_out, out_T, act_out,
in_T, in_channels, depth_cnn_padding, depth_cnn_kernel_size,
depth_cnn_params, depth_cnn_activations);
free(act_out);
}
else{
// Norm
float* norm_out = (float*)malloc(in_T * in_channels * sizeof(float));
BatchNorm1d(norm_out, act_out, in_T, in_channels,
mean, var, affine, gamma, beta, in_place, 0.00001);
free(act_out);
// Depth CNN
out_T = in_T - depth_cnn_kernel_size + 2*depth_cnn_padding + 1;
depth_out = (float*)malloc(out_T * depth_cnn_hidden * sizeof(float));
Conv1D_Depth(depth_out, out_T, norm_out,
in_T, in_channels, depth_cnn_padding, depth_cnn_kernel_size,
depth_cnn_params, depth_cnn_activations);
free(norm_out);
}

// Point CNN
in_T = out_T;
out_T = in_T - point_cnn_kernel_size + 2*point_cnn_padding + 1;
float* point_out = (float*)malloc(out_T * point_cnn_hidden * sizeof(float));
Conv1D_LR(point_out, out_T, point_cnn_hidden, depth_out,
in_T, depth_cnn_hidden, point_cnn_padding, point_cnn_kernel_size,
point_cnn_params, point_cnn_activations);
in_T, depth_cnn_hidden, point_cnn_padding, point_cnn_kernel_size,
point_cnn_params, point_cnn_activations);
free(depth_out);

// Pool
in_T = out_T;
out_T = in_T - pool_kernel_size + 2*pool_padding + 1;
AvgPool1D(output_signal, out_T, point_out, in_T, point_cnn_hidden,
pool_padding, pool_kernel_size, pool_activation);
pool_padding, pool_kernel_size, pool_activation);
free(point_out);

return 0;
Expand Down
8 changes: 3 additions & 5 deletions c_reference/tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@ MODEL_DIR=../models
SRC_DIR=../src
IFLAGS = -I $(INCLUDE_DIR) -I $(MODEL_DIR)

all: test_rnn test_postcnn test_avg_pool test_conv1d test_conv1d_depth test_conv1d_lr test_conv1d_lr_depth test_dscnn_lr test_dscnn_lr_depth_point test_fastgrnn_lr test_rnnpool test_quantized_utils test_quantized_fastgrnn test_quantized_rnnpool test_quantized_mbconv test_quantized_face_detection test_quantized_face_detection_fast
all: test_avg_pool test_conv1d test_conv1d_depth test_conv1d_lr test_conv1d_lr_depth test_dscnn_lr test_dscnn_lr_depth_point test_fastgrnn_lr test_rnnpool test_quantized_utils test_quantized_fastgrnn test_quantized_rnnpool test_quantized_mbconv test_quantized_face_detection test_quantized_face_detection_fast test_keyword_spotting

KWS_DIR=kws
test_postcnn: $(KWS_DIR)/test_postcnn.c $(SRC_DIR)/conv_utils.o $(SRC_DIR)/utils.o $(SRC_DIR)/conv1d.o $(SRC_DIR)/dscnn.o
$(CC) -o $@ $^ $(IFLAGS) $(CFLAGS) -lm
test_rnn: $(KWS_DIR)/test_rnn.c $(SRC_DIR)/fastgrnn.o $(SRC_DIR)/utils.o
test_keyword_spotting: $(KWS_DIR)/test_keyword_spotting.c $(SRC_DIR)/conv_utils.o $(SRC_DIR)/utils.o $(SRC_DIR)/conv1d.o $(SRC_DIR)/dscnn.o $(SRC_DIR)/fastgrnn.o
$(CC) -o $@ $^ $(IFLAGS) $(CFLAGS) -lm

DSCNN_DIR=dscnn
Expand Down Expand Up @@ -63,7 +61,7 @@ test_quantized_face_detection_fast: $(FACE_DETECTION_DIR)/test_quantized_face_de
.PHONY: clean cleanest

clean:
rm -f *.o *.gch test_rnn test_postcnn test_avg_pool test_conv1d test_conv1d_depth test_conv1d_lr test_conv1d_lr_depth test_dscnn_lr test_dscnn_lr_depth_point test_fastgrnn_lr test_rnnpool test_quantized_utils test_quantized_fastgrnn test_quantized_rnnpool test_quantized_mbconv test_quantized_face_detection test_quantized_face_detection_fast
rm -f *.o *.gch test_avg_pool test_conv1d test_conv1d_depth test_conv1d_lr test_conv1d_lr_depth test_dscnn_lr test_dscnn_lr_depth_point test_fastgrnn_lr test_rnnpool test_quantized_utils test_quantized_fastgrnn test_quantized_rnnpool test_quantized_mbconv test_quantized_face_detection test_quantized_face_detection_fast test_keyword_spotting

cleanest: clean
rm *~
14 changes: 14 additions & 0 deletions c_reference/tests/kws/keyword_spotting_io_1.h

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions c_reference/tests/kws/keyword_spotting_io_2.h

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions c_reference/tests/kws/keyword_spotting_io_3.h

Large diffs are not rendered by default.

12 changes: 0 additions & 12 deletions c_reference/tests/kws/postcnn_io.h

This file was deleted.

18 changes: 11 additions & 7 deletions c_reference/tests/kws/postcnn_params.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

#define I_T 101
#define O_T 97
#define I_F 400
#define O_F 41
#define DEPTH_FILT 5
#define POOL_FILT 2
#define POINT_FILT 1
#define POST_CNN_I_F 400
#define POST_CNN_O_F 41
#define POST_CNN_DEPTH_FILT 5
#define POST_CNN_DEPTH_PAD 2
#define POST_CNN_DEPTH_ACT 0
#define POST_CNN_POOL 2
#define POST_CNN_POOL_PAD 0
#define POST_CNN_POOL_ACT 0
#define POST_CNN_POINT_FILT 1
#define POST_CNN_POINT_PAD 0
#define POST_CNN_POINT_ACT 0
#define LOW_RANK 50

static float CNN2_BNORM_MEAN[200] = {0.015971357, 6.501892e-05, 0.038588, 0.097353816, 0.015376935, 0.015602097, 0.036061652, 0.03898482, 0.039924037, 0.006442192, 0.03593987, 0.04222, 0.07892465, 0.040109057, 0.04670995, 0.034607653, 0.08764101, 0.0654985, 0.05729792, 0.016125392, 0.04164587, 0.067503765, -0.028087385, -0.015071113, -0.059104428, 0.016970858, 0.066376075, 0.08096571, 0.07588531, -0.052501034, 0.059453476, 0.051389035, 0.068797044, 0.07448322, -0.039696753, 0.035946004, -0.0014653725, 0.088247605, 0.041575737, 0.10277397, 0.0526724, -0.036430024, 0.045154247, 0.025572386, -0.03798233, -0.03787984, 0.038794607, 0.069571935, 0.04857156, -0.0071943738, -0.032641422, 0.09272896, 0.051213413, 0.027016614, 0.016180614, 0.06295424, 0.037129566, 0.032467406, -0.025120424, 0.030453552, 0.06195704, -0.018342853, 0.01071218, 0.08289012, -0.0020247104, -0.025325423, 0.09067498, 0.08771331, -0.00087628997, 0.058319252, -0.008090766, 0.017746149, 0.018517531, 0.086093254, -0.027057227, -0.016622052, 0.046957616, 0.09430719, 0.034838118, -0.012873425, 0.037685357, 0.06542477, 0.019381318, 0.056773286, 0.013210703, 0.061603934, 0.058845002, 0.038058348, 0.059714716, 0.08331196, 0.07923841, 0.062044818, 0.0026301693, 0.06795837, -0.0030768516, 0.06081296, 0.007542504, 0.023446025, 0.029961232, 0.036956295, 0.038154498, 0.0475312, -0.002375686, 0.06024979, 0.008005025, 0.077196926, 0.0789222, 0.08032208, -0.017859492, 0.09035916, 0.0018038531, 0.09352108, 0.01635931, 0.019340234, 0.08410177, 0.039363027, 0.072246365, 0.014267202, 0.09342878, 0.035540942, -0.024538206, -0.029167939, 0.022879561, 0.038776945, -0.008962599, 0.028289083, 0.07435437, 0.033813186, 0.03850727, 0.029347735, -0.0625054, -0.040992733, 0.07381808, 0.066816054, 0.0124026975, 0.071189724, 0.030857489, 0.0691046, 0.0816209, 0.055097148, 0.0014903225, 0.03434464, -0.0059871743, 0.056997567, 0.03338638, 0.006343891, -0.012877238, -0.06268586, -0.021703077, 0.04223155, -0.0071792454, 0.042931616, 0.07306048, 0.069892146, 0.04687559, 0.070037335, 0.04052685, 0.014894321, 0.0052992613, 0.095167615, -0.013672871, 0.041835114, 0.029129915, 0.044560347, 0.050107688, 0.053555574, 0.072749875, 0.078805596, 0.010685162, 0.0071339705, 0.064971276, 0.00079534744, -0.017808538, 0.03917341, 0.08676169, 0.07253845, -0.05193372, -0.019195765, 0.0065121413, 0.03828087, -0.0030095147, 0.04439866, 0.077847764, 0.033887736, -0.0059973625, 0.031676162, -0.06647171, 0.044209484, 0.053383026, -0.009077284, -0.046332154, -0.0028125723, 0.09074347, 0.08587855, -0.0076064896, 0.0626982, -0.021182764, 0.064857244, 0.02505869, -0.017837577};
Expand Down
23 changes: 23 additions & 0 deletions c_reference/tests/kws/precnn_params.h

Large diffs are not rendered by default.

12 changes: 0 additions & 12 deletions c_reference/tests/kws/rnn_io.h

This file was deleted.

Loading

0 comments on commit b85cda2

Please sign in to comment.