def __init__(self): super(DurationPredictor, self).__init__() self.layers = nn.Sequential( ResidualBlock(hp.channels, 4, 1, n=1, norm=hp.normalize, activation=nn.ReLU), ResidualBlock(hp.channels, 3, 1, n=1, norm=hp.normalize, activation=nn.ReLU), ResidualBlock(hp.channels, 1, 1, n=1, norm=hp.normalize, activation=nn.ReLU), Conv1d(hp.channels, 1))
def create_network(Zt, Ct, channels=1, encoder_filters=[64, 128, 256], encoder_ks=[7, 3, 3], encoder_strides=[1, 2, 2], resblock_filters=[256, 256, 256, 256, 256, 256], resblock_ks=[3, 3, 3, 3, 3, 3], decoder_filters=[128, 64], decoder_ks=[3, 3, 7], decoder_strides=[2, 2, 1]): with tf.name_scope("Gen"): Z = kl.Input(( None, None, channels, ), tensor=Zt, name="Z") C = kl.Input(( None, None, channels, ), tensor=Ct, name="C") # Encoder layer = C for l in range(len(encoder_filters)): layer = kl.Conv2D(filters=encoder_filters[l], kernel_size=encoder_ks[l], padding="same", activation="relu", strides=encoder_strides[l])(layer) layer = InstanceNormalization()(layer) layer = kl.concatenate([layer, Z]) # Transformer for l in range(len(resblock_filters)): layer = ResidualBlock(resblock_filters[l] + channels, nb_layers=3, kernel_size=resblock_ks[l], normalization="instancenorm")(layer) # Decoder for l in range(len(decoder_filters)): layer = kl.Conv2DTranspose(filters=decoder_filters[l], kernel_size=decoder_ks[l], padding="same", strides=decoder_strides[l], activation="relu")(layer) layer = InstanceNormalization()(layer) G_out = kl.Conv2D(filters=channels, kernel_size=decoder_ks[-1], strides=decoder_strides[-1], activation="tanh", padding="same")(layer) model = k.Model(inputs=[Z, C], outputs=G_out, name="G") return model
def __init__(self): super(Decoder, self).__init__() self.res_blocks = nn.Sequential( *[ResidualBlock(hp.channels, hp.dec_kernel_size, d, n=2, norm=hp.normalize, activation=hp.activation) for d in hp.dec_dilations], ) self.post_net1 = nn.Sequential( Conv1d(hp.channels, hp.channels), ) self.post_net2 = nn.Sequential( ResidualBlock(hp.channels, hp.dec_kernel_size, 1, n=2), Conv1d(hp.channels, hp.out_channels), hp.final_activation() )
def build_cnn(feat_dim=(1024, 14, 14), res_block_dim=128, num_res_blocks=0, proj_dim=512, pooling='maxpool2'): C, H, W = feat_dim layers = [] if num_res_blocks > 0: layers.append(nn.Conv2d(C, res_block_dim, kernel_size=3, padding=1)) layers.append(nn.ReLU(inplace=True)) C = res_block_dim for _ in range(num_res_blocks): layers.append(ResidualBlock(C)) if proj_dim > 0: layers.append(nn.Conv2d(C, proj_dim, kernel_size=1, padding=0)) layers.append(nn.ReLU(inplace=True)) C = proj_dim if pooling == 'maxpool2': layers.append(nn.MaxPool2d(kernel_size=2, stride=2)) H, W = H // 2, W // 2 return nn.Sequential(*layers), (C, H, W)
def __init__(self): super(Encoder, self).__init__() self.prenet = nn.Sequential( nn.Embedding(hp.alphabet_size, hp.channels, padding_idx=0), Conv1d(hp.channels, hp.channels), hp.activation(), ) self.res_blocks = nn.Sequential(*[ ResidualBlock(hp.channels, hp.enc_kernel_size, d, n=2, norm=hp.normalize, activation=hp.activation) for d in hp.enc_dilations ]) self.post_net1 = nn.Sequential(Conv1d(hp.channels, hp.channels), ) self.post_net2 = nn.Sequential(hp.activation(), hp.normalize(hp.channels), Conv1d(hp.channels, hp.channels))
def BasicBlock(d, k, delta): if hp.ssrn_basic_block == 'gated_conv': return GatedConvBlock(d, k, delta, causal=False, weight_init=hp.ssrn_weight_init, normalization=hp.ssrn_normalization) elif hp.ssrn_basic_block == 'highway': return HighwayBlock(d, k, delta, causal=False, weight_init=hp.ssrn_weight_init, normalization=hp.ssrn_normalization) else: return ResidualBlock(d, k, delta, causal=False, weight_init=hp.ssrn_weight_init, normalization=hp.ssrn_normalization, widening_factor=1)
def build_DSen2(self): input_pan_lyr = tf.keras.Input(self.pan_lr_shape) input_mul_lyr = tf.keras.Input(self.mul_lr_shape) up_sample_mul = layers.UpSampling2D(size=(2, 2))(input_mul_lyr) x = feature_concatenate = layers.Concatenate()( [input_pan_lyr, up_sample_mul]) # make the input for residual block # use 128 filters as same as paper x = layers.Conv2D(filters=128, kernel_size=3, strides=1, padding="same")(x) for _ in range(self.residual_block_num): x = ResidualBlock()(x) x = layers.Conv2D(filters=self.mul_hr_shape[2], kernel_size=1, strides=1, padding="same")(x) output_lyr = layers.Add()([up_sample_mul, x]) model = tf.keras.Model(inputs=[input_pan_lyr, input_mul_lyr], outputs=output_lyr) return model
def __init__(self, num_classes, bow_training=False): """Define layers""" super().__init__() print("[**] Using BowNet1") self.num_classes = num_classes self.bow_training = bow_training self.conv1_64 = nn.Conv2d(in_channels=3, out_channels=64, kernel_size=3, stride=1, padding=1) self.bn1_64 = nn.BatchNorm2d(num_features=64) self.relu1_64 = nn.ReLU() # Resblock 1 self.resblock1_64a = ResidualBlock(in_channels=64, out_channels=64, kernel_size=3, downsample_factor=2) self.resblock1_64b = ResidualBlock(in_channels=64, out_channels=64, kernel_size=3, downsample_factor=1) # Resblock 2 self.resblock2_128a = ResidualBlock(in_channels=64, out_channels=128, kernel_size=3, downsample_factor=1) self.resblock2_128b = ResidualBlock(in_channels=128, out_channels=128, kernel_size=3, downsample_factor=1) # Resblock 3 self.resblock3_256a = ResidualBlock(in_channels=128, out_channels=256, kernel_size=3, downsample_factor=2) self.resblock3_256b = ResidualBlock(in_channels=256, out_channels=256, kernel_size=3, downsample_factor=1) # Rotation prediction head self.resblock4_512a = ResidualBlock(in_channels=256, out_channels=512, kernel_size=3, downsample_factor=1) self.resblock4_512b = ResidualBlock(in_channels=512, out_channels=512, kernel_size=3, downsample_factor=1) self.global_avg_pool = nn.AvgPool2d(kernel_size=8, stride=1) if self.bow_training: self.fc_fin = 256 self.fc_out = NormalizedLinear(self.fc_fin, self.num_classes) else: self.fc_out = nn.Linear(512, self.num_classes) self.fc_fin = 512 self.initialize()