jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def rpn_bbox_structural_loss_graph(roi_box_coordinate, roi_gt_coordinate,
                                   segments):
    #tf.InteractiveSession()
    segments = t.cast(segments, t.float32)
    roi_box_pairwise_diff = t.expand_dims(
        roi_box_coordinate, 1) - t.expand_dims(roi_box_coordinate, 2)
    roi_gt_box_pairwise_diff = t.expand_dims(
        roi_gt_coordinate, 1) - t.expand_dims(roi_gt_coordinate, 2)
    pairwise_equal_segments = t.equal(t.expand_dims(segments, 1),
                                       t.expand_dims(segments, 2))
    pairwise_equal_segments = t.cast(pairwise_equal_segments, t.float32)
    #print(roi_gt_box_pairwise_diff.shape)
    #print(pairwise_equal_segments.eval())
    pairwise_diff_square = t.square(
        (roi_gt_box_pairwise_diff * pairwise_equal_segments) -
        (roi_box_pairwise_diff * pairwise_equal_segments)) / 2
    sum_error_per_channel = t.math.reduce_sum(pairwise_diff_square,
                                               axis=[1, 2])
    count_non_zero_per_channel = t.cast(
        t.math.count_nonzero(pairwise_diff_square, axis=[1, 2]), t.float32)
    #print(sum_error_per_channel.shape)
    #print(count_non_zero_per_channel.shape)
    avg_loss = sum_error_per_channel / t.math.add(
        count_non_zero_per_channel, t.constant([0.000001], dtype=t.float32))
    #print(avg_loss.shape)
    total_loss = t.math.reduce_sum(avg_loss)
    #zero = t.constant(0, dtype=t.float32)
    #non_zero_gts = t.gather_nd(roi_gt_coordinate, t.where(t.not_equal(roi_gt_coordinate, zero)))
    #gt_equals = t.equal(t.expand_dims(non_zero_gts, 0), t.expand_dims(non_zero_gts, 1))
    #gt_equals = t.cast(gt_equals, t.float32)
    #gt_equals = t.matrix_set_diag(gt_equals, t.zeros(t.shape(gt_equals)[0]))
    #return t.math.reduce_sum(gt_equals)

    #return pairwise_equal_segments
    #return t.square((roi_gt_box_pairwise_diff * pairwise_equal_segments))
    return total_loss
//  [python]
def rpn_bbox_structural_loss_graph(roi_box_coordinate, roi_gt_coordinate,
                                   segments):
    #tf.InteractiveSession()
    segments = tf.cast(segments, tf.float32)
    roi_box_pairwise_diff = tf.expand_dims(
        roi_box_coordinate, 1) - tf.expand_dims(roi_box_coordinate, 2)
    roi_gt_box_pairwise_diff = tf.expand_dims(
        roi_gt_coordinate, 1) - tf.expand_dims(roi_gt_coordinate, 2)
    pairwise_equal_segments = tf.equal(tf.expand_dims(segments, 1),
                                       tf.expand_dims(segments, 2))
    pairwise_equal_segments = tf.cast(pairwise_equal_segments, tf.float32)
    #print(roi_gt_box_pairwise_diff.shape)
    #print(pairwise_equal_segments.eval())
    pairwise_diff_square = tf.square(
        (roi_gt_box_pairwise_diff * pairwise_equal_segments) -
        (roi_box_pairwise_diff * pairwise_equal_segments)) / 2
    sum_error_per_channel = tf.math.reduce_sum(pairwise_diff_square,
                                               axis=[1, 2])
    count_non_zero_per_channel = tf.cast(
        tf.math.count_nonzero(pairwise_diff_square, axis=[1, 2]), tf.float32)
    #print(sum_error_per_channel.shape)
    #print(count_non_zero_per_channel.shape)
    avg_loss = sum_error_per_channel / tf.math.add(
        count_non_zero_per_channel, tf.constant([0.000001], dtype=tf.float32))
    #print(avg_loss.shape)
    total_loss = tf.math.reduce_sum(avg_loss)
    #zero = tf.constant(0, dtype=tf.float32)
    #non_zero_gts = tf.gather_nd(roi_gt_coordinate, tf.where(tf.not_equal(roi_gt_coordinate, zero)))
    #gt_equals = tf.equal(tf.expand_dims(non_zero_gts, 0), tf.expand_dims(non_zero_gts, 1))
    #gt_equals = tf.cast(gt_equals, tf.float32)
    #gt_equals = tf.matrix_set_diag(gt_equals, tf.zeros(tf.shape(gt_equals)[0]))
    #return tf.math.reduce_sum(gt_equals)

    #return pairwise_equal_segments
    #return tf.square((roi_gt_box_pairwise_diff * pairwise_equal_segments))
    return total_loss
//  [python]
def mrcnn_mask_loss_graph(target_masks, target_class_ids, pred_masks):
    """Mask binary cross-entropy loss for the masks head.

    target_masks: [batch, num_rois, height, width].
        A float32 tensor of values 0 or 1. Uses zero padding to fill array.
    target_class_ids: [batch, num_rois]. Integer class IDs. Zero padded.
    pred_masks: [batch, proposals, height, width, num_classes] float32 tensor
                with values from 0 to 1.
    """
    # Reshape for simplicity. Merge first two dimensions into one.
    target_class_ids = K.reshape(target_class_ids, (-1, ))
    mask_shape = tf.shape(target_masks)
    target_masks = K.reshape(target_masks, (-1, mask_shape[2], mask_shape[3]))
    pred_shape = tf.shape(pred_masks)
    pred_masks = K.reshape(pred_masks,
                           (-1, pred_shape[2], pred_shape[3], pred_shape[4]))
    # Permute predicted masks to [N, num_classes, height, width]
    pred_masks = tf.transpose(pred_masks, [0, 3, 1, 2])

    # Only positive ROIs contribute to the loss. And only
    # the class specific mask of each ROI.
    positive_ix = tf.where(target_class_ids > 0)[:, 0]
    positive_class_ids = tf.to_int64(tf.gather(target_class_ids, positive_ix))
    indices = tf.stack([positive_ix, positive_class_ids], axis=1)

    # Gather the masks (predicted and true) that contribute to loss
    y_true = tf.gather(target_masks, positive_ix)
    y_pred = tf.gather_nd(pred_masks, indices)

    # Compute binary cross entropy. If no positive ROIs, then return 0.
    # shape: [batch, roi, num_classes]
    loss = K.switch(
        tf.size(y_true) > 0, K.binary_crossentropy(target=y_true,
                                                   output=y_pred),
        tf.constant(0.0))
    loss = K.mean(loss)
    return loss
