Python torch.as_tensor() Examples

The following are 30 code examples of torch.as_tensor(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module torch , or try the search function .
Example #1
Source File: keypoint.py    From Res2Net-maskrcnn with MIT License 6 votes vote down vote up
def __init__(self, keypoints, size, mode=None):
        # FIXME remove check once we have better integration with device
        # in my version this would consistently return a CPU tensor
        device = keypoints.device if isinstance(keypoints, torch.Tensor) else torch.device('cpu')
        keypoints = torch.as_tensor(keypoints, dtype=torch.float32, device=device)
        num_keypoints = keypoints.shape[0]
        if num_keypoints:
            keypoints = keypoints.view(num_keypoints, -1, 3)
        
        # TODO should I split them?
        # self.visibility = keypoints[..., 2]
        self.keypoints = keypoints# [..., :2]

        self.size = size
        self.mode = mode
        self.extra_fields = {} 
Example #2
Source File: base.py    From nsf with MIT License 6 votes vote down vote up
def log_prob(self, inputs, context=None):
        """Calculate log probability under the distribution.

        Args:
            inputs: Tensor, input variables.
            context: Tensor or None, conditioning variables. If a Tensor, it must have the same
                number or rows as the inputs. If None, the context is ignored.

        Returns:
            A Tensor of shape [input_size], the log probability of the inputs given the context.
        """
        inputs = torch.as_tensor(inputs)
        if context is not None:
            context = torch.as_tensor(context)
            if inputs.shape[0] != context.shape[0]:
                raise ValueError('Number of input items must be equal to number of context items.')
        return self._log_prob(inputs, context) 
Example #3
Source File: keypoint.py    From R2CNN.pytorch with MIT License 6 votes vote down vote up
def __init__(self, keypoints, size, mode=None):
        # FIXME remove check once we have better integration with device
        # in my version this would consistently return a CPU tensor
        device = keypoints.device if isinstance(keypoints, torch.Tensor) else torch.device('cpu')
        keypoints = torch.as_tensor(keypoints, dtype=torch.float32, device=device)
        num_keypoints = keypoints.shape[0]
        if num_keypoints:
            keypoints = keypoints.view(num_keypoints, -1, 3)
        
        # TODO should I split them?
        # self.visibility = keypoints[..., 2]
        self.keypoints = keypoints# [..., :2]

        self.size = size
        self.mode = mode
        self.extra_fields = {} 
Example #4
Source File: bounding_box.py    From R2CNN.pytorch with MIT License 6 votes vote down vote up
def __init__(self, bbox, image_size, mode="xyxy"):
        device = bbox.device if isinstance(bbox, torch.Tensor) else torch.device("cpu")
        bbox = torch.as_tensor(bbox, dtype=torch.float32, device=device)
        if bbox.ndimension() != 2:
            raise ValueError(
                "bbox should have 2 dimensions, got {}".format(bbox.ndimension())
            )
        if bbox.size(-1) != 4:
            raise ValueError(
                "last dimenion of bbox should have a "
                "size of 4, got {}".format(bbox.size(-1))
            )
        if mode not in ("xyxy", "xywh"):
            raise ValueError("mode should be 'xyxy' or 'xywh'")

        self.bbox = bbox
        self.size = image_size  # (image_width, image_height)
        self.mode = mode
        self.extra_fields = {} 
Example #5
Source File: parsing.py    From Parsing-R-CNN with MIT License 6 votes vote down vote up
def __init__(self, parsing, size, mode=None):
        if isinstance(parsing, torch.Tensor):
            # The raw data representation is passed as argument
            parsing = parsing.clone()
        elif isinstance(parsing, (list, tuple)):
            parsing = torch.as_tensor(parsing)

        if len(parsing.shape) == 2:
            # if only a single instance mask is passed
            parsing = parsing[None]

        assert len(parsing.shape) == 3
        assert parsing.shape[1] == size[1], "%s != %s" % (parsing.shape[1], size[1])
        assert parsing.shape[2] == size[0], "%s != %s" % (parsing.shape[2], size[0])

        self.parsing = parsing
        self.size = size
        self.mode = mode
        self.extra_fields = {} 
Example #6
Source File: keypoint.py    From Parsing-R-CNN with MIT License 6 votes vote down vote up
def __init__(self, keypoints, size, mode=None):
        # FIXME remove check once we have better integration with device
        # in my version this would consistently return a CPU tensor
        device = keypoints.device if isinstance(keypoints, torch.Tensor) else torch.device('cpu')
        keypoints = torch.as_tensor(keypoints, dtype=torch.float32, device=device)
        num_keypoints = keypoints.shape[0]
        if num_keypoints:
            keypoints = keypoints.view(num_keypoints, -1, 3)

        # TODO should I split them?
        # self.visibility = keypoints[..., 2]
        self.keypoints = keypoints  # [..., :2]

        self.size = size
        self.mode = mode
        self.extra_fields = {} 
Example #7
Source File: quad_bounding_box.py    From R2CNN.pytorch with MIT License 6 votes vote down vote up
def __init__(self, quad_bbox, image_size, mode="xyxy"):
        device = quad_bbox.device if isinstance(quad_bbox, torch.Tensor) else torch.device("cpu")
        quad_bbox = torch.as_tensor(quad_bbox, dtype=torch.float32, device=device)
        if quad_bbox.ndimension() != 2:
            raise ValueError(
                "bbox should have 2 dimensions, got {}".format(quad_bbox.ndimension())
            )
        if quad_bbox.size(-1) != 8:
            raise ValueError(
                "last dimenion of bbox should have a "
                "size of 8, got {}".format(quad_bbox.size(-1))
            )
        if mode not in ("xyxy"):
            raise ValueError("mode should be 'xyxy'")
        self.device = device
        self.quad_bbox = quad_bbox
        self.bbox = self.quad_bbox_to_bbox()
        self.size = image_size  # (image_width, image_height)
        self.mode = mode
        self.extra_fields = {} 
Example #8
Source File: context_conditioned_policy.py    From garage with MIT License 6 votes vote down vote up
def get_action(self, obs):
        """Sample action from the policy, conditioned on the task embedding.

        Args:
            obs (torch.Tensor): Observation values, with shape :math:`(1, O)`.
                O is the size of the flattened observation space.

        Returns:
            torch.Tensor: Output action value, with shape :math:`(1, A)`.
                A is the size of the flattened action space.
            dict:
                * np.ndarray[float]: Mean of the distribution.
                * np.ndarray[float]: Standard deviation of logarithmic values
                    of the distribution.

        """
        z = self.z
        obs = torch.as_tensor(obs[None], device=global_device()).float()
        obs_in = torch.cat([obs, z], dim=1)
        action, info = self._policy.get_action(obs_in)
        action = np.squeeze(action, axis=0)
        info['mean'] = np.squeeze(info['mean'], axis=0)
        return action, info 
Example #9
Source File: test_policy.py    From spinningup with MIT License 6 votes vote down vote up
def load_pytorch_policy(fpath, itr, deterministic=False):
    """ Load a pytorch policy saved with Spinning Up Logger."""
    
    fname = osp.join(fpath, 'pyt_save', 'model'+itr+'.pt')
    print('\n\nLoading from %s.\n\n'%fname)

    model = torch.load(fname)

    # make function for producing an action given a single state
    def get_action(x):
        with torch.no_grad():
            x = torch.as_tensor(x, dtype=torch.float32)
            action = model.act(x)
        return action

    return get_action 
Example #10
Source File: test_gyrovector_math.py    From geoopt with Apache License 2.0 6 votes vote down vote up
def test_n_additions_via_scalar_multiplication(n, a, dtype, negative, manifold, strict):
    n = torch.as_tensor(n, dtype=a.dtype).requires_grad_()
    y = torch.zeros_like(a)
    for _ in range(int(n.item())):
        y = manifold.mobius_add(a, y)
    ny = manifold.mobius_scalar_mul(n, a)
    if negative:
        tolerance = {
            torch.float32: dict(atol=4e-5, rtol=1e-3),
            torch.float64: dict(atol=1e-5, rtol=1e-3),
        }
    else:
        tolerance = {
            torch.float32: dict(atol=2e-6, rtol=1e-3),
            torch.float64: dict(atol=1e-5, rtol=1e-3),
        }
    tolerant_allclose_check(y, ny, strict=strict, **tolerance[dtype])
    ny.sum().backward()
    assert torch.isfinite(n.grad).all()
    assert torch.isfinite(a.grad).all()
    assert torch.isfinite(manifold.k.grad).all() 
Example #11
Source File: bounding_box.py    From Res2Net-maskrcnn with MIT License 6 votes vote down vote up
def __init__(self, bbox, image_size, mode="xyxy"):
        device = bbox.device if isinstance(bbox, torch.Tensor) else torch.device("cpu")
        bbox = torch.as_tensor(bbox, dtype=torch.float32, device=device)
        if bbox.ndimension() != 2:
            raise ValueError(
                "bbox should have 2 dimensions, got {}".format(bbox.ndimension())
            )
        if bbox.size(-1) != 4:
            raise ValueError(
                "last dimenion of bbox should have a "
                "size of 4, got {}".format(bbox.size(-1))
            )
        if mode not in ("xyxy", "xywh"):
            raise ValueError("mode should be 'xyxy' or 'xywh'")

        self.bbox = bbox
        self.size = image_size  # (image_width, image_height)
        self.mode = mode
        self.extra_fields = {} 
Example #12
Source File: scaled.py    From geoopt with Apache License 2.0 6 votes vote down vote up
def __init__(self, manifold: Manifold, scale=1.0, learnable=False):
        super().__init__()
        self.base = manifold
        scale = torch.as_tensor(scale, dtype=torch.get_default_dtype())
        scale = scale.requires_grad_(False)
        if not learnable:
            self.register_buffer("_scale", scale)
            self.register_buffer("_log_scale", None)
        else:
            self.register_buffer("_scale", None)
            self.register_parameter("_log_scale", torch.nn.Parameter(scale.log()))
        # do not rebuild scaled functions very frequently, save them

        for method, scaling_info in self.base.__scaling__.items():
            # register rescaled functions as bound methods of this particular instance
            unbound_method = getattr(self.base, method).__func__  # unbound method
            self.__setattr__(
                method, types.MethodType(rescale(unbound_method, scaling_info), self)
            ) 
Example #13
Source File: test_negative_sampling.py    From pytorch_geometric with MIT License 6 votes vote down vote up
def test_batched_negative_sampling():
    edge_index = torch.as_tensor([[0, 0, 1, 2], [0, 1, 2, 3]])
    edge_index = torch.cat([edge_index, edge_index + 4], dim=1)
    batch = torch.tensor([0, 0, 0, 0, 1, 1, 1, 1])

    neg_edge_index = batched_negative_sampling(edge_index, batch)
    assert neg_edge_index.size(1) <= edge_index.size(1)

    adj = torch.zeros(8, 8, dtype=torch.bool)
    adj[edge_index[0], edge_index[1]] = True

    neg_adj = torch.zeros(8, 8, dtype=torch.bool)
    neg_adj[neg_edge_index[0], neg_edge_index[1]] = True
    assert (adj & neg_adj).sum() == 0
    assert neg_adj[:4, 4:].sum() == 0
    assert neg_adj[4:, :4].sum() == 0 
Example #14
Source File: icdar.py    From R2CNN.pytorch with MIT License 6 votes vote down vote up
def __getitem__(self, idx):
        anno = self.ids[idx]
        boxes = [obj["bbox"] for obj in anno['objs'] if self.keep_difficult or not obj['isDifficult']]
        boxes = torch.as_tensor(boxes).reshape(-1, 8)
        target = QuadBoxList(boxes, [anno['width'], anno['height']], mode="xyxy")
        classes = [obj["category_id"] for obj in anno['objs']]
        classes = torch.tensor(classes)
        target.add_field("labels", classes)
        target = target.clip_to_image(remove_empty=False)

        img = Image.open(os.path.join(self.img_dir, anno['img_name'])).convert("RGB")

        if self.transforms is not None:
            img, target = self.transforms(img, target)

        return img, target, idx 
Example #15
Source File: keypoint.py    From Clothing-Detection with GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, keypoints, size, mode=None):
        # FIXME remove check once we have better integration with device
        # in my version this would consistently return a CPU tensor
        device = keypoints.device if isinstance(keypoints, torch.Tensor) else torch.device('cpu')
        keypoints = torch.as_tensor(keypoints, dtype=torch.float32, device=device)
        num_keypoints = keypoints.shape[0]
        if num_keypoints:
            keypoints = keypoints.view(num_keypoints, -1, 3)
        
        # TODO should I split them?
        # self.visibility = keypoints[..., 2]
        self.keypoints = keypoints# [..., :2]

        self.size = size
        self.mode = mode
        self.extra_fields = {} 
