【发布时间】:2019-12-28 18:38:33
【问题描述】:
我正在尝试使用医学图像上的模型 ODE-Net 来获得一些分割结果。
在进行实验时,出现了这个错误:
TypeError: div(): argument 'other' (position 1) must be Tensor, not numpy.bool_
错误显示在以下代码部分的最后一行:
def apply_data_augmentation(self, image, mask):
patch = torch.from_numpy(image.transpose(2, 0, 1)).float() / 255
n_glands = mask.max()
label = torch.from_numpy(mask).float() / n_glands
mask 是图像的基本事实。
以上部分前面有:
def __getitem__(self, index):
image, mask = self.index_to_filename(index)
image, mask = self.open_and_resize(image, mask)
image, mask = self.pad_image(image, mask)
label, patch = self.apply_data_augmentation(image, mask)
label = self.create_eroded_mask(label, mask)
patch, label = self.extract_random_region(image, patch, label)
return patch, label.float()
def index_to_filename(self, index):
"""Helper function to retrieve filenames from index"""
index_img = index // self.repeat
index_img = self.images[index_img]
index_str = str(index_img.item() + 1)
image = self.image_fname + index_str + '.jpg'
mask = self.image_fname + index_str + '_anno.png'
return image, mask
def open_and_resize(self, image, mask):
"""Helper function to pad smaller image to the correct size"""
image = PIL.Image.open(image)
mask = PIL.Image.open(mask)
ratio = (775 / 512)
new_size = (int(round(image.size[0] / ratio)),
int(round(image.size[1] / ratio)))
image = image.resize(new_size)
mask = mask.resize(new_size)
image = np.array(image)
mask = np.array(mask)
return image, mask
def pad_image(self, image, mask):
"""Helper function to pad smaller image to the correct size"""
if not self.validation:
pad_h = max(self.patch_size[0] - image.shape[0], 128)
pad_w = max(self.patch_size[1] - image.shape[1], 128)
else:
# we pad more than needed to later do translation augmentation
pad_h = max((self.patch_size[0] - image.shape[0]) // 2 + 1, 0)
pad_w = max((self.patch_size[1] - image.shape[1]) // 2 + 1, 0)
padded_image = np.pad(image, ((pad_h, pad_h), (pad_w, pad_w), (0, 0)), mode='reflect')
mask = np.pad(mask, ((pad_h, pad_h), (pad_w, pad_w)), mode='reflect')
return padded_image, mask
在这方面的任何帮助将不胜感激;提前致谢!
PS:我正在 Google Colab 上尝试代码。
【问题讨论】:
-
这是您自己的代码吗?如果有,目的是什么?
mask是什么?那是一个带有booleandtype 的 numpy 数组吗?如果是这样,max将只是True/False,而不是某种计数。除以布尔值是没有意义的,不是吗? -
@hpaulj 不,先生!那不是我自己的代码。我已经在他们测试的数据上使用代码复制了结果。现在,我想在这种不同的数据上进行尝试。
-
@hpaulj 先生,
mask是图像的基本事实,在这部分代码之上定义为:pad_h = max((self.patch_size[0] - image.shape[0]) // 2 + 1, 0)pad_w = max((self.patch_size[1] - image.shape[1]) // 2 + 1, 0)padded_image = np.pad(image, ((pad_h, pad_h), (pad_w, pad_w), (0, 0)), mode='reflect')mask = np.pad(mask, ((pad_h, pad_h), (pad_w, pad_w)), mode='reflect') -
评论中的代码难以阅读。
-
@hpaulj 对不起,先生!我已经在问题中包含了代码。请检查那里,先生。
标签: python numpy tensorflow tensor