Python tensorflow.compat.v1.convert_to_tensor() Examples

The following are 30 code examples of tensorflow.compat.v1.convert_to_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 tensorflow.compat.v1 , or try the search function .
Example #1
Source File: specgrams_helper_test.py    From magenta with Apache License 2.0 6 votes vote down vote up
def testShapesAndReconstructions(self, transform_name, target_channels):
    # Transform the data, test shape
    transform = self.transform_pairs[transform_name][0]
    target_shape = tuple([self.batch_size]
                         + self.spec_shape + [target_channels])
    with self.cached_session() as sess:
      spectra_np = sess.run(transform(self.audio))
    self.assertEqual(spectra_np.shape, target_shape)

    # Reconstruct the audio, test shape
    inv_transform = self.transform_pairs[transform_name][1]
    with self.cached_session() as sess:
      recon_np = sess.run(inv_transform(tf.convert_to_tensor(spectra_np)))
    self.assertEqual(recon_np.shape, (self.batch_size, self.audio_length, 1))

    # Test reconstruction error
    # Mel compression adds differences so skip
    if transform_name != 'melspecgrams':
      # Edges have known differences due to windowing
      edge = self.spec_shape[1] * 2
      diff = np.abs(self.audio_np[:, edge:-edge] - recon_np[:, edge:-edge])
      rms = np.mean(diff**2.0)**0.5
      print(transform_name, 'RMS:', rms)
      self.assertLessEqual(rms, 1e-5) 
Example #2
Source File: common_attention_test.py    From tensor2tensor with Apache License 2.0 6 votes vote down vote up
def testRightShiftBlockwiseND(self):
    tensor = tf.convert_to_tensor(np.array([[
        [[1], [2], [3], [4]],
        [[5], [6], [7], [8]],
        [[9], [10], [11], [12]],
        [[13], [14], [15], [16]],
    ]], dtype=np.float32))
    val = common_attention.right_shift_blockwise_nd(tensor, (2, 2))
    res = self.evaluate(val)
    expected_val = np.array([[
        [[0], [1], [6], [3]],
        [[2], [5], [4], [7]],
        [[8], [9], [14], [11]],
        [[10], [13], [12], [15]],
    ]], dtype=np.float32)
    self.assertAllClose(expected_val, res) 
Example #3
Source File: glow_ops_test.py    From tensor2tensor with Apache License 2.0 6 votes vote down vote up
def linear_interpolate_rank(self):
    with tf.Graph().as_default():
      # Since rank is 1, the first channel should remain 1.0.
      # and the second channel should be interpolated between 1.0 and 6.0
      z1 = np.ones(shape=(4, 4, 2))
      z2 = np.copy(z1)
      z2[:, :, 0] += 0.01
      z2[:, :, 1] += 5.0
      coeffs = np.linspace(0.0, 1.0, 11)
      z1 = np.expand_dims(z1, axis=0)
      z2 = np.expand_dims(z2, axis=0)
      tensor1 = tf.convert_to_tensor(z1, dtype=tf.float32)
      tensor2 = tf.convert_to_tensor(z2, dtype=tf.float32)
      lin_interp_max = glow_ops.linear_interpolate_rank(
          tensor1, tensor2, coeffs)
      with tf.Session() as sess:
        lin_interp_np_max = sess.run(lin_interp_max)
        for lin_interp_np, coeff in zip(lin_interp_np_max, coeffs):
          exp_val = 1.0 + coeff * (6.0 - 1.0)
          self.assertTrue(np.allclose(lin_interp_np[:, :, 0], 1.0))
          self.assertTrue(np.allclose(lin_interp_np[:, :, 1], exp_val)) 