//  [python]
def mrcnn_mask_loss_graph(target_masks, target_class_ids, pred_masks):
    """Mask binary cross-entropy loss for the masks head.

    target_masks: [batch, num_rois, height, width].
        A float32 tensor of values 0 or 1. Uses zero padding to fill array.
    target_class_ids: [batch, num_rois]. Integer class IDs. Zero padded.
    pred_masks: [batch, proposals, height, width, num_classes] float32 tensor
                with values from 0 to 1.
    """
    # Reshape for simplicity. Merge first two dimensions into one.
    target_class_ids = K.reshape(target_class_ids, (-1, ))
    mask_shape = tf.shape(target_masks)
    target_masks = K.reshape(target_masks, (-1, mask_shape[2], mask_shape[3]))
    pred_shape = tf.shape(pred_masks)
    pred_masks = K.reshape(pred_masks,
                           (-1, pred_shape[2], pred_shape[3], pred_shape[4]))
    # Permute predicted masks to [N, num_classes, height, width]
    pred_masks = tf.transpose(pred_masks, [0, 3, 1, 2])

    # Only positive ROIs contribute to the loss. And only
    # the class specific mask of each ROI.
    positive_ix = tf.where(target_class_ids > 0)[:, 0]
    positive_class_ids = tf.cast(tf.gather(target_class_ids, positive_ix),
                                 tf.int64)
    indices = tf.stack([positive_ix, positive_class_ids], axis=1)

    # Gather the masks (predicted and true) that contribute to loss
    y_true = tf.gather(target_masks, positive_ix)
    y_pred = tf.gather_nd(pred_masks, indices)

    # Compute binary cross entropy. If no positive ROIs, then return 0.
    # shape: [batch, roi, num_classes]
    loss = K.switch(
        tf.size(y_true) > 0, K.binary_crossentropy(target=y_true,
                                                   output=y_pred),
        tf.constant(0.0))
    loss = K.mean(loss)
    return loss
//  [python]
def mrcnn_class_loss_graph(target_class_ids, pred_class_logits,
                           active_class_ids):
    """Loss for the classifier head of Mask RCNN.
    target_class_ids: [batch, num_rois]. Integer class IDs. Uses zero
        padding to fill in the array.
    pred_class_logits: [batch, num_rois, num_classes]
    active_class_ids: [batch, num_classes]. Has a value of 1 for
        classes that are in the dataset of the image, and 0
        for classes that are not in the dataset.
    """
    # During model building, Keras calls this function with
    # target_class_ids of type float32. Unclear why. Cast it
    # to int to get around it.
    target_class_ids = tf.to_int64(target_class_ids)

    # Find predictions of classes that are not in the dataset.
    pred_class_ids = tf.argmax(input=pred_class_logits, axis=2)
    # TODO: Update this line to work with batch > 1. Right now it assumes all
    #       images in a batch have the same active_class_ids
    pred_active = tf.gather(active_class_ids[0], pred_class_ids)

    # Loss
    loss = tf.nn.sparse_softmax_cross_entropy_with_logits(
        labels=target_class_ids, logits=pred_class_logits)

    # Erase losses of predictions of classes that are not in the active
    # classes of the image.
    loss = loss * pred_active

    # Computer loss mean. Use only predictions that contribute
    # to the loss to get a correct mean.
    loss = tf.reduce_sum(input_tensor=loss) / tf.reduce_sum(input_tensor=pred_active)
    return loss
//  [python]
def mrcnn_class_loss_graph(target_class_ids, pred_class_logits,
                           active_class_ids):
    """Loss for the classifier head of Mask RCNN.
    target_class_ids: [batch, num_rois]. Integer class IDs. Uses zero
        padding to fill in the array.
    pred_class_logits: [batch, num_rois, num_classes]
    active_class_ids: [batch, num_classes]. Has a value of 1 for
        classes that are in the dataset of the image, and 0
        for classes that are not in the dataset.
    """
    # During model building, Keras calls this function with
    # target_class_ids of type float32. Unclear why. Cast it
    # to int to get around it.
    target_class_ids = tf.cast(target_class_ids, 'int64')

    # Find predictions of classes that are not in the dataset.
    pred_class_ids = tf.argmax(input=pred_class_logits, axis=2)
    # TODO: Update this line to work with batch > 1. Right now it assumes all
    #       images in a batch have the same active_class_ids
    pred_active = tf.gather(active_class_ids[0], pred_class_ids)

    # Loss
    loss = tf.nn.sparse_softmax_cross_entropy_with_logits(
        labels=target_class_ids, logits=pred_class_logits)

    # Erase losses of predictions of classes that are not in the active
    # classes of the image.
    loss = loss * pred_active

    # Computer loss mean. Use only predictions that contribute
    # to the loss to get a correct mean.
    loss = tf.reduce_sum(input_tensor=loss) / tf.reduce_sum(input_tensor=pred_active)
    return loss
//  [python]
def compile(self, learning_rate, momentum):
        """Gets the model ready for training. Adds losses, regularization, and
        metrics. Then calls the Keras compile() function.
        """
        # Optimizer object
        optimizer = keras.optimizers.SGD(
            lr=learning_rate,
            momentum=momentum,
            clipnorm=self.config.GRADIENT_CLIP_NORM)
        # Add Losses
        # First, clear previously set losses to avoid duplication
        self.keras_model._losses = []
        self.keras_model._per_input_losses = {}
        loss_names = [
            "rpn_class_loss", "rpn_bbox_loss", "mrcnn_class_loss",
            "mrcnn_bbox_loss", "mrcnn_mask_loss", "roi_alignment_loss",
            "row_adj_loss", "col_adj_loss"
        ]
        for name in loss_names:
            layer = self.keras_model.get_layer(name)
            if layer.output in self.keras_model.losses:
                continue
            loss = (tf.reduce_mean(layer.output) *
                    self.config.LOSS_WEIGHTS.get(name, 1.))
            self.keras_model.add_loss(loss)

        # Add L2 Regularization
        # Skip gamma and beta weights of batch normalization layers.
        reg_losses = [
            keras.regularizers.l2(self.config.WEIGHT_DECAY)(w) /
            tf.cast(tf.size(w), tf.float32)
            for w in self.keras_model.trainable_weights
            if 'gamma' not in w.name and 'beta' not in w.name
        ]
        self.keras_model.add_loss(tf.add_n(reg_losses))

        # Compile
        self.keras_model.compile(optimizer=optimizer,
                                 loss=[None] * len(self.keras_model.outputs))

        # Add metrics for losses
        for name in loss_names:
            if name in self.keras_model.metrics_names:
                continue
            layer = self.keras_model.get_layer(name)
            self.keras_model.metrics_names.append(name)
            loss = (tf.reduce_mean(layer.output) *
                    self.config.LOSS_WEIGHTS.get(name, 1.))
            self.keras_model.metrics_tensors.append(loss)
