本文整理匯總了Python中tensorflow.keras.layers.UpSampling2D方法的典型用法代碼示例。如果您正苦於以下問題:Python layers.UpSampling2D方法的具體用法?Python layers.UpSampling2D怎麽用?Python layers.UpSampling2D使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類tensorflow.keras.layers
的用法示例。
在下文中一共展示了layers.UpSampling2D方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: up_stage
# 需要導入模塊: from tensorflow.keras import layers [as 別名]
# 或者: from tensorflow.keras.layers import UpSampling2D [as 別名]
def up_stage(inputs, skip, filters, kernel_size=3,
activation="relu", padding="SAME"):
up = UpSampling2D()(inputs)
up = Conv2D(filters, 2, activation=activation, padding=padding)(up)
up = GroupNormalization()(up)
merge = concatenate([skip, up])
merge = GroupNormalization()(merge)
conv = Conv2D(filters, kernel_size,
activation=activation, padding=padding)(merge)
conv = GroupNormalization()(conv)
conv = Conv2D(filters, kernel_size,
activation=activation, padding=padding)(conv)
conv = GroupNormalization()(conv)
conv = SpatialDropout2D(0.5)(conv, training=True)
return conv
示例2: up_stage
# 需要導入模塊: from tensorflow.keras import layers [as 別名]
# 或者: from tensorflow.keras.layers import UpSampling2D [as 別名]
def up_stage(inputs, skip, filters, prior_fn, kernel_size=3,
activation="relu", padding="SAME"):
up = UpSampling2D()(inputs)
up = tfp.layers.Convolution2DFlipout(filters, 2,
activation=activation,
padding=padding,
kernel_prior_fn=prior_fn)(up)
up = GroupNormalization()(up)
merge = concatenate([skip, up])
merge = GroupNormalization()(merge)
conv = tfp.layers.Convolution2DFlipout(filters, kernel_size,
activation=activation,
padding=padding,
kernel_prior_fn=prior_fn)(merge)
conv = GroupNormalization()(conv)
conv = tfp.layers.Convolution2DFlipout(filters, kernel_size,
activation=activation,
padding=padding,
kernel_prior_fn=prior_fn)(conv)
conv = GroupNormalization()(conv)
return conv
示例3: __init__
# 需要導入模塊: from tensorflow.keras import layers [as 別名]
# 或者: from tensorflow.keras.layers import UpSampling2D [as 別名]
def __init__(self,
in_channels,
out_channels,
scale_factor,
data_format="channels_last",
**kwargs):
super(UpSamplingBlock, self).__init__(**kwargs)
self.scale_factor = scale_factor
self.conv = conv1x1_block(
in_channels=in_channels,
out_channels=out_channels,
strides=1,
activation=None,
data_format=data_format,
name="conv")
self.upsample = nn.UpSampling2D(
size=scale_factor,
data_format=data_format,
interpolation="nearest",
name="upsample")
示例4: __init__
# 需要導入模塊: from tensorflow.keras import layers [as 別名]
# 或者: from tensorflow.keras.layers import UpSampling2D [as 別名]
def __init__(self,
in_channels,
out_channels,
groups=1,
ratio=2,
data_format="channels_last",
**kwargs):
super(AirBlock, self).__init__(**kwargs)
assert (out_channels % ratio == 0)
mid_channels = out_channels // ratio
self.conv1 = conv1x1_block(
in_channels=in_channels,
out_channels=mid_channels,
data_format=data_format,
name="conv1")
self.pool = MaxPool2d(
pool_size=3,
strides=2,
padding=1,
data_format=data_format,
name="pool")
self.conv2 = conv3x3_block(
in_channels=mid_channels,
out_channels=mid_channels,
groups=groups,
data_format=data_format,
name="conv2")
self.conv3 = conv1x1_block(
in_channels=mid_channels,
out_channels=out_channels,
activation=None,
data_format=data_format,
name="conv3")
self.sigmoid = tf.nn.sigmoid
self.upsample = nn.UpSampling2D(
size=(2, 2),
data_format=data_format,
interpolation="bilinear",
name="upsample")
示例5: create_model
# 需要導入模塊: from tensorflow.keras import layers [as 別名]
# 或者: from tensorflow.keras.layers import UpSampling2D [as 別名]
def create_model(trainable=True):
model = MobileNetV2(input_shape=(IMAGE_SIZE, IMAGE_SIZE, 3), include_top=False, alpha=ALPHA, weights="imagenet")
for layer in model.layers:
layer.trainable = trainable
block1 = model.get_layer("block_5_add").output
block2 = model.get_layer("block_12_add").output
block3 = model.get_layer("block_15_add").output
blocks = [block2, block1]
x = block3
for block in blocks:
x = UpSampling2D()(x)
x = Conv2D(256, kernel_size=3, padding="same", strides=1)(x)
x = BatchNormalization()(x)
x = Activation("relu")(x)
x = Concatenate()([x, block])
x = Conv2D(256, kernel_size=3, padding="same", strides=1)(x)
x = BatchNormalization()(x)
x = Activation("relu")(x)
x = Conv2D(1, kernel_size=1, activation="sigmoid")(x)
return Model(inputs=model.input, outputs=x)
示例6: _hourglass_module
# 需要導入模塊: from tensorflow.keras import layers [as 別名]
# 或者: from tensorflow.keras.layers import UpSampling2D [as 別名]
def _hourglass_module(input, stage_index, number_of_keypoints):
if stage_index == 0:
return _inverted_bottleneck(input, up_channel_rate=6, channels=24, is_subsample=False, kernel_size=3), []
else:
# down sample
x = layers.MaxPool2D(pool_size=(2, 2), strides=(2, 2), padding='SAME')(input)
# block front
x = _inverted_bottleneck(x, up_channel_rate=6, channels=24, is_subsample=False, kernel_size=3)
x = _inverted_bottleneck(x, up_channel_rate=6, channels=24, is_subsample=False, kernel_size=3)
x = _inverted_bottleneck(x, up_channel_rate=6, channels=24, is_subsample=False, kernel_size=3)
x = _inverted_bottleneck(x, up_channel_rate=6, channels=24, is_subsample=False, kernel_size=3)
x = _inverted_bottleneck(x, up_channel_rate=6, channels=24, is_subsample=False, kernel_size=3)
stage_index -= 1
# block middle
x, middle_layers = _hourglass_module(x, stage_index=stage_index, number_of_keypoints=number_of_keypoints)
# block back
x = _inverted_bottleneck(x, up_channel_rate=6, channels=number_of_keypoints, is_subsample=False, kernel_size=3)
# up sample
upsampling_size = (2, 2) # (x.shape[1] * 2, x.shape[2] * 2)
x = layers.UpSampling2D(size=upsampling_size, interpolation='bilinear')(x)
upsampling_layer = x
# jump layer
x = _inverted_bottleneck(input, up_channel_rate=6, channels=24, is_subsample=False, kernel_size=3)
x = _inverted_bottleneck(x, up_channel_rate=6, channels=24, is_subsample=False, kernel_size=3)
x = _inverted_bottleneck(x, up_channel_rate=6, channels=24, is_subsample=False, kernel_size=3)
x = _inverted_bottleneck(x, up_channel_rate=6, channels=24, is_subsample=False, kernel_size=3)
x = _inverted_bottleneck(x, up_channel_rate=6, channels=number_of_keypoints, is_subsample=False, kernel_size=3)
jump_branch_layer = x
# add
x = upsampling_layer + jump_branch_layer
middle_layers.append(x)
return x, middle_layers
示例7: __init__
# 需要導入模塊: from tensorflow.keras import layers [as 別名]
# 或者: from tensorflow.keras.layers import UpSampling2D [as 別名]
def __init__(self, compression_factor=0.5, **kwargs):
# super(TransitionDown, self).__init__(self, **kwargs)
self.concat = Concatenate()
self.compression_factor = compression_factor
self.upsample = (
SubPixelUpscaling()
) # layers.UpSampling2D(interpolation='bilinear')
示例8: upsample_module
# 需要導入模塊: from tensorflow.keras import layers [as 別名]
# 或者: from tensorflow.keras.layers import UpSampling2D [as 別名]
def upsample_module(inputs, out1, out2):
left, right = inputs
xl = res_layer0(left,out2)
xl = res_layer0(xl, out2)
xr = convblock(right, out1, 3)
xr = convblock(xr, out2, 3)
xr = layers.UpSampling2D()(xr)
out = layers.Add()([xl, xr])
return out
示例9: connect_left_right
# 需要導入模塊: from tensorflow.keras import layers [as 別名]
# 或者: from tensorflow.keras.layers import UpSampling2D [as 別名]
def connect_left_right(left, right, num_channels, num_channels_next, name):
# left: 2 residual modules
left = residual(left, num_channels_next, name=name + 'skip.0')
left = residual(left, num_channels_next, name=name + 'skip.1')
# up: 2 times residual & nearest neighbour
out = residual(right, num_channels, name=name + 'out.0')
out = residual(out, num_channels_next, name=name + 'out.1')
out = UpSampling2D(name=name + 'out.upsampleNN')(out)
out = Add(name=name + 'out.add')([left, out])
return out
示例10: __init__
# 需要導入模塊: from tensorflow.keras import layers [as 別名]
# 或者: from tensorflow.keras.layers import UpSampling2D [as 別名]
def __init__(self, filters):
super(up_conv, self).__init__()
self.up = Sequential([
UpSampling2D(),
Conv2D(filters, kernel_size=(3,3), strides=1, padding='same'),
BatchNormalization(),
Activation('relu')
])
示例11: fuse_layers
# 需要導入模塊: from tensorflow.keras import layers [as 別名]
# 或者: from tensorflow.keras.layers import UpSampling2D [as 別名]
def fuse_layers(x, channels, multi_scale_output=True):
out = []
for i in range(len(channels) if multi_scale_output else 1):
residual = x[i]
for j in range(len(channels)):
if j > i:
y = conv(x[j], channels[i], 1, padding_='valid')
y = BatchNormalization(epsilon=1e-5, momentum=0.1)(y)
y = UpSampling2D(size=2 ** (j - i))(y)
residual = Add()([residual, y])
elif j < i:
y = x[j]
for k in range(i - j):
if k == i - j - 1:
y = conv(y, channels[i], 3, strides_=2)
y = BatchNormalization(epsilon=1e-5, momentum=0.1)(y)
else:
y = conv(y, channels[j], 3, strides_=2)
y = BatchNormalization(epsilon=1e-5, momentum=0.1)(y)
y = Activation('relu')(y)
residual = Add()([residual, y])
residual = Activation('relu')(residual)
out.append(residual)
return out
示例12: MultiResolutionFusion
# 需要導入模塊: from tensorflow.keras import layers [as 別名]
# 或者: from tensorflow.keras.layers import UpSampling2D [as 別名]
def MultiResolutionFusion(high_inputs=None,low_inputs=None,n_filters=256,name=''):
"""
Fuse together all path inputs. This block first applies convolutions
for input adaptation, which generate feature maps of the same feature dimension
(the smallest one among the inputs), and then up-samples all (smaller) feature maps to
the largest resolution of the inputs. Finally, all features maps are fused by summation.
Arguments:
high_inputs: The input tensors that have the higher resolution
low_inputs: The input tensors that have the lower resolution
n_filters: Number of output feature maps for each conv
Returns:
Fused feature maps at higher resolution
"""
if low_inputs is None: # RefineNet block 4
return high_inputs
else:
conv_low = Conv2D(n_filters, 3, padding='same', name=name+'conv_lo', kernel_initializer=kern_init, kernel_regularizer=kern_reg)(low_inputs)
conv_low = BatchNormalization()(conv_low)
conv_high = Conv2D(n_filters, 3, padding='same', name=name+'conv_hi', kernel_initializer=kern_init, kernel_regularizer=kern_reg)(high_inputs)
conv_high = BatchNormalization()(conv_high)
conv_low_up = UpSampling2D(size=2, interpolation='bilinear', name=name+'up')(conv_low)
return Add(name=name+'sum')([conv_low_up, conv_high])
示例13: build_fcn
# 需要導入模塊: from tensorflow.keras import layers [as 別名]
# 或者: from tensorflow.keras.layers import UpSampling2D [as 別名]
def build_fcn(input_shape,
backbone,
n_classes=4):
"""Helper function to build an FCN model.
Arguments:
backbone (Model): A backbone network
such as ResNetv2 or v1
n_classes (int): Number of object classes
including background.
"""
inputs = Input(shape=input_shape)
features = backbone(inputs)
main_feature = features[0]
features = features[1:]
out_features = [main_feature]
feature_size = 8
size = 2
# other half of the features pyramid
# including upsampling to restore the
# feature maps to the dimensions
# equal to 1/4 the image size
for feature in features:
postfix = "fcn_" + str(feature_size)
feature = conv_layer(feature,
filters=256,
use_maxpool=False,
postfix=postfix)
postfix = postfix + "_up2d"
feature = UpSampling2D(size=size,
interpolation='bilinear',
name=postfix)(feature)
size = size * 2
feature_size = feature_size * 2
out_features.append(feature)
# concatenate all upsampled features
x = Concatenate()(out_features)
# perform 2 additional feature extraction
# and upsampling
x = tconv_layer(x, 256, postfix="up_x2")
x = tconv_layer(x, 256, postfix="up_x4")
# generate the pixel-wise classifier
x = Conv2DTranspose(filters=n_classes,
kernel_size=1,
strides=1,
padding='same',
kernel_initializer='he_normal',
name="pre_activation")(x)
x = Softmax(name="segmentation")(x)
model = Model(inputs, x, name="fcn")
return model
示例14: build_mv2_hourglass_model
# 需要導入模塊: from tensorflow.keras import layers [as 別名]
# 或者: from tensorflow.keras.layers import UpSampling2D [as 別名]
def build_mv2_hourglass_model(number_of_keypoints):
hourglas_stage_num = 4
input_shape = (192, 192, 3) # h, w, c
input = layers.Input(shape=input_shape)
## HEADER
# cnn with regularizer
x = layers.Conv2D(filters=16, kernel_size=(3, 3), strides=(2, 2), padding='SAME', kernel_regularizer=l2_regularizer_00004)(input)
# batch norm
x = layers.BatchNormalization(momentum=0.999)(x)
# activation
x = layers.ReLU(max_value=6)(x)
# 128, 112
x = _inverted_bottleneck(x, up_channel_rate=1, channels=16, is_subsample=False, kernel_size=3)
x = _inverted_bottleneck(x, up_channel_rate=1, channels=16, is_subsample=False, kernel_size=3)
# 64, 56
x = _inverted_bottleneck(x, up_channel_rate=6, channels=24, is_subsample=True, kernel_size=3)
x = _inverted_bottleneck(x, up_channel_rate=6, channels=24, is_subsample=False, kernel_size=3)
x = _inverted_bottleneck(x, up_channel_rate=6, channels=24, is_subsample=False, kernel_size=3)
x = _inverted_bottleneck(x, up_channel_rate=6, channels=24, is_subsample=False, kernel_size=3)
x = _inverted_bottleneck(x, up_channel_rate=6, channels=24, is_subsample=False, kernel_size=3)
captured_h, captured_w = int(x.shape[1]), int(x.shape[2])
print(f"captured_h, captured_w: {captured_h}, {captured_w}")
# HOURGLASS recursively
# stage = 4
#
x, middle_output_layers = _hourglass_module(x, stage_index=hourglas_stage_num, number_of_keypoints=number_of_keypoints)
print("before")
for l in middle_output_layers:
print(f" l.shape: {l.shape}")
for layer_index, middle_layer in enumerate(middle_output_layers):
layer_stage = layer_index + 1
h, w = middle_layer.shape[1], middle_layer.shape[2]
if h == captured_h and w == captured_w:
continue
else:
upsampling_size = (captured_h // h, captured_w // w)
middle_output_layers[layer_index] = layers.UpSampling2D(size=upsampling_size, interpolation='bilinear')(middle_layer)
print("after")
for l in middle_output_layers:
print(f" l.shape: {l.shape}")
model = models.Model(input, outputs=middle_output_layers)
return model
示例15: _mobilenetV2
# 需要導入模塊: from tensorflow.keras import layers [as 別名]
# 或者: from tensorflow.keras.layers import UpSampling2D [as 別名]
def _mobilenetV2(input):
x = _inverted_bottleneck(input, up_channel_rate=1, channels=12, is_subsample=False, kernel_size=3)
x = _inverted_bottleneck(x, up_channel_rate=1, channels=12, is_subsample=False, kernel_size=3)
mv2_branch_0 = x
x = _inverted_bottleneck(x, up_channel_rate=6, channels=18, is_subsample=True, kernel_size=3)
x = _inverted_bottleneck(x, up_channel_rate=6, channels=18, is_subsample=False, kernel_size=3)
x = _inverted_bottleneck(x, up_channel_rate=6, channels=18, is_subsample=False, kernel_size=3)
x = _inverted_bottleneck(x, up_channel_rate=6, channels=18, is_subsample=False, kernel_size=3)
x = _inverted_bottleneck(x, up_channel_rate=6, channels=18, is_subsample=False, kernel_size=3)
mv2_branch_1 = x
x = _inverted_bottleneck(x, up_channel_rate=6, channels=24, is_subsample=True, kernel_size=3)
x = _inverted_bottleneck(x, up_channel_rate=6, channels=24, is_subsample=False, kernel_size=3)
x = _inverted_bottleneck(x, up_channel_rate=6, channels=24, is_subsample=False, kernel_size=3)
x = _inverted_bottleneck(x, up_channel_rate=6, channels=24, is_subsample=False, kernel_size=3)
x = _inverted_bottleneck(x, up_channel_rate=6, channels=24, is_subsample=False, kernel_size=3)
mv2_branch_2 = x
x = _inverted_bottleneck(x, up_channel_rate=6, channels=48, is_subsample=True, kernel_size=3)
x = _inverted_bottleneck(x, up_channel_rate=6, channels=48, is_subsample=False, kernel_size=3)
x = _inverted_bottleneck(x, up_channel_rate=6, channels=48, is_subsample=False, kernel_size=3)
x = _inverted_bottleneck(x, up_channel_rate=6, channels=48, is_subsample=False, kernel_size=3)
x = _inverted_bottleneck(x, up_channel_rate=6, channels=48, is_subsample=False, kernel_size=3)
mv2_branch_3 = x
x = _inverted_bottleneck(x, up_channel_rate=6, channels=72, is_subsample=True, kernel_size=3)
x = _inverted_bottleneck(x, up_channel_rate=6, channels=72, is_subsample=False, kernel_size=3)
x = _inverted_bottleneck(x, up_channel_rate=6, channels=72, is_subsample=False, kernel_size=3)
x = _inverted_bottleneck(x, up_channel_rate=6, channels=72, is_subsample=False, kernel_size=3)
x = _inverted_bottleneck(x, up_channel_rate=6, channels=72, is_subsample=False, kernel_size=3)
mv2_branch_4 = x
x = layers.Concatenate(axis=3)([
layers.MaxPool2D(pool_size=(4, 4), strides=(4, 4), padding='SAME')(mv2_branch_0),
layers.MaxPool2D(pool_size=(2, 2), strides=(2, 2), padding='SAME')(mv2_branch_1),
mv2_branch_2,
layers.UpSampling2D(size=(2, 2), interpolation='bilinear')(mv2_branch_3),
layers.UpSampling2D(size=(4, 4), interpolation='bilinear')(mv2_branch_4),
])
return x