googlenet_custom_layers.py
Go to the documentation of this file.
1 from keras.layers.core import Layer
2 import theano.tensor as T
3 
4 class LRN(Layer):
5 
6  def __init__(self, alpha=0.0001,k=1,beta=0.75,n=5, **kwargs):
7  self.alpha = alpha
8  self.k = k
9  self.beta = beta
10  self.n = n
11  super(LRN, self).__init__(**kwargs)
12 
13  def call(self, x, mask=None):
14  b, ch, r, c = x.shape
15  half_n = self.n // 2 # half the local region
16  input_sqr = T.sqr(x) # square the input
17  extra_channels = T.alloc(0., b, ch + 2*half_n, r, c) # make an empty tensor with zero pads along channel dimension
18  input_sqr = T.set_subtensor(extra_channels[:, half_n:half_n+ch, :, :],input_sqr) # set the center to be the squared input
19  scale = self.k # offset for the scale
20  norm_alpha = self.alpha / self.n # normalized alpha
21  for i in range(self.n):
22  scale += norm_alpha * input_sqr[:, i:i+ch, :, :]
23  scale = scale ** self.beta
24  x = x / scale
25  return x
26 
27  def get_config(self):
28  config = {"alpha": self.alpha,
29  "k": self.k,
30  "beta": self.beta,
31  "n": self.n}
32  base_config = super(LRN, self).get_config()
33  return dict(list(base_config.items()) + list(config.items()))
34 
35 
36 class PoolHelper(Layer):
37 
38  def __init__(self, **kwargs):
39  super(PoolHelper, self).__init__(**kwargs)
40 
41  def call(self, x, mask=None):
42  return x[:,:,1:,1:]
43 
44  def get_config(self):
45  config = {}
46  base_config = super(PoolHelper, self).get_config()
47  return dict(list(base_config.items()) + list(config.items()))
def __init__(self, alpha=0.0001, k=1, beta=0.75, n=5, kwargs)