//  [python]
def mrcnn_bbox_loss_graph(target_bbox, target_class_ids, pred_bbox):
    """Loss for Mask R-CNN bounding box refinement.
    target_bbox: [batch, num_rois, (dy, dx, log(dh), log(dw))]
    target_class_ids: [batch, num_rois]. Integer class IDs.
    pred_bbox: [batch, num_rois, num_classes, (dy, dx, log(dh), log(dw))]
    """
    # Reshape to merge batch and roi dimensions for simplicity.
    target_class_ids = K.reshape(target_class_ids, (-1,))
    target_bbox = K.reshape(target_bbox, (-1, 4))
    pred_bbox = K.reshape(pred_bbox, (-1, K.int_shape(pred_bbox)[2], 4))

    # Only positive ROIs contribute to the loss. And only
    # the right class_id of each ROI. Get their indices.
    positive_roi_ix = tf.compat.v1.where(target_class_ids > 0)[:, 0]
    positive_roi_class_ids = tf.cast(
        tf.gather(target_class_ids, positive_roi_ix), tf.int64)
    indices = tf.stack([positive_roi_ix, positive_roi_class_ids], axis=1)

    # Gather the deltas (predicted and true) that contribute to loss
    target_bbox = tf.gather(target_bbox, positive_roi_ix)
    pred_bbox = tf.gather_nd(pred_bbox, indices)

    # Smooth-L1 Loss
    loss = K.switch(tf.size(input=target_bbox) > 0,
                    smooth_l1_loss(y_true=target_bbox, y_pred=pred_bbox),
                    tf.constant(0.0))
    loss = K.mean(loss)
    return loss
//  [python]
def trim_zeros_graph(boxes, name='trim_zeros'):
    """Often boxes are represented with matrices of shape [N, 4] and
    are padded with zeros. This removes zero boxes.
    boxes: [N, 4] matrix of boxes.
    non_zeros: [N] a 1D boolean mask identifying the rows to keep
    """
    non_zeros = tf.cast(tf.reduce_sum(input_tensor=tf.abs(boxes), axis=1), tf.bool)
    boxes = tf.boolean_mask(tensor=boxes, mask=non_zeros, name=name)
    return boxes, non_zeros
//  [python]
def test_cast(self):
        x_val = np.array([1.0, 2.0, -3.0, -4.0], dtype=np.float32).reshape((2, 2))
        def func(x):
            x_ = tf.cast(x, tf.int32)
            return tf.identity(x_, name=_TFOUTPUT)
        self._run_test_case(func, [_OUTPUT], {_INPUT: x_val})
//  [python]
def denorm_boxes_graph(boxes, shape):
    """Converts boxes from normalized coordinates to pixel coordinates.
    boxes: [..., (y1, x1, y2, x2)] in normalized coordinates
    shape: [..., (height, width)] in pixels
    Note: In pixel coordinates (y2, x2) is outside the box. But in normalized
    coordinates it's inside the box.
    Returns:
        [..., (y1, x1, y2, x2)] in pixel coordinates
    """
    h, w = tf.split(t.cast(shape, tf.float32), 2)
    scale = tf.concat([h, w, h, w], axis=-1) - tf.constant(1.0)
    shift = tf.constant([0., 0., 1., 1.])
    return t.cast(tf.round(tf.multiply(boxes, scale) + shift), tf.int32)
//  [python]
def denorm_boxes_graph(boxes, shape):
    """Converts boxes from normalized coordinates to pixel coordinates.
    boxes: [..., (y1, x1, y2, x2)] in normalized coordinates
    shape: [..., (height, width)] in pixels
    Note: In pixel coordinates (y2, x2) is outside the box. But in normalized
    coordinates it's inside the box.
    Returns:
        [..., (y1, x1, y2, x2)] in pixel coordinates
    """
    h, w = tf.split(tf.cast(shape, tf.float32), 2)
    scale = tf.concat([h, w, h, w], axis=-1) - tf.constant(1.0)
    shift = tf.constant([0., 0., 1., 1.])
    return tf.cast(tf.round(tf.multiply(boxes, scale) + shift), tf.int32)
//  [python]
def test_roll_neg_axis(self):
        def func(input_ids):
            shifted_input_ids = t.cast(input_ids, tf.int32)
            shifted_input_ids = tf.roll(shifted_input_ids, 1, axis=-1)
            return tf.identity(shifted_input_ids, name=_TFOUTPUT)
        x_val = np.array([[0, 1, 2, 3, 4, 5, 6, 7], [1, 2, 3, 4, 5, 6, 7, 8]], dtype=np.int64)
        self._run_test_case(func, [_OUTPUT], {_INPUT: x_val})
//  [python]
def test_roll_neg_axis(self):
        def func(input_ids):
            shifted_input_ids = tf.cast(input_ids, tf.int32)
            shifted_input_ids = tf.roll(shifted_input_ids, 1, axis=-1)
            return tf.identity(shifted_input_ids, name=_TFOUTPUT)
        x_val = np.array([[0, 1, 2, 3, 4, 5, 6, 7], [1, 2, 3, 4, 5, 6, 7, 8]], dtype=np.int64)
        self._run_test_case(func, [_OUTPUT], {_INPUT: x_val})
//  [python]
def test_cast_unit32(self):
        x_val = np.array([1, 2, 3, 4], dtype=np.uint32).reshape((2, 2))
        def func(x):
            x_ = tf.to_int64(x)
            return tf.identity(x_, name=_TFOUTPUT)
        self._run_test_case(func, [_OUTPUT], {_INPUT: x_val})
