-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLayer_Keras_Subclasse_Mirror_Padding.txt
60 lines (43 loc) · 1.86 KB
/
Layer_Keras_Subclasse_Mirror_Padding.txt
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
Fonte: https://stackoverflow.com/questions/49189496/can-symmetrically-paddding-be-done-in-convolution-layers-in-keras
import keras.backend as K
from keras.layers import Layer
class SymmetricPadding2D(Layer):
def __init__(self, output_dim, padding=[1,1],
data_format="channels_last", **kwargs):
self.output_dim = output_dim
self.data_format = data_format
self.padding = padding
super(SymmetricPadding2D, self).__init__(**kwargs)
def build(self, input_shape):
super(SymmetricPadding2D, self).build(input_shape)
def call(self, inputs):
if self.data_format is "channels_last":
#(batch, depth, rows, cols, channels)
pad = [[0,0]] + [[i,i] for i in self.padding] + [[0,0]]
elif self.data_format is "channels_first":
#(batch, channels, depth, rows, cols)
pad = [[0, 0], [0, 0]] + [[i,i] for i in self.padding]
if K.backend() == "tensorflow":
import tensorflow as tf
paddings = tf.constant(pad)
out = tf.pad(inputs, paddings, "REFLECT")
else:
raise Exception("Backend " + K.backend() + "not implemented")
return out
def compute_output_shape(self, input_shape):
return (input_shape[0], self.output_dim)
if __name__ == "__main__":
from keras.models import Sequential
import numpy as np
#Set Image
image = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]
# Pad to "channels_last format
# which is [batch, width, height, channels]=[1,4,4,1]
image = np.expand_dims(np.expand_dims(np.array(image),2),0)
#Build Keras model
model = Sequential()
model.add(SymmetricPadding2D(1, input_shape=(4,4,1)))
model.build()
# To simply apply existing filter, we use predict with no training
out = model.predict(image)
print(out[0,:,:,0])