In [2]:
import tensorflow as tf
import os
import matplotlib.pyplot as plt
import numpy as np
import soundfile
import glob

In [57]:
os.environ.setdefault('GLUE_SETTINGS_MODULE', 'settings')


Out[57]:
'settings'

In [58]:
import netops as gnet
from flowfairy.conf import settings

In [4]:
batch_size = settings.BATCH_SIZE
samplerate = settings.SAMPLERATE

In [5]:
x = tf.placeholder(tf.float32, [1, samplerate])
cls = tf.placeholder(tf.int32, 1)

In [6]:
with tf.variable_scope('network'):
    net = gnet.conv_net(x, cls, None)


conv1 Tensor("network/causal_conv1_1/glu_conv1_1/mul:0", shape=(1, 16384, 1, 4), dtype=float32)
conv1_d1  Tensor("network/glu_conv1_d1/mul:0", shape=(1, 16384, 1, 4), dtype=float32)
conv1_d2  Tensor("network/glu_conv1_d2/mul:0", shape=(1, 16384, 1, 4), dtype=float32)
conv1_d4  Tensor("network/glu_conv1_d4/mul:0", shape=(1, 16384, 1, 4), dtype=float32)
conv1_concat Tensor("network/concat:0", shape=(1, 16384, 1, 12), dtype=float32)
conv2:  Tensor("network/MaxPool2D/MaxPool:0", shape=(1, 8192, 1, 8), dtype=float32)
embedded: Tensor("network/embedding/concat:0", shape=(1, 8192, 1, 10), dtype=float32)
conv3:  Tensor("network/glu_conv3_2/mul:0", shape=(1, 8192, 1, 16), dtype=float32)
d2sp:  Tensor("network/DepthToSpace:0", shape=(1, 32768, 4, 1), dtype=float32)
conv4:  Tensor("network/glu_conv4_2/mul:0", shape=(1, 16384, 1, 16), dtype=float32)
conv5:  Tensor("network/glu_conv5/mul:0", shape=(1, 16384, 1, 256), dtype=float32)
out:  Tensor("network/Reshape_1:0", shape=(1, 16384, 256), dtype=float32)

In [7]:
saver = tf.train.Saver()

In [11]:
sess = tf.InteractiveSession()

In [9]:
fname = 'dilated3.checkpoint'
#saver.restore(sess, 'logs/dilated3.ckpt-70000')
saver.restore(sess, tf.train.latest_checkpoint('logs', latest_filename=fname))


INFO:tensorflow:Restoring parameters from /home/s123859/flowfairy/examples/sine_fix/logs/dilated3.ckpt-79000

In [10]:
coll = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES)
c1uw = coll[0]

In [11]:
def plot_filter(var, flt):
    plt.plot(var.eval()[:,0,0,flt])

In [12]:
#plot_filter(c1uw, 0)
#plot_filter(c1uw, 1)
#plot_filter(c1uw, 2)
#plot_filter(c1uw, 3)
#plt.show()

In [13]:
for v in enumerate(coll):
    print(v)