//  [python]
def test_cast_unit32(self):
        x_val = np.array([1, 2, 3, 4], dtype=np.uint32).reshape((2, 2))
        def func(x):
            x_ = tf.cast(x, tf.uint64)
            return tf.identity(x_, name=_TFOUTPUT)
        self._run_test_case(func, [_OUTPUT], {_INPUT: x_val})
//  [python]
def test_fft_ops(self):
        x_val = make_xval([3, 4]).astype(np.float32)
        def func1(x):
            xc = t.cast(x, tf.complex64)
            op_ = tf.signal.fft(xc)
            return tf.abs(op_, name=_TFOUTPUT)
        self._run_test_case(func1, [_OUTPUT], {_INPUT: x_val})
//  [python]
def test_fft_ops(self):
        x_val = make_xval([3, 4]).astype(np.float32)
        def func1(x):
            xc = tf.cast(x, tf.complex64)
            op_ = tf.signal.fft(xc)
            return tf.abs(op_, name=_TFOUTPUT)
        self._run_test_case(func1, [_OUTPUT], {_INPUT: x_val})
//  [python]
def mrcnn_mask_loss_graph(target_masks, target_class_ids, pred_masks):
    """Mask binary cross-entropy loss for the masks head.
    target_masks: [batch, num_rois, height, width].
        A float32 tensor of values 0 or 1. Uses zero padding to fill array.
    target_class_ids: [batch, num_rois]. Integer class IDs. Zero padded.
    pred_masks: [batch, proposals, height, width, num_classes] float32 tensor
                with values from 0 to 1.
    """
    # Reshape for simplicity. Merge first two dimensions into one.
    target_class_ids = K.reshape(target_class_ids, (-1,))
    mask_shape = tf.shape(input=target_masks)
    target_masks = K.reshape(target_masks, (-1, mask_shape[2], mask_shape[3]))
    pred_shape = tf.shape(input=pred_masks)
    pred_masks = K.reshape(pred_masks,
                           (-1, pred_shape[2], pred_shape[3], pred_shape[4]))
    # Permute predicted masks to [N, num_classes, height, width]
    pred_masks = tf.transpose(a=pred_masks, perm=[0, 3, 1, 2])

    # Only positive ROIs contribute to the loss. And only
    # the class specific mask of each ROI.
    positive_ix = tf.compat.v1.where(target_class_ids > 0)[:, 0]
    positive_class_ids = tf.cast(
        tf.gather(target_class_ids, positive_ix), tf.int64)
    indices = tf.stack([positive_ix, positive_class_ids], axis=1)

    # Gather the masks (predicted and true) that contribute to loss
    y_true = tf.gather(target_masks, positive_ix)
    y_pred = tf.gather_nd(pred_masks, indices)

    # Compute binary cross entropy. If no positive ROIs, then return 0.
    # shape: [batch, roi, num_classes]
    loss = K.switch(tf.size(input=y_true) > 0,
                    K.binary_crossentropy(target=y_true, output=y_pred),
                    tf.constant(0.0))
    loss = K.mean(loss)
    return loss
//  [python]
def compile(self):
        """Gets the model ready for training. Adds losses, regularization, and
        metrics. Then calls the Keras compile() function.
        """
        # Optimizer object
        optimizer = keras.optimizers.SGD(self.lr_scheduler(0), momentum = self.config.LEARNING_MOMENTUM, 
        clipnorm = self.config.GRADIENT_CLIP_NORM)
    
               
        # Add Losses
        loss_names = [
            "rpn_class_loss",  "rpn_bbox_loss",
            "mrcnn_class_loss", "mrcnn_bbox_loss", "mrcnn_mask_loss"]
        for name in loss_names:
            layer = self.keras_model.get_layer(name)
            if layer.output in self.keras_model.losses:
                continue
            loss = (
                tf.reduce_mean(input_tensor=layer.output, keepdims=True)
                * self.config.LOSS_WEIGHTS.get(name, 1.))
            self.keras_model.add_loss(loss)

        # Add L2 Regularization
        # Skip gamma and beta weights of batch normalization layers.
        reg_losses = [
            keras.regularizers.l2(self.config.WEIGHT_DECAY)(w) / tf.cast(tf.size(input=w), tf.float32)
            for w in self.keras_model.trainable_weights
            if 'gamma' not in w.name and 'beta' not in w.name]
        self.keras_model.add_loss(tf.add_n(reg_losses))

        # Compile
        self.keras_model.compile(
            optimizer=optimizer,
            loss=[None] * len(self.keras_model.outputs))

        # Add metrics for losses
        for name in loss_names:
            if name in self.keras_model.metrics_names:
                continue
            layer = self.keras_model.get_layer(name)
            self.keras_model.metrics_names.append(name)
            loss = (
                tf.reduce_mean(input_tensor=layer.output, keepdims=True)
                * self.config.LOSS_WEIGHTS.get(name, 1.))
            self.keras_model.add_metric(loss, name=name, aggregation='mean')
//  [python]
def test_bfloat(self):
        x_val = np.array([0, 1, 2], dtype=np.float32)
        y_val = np.array([3, 4, 5], dtype=np.float32)
        def func(x, y):
            x_ = tf.cast(x, tf.bfloat16)
            y_ = tf.cast(y, tf.bfloat16)
            s_ = tf.add(x_, y_)
            return tf.cast(s_, tf.float32, name=_TFOUTPUT)
        self._run_test_case(func, [_OUTPUT], {_INPUT: x_val, _INPUT1: y_val})
//  [python]
def convert_uint16_to_float(array, min_val, max_val):
  return t.cast(array, t.float32) / 65535. * (max_val - min_val) + min_val
//  [python]
def convert_uint16_to_float(array, min_val, max_val):
  return tf.cast(array, tf.float32) / 65535. * (max_val - min_val) + min_val
//  [python]
def get_resize_small_shape(original_size,
                           small_size):
  h, w = original_size
  ratio = (
      t.cast(small_size, t.float32) / t.cast(t.minimum(h, w), t.float32))
  h = t.cast(t.round(t.cast(h, t.float32) * ratio), t.int32)
  w = t.cast(t.round(t.cast(w, t.float32) * ratio), t.int32)
  return h, w
//  [python]
def get_resize_small_shape(original_size,
                           small_size):
  h, w = original_size
  ratio = (
      tf.cast(small_size, tf.float32) / tf.cast(tf.minimum(h, w), tf.float32))
  h = tf.cast(tf.round(tf.cast(h, tf.float32) * ratio), tf.int32)
  w = tf.cast(tf.round(tf.cast(w, tf.float32) * ratio), tf.int32)
  return h, w