Example #4
Source File: glow_ops_test.py    From tensor2tensor with Apache License 2.0 6 votes vote down vote up
def test_temperature_normal(self, temperature):
    with tf.Graph().as_default():
      rng = np.random.RandomState(0)
      # in numpy, so that multiple calls don't trigger different random numbers.
      loc_t = tf.convert_to_tensor(rng.randn(5, 5))
      scale_t = tf.convert_to_tensor(rng.rand(5, 5))
      tempered_normal = glow_ops.TemperedNormal(
          loc=loc_t, scale=scale_t, temperature=temperature)
      # smoke test for a single sample.
      smoke_sample = tempered_normal.sample()
      samples = tempered_normal.sample((10000,), seed=0)

      with tf.Session() as sess:
        ops = [samples, loc_t, scale_t, smoke_sample]
        samples_np, loc_exp, scale_exp, _ = sess.run(ops)
        scale_exp *= temperature
        loc_act = np.mean(samples_np, axis=0)
        scale_act = np.std(samples_np, axis=0)
        self.assertTrue(np.allclose(loc_exp, loc_act, atol=1e-2))
        self.assertTrue(np.allclose(scale_exp, scale_act, atol=1e-2)) 
Example #5
Source File: tensorspec_utils_test.py    From tensor2robot with Apache License 2.0 6 votes vote down vote up
def test_pad_image_tensor_to_spec_shape(self):
    varlen_spec = utils.ExtendedTensorSpec(
        shape=(3, 2, 2, 1),
        dtype=tf.uint8,
        name='varlen',
        data_format='png',
        varlen_default_value=3.0)
    test_data = [[
        [[[1]] * 2] * 2,
        [[[2]] * 2] * 2,
    ]]
    prepadded_tensor = tf.convert_to_tensor(test_data, dtype=varlen_spec.dtype)
    tensor = utils.pad_or_clip_tensor_to_spec_shape(prepadded_tensor,
                                                    varlen_spec)
    with self.session() as sess:
      np_tensor = sess.run(tensor)
      self.assertAllEqual(
          np_tensor,
          np.array([[
              [[[1]] * 2] * 2,
              [[[2]] * 2] * 2,
              [[[3]] * 2] * 2,
          ]])) 
Example #6
Source File: tensorspec_utils.py    From tensor2robot with Apache License 2.0 6 votes vote down vote up
def maybe_ignore_batch(spec_or_tensors, ignore_batch = False):
  """Optionally strips the batch dimension and returns new spec.

  Args:
    spec_or_tensors: A dict, (named)tuple, list or a hierarchy thereof filled by
      TensorSpecs(subclasses) or Tensors.
    ignore_batch: If True, the spec_or_batch's batch dimensions are ignored for
      shape comparison.

  Returns:
    spec_or_tensors: If ignore_batch=True we return a spec structure with the
      stripped batch_dimension otherwise we return spec_or_tensors.
  """
  if ignore_batch:
    def map_fn(spec):
      if isinstance(spec, np.ndarray):
        spec = tf.convert_to_tensor(spec)
      if isinstance(spec, tf.Tensor):
        return ExtendedTensorSpec.from_tensor(spec[0])
      else:
        return ExtendedTensorSpec.from_spec(spec, shape=spec.shape[1:])
    return nest.map_structure(
        map_fn,
        spec_or_tensors)
  return spec_or_tensors 
Example #7
Source File: global_step_functions_test.py    From tensor2robot with Apache License 2.0 6 votes vote down vote up
def test_piecewise_linear(self, boundaries, values, test_inputs,
                            expected_outputs):
    global_step = tf.train.get_or_create_global_step()
    global_step_value = tf.placeholder(tf.int64, [])
    set_global_step = tf.assign(global_step, global_step_value)

    test_function = global_step_functions.piecewise_linear(boundaries, values)
    with tf.Session() as sess:
      for x, y_expected in zip(test_inputs, expected_outputs):
        sess.run(set_global_step, {global_step_value: x})
        y = sess.run(test_function)
        self.assertEqual(y, y_expected)

    # Test the same with tensors as inputs
    test_function = global_step_functions.piecewise_linear(
        tf.convert_to_tensor(boundaries), tf.convert_to_tensor(values))
    with tf.Session() as sess:
      for x, y_expected in zip(test_inputs, expected_outputs):
        sess.run(set_global_step, {global_step_value: x})
        y = sess.run(test_function)
        self.assertEqual(y, y_expected) 
Example #8
Source File: mocks.py    From tensor2robot with Apache License 2.0 6 votes vote down vote up
def inference_network_fn(self,
                           features,
                           labels,
                           mode,
                           config=None,
                           params=None):
    """See base class documentation."""
    del mode, config, params
    if not self._model:
      self._build_model()

    if self._multi_dataset:
      if tf.executing_eagerly():
        x1 = tf.convert_to_tensor(features.x1)
        x2 = tf.convert_to_tensor(features.x2)
      else:
        x1 = features.x1
        x2 = features.x2
      net = x1 + x2
    else:
      net = features.x

    net = self._model(net)
    return dict(logits=net) 
