首页 > 解决方案 > 使用 Pytorch Conv2d 将多个过滤器应用于多个 CT 扫描中的每一层的最快方法是什么?

问题描述

假设输入数据集包含 100 名患者的 CT 扫描,每次扫描包含 16 层,每层包含 512 x 512 像素。我想在每次 CT 扫描中对每一层应用八个 3x3 卷积过滤器。因此,输入数组的形状为 [100, 16, 512, 512],内核数组的形状为 [8, 3, 3]。应用卷积后,目标是一个形状为 [100, 16, 8, 512, 512] 的输出数组。以下代码使用 Pytorch Conv2d 函数来实现这一点;但是,我想知道 groups 参数(和/或其他方式)是否可以以某种方式消除对循环的需要。

 for layer_index in range(0, number_of_layers):
    # Getting current ct scan layer for all patients
    # ct_scans dimensions are:  [patient, scan layer, pixel row, pixel column]
    # ct_scans shape: [100, 16, 512, 512]
    image_stack = ct_scans[:, layer_index, :, :]
    # Converting from numpy to tensor format
    image_stack_t = torch.from_numpy(image[:, None, :, :])
    # Applying convolution to create 8 filtered versions of current scan layer across all patients
    # shape of kernels is: [8, 3, 3]
    filtered_image_stack_t = conv2d(image_stack_t, kernels, padding=1, groups=1)
    # Converting from tensor format back to numpy format
    filtered_image_stack = filtered_image_stack_t.numpy()
    # Amassing filtered ct scans for all patients back into one array
    # filtered_ct_scans dimensions are: [patient, ct scan layer, filter number, pixel row, pixel column]
    # filtered_ct_scans shape is: [100, 16, 8, 512, 512]
    filtered_ct_scans[:, layer_index, :, :, :] = filtered_image_stack

到目前为止,我尝试使用除此之外的任何东西都会groups=1导致错误。我还发现了以下类似的帖子;但是,他们没有解决我的具体问题。

如何在 PyTorch conv2d 函数中批量使用组参数?

如何在 PyTorch conv2d 函数中使用组参数

标签: pythonpytorch

解决方案


您不需要使用分组卷积。只需适当调整输入的大小即可。

import torch
import torch.nn.functional as F

ct_scans = torch.randn((100,16,512,512))
kernels = torch.randn((8,1,3,3))

B,L,H,W = ct_scans.shape #(batch,layers,height,width)
ct_scans = ct_scans.view(-1,H,W)
ct_scans.unsqueeze_(1)
out = F.conv2d(ct_scans, kernels)
out = out.view(B,L,*out.shape[1:])
print(out)

推荐阅读