-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathCNN_recurrent.py
204 lines (153 loc) · 7.82 KB
/
CNN_recurrent.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import tensorflow as tf
import tensorflow_compression as tfc
import functions
def one_step_rnn(tensor, state_c, state_h, Height, Width, num_filters, scale, kernal, act):
tensor = tf.expand_dims(tensor, axis=1)
cell = functions.ConvLSTMCell(shape=[Height // scale, Width // scale], activation=act,
filters=num_filters, kernel=kernal)
state = tf.nn.rnn_cell.LSTMStateTuple(state_c, state_h)
tensor, state = tf.nn.dynamic_rnn(cell, tensor, initial_state=state, dtype=tensor.dtype)
state_c, state_h = state
tensor = tf.squeeze(tensor, axis=1)
return tensor, state_c, state_h
def MV_analysis(tensor, num_filters, out_filters, Height, Width, c_state, h_state, act):
"""Builds the analysis transform."""
with tf.variable_scope("MV_analysis", reuse=tf.AUTO_REUSE):
with tf.variable_scope("layer_0"):
layer = tfc.SignalConv2D(
num_filters, (3, 3), corr=True, strides_down=2, padding="same_zeros",
use_bias=True, activation=tfc.GDN())
tensor = layer(tensor)
with tf.variable_scope("layer_1"):
layer = tfc.SignalConv2D(
num_filters, (3, 3), corr=True, strides_down=2, padding="same_zeros",
use_bias=True, activation=tfc.GDN())
tensor = layer(tensor)
with tf.variable_scope("recurrent"):
tensor, c_state_out, h_state_out = one_step_rnn(tensor, c_state, h_state,
Height, Width, num_filters,
scale=4, kernal=[3, 3], act=act)
with tf.variable_scope("layer_2"):
layer = tfc.SignalConv2D(
num_filters, (3, 3), corr=True, strides_down=2, padding="same_zeros",
use_bias=True, activation=tfc.GDN())
tensor = layer(tensor)
with tf.variable_scope("layer_3"):
layer = tfc.SignalConv2D(
out_filters, (3, 3), corr=True, strides_down=2, padding="same_zeros",
use_bias=True, activation=None)
tensor = layer(tensor)
return tensor, c_state_out, h_state_out
def MV_synthesis(tensor, num_filters, Height, Width, c_state, h_state, act):
"""Builds the synthesis transform."""
with tf.variable_scope("MV_synthesis", reuse=tf.AUTO_REUSE):
with tf.variable_scope("layer_0"):
layer = tfc.SignalConv2D(
num_filters, (3, 3), corr=False, strides_up=2, padding="same_zeros",
use_bias=True, activation=tfc.GDN(inverse=True))
tensor = layer(tensor)
with tf.variable_scope("layer_1"):
layer = tfc.SignalConv2D(
num_filters, (3, 3), corr=False, strides_up=2, padding="same_zeros",
use_bias=True, activation=tfc.GDN(inverse=True))
tensor = layer(tensor)
with tf.variable_scope("recurrent"):
tensor, c_state_out, h_state_out = one_step_rnn(tensor, c_state, h_state,
Height, Width, num_filters,
scale=4, kernal=[3, 3], act=act)
with tf.variable_scope("layer_2"):
layer = tfc.SignalConv2D(
num_filters, (3, 3), corr=False, strides_up=2, padding="same_zeros",
use_bias=True, activation=tfc.GDN(inverse=True))
tensor = layer(tensor)
with tf.variable_scope("layer_3"):
layer = tfc.SignalConv2D(
2, (3, 3), corr=False, strides_up=2, padding="same_zeros",
use_bias=True, activation=None)
tensor = layer(tensor)
return tensor, c_state_out, h_state_out
def Res_analysis(tensor, num_filters, out_filters, Height, Width, c_state, h_state, act):
"""Builds the analysis transform."""
with tf.variable_scope("analysis", reuse=tf.AUTO_REUSE):
with tf.variable_scope("layer_0"):
layer = tfc.SignalConv2D(
num_filters, (5, 5), corr=True, strides_down=2, padding="same_zeros",
use_bias=True, activation=tfc.GDN())
tensor = layer(tensor)
with tf.variable_scope("layer_1"):
layer = tfc.SignalConv2D(
num_filters, (5, 5), corr=True, strides_down=2, padding="same_zeros",
use_bias=True, activation=tfc.GDN())
tensor = layer(tensor)
with tf.variable_scope("recurrent"):
tensor, c_state_out, h_state_out = one_step_rnn(tensor, c_state, h_state,
Height, Width, num_filters,
scale=4, kernal=[5, 5], act=act)
with tf.variable_scope("layer_2"):
layer = tfc.SignalConv2D(
num_filters, (5, 5), corr=True, strides_down=2, padding="same_zeros",
use_bias=True, activation=tfc.GDN())
tensor = layer(tensor)
with tf.variable_scope("layer_3"):
layer = tfc.SignalConv2D(
out_filters, (5, 5), corr=True, strides_down=2, padding="same_zeros",
use_bias=True, activation=None)
tensor = layer(tensor)
return tensor, c_state_out, h_state_out
def Res_synthesis(tensor, num_filters, Height, Width, c_state, h_state, act):
"""Builds the synthesis transform."""
with tf.variable_scope("synthesis", reuse=tf.AUTO_REUSE):
with tf.variable_scope("layer_0"):
layer = tfc.SignalConv2D(
num_filters, (5, 5), corr=False, strides_up=2, padding="same_zeros",
use_bias=True, activation=tfc.GDN(inverse=True))
tensor = layer(tensor)
with tf.variable_scope("layer_1"):
layer = tfc.SignalConv2D(
num_filters, (5, 5), corr=False, strides_up=2, padding="same_zeros",
use_bias=True, activation=tfc.GDN(inverse=True))
tensor = layer(tensor)
with tf.variable_scope("recurrent"):
tensor, c_state_out, h_state_out = one_step_rnn(tensor, c_state, h_state,
Height, Width, num_filters,
scale=4, kernal=[5, 5], act=act)
with tf.variable_scope("layer_2"):
layer = tfc.SignalConv2D(
num_filters, (5, 5), corr=False, strides_up=2, padding="same_zeros",
use_bias=True, activation=tfc.GDN(inverse=True))
tensor = layer(tensor)
with tf.variable_scope("layer_3"):
layer = tfc.SignalConv2D(
3, (5, 5), corr=False, strides_up=2, padding="same_zeros",
use_bias=True, activation=None)
tensor = layer(tensor)
return tensor, c_state_out, h_state_out
def rec_prob(tensor, num_filters, Height, Width, c_state, h_state, k=3, act=tf.tanh):
with tf.variable_scope("CNN_input"):
tensor = tf.expand_dims(tensor, axis=1)
y1 = functions.recurrent_cnn(tensor, 1, layer=4, num_filters=num_filters, stride=1,
out_filters=num_filters, kernel=[k, k], act=tf.nn.relu, act_last=None)
y1 = tf.squeeze(y1, axis=1)
with tf.variable_scope("RNN"):
y2, c_state_out, h_state_out = one_step_rnn(y1, c_state, h_state,
Height, Width, num_filters,
scale=16, kernal=[k, k], act=act)
with tf.variable_scope("CNN_output"):
y2 = tf.expand_dims(y2, axis=1)
y3 = functions.recurrent_cnn(y2, 1, layer=4, num_filters=num_filters, stride=1,
out_filters=2 * num_filters, kernel=[k, k], act=tf.nn.relu, act_last=None)
y3 = tf.squeeze(y3, axis=1)
return y3, c_state_out, h_state_out
def bpp_est(x_target, sigma_mu, num_filters, tiny=1e-10):
sigma, mu = tf.split(sigma_mu, [num_filters, num_filters], axis=-1)
half = tf.constant(.5, dtype=tf.float32)
upper = tf.math.add(x_target, half)
lower = tf.math.add(x_target, -half)
sig = tf.maximum(sigma, -7.0)
upper_l = tf.sigmoid(tf.multiply((upper - mu), (tf.exp(-sig) + tiny)))
lower_l = tf.sigmoid(tf.multiply((lower - mu), (tf.exp(-sig) + tiny)))
p_element = upper_l - lower_l
p_element = tf.clip_by_value(p_element, tiny, 1 - tiny)
ent = -tf.log(p_element) / tf.log(2.0)
bits = tf.math.reduce_sum(ent)
return bits, sigma, mu