Example #9
Source File: seq2seq.py    From magenta with Apache License 2.0 6 votes vote down vote up
def categorical_sample(logits, dtype=tf.int32,
                       sample_shape=(), seed=None):
  """Samples from categorical distribution."""
  logits = tf.convert_to_tensor(logits, name="logits")
  event_size = tf.shape(logits)[-1]
  batch_shape_tensor = tf.shape(logits)[:-1]
  def _sample_n(n):
    """Sample vector of categoricals."""
    if logits.shape.ndims == 2:
      logits_2d = logits
    else:
      logits_2d = tf.reshape(logits, [-1, event_size])
    sample_dtype = tf.int64 if logits.dtype.size > 4 else tf.int32
    draws = tf.multinomial(
        logits_2d, n, seed=seed, output_dtype=sample_dtype)
    draws = tf.reshape(
        tf.transpose(draws),
        tf.concat([[n], batch_shape_tensor], 0))
    return tf.cast(draws, dtype)
  return _call_sampler(_sample_n, sample_shape) 
Example #10
Source File: seq2seq.py    From magenta with Apache License 2.0 6 votes vote down vote up
def _call_sampler(sample_n_fn, sample_shape, name=None):
  """Reshapes vector of samples."""
  with tf.name_scope(name, "call_sampler", values=[sample_shape]):
    sample_shape = tf.convert_to_tensor(
        sample_shape, dtype=tf.int32, name="sample_shape")
    # Ensure sample_shape is a vector (vs just a scalar).
    pad = tf.cast(tf.equal(tf.rank(sample_shape), 0), tf.int32)
    sample_shape = tf.reshape(
        sample_shape,
        tf.pad(tf.shape(sample_shape),
               paddings=[[pad, 0]],
               constant_values=1))
    samples = sample_n_fn(tf.reduce_prod(sample_shape))
    batch_event_shape = tf.shape(samples)[1:]
    final_shape = tf.concat([sample_shape, batch_event_shape], 0)
    return tf.reshape(samples, final_shape) 
Example #11
Source File: common_layers.py    From tensor2tensor with Apache License 2.0 6 votes vote down vote up
def cast_like(x, y):
  """Cast x to y's dtype, if necessary."""
  x = tf.convert_to_tensor(x)
  y = tf.convert_to_tensor(y)

  if x.dtype.base_dtype == y.dtype.base_dtype:
    return x

  cast_x = tf.cast(x, y.dtype)
  if cast_x.device != x.device:
    x_name = "(eager Tensor)"
    try:
      x_name = x.name
    except AttributeError:
      pass
    tf.logging.warning("Cast for %s may induce copy from '%s' to '%s'", x_name,
                       x.device, cast_x.device)
  return cast_x 
Example #12
Source File: common_layers.py    From tensor2tensor with Apache License 2.0 6 votes vote down vote up
def shape_list(x):
  """Return list of dims, statically where possible."""
  x = tf.convert_to_tensor(x)

  # If unknown rank, return dynamic shape
  if x.get_shape().dims is None:
    return tf.shape(x)

  static = x.get_shape().as_list()
  shape = tf.shape(x)

  ret = []
  for i, dim in enumerate(static):
    if dim is None:
      dim = shape[i]
    ret.append(dim)
  return ret 
Example #13
Source File: seq2seq.py    From magenta with Apache License 2.0 6 votes vote down vote up
def __init__(self, sample_fn, sample_shape, sample_dtype,
               start_inputs, end_fn, next_inputs_fn=None):
    """Initializer.

    Args:
      sample_fn: A callable that takes `outputs` and emits tensor `sample_ids`.
      sample_shape: Either a list of integers, or a 1-D Tensor of type `int32`,
        the shape of the each sample in the batch returned by `sample_fn`.
      sample_dtype: the dtype of the sample returned by `sample_fn`.
      start_inputs: The initial batch of inputs.
      end_fn: A callable that takes `sample_ids` and emits a `bool` vector
        shaped `[batch_size]` indicating whether each sample is an end token.
      next_inputs_fn: (Optional) A callable that takes `sample_ids` and returns
        the next batch of inputs. If not provided, `sample_ids` is used as the
        next batch of inputs.
    """
    self._sample_fn = sample_fn
    self._end_fn = end_fn
    self._sample_shape = tf.TensorShape(sample_shape)
    self._sample_dtype = sample_dtype
    self._next_inputs_fn = next_inputs_fn
    self._batch_size = tf.shape(start_inputs)[0]
    self._start_inputs = tf.convert_to_tensor(
        start_inputs, name="start_inputs") 
