boxes.box_wrapper module

class boxes.box_wrapper.BoxTensor(data: torch.Tensor)

Bases: object

A wrapper to which contains single tensor which represents single or multiple boxes.

Have to use composition instead of inheritance because it is not safe to interit from torch.Tensor because creating an instance of such a class will always make it a leaf node. This works for torch.nn.Parameter but won’t work for a general box_tensor.

property Z

Top right coordinate as Tensor

classmethod cat(tensors: Tuple[TBoxTensor, …]) → TBoxTensor
property centre

Centre coordinate as Tensor

clamp_volume() → torch.Tensor

Volume of boxes. Returns 0 where boxes are flipped.

Returns

Tensor of shape (, ) when self has shape (, 2, num_dims)

contains_violations(other: TBoxTensor, margin: float = 0, per_dim_op='max', accross_dim_op='max')

When self is supposed to contain other

dimension_wise_clamp_volume() → torch.Tensor

Returns clamp volume per dimension

Returns

Tensor of shape(, num_dims) when self has shape (,2,num_dims)

dimension_wise_intersection_log_soft_volume(other: TBoxTensor, temp: float = 1.0, scale: Union[float, torch.Tensor] = 1.0) → torch.Tensor

Computes the log soft volume of the intersection intervals

Returns

Tensor of shape(,num_dims) when self and other have shape (, 2, num_dims)

dimension_wise_intersection_soft_volume(other: TBoxTensor, temp: float = 1.0, scale: Union[float, torch.Tensor] = 1.0) → torch.Tensor

Computes the soft volume of the intersection intervals

Returns

Tensor of shape(,num_dims) when self and other have shape (, 2, num_dims)

dimension_wise_log_conditional_prob(on_box: TBoxTensor, temp: float = 1.0, scale: Union[float, torch.Tensor] = 1.0) → Tuple[torch.Tensor, torch.Tensor]

Gives P(b1|b2=1) ie two values, one for b1=1 and other for b1=0

Returns

Tuple of tensors of shape (**, num_dims). First tensor is log_p and the second is log(1-p)

dimension_wise_log_soft_volume(temp: float = 1.0, scale: Union[float, torch.Tensor] = 1.0) → torch.Tensor
dimension_wise_soft_volume(temp: float = 1.0, scale: Union[float, torch.Tensor] = 1.0) → torch.Tensor

Volume of intervals. Uses softplus instead of ReLU/clamp

Returns

Tensor of shape (, num_dims) when self has shape (, 2, num_dims)

does_not_contain_violations(other: TBoxTensor, margin: float = 0, per_dim_op='min', accross_dim_op='min')

When self is not supposed to contain other

classmethod from_split(t: torch.Tensor, dim: int = - 1) → TBoxTensor

Creates a BoxTensor by splitting on the dimension dim at midpoint

Parameters
  • t – input

  • dim – dimension to split on

Returns

output BoxTensor

Return type

BoxTensor

Raises

ValueErrordim has to be even

classmethod from_zZ(z: torch.Tensor, Z: torch.Tensor) → TBoxTensor

Creates a box by stacking z and Z along -2 dim. That is if z.shape == Z.shape == (, num_dim), then the result would be box of shape (, 2, num_dim)

get(indices: torch.LongTensor, dim: int = 0) → TBoxTensor

Get boxes at particular indices on a particular dimension.

Shape of indices should be according to the shape of BoxTensor. For instance, if shape of BoxTensor is (3,4,2,5), then shape of indice should be (,)

intersection(other: TBoxTensor) → TBoxTensor

Gives intersection of self and other.

Note

This function can give fipped boxes, i.e. where z[i] > Z[i]

intersection_log_soft_volume(other: TBoxTensor, temp: float = 1.0, scale: Union[float, torch.Tensor] = 1.0) → torch.Tensor
intersection_soft_volume(other: TBoxTensor, temp: float = 1.0, scale: Union[float, torch.Tensor] = 1.0) → torch.Tensor

Computes the soft volume of the intersection box

Returns

Tensor of shape(,) when self and other have shape (, 2, num_dims)

join(other: TBoxTensor) → TBoxTensor

Gives join

log_clamp_volume() → torch.Tensor
log_conditional_prob(on_box: TBoxTensor, temp: float = 1.0, scale: Union[float, torch.Tensor] = 1.0) → Tuple[torch.Tensor, torch.Tensor]
log_soft_volume(temp: float = 1.0, scale: Union[float, torch.Tensor] = 1.0) → torch.Tensor
scaled_box(ref_box: TBoxTensor)boxes.box_wrapper.BoxTensor
soft_volume(temp: float = 1.0, scale: Union[float, torch.Tensor] = 1.0) → torch.Tensor

Volume of boxes. Uses softplus instead of ReLU/clamp

Returns

Tensor of shape (, ) when self has shape (, 2, num_dims)

property z

Lower left coordinate as Tensor

class boxes.box_wrapper.DeltaBoxTensor(data: torch.Tensor)

Bases: boxes.box_wrapper.SigmoidBoxTensor

Same as BoxTensor but with a different parameterization: (**,wW, num_dims)

z = w Z = z + delta(which is always positive)

property Z

Top right coordinate as Tensor

classmethod from_zZ(z: torch.Tensor, Z: torch.Tensor) → TBoxTensor

This method is blocked for now

