本文整理汇总了Python中keras.layers.SpatialDropout2D方法的典型用法代码示例。如果您正苦于以下问题:Python layers.SpatialDropout2D方法的具体用法?Python layers.SpatialDropout2D怎么用?Python layers.SpatialDropout2D使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类keras.layers
的用法示例。
在下文中一共展示了layers.SpatialDropout2D方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: keras_dropout
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import SpatialDropout2D [as 别名]
def keras_dropout(layer, rate):
"""
Keras dropout layer.
"""
from keras import layers
input_dim = len(layer.input.shape)
if input_dim == 2:
return layers.SpatialDropout1D(rate)
elif input_dim == 3:
return layers.SpatialDropout2D(rate)
elif input_dim == 4:
return layers.SpatialDropout3D(rate)
else:
return layers.Dropout(rate)
示例2: test_dropout
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import SpatialDropout2D [as 别名]
def test_dropout():
layer_test(layers.Dropout,
kwargs={'rate': 0.5},
input_shape=(3, 2))
layer_test(layers.Dropout,
kwargs={'rate': 0.5, 'noise_shape': [3, 1]},
input_shape=(3, 2))
layer_test(layers.Dropout,
kwargs={'rate': 0.5, 'noise_shape': [None, 1]},
input_shape=(3, 2))
layer_test(layers.SpatialDropout1D,
kwargs={'rate': 0.5},
input_shape=(2, 3, 4))
for data_format in ['channels_last', 'channels_first']:
for shape in [(4, 5), (4, 5, 6)]:
if data_format == 'channels_last':
input_shape = (2,) + shape + (3,)
else:
input_shape = (2, 3) + shape
layer_test(layers.SpatialDropout2D if len(shape) == 2 else layers.SpatialDropout3D,
kwargs={'rate': 0.5,
'data_format': data_format},
input_shape=input_shape)
# Test invalid use cases
with pytest.raises(ValueError):
layer_test(layers.SpatialDropout2D if len(shape) == 2 else layers.SpatialDropout3D,
kwargs={'rate': 0.5,
'data_format': 'channels_middle'},
input_shape=input_shape)
示例3: build_psp
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import SpatialDropout2D [as 别名]
def build_psp(backbone,
psp_layer,
last_upsampling_factor,
classes=21,
activation='softmax',
conv_filters=512,
pooling_type='avg',
dropout=None,
final_interpolation='bilinear',
use_batchnorm=True):
input = backbone.input
x = extract_outputs(backbone, [psp_layer])[0]
x = PyramidPoolingModule(
conv_filters=conv_filters,
pooling_type=pooling_type,
use_batchnorm=use_batchnorm)(x)
x = Conv2DBlock(512, (1, 1), activation='relu', padding='same',
use_batchnorm=use_batchnorm)(x)
if dropout is not None:
x = SpatialDropout2D(dropout)(x)
x = Conv2D(classes, (3,3), padding='same', name='final_conv')(x)
if final_interpolation == 'bilinear':
x = ResizeImage(to_tuple(last_upsampling_factor))(x)
elif final_interpolation == 'duc':
x = DUC(to_tuple(last_upsampling_factor))(x)
else:
raise ValueError('Unsupported interpolation type {}. '.format(final_interpolation) +
'Use `duc` or `bilinear`.')
x = Activation(activation, name=activation)(x)
model = Model(input, x)
return model
示例4: decoder_a
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import SpatialDropout2D [as 别名]
def decoder_a(self):
""" Decoder for side A """
kwargs = dict(kernel_size=5, kernel_initializer=self.kernel_initializer)
decoder_complexity = 320 if self.lowmem else self.config["complexity_decoder_a"]
dense_dim = 384 if self.lowmem else 512
decoder_shape = self.input_shape[0] // 16
input_ = Input(shape=(decoder_shape, decoder_shape, dense_dim))
var_x = input_
var_x = self.blocks.upscale(var_x, decoder_complexity, **kwargs)
var_x = SpatialDropout2D(0.25)(var_x)
var_x = self.blocks.upscale(var_x, decoder_complexity, **kwargs)
if self.lowmem:
var_x = SpatialDropout2D(0.15)(var_x)
else:
var_x = SpatialDropout2D(0.25)(var_x)
var_x = self.blocks.upscale(var_x, decoder_complexity // 2, **kwargs)
var_x = self.blocks.upscale(var_x, decoder_complexity // 4, **kwargs)
var_x = self.blocks.conv2d(var_x, 3,
kernel_size=5,
padding="same",
activation="sigmoid",
name="face_out")
outputs = [var_x]
if self.config.get("learn_mask", False):
var_y = input_
var_y = self.blocks.upscale(var_y, decoder_complexity)
var_y = self.blocks.upscale(var_y, decoder_complexity)
var_y = self.blocks.upscale(var_y, decoder_complexity // 2)
var_y = self.blocks.upscale(var_y, decoder_complexity // 4)
var_y = self.blocks.conv2d(var_y, 1,
kernel_size=5,
padding="same",
activation="sigmoid",
name="mask_out")
outputs.append(var_y)
return KerasModel(input_, outputs=outputs)
示例5: build_shallow_weight
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import SpatialDropout2D [as 别名]
def build_shallow_weight(channels, width, height, output_size, nb_classes):
# input
inputs = Input(shape=(channels, height, width))
# 1 conv
conv1_1 = Convolution2D(8, 3, 3, border_mode='same', activation='relu',
W_regularizer=l2(0.01))(inputs)
bn1 = BatchNormalization(mode=0, axis=1)(conv1_1)
pool1 = MaxPooling2D(pool_size=(2,2), strides=(2,2))(bn1)
gn1 = GaussianNoise(0.5)(pool1)
drop1 = SpatialDropout2D(0.5)(gn1)
# 2 conv
conv2_1 = Convolution2D(8, 3, 3, border_mode='same', activation='relu',
W_regularizer=l2(0.01))(gn1)
bn2 = BatchNormalization(mode=0, axis=1)(conv2_1)
pool2 = MaxPooling2D(pool_size=(2,2), strides=(2,2))(bn2)
gn2 = GaussianNoise(0.5)(pool2)
drop2 = SpatialDropout2D(0.5)(gn2)
# 3 conv
conv3_1 = Convolution2D(8, 3, 3, border_mode='same', activation='relu',
W_regularizer=l2(0.01))(drop2)
bn3 = BatchNormalization(mode=0, axis=1)(conv3_1)
pool3 = MaxPooling2D(pool_size=(2,2), strides=(2,2))(bn3)
gn3 = GaussianNoise(0.5)(pool3)
drop3 = SpatialDropout2D(0.5)(gn3)
# 4 conv
conv4_1 = Convolution2D(8, 3, 3, border_mode='same', activation='relu',
W_regularizer=l2(0.01))(gn3)
bn4 = BatchNormalization(mode=0, axis=1)(conv4_1)
pool4 = MaxPooling2D(pool_size=(2,2), strides=(2,2))(bn4)
gn4 = GaussianNoise(0.5)(pool4)
drop4 = SpatialDropout2D(0.5)(gn4)
# flaten
flat = Flatten()(gn4)
# 1 dense
dense1 = Dense(8, activation='relu', W_regularizer=l2(0.1))(flat)
bn6 = BatchNormalization(mode=0, axis=1)(dense1)
drop6 = Dropout(0.5)(bn6)
# output
out = []
for i in range(output_size):
out.append(Dense(nb_classes, activation='softmax')(bn6))
if output_size > 1:
merged_out = merge(out, mode='concat')
shaped_out = Reshape((output_size, nb_classes))(merged_out)
sample_weight_mode = 'temporal'
else:
shaped_out = out
sample_weight_mode = None
model = Model(input=[inputs], output=shaped_out)
model.summary()
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=[categorical_accuracy_per_sequence],
sample_weight_mode = sample_weight_mode
)
return model
示例6: unet_model1
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import SpatialDropout2D [as 别名]
def unet_model1():
inputs = Input((1, 512, 512))
conv1 = Convolution2D(width, 3, 3, activation='relu', border_mode='same')(inputs)
conv1 = BatchNormalization(axis = 1)(conv1)
conv1 = Convolution2D(width, 3, 3, activation='relu', border_mode='same')(conv1)
pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)
conv2 = Convolution2D(width*2, 3, 3, activation='relu', border_mode='same')(pool1)
conv2 = BatchNormalization(axis = 1)(conv2)
conv2 = Convolution2D(width*2, 3, 3, activation='relu', border_mode='same')(conv2)
pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)
conv3 = Convolution2D(width*4, 3, 3, activation='relu', border_mode='same')(pool2)
conv3 = BatchNormalization(axis = 1)(conv3)
conv3 = Convolution2D(width*4, 3, 3, activation='relu', border_mode='same')(conv3)
pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)
conv4 = Convolution2D(width*8, 3, 3, activation='relu', border_mode='same')(pool3)
conv4 = BatchNormalization(axis = 1)(conv4)
conv4 = Convolution2D(width*8, 3, 3, activation='relu', border_mode='same')(conv4)
pool4 = MaxPooling2D(pool_size=(2, 2))(conv4)
conv5 = Convolution2D(width*16, 3, 3, activation='relu', border_mode='same')(pool4)
conv5 = BatchNormalization(axis = 1)(conv5)
conv5 = Convolution2D(width*16, 3, 3, activation='relu', border_mode='same')(conv5)
up6 = merge([UpSampling2D(size=(2, 2))(conv5), conv4], mode='concat', concat_axis=1)
conv6 = SpatialDropout2D(dropout_rate)(up6)
conv6 = Convolution2D(width*8, 3, 3, activation='relu', border_mode='same')(conv6)
conv6 = Convolution2D(width*8, 3, 3, activation='relu', border_mode='same')(conv6)
up7 = merge([UpSampling2D(size=(2, 2))(conv6), conv3], mode='concat', concat_axis=1)
conv7 = SpatialDropout2D(dropout_rate)(up7)
conv7 = Convolution2D(width*4, 3, 3, activation='relu', border_mode='same')(conv7)
conv7 = Convolution2D(width*4, 3, 3, activation='relu', border_mode='same')(conv7)
up8 = merge([UpSampling2D(size=(2, 2))(conv7), conv2], mode='concat', concat_axis=1)
conv8 = SpatialDropout2D(dropout_rate)(up8)
conv8 = Convolution2D(width*2, 3, 3, activation='relu', border_mode='same')(conv8)
conv8 = Convolution2D(width*2, 3, 3, activation='relu', border_mode='same')(conv8)
up9 = merge([UpSampling2D(size=(2, 2))(conv8), conv1], mode='concat', concat_axis=1)
conv9 = SpatialDropout2D(dropout_rate)(up9)
conv9 = Convolution2D(width, 3, 3, activation='relu', border_mode='same')(conv9)
conv9 = Convolution2D(width, 3, 3, activation='relu', border_mode='same')(conv9)
conv10 = Convolution2D(1, 1, 1, activation='sigmoid')(conv9)
model = Model(input=inputs, output=conv10)
model.compile(optimizer=Adam(lr=1e-5), loss=dice_coef_loss, metrics=[dice_coef])
return model