(0, <tf.Variable 'network/conv1_1_unit/weights:0' shape=(128, 1, 1, 4) dtype=float32_ref>)
(1, <tf.Variable 'network/conv1_1_unit/BatchNorm/beta:0' shape=(4,) dtype=float32_ref>)
(2, <tf.Variable 'network/conv1_1_gate/weights:0' shape=(128, 1, 1, 4) dtype=float32_ref>)
(3, <tf.Variable 'network/conv1_1_gate/BatchNorm/beta:0' shape=(4,) dtype=float32_ref>)
(4, <tf.Variable 'network/conv1_d1_unit/weights:0' shape=(128, 1, 4, 4) dtype=float32_ref>)
(5, <tf.Variable 'network/conv1_d1_unit/biases:0' shape=(4,) dtype=float32_ref>)
(6, <tf.Variable 'network/conv1_d1_gate/weights:0' shape=(128, 1, 4, 4) dtype=float32_ref>)
(7, <tf.Variable 'network/conv1_d1_gate/biases:0' shape=(4,) dtype=float32_ref>)
(8, <tf.Variable 'network/conv1_d2_unit/weights:0' shape=(128, 1, 4, 4) dtype=float32_ref>)
(9, <tf.Variable 'network/conv1_d2_unit/biases:0' shape=(4,) dtype=float32_ref>)
(10, <tf.Variable 'network/conv1_d2_gate/weights:0' shape=(128, 1, 4, 4) dtype=float32_ref>)
(11, <tf.Variable 'network/conv1_d2_gate/biases:0' shape=(4,) dtype=float32_ref>)
(12, <tf.Variable 'network/conv1_d4_unit/weights:0' shape=(128, 1, 4, 4) dtype=float32_ref>)
(13, <tf.Variable 'network/conv1_d4_unit/biases:0' shape=(4,) dtype=float32_ref>)
(14, <tf.Variable 'network/conv1_d4_gate/weights:0' shape=(128, 1, 4, 4) dtype=float32_ref>)
(15, <tf.Variable 'network/conv1_d4_gate/biases:0' shape=(4,) dtype=float32_ref>)
(16, <tf.Variable 'network/conv2_1_unit/weights:0' shape=(128, 1, 12, 8) dtype=float32_ref>)
(17, <tf.Variable 'network/conv2_1_unit/biases:0' shape=(8,) dtype=float32_ref>)
(18, <tf.Variable 'network/conv2_1_gate/weights:0' shape=(128, 1, 12, 8) dtype=float32_ref>)
(19, <tf.Variable 'network/conv2_1_gate/biases:0' shape=(8,) dtype=float32_ref>)
(20, <tf.Variable 'network/conv2_2_unit/weights:0' shape=(128, 1, 8, 8) dtype=float32_ref>)
(21, <tf.Variable 'network/conv2_2_unit/biases:0' shape=(8,) dtype=float32_ref>)
(22, <tf.Variable 'network/conv2_2_gate/weights:0' shape=(128, 1, 8, 8) dtype=float32_ref>)
(23, <tf.Variable 'network/conv2_2_gate/biases:0' shape=(8,) dtype=float32_ref>)
(24, <tf.Variable 'network/embedding/embedding:0' shape=(200, 2) dtype=float32_ref>)
(25, <tf.Variable 'network/conv3_1_unit/weights:0' shape=(128, 1, 10, 16) dtype=float32_ref>)
(26, <tf.Variable 'network/conv3_1_unit/biases:0' shape=(16,) dtype=float32_ref>)
(27, <tf.Variable 'network/conv3_1_gate/weights:0' shape=(128, 1, 10, 16) dtype=float32_ref>)
(28, <tf.Variable 'network/conv3_1_gate/biases:0' shape=(16,) dtype=float32_ref>)
(29, <tf.Variable 'network/conv3_2_unit/weights:0' shape=(128, 1, 16, 16) dtype=float32_ref>)
(30, <tf.Variable 'network/conv3_2_unit/biases:0' shape=(16,) dtype=float32_ref>)
(31, <tf.Variable 'network/conv3_2_gate/weights:0' shape=(128, 1, 16, 16) dtype=float32_ref>)
(32, <tf.Variable 'network/conv3_2_gate/biases:0' shape=(16,) dtype=float32_ref>)
(33, <tf.Variable 'network/conv4_1_unit/weights:0' shape=(128, 1, 8, 16) dtype=float32_ref>)
(34, <tf.Variable 'network/conv4_1_unit/biases:0' shape=(16,) dtype=float32_ref>)
(35, <tf.Variable 'network/conv4_1_gate/weights:0' shape=(128, 1, 8, 16) dtype=float32_ref>)
(36, <tf.Variable 'network/conv4_1_gate/biases:0' shape=(16,) dtype=float32_ref>)
(37, <tf.Variable 'network/conv4_2_unit/weights:0' shape=(128, 1, 28, 16) dtype=float32_ref>)
(38, <tf.Variable 'network/conv4_2_unit/biases:0' shape=(16,) dtype=float32_ref>)
(39, <tf.Variable 'network/conv4_2_gate/weights:0' shape=(128, 1, 28, 16) dtype=float32_ref>)
(40, <tf.Variable 'network/conv4_2_gate/biases:0' shape=(16,) dtype=float32_ref>)
(41, <tf.Variable 'network/conv5_unit/weights:0' shape=(2, 1, 16, 256) dtype=float32_ref>)
(42, <tf.Variable 'network/conv5_unit/biases:0' shape=(256,) dtype=float32_ref>)
(43, <tf.Variable 'network/conv5_gate/weights:0' shape=(2, 1, 16, 256) dtype=float32_ref>)
(44, <tf.Variable 'network/conv5_gate/biases:0' shape=(256,) dtype=float32_ref>)