Example #16
Source File: bounding_box.py    From Clothing-Detection with GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, bbox, image_size, mode="xyxy"):
        device = bbox.device if isinstance(bbox, torch.Tensor) else torch.device("cpu")
        bbox = torch.as_tensor(bbox, dtype=torch.float32, device=device)
        if bbox.ndimension() != 2:
            raise ValueError(
                "bbox should have 2 dimensions, got {}".format(bbox.ndimension())
            )
        if bbox.size(-1) != 4:
            raise ValueError(
                "last dimension of bbox should have a "
                "size of 4, got {}".format(bbox.size(-1))
            )
        if mode not in ("xyxy", "xywh"):
            raise ValueError("mode should be 'xyxy' or 'xywh'")

        self.bbox = bbox
        self.size = image_size  # (image_width, image_height)
        self.mode = mode
        self.extra_fields = {}
        #self.bbox_id = uuid.uuid4() 
Example #17
Source File: coco.py    From Clothing-Detection with GNU General Public License v3.0 5 votes vote down vote up
def __getitem__(self, idx):
        img, anno = super(COCODataset, self).__getitem__(idx)

        # filter crowd annotations
        # TODO might be better to add an extra field
        anno = [obj for obj in anno if obj["iscrowd"] == 0]

        boxes = [obj["bbox"] for obj in anno]
        boxes = torch.as_tensor(boxes).reshape(-1, 4)  # guard against no boxes
        target = BoxList(boxes, img.size, mode="xywh").convert("xyxy")

        classes = [obj["category_id"] for obj in anno]
        classes = [self.json_category_id_to_contiguous_id[c] for c in classes]
        classes = torch.tensor(classes)
        target.add_field("labels", classes)

        if anno and "segmentation" in anno[0]:
            masks = [obj["segmentation"] for obj in anno]
            masks = SegmentationMask(masks, img.size, mode='poly')
            target.add_field("masks", masks)

        if anno and "keypoints" in anno[0]:
            keypoints = [obj["keypoints"] for obj in anno]
            keypoints = PersonKeypoints(keypoints, img.size)
            target.add_field("keypoints", keypoints)

        target = target.clip_to_image(remove_empty=True)

        if self._transforms is not None:
            img, target = self._transforms(img, target)

        return img, target, idx 
