def __init__(self, intLevel): super(Matching, self).__init__() self.fltBackwarp = [0.0, 0.0, 10.0, 5.0, 2.5, 1.25, 0.625][intLevel] if intLevel != 2: self.moduleFeat = dg.Sequential() elif intLevel == 2: self.moduleFeat = dg.Sequential( ('0', conv1x1(in_channels=32, out_channels=64)), ('1', LeakyReLU()) ) if intLevel == 6: self.moduleUpflow = None elif intLevel != 6: self.moduleUpflow = deconv(in_channels=2, out_channels=2, groups=2) if intLevel >= 4: self.moduleUpcorr = None elif intLevel < 4: self.moduleUpcorr = deconv(in_channels=49, out_channels=49, groups=49) self.moduleMain = dg.Sequential( ('0', conv3x3(in_channels=49, out_channels=128)), ('1', LeakyReLU()), ('2', conv3x3(in_channels=128, out_channels=64)), ('3', LeakyReLU()), ('4', conv3x3(in_channels=64, out_channels=32)), ('5', LeakyReLU()), ('6', conv2d(in_channels=32, out_channels=2, kernel_size=[0, 0, 7, 5, 5, 3, 3][intLevel], stride=1, padding=[0, 0, 3, 2, 2, 1, 1][intLevel])) )
def __init__(self, dim=300, K=65536, m=0.999, T=0.07, mlp=False): """ dim: feature dimension (default: 128) K: queue size; number of negative keys (default: 65536) m: moco momentum of updating key encoder (default: 0.999) T: softmax temperature (default: 0.07) """ super(MoCo, self).__init__() self.K = K self.m = m self.T = T # create the encoders self.encoder_q = ErnieModelForSequenceClassification.from_pretrained('ernie-2.0-large-en', num_labels=dim) self.encoder_k = ErnieModelForSequenceClassification.from_pretrained('ernie-2.0-large-en', num_labels=dim) if mlp: dim_mlp = 1024 self.encoder_q.classifier = D.Sequential(D.Linear(dim_mlp, dim_mlp, act='relu'), self.encoder_q.classifier) self.encoder_k.classifier = D.Sequential(D.Linear(dim_mlp, dim_mlp,act='relu'), self.encoder_k.classifier) for param_q, param_k in zip(self.encoder_q.parameters(), self.encoder_k.parameters()): param_k=param_q # initialize param_k.requires_grad = False # not update by gradient # create the queue self.queue = L.randn([dim, K]) self.queue = norm(self.queue, dim=0) self.queue_ptr = L.zeros([1], dtype='int32')
def __init__(self, ): super(BodyPose, self).__init__() features = [ ('conv1_1', ([3, 64], 'conv3x3', 'relu')), ('conv1_2', ([64, 64], 'conv3x3', 'relu')), ('pool1_stage1', ([2, 2, 0], None, None)), ('conv2_1', ([64, 128], 'conv3x3', 'relu')), ('conv2_2', ([128, 128], 'conv3x3', 'relu')), ('pool2_stage1', ([2, 2, 0], None, None)), ('conv3_1', ([128, 256], 'conv3x3', 'relu')), ('conv3_2', ([256, 256], 'conv3x3', 'relu')), ('conv3_3', ([256, 256], 'conv3x3', 'relu')), ('conv3_4', ([256, 256], 'conv3x3', 'relu')), ('pool3_stage1', ([2, 2, 0], None, None)), ('conv4_1', ([256, 512], 'conv3x3', 'relu')), ('conv4_2', ([512, 512], 'conv3x3', 'relu')), ('conv4_3_CPM', ([512, 256], 'conv3x3', 'relu')), ('conv4_4_CPM', ([256, 128], 'conv3x3', 'relu')), ] features = make_layers(features) self.feature = dg.Sequential(*features) # PAF: Part Affinity Field PAF_stage1 = [ ('conv5_1_CPM_L1', ([128, 128], 'conv3x3', 'relu')), ('conv5_2_CPM_L1', ([128, 128], 'conv3x3', 'relu')), ('conv5_3_CPM_L1', ([128, 128], 'conv3x3', 'relu')), ('conv5_4_CPM_L1', ([128, 512], 'conv1x1', 'relu')), ('conv5_5_CPM_L1', ([512, 28], 'conv1x1', 'no_relu')), ] PAF_stage1 = make_layers(PAF_stage1) self.PAF_stage1 = dg.Sequential(*PAF_stage1) # CHM: Confidence HeatMap CHM_stage1 = [ ('conv5_1_CPM_L2', ([128, 128], 'conv3x3', 'relu')), ('conv5_2_CPM_L2', ([128, 128], 'conv3x3', 'relu')), ('conv5_3_CPM_L2', ([128, 128], 'conv3x3', 'relu')), ('conv5_4_CPM_L2', ([128, 512], 'conv1x1', 'relu')), ('conv5_5_CPM_L2', ([512, 16], 'conv1x1', 'no_relu')), ] CHM_stage1 = make_layers(CHM_stage1) self.CHM_stage1 = dg.Sequential(*CHM_stage1) self.PAF_stage2 = PoseBlock(172, 128, 28, 2, 1) self.PAF_stage3 = PoseBlock(172, 128, 28, 3, 1) self.PAF_stage4 = PoseBlock(172, 128, 28, 4, 1) self.PAF_stage5 = PoseBlock(172, 128, 28, 5, 1) self.PAF_stage6 = PoseBlock(172, 128, 28, 6, 1) self.CHM_stage2 = PoseBlock(172, 128, 16, 2, 2) self.CHM_stage3 = PoseBlock(172, 128, 16, 3, 2) self.CHM_stage4 = PoseBlock(172, 128, 16, 4, 2) self.CHM_stage5 = PoseBlock(172, 128, 16, 5, 2) self.CHM_stage6 = PoseBlock(172, 128, 16, 6, 2)
def __init__(self, name=None, num=None): super(TSNResNet, self).__init__() self.convbn = convbn(3, 16) self.convpools = dygraph.Sequential(convpool(16, 32, pooling=4), convpool(32, 64, pooling=4), convpool(64, 128)) self.fcs = dygraph.Sequential( dygraph.Linear(7 * 7 * 128, 1024, act='relu'), dygraph.BatchNorm(1024), dygraph.Dropout(0.5), dygraph.Linear(1024, 101, act='softmax')) self.seg_num = 32
def __init__(self, in_channel, hidden_channel1, hidden_channel2, out_channel): super(PoseBlock, self).__init__() self.sub_block1 = DenseNetBlock(in_channel , hidden_channel1) self.sub_block2 = DenseNetBlock(hidden_channel1 * 3, hidden_channel1) self.sub_block3 = DenseNetBlock(hidden_channel1 * 3, hidden_channel1) self.sub_block4 = DenseNetBlock(hidden_channel1 * 3, hidden_channel1) self.sub_block5 = DenseNetBlock(hidden_channel1 * 3, hidden_channel1) self.sub_block6 = dg.Sequential( ('conv', conv1x1(hidden_channel1 * 3, hidden_channel2)), ('prelu', dg.PRelu(mode='channel', channel=hidden_channel2)), ) self.sub_block7 = dg.Sequential( ('conv', conv1x1(hidden_channel2, out_channel)) )
def __init__(self, intLevel): super(Regularization, self).__init__() self.fltBackward = [0.0, 0.0, 10.0, 5.0, 2.5, 1.25, 0.625][intLevel] self.intUnfold = [0, 0, 7, 5, 5, 3, 3][intLevel] if intLevel >= 5: self.moduleFeat = dg.Sequential() elif intLevel < 5: self.moduleFeat = dg.Sequential( ('0', conv1x1(in_channels=[0, 0, 32, 64, 96, 128, 192][intLevel], out_channels=128)), ('1', LeakyReLU()) ) self.moduleMain = dg.Sequential( ('0', conv3x3(in_channels=[0, 0, 131, 131, 131, 131, 195][intLevel], out_channels=128)), ('1', LeakyReLU()), ('2', conv3x3(in_channels=128, out_channels=128)), ('3', LeakyReLU()), ('4', conv3x3(in_channels=128, out_channels=64)), ('5', LeakyReLU()), ('6', conv3x3(in_channels=64, out_channels=64)), ('7', LeakyReLU()), ('8', conv3x3(in_channels=64, out_channels=32)), ('9', LeakyReLU()), ('10', conv3x3(in_channels=32, out_channels=32)), ('11', LeakyReLU()) ) if intLevel >= 5: self.moduleDist = dg.Sequential( ('0', conv2d(in_channels=32, out_channels=[0, 0, 49, 25, 25, 9, 9][intLevel], kernel_size=[0, 0, 7, 5, 5, 3, 3][intLevel], stride=1, padding=[0, 0, 3, 2, 2, 1, 1][intLevel])) ) elif intLevel < 5: self.moduleDist = dg.Sequential( ('0', conv2d(in_channels=32, out_channels=[0, 0, 49, 25, 25, 9, 9][intLevel], kernel_size=([0, 0, 7, 5, 5, 3, 3][intLevel], 1), stride=1, padding=([0, 0, 3, 2, 2, 1, 1][intLevel], 0))), ('1', conv2d(in_channels=[0, 0, 49, 25, 25, 9, 9][intLevel], out_channels=[0, 0, 49, 25, 25, 9, 9][intLevel], kernel_size=(1, [0, 0, 7, 5, 5, 3, 3][intLevel]), stride=1, padding=(0, [0, 0, 3, 2, 2, 1, 1][intLevel]))) ) self.moduleScaleX = conv1x1(in_channels=[0, 0, 49, 25, 25, 9, 9][intLevel], out_channels=1) self.moduleScaleY = conv1x1(in_channels=[0, 0, 49, 25, 25, 9, 9][intLevel], out_channels=1)
def __init__(self, dim, use_bias): super(ResnetBlock, self).__init__() conv_block = [] # conv_block += [nn.ReflectionPad2d(1), # nn.Conv2d(dim, dim, kernel_size=3, stride=1, padding=0, bias=use_bias), # nn.InstanceNorm2d(dim), # nn.ReLU(True)] conv_block += [ ReflectionPad2d(1), dygraph.Conv2D(dim, dim, 3, bias_attr=use_bias), dygraph.InstanceNorm(dim), ReLU(True) ] # conv_block += [nn.ReflectionPad2d(1), # nn.Conv2d(dim, dim, kernel_size=3, stride=1, padding=0, bias=use_bias), # nn.InstanceNorm2d(dim)] conv_block += [ ReflectionPad2d(1), dygraph.Conv2D(dim, dim, 3, bias_attr=use_bias), dygraph.InstanceNorm(dim) ] # self.conv_block = nn.Sequential(*conv_block) self.conv_block = dygraph.Sequential(*conv_block)
def __init__(self, num_features, cond_dims, num_filters=0, kernel_size=3, weight_norm_type='', activation_norm_type='sync_batch', is_hyper=True): super().__init__() padding = kernel_size // 2 mlps = [] if type(cond_dims) != list: cond_dims = [cond_dims] for i, cond_dim in enumerate(cond_dims): mlp = [] if not is_hyper or (i != 0): if num_filters > 0: mlp += [(str(i), Conv2dBlock(cond_dim, num_filters, kernel_size, padding=padding, weight_norm_type=weight_norm_type, nonlinearity='relu'))] mlp_ch = cond_dim if num_filters == 0 else num_filters mlp += [(str(len(mlp)), Conv2dBlock(mlp_ch, num_features * 2, kernel_size, padding=padding, weight_norm_type=weight_norm_type))] mlp = dg.Sequential(*mlp) else: if num_filters > 0: raise ValueError('Multi hyper layer not supported yet.') mlp = HyperConv2D(padding=padding) mlps.append(mlp) self.mlps = dg.LayerList(mlps) self.norm = get_activation_norm_layer(num_features, activation_norm_type, 2, affine=False) self.conditional = True
def __init__(self, ): super(BodyPose, self).__init__() feature = [ ('conv1_1', ([3, 64], 'conv3x3', 'relu')), ('conv1_2', ([64, 64], 'conv3x3', 'relu')), ('pool1_stage1', ([2, 2, 0], None, None)), ('conv2_1', ([64, 128], 'conv3x3', 'relu')), ('conv2_2', ([128, 128], 'conv3x3', 'relu')), ('pool2_stage1', ([2, 2, 0], None, None)), ('conv3_1', ([128, 256], 'conv3x3', 'relu')), ('conv3_2', ([256, 256], 'conv3x3', 'relu')), ('conv3_3', ([256, 256], 'conv3x3', 'relu')), ('conv3_4', ([256, 256], 'conv3x3', 'relu')), ('pool3_state1', ([2, 2, 0], None, None)), ('conv4_1', ([256, 512], 'conv3x3', 'relu')), ('conv4_2', ([512, 512], 'conv3x3', 'relu')), ('conv4_3_CPM', ([512, 256], 'conv3x3','prelu')), ('conv4_4_CPM', ([256, 128], 'conv3x3','prelu')), ] feature = make_layers(feature) self.features = dg.Sequential(*feature) # Part Affinity Field blocks self.PAF_block0 = PoseBlock(128, 96, 256, 52) self.PAF_block1 = PoseBlock(180, 128, 512, 52) self.PAF_block2 = PoseBlock(180, 128, 512, 52) self.PAF_block3 = PoseBlock(180, 128, 512, 52) # Confidence Heatmap blocks self.CHM_block0 = PoseBlock(180, 96, 256, 26) self.CHM_block1 = PoseBlock(206, 128, 512, 26)
def __init__(self, num_features, cond_dims, num_filters=128, kernel_size=3, weight_norm_type='', separate_projection=False, activation_norm_type='sync_batch', activation_norm_params=None, partial=False): super().__init__() if activation_norm_params is None: activation_norm_params = SimpleNamespace(affine=False) padding = kernel_size // 2 self.separate_projection = separate_projection mlps = [] gammas = [] betas = [] # Make cond_dims a list. if type(cond_dims) != list: cond_dims = [cond_dims] # Make num_filters a list if not isinstance(num_filters, list): num_filters = [num_filters] * len(cond_dims) else: assert len(num_filters) >= len(cond_dims) # Make partial a list. if not isinstance(partial, list): partial = [partial] * len(cond_dims) else: assert len(partial) >= len(cond_dims) for i, cond_dim in enumerate(cond_dims): mlp = [] conv_block = PartialConv2dBlock if partial[i] else Conv2dBlock sequential = PartialSequential if partial[i] else dg.Sequential if num_filters[i] > 0: mlp += [(str(i), conv_block(cond_dim, num_filters[i], kernel_size, padding=padding, weight_norm_type=weight_norm_type, nonlinearity='relu'))] mlp_ch = cond_dim if num_filters[i] == 0 else num_filters[i] if self.separate_projection: if partial[i]: raise NotImplementedError("Separate projection not yet implemented for partial conv") mlps.append(dg.Sequential(*mlp)) gammas.append((str(i), conv_block(mlp_ch, num_features, kernel_size, padding=padding, weight_norm_type=weight_norm_type))) betas.append((str(i), conv_block(mlp_ch, num_features, kernel_size, padding=padding, weight_norm_type=weight_norm_type))) else: mlp += [(str(i), conv_block(mlp_ch, num_features * 2, kernel_size, padding=padding, weight_norm_type=weight_norm_type))] mlps.append(sequential(*mlp)) self.mlps = dg.LayerList(mlps) self.gammas = dg.LayerList(gammas) self.betas = dg.LayerList(betas) self.norm = get_activation_norm_layer(num_features, activation_norm_type, 2, **vars(activation_norm_params)) self.conditional = True
def __init__(self, ): super(FacePose, self).__init__() features = [ ('conv1_1', ([3, 64], 'conv3x3', 'relu')), ('conv1_2', ([64, 64], 'conv3x3', 'relu')), ('pool1_stage1', ([2, 2, 0], None, None)), ('conv2_1', ([64, 128], 'conv3x3', 'relu')), ('conv2_2', ([128, 128], 'conv3x3', 'relu')), ('pool2_stage1', ([2, 2, 0], None, None)), ('conv3_1', ([128, 256], 'conv3x3', 'relu')), ('conv3_2', ([256, 256], 'conv3x3', 'relu')), ('conv3_3', ([256, 256], 'conv3x3', 'relu')), ('conv3_4', ([256, 256], 'conv3x3', 'relu')), ('pool3_stage1', ([2, 2, 0], None, None)), ('conv4_1', ([256, 512], 'conv3x3', 'relu')), ('conv4_2', ([512, 512], 'conv3x3', 'relu')), ('conv4_3', ([512, 512], 'conv3x3', 'relu')), ('conv4_4', ([512, 512], 'conv3x3', 'relu')), ('conv5_1', ([512, 512], 'conv3x3', 'relu')), ('conv5_2', ([512, 512], 'conv3x3', 'relu')), ('conv5_3_CPM', ([512, 128], 'conv3x3', 'relu')), ] features = make_layers(features) self.feature = dg.Sequential(*features) # PAF: Part Affinity Field stage1 = [ ('conv6_1_CPM', ([128, 512], 'conv1x1', 'relu')), ('conv6_2_CPM', ([512, 71], 'conv1x1', 'no_relu')), ] stage1 = make_layers(stage1) self.stage1 = dg.Sequential(*stage1) self.stage2 = PoseBlock(199, 128, 71, 2, 1) self.stage3 = PoseBlock(199, 128, 71, 3, 1) self.stage4 = PoseBlock(199, 128, 71, 4, 1) self.stage5 = PoseBlock(199, 128, 71, 5, 1) self.stage6 = PoseBlock(199, 128, 71, 6, 1)
def _make_layer(self, block, planes, blocks, stride=1, dilate=False): norm_layer = self._norm_layer downsample = None previous_dilation = self.dilation if dilate: self.dilation *= stride stride = 1 if stride != 1 or self.inplanes != planes * block.expansion: downsample = dg.Sequential( ("0", conv1x1(self.inplanes, planes * block.expansion, stride)), ("1", norm_layer(planes * block.expansion)), ) layers = [] layers.append(("0", block(self.inplanes, planes, stride, downsample, self.groups, self.base_width, previous_dilation, norm_layer))) self.inplanes = planes * block.expansion for i in range(1, blocks): layers.append((str(i), block(self.inplanes, planes, groups=self.groups, base_width=self.base_width, dilation=self.dilation, norm_layer=norm_layer))) return dg.Sequential(*layers)
def __init__(self, stem, stages, out_features): super().__init__() self.stem = stem self.stages_and_names = [] for i, blocks in enumerate(stages): name = "res" + str(i + 2) stage = dg.Sequential(*blocks) self.add_sublayer(name, stage) self.stages_and_names.append((name, stage)) self._out_features = out_features
def _make_layer(self, block, planes, num_blocks, stride=1, dilate=False): norm_layer = self._norm_layer downsample = None if stride != 1 or self.inplanes != planes * block.expansion: downsample = dg.Sequential( conv1x1(self.inplanes, planes * block.expansion, stride), norm_layer(planes * block.expansion)) layers = [] layers.append( block(self.inplanes, planes, stride, downsample, self.groups, self.base_width, self.dilation, norm_layer)) self.inplanes = planes * block.expansion for _ in range(1, num_blocks): layers.append( block(self.inplanes, planes, groups=self.groups, base_width=self.base_width, dilation=self.dilation, norm_layer=norm_layer)) return dg.Sequential(*layers)
def __init__(self): super(HarFcn, self).__init__() self.cnn1 = dy.Sequential( dy.Conv2D(num_channels=1, num_filters=128, filter_size=3, stride=1, padding=1), dy.BatchNorm(num_channels=128), dy.Dropout(p=.2), ) self.cnn2 = dy.Sequential( dy.Conv2D(num_channels=128, num_filters=128, filter_size=3, stride=1, padding=1), dy.BatchNorm(num_channels=128), dy.Dropout(p=.2), ) self.cnn3 = dy.Sequential( dy.Conv2D(num_channels=128, num_filters=128, filter_size=3, stride=1, padding=1), dy.BatchNorm(num_channels=128), dy.Dropout(p=.2), ) self.cls = dy.Sequential( dy.Linear(input_dim=384, output_dim=128), dy.Dropout(p=.2), dy.Linear(input_dim=128, output_dim=5), )
def __init__(self, intLevel): super(Subpixel, self).__init__() self.fltBackward = [ 0.0, 0.0, 10.0, 5.0, 2.5, 1.25, 0.625][intLevel] if intLevel != 2: self.moduleFeat = dg.Sequential() elif intLevel == 2: self.moduleFeat = dg.Sequential( ('0', conv1x1(in_channels=32, out_channels=64)), ('1', LeakyReLU()) ) self.moduleMain = dg.Sequential( ('0', conv3x3(in_channels=[0, 0, 130, 130, 194, 258, 386][intLevel], out_channels=128)), ('1', LeakyReLU()), ('2', conv3x3(in_channels=128, out_channels=64)), ('3', LeakyReLU()), ('4', conv3x3(in_channels=64, out_channels=32)), ('5', LeakyReLU()), ('6', conv2d(in_channels=32, out_channels=2, kernel_size=[0, 0, 7, 5, 5, 3, 3][intLevel], stride=1, padding=[0, 0, 3, 2, 2, 1, 1][intLevel])) )
def __init__(self): super(MNIST, self).__init__() self.cnn = dy.Conv2D(num_channels=3, num_filters=1, filter_size=3, stride=1, padding=1, act='relu') self.cls = dy.Sequential( dy.Linear(input_dim=784, output_dim=128), dy.Dropout(p=.2), dy.Linear(input_dim=128, output_dim=5), )
def __init__(self, kernel_size, num_input_channels, num_filters, num_layers, max_num_filters, activation_norm_type, weight_norm_type): super(NLayerPatchDiscriminator, self).__init__() self.num_layers = num_layers padding = int(np.floor((kernel_size - 1.0) / 2)) nonlinearity = 'leakyrelu' base_conv2d_block = functools.partial(Conv2dBlock, kernel_size=kernel_size, padding=padding, weight_norm_type=weight_norm_type, activation_norm_type=activation_norm_type, nonlinearity=nonlinearity, order='CNA') layers = [[base_conv2d_block(num_input_channels, num_filters, stride=2)]] for n in range(num_layers): num_filters_prev = num_filters num_filters = min(num_filters * 2, max_num_filters) stride = 2 if n < (num_layers - 1) else 1 layers += [[base_conv2d_block(num_filters_prev, num_filters, stride=stride)]] layers += [[Conv2dBlock(num_filters, 1, 3, 1, padding, weight_norm_type=weight_norm_type)]] for n in range(len(layers)): self.add_sublayer('layer' + str(n), dg.Sequential(*layers[n]))
def __init__(self, input_channels, num_filter, groups, name=None, use_bias=False): super(conv_block, self).__init__() self._layers = [] i = 0 self.conv_in = dygraph.Conv2D( num_channels=input_channels, num_filters=num_filter, filter_size=3, stride=1, padding=1, act='relu', param_attr=fluid.param_attr.ParamAttr(name=name + str(i + 1) + "_weights"), bias_attr=False if not use_bias else fluid.param_attr.ParamAttr( name=name + str(i + 1) + "_bias")) if groups == 1: return for i in range(1, groups): _a = dygraph.Conv2D( num_channels=num_filter, num_filters=num_filter, filter_size=3, stride=1, padding=1, act='relu', param_attr=fluid.param_attr.ParamAttr(name=name + str(i + 1) + "_weights"), bias_attr=False if not use_bias else fluid.param_attr.ParamAttr(name=name + str(i + 1) + "_bias")) self._layers.append(_a) self.conv = dygraph.Sequential(*self._layers)
def __init__(self, in_channel, hidden_channel, out_channel, stage_idx, branch_idx, layer_num=5): super(PoseBlock, self).__init__() in_channels = [in_channel] + [hidden_channel] * (layer_num - 1) sub_layers = [] for i in range(0, layer_num): sub_layers.append( ('Mconv%d_stage%d_L%d' % (i + 1, stage_idx, branch_idx), ([in_channels[i], hidden_channel], 'conv7x7', 'relu'))) sub_layers.append(('Mconv6_stage%d_L%d' % (stage_idx, branch_idx), ([hidden_channel, hidden_channel], 'conv1x1', 'relu'))) sub_layers.append(('Mconv7_stage%d_L%d' % (stage_idx, branch_idx), ([hidden_channel, out_channel], 'conv1x1', 'no_relu'))) sub_layers = make_layers(sub_layers) self.sub_layers = dg.Sequential(*sub_layers)
def __init__(self): super(Features, self).__init__() self.moduleOne = dg.Sequential( ('0', conv2d(in_channels=3, out_channels=32, kernel_size=7, stride=1, padding=3)), ('1', LeakyReLU()) ) self.moduleTwo = dg.Sequential( ('0', conv3x3(in_channels=32, out_channels=32, stride=2)), ('1', LeakyReLU()), ('2', conv3x3(in_channels=32, out_channels=32)), ('3', LeakyReLU()), ('4', conv3x3(in_channels=32, out_channels=32)), ('5', LeakyReLU()) ) self.moduleThr = dg.Sequential( ('0', conv3x3(in_channels=32, out_channels=64, stride=2)), ('1', LeakyReLU()), ('2', conv3x3(in_channels=64, out_channels=64)), ('3', LeakyReLU()) ) self.moduleFou = dg.Sequential( ('0', conv3x3(in_channels=64, out_channels=96, stride=2)), ('1', LeakyReLU()), ('2', conv3x3(in_channels=96, out_channels=96)), ('3', LeakyReLU()) ) self.moduleFiv = dg.Sequential( ('0', conv3x3(in_channels=96, out_channels=128, stride=2)), ('1', LeakyReLU()) ) self.moduleSix = dg.Sequential( ('0', conv3x3(in_channels=128, out_channels=192, stride=2)), ('1', LeakyReLU()) )
def __init__(self, input_nc, ndf=64, n_layers=5): super(Discriminator, self).__init__() # model = [nn.ReflectionPad2d(1), # nn.utils.spectral_norm( # nn.Conv2d(input_nc, ndf, kernel_size=4, stride=2, padding=0, bias=True)), # nn.LeakyReLU(0.2, True)] model = [ ReflectionPad2d(1), SpectralNorm(dygraph.Conv2D(input_nc, ndf, 4, stride=2), dim=1), LeakyReLU(0.2, True) ] for i in range(1, n_layers - 2): mult = 2**(i - 1) # model += [nn.ReflectionPad2d(1), # nn.utils.spectral_norm( # nn.Conv2d(ndf * mult, ndf * mult * 2, kernel_size=4, stride=2, padding=0, bias=True)), # nn.LeakyReLU(0.2, True)] model += [ ReflectionPad2d(1), SpectralNorm(dygraph.Conv2D(ndf * mult, ndf * mult * 2, 4, stride=2), dim=1), LeakyReLU(0.2, True) ] mult = 2**(n_layers - 2 - 1) # model += [nn.ReflectionPad2d(1), # nn.utils.spectral_norm( # nn.Conv2d(ndf * mult, ndf * mult * 2, kernel_size=4, stride=1, padding=0, bias=True)), # nn.LeakyReLU(0.2, True)] model += [ ReflectionPad2d(1), SpectralNorm(dygraph.Conv2D(ndf * mult, ndf * mult * 2, 4), dim=1), LeakyReLU(0.2, True) ] # Class Activation Map mult = 2**(n_layers - 2) # self.gap_fc = nn.utils.spectral_norm(nn.Linear(ndf * mult, 1, bias=False)) # self.gmp_fc = nn.utils.spectral_norm(nn.Linear(ndf * mult, 1, bias=False)) # self.conv1x1 = nn.Conv2d(ndf * mult * 2, ndf * mult, kernel_size=1, stride=1, bias=True) # self.leaky_relu = nn.LeakyReLU(0.2, True) self.gap_fc = SpectralNorm(dygraph.Linear(ndf * mult, 1, bias_attr=False), dim=0) self.gmp_fc = SpectralNorm(dygraph.Linear(ndf * mult, 1, bias_attr=False), dim=0) self.conv1x1 = dygraph.Conv2D(ndf * mult * 2, ndf * mult, 1) self.leaky_relu = LeakyReLU(0.2, True) # self.pad = nn.ReflectionPad2d(1) # self.conv = nn.utils.spectral_norm( # nn.Conv2d(ndf * mult, 1, kernel_size=4, stride=1, padding=0, bias=False)) self.pad = ReflectionPad2d(1) self.conv = SpectralNorm(dygraph.Conv2D(ndf * mult, 1, 4, bias_attr=False), dim=1) # self.model = nn.Sequential(*model) self.model = dygraph.Sequential(*model)
def __init__(self, num_channels, num_kp, block_expansion, max_features, num_down_blocks, num_bottleneck_blocks, estimate_occlusion_map=False, dense_motion_params=None, estimate_jacobian=False): super(OcclusionAwareGenerator, self).__init__() if dense_motion_params is not None: self.dense_motion_network = DenseMotionNetwork( num_kp=num_kp, num_channels=num_channels, estimate_occlusion_map=estimate_occlusion_map, **dense_motion_params) else: self.dense_motion_network = None self.first = SameBlock2d(num_channels, block_expansion, kernel_size=(7, 7), padding=(3, 3)) down_blocks = [] for i in range(num_down_blocks): in_features = min(max_features, block_expansion * (2**i)) out_features = min(max_features, block_expansion * (2**(i + 1))) down_blocks.append( DownBlock2d(in_features, out_features, kernel_size=(3, 3), padding=(1, 1))) self.down_blocks = dygraph.LayerList(down_blocks) up_blocks = [] for i in range(num_down_blocks): in_features = min(max_features, block_expansion * (2**(num_down_blocks - i))) out_features = min( max_features, block_expansion * (2**(num_down_blocks - i - 1))) up_blocks.append( UpBlock2d(in_features, out_features, kernel_size=(3, 3), padding=(1, 1))) self.up_blocks = dygraph.LayerList(up_blocks) self.bottleneck = dygraph.Sequential() in_features = min(max_features, block_expansion * (2**num_down_blocks)) for i in range(num_bottleneck_blocks): self.bottleneck.add_sublayer( 'r' + str(i), ResBlock2d(in_features, kernel_size=(3, 3), padding=(1, 1))) self.final = dygraph.Conv2D(block_expansion, num_channels, filter_size=(7, 7), padding=(3, 3)) self.estimate_occlusion_map = estimate_occlusion_map self.num_channels = num_channels
def __init__( self, mapping_fmaps=512, # Z space dimensionality dlatent_size=512, # W space dimensionality resolution=1024, # image resolution normalize_latents=True, # Normalize latent vectors (Z) before feeding them to the mapping layers? use_wscale=True, # Enable equalized learning rate? lrmul=0.01, # Learning rate multiplier for the mapping layers. gain=2**(0.5) # original gain in tensorflow. ): ''' The mapping of generator, it will product w for style y afterwards. parameters: - mapping_fmaps: default 512, Z space dimensionality - dlatent_size: default 512, W space dimensionality - resolution: default 1024 image resolution - normalize_latents: default True, Normalize latent vectors (Z) before feeding them to the mapping layers? - use_wscale: default True,Enable equalized learning rate? - lrmul: default 0.01, Learning rate multiplier for the mapping layers. - gain: default 2**(0.5), original gain in tensorflow. returns: a tensor with size dlatent_size ''' super(G_mapping, self).__init__() self.mapping_fmaps = mapping_fmaps self.func = dygraph.Sequential(*[ FC(self.mapping_fmaps, dlatent_size, gain, lrmul=lrmul, use_wscale=use_wscale), FC(dlatent_size, dlatent_size, gain, lrmul=lrmul, use_wscale=use_wscale), FC(dlatent_size, dlatent_size, gain, lrmul=lrmul, use_wscale=use_wscale), FC(dlatent_size, dlatent_size, gain, lrmul=lrmul, use_wscale=use_wscale), FC(dlatent_size, dlatent_size, gain, lrmul=lrmul, use_wscale=use_wscale), FC(dlatent_size, dlatent_size, gain, lrmul=lrmul, use_wscale=use_wscale), FC(dlatent_size, dlatent_size, gain, lrmul=lrmul, use_wscale=use_wscale), FC(dlatent_size, dlatent_size, gain, lrmul=lrmul, use_wscale=use_wscale) ]) self.normalize_latents = normalize_latents # 4^2~1024^2 --> 2~18layers # - 2 means we start from feature map with height and width equals 4. # as this example, 1024*1024 pixel image we get num_layers = 18. self.resolution_log2 = int(np.log2(resolution)) self.num_layers = self.resolution_log2 * 2 - 2 self.pixel_norm = PixelNorm()
def Sequential(*layers): return dygraph.Sequential(*layers)
def __init__(self, flow_cfg, data_cfg, num_frames): super().__init__() num_input_channels = data_cfg.num_input_channels # 6 if num_input_channels == 0: num_input_channels = 1 num_prev_img_channels = data_utils.get_paired_input_image_channel_number( data_cfg) # 3 num_downsamples = getattr(flow_cfg, 'num_downsamples', 3) kernel_size = getattr(flow_cfg, 'kernel_size', 3) padding = kernel_size // 2 num_blocks = getattr(flow_cfg, 'num_blocks', 6) num_filters = getattr(flow_cfg, 'num_filters', 32) max_num_filters = getattr(flow_cfg, 'max_num_filters', 1024) num_filters_each_layer = [ min(max_num_filters, num_filters * (2**i)) for i in range(num_downsamples + 1) ] self.flow_output_multiplier = getattr(flow_cfg, 'flow_output_multiplier', 20) self.sep_up_mask = getattr(flow_cfg, 'sep_up_mask', False) activation_norm_type = getattr(flow_cfg, 'activation_norm_type', 'sync_batch') weight_norm_type = getattr(flow_cfg, 'weight_norm_type', 'spectral') base_conv_block = partial(Conv2dBlock, kernel_size=kernel_size, padding=padding, weight_norm_type=weight_norm_type, activation_norm_type=activation_norm_type, nonlinearity='leakyrelu') num_input_channels = num_input_channels * num_frames + num_prev_img_channels * ( num_frames - 1) # First layer. down_flow = [('0', base_conv_block(num_input_channels, num_filters))] # Downsamples. for i in range(num_downsamples): down_flow += [(str(i + 1), base_conv_block(num_filters_each_layer[i], num_filters_each_layer[i + 1], stride=2))] # Resnet blocks. res_flow = [] ch = num_filters_each_layer[num_downsamples] for i in range(num_blocks): res_flow += [(str(i), Res2dBlock(ch, ch, kernel_size, padding=padding, weight_norm_type=weight_norm_type, activation_norm_type=activation_norm_type, order='NACNAC'))] # Upsamples. up_flow = [] for i in reversed(range(num_downsamples)): up_flow += [(str( (num_downsamples - 1 - i) * 2), Upsample(scale=2)), (str((num_downsamples - 1 - i) * 2 + 1), base_conv_block(num_filters_each_layer[i + 1], num_filters_each_layer[i]))] conv_flow = [('0', Conv2dBlock(num_filters, 2, kernel_size, padding=padding))] conv_mask = [('0', Conv2dBlock(num_filters, 1, kernel_size, padding=padding, nonlinearity='sigmoid'))] self.down_flow = dg.Sequential(*down_flow) self.res_flow = dg.Sequential(*res_flow) self.up_flow = dg.Sequential(*up_flow) if self.sep_up_mask: self.up_mask = dg.Sequential(*copy.deepcopy(up_flow)) self.conv_flow = dg.Sequential(*conv_flow) self.conv_mask = dg.Sequential(*conv_mask)
def __init__(self, gen_cfg, data_cfg): super().__init__() self.data_cfg = data_cfg self.embed_cfg = embed_cfg = gen_cfg.embed self.embed_arch = embed_cfg.arch # encoderdecoder num_filters = gen_cfg.num_filters # 32 self.max_num_filters = gen_cfg.max_num_filters # 1024 self.num_downsamples = num_downsamples = gen_cfg.num_downsamples # 5 self.num_filters_each_layer = num_filters_each_layer = \ [min(self.max_num_filters, num_filters * (2 ** i)) for i in range(num_downsamples + 2)] # Normalization params. # Conv kernel size in main branch. self.conv_kernel_size = conv_kernel_size = gen_cfg.kernel_size # 3 # Conv kernel size in embedding branch. self.embed_kernel_size = embed_kernel_size = getattr( gen_cfg.embed, 'kernel_size', 3) # 3 # Conv kernel size in SPADE. self.kernel_size = kernel_size = getattr( gen_cfg.activation_norm_params, 'kernel_size', 1) # 1 # Input channel size in SPADE module. self.spade_in_channels = [] for i in range(num_downsamples + 1): self.spade_in_channels += [num_filters_each_layer[i]] hyper_cfg = gen_cfg.hyper # For reference image encoding. # How to utilize the reference label map: concat | mul self.mul_ref_label = 'mul' in hyper_cfg.method_to_use_ref_labels # Output spatial size for adaptive pooling layer. self.sh_fix = self.sw_fix = 32 # Number of fc layers in weight generation. self.num_fc_layers = getattr(hyper_cfg, 'num_fc_layers', 2) # 2 # Number of hyper layers. self.num_hyper_layers = hyper_cfg.num_hyper_layers # 4 # Use adaptive for convolutional layers in the main branch. self.use_hyper_conv = hyper_cfg.is_hyper_conv # False # Use adaptive weight generation for SPADE self.use_hyper_spade = hyper_cfg.is_hyper_spade # True # Use adaptive for label embedding network. self.use_hyper_embed = hyper_cfg.is_hyper_embed # True # Order of operations in the conv block. order = getattr(gen_cfg.hyper, 'hyper_block_order', 'NAC') self.conv_before_norm = order.find('C') < order.find('N') # Normalization / main branch conv weight generation. if self.use_hyper_spade or self.use_hyper_conv: for i in range(self.num_hyper_layers): ch_in, ch_out = num_filters_each_layer[ i], num_filters_each_layer[i + 1] conv_ks2 = conv_kernel_size**2 embed_ks2 = embed_kernel_size**2 spade_ks2 = kernel_size**2 spade_in_ch = self.spade_in_channels[i] fc_names, fc_ins, fc_outs = [], [], [] if self.use_hyper_spade: fc0_out = fcs_out = (spade_in_ch * spade_ks2 + 1) * ( 1 if self.conv_before_norm else 2) fc1_out = (spade_in_ch * spade_ks2 + 1) * (1 if ch_in != ch_out else 2) fc_names += ['fc_spade_0', 'fc_spade_1', 'fc_spade_s'] fc_ins += [ch_out] * 3 fc_outs += [fc0_out, fc1_out, fcs_out] if self.use_hyper_embed: fc_names += ['fc_spade_e'] fc_ins += [ch_out] fc_outs += [ch_in * embed_ks2 + 1] if self.use_hyper_conv: fc0_out = ch_out * conv_ks2 + 1 fc1_out = ch_in * conv_ks2 + 1 fcs_out = ch_out + 1 fc_names += ['fc_conv_0', 'fc_conv_1', 'fc_conv_s'] fc_ins += [ch_in] * 3 fc_outs += [fc0_out, fc1_out, fcs_out] linear_block = partial(LinearBlock, weight_norm_type='spectral', nonlinearity='leakyrelu') for n, l in enumerate(fc_names): fc_in = fc_ins[ n] if self.mul_ref_label else self.sh_fix * self.sw_fix fc_layer = [('0', linear_block(fc_in, ch_out))] for k in range(1, self.num_fc_layers): fc_layer += [(str(k), linear_block(ch_out, ch_out))] fc_layer += [(str(len(fc_layer)), LinearBlock(ch_out, fc_outs[n], weight_norm_type='spectral'))] self.add_sublayer('%s_%d' % (l, i), dg.Sequential(*fc_layer))
def __init__(self, input_nc, output_nc, ngf=64, n_blocks=6, img_size=256, light=False): assert (n_blocks >= 0) super(ResnetGenerator, self).__init__() self.input_nc = input_nc self.output_nc = output_nc self.ngf = ngf self.n_blocks = n_blocks self.img_size = img_size self.light = light # DownBlock = [] # DownBlock += [nn.ReflectionPad2d(3), # nn.Conv2d(input_nc, ngf, kernel_size=7, stride=1, padding=0, bias=False), # nn.InstanceNorm2d(ngf), # nn.ReLU(True)] DownBlock = [] DownBlock += [ ReflectionPad2d(3), dygraph.Conv2D(input_nc, ngf, 7, bias_attr=False), dygraph.InstanceNorm(ngf), ReLU(True) ] # Down-Sampling n_downsampling = 2 for i in range(n_downsampling): mult = 2**i # DownBlock += [nn.ReflectionPad2d(1), # nn.Conv2d(ngf * mult, ngf * mult * 2, kernel_size=3, stride=2, padding=0, bias=False), # nn.InstanceNorm2d(ngf * mult * 2), # nn.ReLU(True)] DownBlock += [ ReflectionPad2d(1), dygraph.Conv2D(ngf * mult, ngf * mult * 2, 3, stride=2, bias_attr=False), dygraph.InstanceNorm(ngf * mult * 2), ReLU(True) ] # Down-Sampling Bottleneck mult = 2**n_downsampling for i in range(n_blocks): DownBlock += [ResnetBlock(ngf * mult, use_bias=False)] # self.DownBlock = nn.Sequential(*DownBlock) self.DownBlock = dygraph.Sequential(*DownBlock) # Class Activation Map # self.gap_fc = nn.Linear(ngf * mult, 1, bias=False) # self.gmp_fc = nn.Linear(ngf * mult, 1, bias=False) # self.conv1x1 = nn.Conv2d(ngf * mult * 2, ngf * mult, kernel_size=1, stride=1, bias=True) # self.relu = nn.ReLU(True) self.gap_fc = dygraph.Linear(ngf * mult, 1, bias_attr=False) self.gmp_fc = dygraph.Linear(ngf * mult, 1, bias_attr=False) self.conv1x1 = dygraph.Conv2D(ngf * mult * 2, ngf * mult, 1) self.relu = ReLU(True) # Gamma, Beta block if self.light: # FC = [nn.Linear(ngf * mult, ngf * mult, bias=False), # nn.ReLU(True), # nn.Linear(ngf * mult, ngf * mult, bias=False), # nn.ReLU(True)] FC = [ dygraph.Linear(ngf * mult, ngf * mult, bias_attr=False), ReLU(True), dygraph.Linear(ngf * mult, ngf * mult, bias_attr=False), ReLU(True) ] else: # FC = [nn.Linear(img_size // mult * img_size // mult * ngf * mult, ngf * mult, bias=False), # nn.ReLU(True), # nn.Linear(ngf * mult, ngf * mult, bias=False), # nn.ReLU(True)] FC = [ dygraph.Linear(img_size // mult * img_size // mult * ngf * mult, ngf * mult, bias_attr=False), ReLU(True), dygraph.Linear(ngf * mult, ngf * mult, bias_attr=False), ReLU(True) ] # self.gamma = nn.Linear(ngf * mult, ngf * mult, bias=False) # self.beta = nn.Linear(ngf * mult, ngf * mult, bias=False) self.gamma = dygraph.Linear(ngf * mult, ngf * mult, bias_attr=False) self.beta = dygraph.Linear(ngf * mult, ngf * mult, bias_attr=False) # self.FC = nn.Sequential(*FC) self.FC = dygraph.Sequential(*FC) # Up-Sampling Bottleneck for i in range(n_blocks): setattr(self, 'UpBlock1_' + str(i + 1), ResnetAdaILNBlock(ngf * mult, use_bias=False)) # Up-Sampling UpBlock2 = [] for i in range(n_downsampling): mult = 2**(n_downsampling - i) # UpBlock2 += [nn.Upsample(scale_factor=2, mode='nearest'), # nn.ReflectionPad2d(1), # nn.Conv2d(ngf * mult, int(ngf * mult / 2), kernel_size=3, stride=1, padding=0, bias=False), # ILN(int(ngf * mult / 2)), # nn.ReLU(True)] UpBlock2 += [ Upsample(scale=2, resample='NEAREST'), ReflectionPad2d(1), dygraph.Conv2D(ngf * mult, int(ngf * mult / 2), 3, bias_attr=False), ILN(int(ngf * mult / 2)), ReLU(True) ] # UpBlock2 += [nn.ReflectionPad2d(3), # nn.Conv2d(ngf, output_nc, kernel_size=7, stride=1, padding=0, bias=False), # nn.Tanh()] UpBlock2 += [ ReflectionPad2d(3), dygraph.Conv2D(ngf, output_nc, 7, bias_attr=False), Tanh() ] # self.UpBlock2 = nn.Sequential(*UpBlock2) self.UpBlock2 = dygraph.Sequential(*UpBlock2)