Example #14
Source File: linear.py    From language with Apache License 2.0 6 votes vote down vote up
def call(self, inputs):
    inputs = tf.convert_to_tensor(inputs)
    rank = tf.rank(inputs)
    if rank > 2:
      outputs = tf.einsum("aki,aij->akj", inputs, self.kernel)

      # Reshape the output back to the original ndim of the input.
      if not tf.executing_eagerly():
        shape = inputs.get_shape().as_list()
        output_shape = shape[:-1] + [self.units]
        outputs.set_shape(output_shape)
    else:
      assert False
      # outputs = tf.mat_mul(inputs, self.kernel)
    if self.use_bias:
      outputs = tf.nn.bias_add(outputs, self.bias)
    if self.activation is not None:
      return self.activation(outputs)  # pylint: disable=not-callable
    return outputs 
Example #15
Source File: attention.py    From language with Apache License 2.0 6 votes vote down vote up
def safe_cumprod(x, *args, **kwargs):
  """Computes cumprod of x in logspace using cumsum to avoid underflow.

  The cumprod function and its gradient can result in numerical instabilities
  when its argument has very small and/or zero values.  As long as the argument
  is all positive, we can instead compute the cumulative product as
  exp(cumsum(log(x))).  This function can be called identically to tf.cumprod.

  Args:
    x: Tensor to take the cumulative product of.
    *args: Passed on to cumsum; these are identical to those in cumprod.
    **kwargs: Passed on to cumsum; these are identical to those in cumprod.
  Returns:
    Cumulative product of x.
  """
  with tf.name_scope(None, "SafeCumprod", [x]):
    x = tf.convert_to_tensor(x, name="x")
    tiny = np.finfo(x.dtype.as_numpy_dtype).tiny
    return tf.exp(
        tf.cumsum(tf.log(tf.clip_by_value(x, tiny, 1)), *args, **kwargs)) 
Example #16
Source File: vqa_utils.py    From tensor2tensor with Apache License 2.0 6 votes vote down vote up
def _aspect_preserving_resize(image, smallest_side):
  """Resize images preserving the original aspect ratio.

  Args:
    image: A 3-D image `Tensor`.
    smallest_side: A python integer or scalar `Tensor` indicating the size of
    the smallest side after resize.

  Returns:
    resized_image: A 3-D tensor containing the resized image.
  """
  smallest_side = tf.convert_to_tensor(smallest_side, dtype=tf.int32)

  shape = tf.shape(image)
  height = shape[0]
  width = shape[1]
  new_height, new_width = _smallest_size_at_least(height, width, smallest_side)
  image = tf.expand_dims(image, 0)
  resized_image = tf.image.resize_images(
      image, size=[new_height, new_width], method=tf.image.ResizeMethod.BICUBIC)

  resized_image = tf.squeeze(resized_image)
  resized_image.set_shape([None, None, 3])
  return resized_image 
Example #17
Source File: model.py    From compression with Apache License 2.0 6 votes vote down vote up
def _scheduled_value(value, schedule, step, name, summary=False):
  """Create a tensor whose value depends on global step.

  Args:
    value: The value to adapt.
    schedule: Dictionary. Expects 'steps' and 'vals'.
    step: The global_step to find to.
    name: Name of the value.
    summary: Boolean, whether to add a summary for the scheduled value.

  Returns:
    tf.Tensor.
  """
  with tf.name_scope("schedule_" + name):
    if len(schedule["steps"]) + 1 != len(schedule["vals"]):
      raise ValueError("Schedule expects one more value than steps.")
    steps = [int(s) for s in schedule["steps"]]
    steps = tf.stack(steps + [step + 1])
    idx = tf.where(step < steps)[0, 0]
    value = value * tf.convert_to_tensor(schedule["vals"])[idx]
  if summary:
    tf.summary.scalar(name, value)
  return value 
