【问题标题】:TypeError: div(): argument 'other' (position 1) must be Tensor, not numpy.bool_TypeError: div(): argument 'other' (position 1) must be Tensor, not numpy.bool_
【发布时间】: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 是什么?那是一个带有boolean dtype 的 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


【解决方案1】:

在初始化掩码后添加这一行。

mask = np.array(mask, dtype='float')

方法torch.from_numpy 需要floatnp.float 类型的对象,因为torch 中的每个张量都是torch.FloatTensor
有几种方法可以绕过它。一种方法是使用np.arraydtype='float'
OR
将布尔数组转换为浮点数,然后声明一个空的torch.BoolTensor,然后使用方法from_numpy

PS
np.array 为加载的图像选择合适的数据类型。对于bool 图像,它是 np.bool.

【讨论】:

    猜你喜欢
    • 2019-05-25
    • 2019-12-02
    • 1970-01-01
    • 1970-01-01
    • 2020-03-28
    • 2016-11-13
    • 1970-01-01
    • 2017-08-07
    • 1970-01-01
    相关资源
    最近更新 更多