本文整理汇总了Python中keras.backend.pool2d方法的典型用法代码示例。如果您正苦于以下问题:Python backend.pool2d方法的具体用法?Python backend.pool2d怎么用?Python backend.pool2d使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类keras.backend
的用法示例。
在下文中一共展示了backend.pool2d方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: call
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import pool2d [as 别名]
def call(self, x, mask=None):
if K.image_dim_ordering == "th":
_, f, r, c = self.shape
else:
_, r, c, f = self.shape
squared = K.square(x)
pooled = K.pool2d(squared, (self.n, self.n), strides=(1, 1),
padding="same", pool_mode="avg")
if K.image_dim_ordering == "th":
summed = K.sum(pooled, axis=1, keepdims=True)
averaged = self.alpha * K.repeat_elements(summed, f, axis=1)
else:
summed = K.sum(pooled, axis=3, keepdims=True)
averaged = self.alpha * K.repeat_elements(summed, f, axis=3)
denom = K.pow(self.k + averaged, self.beta)
return x / denom
示例2: pool1d
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import pool2d [as 别名]
def pool1d(
x,
pool_size,
strides=1,
padding='valid',
data_format=None,
pool_mode='max'
):
"""向量序列的pool函数
"""
x = K.expand_dims(x, 1)
x = K.pool2d(
x,
pool_size=(1, pool_size),
strides=(1, strides),
padding=padding,
data_format=data_format,
pool_mode=pool_mode
)
return x[:, 0]
示例3: call
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import pool2d [as 别名]
def call(self, x, mask=None):
"""Layer functionality."""
maxpool_type = self.config.get('conversion', 'maxpool_type')
if 'binary' in self.activation_str:
return k.pool2d(x, self.pool_size, self.strides, self.padding,
pool_mode='max')
elif maxpool_type == 'avg_max':
update_rule = self.spikerate_pre + (x - self.spikerate_pre) * \
self.dt / self.time
elif maxpool_type == 'exp_max':
# update_rule = self.spikerate_pre + x / 2. ** (1 / t_inv)
update_rule = self.spikerate_pre * 1.005 + x * 0.995
elif maxpool_type == 'fir_max':
update_rule = self.spikerate_pre + x * self.dt / self.time
else:
print("Wrong max pooling type, falling back on average pooling.")
return k.pool2d(x, self.pool_size, self.strides, self.padding,
pool_mode='avg')
self.add_update([(self.spikerate_pre, update_rule),
(self.previous_x, x)])
return self._pooling_function([self.spikerate_pre, self.previous_x],
self.pool_size, self.strides,
self.padding, self.data_format)
示例4: weighted_dice_loss
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import pool2d [as 别名]
def weighted_dice_loss(y_true, y_pred):
y_true = K.cast(y_true, 'float32')
y_pred = K.cast(y_pred, 'float32')
# if we want to get same size of output, kernel size must be odd number
if K.int_shape(y_pred)[1] == 128:
kernel_size = 11
elif K.int_shape(y_pred)[1] == 256:
kernel_size = 21
elif K.int_shape(y_pred)[1] == 512:
kernel_size = 21
elif K.int_shape(y_pred)[1] == 1024:
kernel_size = 41
else:
raise ValueError('Unexpected image size')
averaged_mask = K.pool2d(
y_true, pool_size=(kernel_size, kernel_size), strides=(1, 1), padding='same', pool_mode='avg')
border = K.cast(K.greater(averaged_mask, 0.005), 'float32') * K.cast(K.less(averaged_mask, 0.995), 'float32')
weight = K.ones_like(averaged_mask)
w0 = K.sum(weight)
weight += border * 2
w1 = K.sum(weight)
weight *= (w0 / w1)
loss = 1 - weighted_dice_coeff(y_true, y_pred, weight)
return loss
示例5: weighted_bce_dice_loss
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import pool2d [as 别名]
def weighted_bce_dice_loss(y_true, y_pred):
y_true = K.cast(y_true, 'float32')
y_pred = K.cast(y_pred, 'float32')
# if we want to get same size of output, kernel size must be odd number
if K.int_shape(y_pred)[1] == 128:
kernel_size = 11
elif K.int_shape(y_pred)[1] == 256:
kernel_size = 21
elif K.int_shape(y_pred)[1] == 512:
kernel_size = 21
elif K.int_shape(y_pred)[1] == 1024:
kernel_size = 41
else:
raise ValueError('Unexpected image size')
averaged_mask = K.pool2d(
y_true, pool_size=(kernel_size, kernel_size), strides=(1, 1), padding='same', pool_mode='avg')
border = K.cast(K.greater(averaged_mask, 0.005), 'float32') * K.cast(K.less(averaged_mask, 0.995), 'float32')
weight = K.ones_like(averaged_mask)
w0 = K.sum(weight)
weight += border * 2
w1 = K.sum(weight)
weight *= (w0 / w1)
loss = weighted_bce_loss(y_true, y_pred, weight) + (1 - weighted_dice_coeff(y_true, y_pred, weight))
return loss
示例6: call
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import pool2d [as 别名]
def call(self, v, **kwargs):
assert (len(self.input_dims) == len(self.output_dims) and
self.input_dims[0] == self.output_dims[0])
# possibly shrink spatial axis by pooling elements
if len(self.input_dims) == 4 and (self.input_dims[1] > self.output_dims[1] or self.input_dims[2] > self.output_dims[2]):
assert (self.input_dims[1] % self.output_dims[1] == 0 and
self.input_dims[2] % self.output_dims[2] == 0)
pool_sizes = (self.input_dims[1] / self.output_dims[1],
self.input_dims[2] / self.output_dims[2])
strides = pool_sizes
v = K.pool2d(
v, pool_size=pool_sizes, strides=strides,
padding='same', data_format='channels_last', pool_mode='avg')
# possibly extend spatial axis by repeating elements
for i in range(1, len(self.input_dims) - 1):
if self.input_dims[i] < self.output_dims[i]:
assert self.output_dims[i] % self.input_dims[i] == 0
v = K.repeat_elements(
v, rep=int(self.output_dims[i] / self.input_dims[i]),
axis=i)
return v
开发者ID:PacktPublishing,项目名称:Hands-On-Generative-Adversarial-Networks-with-Keras,代码行数:27,代码来源:layers.py
示例7: get_border_mask
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import pool2d [as 别名]
def get_border_mask(pool_size, y_true):
negative = 1 - y_true
positive = y_true
positive = K.pool2d(positive, pool_size=pool_size, padding="same")
negative = K.pool2d(negative, pool_size=pool_size, padding="same")
border = positive * negative
return border
示例8: _nms
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import pool2d [as 别名]
def _nms(heat, kernel=3):
hmax = K.pool2d(heat, (kernel, kernel), padding='same', pool_mode='max')
keep = K.cast(K.equal(hmax, heat), K.floatx())
return heat * keep
示例9: min_max_pool2d
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import pool2d [as 别名]
def min_max_pool2d(x):
max_x = K.pool2d(x, pool_size=(2, 2), strides=(2, 2))
min_x = min_pool2d(x)
return K.concatenate([max_x, min_x], axis=3) # concatenate on channel
示例10: call
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import pool2d [as 别名]
def call(self, v, **kwargs):
assert len(self.si) == len(self.so) and self.si[0] == self.so[0]
# Decrease feature maps. Attention: channels last
if self.si[-1] > self.so[-1]:
v = v[...,:self.so[-1]]
# Increase feature maps. Attention:channels last
if self.si[-1] < self.so[-1]:
z = K.zeros((self.so[:-1] + (self.so[-1] - self.si[-1])),dtype=v.dtype)
v = K.concatenate([v,z])
# Shrink spatial axis
if len(self.si) == 4 and (self.si[1] > self.so[1] or self.si[2] > self.so[2]):
assert self.si[1] % self.so[1] == 0 and self.si[2] % self.so[2] == 0
pool_size = (self.si[1] / self.so[1],self.si[2] / self.so[2])
strides = pool_size
v = K.pool2d(v,pool_size=pool_size,strides=strides,padding='same',data_format='channels_last',pool_mode='avg')
#Extend spatial axis
for i in range(1,len(self.si) - 1):
if self.si[i] < self.so[i]:
assert self.so[i] % self.si[i] == 0
v = K.repeat_elements(v,rep=int(self.so[i] / self.si[i]),axis=i)
return v
示例11: weighted_bce_dice_loss
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import pool2d [as 别名]
def weighted_bce_dice_loss(y_true, y_pred):
y_true = K.cast(y_true, 'float32')
y_pred = K.cast(y_pred, 'float32')
# if we want to get same size of output, kernel size must be odd number
averaged_mask = K.pool2d(
y_true, pool_size=(11, 11), strides=(1, 1), padding='same', pool_mode='avg')
border = K.cast(K.greater(averaged_mask, 0.005), 'float32') * K.cast(K.less(averaged_mask, 0.995), 'float32')
weight = K.ones_like(averaged_mask)
w0 = K.sum(weight)
weight += border * 2
w1 = K.sum(weight)
weight *= (w0 / w1)
loss = weighted_bce_loss(y_true, y_pred, weight) + \
weighted_dice_loss(y_true, y_pred, weight)
return loss