Example #18
Source File: ssd_dataloader.py    From benchmarks with Apache License 2.0 6 votes vote down vote up
def __init__(self):
    similarity_calc = region_similarity_calculator.IouSimilarity()
    matcher = argmax_matcher.ArgMaxMatcher(
        matched_threshold=ssd_constants.MATCH_THRESHOLD,
        unmatched_threshold=ssd_constants.MATCH_THRESHOLD,
        negatives_lower_than_unmatched=True,
        force_match_for_each_row=True)

    box_coder = faster_rcnn_box_coder.FasterRcnnBoxCoder(
        scale_factors=ssd_constants.BOX_CODER_SCALES)

    self.default_boxes = DefaultBoxes()('ltrb')
    self.default_boxes = box_list.BoxList(
        tf.convert_to_tensor(self.default_boxes))
    self.assigner = target_assigner.TargetAssigner(
        similarity_calc, matcher, box_coder) 
Example #19
Source File: deepspeech.py    From benchmarks with Apache License 2.0 6 votes vote down vote up
def get_synthetic_inputs(self, input_name, nclass):
    inputs = tf.random_uniform(self.get_input_shapes('train')[0],
                               dtype=self.get_input_data_types('train')[0])
    inputs = variables.VariableV1(inputs, trainable=False,
                                  collections=[tf.GraphKeys.LOCAL_VARIABLES],
                                  name=input_name)
    labels = tf.convert_to_tensor(
        np.random.randint(28, size=[self.batch_size, self.max_label_length]))
    input_lengths = tf.convert_to_tensor(
        [self.max_time_steps] * self.batch_size)
    label_lengths = tf.convert_to_tensor(
        [self.max_label_length] * self.batch_size)
    return [inputs, labels, input_lengths, label_lengths]

  # TODO(laigd): support fp16.
  # TODO(laigd): support multiple gpus. 
Example #20
Source File: mobilenet.py    From benchmarks with Apache License 2.0 6 votes vote down vote up
def global_pool(input_tensor, pool_op=tf.nn.avg_pool):
  """Applies avg pool to produce 1x1 output.

  NOTE: This function is funcitonally equivalenet to reduce_mean, but it has
  baked in average pool which has better support across hardware.

  Args:
    input_tensor: input tensor
    pool_op: pooling op (avg pool is default)
  Returns:
    a tensor batch_size x 1 x 1 x depth.
  """
  shape = input_tensor.get_shape().as_list()
  if shape[1] is None or shape[2] is None:
    kernel_size = tf.convert_to_tensor(
        [1, tf.shape(input_tensor)[1],
         tf.shape(input_tensor)[2], 1])
  else:
    kernel_size = [1, shape[1], shape[2], 1]
  output = pool_op(
      input_tensor, ksize=kernel_size, strides=[1, 1, 1, 1], padding='VALID')
  # Recover output shape, for unknown shape.
  output.set_shape([None, 1, 1, None])
  return output 
Example #21
Source File: ops_test.py    From language with Apache License 2.0 5 votes vote down vote up
def test_lowercase(self):
    with self.test_session() as sess:
      test_str = [["Abc%@||", "DZ dzD", ""]]
      self.assertEqual(
          sess.run(ops.lowercase_op(tf.convert_to_tensor(test_str))).tolist(),
          [[x.lower() for x in test_str[0]]]) 
