np.pad()函数解析

np.pad()函数

方法参数:pad(array, pad_width, mode, **kwargs)

方法返回:填充后的数组

参数解释:
array:表示需要填充的数组;
pad_width:表示每个轴(axis)边缘需要填充的数值数目。
参数输入方式为:((before_1, after_1), … (before_N, after_N)),其中(before_1, after_1)表示第1轴两边缘分别填充before_1个和after_1个数值。
mode:表示填充的方式(取值:str字符串或用户提供的函数)

mode中的填充方式:
constant’——表示连续填充相同的值,每个轴可以分别指定填充值,constant_values=(x, y)时前面用x填充,后面用y填充,缺省值填充0
‘edge’——表示用边缘值填充
‘linear_ramp’——表示用边缘递减的方式填充
‘maximum’——表示最大值填充
‘mean’——表示均值填充
‘median’——表示中位数填
‘minimum’——表示最小值填充
‘reflect’——表示对称填充
‘symmetric’——表示对称填充
‘wrap’——表示用原数组后面的值填充前面,前面的值填充后面

代码实现

import numpy as np

arr3D = np.array([[[1, 1, 2, 2, 3, 4],
                   [1, 1, 2, 2, 3, 4],
                   [1, 1, 2, 2, 3, 4]],

                  [[0, 1, 2, 3, 4, 5],
                   [0, 1, 2, 3, 4, 5],
                   [0, 1, 2, 3, 4, 5]],

                  [[1, 1, 2, 2, 3, 4],
                   [1, 1, 2, 2, 3, 4],
                   [1, 1, 2, 2, 3, 4]]])

print('constant:  \n' + str(np.pad(arr3D, ((0,0),(1, 1),(2,2),), 'constant')))
# (0,0),    样本数,不填充
#(1, 1), 图像高度,你可以视为上面填充1个,下面填充1个
#(2,2),图像宽度,你可以视为左边填充2个,右边填充2个
#'constant' 表示填充值默认为0

结果:

constant:  
[[[0 0 0 0 0 0 0 0 0 0]
  [0 0 1 1 2 2 3 4 0 0]
  [0 0 1 1 2 2 3 4 0 0]
  [0 0 1 1 2 2 3 4 0 0]
  [0 0 0 0 0 0 0 0 0 0]]

 [[0 0 0 0 0 0 0 0 0 0]
  [0 0 0 1 2 3 4 5 0 0]
  [0 0 0 1 2 3 4 5 0 0]
  [0 0 0 1 2 3 4 5 0 0]
  [0 0 0 0 0 0 0 0 0 0]]

 [[0 0 0 0 0 0 0 0 0 0]
  [0 0 1 1 2 2 3 4 0 0]
  [0 0 1 1 2 2 3 4 0 0]
  [0 0 1 1 2 2 3 4 0 0]
  [0 0 0 0 0 0 0 0 0 0]]]
``` # Compute padding. pad_total = (self.out_size - 1) * self.down_factor + 1 # Desired output size before downsampling. pad_total -= (self.in_size + self.conv_kernel - 1) * self.up_factor # Input size after upsampling. pad_total += self.up_taps + self.down_taps - 2 # Size reduction caused by the filters. pad_lo = (pad_total + self.up_factor) // 2 # Shift sample locations according to the symmetric interpretation (Appendix C.3). pad_hi = pad_total - pad_lo self.padding = [int(pad_lo[0]), int(pad_hi[0]), int(pad_lo[1]), int(pad_hi[1])] def forward(self, x, w, noise_mode='random', force_fp32=False, update_emas=False): assert noise_mode in ['random', 'const', 'none'] # unused misc.assert_shape(x, [None, self.in_channels, int(self.in_size[1]), int(self.in_size[0])]) misc.assert_shape(w, [x.shape[0], self.w_dim]) # Track input magnitude. if update_emas: with torch.autograd.profiler.record_function('update_magnitude_ema'): magnitude_cur = x.detach().to(torch.float32).square().mean() self.magnitude_ema.copy_(magnitude_cur.lerp(self.magnitude_ema, self.magnitude_ema_beta)) input_gain = self.magnitude_ema.rsqrt() # Execute affine layer. styles = self.affine(w) if self.is_torgb: weight_gain = 1 / np.sqrt(self.in_channels * (self.conv_kernel ** 2)) styles = styles * weight_gain # Execute modulated conv2d. dtype = torch.float16 if (self.use_fp16 and not force_fp32 and x.device.type == 'cuda') else torch.float32 x = modulated_conv2d(x=x.to(dtype), w=self.weight, s=styles, padding=self.conv_kernel-1, demodulate=(not self.is_torgb), input_gain=input_gain) # Execute bias, filtered leaky ReLU, and clamping. gain = 1 if self.is_torgb else np.sqrt(2) slope = 1 if self.is_torgb else 0.2 x = filtered_lrelu.filtered_lrelu(x=x, fu=self.up_filter, fd=self.down_filter, b=self.bias.to(x.dtype), up=self.up_factor, down=self.down_factor, padding=self.padding, gain=gain, slope=slope, clamp=self.conv_clamp) # Ensure correct shape and dtype. misc.assert_shape(x, [None, self.out_channels, int(self.out_size[1]), int(self.out_size[0])]) assert x.dtype == dtype return x```解释这段代码
最新发布
03-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值