小编Dir*_* Li的帖子

如何在 Pytorch 中使用 torchvision.transforms 进行分割任务的数据增强?

我对 PyTorch 中执行的数据增强有点困惑。

因为我们处理的是分割任务,所以同样的数据增强我们需要数据和掩码,但其中一些是随机的,比如随机旋转。

Keras 提供了random seed数据和掩码做相同操作的保证,如下代码所示:

    data_gen_args = dict(featurewise_center=True,
                         featurewise_std_normalization=True,
                         rotation_range=25,
                         horizontal_flip=True,
                         vertical_flip=True)


    image_datagen = ImageDataGenerator(**data_gen_args)
    mask_datagen = ImageDataGenerator(**data_gen_args)

    seed = 1
    image_generator = image_datagen.flow(train_data, seed=seed, batch_size=1)
    mask_generator = mask_datagen.flow(train_label, seed=seed, batch_size=1)

    train_generator = zip(image_generator, mask_generator)
Run Code Online (Sandbox Code Playgroud)

在Pytorch官方文档中没有找到类似的描述,所以不知道如何保证data和mask能同步处理。

Pytorch 确实提供了这样的功能,但我想将其应用于自定义 Dataloader。

例如?

def __getitem__(self, index):
    img = np.zeros((self.im_ht, self.im_wd, channel_size))
    mask = np.zeros((self.im_ht, self.im_wd, channel_size))

    temp_img = np.load(Image_path + '{:0>4}'.format(self.patient_index[index]) + '.npy')
    temp_label = np.load(Label_path + '{:0>4}'.format(self.patient_index[index]) + '.npy')

    for i in range(channel_size):
        img[:,:,i] = temp_img[self.count[index] + i] …
Run Code Online (Sandbox Code Playgroud)

python transformation pytorch torchvision

6
推荐指数
2
解决办法
9725
查看次数

为什么设置eval()后pytorch模型表现不佳?

我使用 pytorch 构建了一个使用 BatchNormalization 层的分割模型。我发现当我设置model.eval()测试时,测试结果将为0。如果我不设置model.eval(),它会表现良好。

我试图搜索相关问题,但得到了model.eval()可以修复 的参数的结论BN,但我仍然对如何解决这个问题感到困惑。

我的批量大小是 1,这是我的模型:

import torch
import torch.nn as nn


class Encode_Block(nn.Module):
    def __init__(self, in_feat, out_feat):
        super(Encode_Block, self).__init__()

        self.conv1 = Res_Block(in_feat, out_feat)
        self.conv2 = Res_Block_identity(out_feat, out_feat)

    def forward(self, inputs):
        outputs = self.conv1(inputs)
        outputs = self.conv2(outputs)
        return outputs


class Decode_Block(nn.Module):
    def __init__(self, in_feat, out_feat):
        super(Decode_Block, self).__init__()

        self.conv1 = Res_Block(in_feat, out_feat)
        self.conv2 = Res_Block_identity(out_feat, out_feat)

    def forward(self, inputs):
        outputs = self.conv1(inputs)
        outputs = self.conv2(outputs)
        return outputs


class Conv_Block(nn.Module):
    def …
Run Code Online (Sandbox Code Playgroud)

python image-segmentation deep-learning pytorch

5
推荐指数
1
解决办法
2532
查看次数