//  [python]
def adjust_small_size(original_size,
                      small_size, max_size):
  """Computes the adjusted small size to ensure large side < max_size."""
  h, w = original_size
  min_original_size = t.cast(t.minimum(w, h), t.float32)
  max_original_size = t.cast(t.maximum(w, h), t.float32)
  if max_original_size / min_original_size * small_size > max_size:
    small_size = t.cast(t.floor(
        max_size * min_original_size / max_original_size), t.int32)
  return small_size
//  [python]
def adjust_small_size(original_size,
                      small_size, max_size):
  """Computes the adjusted small size to ensure large side < max_size."""
  h, w = original_size
  min_original_size = tf.cast(tf.minimum(w, h), tf.float32)
  max_original_size = tf.cast(tf.maximum(w, h), tf.float32)
  if max_original_size / min_original_size * small_size > max_size:
    small_size = tf.cast(tf.floor(
        max_size * min_original_size / max_original_size), tf.int32)
  return small_size
//  [python]
def _decompose(cls, i, seg):
    return t.cast(t.equal(seg, i), t.int32)
//  [python]
def _decompose(cls, i, seg):
    return tf.cast(tf.equal(seg, i), tf.int32)
//  [python]
def _apply(self, tensor, seed,
             constant_values):
    """Applies the strided crop operation to the video tensor."""
    num_frames = tf.shape(tensor)[0]
    num_crop_points = tf.to_int32(tf.math.ceil(num_frames / self.length))
    crop_point = tf.random.stateless_uniform(
        shape=(), minval=0, maxval=num_crop_points, dtype=tf.int32, seed=seed)
    crop_point *= self.length
    frames_sample = tensor[crop_point:crop_point + self.length]
    frames_to_pad = tf.maximum(self.length - tf.shape(frames_sample)[0], 0)
    frames_sample = tf.pad(
        frames_sample,
        ((0, frames_to_pad),) + ((0, 0),) * (len(frames_sample.shape) - 1),
        constant_values=constant_values)
    frames_sample = tf.ensure_shape(frames_sample, [self.length] +
                                    frames_sample.get_shape()[1:])
    return frames_sample
//  [python]
def _apply(self, tensor, seed,
             constant_values):
    """Applies the strided crop operation to the video tensor."""
    num_frames = tf.shape(tensor)[0]
    num_crop_points = tf.cast(tf.math.ceil(num_frames / self.length), tf.int32)
    crop_point = tf.random.stateless_uniform(
        shape=(), minval=0, maxval=num_crop_points, dtype=tf.int32, seed=seed)
    crop_point *= self.length
    frames_sample = tensor[crop_point:crop_point + self.length]
    frames_to_pad = tf.maximum(self.length - tf.shape(frames_sample)[0], 0)
    frames_sample = tf.pad(
        frames_sample,
        ((0, frames_to_pad),) + ((0, 0),) * (len(frames_sample.shape) - 1),
        constant_values=constant_values)
    frames_sample = tf.ensure_shape(frames_sample, [self.length] +
                                    frames_sample.get_shape()[1:])
    return frames_sample
//  [python]
def __call__(self, features):
    assert self.image_key in features

    features_new = {}
    for k, v in features.items():
      if k == self.image_key:
        features_new[self.video_key] = v[tf.newaxis]
      elif k in (self.padding_mask_key, self.boxes_key, self.segmentations_key,
                 self.sparse_segmentations_key):
        features_new[k] = v[tf.newaxis]
      elif k == self.original_size_key:
        pass  # See comment in the docstring of the class.
      else:
        features_new[k] = v

    if self.original_size_key in features:
      # The features come from an image preprocessing pipeline.
      shape = tf.concat([[1], features[self.original_size_key],
                         [features[self.image_key].shape[-1]]],  # pytype: disable=attribute-error  # allow-recursive-types
                        axis=0)
    elif self.shape_key in features:
      # The features come from a video preprocessing pipeline.
      shape = tf.concat([[1], features[self.shape_key]], axis=0)
    else:
      shape = tf.shape(features_new[self.video_key])
    features_new[self.shape_key] = shape

    if self.padding_mask_key not in features_new:
      features_new[self.padding_mask_key] = tf.cast(
          tf.ones_like(features_new[self.video_key])[Ellipsis, 0], tf.uint8)

    return features_new
//  [python]
def estimate_transformation(self, param, video_shape
                              ):
    """Computes the affine transformation for crop params.

    Args:
      param: Crop parameters in the [y, x, h, w] format of shape [4,].
      video_shape: Unused.

    Returns:
      Affine transformation of shape [3, 3] corresponding to cropping the image
      at [y, x] of size [h, w] and resizing it into [self.height, self.width].
    """
    del video_shape
    crop = t.cast(param, t.float32)
    si, sj = crop[0], crop[1]
    crop_h, crop_w = crop[2], crop[3]
    ei, ej = si + crop_h - 1.0, sj + crop_w - 1.0
    h, w = float(self.height), float(self.width)

    a1 = (ei - si + 1.)/h
    a2 = 0.
    a3 = si - 0.5 + a1 / 2.
    a4 = 0.
    a5 = (ej - sj + 1.)/w
    a6 = sj - 0.5 + a5 / 2.
    affine = t.stack([a1, a2, a3, a4, a5, a6, 0., 0., 1.])
    return t.reshape(affine, [3, 3])
//  [python]
def estimate_transformation(self, param, video_shape
                              ):
    """Computes the affine transformation for crop params.

    Args:
      param: Crop parameters in the [y, x, h, w] format of shape [4,].
      video_shape: Unused.

    Returns:
      Affine transformation of shape [3, 3] corresponding to cropping the image
      at [y, x] of size [h, w] and resizing it into [self.height, self.width].
    """
    del video_shape
    crop = tf.cast(param, tf.float32)
    si, sj = crop[0], crop[1]
    crop_h, crop_w = crop[2], crop[3]
    ei, ej = si + crop_h - 1.0, sj + crop_w - 1.0
    h, w = float(self.height), float(self.width)

    a1 = (ei - si + 1.)/h
    a2 = 0.
    a3 = si - 0.5 + a1 / 2.
    a4 = 0.
    a5 = (ej - sj + 1.)/w
    a6 = sj - 0.5 + a5 / 2.
    affine = tf.stack([a1, a2, a3, a4, a5, a6, 0., 0., 1.])
    return tf.reshape(affine, [3, 3])