Example #18
Source File: exercise1_2_soln.py    From spinningup with MIT License 5 votes vote down vote up
def __init__(self, obs_dim, act_dim, hidden_sizes, activation):
        super().__init__()
        log_std = -0.5 * np.ones(act_dim, dtype=np.float32)
        self.log_std = torch.nn.Parameter(torch.as_tensor(log_std))
        self.mu_net = mlp([obs_dim] + list(hidden_sizes) + [act_dim], activation) 
Example #19
Source File: modanet.py    From Clothing-Detection with GNU General Public License v3.0 5 votes vote down vote up
def __getitem__(self, idx):
        img, anno = super(ModaNetDataset, self).__getitem__(idx)
        
        # filter crowd annotations
        # TODO might be better to add an extra field
        anno = [obj for obj in anno if obj["iscrowd"] == 0]

        boxes = [obj["bbox"] for obj in anno]
        boxes = torch.as_tensor(boxes).reshape(-1, 4)  # guard against no boxes
        target = BoxList(boxes, img.size, mode="xywh").convert("xyxy")

        classes = [obj["category_id"]+1 for obj in anno]
        #print(classes,'old')
        classes = [self.json_category_id_to_contiguous_id[c] for c in classes]
        #print(classes,classes2)
        classes = torch.tensor(classes)
        target.add_field("labels", classes) #

        #masks = [obj["segmentation"] for obj in anno]
        #masks = SegmentationMask(masks, img.size, mode='poly')
        #target.add_field("masks", masks)

        #if anno and "keypoints" in anno[0]:
         #   keypoints = [obj["keypoints"] for obj in anno]
          #  keypoints = PersonKeypoints(keypoints, img.size)
           # target.add_field("keypoints", keypoints)

        target = target.clip_to_image(remove_empty=True)

        if self.transforms is not None:
            img, target = self.transforms(img, target)

        return img, target, idx 