Example #22
Source File: feature_delf.py    From pyslam with GNU General Public License v3.0 5 votes vote down vote up
def compute_kps_des(self, frame):
        with self.lock:         
            image_tf = tf.convert_to_tensor(frame, np.float32)
            im = self.session.run(image_tf)
            
            # Extract and save features.
            (locations_out, descriptors_out, feature_scales_out, attention_out) = self.extractor_fn(im) 
            
            self.pts = locations_out[:, ::-1]
            self.des = descriptors_out
            self.scales = feature_scales_out
            self.scores = attention_out
                    
            # N.B.: according to the paper "Large-Scale Image Retrieval with Attentive Deep Local Features":
                # We construct image pyramids by using scales that are a 2 factor apart. For the set of scales 
                # with range from 0.25 to 2.0, 7 different scales are used.            
                # The size of receptive field is inversely proportional to the scale; for example, for the 2.0 scale, the
                # receptive field of the network covers 146 × 146 pixels. 
                # The receptive field size for the image at the original scale is 291 × 291.
            #sizes = self.keypoint_size * 1./self.scales
            sizes = self.keypoint_size * self.scales
            
            if False:            
                # print('kps.shape', self.pts.shape)
                # print('des.shape', self.des.shape)          
                # print('scales.shape', self.scales.shape)          
                # print('scores.shape', self.scores.shape)  
                print('scales:',self.scales)     
                print('sizes:',sizes)
            
            self.kps = convert_pts_to_keypoints(self.pts, self.scores, sizes)
            
            return self.kps, self.des 
Example #23
Source File: helpers.py    From language with Apache License 2.0 5 votes vote down vote up
def __init__(self, inputs, sequence_length, mixing_concentration=1.,
               time_major=False, seed=None, scheduling_seed=None, name=None):
    """Initializer.

    Args:
      inputs: A (structure of) input tensors.
      sequence_length: An int32 vector tensor.
      mixing_concentration: <float32> [] for the alpha parameter of the
        Dirichlet distribution used to sample mixing weights from [0, 1].
      time_major: Python bool. Whether the tensors in `inputs` are time major.
        If `False` (default), they are assumed to be batch major.
      seed: The sampling seed.
      scheduling_seed: The schedule decision rule sampling seed.
      name: Name scope for any created operations.

    Raises:
      ValueError: if `sampling_probability` is not a scalar or vector.
    """
    with tf.name_scope(name, "ScheduledContinuousEmbedding",
                       [mixing_concentration]):
      self._mixing_concentration = tf.convert_to_tensor(
          mixing_concentration, name="mixing_concentration")
      if self._mixing_concentration.get_shape().ndims == 0:
        self._mixing_concentration = tf.expand_dims(self._mixing_concentration,
                                                    0)
      if (self._mixing_concentration.get_shape().ndims != 1 or
          self._mixing_concentration.get_shape().as_list()[0] > 1):
        raise ValueError(
            "mixing_concentration must be a scalar. saw shape: %s" %
            (self._mixing_concentration.get_shape()))
      self._seed = seed
      self._scheduling_seed = scheduling_seed
      super(ScheduledContinuousEmbeddingTrainingHelper, self).__init__(
          inputs=inputs,
          sequence_length=sequence_length,
          time_major=time_major,
          name=name) 
Example #24
Source File: spectral_ops_test.py    From magenta with Apache License 2.0 5 votes vote down vote up
def testDiff(self, shape, axis):
    x_np = np.random.randn(*shape)
    x_tf = tf.convert_to_tensor(x_np)
    res_np = np.diff(x_np, axis=axis)
    with self.cached_session() as sess:
      res_tf = sess.run(spectral_ops.diff(x_tf, axis=axis))
    self.assertEqual(res_np.shape, res_tf.shape)
    self.assertAllClose(res_np, res_tf) 
Example #25
Source File: image_utils.py    From magenta with Apache License 2.0 5 votes vote down vote up
def _aspect_preserving_resize(image, smallest_side):
  """Resize images preserving the original aspect ratio.

  Args:
    image: A 3-D image or a 4-D batch of images `Tensor`.
    smallest_side: A python integer or scalar `Tensor` indicating the size of
      the smallest side after resize.

  Returns:
    resized_image: A 3-D or 4-D tensor containing the resized image(s).
  """
  smallest_side = tf.convert_to_tensor(smallest_side, dtype=tf.int32)

  input_rank = len(image.shape)
  if input_rank == 3:
    image = tf.expand_dims(image, 0)

  shape = tf.shape(image)
  height = shape[1]
  width = shape[2]
  new_height, new_width = _smallest_size_at_least(height, width, smallest_side)
  resized_image = tf.image.resize_bilinear(image, [new_height, new_width],
                                           align_corners=False)
  if input_rank == 3:
    resized_image = tf.squeeze(resized_image)
    resized_image.set_shape([None, None, 3])
  else:
    resized_image.set_shape([None, None, None, 3])
  return resized_image 