//  [python]
def _compose_sort(cls, i, seg, sort, mask_s):
    return t.cast(t.equal(seg, sort[i]), t.int32) * i * mask_s[i]
//  [python]
def _compose_sort(cls, i, seg, sort, mask_s):
    return tf.cast(tf.equal(seg, sort[i]), tf.int32) * i * mask_s[i]
//  [python]
def crop_or_pad_boxes(boxes, top, left, height,
                      width, h_orig, w_orig):
  """Transforms the relative box coordinates according to the frame crop.

  Note that, if height/width are larger than h_orig/w_orig, this function
  implements the equivalent of padding.

  Args:
    boxes: Tensor of bounding boxes with shape (..., 4).
    top: Top of crop box in absolute pixel coordinates.
    left: Left of crop box in absolute pixel coordinates.
    height: Height of crop box in absolute pixel coordinates.
    width: Width of crop box in absolute pixel coordinates.
    h_orig: Original image height in absolute pixel coordinates.
    w_orig: Original image width in absolute pixel coordinates.
  Returns:
    Boxes tensor with same shape as input boxes but updated values.
  """
  # Video track bound boxes: [num_instances, num_tracks, 4]
  # Image bounding boxes: [num_instances, 4]
  assert boxes.shape[-1] == 4
  seq_len = tf.shape(boxes)[0]
  has_tracks = len(boxes.shape) == 3
  if has_tracks:
    num_tracks = boxes.shape[1]
  else:
    assert len(boxes.shape) == 2
    num_tracks = 1

  # Transform the box coordinates.
  a = t.cast(t.stack([h_orig, w_orig]), t.float32)
  b = t.cast(t.stack([top, left]), t.float32)
  c = t.cast(t.stack([height, width]), t.float32)
  boxes = tf.reshape(
      (tf.reshape(boxes, (seq_len, num_tracks, 2, 2)) * a - b) / c,
      (seq_len, num_tracks, len(NOTRACK_BOX)))

  # Filter the valid boxes.
  boxes = tf.minimum(tf.maximum(boxes, 0.0), 1.0)
  if has_tracks:
    cond = tf.reduce_all((boxes[:, :, 2:] - boxes[:, :, :2]) > 0.0, axis=-1)
    boxes = tf.where(cond[:, :, tf.newaxis], boxes, NOTRACK_BOX)
  else:
    boxes = tf.reshape(boxes, (seq_len, 4))

  return boxes
//  [python]
def crop_or_pad_boxes(boxes, top, left, height,
                      width, h_orig, w_orig):
  """Transforms the relative box coordinates according to the frame crop.

  Note that, if height/width are larger than h_orig/w_orig, this function
  implements the equivalent of padding.

  Args:
    boxes: Tensor of bounding boxes with shape (..., 4).
    top: Top of crop box in absolute pixel coordinates.
    left: Left of crop box in absolute pixel coordinates.
    height: Height of crop box in absolute pixel coordinates.
    width: Width of crop box in absolute pixel coordinates.
    h_orig: Original image height in absolute pixel coordinates.
    w_orig: Original image width in absolute pixel coordinates.
  Returns:
    Boxes tensor with same shape as input boxes but updated values.
  """
  # Video track bound boxes: [num_instances, num_tracks, 4]
  # Image bounding boxes: [num_instances, 4]
  assert boxes.shape[-1] == 4
  seq_len = tf.shape(boxes)[0]
  has_tracks = len(boxes.shape) == 3
  if has_tracks:
    num_tracks = boxes.shape[1]
  else:
    assert len(boxes.shape) == 2
    num_tracks = 1

  # Transform the box coordinates.
  a = tf.cast(tf.stack([h_orig, w_orig]), tf.float32)
  b = tf.cast(tf.stack([top, left]), tf.float32)
  c = tf.cast(tf.stack([height, width]), tf.float32)
  boxes = tf.reshape(
      (tf.reshape(boxes, (seq_len, num_tracks, 2, 2)) * a - b) / c,
      (seq_len, num_tracks, len(NOTRACK_BOX)))

  # Filter the valid boxes.
  boxes = tf.minimum(tf.maximum(boxes, 0.0), 1.0)
  if has_tracks:
    cond = tf.reduce_all((boxes[:, :, 2:] - boxes[:, :, :2]) > 0.0, axis=-1)
    boxes = tf.where(cond[:, :, tf.newaxis], boxes, NOTRACK_BOX)
  else:
    boxes = tf.reshape(boxes, (seq_len, 4))

  return boxes
