-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtf_util.py
95 lines (60 loc) · 3.17 KB
/
tf_util.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import tensorflow as tf
# define function to apply cqt ratio
def tf_broadcast(tensor, shape):
return (tensor + tf.zeros(shape, dtype=tensor.dtype))
# use CQT ration to give bar data different weight
def apply_cqt_ratio(cqt_tensor_in, ratio_tensor_in):
ratio_tensor_in_4d = tf.reshape(ratio_tensor_in, tf.shape(cqt_tensor_in[:,0:1,0:1,:]))
ratio_tensor_in_expanded = tf_broadcast(ratio_tensor_in_4d, tf.shape(cqt_tensor_in[:,:,:,:]))
return (tf.multiply(cqt_tensor_in, ratio_tensor_in_expanded))
#define function to calculate diff y layer
# note: input tensor must be 4-D data
def get_matx_2_layer_tf(matx_data_in):
matx_layer_0 = matx_data_in
matx_data_pady = tf.pad(matx_data_in,
paddings=[[0,0], [0,0], [1,0], [0,0]],
mode='CONSTANT',
name='tf_diff2_pady',
constant_values=0
)[:, :, :-1, :]
#)[:, :matx_data_in.get_shape()[1], :, :]
matx_layer_1 = matx_data_in - matx_data_pady
matx_layer_1_concat = tf.concat([tf.zeros_like(matx_layer_1)[:,:,0:1,:],
matx_layer_1[:,:,1:,:]],
axis=2)
matx_layer_all = tf.concat([matx_layer_0, matx_layer_1_concat],
axis=-1,
name='tf_diff2_concat')
return matx_layer_all
#print ('Diff-2 function define done.')
# define function to calculate 3 layer (add diif x/y layer)
# note: input tensor must be 4-D data
def get_matx_3_layer_tf(matx_data_in):
matx_layer_0 = matx_data_in
matx_data_pady = tf.pad(matx_data_in,
paddings=[[0,0], [0,0], [1,0], [0,0]],
mode='CONSTANT',
name='tf_diff3_pady',
constant_values=0
)[:, :, :-1, :]
#)[:, :matx_data_in.get_shape()[1], :, :]
matx_layer_1 = matx_data_in - matx_data_pady
matx_layer_1_concat = tf.concat([tf.zeros_like(matx_layer_1)[:,:,0:1,:],
matx_layer_1[:,:,1:,:]],
axis=2)
matx_data_padx = tf.pad(matx_data_in,
paddings=[[0,0], [1,0], [0,0], [0,0]],
mode='CONSTANT',
name='tf_diff3_padx',
constant_values=0
)[:, :-1, :, :]
#)[:, :, :matx_data_in.get_shape()[2], :]
matx_layer_2 = matx_data_in - matx_data_padx
matx_layer_2_concat = tf.concat([tf.zeros_like(matx_layer_1)[:,0:1,:,:],
matx_layer_1[:,1:,:,:]],
axis=1)
matx_layer_all = tf.concat([matx_layer_0, matx_layer_1_concat, matx_layer_2_concat],
axis=-1,
name='tf_diff3_concat')
return matx_layer_all
#print ('Diff-3 function define done.')