Example #20
Source File: deepfashion2.py    From Clothing-Detection with GNU General Public License v3.0 5 votes vote down vote up
def __getitem__(self, idx):
        img, anno = super(DeepFashion2Dataset, self).__getitem__(idx)

        # filter crowd annotations
        # TODO might be better to add an extra field
        anno = [obj for obj in anno if obj["iscrowd"] == 0]

        boxes = [obj["bbox"] for obj in anno]
        boxes = torch.as_tensor(boxes).reshape(-1, 4)  # guard against no boxes
        target = BoxList(boxes, img.size, mode="xywh").convert("xyxy")

        classes = [obj["category_id"] for obj in anno]
        #print(classes)
        classes = [self.json_category_id_to_contiguous_id[c] for c in classes]
        classes = torch.tensor(classes)
        target.add_field("labels", classes)

        #masks = [obj["segmentation"] for obj in anno]
        #masks = SegmentationMask(masks, img.size, mode='poly')
        #target.add_field("masks", masks)

        #if anno and "keypoints" in anno[0]:
         #   keypoints = [obj["keypoints"] for obj in anno]
          #  keypoints = PersonKeypoints(keypoints, img.size)
           # target.add_field("keypoints", keypoints)

        target = target.clip_to_image(remove_empty=True)

        if self.transforms is not None:
            img, target = self.transforms(img, target)

        return img, target, idx 
