def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, norm_cfg=dict(type='BN3d')): super().__init__() kernel_size = _triple(kernel_size) stride = _triple(stride) padding = _triple(padding) assert len(kernel_size) == len(stride) == len(padding) == 3 self.in_channels = in_channels self.out_channels = out_channels self.kernel_size = kernel_size self.stride = stride self.padding = padding self.dilation = dilation self.groups = groups self.bias = bias self.norm_cfg = norm_cfg self.output_padding = (0, 0, 0) self.transposed = False # The middle-plane is calculated according to: # M_i = \floor{\frac{t * d^2 N_i-1 * N_i} # {d^2 * N_i-1 + t * N_i}} # where d, t are spatial and temporal kernel, and # N_i, N_i-1 are planes # and inplanes. https://arxiv.org/pdf/1711.11248.pdf mid_channels = 3 * (in_channels * out_channels * kernel_size[1] * kernel_size[2]) mid_channels /= (in_channels * kernel_size[1] * kernel_size[2] + 3 * out_channels) mid_channels = int(mid_channels) self.conv_s = nn.Conv3d(in_channels, mid_channels, kernel_size=(1, kernel_size[1], kernel_size[2]), stride=(1, stride[1], stride[2]), padding=(0, padding[1], padding[2]), bias=bias) _, self.bn_s = build_norm_layer(self.norm_cfg, mid_channels) self.relu = nn.ReLU(inplace=True) self.conv_t = nn.Conv3d(mid_channels, out_channels, kernel_size=(kernel_size[0], 1, 1), stride=(stride[0], 1, 1), padding=(padding[0], 0, 0), bias=bias) self.init_weights()
def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0, bias=False, first_conv=False): super(SpatioTemporalConv, self).__init__() # if ints are entered, convert them to iterables, 1 -> [1, 1, 1] kernel_size = _triple(kernel_size) stride = _triple(stride) padding = _triple(padding) # decomposing the parameters into spatial and temporal components by # masking out the values with the defaults on the axis that # won't be convolved over. This is necessary to avoid unintentional # behavior such as padding being added twice spatial_kernel_size = (1, kernel_size[1], kernel_size[2]) spatial_stride = (1, stride[1], stride[2]) spatial_padding = (0, padding[1], padding[2]) temporal_kernel_size = (kernel_size[0], 1, 1) temporal_stride = (stride[0], 1, 1) temporal_padding = (padding[0], 0, 0) # compute the number of intermediary channels (M) using formula # from the paper section 3.5 intermed_channels = int(math.floor((kernel_size[0] * kernel_size[1] * kernel_size[2] * in_channels * out_channels)/ \ (kernel_size[1] * kernel_size[2] * in_channels + kernel_size[0] * out_channels))) # print(intermed_channels) # the spatial conv is effectively a 2D conv due to the # spatial_kernel_size, followed by batch_norm and ReLU print(spatial_kernel_size) print(temporal_kernel_size) self.spatial_conv = nn.Conv3d(in_channels, intermed_channels, spatial_kernel_size, stride=spatial_stride, padding=spatial_padding, bias=bias) self.bn = nn.BatchNorm3d(intermed_channels) self.relu = nn.ReLU() # the temporal conv is effectively a 1D conv, but has batch norm # and ReLU added inside the model constructor, not here. This is an # intentional design choice, to allow this module to externally act # identical to a standard Conv3D, so it can be reused easily in any # other codebase self.temporal_conv = nn.Conv3d(intermed_channels, out_channels, temporal_kernel_size, stride=temporal_stride, padding=temporal_padding, bias=bias)
def __init__(self, process, thresholds, quant_levels, in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True): kernel_size = _triple(kernel_size) stride = _triple(stride) padding = _triple(padding) dilation = _triple(dilation) super(StochasticConv3d, self).__init__( process, thresholds, quant_levels, in_channels, out_channels, kernel_size, stride, padding, dilation, False, _triple(0), groups, bias)
def conv3d(input, weight, bias, stride=1, padding=0, dilation=1, groups=1, padding_mode='zeros', scale=1.0, zero_point=0, dtype=torch.quint8): r""" Applies a 3D convolution over a quantized 3D input composed of several input planes. See :class:`~torch.nn.quantized.Conv3d` for details and output shape. Args: input: quantized input tensor of shape :math:`(\text{minibatch} , \text{in\_channels} , iD , iH , iW)` weight: quantized filters of shape :math:`(\text{out\_channels} , \frac{\text{in\_channels}}{\text{groups}} , kD , kH , kW)` bias: **non-quantized** bias tensor of shape :math:`(\text{out\_channels})`. The tensor type must be `torch.float`. stride: the stride of the convolving kernel. Can be a single number or a tuple `(sD, sH, sW)`. Default: 1 padding: implicit paddings on both sides of the input. Can be a single number or a tuple `(padD, padH, padW)`. Default: 0 dilation: the spacing between kernel elements. Can be a single number or a tuple `(dD, dH, dW)`. Default: 1 groups: split input into groups, :math:`\text{in\_channels}` should be divisible by the number of groups. Default: 1 padding_mode: the padding mode to use. Only "zeros" is supported for quantized convolution at the moment. Default: "zeros" scale: quantization scale for the output. Default: 1.0 zero_point: quantization zero_point for the output. Default: 0 dtype: quantization data type to use. Default: ``torch.quint8`` Examples:: >>> from torch.nn.quantized import functional as qF >>> filters = torch.randn(8, 4, 3, 3, 3, dtype=torch.float) >>> inputs = torch.randn(1, 4, 5, 5, 5, dtype=torch.float) >>> bias = torch.randn(4, dtype=torch.float) >>> >>> scale, zero_point = 1.0, 0 >>> dtype = torch.quint8 >>> >>> q_filters = torch.quantize_per_tensor(filters, scale, zero_point, dtype) >>> q_inputs = torch.quantize_per_tensor(inputs, scale, zero_point, dtype) >>> qF.conv3d(q_inputs, q_filters, bias, scale, zero_point, padding=1) """ # noqa: E501 if padding_mode != 'zeros': raise NotImplementedError("Only zero-padding is supported!") if input.ndim != 5: raise ValueError("Input shape must be `(N, C, D, H, W)`!") stride = _triple(stride) padding = _triple(padding) dilation = _triple(dilation) prepacked_weight = torch.ops.quantized.conv3d_prepack( weight, bias, stride, padding, dilation, groups) return torch.ops.quantized.conv3d( input, prepacked_weight, stride, padding, dilation, groups, scale, zero_point)
def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, padding_mode='zeros', weight_manifold=None, transpose_flag=False): kernel_size = _triple(kernel_size) stride = _triple(stride) padding = _triple(padding) dilation = _triple(dilation) super(rConv3d, self).__init__( in_channels, out_channels, kernel_size, stride, padding, dilation, False, _triple(0), groups, bias, padding_mode, weight_manifold, transpose_flag)
def avg_pool3d(g, input, kernel_size, stride, padding, ceil_mode, count_include_pad): if ceil_mode: return _unimplemented("avg_pool3d", "ceil_mode") if not stride: stride = kernel_size # TODO: What about count_include_pad?! return g.op("AveragePool", input, kernel_shape_i=_triple(kernel_size), strides_i=_triple(stride), pads_i=_triple(padding))
def forward(ctx, input, kernel_size=1, stride=1, padding=0, ceil_mode=False, count_include_pad=True): # if len(stride) == 1: # # view 1d convolutions as 2d # stride = (1,) + stride # padding = (0,) + padding # dilation = (1,) + dilation output = F.avg_pool3d(input, kernel_size, stride, padding, ceil_mode, count_include_pad) ctx.save_for_backward(input, output) ctx.hparams = [_triple(kernel_size), _triple(stride), _triple(padding), ceil_mode, count_include_pad] return output
def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, cuda=False, init_weight=None, init_bias=None, clip_var=None): kernel_size = utils._triple(kernel_size) stride = utils._triple(stride) padding = utils._triple(padding) dilation = utils.triple(dilation) super(Conv3dGroupNJ, self).__init__( in_channels, out_channels, kernel_size, stride, padding, dilation, False, utils._pair(0), groups, bias, init_weight, init_bias, cuda, clip_var)
def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, cuda=False, init_weight=None, init_bias=None, clip_var=None): kernel_size = utils._triple(kernel_size) stride = utils._triple(stride) padding = utils._triple(padding) dilation = utils._triple(dilation) super(Conv3dGroupNJ, self).__init__( in_channels, out_channels, kernel_size, stride, padding, dilation, False, utils._pair(0), groups, bias, init_weight, init_bias, cuda, clip_var)
def symbolic(g, input, kernel_size, stride=None, padding=0, dilation=1, ceil_mode=False): if ceil_mode: raise RuntimeError("ceil_mode not supported in MaxPool3d") n = g.appendNode(g.create("MaxPool", [input]) .is_("kernel_shape", _triple(kernel_size)) .is_("pads", _triple(padding)) .is_("dilations", _triple(dilation)) .is_("strides", _triple(stride))) return (n, None)
def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, padding_mode='zeros'): kernel_size = _triple(kernel_size) stride = _triple(stride) padding = _triple(padding) dilation = _triple(dilation) super(Conv3d, self).__init__( in_channels, out_channels, kernel_size, stride, padding, dilation, False, _triple(0), groups, bias, padding_mode)
def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0, bias=False, first_conv=False): super(SpatioTemporalConv, self).__init__() kernel_size = _triple(kernel_size) stride = _triple(stride) padding = _triple(padding) spatial_kernel_size = (1, kernel_size[1], kernel_size[2]) spatial_stride = (1, stride[1], stride[2]) spatial_padding = (0, padding[1], padding[2]) temporal_kernel_size = (kernel_size[0], 1, 1) temporal_stride = (stride[0], 1, 1) temporal_padding = (padding[0], 0, 0) if first_conv: intermed_channels = 45 print('----------------') else: intermed_channels = int( math.floor((kernel_size[0] * kernel_size[1] * kernel_size[2] * in_channels * out_channels) / (kernel_size[1] * kernel_size[2] * in_channels + kernel_size[0] * out_channels))) print('***') print(intermed_channels) self.spatial_conv = nn.Conv3d(in_channels, intermed_channels, spatial_kernel_size, stride=spatial_stride, padding=spatial_padding, bias=bias) self.bn1 = nn.BatchNorm3d(intermed_channels) self.temporal_conv = nn.Conv3d(intermed_channels, out_channels, temporal_kernel_size, stride=temporal_stride, padding=temporal_padding, bias=bias) self.bn2 = nn.BatchNorm3d(out_channels) self.relu = nn.ReLU(inplace=True)
def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0, bias=True): super().__init__() # If ints are given, convert them to an iterable, 1 -> [1, 1, 1]. stride = _triple(stride) padding = _triple(padding) kernel_size = _triple(kernel_size) # Decompose the parameters into spatial and temporal components # by masking out the values with the defaults on the axis that # won't be convolved over. This is necessary to avoid aberrant # behavior such as padding being added twice. spatial_stride = [1, stride[1], stride[2]] spatial_padding = [0, padding[1], padding[2]] spatial_kernel_size = [1, kernel_size[1], kernel_size[2]] temporal_stride = [stride[0], 1, 1] temporal_padding = [padding[0], 0, 0] temporal_kernel_size = [kernel_size[0], 1, 1] # Compute the number of intermediary channels (M) using formula # from the paper section 3.5: intermed_channels = int( math.floor((kernel_size[0] * kernel_size[1] * kernel_size[2] * in_channels * out_channels) / (kernel_size[1] * kernel_size[2] * in_channels + kernel_size[0] * out_channels))) # The spatial conv is effectively a 2D conv due to the # spatial_kernel_size, followed by batch_norm and ReLU. self.spatial_conv = nn.Conv3d(in_channels, intermed_channels, spatial_kernel_size, stride=spatial_stride, padding=spatial_padding, bias=bias) self.bn = nn.BatchNorm3d(intermed_channels) self.relu = nn.ReLU() # The temporal conv is effectively a 1D conv, but has batch norm # and ReLU added inside the model constructor, not here. This is an # intentional design choice, to allow this module to externally act # identically to a standard Conv3D, so it can be reused easily in any other codebase self.temporal_conv = nn.Conv3d(intermed_channels, out_channels, temporal_kernel_size, stride=temporal_stride, padding=temporal_padding, bias=bias)
def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0, bias=False): super(SpatioTemporalConv, self).__init__() # if ints are entered, convert them to iterables, 1 -> [1, 1, 1] kernel_size = _triple(kernel_size) stride = _triple(stride) padding = _triple(padding) self.temporal_spatial_conv = nn.Conv3d(in_channels, out_channels, kernel_size, stride=stride, padding=padding, bias=bias)
def __init__(self, in_channels, out_channels, kernel_size, stride, padding, dilation, transposed, output_padding, groups, bias, padding_mode): if padding_mode not in ('zeros', 'circular'): raise ValueError('Only "zeros" or "circular" padding mode are supported for {}'\ .format(self.__class__.__name__)) super().__init__( in_channels, out_channels, _triple(kernel_size), _triple(stride), _triple(padding), _triple(dilation), transposed, output_padding, groups, bias, padding_mode)
def __init__(self, kernel_size, stride=None, padding=0, ceil_mode=False, count_include_pad=True): super(AvgPool3d, self).__init__() self.kernel_size = _triple(kernel_size) self.stride = _triple(stride) or _triple(kernel_size) self.padding = _triple(padding) self.ceil_mode = ceil_mode self.count_include_pad = count_include_pad
def max_pool3d_with_indices(g, input, kernel_size, stride, padding, dilation, ceil_mode): if ceil_mode: return _unimplemented("max_pool3d_with_indices", "ceil_mode") if set(_triple(dilation)) != {1}: return _unimplemented("max_pool3d_with_indices", "dilation") if not stride: stride = kernel_size r = g.op("MaxPool", input, kernel_shape_i=_triple(kernel_size), pads_i=_triple(padding) * 2, strides_i=_triple(stride)) return r, None
def forward(ctx, input, kernel_size, stride=None): ctx.kernel_size = _triple(kernel_size) ctx.stride = _triple(stride if stride is not None else kernel_size) backend = type2backend[type(input)] output = input.new() # can avoid this with cudnn ctx.save_for_backward(input) backend.VolumetricAveragePooling_updateOutput(backend.library_state, input, output, ctx.kernel_size[0], ctx.kernel_size[2], ctx.kernel_size[1], ctx.stride[0], ctx.stride[2], ctx.stride[1]) return output
def eb_conv3d(input, weight, bias=None, stride=1, padding=0, dilation=1, groups=1): f = EBConvNd(_triple(stride), _triple(padding), _triple(dilation), False, _triple(0), groups) if bias is None: return f(input, weight) return f(input, weight, bias)
def max_pool3d(g, input, kernel_size, stride, padding, dilation, ceil_mode): if ceil_mode: return _unimplemented("max_pool3d", "ceil_mode") if set(_triple(dilation)) != {1}: return _unimplemented("max_pool3d", "dilation") if not stride: stride = kernel_size r = g.op("MaxPool", input, kernel_shape_i=_triple(kernel_size), pads_i=_triple(padding) * 2, strides_i=_triple(stride)) return r, None
def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0, output_padding=0, groups=1, bias=True, dilation=1, padding_mode='zeros', device=None, dtype=None): factory_kwargs = {'device': device, 'dtype': dtype} kernel_size = _triple(kernel_size) stride = _triple(stride) padding = _triple(padding) dilation = _triple(dilation) output_padding = _triple(output_padding) super(ConvTranspose3d, self).__init__( in_channels, out_channels, kernel_size, stride, padding, dilation, True, output_padding, groups, bias, padding_mode, **factory_kwargs)
def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, padding_mode='zeros', device=None, dtype=None): assert padding_mode != 'reflect', "Conv3d does not support reflection padding" factory_kwargs = {'device': device, 'dtype': dtype} kernel_size = _triple(kernel_size) stride = _triple(stride) padding = _triple(padding) dilation = _triple(dilation) super(Conv3d, self)._init( in_channels, out_channels, kernel_size, stride, padding, dilation, False, _triple(0), groups, bias, padding_mode, **factory_kwargs)
def __init__(self, kernel_size, stride=None, padding=0, dilation=1, return_indices=False, ceil_mode=False): self.kernel_size = _triple(kernel_size) self.stride = _triple(stride if stride is not None else kernel_size) self.padding = _triple(padding) self.dilation = _triple(dilation) self.return_indices = return_indices self.ceil_mode = ceil_mode
def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, zero_mean=False, threshold=3): kernel_size = _triple(kernel_size) stride = _triple(stride) padding = _triple(padding) dilation = _triple(dilation) self.zero_mean = zero_mean self.threshold = threshold super(BayesConv3d, self).__init__(in_channels, out_channels, kernel_size, stride, padding, dilation, False, _triple(0), groups, bias) if zero_mean: self.mu_weight = nn.Parameter(torch.zeros_like(self.mu_weight))
def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0, bias=True): # bias needs to be True for BFP module super(BFP_SpatioTemporalConv, self).__init__() # if ints are entered, convert them to iterables, 1 -> [1, 1, 1] kernel_size = _triple(kernel_size) stride = _triple(stride) padding = _triple(padding) # self.exp_bit = exp_bit # self.mantisa_bit = mantisa_bit # self.opt_exp_act_list = opt_exp_act_list self.temporal_spatial_conv = nn.Conv3d(in_channels, out_channels, kernel_size, stride=stride, padding=padding, bias=bias)
def __init__(self, *args, **kwargs): super(DeformConvPack3D, self).__init__(*args, **kwargs) self.conv_offset = nn.Conv3d(self.in_channels, self.deformable_groups * 2 * self.kernel_size[0] * self.kernel_size[1] * self.kernel_size[2], kernel_size=self.kernel_size, stride=_triple(self.stride), padding=_triple(self.padding), dilation=_triple(self.dilation), bias=True) self.init_offset()
def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0, bias=False, first_conv=False): super(R2P1D, self).__init__() kernel_size = _triple(kernel_size) stride = _triple(stride) padding = _triple(padding) if first_conv: spatial_kernel_size = kernel_size spatial_stride = (1, stride[1], stride[2]) spatial_padding = padding temporal_kernel_size = (3, 1, 1) temporal_stride = (stride[0], 1, 1) temporal_padding = (1, 0, 0) intermed_channels = 45 self.spatial_conv = nn.Conv3d(in_channels, intermed_channels, spatial_kernel_size, stride=spatial_stride, padding=spatial_padding, bias=bias) self.bn1 = nn.BatchNorm3d(intermed_channels) self.temporal_conv = nn.Conv3d(intermed_channels, out_channels, temporal_kernel_size, stride=temporal_stride, padding=temporal_padding, bias=bias) self.bn2 = nn.BatchNorm3d(out_channels) self.relu = nn.ReLU() else: spatial_kernel_size = (1, kernel_size[1], kernel_size[2]) spatial_stride = (1, stride[1], stride[2]) spatial_padding = (0, padding[1], padding[2]) temporal_kernel_size = (kernel_size[0], 1, 1) temporal_stride = (stride[0], 1, 1) temporal_padding = (padding[0], 0, 0) intermed_channels = int(math.floor((kernel_size[0] * kernel_size[1] * kernel_size[2] * in_channels * out_channels)/ \ (kernel_size[1] * kernel_size[2] * in_channels + kernel_size[0] * out_channels))) self.spatial_conv = nn.Conv3d(in_channels, intermed_channels, spatial_kernel_size, stride=spatial_stride, padding=spatial_padding, bias=bias) self.bn1 = nn.BatchNorm3d(intermed_channels) self.temporal_conv = nn.Conv3d(intermed_channels, out_channels, temporal_kernel_size, stride=temporal_stride, padding=temporal_padding, bias=bias) self.bn2 = nn.BatchNorm3d(out_channels) self.relu = nn.ReLU()
def conv_offset3d(input, offset, weight, stride=1, padding=0, channel_per_group=1): if input is not None and input.dim() != 5: raise ValueError( "Expected 5D tensor as input, got {}D tensor instead.".format( input.dim())) f = ConvOffset3dFunction(_triple(stride), _triple(padding), channel_per_group) return f(input, offset, weight)
def fft_conv3d(input: Tensor, weight: Tensor, bias: Optional[Tensor] = None, stride: Union[int, Tuple[int]] = 1, padding: Union[int, Tuple[int]] = 0, dilation: Union[int, Tuple[int]] = 1, groups: int = 1) -> Tensor: r""" """ stride = _triple(stride) padding = _triple(padding) dilation = _triple(dilation) return _fft_convnd(input, weight, bias, stride, padding, dilation, groups)
def forward(ctx, input, offset, weight, bias, stride=1, padding=0, dilation=1, deformable_groups=1, im2col_step=64): if input is not None and input.dim() != 5: raise ValueError( "Expected 5D tensor as input, got {}D tensor instead.".format( input.dim())) ctx.stride = _triple(stride) ctx.padding = _triple(padding) ctx.dilation = _triple(dilation) ctx.deformable_groups = deformable_groups ctx.im2col_step = im2col_step ctx.save_for_backward(input, offset, weight, bias) output = input.new(*TrajConvFunction._output_size( input, weight, ctx.padding, ctx.dilation, ctx.stride)) ctx.bufs_ = [input.new(), input.new()] # columns, ones if not input.is_cuda: raise NotImplementedError else: if isinstance(input, torch.autograd.Variable): if not (isinstance(input.data, torch.cuda.FloatTensor) or isinstance(input.data, torch.cuda.DoubleTensor)): raise NotImplementedError else: if not (isinstance(input, torch.cuda.FloatTensor) or isinstance(input, torch.cuda.DoubleTensor)): raise NotImplementedError cur_im2col_step = min(ctx.im2col_step, input.shape[0]) assert (input.shape[0] % cur_im2col_step) == 0, 'im2col step must divide batchsize' traj_conv_cuda.deform_3d_conv_forward_cuda( input, weight, bias, offset, output, ctx.bufs_[0], ctx.bufs_[1], weight.size(2), weight.size(3), weight.size(4), ctx.stride[0], ctx.stride[1], ctx.stride[2], ctx.padding[0], ctx.padding[1], ctx.padding[2], ctx.dilation[0], ctx.dilation[1], ctx.dilation[2], ctx.deformable_groups, cur_im2col_step) return output
def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, padding_mode="zeros"): super().__init__( in_channels, out_channels, _triple(kernel_size), _triple(stride), _triple(padding), _triple(dilation), False, _triple(0), groups, bias, padding_mode)
def symbolic(g, input, kernel_size, stride=None, padding=0, dilation=1, ceil_mode=False): from torch.onnx.symbolic import _unimplemented if ceil_mode: return _unimplemented("MaxPool3d", "ceil_mode") if set(_triple(dilation)) != {1}: return _unimplemented("MaxPool3d", "dilation") if stride is None: stride = kernel_size r = g.op("MaxPool", input, kernel_shape_i=_triple(kernel_size), pads_i=_triple(padding), strides_i=_triple(stride)) return r, None
def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=1, dilation=1, groups=1, bias=True): super(Conv3dLSTM, self).__init__() if in_channels % groups != 0: raise ValueError('in_channels must be divisible by groups') if out_channels % groups != 0: raise ValueError('out_channels must be divisible by groups') kernel_size = _triple(kernel_size) stride = _triple(stride) padding = _triple(padding) dilation = _triple(dilation) self.in_channels = in_channels self.out_channels = out_channels self.kernel_size = kernel_size self.stride = stride self.padding = padding self.padding_h = tuple( k // 2 for k, s, p, d in zip(kernel_size, stride, padding, dilation)) self.dilation = dilation self.groups = groups self.weight = Parameter( torch.Tensor(4 * out_channels, (in_channels + out_channels) // groups, *kernel_size)) #self.weight_ih = Parameter(torch.Tensor( # 4 * out_channels, in_channels // groups, *kernel_size)) #self.weight_hh = Parameter(torch.Tensor( # 4 * out_channels, out_channels // groups, *kernel_size)) #self.weight_ch = Parameter(torch.Tensor( # 3 * out_channels, out_channels // groups, *kernel_size)) if bias: self.bias = Parameter(torch.Tensor(4 * out_channels)) #self.bias_ih = Parameter(torch.Tensor(4 * out_channels)) #self.bias_hh = Parameter(torch.Tensor(4 * out_channels)) #self.bias_ch = Parameter(torch.Tensor(3 * out_channels)) else: self.register_parameter('bias', None) #self.register_parameter('bias_ih', None) #self.register_parameter('bias_hh', None) #self.register_parameter('bias_ch', None) #self.register_buffer('wc_blank', torch.zeros(1, 1, 1, 1, 1)) self.reset_parameters()
def forward(ctx, input, kernel_size, stride=None, padding=0, ceil_mode=False, count_include_pad=True): ctx.kernel_size = _triple(kernel_size) ctx.stride = _triple(stride if stride is not None else kernel_size) ctx.padding = _triple(padding) ctx.ceil_mode = ceil_mode ctx.count_include_pad = count_include_pad backend = type2backend[type(input)] output = input.new() # can avoid this with cudnn ctx.save_for_backward(input) backend.VolumetricAveragePooling_updateOutput( backend.library_state, input, output, ctx.kernel_size[0], ctx.kernel_size[2], ctx.kernel_size[1], ctx.stride[0], ctx.stride[2], ctx.stride[1], ctx.padding[0], ctx.padding[2], ctx.padding[1], ctx.ceil_mode, ctx.count_include_pad) return output
def forward(ctx, input, kernel_size, stride=None, padding=0, dilation=1, ceil_mode=False): ctx.kernel_size = _triple(kernel_size) ctx.stride = _triple(stride if stride is not None else kernel_size) ctx.padding = _triple(padding) ctx.dilation = _triple(dilation) ctx.ceil_mode = ceil_mode backend = type2backend[type(input)] indices, output = input.new().long(), input.new() backend.VolumetricDilatedMaxPooling_updateOutput(backend.library_state, input, output, indices, ctx.kernel_size[0], ctx.kernel_size[2], ctx.kernel_size[1], ctx.stride[0], ctx.stride[2], ctx.stride[1], ctx.padding[0], ctx.padding[2], ctx.padding[1], ctx.dilation[0], ctx.dilation[2], ctx.dilation[1], ctx.ceil_mode) ctx.save_for_backward(input, indices) ctx.mark_non_differentiable(indices) return output, indices
def forward(ctx, input, output_size): ctx.output_size = list(_triple(output_size)) for i, s in enumerate(ctx.output_size): ctx.output_size[i] = ctx.output_size[i] or input.size(i + 2) ctx.output_size = tuple(ctx.output_size) backend = type2backend[type(input)] output = input.new() ctx.save_for_backward(input) backend.VolumetricAdaptiveAveragePooling_updateOutput( backend.library_state, input, output, ctx.output_size[0], ctx.output_size[2], ctx.output_size[1]) return output
def forward(ctx, input, output_size): ctx.output_size = list(_triple(output_size)) for i, s in enumerate(ctx.output_size): ctx.output_size[i] = ctx.output_size[i] or input.size(i + 2) ctx.output_size = tuple(ctx.output_size) backend = type2backend[type(input)] indices, output = input.new().long(), input.new() backend.VolumetricAdaptiveMaxPooling_updateOutput( backend.library_state, input, output, indices, ctx.output_size[0], ctx.output_size[2], ctx.output_size[1]) ctx.save_for_backward(input, indices) ctx.mark_non_differentiable(indices) return output, indices
def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0, bias=False, first_conv=False): super(SpatioTemporalConv, self).__init__() # if ints are entered, convert them to iterables, 1 -> [1, 1, 1] kernel_size = _triple(kernel_size) stride = _triple(stride) padding = _triple(padding) if first_conv: # decomposing the parameters into spatial and temporal components by # masking out the values with the defaults on the axis that # won't be convolved over. This is necessary to avoid unintentional # behavior such as padding being added twice spatial_kernel_size = kernel_size spatial_stride = (1, stride[1], stride[2]) spatial_padding = padding temporal_kernel_size = (3, 1, 1) temporal_stride = (stride[0], 1, 1) temporal_padding = (1, 0, 0) # from the official code, first conv's intermed_channels = 45 intermed_channels = 45 # the spatial conv is effectively a 2D conv due to the # spatial_kernel_size, followed by batch_norm and ReLU self.spatial_conv = nn.Conv3d(in_channels, intermed_channels, spatial_kernel_size, stride=spatial_stride, padding=spatial_padding, bias=bias) self.bn1 = nn.BatchNorm3d(intermed_channels) # the temporal conv is effectively a 1D conv, but has batch norm # and ReLU added inside the model constructor, not here. This is an # intentional design choice, to allow this module to externally act # identical to a standard Conv3D, so it can be reused easily in any # other codebase self.temporal_conv = nn.Conv3d(intermed_channels, out_channels, temporal_kernel_size, stride=temporal_stride, padding=temporal_padding, bias=bias) self.bn2 = nn.BatchNorm3d(out_channels) self.relu = nn.ReLU() else: # decomposing the parameters into spatial and temporal components by # masking out the values with the defaults on the axis that # won't be convolved over. This is necessary to avoid unintentional # behavior such as padding being added twice spatial_kernel_size = (1, kernel_size[1], kernel_size[2]) spatial_stride = (1, stride[1], stride[2]) spatial_padding = (0, padding[1], padding[2]) temporal_kernel_size = (kernel_size[0], 1, 1) temporal_stride = (stride[0], 1, 1) temporal_padding = (padding[0], 0, 0) # compute the number of intermediary channels (M) using formula # from the paper section 3.5 intermed_channels = int(math.floor((kernel_size[0] * kernel_size[1] * kernel_size[2] * in_channels * out_channels)/ \ (kernel_size[1] * kernel_size[2] * in_channels + kernel_size[0] * out_channels))) # the spatial conv is effectively a 2D conv due to the # spatial_kernel_size, followed by batch_norm and ReLU self.spatial_conv = nn.Conv3d(in_channels, intermed_channels, spatial_kernel_size, stride=spatial_stride, padding=spatial_padding, bias=bias) self.bn1 = nn.BatchNorm3d(intermed_channels) # the temporal conv is effectively a 1D conv, but has batch norm # and ReLU added inside the model constructor, not here. This is an # intentional design choice, to allow this module to externally act # identical to a standard Conv3D, so it can be reused easily in any # other codebase self.temporal_conv = nn.Conv3d(intermed_channels, out_channels, temporal_kernel_size, stride=temporal_stride, padding=temporal_padding, bias=bias) self.bn2 = nn.BatchNorm3d(out_channels) self.relu = nn.ReLU()
def __init__(self, kernel_size, stride=1, padding=0): super(AvgPool3d, self).__init__(_triple(kernel_size), 'avg', _triple(stride), _triple(padding))
def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, upsample=1, groups=1, bias=True, activation = 'linear', alpha = 0., residual = None): super(Conv3d, self).__init__(5, in_channels, out_channels, _triple(kernel_size), _triple(stride), _triple(padding), _triple(dilation), _triple(upsample), groups, bias, activation, alpha, residual)