-
Notifications
You must be signed in to change notification settings - Fork 1
/
layers.py
67 lines (53 loc) · 2.5 KB
/
layers.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
import tensorflow as tf
import numpy as np
####################################################
# Operational Layers.
class Oper2D(tf.keras.Model):
def __init__(self, filters, kernel_size, activation = None, q = 1, padding = 'valid', use_bias=True, strides=1):
super(Oper2D, self).__init__(name='')
self.activation = activation
self.q = q
self.all_layers = []
for i in range(0, q): # q convolutional layers.
self.all_layers.append(tf.keras.layers.Conv2D(filters,
(kernel_size,
kernel_size),
padding=padding,
use_bias=use_bias,
strides = strides,
activation=None))
@tf.function
def call(self, input_tensor, training=False):
x = self.all_layers[0](input_tensor) # First convolutional layer.
if self.q > 1:
for i in range(1, self.q):
x += self.all_layers[i](tf.math.pow(input_tensor, i + 1))
if self.activation is not None:
return eval('tf.nn.' + self.activation + '(x)')
else:
return x
####################################################
# Transposed Operational Layers.
class Oper2DTranspose(tf.keras.Model):
def __init__(self, filters, kernel_size, activation = None, q = 1, padding = 'valid', use_bias=True, strides=1):
super(Oper2DTranspose, self).__init__(name='')
self.activation = activation
self.q = q
self.all_layers = []
for i in range(0, q): # q convolutional layers.
self.all_layers.append(tf.keras.layers.Conv2DTranspose(filters,
kernel_size,
padding=padding,
use_bias=use_bias,
strides = strides,
activation=None))
@tf.function
def call(self, input_tensor, training=False):
x = self.all_layers[0](input_tensor) # First convolutional layer.
if self.q > 1:
for i in range(1, self.q):
x += self.all_layers[i](tf.math.pow(input_tensor, i + 1))
if self.activation is not None:
return eval('tf.nn.' + self.activation + '(x)')
else:
return x