-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathkeras_simpl_densenet.py
67 lines (54 loc) · 2.18 KB
/
keras_simpl_densenet.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
# -*- coding: utf-8 -*-
__author__ = 'Gerald Schuller'
__copyright__ = 'G.S.'
"""
Simple program to implement a convolutional neural network using a dense network, for real time audio implementations.
Gerald Schuller, July 2017.
"""
from keras.models import Sequential
from keras.layers import Dense, Activation
from keras.layers import Conv1D
from keras.constraints import unit_norm
import numpy as np
import matplotlib.pyplot as plt
import sys
if sys.version_info[0] < 3:
# for Python 2
import cPickle as pickle
else:
# for Python 3
import pickle
from keras_simpl_convnet import generate_dummy_data
if __name__ == '__main__':
#Open weights file:
with open("convnetweights.pickle", 'rb') as weightfile:
weights=pickle.load(weightfile)
print("weights[0].shape=", weights[0].shape)
#Converting weights from Convolutional layer to Dense layer:
weights[0]=weights[0][:,:,0] #remove channel dimension for the weights
print("weights[0].shape=", weights[0].shape)
#Flipping the impulse response (filter) dimension from the convolutional layer
#(Convolution is correlation with flipped filter weights):
#weights[0]=np.flip(weights[0], axis=0)
filtlen=len(weights[0])
print("filtlen=", filtlen)
#Setup dense neural network model:
model = Sequential()
model.add(Dense(units=1, activation='linear', use_bias=False, input_shape=(filtlen,)) )
model.set_weights(weights)
X, Y = generate_dummy_data() # Acquire signal
siglen=len(X[0,:,0])
prediction=np.zeros(siglen-filtlen+1)
#Transpose the signal for the dense layer, to obtain a row vector to be multiplied from the left
#to the weight vector in Dense:
X=np.transpose(X,axes=(0,2,1))
#Loop over the entire signal in single steps until the end, in each step try to decode:
for n in range(siglen-filtlen):
#cut out the current signal block:
Xblock=X[0,:,n:n+filtlen]
#print("Xblock.shape=", Xblock.shape)
#Estimate the likelyhood of each character for the current block:
prediction[n]=model.predict(Xblock)
plt.plot(prediction)
plt.title('The Conv. Neural Network Output')
plt.show()