我对 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) 我使用 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)