classmethod get_wW(z, Z)
property z

Lower left coordinate as Tensor

class boxes.box_wrapper.MinDeltaBoxesOnTorus(data: torch.Tensor)

Bases: boxes.box_wrapper.SigmoidBoxTensor

property Z

Top right coordinate as Tensor

per_dim_int_length(boxes: torch.Tensor, _scaling_factor: torch.Tensor = tensor([1.0])) → torch.Tensor
Parameters
  • boxes – Tensor(…, intersect_axis, min/delta, embedding_dim)

  • _scaling_factor – Used for recursion, will be set internally

Returns

Tensor(…, embedding_dim) of intersection length in each dimension

property z

Lower left coordinate as Tensor

class boxes.box_wrapper.SigmoidBoxTensor(data: torch.Tensor)

Bases: boxes.box_wrapper.BoxTensor

Same as BoxTensor but with a different parameterization: (**,wW, num_dims)

z = sigmoid(w) Z = z + sigmoid(W) * (1-z)

w = inv_sigmoid(z) W = inv_sigmoid((Z - z)/(1-z))

property Z

Top right coordinate as Tensor

classmethod from_split(t: torch.Tensor, dim: int = - 1) → TBoxTensor

Creates a BoxTensor by splitting on the dimension dim at midpoint

Parameters
  • t – input

  • dim – dimension to split on

Returns

output BoxTensor

Return type

BoxTensor

Raises

ValueErrordim has to be even

classmethod from_zZ(z: torch.Tensor, Z: torch.Tensor) → TBoxTensor

This method is blocked for now

classmethod get_wW(z, Z)
intersection(other: TBoxTensor) → TBoxTensor

Gives intersection of self and other.

Note

This function can give fipped boxes, i.e. where z[i] > Z[i]

property z

Lower left coordinate as Tensor

class boxes.box_wrapper.TanhActivatedBoxTensor(data: torch.Tensor)

Bases: boxes.box_wrapper.BoxTensor

Same as BoxTensor but with a parameterization which is assumed to be the output from an activation function.

Supported activations:

  1. tanh

let (, num_dims) be the shape of output of the activations, then the BoxTensor is created with shape (, zZ, num_dims/2)

For tanh:

z = (w + 1)/2

Z = z + ((W + 1)/2) * (1-z) => To avoid zero volume boxes z should not be equal to Z=> w should be in [-1., 1.) => Also, W cannot be -1 => W should be in (-1, 1]

where w and W are outputs of tanh and hence are in (-1, 1)

=> 0 < z < 1

=> z < Z < 1

w = 2z -1 W = 2(Z - z)/(1-z) -1

property Z

Top right coordinate as Tensor

classmethod from_split(t: torch.Tensor, dim: int = - 1) → TBoxTensor

Creates a BoxTensor by splitting on the dimension dim at midpoint

Parameters
  • t – input

  • dim – dimension to split on

Returns

output BoxTensor

Return type

BoxTensor

Raises

ValueErrordim has to be even

classmethod from_zZ(z: torch.Tensor, Z: torch.Tensor) → TBoxTensor

Creates a box by stacking z and Z along -2 dim. That is if z.shape == Z.shape == (, num_dim), then the result would be box of shape (, 2, num_dim)

intersection(other: TBoxTensor) → TBoxTensor

Gives intersection of self and other.

Note

This function can give fipped boxes, i.e. where z[i] > Z[i]

classmethod w2z(w: torch.Tensor) → torch.Tensor
property z

Lower left coordinate as Tensor

class boxes.box_wrapper.TanhActivatedCenterSideBoxTensor(data: torch.Tensor)

Bases: boxes.box_wrapper.TanhActivatedBoxTensor

Same as BoxTensor but with a parameterization which is assumed to be the output from tanh activation. Differs from TanhActivatedBoxTensor() in the way it parameterizes the boxes

let (, num_dims) be the shape of output of the activations, then the BoxTensor is created with shape (, zZ, num_dims/2)

c = (w + 1)/2 => c in (0,1)

l = (W + 1)/2 => l in (0,1)

z = sigmoid(c - l) Z = sigmoid(c + l)

property Z

Top right coordinate as Tensor

classmethod from_zZ(z: torch.Tensor, Z: torch.Tensor) → TBoxTensor

Creates a box by stacking z and Z along -2 dim. That is if z.shape == Z.shape == (, num_dim), then the result would be box of shape (, 2, num_dim)

classmethod w2z(w: torch.Tensor) → torch.Tensor
property z

Lower left coordinate as Tensor

class boxes.box_wrapper.TanhActivatedMinMaxBoxTensor(data: torch.Tensor)

Bases: boxes.box_wrapper.TanhActivatedBoxTensor

Same as TanhActivatedBoxTensor as in it assumes input from a tanh but different in how it uses this input to create boxes.

z = min((1+w)/2 , (1+W)/2) Z = max((1+w)/2, (1+W)/2)

property Z

Top right coordinate as Tensor

classmethod from_zZ(z: torch.Tensor, Z: torch.Tensor) → TBoxTensor

Creates a box by stacking z and Z along -2 dim. That is if z.shape == Z.shape == (, num_dim), then the result would be box of shape (, 2, num_dim)

property z

Lower left coordinate as Tensor

boxes.box_wrapper.inv_sigmoid(v: torch.Tensor) → torch.Tensor