In [14]:
embedding = coll[24]
#print(embedding)
emb = embedding.eval()
print(emb.shape)
plt.plot(emb)
plt.savefig('embedding.eps', format='eps', dpi=1000)
plt.show()

print(embedding.eval()[50])
print(embedding.eval()[64])


(200, 2)
[-0.25976446 -0.06300437]
[-0.30728337  0.08011562]

In [15]:
def classify(val):
    val = (val-np.min(val))/(np.max(val)-np.min(val))
    return np.floor(val*255)

def getid(frq):
    limits = settings.FREQUENCY_LIMIT
    count = settings.CLASS_COUNT
    step = (limits[1] - limits[0]) // count
    return (frq-limits[0])//step

def getfrq(fid):
    limits = settings.FREQUENCY_LIMIT
    count = settings.CLASS_COUNT
    step = (limits[1] - limits[0]) // count
    return limits[0] + step*fid

In [16]:
with tf.variable_scope('network', reuse=True):
    tnet = gnet.conv_net(x, cls, None, is_training=True)


conv1 Tensor("network_1/causal_conv1_1/glu_conv1_1/mul:0", shape=(1, 16384, 1, 4), dtype=float32)
conv1_d1  Tensor("network_1/glu_conv1_d1/mul:0", shape=(1, 16384, 1, 4), dtype=float32)
conv1_d2  Tensor("network_1/glu_conv1_d2/mul:0", shape=(1, 16384, 1, 4), dtype=float32)
conv1_d4  Tensor("network_1/glu_conv1_d4/mul:0", shape=(1, 16384, 1, 4), dtype=float32)
conv1_concat Tensor("network_1/concat:0", shape=(1, 16384, 1, 12), dtype=float32)
conv2:  Tensor("network_1/MaxPool2D/MaxPool:0", shape=(1, 8192, 1, 8), dtype=float32)
embedded: Tensor("network_1/embedding/concat:0", shape=(1, 8192, 1, 10), dtype=float32)
conv3:  Tensor("network_1/glu_conv3_2/mul:0", shape=(1, 8192, 1, 16), dtype=float32)
d2sp:  Tensor("network_1/DepthToSpace:0", shape=(1, 32768, 4, 1), dtype=float32)
conv4:  Tensor("network_1/glu_conv4_2/mul:0", shape=(1, 16384, 1, 16), dtype=float32)
conv5:  Tensor("network_1/glu_conv5/mul:0", shape=(1, 16384, 1, 256), dtype=float32)
out:  Tensor("network_1/Reshape_1:0", shape=(1, 16384, 256), dtype=float32)

In [17]:
pred = tf.argmax(net, 2)

In [18]:
tpred = tf.argmax(tnet, 2)

In [19]:
frqid = 98
frq = getfrq(frqid)
print(frq)

bfrq = getfrq(195)
print(bfrq)

amp = np.random.rand(2) * 3 + 1
print(amp)

myrange = np.arange(samplerate) * 2 * np.pi / samplerate
mysine = np.sin(myrange * frq * amp[0])
mysine2 = np.sin(myrange * bfrq * amp[1])
noise = np.random.uniform(-0.5, 0.5, samplerate)

#plt.plot(mysine)
#plt.show()


396
590
[ 2.12955805  1.56227273]

In [20]:
b = np.array([mysine+mysine2+noise])
y = np.array([frqid])
#b=np.tile(classify(mysine+mysine2),(64,1))
#y=np.arange(136,200)
run = sess.run(pred, feed_dict={x: b, cls: y})