Example #21
Source File: test_hook.py    From PySyft with Apache License 2.0 5 votes vote down vote up
def test_torch_func_signature_without_tensor():
    """The hook on the args of torch commands should work even if the args
    don't contain any tensor"""
    x = torch.as_tensor((0.1307,), dtype=torch.float32, device="cpu")
    assert (x == torch.tensor([0.1307])).all() 
Example #22
Source File: core.py    From spinningup with MIT License 5 votes vote down vote up
def __init__(self, obs_dim, act_dim, hidden_sizes, activation):
        super().__init__()
        log_std = -0.5 * np.ones(act_dim, dtype=np.float32)
        self.log_std = torch.nn.Parameter(torch.as_tensor(log_std))
        self.mu_net = mlp([obs_dim] + list(hidden_sizes) + [act_dim], activation) 
Example #23
Source File: ddpg.py    From spinningup with MIT License 5 votes vote down vote up
def sample_batch(self, batch_size=32):
        idxs = np.random.randint(0, self.size, size=batch_size)
        batch = dict(obs=self.obs_buf[idxs],
                     obs2=self.obs2_buf[idxs],
                     act=self.act_buf[idxs],
                     rew=self.rew_buf[idxs],
                     done=self.done_buf[idxs])
        return {k: torch.as_tensor(v, dtype=torch.float32) for k,v in batch.items()} 
Example #24
Source File: sac.py    From spinningup with MIT License 5 votes vote down vote up
def sample_batch(self, batch_size=32):
        idxs = np.random.randint(0, self.size, size=batch_size)
        batch = dict(obs=self.obs_buf[idxs],
                     obs2=self.obs2_buf[idxs],
                     act=self.act_buf[idxs],
                     rew=self.rew_buf[idxs],
                     done=self.done_buf[idxs])
        return {k: torch.as_tensor(v, dtype=torch.float32) for k,v in batch.items()} 
Example #25
Source File: td3.py    From spinningup with MIT License 5 votes vote down vote up
def sample_batch(self, batch_size=32):
        idxs = np.random.randint(0, self.size, size=batch_size)
        batch = dict(obs=self.obs_buf[idxs],
                     obs2=self.obs2_buf[idxs],
                     act=self.act_buf[idxs],
                     rew=self.rew_buf[idxs],
                     done=self.done_buf[idxs])
        return {k: torch.as_tensor(v, dtype=torch.float32) for k,v in batch.items()} 
Example #26
Source File: core.py    From spinningup with MIT License 5 votes vote down vote up
def __init__(self, obs_dim, act_dim, hidden_sizes, activation):
        super().__init__()
        log_std = -0.5 * np.ones(act_dim, dtype=np.float32)
        self.log_std = torch.nn.Parameter(torch.as_tensor(log_std))
        self.mu_net = mlp([obs_dim] + list(hidden_sizes) + [act_dim], activation) 