//  [python]
def apply(self, tensor, key=None, video_shape=None):
    """See base class."""

    # Boxes are defined in normalized image coordinates and are not affected.
    if key == self.boxes_key:
      return tensor

    if key in (self.padding_mask_key, self.segmentations_key):
      tensor = tensor[Ellipsis, tf.newaxis]
    elif key == self.sparse_segmentations_key:
      tensor = tf.reshape(tensor,
                          (-1, tf.shape(tensor)[2], tf.shape(tensor)[3], 1))

    h, w = tf.shape(tensor)[1], tf.shape(tensor)[2]

    # Determine resize method based on dtype (e.g. segmentations are int).
    if tensor.dtype.is_integer:
      resize_method = "nearest"
    else:
      resize_method = "bilinear"

    # Clip size to max_size if needed.
    small_size = self.size
    if self.max_size is not None:
      small_size = adjust_small_size(
          original_size=(h, w), small_size=small_size, max_size=self.max_size)
    new_h, new_w = get_resize_small_shape(
        original_size=(h, w), small_size=small_size)
    tensor = tf.image.resize(tensor, [new_h, new_w], method=resize_method)

    # Flow needs to be rescaled according to the new size to stay valid.
    if key == self.flow_key:
      scale_h = tf.to_float(new_h) / tf.to_float(h)
      scale_w = tf.to_float(new_w) / tf.to_float(w)
      scale = tf.reshape(tf.stack([scale_h, scale_w], axis=0), (1, 2))
      # Optionally repeat scale in case both forward and backward flow are
      # stacked in the last dimension.
      scale = tf.repeat(scale, tf.shape(tensor)[-1] // 2, axis=0)
      scale = tf.reshape(scale, (1, 1, 1, tf.shape(tensor)[-1]))
      tensor *= scale

    if key in (self.padding_mask_key, self.segmentations_key):
      tensor = tensor[Ellipsis, 0]
    elif key == self.sparse_segmentations_key:
      tensor = tf.reshape(tensor, (video_shape[0], -1, new_h, new_w))

    return tensor
//  [python]
def apply(self, tensor, key=None, video_shape=None):
    """See base class."""

    # Boxes are defined in normalized image coordinates and are not affected.
    if key == self.boxes_key:
      return tensor

    if key in (self.padding_mask_key, self.segmentations_key):
      tensor = tensor[Ellipsis, tf.newaxis]
    elif key == self.sparse_segmentations_key:
      tensor = tf.reshape(tensor,
                          (-1, tf.shape(tensor)[2], tf.shape(tensor)[3], 1))

    h, w = tf.shape(tensor)[1], tf.shape(tensor)[2]

    # Determine resize method based on dtype (e.g. segmentations are int).
    if tensor.dtype.is_integer:
      resize_method = "nearest"
    else:
      resize_method = "bilinear"

    # Clip size to max_size if needed.
    small_size = self.size
    if self.max_size is not None:
      small_size = adjust_small_size(
          original_size=(h, w), small_size=small_size, max_size=self.max_size)
    new_h, new_w = get_resize_small_shape(
        original_size=(h, w), small_size=small_size)
    tensor = tf.image.resize(tensor, [new_h, new_w], method=resize_method)

    # Flow needs to be rescaled according to the new size to stay valid.
    if key == self.flow_key:
      scale_h = tf.cast(new_h, tf.float32) / tf.cast(h, tf.float32)
      scale_w = tf.cast(new_w, tf.float32) / tf.cast(w, tf.float32)
      scale = tf.reshape(tf.stack([scale_h, scale_w], axis=0), (1, 2))
      # Optionally repeat scale in case both forward and backward flow are
      # stacked in the last dimension.
      scale = tf.repeat(scale, tf.shape(tensor)[-1] // 2, axis=0)
      scale = tf.reshape(scale, (1, 1, 1, tf.shape(tensor)[-1]))
      tensor *= scale

    if key in (self.padding_mask_key, self.segmentations_key):
      tensor = tensor[Ellipsis, 0]
    elif key == self.sparse_segmentations_key:
      tensor = tf.reshape(tensor, (video_shape[0], -1, new_h, new_w))

    return tensor
//  [python]
def __call__(self, features):

    features_new = {}

    for key in features.keys():

      if key == SEGMENTATIONS:
        seg = features[key]
        size = tf.shape(seg)

        assert_op = tf.Assert(
            tf.equal(size[0], 1), ["Implemented only for a single frame."])

        with tf.control_dependencies([assert_op]):
          # Delete time dimension.
          seg = seg[0]

          # Get the minimum number of pixels a masks needs to have.
          max_pixels = size[1] * size[2]
          threshold_pixels = tf.to_int32(
              tf.to_float(max_pixels) * self.threshold)

          # Decompose the segmentation map as a single image for each instance.
          dec_seg = tf.stack(
              tf.map_fn(functools.partial(self._decompose, seg=seg),
                        tf.range(self.max_instances)), axis=0)

          # Count the pixels and find segmentation masks that are big enough.
          sums = tf.reduce_sum(dec_seg, axis=(1, 2))
          # We want the background to always be slot zero.
          # We can accomplish that be pretending it has the maximum
          # number of pixels.
          sums = tf.concat(
              [tf.ones_like(sums[0: 1]) * max_pixels, sums[1:]],
              axis=0)

          sort = tf.argsort(sums, axis=0, direction="DESCENDING")
          sums_s = tf.gather(sums, sort, axis=0)
          mask_s = tf.to_int32(tf.greater_equal(sums_s, threshold_pixels))

          dec_seg_plus = tf.stack(
              tf.map_fn(functools.partial(
                  self._compose_sort, seg=seg, sort=sort, mask_s=mask_s),
                        tf.range(self.max_instances_after)), axis=0)
          new_seg = tf.reduce_sum(dec_seg_plus, axis=0)

          features_new[key] = tf.to_int32(new_seg[None])

      else:
        # keep all other features
        features_new[key] = features[key]

    return features_new
//  [python]
def __call__(self, features):

    features_new = {}

    for key in features.keys():

      if key == SEGMENTATIONS:
        seg = features[key]
        size = tf.shape(seg)

        assert_op = tf.Assert(
            tf.equal(size[0], 1), ["Implemented only for a single frame."])

        with tf.control_dependencies([assert_op]):
          # Delete time dimension.
          seg = seg[0]

          # Get the minimum number of pixels a masks needs to have.
          max_pixels = size[1] * size[2]
          threshold_pixels = tf.cast(
              tf.cast(max_pixels, tf.float32) * self.threshold, tf.int32)

          # Decompose the segmentation map as a single image for each instance.
          dec_seg = tf.stack(
              tf.map_fn(functools.partial(self._decompose, seg=seg),
                        tf.range(self.max_instances)), axis=0)

          # Count the pixels and find segmentation masks that are big enough.
          sums = tf.reduce_sum(dec_seg, axis=(1, 2))
          # We want the background to always be slot zero.
          # We can accomplish that be pretending it has the maximum
          # number of pixels.
          sums = tf.concat(
              [tf.ones_like(sums[0: 1]) * max_pixels, sums[1:]],
              axis=0)

          sort = tf.argsort(sums, axis=0, direction="DESCENDING")
          sums_s = tf.gather(sums, sort, axis=0)
          mask_s = tf.cast(tf.greater_equal(sums_s, threshold_pixels), tf.int32)

          dec_seg_plus = tf.stack(
              tf.map_fn(functools.partial(
                  self._compose_sort, seg=seg, sort=sort, mask_s=mask_s),
                        tf.range(self.max_instances_after)), axis=0)
          new_seg = tf.reduce_sum(dec_seg_plus, axis=0)

          features_new[key] = tf.cast(new_seg[None], tf.int32)

      else:
        # keep all other features
        features_new[key] = features[key]

    return features_new
//  [python]
def __call__(self, features):
    if self.video_key in features and not self.force_overwrite:
      return features

    features_new = {}
    for k, v in features.items():
      if k == self.image_key:
        features_new[self.video_key] = v[tf.newaxis]
      elif k == self.image_segmentations_key:
        features_new[self.video_segmentations_key] = v[tf.newaxis]
      elif k == self.image_depths_key:
        features_new[self.depths_key] = v[tf.newaxis]
      else:
        features_new[k] = v

    if self.video_padding_mask_key not in features_new:
      logging.warning("Adding default video_padding_mask")
      features_new[self.video_padding_mask_key] = t.cast(
          t.ones_like(features_new[self.video_key])[Ellipsis, 0], t.uint8)

    return features_new
//  [python]
def __call__(self, features):
    if self.video_key in features and not self.force_overwrite:
      return features

    features_new = {}
    for k, v in features.items():
      if k == self.image_key:
        features_new[self.video_key] = v[tf.newaxis]
      elif k == self.image_segmentations_key:
        features_new[self.video_segmentations_key] = v[tf.newaxis]
      elif k == self.image_depths_key:
        features_new[self.depths_key] = v[tf.newaxis]
      else:
        features_new[k] = v

    if self.video_padding_mask_key not in features_new:
      logging.warning("Adding default video_padding_mask")
      features_new[self.video_padding_mask_key] = tf.cast(
          tf.ones_like(features_new[self.video_key])[Ellipsis, 0], tf.uint8)

    return features_new
//  [python]
def residual(self, points: TensorType) -> TensorType:
        """
        Calculate the residuals between the constraint function and its lower/upper limits.

        :param points: The points to calculate the residuals for, with shape [..., D].
        :return: A tensor containing the lower and upper residual values with shape [..., 2].
        """
        tf.debugging.assert_rank_at_least(points, 2)
        non_d_axes = np.ones_like(points.shape)[:-1]  # Avoid adding axes shape to static graph.
        lb = t.cast(t.reshape(self.lb, (*non_d_axes, -1)), dtype=points.dtype)
        ub = t.cast(t.reshape(self.ub, (*non_d_axes, -1)), dtype=points.dtype)
        fval = self.fun(points)
        fval = t.reshape(fval, (*points.shape[:-1], -1))  # Atleast 2D.
        fval = t.cast(fval, dtype=points.dtype)
        values = [fval - lb, ub - fval]
        values = t.concat(values, axis=-1)
        return values
//  [python]
def residual(self, points: TensorType) -> TensorType:
        """
        Calculate the residuals between the constraint function and its lower/upper limits.

        :param points: The points to calculate the residuals for, with shape [..., D].
        :return: A tensor containing the lower and upper residual values with shape [..., 2].
        """
        tf.debugging.assert_rank_at_least(points, 2)
        non_d_axes = np.ones_like(points.shape)[:-1]  # Avoid adding axes shape to static graph.
        lb = tf.cast(tf.reshape(self.lb, (*non_d_axes, -1)), dtype=points.dtype)
        ub = tf.cast(tf.reshape(self.ub, (*non_d_axes, -1)), dtype=points.dtype)
        fval = self.fun(points)
        fval = tf.reshape(fval, (*points.shape[:-1], -1))  # Atleast 2D.
        fval = tf.cast(fval, dtype=points.dtype)
        values = [fval - lb, ub - fval]
        values = tf.concat(values, axis=-1)
        return values
//  [python]
def residual(self, points: TensorType) -> TensorType:
        """
        Calculate the residuals between the constraint function and its lower/upper limits.

        :param points: The points to calculate the residuals for, with shape [..., D].
        :return: A tensor containing the lower and upper residual values with shape [..., M*2].
        """
        tf.debugging.assert_rank_at_least(points, 2)
        non_d_axes = np.ones_like(points.shape)[:-1]  # Avoid adding axes shape to static graph.
        lb = tf.reshape(tf.cast(self.lb, points.dtype), (*non_d_axes, -1))
        ub = tf.reshape(tf.cast(self.ub, points.dtype), (*non_d_axes, -1))
        A = tf.cast(self.A, points.dtype)
        fval = tf.linalg.matmul(points, A, transpose_b=True)
        fval = tf.reshape(fval, (*points.shape[:-1], -1))  # Atleast 2D.
        values = [fval - lb, ub - fval]
        values = tf.concat(values, axis=-1)
        return values
//  [python]
def residual(self, points: TensorType) -> TensorType:
        """
        Calculate the residuals between the constraint function and its lower/upper limits.

        :param points: The points to calculate the residuals for, with shape [..., D].
        :return: A tensor containing the lower and upper residual values with shape [..., M*2].
        """
        tf.debugging.assert_rank_at_least(points, 2)
        non_d_axes = np.ones_like(points.shape)[:-1]  # Avoid adding axes shape to static graph.
        lb = tf.cast(tf.reshape(self.lb, (*non_d_axes, -1)), dtype=points.dtype)
        ub = tf.cast(tf.reshape(self.ub, (*non_d_axes, -1)), dtype=points.dtype)
        A = tf.cast(self.A, dtype=points.dtype)
        fval = tf.linalg.matmul(points, A, transpose_b=True)
        fval = tf.reshape(fval, (*points.shape[:-1], -1))  # Atleast 2D.
        values = [fval - lb, ub - fval]
        values = tf.concat(values, axis=-1)
        return values
//  [python]
def is_feasible(self, points: TensorType) -> TensorType:
        """
        Checks if points satisfy the explicit constraints of this :class:`SearchSpace`.
        Note membership of the search space is not checked.

        :param points: The points to check constraints feasibility for, with shape [..., D].
        :return: A tensor of booleans. Returns `True` for each point if it is feasible in this
            search space, else `False`.
        :raise NotImplementedError: If this :class:`SearchSpace` has constraints.
        """
        # Everything is feasible in the absence of constraints. Must be overriden if there are
        # constraints.
        if self.has_constraints:
            raise NotImplementedError("Feasibility check is not implemented for this search space.")
        return t.cast(t.ones(points.shape[:-1]), dtype=bool)
//  [python]
def is_feasible(self, points: TensorType) -> TensorType:
        """
        Checks if points satisfy the explicit constraints of this :class:`SearchSpace`.
        Note membership of the search space is not checked.

        :param points: The points to check constraints feasibility for, with shape [..., D].
        :return: A tensor of booleans. Returns `True` for each point if it is feasible in this
            search space, else `False`.
        :raise NotImplementedError: If this :class:`SearchSpace` has constraints.
        """
        # Everything is feasible in the absence of constraints. Must be overriden if there are
        # constraints.
        if self.has_constraints:
            raise NotImplementedError("Feasibility check is not implemented for this search space.")
        return tf.cast(tf.ones(points.shape[:-1]), dtype=bool)