In [21]:
plt.plot(classify(b[0][1000:1100]), 'g', alpha=0.5)
plt.plot(run[0][1000:1100], 'r')
plt.plot(classify(mysine)[1000:1100], 'b', alpha=0.5)
#plt.plot(classify(mysine2)[1000:1100], 'g', alpha=0.5)
plt.show()



In [22]:
print(tf.train.Saver.last_checkpoints)
print(saver.last_checkpoints)
l = tf.train.latest_checkpoint('logs', latest_filename='GGLU_e2_256.checkpoint')
step = l[l.rfind('-')+1:]
print(step)


<property object at 0x7f45233ca278>
[]
130000

In [10]:
unc = np.random.rand(2,20)
unc = unc[0,:,None]
noise = np.random.rand(20, 1)
unc = unc + noise
print(unc.shape)


(20, 1)

In [85]:
basedir = '/home/sorson/audio_dataset/preprocessed_22050/'

#npz = list(glob.glob(os.path.join(basedir, 'test', '*.npz')))[0]
npz = '/home/sorson/audio_dataset/preprocessed_22050/train/7565-101287-0066.npz'
mid = np.array(0)

f = np.load(npz)
it = f['speaker_class']

        
#print(it)

#print(f.items())

spath = str(f['audio_file']).replace('//', '/')
data, sr = soundfile.read(spath)

#print(data[292265:])
print((data + np.abs(data.min())).max())
ol = settings.OUTPUTLEN
lidx = 292265
ridx = lidx + ol
print(ridx)

rip = data[292265:ridx]

print('rip', rip)
print(rip.min())
rip += rip.min()
print('rip+min', rip)
print('min',rip.min())
print(np.abs(rip).max())
rip = rip / np.abs(rip).max()
print(f'max {rip.max()}')
#(2483) do not match the number of lines in metadata (2338)
#print(data[:,None].shape)
rip.dtype


0.971984863281
303289
rip [  2.74658203e-04   2.13623047e-04   6.10351562e-05 ...,  -6.10351562e-05
   3.05175781e-05  -1.22070312e-04]
-0.000762939453125
rip+min [-0.00048828 -0.00054932 -0.0007019  ..., -0.00082397 -0.00073242
 -0.00088501]
min -0.00152587890625
0.00152587890625
max 0.0
Out[85]:
dtype('float64')

In [93]:
print(sorted([(2, 'x'), (1,'y')]))


[(1, 'y'), (2, 'x')]

In [91]:
npz = glob.glob(os.path.join(basedir, 'train', '*.npz'))

d = {}
n2g = ['M', 'F']
for npz in npz:
    f = np.load(npz)
    spkid = int(f['speaker_class'])
    gender = n2g[f['gender']]
    d[spkid] = gender
    
with open('idtogender.tsv', 'w+') as f:
    f.write('spkid\tgender\n')
    for spkid, gender in sorted(d.items()):
        f.write(f'{spkid}\t{gender}\n')

In [92]:
with open('idtogender.tsv', 'a+') as f:
    for i in range(2483):
        if i not in d:
            d[i] = 'U'
            f.write(f'{i}\tU\n')

In [94]:
for i in range(2483):
    if i not in d:
        d[i] = 'U'

In [95]:
with open('idtogender.tsv', 'w+') as f:
    f.write('spkid\tgender\n')
    for spkid, gender in sorted(d.items()):
        f.write(f'{spkid}\t{gender}\n')

In [71]:
r = tf.random_uniform([1], maxval=2, dtype=tf.int32)


print(r.eval())


[1]

In [5]:
r = np.arange(100)/100
frq = 5
s1 = np.sin(r * np.pi * 2 * frq)
s2 = np.sin(r * np.pi * 2 * (frq + 2))
plt.plot(s1)

plt.plot(s1+s2)
plt.show()



In [9]:
r = np.arange(100)/100
frq = 5
s1 = np.sin(r * np.pi * 2 * frq)
s2 = np.sin(r * np.pi * 2 * (frq ) + 1)
plt.plot(s1)

plt.plot(s1)
#plt.plot(s2)
plt.show()