Example #26
Source File: image_utils.py    From magenta with Apache License 2.0 5 votes vote down vote up
def _smallest_size_at_least(height, width, smallest_side):
  """Computes new shape with the smallest side equal to `smallest_side`.

  Computes new shape with the smallest side equal to `smallest_side` while
  preserving the original aspect ratio.

  Args:
    height: an int32 scalar tensor indicating the current height.
    width: an int32 scalar tensor indicating the current width.
    smallest_side: A python integer or scalar `Tensor` indicating the size of
      the smallest side after resize.

  Returns:
    new_height: an int32 scalar tensor indicating the new height.
    new_width: and int32 scalar tensor indicating the new width.
  """
  smallest_side = tf.convert_to_tensor(smallest_side, dtype=tf.int32)

  height = tf.to_float(height)
  width = tf.to_float(width)
  smallest_side = tf.to_float(smallest_side)

  scale = tf.cond(tf.greater(height, width),
                  lambda: smallest_side / width,
                  lambda: smallest_side / height)
  new_height = tf.to_int32(height * scale)
  new_width = tf.to_int32(width * scale)
  return new_height, new_width 
Example #27
Source File: specgrams_helper_test.py    From magenta with Apache License 2.0 5 votes vote down vote up
def setUp(self):
    super(SpecgramsHelperTest, self).setUp()
    self.data_filename = os.path.join(
        tf.resource_loader.get_data_files_path(),
        '../../../testdata/example_nsynth_audio.npy')
    audio = np.load(self.data_filename)
    # Reduce batch size and audio length to speed up test
    self.batch_size = 2
    self.sr = 16000
    self.audio_length = int(self.sr * 0.5)
    self.audio_np = audio[:self.batch_size, :self.audio_length, np.newaxis]
    self.audio = tf.convert_to_tensor(self.audio_np)
    # Test the standard configuration of SpecgramsHelper
    self.spec_shape = [128, 1024]
    self.overlap = 0.75
    self.sh = specgrams_helper.SpecgramsHelper(
        audio_length=self.audio_length,
        spec_shape=tuple(self.spec_shape),
        overlap=self.overlap,
        sample_rate=self.sr,
        mel_downscale=1,
        ifreq=True,
        discard_dc=True)
    self.transform_pairs = {
        'stfts':
            (self.sh.waves_to_stfts, self.sh.stfts_to_waves),
        'specgrams':
            (self.sh.waves_to_specgrams, self.sh.specgrams_to_waves),
        'melspecgrams':
            (self.sh.waves_to_melspecgrams, self.sh.melspecgrams_to_waves)
    } 
Example #28
Source File: spectral_ops_test.py    From magenta with Apache License 2.0 5 votes vote down vote up
def testPolar2Rect(self, shape):
    mag_np = 10 * np.random.rand(*shape)
    phase_np = np.pi * (2 * np.random.rand(*shape) - 1)
    rect_np = mag_np * np.cos(phase_np) + 1.0j * mag_np * np.sin(phase_np)
    mag_tf = tf.convert_to_tensor(mag_np)
    phase_tf = tf.convert_to_tensor(phase_np)
    with self.cached_session() as sess:
      rect_tf = sess.run(spectral_ops.polar2rect(mag_tf, phase_tf))
    self.assertAllClose(rect_np, rect_tf) 
Example #29
Source File: spectral_ops_test.py    From magenta with Apache License 2.0 5 votes vote down vote up
def testUnwrap(self, shape, axis):
    x_np = 5 * np.random.randn(*shape)
    x_tf = tf.convert_to_tensor(x_np)
    res_np = np.unwrap(x_np, axis=axis)
    with self.cached_session() as sess:
      res_tf = sess.run(spectral_ops.unwrap(x_tf, axis=axis))
    self.assertEqual(res_np.shape, res_tf.shape)
    self.assertAllClose(res_np, res_tf) 
Example #30
Source File: spectral_ops.py    From magenta with Apache License 2.0 5 votes vote down vote up
def polar2rect(mag, phase_angle):
  """Convert polar-form complex number to its rectangular form."""
  mag = tf.complex(mag, tf.convert_to_tensor(0.0, dtype=mag.dtype))
  phase = tf.complex(tf.cos(phase_angle), tf.sin(phase_angle))
  return mag * phase