Python tensorflow.keras.layers.Conv2DTranspose() Examples
The following are 22
code examples of tensorflow.keras.layers.Conv2DTranspose().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
tensorflow.keras.layers
, or try the search function
.
Example #1
Source File: decoders.py From AirSim-Drone-Racing-VAE-Imitation with MIT License | 7 votes |
def create_model(self): print('[ImgDecoder] Starting create_model') dense = Dense(units=1024, name='p_img_dense') reshape = Reshape((1, 1, 1024)) # for 64x64 img deconv1 = Conv2DTranspose(filters=128, kernel_size=4, strides=1, padding='valid', activation='relu') deconv2 = Conv2DTranspose(filters=64, kernel_size=5, strides=1, padding='valid', activation='relu', dilation_rate=3) deconv3 = Conv2DTranspose(filters=64, kernel_size=6, strides=1, padding='valid', activation='relu', dilation_rate=2) deconv4 = Conv2DTranspose(filters=32, kernel_size=5, strides=2, padding='valid', activation='relu', dilation_rate=1) deconv5 = Conv2DTranspose(filters=16, kernel_size=5, strides=1, padding='valid', activation='relu', dilation_rate=1) # deconv6 = Conv2DTranspose(filters=8, kernel_size=6, strides=2, padding='valid', activation='relu') deconv7 = Conv2DTranspose(filters=3, kernel_size=6, strides=1, padding='valid', activation='tanh') self.network = tf.keras.Sequential([ dense, reshape, deconv1, deconv2, deconv3, deconv4, deconv5, deconv7], name='p_img') print('[ImgDecoder] Done with create_model')
Example #2
Source File: multiRes.py From MIScnn with GNU General Public License v3.0 | 6 votes |
def trans_conv2d_bn(x, filters, num_row, num_col, padding='same', strides=(2, 2), name=None): ''' 2D Transposed Convolutional layers Arguments: x {keras layer} -- input layer filters {int} -- number of filters num_row {int} -- number of rows in filters num_col {int} -- number of columns in filters Keyword Arguments: padding {str} -- mode of padding (default: {'same'}) strides {tuple} -- stride of convolution operation (default: {(2, 2)}) name {str} -- name of the layer (default: {None}) Returns: [keras layer] -- [output layer] ''' x = Conv2DTranspose(filters, (num_row, num_col), strides=strides, padding=padding)(x) x = BatchNormalization(axis=3, scale=False)(x) return x
Example #3
Source File: residual.py From MIScnn with GNU General Public License v3.0 | 6 votes |
def expanding_layer_2D(input, neurons, concatenate_link, ba_norm, ba_norm_momentum): up = concatenate([Conv2DTranspose(neurons, (2, 2), strides=(2, 2), padding='same')(input), concatenate_link], axis=-1) conv1 = Conv2D(neurons, (3, 3,), activation='relu', padding='same')(up) if ba_norm : conv1 = BatchNormalization(momentum=ba_norm_momentum)(conv1) conv2 = Conv2D(neurons, (3, 3), activation='relu', padding='same')(conv1) if ba_norm : conv2 = BatchNormalization(momentum=ba_norm_momentum)(conv2) shortcut = Conv2D(neurons, (1, 1), activation='relu', padding="same")(up) add_layer = add([shortcut, conv2]) return add_layer #-----------------------------------------------------# # Subroutines 3D # #-----------------------------------------------------# # Create a contracting layer
Example #4
Source File: model.py From Advanced-Deep-Learning-with-Keras with MIT License | 6 votes |
def tconv_layer(inputs, filters=32, kernel_size=3, strides=2, postfix=None): """Helper function to build Conv2DTranspose-BN-ReLU layer """ x = Conv2DTranspose(filters=filters, kernel_size=kernel_size, strides=strides, padding='same', kernel_initializer='he_normal', name='tconv_'+postfix)(inputs) x = BatchNormalization(name="bn_"+postfix)(x) x = Activation('relu', name='relu_'+postfix)(x) return x
Example #5
Source File: dense.py From MIScnn with GNU General Public License v3.0 | 6 votes |
def expanding_layer_2D(input, neurons, concatenate_link, ba_norm, ba_norm_momentum): up = concatenate([Conv2DTranspose(neurons, (2, 2), strides=(2, 2), padding='same')(input), concatenate_link], axis=-1) conv1 = Conv2D(neurons, (3, 3,), activation='relu', padding='same')(up) if ba_norm : conv1 = BatchNormalization(momentum=ba_norm_momentum)(conv1) conc1 = concatenate([up, conv1], axis=-1) conv2 = Conv2D(neurons, (3, 3), activation='relu', padding='same')(conc1) if ba_norm : conv2 = BatchNormalization(momentum=ba_norm_momentum)(conv2) conc2 = concatenate([up, conv2], axis=-1) return conc2 #-----------------------------------------------------# # Subroutines 3D # #-----------------------------------------------------# # Create a contracting layer
Example #6
Source File: classic_unet.py From stacks-usecase with Apache License 2.0 | 5 votes |
def upsample(N, input_layer, base_filters=64): """deconv defaults.""" return Conv2DTranspose( filters=base_filters * N, kernel_size=3, strides=(2, 2), padding="same", kernel_initializer="he_normal", )(input_layer)
Example #7
Source File: MiniNetv2.py From TF.Keras-Commonly-used-models with Apache License 2.0 | 5 votes |
def transposeConv(filters, kernel_size, strides=1, dilation_rate=1, use_bias=True): return layers.Conv2DTranspose(filters, kernel_size, strides=strides, padding='same', use_bias=use_bias, kernel_regularizer=regularizers.l2(l=0.0003), dilation_rate=dilation_rate) # Depthwise convolution
Example #8
Source File: standard.py From MIScnn with GNU General Public License v3.0 | 5 votes |
def expanding_layer_2D(input, neurons, concatenate_link, ba_norm, ba_norm_momentum): up = concatenate([Conv2DTranspose(neurons, (2, 2), strides=(2, 2), padding='same')(input), concatenate_link], axis=-1) conv1 = Conv2D(neurons, (3, 3,), activation='relu', padding='same')(up) if ba_norm : conv1 = BatchNormalization(momentum=ba_norm_momentum)(conv1) conv2 = Conv2D(neurons, (3, 3), activation='relu', padding='same')(conv1) if ba_norm : conv2 = BatchNormalization(momentum=ba_norm_momentum)(conv2) return conv2 #-----------------------------------------------------# # Subroutines 3D # #-----------------------------------------------------# # Create a contracting layer
Example #9
Source File: compact.py From MIScnn with GNU General Public License v3.0 | 5 votes |
def expanding_layer_2D(input, neurons, concatenate_link, ba_norm, ba_norm_momentum): up = concatenate([Conv2DTranspose(neurons, (2, 2), strides=(2, 2), padding='same')(input), concatenate_link], axis=-1) conv1 = Conv2D(neurons, (3, 3,), activation='relu', padding='same')(up) if ba_norm : conv1 = BatchNormalization(momentum=ba_norm_momentum)(conv1) conv2 = Conv2D(neurons, (3, 3), activation='relu', padding='same')(conv1) if ba_norm : conv2 = BatchNormalization(momentum=ba_norm_momentum)(conv2) conc = concatenate([up, conv2], axis=-1) return conc #-----------------------------------------------------# # Subroutines 3D # #-----------------------------------------------------# # Create a contracting layer
Example #10
Source File: simplepose_coco.py From tf2-mobile-pose-estimation with Apache License 2.0 | 5 votes |
def __init__(self, in_channels, out_channels, kernel_size, strides=1, padding=0, out_padding=0, dilation=1, groups=1, use_bias=True, data_format="channels_last", **kwargs): super(Deconv2d, self).__init__(**kwargs) assert (dilation == 1) assert (groups == 1) assert (in_channels is not None) if isinstance(padding, int): padding = (padding, padding) self.use_crop = (padding[0] > 0) or (padding[1] > 0) if self.use_crop: self.crop = nn.Cropping2D( cropping=padding, data_format=data_format, name="crop") self.conv = nn.Conv2DTranspose( filters=out_channels, kernel_size=kernel_size, strides=strides, padding="valid", output_padding=out_padding, data_format=data_format, dilation_rate=dilation, use_bias=use_bias, name="conv")
Example #11
Source File: model.py From DexiNed with MIT License | 5 votes |
def __init__(self, up_scale,**kwargs): super(UpConvBlock, self).__init__(**kwargs) constant_features = 16 k_reg = None if w_decay is None else l2(w_decay) features = [] total_up_scale = 2 ** up_scale for i in range(up_scale): out_features = 1 if i == up_scale-1 else constant_features if i==up_scale-1: features.append(layers.Conv2D( filters=out_features, kernel_size=(1,1), strides=(1,1), padding='same', activation='relu', kernel_initializer=tf.initializers.TruncatedNormal(stddev=0.1), kernel_regularizer=k_reg,use_bias=True)) #tf.initializers.TruncatedNormal(mean=0.) features.append(layers.Conv2DTranspose( out_features, kernel_size=(total_up_scale,total_up_scale), strides=(2,2), padding='same', kernel_initializer=tf.initializers.TruncatedNormal(stddev=0.1), kernel_regularizer=k_reg,use_bias=True)) # stddev=0.1 else: features.append(layers.Conv2D( filters=out_features, kernel_size=(1,1), strides=(1,1), padding='same', activation='relu',kernel_initializer=weight_init, kernel_regularizer=k_reg,use_bias=True)) features.append(layers.Conv2DTranspose( out_features, kernel_size=(total_up_scale,total_up_scale), strides=(2,2), padding='same', use_bias=True, kernel_initializer=weight_init, kernel_regularizer=k_reg)) self.features = keras.Sequential(features)
Example #12
Source File: custom_unet.py From stacks-usecase with Apache License 2.0 | 5 votes |
def last_layer(out_channels=1): """last layer of u-net. """ return layers.Conv2DTranspose( filters=out_channels, kernel_size=1, strides=2, padding="same", activation="sigmoid", kernel_initializer="he_normal", )
Example #13
Source File: custom_unet.py From stacks-usecase with Apache License 2.0 | 5 votes |
def decode(filters): """upsample sequential model.""" net = Seq() net.add( layers.Conv2DTranspose( filters, 3, strides=2, padding="same", kernel_initializer="he_normal" ) ) net.add(layers.ReLU()) return net
Example #14
Source File: cyclegan-7.1.1.py From Advanced-Deep-Learning-with-Keras with MIT License | 5 votes |
def decoder_layer(inputs, paired_inputs, filters=16, kernel_size=3, strides=2, activation='relu', instance_norm=True): """Builds a generic decoder layer made of Conv2D-IN-LeakyReLU IN is optional, LeakyReLU may be replaced by ReLU Arguments: (partial) inputs (tensor): the decoder layer input paired_inputs (tensor): the encoder layer output provided by U-Net skip connection & concatenated to inputs. """ conv = Conv2DTranspose(filters=filters, kernel_size=kernel_size, strides=strides, padding='same') x = inputs if instance_norm: x = InstanceNormalization()(x) if activation == 'relu': x = Activation('relu')(x) else: x = LeakyReLU(alpha=0.2)(x) x = conv(x) x = concatenate([x, paired_inputs]) return x
Example #15
Source File: ConvDEC.py From DEC-DA with MIT License | 5 votes |
def CAE(input_shape=(28, 28, 1), filters=[32, 64, 128, 10]): model = Sequential() if input_shape[0] % 8 == 0: pad3 = 'same' else: pad3 = 'valid' model.add(InputLayer(input_shape)) model.add(Conv2D(filters[0], 5, strides=2, padding='same', activation='relu', name='conv1')) model.add(Conv2D(filters[1], 5, strides=2, padding='same', activation='relu', name='conv2')) model.add(Conv2D(filters[2], 3, strides=2, padding=pad3, activation='relu', name='conv3')) model.add(Flatten()) model.add(Dense(units=filters[3], name='embedding')) model.add(Dense(units=filters[2]*int(input_shape[0]/8)*int(input_shape[0]/8), activation='relu')) model.add(Reshape((int(input_shape[0]/8), int(input_shape[0]/8), filters[2]))) model.add(Conv2DTranspose(filters[1], 3, strides=2, padding=pad3, activation='relu', name='deconv3')) model.add(Conv2DTranspose(filters[0], 5, strides=2, padding='same', activation='relu', name='deconv2')) model.add(Conv2DTranspose(input_shape[2], 5, strides=2, padding='same', name='deconv1')) encoder = Model(inputs=model.input, outputs=model.get_layer('embedding').output) return model, encoder
Example #16
Source File: common.py From imgclsmob with MIT License | 5 votes |
def __init__(self, in_channels, out_channels, kernel_size, strides=1, padding=0, out_padding=0, dilation=1, groups=1, use_bias=True, data_format="channels_last", **kwargs): super(Deconv2d, self).__init__(**kwargs) assert (dilation == 1) assert (groups == 1) assert (in_channels is not None) if isinstance(padding, int): padding = (padding, padding) self.use_crop = (padding[0] > 0) or (padding[1] > 0) if self.use_crop: self.crop = nn.Cropping2D( cropping=padding, data_format=data_format, name="crop") self.conv = nn.Conv2DTranspose( filters=out_channels, kernel_size=kernel_size, strides=strides, padding="valid", output_padding=out_padding, data_format=data_format, dilation_rate=dilation, use_bias=use_bias, name="conv")
Example #17
Source File: sinet.py From imgclsmob with MIT License | 5 votes |
def __init__(self, dim2, classes, out_size, bn_eps, data_format="channels_last", **kwargs): super(SBDecoder, self).__init__(**kwargs) self.decode1 = SBDecodeBlock( channels=classes, out_size=((out_size[0] // 8, out_size[1] // 8) if out_size else None), bn_eps=bn_eps, data_format=data_format, name="decode1") self.decode2 = SBDecodeBlock( channels=classes, out_size=((out_size[0] // 4, out_size[1] // 4) if out_size else None), bn_eps=bn_eps, data_format=data_format, name="decode2") self.conv3c = conv1x1_block( in_channels=dim2, out_channels=classes, bn_eps=bn_eps, activation=(lambda: PReLU2(classes, data_format=data_format, name="activ")), data_format=data_format, name="conv3c") self.output_conv = nn.Conv2DTranspose( filters=classes, kernel_size=2, strides=2, padding="valid", output_padding=0, use_bias=False, data_format=data_format, name="output_conv") self.up = InterpolationBlock( scale_factor=2, out_size=out_size, data_format=data_format, name="up")
Example #18
Source File: model.py From Advanced-Deep-Learning-with-Keras with MIT License | 4 votes |
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
Example #19
Source File: cgan-mnist-4.3.1.py From Advanced-Deep-Learning-with-Keras with MIT License | 4 votes |
def build_generator(inputs, labels, image_size): """Build a Generator Model Inputs are concatenated before Dense layer. Stack of BN-ReLU-Conv2DTranpose to generate fake images. Output activation is sigmoid instead of tanh in orig DCGAN. Sigmoid converges easily. Arguments: inputs (Layer): Input layer of the generator (the z-vector) labels (Layer): Input layer for one-hot vector to condition the inputs image_size: Target size of one side (assuming square image) Returns: generator (Model): Generator Model """ image_resize = image_size // 4 # network parameters kernel_size = 5 layer_filters = [128, 64, 32, 1] x = concatenate([inputs, labels], axis=1) x = Dense(image_resize * image_resize * layer_filters[0])(x) x = Reshape((image_resize, image_resize, layer_filters[0]))(x) for filters in layer_filters: # first two convolution layers use strides = 2 # the last two use strides = 1 if filters > layer_filters[-2]: strides = 2 else: strides = 1 x = BatchNormalization()(x) x = Activation('relu')(x) x = Conv2DTranspose(filters=filters, kernel_size=kernel_size, strides=strides, padding='same')(x) x = Activation('sigmoid')(x) # input is conditioned by labels generator = Model([inputs, labels], x, name='generator') return generator
Example #20
Source File: multiRes.py From MIScnn with GNU General Public License v3.0 | 4 votes |
def create_model_2D(self, input_shape, n_labels=2): # Input layer inputs = Input(input_shape) mresblock1 = MultiResBlock_2D(32, inputs) pool1 = MaxPooling2D(pool_size=(2, 2))(mresblock1) mresblock1 = ResPath_2D(32, 4, mresblock1) mresblock2 = MultiResBlock_2D(32*2, pool1) pool2 = MaxPooling2D(pool_size=(2, 2))(mresblock2) mresblock2 = ResPath_2D(32*2, 3, mresblock2) mresblock3 = MultiResBlock_2D(32*4, pool2) pool3 = MaxPooling2D(pool_size=(2, 2))(mresblock3) mresblock3 = ResPath_2D(32*4, 2, mresblock3) mresblock4 = MultiResBlock_2D(32*8, pool3) pool4 = MaxPooling2D(pool_size=(2, 2))(mresblock4) mresblock4 = ResPath_2D(32*8, 1, mresblock4) mresblock5 = MultiResBlock_2D(32*16, pool4) up6 = concatenate([Conv2DTranspose( 32*8, (2, 2), strides=(2, 2), padding='same')(mresblock5), mresblock4], axis=3) mresblock6 = MultiResBlock_2D(32*8, up6) up7 = concatenate([Conv2DTranspose( 32*4, (2, 2), strides=(2, 2), padding='same')(mresblock6), mresblock3], axis=3) mresblock7 = MultiResBlock_2D(32*4, up7) up8 = concatenate([Conv2DTranspose( 32*2, (2, 2), strides=(2, 2), padding='same')(mresblock7), mresblock2], axis=3) mresblock8 = MultiResBlock_2D(32*2, up8) up9 = concatenate([Conv2DTranspose(32, (2, 2), strides=( 2, 2), padding='same')(mresblock8), mresblock1], axis=3) mresblock9 = MultiResBlock_2D(32, up9) conv10 = conv2d_bn(mresblock9, n_labels, 1, 1, activation=self.activation) model = Model(inputs=[inputs], outputs=[conv10]) return model #---------------------------------------------# # Create 3D Model # #---------------------------------------------#
Example #21
Source File: dcgan-mnist-4.2.1.py From Advanced-Deep-Learning-with-Keras with MIT License | 4 votes |
def build_generator(inputs, image_size): """Build a Generator Model Stack of BN-ReLU-Conv2DTranpose to generate fake images Output activation is sigmoid instead of tanh in [1]. Sigmoid converges easily. Arguments: inputs (Layer): Input layer of the generator the z-vector) image_size (tensor): Target size of one side (assuming square image) Returns: generator (Model): Generator Model """ image_resize = image_size // 4 # network parameters kernel_size = 5 layer_filters = [128, 64, 32, 1] x = Dense(image_resize * image_resize * layer_filters[0])(inputs) x = Reshape((image_resize, image_resize, layer_filters[0]))(x) for filters in layer_filters: # first two convolution layers use strides = 2 # the last two use strides = 1 if filters > layer_filters[-2]: strides = 2 else: strides = 1 x = BatchNormalization()(x) x = Activation('relu')(x) x = Conv2DTranspose(filters=filters, kernel_size=kernel_size, strides=strides, padding='same')(x) x = Activation('sigmoid')(x) generator = Model(inputs, x, name='generator') return generator
Example #22
Source File: DeepLabCut.py From DeepPoseKit with Apache License 2.0 | 4 votes |
def __init_model__(self): if self.input_shape[-1] is 1: inputs = Concatenate()([self.inputs] * 3) else: inputs = self.inputs if self.backbone in list(MODELS.keys()): normalized = ImageNetPreprocess(self.backbone)(inputs) else: raise ValueError( "backbone model {} is not supported. Must be one of {}".format( self.backbone, list(MODELS.keys()) ) ) backbone = MODELS[self.backbone] if self.backbone in list(MODELS.keys()): input_shape = None # self.input_shape[:-1] + (3,) if self.backbone.startswith("mobile"): input_shape = None backbone = partial(backbone, alpha=self.alpha) pretrained_model = backbone( include_top=False, weights=self.weights, input_shape=input_shape ) pretrained_features = pretrained_model(normalized) if self.train_generator.downsample_factor is 4: x = pretrained_features x_out = Conv2D(self.train_generator.n_output_channels, (1, 1))(x) elif self.train_generator.downsample_factor is 3: x = pretrained_features x_out = Conv2DTranspose( self.train_generator.n_output_channels, (3, 3), strides=(2, 2), padding="same", )(x) elif self.train_generator.downsample_factor is 2: x = pretrained_features x = SubPixelUpscaling()(x) x_out = Conv2DTranspose( self.train_generator.n_output_channels, (3, 3), strides=(2, 2), padding="same", )(x) else: raise ValueError( "`downsample_factor={}` is not supported for DeepLabCut. Adjust your TrainingGenerator".format( self.train_generator.downsample_factor ) ) self.train_model = Model(self.inputs, x_out, name=self.__class__.__name__)