def tf_occlude(image: tf.Tensor, mask: tf.Tensor, occlusion_size=50):
    """ TF Wrapper for numpy function
    Blanks out a cubic subvolume of the image tensor in each channel and each example in the batch

    Parameters
    ----------
    image : np.ndarray
        image tensor in xyzc format
    mask : np.ndarray
        mask tensor (unaltered)
    occlusion_size : int, optional
        side length of the cubic subvolume, by default 50

    Returns
    -------
    image, mask
        the processed image, mask pair
    """
    image_shape = image.shape
    mask_shape = mask.shape
    image, mask = tf.numpy_function(np_occlude,
                                    inp=[image, mask],
                                    Tout=(tf.float32, tf.int32))
    image.set_shape(image_shape)
    mask.set_shape(mask_shape)
    return image, mask
Exemple #2
0
    def call(self, x: tf.Tensor, padding=None) -> tf.Tensor:
        batch_size = tf.shape(x)[0]
        length = tf.shape(x)[1]

        if padding is not None:
            # removing pad
            pad_mask = tf.reshape(padding, [-1])
            nonpad_ids = tf.to_int32(tf.where(pad_mask < 1e-9))

            x = tf.reshape(x, [-1, self.hidden_size])
            x = tf.gather_nd(x, indices=nonpad_ids)

            x.set_shape([None, self.hidden_size])
            x = tf.expand_dims(x, axis=0)  # [1, batch_size x length, hidden_size]

        outputs = self.filter_dense_layer(x)
        if self.is_train:
            outputs = self.dropout_layer(outputs)
        outputs = self.output_dense_layer(outputs)

        if padding is not None:
            # re add padding
            outputs = tf.squeeze(outputs, axis=0)  # [batch_size x length, hidden_units]
            outputs = tf.scatter_nd(
                indices=nonpad_ids,
                updates=outputs,
                shape=[batch_size * length, self.hidden_size]
            )
            outputs = tf.reshape(outputs, [batch_size, length, self.hidden_size])

        return outputs
Exemple #3
0
def restore_shape(t0: tf.Tensor, t1: tf.Tensor):
    shape = t0.get_shape()
    if shape == tensor_shape.unknown_shape():
        t1.set_shape([None, None, None])
    else:
        t1.set_shape(shape)
    return t1
    def prepare_input_for_training(skip_gram_pairs_batch: tf.Tensor,
                                   sent_percentages: tf.Tensor):
        """
        Prepares the target/context skip-gram pairs for training. Also converts
        the list of percentages into a single percentage.

        Parameters
        ----------
        skip_gram_pairs_batch : tf.Tensor
            Tensor containing input target/context pairs
        sent_percentages : tf.Tensor
            Tensor containing percentages of training progress
        Returns
        -------
        training_data : tuple of tf.Tensor
            Tuple consisting of targets, contexts and training progress (percentage).
        """

        # Set shape of tf.Tensor and extract targets/contexts
        skip_gram_pairs_batch.set_shape([batch_size, 2])
        input_targets = skip_gram_pairs_batch[:, :1]
        input_contexts = skip_gram_pairs_batch[:, 1:]

        # Ensure that dimensions are correct
        input_targets = tf.squeeze(input_targets, axis=1)
        input_contexts = tf.squeeze(input_contexts, axis=1)

        # Return percentage as a single number
        sent_percentage = sent_percentages[0]
        sent_percentage = tf.cast(sent_percentage, "float32")

        # Combine into tuple
        training_data = (input_targets, input_contexts, sent_percentage)

        return training_data
Exemple #5
0
def set_shape_dim(tensor: tf.Tensor, index: int, size: int) -> None:
    """Set value of index-th element of tensor shape to size."""
    shape = tensor.get_shape().as_list()
    if len(shape) <= index:
        raise ValueError('Tensor rank must be at least %d. Got %d' %
                         (index + 1, len(shape)))
    shape[index] = size
    tensor.set_shape(shape)
def tf_affine(image: tf.Tensor, mask: tf.Tensor):
    image_shape = image.shape
    mask_shape = mask.shape
    image, mask = tf.numpy_function(affineTransformation,
                                    inp=[image, mask],
                                    Tout=(tf.float32, tf.int32))
    image.set_shape(image_shape)
    mask.set_shape(mask_shape)
    return image, mask
Exemple #7
0
def tf_random_elastic_deform(image: tf.Tensor, mask: tf.Tensor):
    image_shape = image.shape
    mask_shape = mask.shape
    image, mask = tf.numpy_function(random_elastic_deform,
                                    inp=[image, mask],
                                    Tout=(tf.float32, tf.int32))
    image.set_shape(image_shape)
    mask.set_shape(mask_shape)
    return image, mask