Example #27
Source File: ppo.py    From spinningup with MIT License 5 votes vote down vote up
def get(self):
        """
        Call this at the end of an epoch to get all of the data from
        the buffer, with advantages appropriately normalized (shifted to have
        mean zero and std one). Also, resets some pointers in the buffer.
        """
        assert self.ptr == self.max_size    # buffer has to be full before you can get
        self.ptr, self.path_start_idx = 0, 0
        # the next two lines implement the advantage normalization trick
        adv_mean, adv_std = mpi_statistics_scalar(self.adv_buf)
        self.adv_buf = (self.adv_buf - adv_mean) / adv_std
        data = dict(obs=self.obs_buf, act=self.act_buf, ret=self.ret_buf,
                    adv=self.adv_buf, logp=self.logp_buf)
        return {k: torch.as_tensor(v, dtype=torch.float32) for k,v in data.items()} 
Example #28
Source File: types.py    From chainer-compiler with MIT License 5 votes vote down vote up
def generate_dummy_value(ty) -> object:
    # creates dummy value

    ty = ty.deref()

    if isinstance(ty, TyNone):
        return None
    if isinstance(ty, TyNum):
        if ty.value is not None:
            return ty.value
        return eval(str(NumKind(ty.kind)))(1)  # XXX: use 1 to avoid division by zero
    if isinstance(ty, TyString):
        if ty.value is not None:
            return ty.value
        return ""
    if isinstance(ty, TyList):
        return [generate_dummy_value(ty.ty)]
    if isinstance(ty, TyTuple):
        if ty.is_fixed_len:
            return tuple([generate_dummy_value(t) for t in ty.get_tys()])
        return tuple([generate_dummy_value(ty.get_ty())])
    if isinstance(ty, TyDict):
        return { generate_dummy_value(ty.keyty) : generate_dummy_value(ty.valty) }
    if isinstance(ty, TyTensor):
        ret = np.zeros(dtype=ty.dtype, shape=unwrap_shape(ty.shape))
        if ty.is_ndarray():
            return ret
        if ty.is_chainer_variable():
            return chainer.Variable(ret)
        if ty.is_torch_tensor():
            return torch.as_tensor(ret)
    if isinstance(ty, TyDType):
        return ty.t
    if isinstance(ty, TyUserDefinedClass):
        # We don't need to copy the instance because it won't be overwritten
        return ty.instance

    assert False, "generate_dummy_value: type not understood: " + str(ty) 
Example #29
Source File: transforms.py    From torchbench with Apache License 2.0 5 votes vote down vote up
def __call__(self, image, target):

        height, width, channels = image.shape

        res = []

        if isinstance(target["annotation"]["object"], dict):
            target_item = [target["annotation"]["object"]]
        else:
            target_item = target["annotation"]["object"]

        for obj in target_item:
            difficult = obj["difficult"] == 1
            if not self.keep_difficult and difficult:
                continue
            name = obj["name"]
            bbox = obj["bndbox"]

            pts = ["xmin", "ymin", "xmax", "ymax"]
            bndbox = []
            for i, pt in enumerate(pts):
                cur_pt = int(bbox[pt]) - 1
                # scale height or width
                cur_pt = cur_pt / width if i % 2 == 0 else cur_pt / height
                bndbox.append(cur_pt)
            label_idx = self.class_to_ind[name]
            bndbox.append(label_idx)
            res += [bndbox]  # [xmin, ymin, xmax, ymax, label_ind]

        return (
            image,
            torch.as_tensor(np.array(res), dtype=torch.float32),
        )  # [[xmin, ymin, xmax, ymax, label_ind], ... ] 
Example #30
Source File: transforms.py    From torchbench with Apache License 2.0 5 votes vote down vote up
def convert_coco_poly_to_mask(segmentations, height, width):
    masks = []
    for polygons in segmentations:
        rles = coco_mask.frPyObjects(polygons, height, width)
        mask = coco_mask.decode(rles)
        if len(mask.shape) < 3:
            mask = mask[..., None]
        mask = torch.as_tensor(mask, dtype=torch.uint8)
        mask = mask.any(dim=2)
        masks.append(mask)
    if masks:
        masks = torch.stack(masks, dim=0)
    else:
        masks = torch.zeros((0, height, width), dtype=torch.uint8)
    return masks