From c7c324d865478c3c75cd1916ec82eee43d29d103 Mon Sep 17 00:00:00 2001 From: littletomatodonkey Date: Mon, 19 Oct 2020 17:30:03 +0000 Subject: [PATCH 1/2] fix some apis to paddle-dev --- ppgan/datasets/builder.py | 22 +++++---- ppgan/models/generators/resnet.py | 76 +++++++++++++++---------------- ppgan/models/losses.py | 13 ++---- ppgan/modules/init.py | 9 ++-- ppgan/modules/norm.py | 17 +++---- 5 files changed, 67 insertions(+), 70 deletions(-) diff --git a/ppgan/datasets/builder.py b/ppgan/datasets/builder.py index 7c0b02bfeed82..68c57e05516f2 100644 --- a/ppgan/datasets/builder.py +++ b/ppgan/datasets/builder.py @@ -59,15 +59,17 @@ def __init__(self, dataset, batch_size, is_train, num_workers=4): place = paddle.CUDAPlace(ParallelEnv().dev_id) \ if ParallelEnv().nranks > 1 else paddle.CUDAPlace(0) - sampler = DistributedBatchSampler(self.dataset, - batch_size=batch_size, - shuffle=True if is_train else False, - drop_last=True if is_train else False) - - self.dataloader = paddle.io.DataLoader(self.dataset, - batch_sampler=sampler, - places=place, - num_workers=num_workers) + sampler = DistributedBatchSampler( + self.dataset, + batch_size=batch_size, + shuffle=True if is_train else False, + drop_last=True if is_train else False) + + self.dataloader = paddle.io.DataLoader( + self.dataset, + batch_sampler=sampler, + places=place, + num_workers=num_workers) self.batch_size = batch_size @@ -92,7 +94,7 @@ def __len__(self): return len(self.dataloader) def get_items_by_indexs(self, key, indexs): - if isinstance(indexs, paddle.Variable): + if isinstance(indexs, paddle.Tensor): indexs = indexs.numpy() current_items = [] items = getattr(self.dataset, key) diff --git a/ppgan/models/generators/resnet.py b/ppgan/models/generators/resnet.py index c08bb33d91f90..9bd6b056dedb9 100644 --- a/ppgan/models/generators/resnet.py +++ b/ppgan/models/generators/resnet.py @@ -13,6 +13,7 @@ class ResnetGenerator(nn.Layer): code and idea from Justin Johnson's neural style transfer project(https://github.com/jcjohnson/fast-neural-style) """ + def __init__(self, input_nc, output_nc, @@ -37,17 +38,14 @@ def __init__(self, norm_layer = build_norm_layer(norm_type) if type(norm_layer) == functools.partial: - use_bias = norm_layer.func == nn.InstanceNorm + use_bias = norm_layer.func == nn.InstanceNorm2d else: - use_bias = norm_layer == nn.InstanceNorm + use_bias = norm_layer == nn.InstanceNorm2d model = [ - nn.ReflectionPad2d([3, 3, 3, 3]), - nn.Conv2d(input_nc, - ngf, - kernel_size=7, - padding=0, - bias_attr=use_bias), + nn.Pad2D(padding=[3, 3, 3, 3], mode="reflect"), + nn.Conv2d( + input_nc, ngf, kernel_size=7, padding=0, bias_attr=use_bias), norm_layer(ngf), nn.ReLU() ] @@ -56,12 +54,13 @@ def __init__(self, for i in range(n_downsampling): # add downsampling layers mult = 2**i model += [ - nn.Conv2d(ngf * mult, - ngf * mult * 2, - kernel_size=3, - stride=2, - padding=1, - bias_attr=use_bias), + nn.Conv2d( + ngf * mult, + ngf * mult * 2, + kernel_size=3, + stride=2, + padding=1, + bias_attr=use_bias), norm_layer(ngf * mult * 2), nn.ReLU() ] @@ -70,27 +69,29 @@ def __init__(self, for i in range(n_blocks): # add ResNet blocks model += [ - ResnetBlock(ngf * mult, - padding_type=padding_type, - norm_layer=norm_layer, - use_dropout=use_dropout, - use_bias=use_bias) + ResnetBlock( + ngf * mult, + padding_type=padding_type, + norm_layer=norm_layer, + use_dropout=use_dropout, + use_bias=use_bias) ] for i in range(n_downsampling): # add upsampling layers mult = 2**(n_downsampling - i) model += [ - nn.ConvTranspose2d(ngf * mult, - int(ngf * mult / 2), - kernel_size=3, - stride=2, - padding=1, - output_padding=1, - bias_attr=use_bias), + nn.ConvTranspose2d( + ngf * mult, + int(ngf * mult / 2), + kernel_size=3, + stride=2, + padding=1, + output_padding=1, + bias_attr=use_bias), norm_layer(int(ngf * mult / 2)), nn.ReLU() ] - model += [nn.ReflectionPad2d([3, 3, 3, 3])] + model += [nn.Pad2D(padding=[3, 3, 3, 3], mode="reflect")] model += [nn.Conv2d(ngf, output_nc, kernel_size=7, padding=0)] model += [nn.Tanh()] @@ -103,6 +104,7 @@ def forward(self, x): class ResnetBlock(nn.Layer): """Define a Resnet block""" + def __init__(self, dim, padding_type, norm_layer, use_dropout, use_bias): """Initialize the Resnet block @@ -130,15 +132,13 @@ def build_conv_block(self, dim, padding_type, norm_layer, use_dropout, """ conv_block = [] p = 0 - if padding_type == 'reflect': - conv_block += [nn.ReflectionPad2d([1, 1, 1, 1])] - elif padding_type == 'replicate': - conv_block += [nn.ReplicationPad2d([1, 1, 1, 1])] + if padding_type in ['reflect', 'replicate']: + conv_block += [nn.Pad2D(padding=[1, 1, 1, 1], mode=padding_type)] elif padding_type == 'zero': p = 1 else: - raise NotImplementedError('padding [%s] is not implemented' % - padding_type) + raise NotImplementedError( + 'padding [%s] is not implemented' % padding_type) conv_block += [ nn.Conv2d(dim, dim, kernel_size=3, padding=p, bias_attr=use_bias), @@ -149,15 +149,13 @@ def build_conv_block(self, dim, padding_type, norm_layer, use_dropout, conv_block += [nn.Dropout(0.5)] p = 0 - if padding_type == 'reflect': - conv_block += [nn.ReflectionPad2d([1, 1, 1, 1])] - elif padding_type == 'replicate': - conv_block += [nn.ReplicationPad2d([1, 1, 1, 1])] + if padding_type in ['reflect', 'replicate']: + conv_block += [nn.Pad2D(padding=[1, 1, 1, 1], mode=padding_type)] elif padding_type == 'zero': p = 1 else: - raise NotImplementedError('padding [%s] is not implemented' % - padding_type) + raise NotImplementedError( + 'padding [%s] is not implemented' % padding_type) conv_block += [ nn.Conv2d(dim, dim, kernel_size=3, padding=p, bias_attr=use_bias), norm_layer(dim) diff --git a/ppgan/models/losses.py b/ppgan/models/losses.py index c979548097cfc..3fea20ea7d6fe 100644 --- a/ppgan/models/losses.py +++ b/ppgan/models/losses.py @@ -10,6 +10,7 @@ class GANLoss(nn.Layer): The GANLoss class abstracts away the need to create the target label tensor that has the same size as the input. """ + def __init__(self, gan_mode, target_real_label=1.0, target_fake_label=0.0): """ Initialize the GANLoss class. @@ -47,17 +48,13 @@ def get_target_tensor(self, prediction, target_is_real): """ if target_is_real: if not hasattr(self, 'target_real_tensor'): - self.target_real_tensor = paddle.fill_constant( - shape=paddle.shape(prediction), - value=self.target_real_label, - dtype='float32') + self.target_real_tensor = self.target_real_label * paddle.ones( + paddle.shape(prediction), dtype='float32') target_tensor = self.target_real_tensor else: if not hasattr(self, 'target_fake_tensor'): - self.target_fake_tensor = paddle.fill_constant( - shape=paddle.shape(prediction), - value=self.target_fake_label, - dtype='float32') + self.target_fake_tensor = self.target_fake_label * paddle.ones( + paddle.shape(prediction), dtype='float32') target_tensor = self.target_fake_tensor # target_tensor.stop_gradient = True diff --git a/ppgan/modules/init.py b/ppgan/modules/init.py index 37e4257a1706d..0a9fceef124dd 100644 --- a/ppgan/modules/init.py +++ b/ppgan/modules/init.py @@ -80,7 +80,7 @@ def calculate_gain(nonlinearity, param=None): @paddle.no_grad() def constant_(x, value): - temp_value = paddle.fill_constant(x.shape, x.dtype, value) + temp_value = value * paddle.ones(x.shape, x.dtype) x.set_value(temp_value) return x @@ -256,10 +256,8 @@ def kaiming_init(layer, distribution='normal'): assert distribution in ['uniform', 'normal'] if distribution == 'uniform': - kaiming_uniform_(layer.weight, - a=a, - mode=mode, - nonlinearity=nonlinearity) + kaiming_uniform_( + layer.weight, a=a, mode=mode, nonlinearity=nonlinearity) else: kaiming_normal_(layer.weight, a=a, mode=mode, nonlinearity=nonlinearity) if hasattr(layer, 'bias') and layer.bias is not None: @@ -275,6 +273,7 @@ def init_weights(net, init_type='normal', init_gain=0.02): We use 'normal' in the original pix2pix and CycleGAN paper. But xavier and kaiming might work better for some applications. Feel free to try yourself. """ + def init_func(m): # define the initialization function classname = m.__class__.__name__ if hasattr(m, 'weight') and (classname.find('Conv') != -1 diff --git a/ppgan/modules/norm.py b/ppgan/modules/norm.py index 67b7b43729dec..02a4d7814874a 100644 --- a/ppgan/modules/norm.py +++ b/ppgan/modules/norm.py @@ -21,21 +21,22 @@ def build_norm_layer(norm_type='instance'): if norm_type == 'batch': norm_layer = functools.partial( nn.BatchNorm, - param_attr=paddle.ParamAttr( + weight_attr=paddle.ParamAttr( initializer=nn.initializer.Normal(1.0, 0.02)), bias_attr=paddle.ParamAttr( initializer=nn.initializer.Constant(0.0)), trainable_statistics=True) elif norm_type == 'instance': norm_layer = functools.partial( - nn.InstanceNorm, - param_attr=paddle.ParamAttr( + nn.InstanceNorm2d, + weight_attr=paddle.ParamAttr( initializer=nn.initializer.Constant(1.0), learning_rate=0.0, trainable=False), - bias_attr=paddle.ParamAttr(initializer=nn.initializer.Constant(0.0), - learning_rate=0.0, - trainable=False)) + bias_attr=paddle.ParamAttr( + initializer=nn.initializer.Constant(0.0), + learning_rate=0.0, + trainable=False)) elif norm_type == 'spectral': norm_layer = functools.partial(Spectralnorm) elif norm_type == 'none': @@ -43,6 +44,6 @@ def build_norm_layer(norm_type='instance'): def norm_layer(x): return Identity() else: - raise NotImplementedError('normalization layer [%s] is not found' % - norm_type) + raise NotImplementedError( + 'normalization layer [%s] is not found' % norm_type) return norm_layer From 12aa35f43234727118e50daafd5b2287056bff41 Mon Sep 17 00:00:00 2001 From: littletomatodonkey Date: Mon, 19 Oct 2020 17:38:13 +0000 Subject: [PATCH 2/2] fix ones --- ppgan/models/losses.py | 12 ++++++++---- ppgan/modules/init.py | 2 +- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/ppgan/models/losses.py b/ppgan/models/losses.py index 3fea20ea7d6fe..6da51e2c3842b 100644 --- a/ppgan/models/losses.py +++ b/ppgan/models/losses.py @@ -48,13 +48,17 @@ def get_target_tensor(self, prediction, target_is_real): """ if target_is_real: if not hasattr(self, 'target_real_tensor'): - self.target_real_tensor = self.target_real_label * paddle.ones( - paddle.shape(prediction), dtype='float32') + self.target_real_tensor = paddle.fill_constant( + shape=paddle.shape(prediction), + value=self.target_real_label, + dtype='float32') target_tensor = self.target_real_tensor else: if not hasattr(self, 'target_fake_tensor'): - self.target_fake_tensor = self.target_fake_label * paddle.ones( - paddle.shape(prediction), dtype='float32') + self.target_fake_tensor = paddle.fill_constant( + shape=paddle.shape(prediction), + value=self.target_fake_label, + dtype='float32') target_tensor = self.target_fake_tensor # target_tensor.stop_gradient = True diff --git a/ppgan/modules/init.py b/ppgan/modules/init.py index 0a9fceef124dd..6b33cc197e89e 100644 --- a/ppgan/modules/init.py +++ b/ppgan/modules/init.py @@ -80,7 +80,7 @@ def calculate_gain(nonlinearity, param=None): @paddle.no_grad() def constant_(x, value): - temp_value = value * paddle.ones(x.shape, x.dtype) + temp_value = paddle.full(shape=x.shape, fill_value=value, dtype=x.dtype) x.set_value(temp_value) return x