Exemple #8
0
 def tf_encode_wrapper(self, source: tf.Tensor,
                       target: tf.Tensor) -> Tuple[tf.Tensor, tf.Tensor]:
     shape = source.shape
     source = tf.py_function(self.encode_source, [source], tf.int32)
     source.set_shape(shape)
     shape = target.shape
     target = tf.py_function(self.encode_target, [target], tf.int32)
     target.set_shape(shape)
     return source, target
    def set_shape_function(self, lightcurve: tf.Tensor, label: tf.Tensor):
        """
        Explicitly sets the shapes of the lightcurve and label tensor, otherwise TensorFlow can't infer it.

        :param lightcurve: The lightcurve tensor.
        :param label: The label tensor.
        :return: The lightcurve and label tensor with TensorFlow inferable shapes.
        """
        lightcurve.set_shape([self.time_steps_per_example, 1])
        label.set_shape([1])
        return lightcurve, label
    def _split_sample_pairs(x: tf.Tensor, iter_progress: tf.Tensor) \
            -> Tuple[tf.Tensor, tf.Tensor, tf.Tensor]:
        """Split the sample pair tensors into
        (features, labels, iter_progress) tensor tuples.
        """
        # AutoGraph requires us to set the shape of tensors when
        # it can't automatically infer it from context.
        x.set_shape((batch_size, 2))

        features = tf.squeeze(x[:, :1], axis=1)
        labels = tf.squeeze(x[:, 1:], axis=1)
        iter_progress = tf.cast(iter_progress, tf.float32)
        return features, labels, iter_progress
Exemple #11
0
def imagenet_slim_preprocess_image(image: tf.Tensor,
                                   image_size: int = IMAGE_SIZE,
                                   is_training: bool = False,
                                   dtype: tf.dtypes.DType = tf.float32,
                                   **_) -> tf.Tensor:
    image = tf.image.central_crop(image, central_fraction=0.875)
    if is_training:
        image = tf.image.random_flip_left_right(image)

    image = utils.resize_image(image, image_size, image_size)
    image.set_shape([image_size, image_size, 3])
    image = image / 255

    return tf.image.convert_image_dtype(image, dtype=dtype)
    def __img_label_scaling(self, img: tf.Tensor,
                            label: tf.Tensor) -> Tuple[tf.Tensor, tf.Tensor]:
        """
        Scaling casting and set_shape calls which are needed in the chain of dataset map function calls.
        :param img: Image with values in [0,1]
        :param label: Label of the image
        :return: Tuple of (img, label), where img is in [-1,1] and label is a uint8 (Tiny ImageNet has 200 classes)
        """
        img = tf.multiply(2., img) - 1.  # float32 [-1., 1]
        img.set_shape([self.img_width, self.img_height, self.img_channels])

        label = tf.string_to_number(label, tf.int32)
        label = tf.cast(label, tf.uint8)
        return img, label
Exemple #13
0
  def inference_fn(cls,
                   image: tf.Tensor,
                   input_image_size: List[int],
                   num_channels: int = 3) -> tf.Tensor:
    """Builds image model inputs for serving."""

    image = tf.cast(image, dtype=tf.float32)
    image = preprocess_ops.center_crop_image(image)
    image = tf.image.resize(
        image, input_image_size, method=tf.image.ResizeMethod.BILINEAR)

    # Normalizes image with mean and std pixel values.
    image = preprocess_ops.normalize_image(
        image, offset=MEAN_RGB, scale=STDDEV_RGB)
    image.set_shape(input_image_size + [num_channels])
    return image
Exemple #14
0
def random_crop_resize(frames: tf.Tensor,
                       output_h: int,
                       output_w: int,
                       num_frames: int,
                       num_channels: int,
                       aspect_ratio: Tuple[float, float],
                       area_range: Tuple[float, float]) -> tf.Tensor:
  """First crops clip with jittering and then resizes to (output_h, output_w).

  Args:
    frames: A Tensor of dimension [timesteps, input_h, input_w, channels].
    output_h: Resized image height.
    output_w: Resized image width.
    num_frames: Number of input frames per clip.
    num_channels: Number of channels of the clip.
    aspect_ratio: Float tuple with the aspect range for cropping.
    area_range: Float tuple with the area range for cropping.
  Returns:
    A Tensor of shape [timesteps, output_h, output_w, channels] of type
      frames.dtype.
  """
  shape = tf.shape(frames)
  seq_len, _, _, channels = shape[0], shape[1], shape[2], shape[3]
  bbox = tf.constant([0.0, 0.0, 1.0, 1.0], dtype=tf.float32, shape=[1, 1, 4])
  factor = output_w / output_h
  aspect_ratio = (aspect_ratio[0] * factor, aspect_ratio[1] * factor)
  sample_distorted_bbox = tf.image.sample_distorted_bounding_box(
      shape[1:],
      bounding_boxes=bbox,
      min_object_covered=0.1,
      aspect_ratio_range=aspect_ratio,
      area_range=area_range,
      max_attempts=100,
      use_image_if_no_bounding_boxes=True)
  bbox_begin, bbox_size, _ = sample_distorted_bbox
  offset_y, offset_x, _ = tf.unstack(bbox_begin)
  target_height, target_width, _ = tf.unstack(bbox_size)
  size = tf.convert_to_tensor((
      seq_len, target_height, target_width, channels))
  offset = tf.convert_to_tensor((
      0, offset_y, offset_x, 0))
  frames = tf.slice(frames, offset, size)
  frames = tf.cast(
      tf.image.resize(frames, (output_h, output_w)),
      frames.dtype)
  frames.set_shape((num_frames, output_h, output_w, num_channels))
  return frames
