Python torch.nn.functional.conv3d() Examples
The following are 30
code examples of torch.nn.functional.conv3d().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
torch.nn.functional
, or try the search function
.
Example #1
Source File: pytorch_utils.py From timeception with GNU General Public License v3.0 | 6 votes |
def padding3d(tensor, filter, mode=str('constant')): """ Input shape (BN, C, T, H, W) """ it, ih, iw = tensor.shape[2:] ft, fh, fw = filter.shape pt = max(0, (it - 1) + (ft - 1) + 1 - it) ph = max(0, (ih - 1) + (fh - 1) + 1 - ih) pw = max(0, (iw - 1) + (fw - 1) + 1 - iw) oddt = (pt % 2 != 0) oddh = (ph % 2 != 0) oddw = (pw % 2 != 0) if any([oddt, oddh, oddw]): pad = [0, int(oddt), 0, int(oddh), 0, int(oddw)] tensor = F.pad(tensor, pad, mode=mode) padding = (pt // 2, ph // 2, pw // 2) tensor = F.conv3d(tensor, filter, padding=padding) return tensor
Example #2
Source File: pytorch_utils.py From deep-smoke-machine with BSD 3-Clause "New" or "Revised" License | 6 votes |
def padding3d(tensor, filter, mode=str('constant')): """ Input shape (BN, C, T, H, W) """ it, ih, iw = tensor.shape[2:] ft, fh, fw = filter.shape pt = max(0, (it - 1) + (ft - 1) + 1 - it) ph = max(0, (ih - 1) + (fh - 1) + 1 - ih) pw = max(0, (iw - 1) + (fw - 1) + 1 - iw) oddt = (pt % 2 != 0) oddh = (ph % 2 != 0) oddw = (pw % 2 != 0) if any([oddt, oddh, oddw]): pad = [0, int(oddt), 0, int(oddh), 0, int(oddw)] tensor = F.pad(tensor, pad, mode=mode) padding = (pt // 2, ph // 2, pw // 2) tensor = F.conv3d(tensor, filter, padding=padding) return tensor
Example #3
Source File: test_pyprof_nvtx.py From apex with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_conv3d(self): # Data and weight tensors tensor3d_in_conv = torch.randn(32, 3, 16, 224, 224, device='cuda', dtype=self.dtype) tensor3d_in_conv_grouped = torch.randn(32, 6, 16, 224, 224, device='cuda', dtype=self.dtype) conv3d_filter = torch.randn(16, 3, 3, 3, 3, device='cuda', dtype=self.dtype) conv3d_bias = torch.ones(16, device='cuda', dtype=self.dtype) # Vanilla conv3d conv3d_out_vanilla = F.conv3d(tensor3d_in_conv, conv3d_filter) # conv3d - stride > 1 conv3d_out_strided = F.conv3d(tensor3d_in_conv, conv3d_filter, stride=2) # conv3d - dilation > 1 conv3d_out_dilated = F.conv3d(tensor3d_in_conv, conv3d_filter, dilation=2) # conv3d - groups > 1 conv3d_out_grouped = F.conv3d(tensor3d_in_conv_grouped, conv3d_filter, groups=2) # conv3d - padding with zeros conv3d_out_padding_zeros = F.conv3d(tensor3d_in_conv, conv3d_filter, padding=6)
Example #4
Source File: simplelayers.py From MONAI with Apache License 2.0 | 6 votes |
def __init__(self, spatial_dims: int, sigma, truncated: float = 4.0): """ Args: spatial_dims: number of spatial dimensions of the input image. must have shape (Batch, channels, H[, W, ...]). sigma (float or sequence of floats): std. truncated: spreads how many stds. """ super().__init__() self.spatial_dims = int(spatial_dims) _sigma = ensure_tuple_rep(sigma, self.spatial_dims) self.kernel = [ torch.nn.Parameter(torch.as_tensor(gaussian_1d(s, truncated), dtype=torch.float), False) for s in _sigma ] self.padding = [same_padding(k.size()[0]) for k in self.kernel] self.conv_n = [F.conv1d, F.conv2d, F.conv3d][spatial_dims - 1] for idx, param in enumerate(self.kernel): self.register_parameter(f"kernel_{idx}", param)
Example #5
Source File: losses.py From voxelmorph with GNU General Public License v3.0 | 6 votes |
def compute_local_sums(I, J, filt, stride, padding, win): I2 = I * I J2 = J * J IJ = I * J I_sum = F.conv3d(I, filt, stride=stride, padding=padding) J_sum = F.conv3d(J, filt, stride=stride, padding=padding) I2_sum = F.conv3d(I2, filt, stride=stride, padding=padding) J2_sum = F.conv3d(J2, filt, stride=stride, padding=padding) IJ_sum = F.conv3d(IJ, filt, stride=stride, padding=padding) win_size = np.prod(win) u_I = I_sum / win_size u_J = J_sum / win_size cross = IJ_sum - u_J * I_sum - u_I * J_sum + u_I * u_J * win_size I_var = I2_sum - 2 * u_I * I_sum + u_I * u_I * win_size J_var = J2_sum - 2 * u_J * J_sum + u_J * u_J * win_size return I_var, J_var, cross
Example #6
Source File: quaternion_ops.py From Pytorch-Quaternion-Neural-Networks with GNU General Public License v3.0 | 6 votes |
def quaternion_conv(input, r_weight, i_weight, j_weight, k_weight, bias, stride, padding, groups, dilatation): """ Applies a quaternion convolution to the incoming data: """ cat_kernels_4_r = torch.cat([r_weight, -i_weight, -j_weight, -k_weight], dim=1) cat_kernels_4_i = torch.cat([i_weight, r_weight, -k_weight, j_weight], dim=1) cat_kernels_4_j = torch.cat([j_weight, k_weight, r_weight, -i_weight], dim=1) cat_kernels_4_k = torch.cat([k_weight, -j_weight, i_weight, r_weight], dim=1) cat_kernels_4_quaternion = torch.cat([cat_kernels_4_r, cat_kernels_4_i, cat_kernels_4_j, cat_kernels_4_k], dim=0) if input.dim() == 3: convfunc = F.conv1d elif input.dim() == 4: convfunc = F.conv2d elif input.dim() == 5: convfunc = F.conv3d else: raise Exception("The convolutional input is either 3, 4 or 5 dimensions." " input.dim = " + str(input.dim())) return convfunc(input, cat_kernels_4_quaternion, bias, stride, padding, dilatation, groups)
Example #7
Source File: plca.py From pytorch-NMF with MIT License | 6 votes |
def update_params(self, VdivWZH, update_W, update_H, update_Z, W_alpha, H_alpha, Z_alpha): # type: (Tensor, bool, bool, bool, float, float, float) -> None VdivWZH = VdivWZH.view(self.channel, 1, self.N, self.K, self.M) if update_W or update_Z: new_W = F.conv3d(VdivWZH, self.H.mul(self.Z[:, None, None, None])[:, None], padding=self.pad_size) * self.W if update_H: new_H = F.conv3d(VdivWZH.transpose(0, 1), torch.transpose(self.W * self.Z[:, None, None, None], 0, 1))[ 0] * self.H new_H = normalize(self.fix_neg(new_H + H_alpha - 1), (1, 2, 3)) self.H[:] = new_H if update_W: self.W[:] = normalize(self.fix_neg(new_W + W_alpha - 1), (0, 2, 3, 4)) if update_Z: Z = normalize(self.fix_neg(new_W.sum((0, 2, 3, 4)) + Z_alpha - 1)) self.Z[:] = Z
Example #8
Source File: quaternion_ops.py From Quaternion-Recurrent-Neural-Networks with GNU General Public License v3.0 | 6 votes |
def quaternion_conv(input, r_weight, i_weight, j_weight, k_weight, bias, stride, padding, groups, dilatation): """ Applies a quaternion convolution to the incoming data: """ cat_kernels_4_r = torch.cat([r_weight, -i_weight, -j_weight, -k_weight], dim=1) cat_kernels_4_i = torch.cat([i_weight, r_weight, -k_weight, j_weight], dim=1) cat_kernels_4_j = torch.cat([j_weight, k_weight, r_weight, -i_weight], dim=1) cat_kernels_4_k = torch.cat([k_weight, -j_weight, i_weight, r_weight], dim=1) cat_kernels_4_quaternion = torch.cat([cat_kernels_4_r, cat_kernels_4_i, cat_kernels_4_j, cat_kernels_4_k], dim=0) if input.dim() == 3: convfunc = F.conv1d elif input.dim() == 4: convfunc = F.conv2d elif input.dim() == 5: convfunc = F.conv3d else: raise Exception("The convolutional input is either 3, 4 or 5 dimensions." " input.dim = " + str(input.dim())) return convfunc(input, cat_kernels_4_quaternion, bias, stride, padding, dilatation, groups)
Example #9
Source File: BayesianLayers.py From Tutorial_BayesianCompressionForDL with MIT License | 6 votes |
def forward(self, x): if self.deterministic: assert self.training == False, "Flag deterministic is True. This should not be used in training." return F.conv3d(x, self.post_weight_mu, self.bias_mu, self.stride, self.padding, self.dilation, self.groups) batch_size = x.size()[0] # apply local reparametrisation trick see [1] Eq. (6) # to the parametrisation given in [3] Eq. (6) mu_activations = F.conv3d(x, self.weight_mu, self.bias_mu, self.stride, self.padding, self.dilation, self.groups) var_weights = self.weight_logvar.exp() var_activations = F.conv3d(x.pow(2), var_weights, self.bias_logvar.exp(), self.stride, self.padding, self.dilation, self.groups) # compute z # note that we reparametrise according to [2] Eq. (11) (not [1]) z = reparametrize(self.z_mu.repeat(batch_size, 1, 1, 1, 1), self.z_logvar.repeat(batch_size, 1, 1, 1, 1), sampling=self.training, cuda=self.cuda) z = z[:, :, None, None, None] return reparametrize(mu_activations * z, (var_activations * z.pow(2)).log(), sampling=self.training, cuda=self.cuda)
Example #10
Source File: models.py From Autofocus-Layer with MIT License | 5 votes |
def forward(self, x): residual = x[:,:, 4:-4, 4:-4, 4:-4] x = self.conv1(x) x = self.bn1(x) x = self.relu(x) # compute attention weights for the second layer feature = x.detach() att = self.relu(self.convatt1(feature)) att = self.convatt2(att) #att = torch.sigmoid(att) att = F.softmax(att, dim=1) att = att[:,:,1:-1,1:-1,1:-1] # linear combination of different dilation rates x1 = self.conv2(x) shape = x1.size() x1 = self.bn_list2[0](x1)* att[:,0:1,:,:,:].expand(shape) # sharing weights in parallel convolutions for i in range(1, self.num_branches): x2 = F.conv3d(x, self.conv2.weight, padding=self.padding_list[i], dilation=self.dilation_list[i]) x2 = self.bn_list2[i](x2) x1 += x2* att[:,i:(i+1),:,:,:].expand(shape) if self.downsample is not None: residual = self.downsample(residual) x = x1 + residual x = self.relu(x) return x
Example #11
Source File: center_conv.py From Magic-VNet with MIT License | 5 votes |
def forward(self, x): weight = self.weight weight_mean = weight.mean(dim=1, keepdim=True).mean(dim=2, keepdim=True).mean(dim=3, keepdim=True) weight = weight - weight_mean return F.conv3d(x, weight, self.bias, self.stride, self.padding, self.dilation, self.groups)
Example #12
Source File: elastic.py From advex-uar with Apache License 2.0 | 5 votes |
def __init__(self, channels, kernel_size, sigma, dim=2): super(GaussianSmoothing, self).__init__() if isinstance(kernel_size, numbers.Number): kernel_size = [kernel_size] * dim if isinstance(sigma, numbers.Number): sigma = [sigma] * dim # The gaussian kernel is the product of the # gaussian function of each dimension. kernel = 1 meshgrids = torch.meshgrid( [ torch.arange(size, dtype=torch.float32) for size in kernel_size ] ) for size, std, mgrid in zip(kernel_size, sigma, meshgrids): mean = (size - 1) / 2 kernel *= 1 / (std * math.sqrt(2 * math.pi)) * \ torch.exp(-((mgrid - mean) / std) ** 2 / 2) # Make sure sum of values in gaussian kernel equals 1. kernel = kernel / torch.sum(kernel) # Reshape to depthwise convolutional weight kernel = kernel.view(1, 1, *kernel.size()) kernel = kernel.repeat(channels, *[1] * (kernel.dim() - 1)) self.register_buffer('weight', kernel) self.groups = channels if dim == 1: self.conv = F.conv1d elif dim == 2: self.conv = F.conv2d elif dim == 3: self.conv = F.conv3d else: raise RuntimeError( 'Only 1, 2 and 3 dimensions are supported. Received {}.'.format(dim) )
Example #13
Source File: stn.py From istn with Apache License 2.0 | 5 votes |
def bspline_kernel_3d(self, order): kernel_ones = torch.ones(1, 1, *self.control_point_spacing) kernel = kernel_ones for i in range(1, order + 1): kernel = F.conv3d(kernel, kernel_ones, padding=self.control_point_spacing.tolist()) / self.area return kernel.to(dtype=self.dtype, device=self.device)
Example #14
Source File: conv.py From pytorch-meta with MIT License | 5 votes |
def forward(self, input, params=None): if params is None: params = OrderedDict(self.named_parameters()) bias = params.get('bias', None) if self.padding_mode == 'circular': expanded_padding = ((self.padding[2] + 1) // 2, self.padding[2] // 2, (self.padding[1] + 1) // 2, self.padding[1] // 2, (self.padding[0] + 1) // 2, self.padding[0] // 2) return F.conv3d(F.pad(input, expanded_padding, mode='circular'), params['weight'], bias, self.stride, _triple(0), self.dilation, self.groups) return F.conv3d(input, params['weight'], bias, self.stride, self.padding, self.dilation, self.groups)
Example #15
Source File: utils.py From UMIS with Apache License 2.0 | 5 votes |
def forward(self, x): dx = F.conv3d(x, self.dX, padding=self.padding).abs() dy = F.conv3d(x, self.dY, padding=self.padding).abs() dz = F.conv3d(x, self.dZ, padding=self.padding).abs() return dx + dy + dz
Example #16
Source File: quaternion_ops.py From Quaternion-Recurrent-Neural-Networks with GNU General Public License v3.0 | 5 votes |
def quaternion_conv_rotation(input, r_weight, i_weight, j_weight, k_weight, bias, stride, padding, groups, dilatation): """ WORK IN PROGRESS ... NOT WORKING """ #cat_kernels_4_r = torch.cat([r_weight, -i_weight, -j_weight, -k_weight], dim=1) #cat_kernels_4_i = torch.cat([i_weight, r_weight, -k_weight, j_weight], dim=1) #cat_kernels_4_j = torch.cat([j_weight, k_weight, r_weight, -i_weight], dim=1) #cat_kernels_4_k = torch.cat([k_weight, -j_weight, i_weight, r_weight], dim=1) #cat_kernels_4_quaternion = torch.cat([cat_kernels_4_r, cat_kernels_4_i, cat_kernels_4_j, cat_kernels_4_k], dim=0) # W * I #if input.dim() == 2 : # intermediate = torch.mm(input, cat_kernels_4_quaternion) #else: # intermediate = torch.matmul(input, cat_kernels_4_quaternion) # Transposed W cat_kernels_4_r = torch.cat([r_weight, i_weight, j_weight, k_weight], dim=1) cat_kernels_4_i = torch.cat([-i_weight, r_weight, -k_weight, j_weight], dim=1) cat_kernels_4_j = torch.cat([-j_weight, k_weight, r_weight, -i_weight], dim=1) cat_kernels_4_k = torch.cat([-k_weight, -j_weight, i_weight, r_weight], dim=1) cat_kernels_4_quaternion = torch.cat([cat_kernels_4_r, cat_kernels_4_i, cat_kernels_4_j, cat_kernels_4_k], dim=0) if input.dim() == 3: convfunc = F.conv1d elif input.dim() == 4: convfunc = F.conv2d elif input.dim() == 5: convfunc = F.conv3d else: raise Exception("The convolutional input is either 3, 4 or 5 dimensions." " input.dim = " + str(input.dim())) return convfunc(input, cat_kernels_4_quaternion, bias, stride, padding, dilatation, groups)
Example #17
Source File: operations.py From NNEF-Tools with Apache License 2.0 | 5 votes |
def nnef_conv(input, # type: torch.Tensor filter, # type: torch.Tensor bias, # type: torch.Tensor border='constant', # type: str padding=None, # type: Optional[List[Tuple[int, int]]] stride=None, # type: Optional[List[int]] dilation=None, # type: Optional[List[int]] groups=1, # type: int ): # type: (...)->torch.Tensor if len(input.shape) not in (3, 4, 5): raise utils.NNEFToolsException( "Convolution is only implemented for 3D, 4D, 5D tensors, given: {}D.".format(len(input.shape))) bias = bias.reshape(1, 1).expand((1, filter.shape[0])) if utils.product(bias.size()) == 1 else bias spatial_dims = len(input.shape[2:]) groups = input.shape[1] if groups == 0 else groups stride = [1] * spatial_dims if not stride else stride dilation = [1] * spatial_dims if not dilation else dilation padding = shape_inference.same_padding(upscaled_input=input.shape[2:], filter=filter.shape[2:], stride=stride, dilation=dilation) if not padding else padding pad = nnef_pad(input=input, padding=[(0, 0)] * 2 + padding, border=border) conv = {1: F.conv1d, 2: F.conv2d, 3: F.conv3d}[spatial_dims](input=pad, weight=filter, bias=bias.squeeze(dim=0).contiguous(), stride=tuple(stride), padding=0, dilation=tuple(dilation), groups=groups) return conv
Example #18
Source File: demons.py From airlab with Apache License 2.0 | 5 votes |
def _regularise_3d(self, data): data.data = data.data.unsqueeze(0) data.data = F.conv3d(data.data, self._kernel, padding=self._padding, groups=3) data.data = data.data.squeeze()
Example #19
Source File: kernelFunction.py From airlab with Apache License 2.0 | 5 votes |
def bspline_kernel_3d(sigma=[1, 1, 1], order=2, asTensor=False, dtype=th.float32, device='cpu'): kernel_ones = th.ones(1, 1, *sigma) kernel = kernel_ones padding = np.array(sigma) - 1 for i in range(1, order + 1): kernel = F.conv3d(kernel, kernel_ones, padding=(padding).tolist())/(sigma[0]*sigma[1]*sigma[2]) if asTensor: return kernel[0, 0, ...].to(dtype=dtype, device=device) else: return kernel[0, 0, ...].numpy()
Example #20
Source File: pairwise.py From airlab with Apache License 2.0 | 5 votes |
def _lcc_loss_3d(self, warped_image, mask): mean_moving_image = F.conv3d(warped_image, self._kernel) variance_moving_image = F.conv3d(warped_image.pow(2), self._kernel) - (mean_moving_image.pow(2)) mean_fixed_moving_image = F.conv3d(self._fixed_image.image * warped_image, self._kernel) cc = (mean_fixed_moving_image - mean_moving_image*self._mean_fixed_image)**2\ /(variance_moving_image*self._variance_fixed_image + 1e-10) mask = F.conv3d(mask, self._kernel) mask = mask == 0 return -1.0 * th.masked_select(cc, mask)
Example #21
Source File: pairwise.py From airlab with Apache License 2.0 | 5 votes |
def __init__(self, fixed_image, moving_image,fixed_mask=None, moving_mask=None, sigma=[3], kernel_type="box", size_average=True, reduce=True): super(LCC, self).__init__(fixed_image, moving_image, fixed_mask, moving_mask, size_average, reduce) self._name = "lcc" self.warped_moving_image = th.empty_like(self._moving_image.image, dtype=self._dtype, device=self._device) self._kernel = None dim = len(self._moving_image.size) sigma = np.array(sigma) if sigma.size != dim: sigma_app = sigma[-1] while sigma.size != dim: sigma = np.append(sigma, sigma_app) if kernel_type == "box": kernel_size = sigma*2 + 1 self._kernel = th.ones(*kernel_size.tolist(), dtype=self._dtype, device=self._device) \ / float(np.product(kernel_size)**2) elif kernel_type == "gaussian": self._kernel = utils.gaussian_kernel(sigma, dim, asTensor=True, dtype=self._dtype, device=self._device) self._kernel.unsqueeze_(0).unsqueeze_(0) if dim == 2: self._lcc_loss = self._lcc_loss_2d # 2d lcc self._mean_fixed_image = F.conv2d(self._fixed_image.image, self._kernel) self._variance_fixed_image = F.conv2d(self._fixed_image.image.pow(2), self._kernel) \ - (self._mean_fixed_image.pow(2)) elif dim == 3: self._lcc_loss = self._lcc_loss_3d # 3d lcc self._mean_fixed_image = F.conv3d(self._fixed_image.image, self._kernel) self._variance_fixed_image = F.conv3d(self._fixed_image.image.pow(2), self._kernel) \ - (self._mean_fixed_image.pow(2))
Example #22
Source File: nmf.py From pytorch-NMF with MIT License | 5 votes |
def get_H_positive(self, WH, beta, W_sum): W = self.W if beta == 1: if W_sum is None: W_sum = W.sum((0, 2, 3, 4)) denominator = W_sum[:, None, None, None] else: if beta != 2: WH = WH.pow(beta - 1) WH = WH.view(1, self.channel, self.N, self.K, self.M) WtWH = F.conv3d(WH, W.transpose(0, 1))[0] denominator = WtWH return denominator, W_sum
Example #23
Source File: nmf.py From pytorch-NMF with MIT License | 5 votes |
def get_W_positive(self, WH, beta, H_sum): H = self.H if beta == 1: if H_sum is None: H_sum = H.sum((1, 2, 3)) denominator = H_sum[None, :, None, None, None] else: if beta != 2: WH = WH.pow(beta - 1) WH = WH.view(self.channel, 1, self.N, self.K, self.M) WHHt = F.conv3d(WH, H[:, None]) denominator = WHHt return denominator, H_sum
Example #24
Source File: nmf.py From pytorch-NMF with MIT License | 5 votes |
def reconstruct(self, H, W): out = F.conv3d(H[None, ...], W.flip((2, 3, 4)), padding=self.pad_size)[0] if self.channel == 1: return out[0] return out
Example #25
Source File: plca.py From pytorch-NMF with MIT License | 5 votes |
def reconstruct(self, W, Z, H): out = F.conv3d(H[None, ...], W.mul(Z[:, None, None, None]).flip((2, 3, 4)), padding=self.pad_size)[0] if self.channel == 1: return out[0] return out
Example #26
Source File: array.py From MONAI with Apache License 2.0 | 5 votes |
def __call__(self, img): """ Args: img: torch tensor data to extract the contour, with shape: [batch_size, channels, height, width[, depth]] Returns: A torch tensor with the same shape as img, note: 1. it's the binary classification result of whether a pixel is edge or not. 2. in order to keep the original shape of mask image, we use padding as default. 3. the edge detection is just approximate because it defects inherent to Laplace kernel, ideally the edge should be thin enough, but now it has a thickness. """ channels = img.shape[1] if img.ndim == 4: kernel = torch.tensor([[-1, -1, -1], [-1, 8, -1], [-1, -1, -1]], dtype=torch.float32, device=img.device) kernel = kernel.repeat(channels, 1, 1, 1) contour_img = F.conv2d(img, kernel, bias=None, stride=1, padding=1, dilation=1, groups=channels) elif img.ndim == 5: kernel = -1 * torch.ones(3, 3, 3, dtype=torch.float32, device=img.device) kernel[1, 1, 1] = 26 kernel = kernel.repeat(channels, 1, 1, 1, 1) contour_img = F.conv3d(img, kernel, bias=None, stride=1, padding=1, dilation=1, groups=channels) else: raise RuntimeError("the dimensions of img should be 4 or 5.") torch.clamp_(contour_img, min=0.0, max=1.0) return contour_img
Example #27
Source File: multiview.py From pretorched-x with MIT License | 5 votes |
def forward(self, input): x = torch.stack([ F.conv3d(input, self.weight.view(*self.channel_shape, *kernel_size), self.bias, self.stride, padding, self.dilation, self.groups) for kernel_size, padding in zip(self.kernel_sizes, self.paddings)], -1) x = self.linear(x)[..., 0] return x
Example #28
Source File: image.py From airlab with Apache License 2.0 | 4 votes |
def create_image_pyramid(image, down_sample_factor): image_dim = len(image.size) image_pyramide = [] if image_dim == 2: for level in down_sample_factor: sigma = (th.tensor(level)/2).to(dtype=th.float32) kernel = kernelFunction.gaussian_kernel_2d(sigma.numpy(), asTensor=True) padding = np.array([(x - 1)/2 for x in kernel.size()], dtype=int).tolist() kernel = kernel.unsqueeze(0).unsqueeze(0) kernel = kernel.to(dtype=image.dtype, device=image.device) image_sample = F.conv2d(image.image, kernel, stride=level, padding=padding) image_size = image_sample.size()[-image_dim:] image_spacing = [x*y for x, y in zip(image.spacing, level)] image_origin = image.origin image_pyramide.append(Image(image_sample, image_size, image_spacing, image_origin)) image_pyramide.append(image) elif image_dim == 3: for level in down_sample_factor: sigma = (th.tensor(level)/2).to(dtype=th.float32) kernel = kernelFunction.gaussian_kernel_3d(sigma.numpy(), asTensor=True) padding = np.array([(x - 1) / 2 for x in kernel.size()], dtype=int).tolist() kernel = kernel.unsqueeze(0).unsqueeze(0) kernel = kernel.to(dtype=image.dtype, device=image.device) image_sample = F.conv3d(image.image, kernel, stride=level, padding=padding) image_size = image_sample.size()[-image_dim:] image_spacing = [x*y for x, y in zip(image.spacing, level)] image_origin = image.origin image_pyramide.append(Image(image_sample, image_size, image_spacing, image_origin)) image_pyramide.append(image) else: print("Error: ", image_dim, " is not supported with create_image_pyramide()") sys.exit(-1) return image_pyramide
Example #29
Source File: models.py From Autofocus-Layer with MIT License | 4 votes |
def forward(self, x): residual = x[:,:, 4:-4, 4:-4, 4:-4] # compute attention weights in the first autofocus convolutional layer feature = x.detach() att = self.relu(self.convatt11(feature)) att = self.convatt12(att) att = F.softmax(att, dim=1) att = att[:,:,1:-1,1:-1,1:-1] # linear combination of different rates x1 = self.conv1(x) shape = x1.size() x1 = self.bn_list1[0](x1)* att[:,0:1,:,:,:].expand(shape) for i in range(1, self.num_branches): x2 = F.conv3d(x, self.conv1.weight, padding=self.padding_list[i], dilation=self.dilation_list[i]) x2 = self.bn_list1[i](x2) x1 += x2* att[:,i:(i+1),:,:,:].expand(shape) x = self.relu(x1) # compute attention weights for the second autofocus layer feature2 = x.detach() att2 = self.relu(self.convatt21(feature2)) att2 = self.convatt22(att2) att2 = F.softmax(att2, dim=1) att2 = att2[:,:,1:-1,1:-1,1:-1] # linear combination of different rates x21 = self.conv2(x) shape = x21.size() x21 = self.bn_list2[0](x21)* att2[:,0:1,:,:,:].expand(shape) for i in range(1, self.num_branches): x22 = F.conv3d(x, self.conv2.weight, padding =self.padding_list[i], dilation=self.dilation_list[i]) x22 = self.bn_list2[i](x22) x21 += x22* att2[:,i:(i+1),:,:,:].expand(shape) if self.downsample is not None: residual = self.downsample(residual) x = x21 + residual x = self.relu(x) return x
Example #30
Source File: model.py From CFUN with MIT License | 4 votes |
def compute_mrcnn_mask_edge_loss(target_masks, target_class_ids, pred_masks): """Mask edge mean square error loss for the Edge Agreement Head. Here I use the Sobel kernel without smoothing the ground_truth masks. target_masks: [batch, num_rois, depth, height, width]. target_class_ids: [batch, num_rois]. Integer class IDs. Zero padded. pred_masks: [batch, proposals, num_classes, depth, height, width] float32 tensor with values from 0 to 1. """ if target_class_ids.size()[0] != 0: # Generate the xyz dimension Sobel kernels kernel_x = np.array([[[1, 2, 1], [0, 0, 0], [-1, -2, -1]], [[2, 4, 2], [0, 0, 0], [-2, -4, -2]], [[1, 2, 1], [0, 0, 0], [-1, -2, -1]]]) kernel_y = kernel_x.transpose((1, 0, 2)) kernel_z = kernel_x.transpose((0, 2, 1)) kernel = torch.from_numpy(np.array([kernel_x, kernel_y, kernel_z]).reshape((3, 1, 3, 3, 3))).float().cuda() # Only positive ROIs contribute to the loss. And only the class specific mask of each ROI. positive_ix = torch.nonzero(target_class_ids > 0)[:, 0] positive_class_ids = target_class_ids[positive_ix.detach()].long() indices = torch.stack((positive_ix, positive_class_ids), dim=1) # Gather the masks (predicted and true) that contribute to loss y_true = target_masks[:indices.size()[0], 1:, :, :] y_pred = pred_masks[indices[:, 0].detach(), 1:, :, :, :] # Implement the edge detection convolution loss_fn = nn.MSELoss() loss = torch.FloatTensor([0]).cuda() for i in range(indices.size()[0]): # only compute the tumor's edge y_true_ = y_true[i] y_pred_ = y_pred[i].unsqueeze(0) # [N, 2, 64, 64, 64] for j in range(y_true_.shape[0]): y_true_final = F.conv3d(y_true_[j, :, :, :].unsqueeze(0).unsqueeze(0).cuda().float(), kernel) y_pred_final = F.conv3d(y_pred_[:, j, :, :, :].unsqueeze(1), kernel) # y_true_final = torch.sqrt(torch.pow(y_true_final[:, 0], 2) + torch.pow(y_true_final[:, 1], 2) + # torch.pow(y_true_final[:, 0], 2)) # y_pred_final = torch.sqrt(torch.pow(y_pred_final [:, 0], 2) + torch.pow(y_pred_final [:, 1], 2) + # torch.pow(y_pred_final [:, 0], 2)) # Mean Square Error loss += loss_fn(y_pred_final, y_true_final) # import pdb # pdb.set_trace() loss /= indices.size()[0] else: loss = Variable(torch.FloatTensor([0]), requires_grad=False).cuda() return loss