Exemple #15
0
def set_tensor_shape(tensor: tf.Tensor, tensor_shape: Any) -> tf.Tensor:
    """
    Set shape for a tensor (not in place, as opposed to tf.set_shape)

    Parameters:
        tensor (tensorflow.Tensor):
            Tensor to reshape.
        tensor_shape (Any):
            Shape to apply to the tensor.

    Returns:
        tensorflow.Tensor:
            A reshaped tensor.
    """
    # NOTE: That SOUND LIKE IN PLACE HERE ?
    tensor.set_shape(tensor_shape)
    return tensor
Exemple #16
0
    def return_crf_result(self, labels: tf.Tensor, logits: tf.Tensor, mode: str, input_mask: tf.Tensor):
        input_mask.set_shape([None, None])
        logits = create_dummy_if_empty(logits)
        input_mask = create_dummy_if_empty(input_mask)
        viterbi_decoded, potentials, sequence_length, chain_kernel = self.crf(
            logits, input_mask)
        if mode != tf.estimator.ModeKeys.PREDICT:
            loss = -crf_log_likelihood(potentials,
                                       labels, sequence_length, chain_kernel)[0]
            loss = tf.reduce_mean(loss)
            loss = nan_loss_handling(loss)
            self.add_loss(loss)
            acc = self.metric_fn(
                labels, viterbi_decoded, sample_weight=input_mask)
            self.add_metric(acc)

        # make the crf prediction has the same shape as non-crf prediction
        return tf.one_hot(viterbi_decoded, name='%s_predict' % self.problem_name, depth=self.params.num_classes[self.problem_name])
Exemple #17
0
def padd_for_aligning_pixels(inputs: tf.Tensor):
    """This padding operation is here to make the pixels of the output perfectly aligned.
    It will make the output perfectly aligned at stride 32.
    """

    chan = inputs.shape[3]
    b4_stride = 32.0
    shape2d = tf.shape(inputs)[1:3]
    new_shape2d = tf.cast(
        tf.math.ceil(tf.cast(shape2d, tf.float32) / b4_stride) * b4_stride, tf.int32)
    pad_shape2d = new_shape2d - shape2d
    inputs = tf.pad(inputs,
                    tf.stack([[0, 0],
                              [3, 2 + pad_shape2d[0]],
                              [3, 2 + pad_shape2d[1]],
                              [0, 0]]),
                    name='conv1_pad') # yapf: disable
    inputs.set_shape([None, None, None, chan])
    return inputs
Exemple #18
0
 def _fixup_shape(self, f: tf.Tensor, lb: tf.Tensor):
     """requires further testing, this is for classification"""
     f.set_shape([None, None, f.shape[-1]])
     lb.set_shape(lb.shape)  # number of class
     return f, lb
Exemple #19
0
 def _fixup_shape_semisupervised(self, f: tf.Tensor, lb: tf.Tensor):
     """requires further testing, only for semisupervised for testing"""
     f.set_shape([None, None, f.shape[-1]])
     lb.set_shape([None, None, lb.shape[-1]])
     return f, lb
Exemple #20
0
 def decode_image(self, img: tf.Tensor) -> tf.Tensor:
   """Decode the jpeg or png bytes to 3d tensor."""
   img = tf.image.decode_image(img, channels=self.shape[-1], dtype=self.dtype)
   img.set_shape(self.shape)
   return img
Exemple #21
0
def tst_set_shape_foo(f: tf.Tensor, lb: tf.Tensor):
    f.set_shape(f.shape)
    lb.set_shape(lb.shape)
    return f, lb
Exemple #22
0
def set_shape(x: tf.Tensor, h: int, w: int, c: int = 3) -> tf.Tensor:
    x.set_shape([h, w, c])
    return x
 def _assert_shape(image: tf.Tensor) -> tf.Tensor:
     image.set_shape(output_shape)
     return image