Exemplo n.º 1
0
  def call(self, inputs):
    if ragged_tensor.is_ragged(inputs):
      integer_buckets = ragged_functional_ops.map_flat_values(
          math_ops._bucketize, inputs, boundaries=self.bins)  # pylint: disable=protected-access
      # Ragged map_flat_values doesn't touch the non-values tensors in the
      # ragged composite tensor. If this op is the only op a Keras model,
      # this can cause errors in Graph mode, so wrap the tensor in an identity.
      integer_buckets = array_ops.identity(integer_buckets)
    elif isinstance(inputs, sparse_tensor.SparseTensor):
      integer_buckets = math_ops._bucketize(  # pylint: disable=protected-access
          inputs.values,
          boundaries=self.bins)
    else:
      integer_buckets = math_ops._bucketize(inputs, boundaries=self.bins)  # pylint: disable=protected-access

    if self.output_mode == INTEGER:
      if isinstance(inputs, sparse_tensor.SparseTensor):
        return sparse_tensor.SparseTensor(
            indices=array_ops.identity(inputs.indices),
            values=integer_buckets,
            dense_shape=array_ops.identity(inputs.dense_shape))
      return integer_buckets
    else:
      if isinstance(inputs, sparse_tensor.SparseTensor):
        raise ValueError("`output_mode=binary` is not supported for "
                         "sparse input")
      # The 'bins' array is the set of boundaries between the bins. We actually
      # have 'len(bins)+1' outputs.
      # TODO(momernick): This will change when we have the ability to adapt().
      return array_ops.one_hot(integer_buckets, depth=len(self.bins) + 1)
Exemplo n.º 2
0
 def testInt(self):
   op = math_ops._bucketize(
       constant_op.constant([-5, 0, 2, 3, 5, 8, 10, 11, 12]),
       boundaries=[0, 3, 8, 11])
   expected_out = [0, 1, 1, 2, 2, 3, 3, 4, 4]
   with self.test_session() as sess:
     self.assertAllEqual(expected_out, sess.run(op))
Exemplo n.º 3
0
 def testInt(self):
   op = math_ops._bucketize(
       constant_op.constant([-5, 0, 2, 3, 5, 8, 10, 11, 12]),
       boundaries=[0, 3, 8, 11])
   expected_out = [0, 1, 1, 2, 2, 3, 3, 4, 4]
   with self.session(use_gpu=True) as sess:
     self.assertAllEqual(expected_out, self.evaluate(op))
Exemplo n.º 4
0
def compute_depth_loss(logits, frame):
    # Perform preprocessing
    frame = tf.to_float(frame[:-1, ...])
    frame /= 255.0

    # Convert to floats.
    batch_size = FLAGS.batch_size * FLAGS.unroll_length
    frame, depth = tf.split(frame, [3, 1], axis=-1)
    depth = tf.reshape(depth, [batch_size] + depth.get_shape().as_list()[2:])
    logits = tf.reshape(
        logits,
        [batch_size, -1, logits.get_shape().as_list()[-1]])

    # Create a low resolution (4x16) depth map
    crop_size = tf.expand_dims(tf.constant([0.2, 0.05, 0.8, 0.95]), axis=0)
    depth = tf.reshape(
        tf.image.crop_and_resize(depth,
                                 tf.matmul(tf.ones([batch_size, 1]),
                                           crop_size),
                                 tf.range(batch_size),
                                 crop_size=[4, 16]),
        tf.shape(logits)[:-1])

    # Bucketize depth and compute loss
    depth = math_ops._bucketize(tf.pow(
        depth, [10]), [0, 0.05, 0.175, 0.3, 0.425, 0.55, 0.675, 0.8, 1.01]) - 1
    depth_loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(
        logits=logits, labels=depth),
                                axis=-1)

    return tf.reduce_sum(depth_loss)
Exemplo n.º 5
0
 def testFloat(self):
     op = math_ops._bucketize(constant_op.constant(
         [-5., 0., 2., 3., 5., 8., 10., 11., 12.]),
                              boundaries=[0., 3., 8., 11.])
     expected_out = [0, 1, 1, 2, 2, 3, 3, 4, 4]
     with self.session() as sess:
         self.assertAllEqual(expected_out, self.evaluate(op))
Exemplo n.º 6
0
 def test2DInput(self):
     op = math_ops._bucketize(constant_op.constant([[-5, 0, 2, 3, 5],
                                                    [8, 10, 11, 12, 0]]),
                              boundaries=[0, 3, 8, 11])
     expected_out = [[0, 1, 1, 2, 2], [3, 3, 4, 4, 1]]
     with self.session() as sess:
         self.assertAllEqual(expected_out, self.evaluate(op))
Exemplo n.º 7
0
 def testInvalidBoundariesOrder(self):
     op = math_ops._bucketize(constant_op.constant([-5, 0]),
                              boundaries=[0, 8, 3, 11])
     with self.session() as sess:
         with self.assertRaisesRegex(errors_impl.InvalidArgumentError,
                                     "Expected sorted boundaries"):
             self.evaluate(op)
 def testFloat(self):
   op = math_ops._bucketize(
       constant_op.constant([-5., 0., 2., 3., 5., 8., 10., 11., 12.]),
       boundaries=[0., 3., 8., 11.])
   expected_out = [0, 1, 1, 2, 2, 3, 3, 4, 4]
   with self.session(use_gpu=True) as sess:
     self.assertAllEqual(expected_out, sess.run(op))
 def test2DInput(self):
   op = math_ops._bucketize(
       constant_op.constant([[-5, 0, 2, 3, 5], [8, 10, 11, 12, 0]]),
       boundaries=[0, 3, 8, 11])
   expected_out = [[0, 1, 1, 2, 2], [3, 3, 4, 4, 1]]
   with self.session(use_gpu=True) as sess:
     self.assertAllEqual(expected_out, sess.run(op))
 def testInvalidBoundariesOrder(self):
   op = math_ops._bucketize(
       constant_op.constant([-5, 0]), boundaries=[0, 8, 3, 11])
   with self.session(use_gpu=True) as sess:
     with self.assertRaisesRegexp(
         errors_impl.InvalidArgumentError, "Expected sorted boundaries"):
       sess.run(op)
Exemplo n.º 11
0
 def call(self, inputs):
     if inputs.dtype is tf.string:
         inputs = tf.strings.to_number(inputs, out_type=tf.float32)
     else:
         inputs = tf.cast(inputs, tf.float32)
     bucket_id = math_ops._bucketize(inputs, boundaries=self.boundaries)
     return tf.cast(bucket_id, tf.int64)
Exemplo n.º 12
0
 def testInt(self):
   with self.test_session() as sess:
     p = array_ops.placeholder(dtypes.int32)
     with self.test_scope():
       op = math_ops._bucketize(p, boundaries=[0, 3, 8, 11])
     expected_out = [0, 1, 1, 2, 2, 3, 3, 4, 4]
     self.assertAllEqual(expected_out,
                         sess.run(op, {p: [-5, 0, 2, 3, 5, 8, 10, 11, 12]}))
Exemplo n.º 13
0
 def testInvalidBoundariesOrder(self):
   with self.test_session() as sess:
     p = array_ops.placeholder(dtypes.int32)
     with self.test_scope():
       op = math_ops._bucketize(p, boundaries=[0, 8, 3, 11])
     with self.assertRaisesRegexp(errors_impl.InvalidArgumentError,
                                  "Expected sorted boundaries"):
       sess.run(op, {p: [-5, 0]})
Exemplo n.º 14
0
 def call(self, inputs):
     if ragged_tensor.is_ragged(inputs):
         integer_buckets = ragged_functional_ops.map_flat_values(
             math_ops._bucketize, inputs, boundaries=self.bins)  # pylint: disable=protected-access
         # Ragged map_flat_values doesn't touch the non-values tensors in the
         # ragged composite tensor. If this op is the only op a Keras model,
         # this can cause errors in Graph mode, so wrap the tensor in an identity.
         return array_ops.identity(integer_buckets)
     elif isinstance(inputs, sparse_tensor.SparseTensor):
         integer_buckets = math_ops._bucketize(  # pylint: disable=protected-access
             inputs.values,
             boundaries=self.bins)
         return sparse_tensor.SparseTensor(
             indices=array_ops.identity(inputs.indices),
             values=integer_buckets,
             dense_shape=array_ops.identity(inputs.dense_shape))
     else:
         return math_ops._bucketize(inputs, boundaries=self.bins)  # pylint: disable=protected-access
Exemplo n.º 15
0
 def testFloat(self):
   with self.test_session() as sess:
     p = array_ops.placeholder(dtypes.float32)
     with self.test_scope():
       op = math_ops._bucketize(p, boundaries=[0., 3., 8., 11.])
     expected_out = [0, 1, 1, 2, 2, 3, 3, 4, 4]
     self.assertAllEqual(
         expected_out,
         sess.run(op, {p: [-5., 0., 2., 3., 5., 8., 10., 11., 12.]}))
Exemplo n.º 16
0
 def test2DInput(self):
   with self.test_session() as sess:
     p = array_ops.placeholder(dtypes.float32)
     with self.test_scope():
       op = math_ops._bucketize(p, boundaries=[0, 3, 8, 11])
     expected_out = [[0, 1, 1, 2, 2], [3, 3, 4, 4, 1]]
     self.assertAllEqual(
         expected_out, sess.run(op,
                                {p: [[-5, 0, 2, 3, 5], [8, 10, 11, 12, 0]]}))
Exemplo n.º 17
0
    def call(self, inputs):
        if isinstance(inputs, tf.RaggedTensor):
            integer_buckets = tf.ragged.map_flat_values(math_ops._bucketize,
                                                        inputs,
                                                        boundaries=self.bins)
            integer_buckets = tf.identity(integer_buckets)
        elif isinstance(inputs, tf.SparseTensor):
            integer_bucket_values = math_ops._bucketize(inputs.values,
                                                        boundaries=self.bins)
            integer_buckets = tf.SparseTensor(
                indices=inputs.indices,
                values=integer_bucket_values,
                dense_shape=inputs.dense_shape,
            )
        else:
            integer_buckets = math_ops._bucketize(inputs, boundaries=self.bins)

        return tf.cast(integer_buckets, tf.int64)
Exemplo n.º 18
0
def digitize(num_of_bins, T, min_acti=-1.0, max_acti=1.0):
    """
    cut [-1, 1] into num_of_bins intervals
    the number in T will be changed to its lower cutoff value
    the value that's <-1 or >= 1 will be changed as 1
    """
    bins = np.linspace(start=min_acti, stop=max_acti, num=num_of_bins + 1)
    bins = bins.tolist()  # bucketize needs boundaries to be list
    T_digitized = math_ops._bucketize(T, bins)
    T_digitized = tf.cast(T_digitized, tf.float64) * (
        max_acti -
        min_acti) / num_of_bins + min_acti  # use float64 to be accurate
    return tf.cast(T_digitized, tf.float32)
Exemplo n.º 19
0
def _digitize(values, value_range, nbins, name='digitize'):
    """ tf analog numpy digitize()
    """
    with tf.name_scope(name):
        values = tf.cast(values, dtype=tf.float32)
        if isinstance(value_range, (list, tuple)):
            indices = math_ops._bucketize(values, boundaries=value_range)
        else:
            value_range = tf.cast(value_range, dtype=tf.float32)
            scaled_values = tf.truediv(values - value_range[0],
                                       value_range[1] - value_range[0])
            indices = tf.floor(scaled_values * (nbins - 1))
            indices = tf.clip_by_value(indices, -1, nbins - 1) + 1
        return tf.cast(indices, tf.int32)
 def _pem_bucketing_embeddings(self, buckets_boundaries, bucketing_name):
     from tensorflow.python.ops import math_ops
     bucketized_pem = math_ops._bucketize(self.cand_entities_scores,
                                          boundaries=buckets_boundaries)
     with tf.variable_scope(bucketing_name):
         _pem_embeddings = tf.get_variable(
             name="pem_embeddings_var",
             dtype=tf.float32,
             shape=[len(buckets_boundaries) + 1, 1],
             trainable=True)
         pem_embeddings = tf.nn.embedding_lookup(
             _pem_embeddings, bucketized_pem, name="pem_embeddings_lookup")
         # pem_embeddings =  Tensor("pem_embeddings/pem_embeddings_lookup:0", shape=(?, ?, ?, 1), dtype=float32)
         return tf.squeeze(pem_embeddings, axis=3)
Exemplo n.º 21
0
def bucketize(input_tensor, boundaries, name=None):
  """Bucketizes input_tensor by given boundaries.

  See bucketize_op.cc for more details.

  Args:
    input_tensor: A `Tensor` which will be bucketize.
    boundaries: A list of floats gives the boundaries. It has to be sorted.
    name: A name prefix for the returned tensors (optional).

  Returns:
    A `Tensor` with type int32 which indicates the corresponding bucket for
      each value in `input_tensor`.

  Raises:
    TypeError: If boundaries is not a list.
  """
  return math_ops._bucketize(  # pylint: disable=protected-access
      input_tensor, boundaries=boundaries, name=name)
Exemplo n.º 22
0
def bucketize(input_tensor, boundaries, name=None):
    """Bucketizes input_tensor by given boundaries.

  See bucketize_op.cc for more details.

  Args:
    input_tensor: A `Tensor` which will be bucketize.
    boundaries: A list of floats gives the boundaries. It has to be sorted.
    name: A name prefix for the returned tensors (optional).

  Returns:
    A `Tensor` with type int32 which indicates the corresponding bucket for
      each value in `input_tensor`.

  Raises:
    TypeError: If boundaries is not a list.
  """
    return math_ops._bucketize(  # pylint: disable=protected-access
        input_tensor,
        boundaries=boundaries,
        name=name)
Exemplo n.º 23
0
 def testBoundariesNotList(self):
     with self.assertRaisesRegex(TypeError, "Expected list.*"):
         math_ops._bucketize(constant_op.constant([-5, 0]), boundaries=0)
Exemplo n.º 24
0
 def _transform_feature(self, inputs):
     source_tensor = inputs.get(self.source_column)
     return math_ops._bucketize(  # pylint: disable=protected-access
         source_tensor,
         boundaries=self.boundaries)
 def testBoundariesNotList(self):
   with self.assertRaisesRegexp(
       TypeError, "Expected list.*"):
     math_ops._bucketize(constant_op.constant([-5, 0]), boundaries=0)
Exemplo n.º 26
0
def _extract_fn(data_record, feature_name):
    """Extract the data from a single TFRecord Tensor.

    Parameters
    ----------
    data_record : `tf.Tensor`
        The Tensor object from the sample's TFRecord.
    feature_name : `str`
        The name of the feature to be loaded.

    Returns
    -------
    data : `tf.Tensor`
        Waveform data of the sample.
    maxValStream : `tf.Tensor`
        Maximum amplitude before normalization.
    out_label : `tf.Tensor (?)`
        Target class for the given feature in this sample.

    """
    # Parse record
    all_data = _FEATURES
    sampleData = tf.io.parse_single_example(data_record, all_data)

    # Convert and reshape stream data
    time_data = tf.io.decode_raw(sampleData['time_data'], tf.float32)
    time_data = tf.slice(time_data, [0], [3 * _DATA_STREAM_LENGTH])
    time_data.set_shape([3 * _DATA_STREAM_LENGTH])
    time_data = tf.reshape(time_data, [3, _DATA_STREAM_LENGTH])
    time_data = tf.transpose(time_data, [1, 0])

    # Convert and reshape spectrogram data
    amp1 = tf.io.decode_raw(sampleData['spec0'], tf.float64)
    amp2 = tf.io.decode_raw(sampleData['spec1'], tf.float64)
    amp3 = tf.io.decode_raw(sampleData['spec2'], tf.float64)
    amp1.set_shape([61 * 35])
    amp2.set_shape([61 * 35])
    amp3.set_shape([61 * 35])
    freq_data = tf.concat([amp1, amp2, amp3], 0)
    freq_data = tf.reshape(freq_data, [3, 61, 35])
    freq_data = tf.transpose(freq_data, [1, 2, 0])

    # Read stream max amplitude
    maxValStream = sampleData['stream_max']

    # Dictionary to hold output classes
    classes = {}

    # Read event type
    classes['detection'] = math_ops._bucketize(
        tf.math.subtract(sampleData['event_type'], 1),
        boundaries=list(CLASSES_BINS['detection']))

    # Compute azimuth class
    azim_shape = tf.shape(sampleData['azimuth'])
    azim_flat = tf.reshape(sampleData['azimuth'], [-1])
    azim_180_flat = tf.map_fn(lambda x: x % 180, azim_flat)
    azim_180 = tf.reshape(azim_180_flat, azim_shape)
    classes['azimuth'] = math_ops._bucketize(azim_180,
                                             boundaries=list(
                                                 CLASSES_BINS['azimuth']))

    # For other features, simply read value and bucketize it
    for prop_name in ['depth', 'magnitude', 'distance']:
        classes[prop_name] = math_ops._bucketize(sampleData[prop_name],
                                                 boundaries=list(
                                                     CLASSES_BINS[prop_name]))

    # Get desired feature
    if feature_name in classes:
        out_label = classes[feature_name]
    else:
        raise ValueError('Invalid feature name.')

    return time_data, freq_data, maxValStream, out_label
Exemplo n.º 27
0
def create_prep(inputs, depth=5, treshold=0.050, p_range=[5, 95]):
    """ prepare data inputs
            from shape (?,28800,10)
            to shape (?,16,12,5)

        use tensorflow.python.ops math_ops._bucketize,
            #tensorflow_probability stats.percentile,
            _percentile
            _value_range,
            _digitize,
            _bincount,
            _histogram
    """
    #                0       1        2        3        4         5       6        7
    lv_name = [  'MN1',   'W1',    'D1',    'H4',    'H1',    'M15',   'M5',    'M1' ]
    lv_cols = ['LVMN1', 'LVW1',  'LVD1',  'LVH4',  'LVH1',  'LVM15', 'LVM5',  'LVM1' ]
    lv_time = [  28800,   7200,    1440,     240,      60,       15,      5,       1 ]

    _cols = ['percentile','MN1','W1','D1','H4','H1','M15','M5','M1','MN1_W1','W1_D1','D1_H4','H4_H1','H1_M15','M15_M5','M5_M1']
    _rows = [[1.0,-2.447450723648071,-4.3177623939514165,-6.3557000160217285,-8.466059684753418,-9.868930702209472,-11.187950134277344,-12.33390998840332,-15.201800346374512,-4.037990093231201,-6.00206995010376,-8.103440284729004,-9.51678147315979,-10.829000473022461,-11.951820373535156,-14.190199851989746],
        [2.5,-2.2133100032806396,-3.9119200706481934,-5.871469974517822,-7.971982097625732,-9.357959747314453,-10.654029846191406,-11.706509590148926,-13.815509796142578,-3.5091800689697266,-5.452270030975342,-7.540170192718506,-8.959269523620605,-10.258740425109863,-11.315460205078125,-13.004579544067383],
        [5.0,-1.9093300104141235,-3.4993300437927246,-5.441074085235595,-7.532569885253906,-8.907859802246094,-10.191619873046875,-11.18002986907959,-12.89922046661377,-3.0329699516296387,-4.952859878540039,-7.037650108337402,-8.468887567520142,-9.760040283203125,-10.788009643554688,-12.169260025024414],
        [10.0,-1.5410100221633911,-3.020400047302246,-4.938948059082031,-7.022890090942383,-8.384160041809082,-9.650349617004395,-10.598739624023438,-11.982930183410645,-2.5604898929595947,-4.4264397621154785,-6.470665216445923,-7.90254020690918,-9.177720069885254,-10.207030296325684,-11.321479797363281],
        [20.0,-1.0277600288391113,-2.524139881134033,-4.389679908752441,-6.4340901374816895,-7.755660057067871,-8.987199783325195,-9.90569019317627,-11.000849723815918,-1.9874299764633179,-3.7780098915100098,-5.784180164337158,-7.240270137786865,-8.466130256652832,-9.496930122375488,-10.596630096435547],
        [30.0,-0.693880021572113,-2.1557700634002686,-4.005080223083496,-6.031439781188965,-7.330619812011719,-8.507829666137695,-9.399689674377441,-10.596630096435547,-1.5796500444412231,-3.282870054244995,-5.247350215911865,-6.7797698974609375,-7.970870018005371,-8.972590446472168,-9.943479537963867],
        [40.0,-0.43154001235961914,-1.8716800212860107,-3.6807799339294434,-5.686970233917236,-6.986070156097412,-8.11320972442627,-8.957639694213867,-9.99368953704834,-1.2257399559020996,-2.841749906539917,-4.76255989074707,-6.3881402015686035,-7.5739898681640625,-8.527799606323242,-9.49802017211914],
        [50.0,-0.16436000168323517,-1.5963300466537476,-3.3727200031280518,-5.358520030975342,-6.670050144195557,-7.769979953765869,-8.547510147094727,-9.585029602050781,-0.873420000076294,-2.4463000297546387,-4.315420150756836,-6.023379802703857,-7.221710205078125,-8.135379791259766,-8.987199783325195],
        [60.0,0.10655000060796738,-1.3389500379562378,-3.072000026702881,-5.026080131530762,-6.355410099029541,-7.44789981842041,-8.160039901733398,-9.011489868164062,-0.5510600209236145,-2.056689977645874,-3.879390001296997,-5.650790214538574,-6.881919860839844,-7.777639865875244,-8.517189979553223],
        [70.0,0.39065998792648315,-1.0625499486923218,-2.743410110473633,-4.663980007171631,-6.015250205993652,-7.123149871826172,-7.787829875946045,-8.427579879760742,-0.20032000541687012,-1.661679983139038,-3.438509941101074,-5.243169784545898,-6.515160083770752,-7.427140235900879,-8.053689956665039],
        [80.0,0.7234299778938293,-0.7221300005912781,-2.3731698989868164,-4.230669975280762,-5.598939895629883,-6.750539779663086,-7.397550106048584,-7.957580089569092,0.2176699936389923,-1.241420030593872,-2.9493799209594727,-4.761129856109619,-6.076779842376709,-7.0424299240112305,-7.639100074768066],
        [90.0,1.1333480119705182,-0.27748000621795654,-1.8487399816513062,-3.6258018970489516,-4.991030216217041,-6.211830139160156,-6.9065118789672875,-7.37775993347168,0.7718700170516968,-0.66566002368927,-2.3125998973846436,-4.083680152893066,-5.434470176696777,-6.508309841156006,-7.100130081176758],
        [95.0,1.4636600017547607,0.13891999423503876,-1.4087659597396907,-3.1120100021362305,-4.469629764556885,-5.725886154174811,-6.487239837646484,-6.939499855041504,1.1656099557876587,-0.24150000512599945,-1.8073300123214722,-3.5262200832366943,-4.877600193023682,-6.028369903564453,-6.675849914550781],
        [97.5,1.7651900053024292,0.4791100025177002,-0.9900799989700317,-2.659019947052002,-4.001947784423827,-5.268199920654297,-6.086209774017334,-6.557980060577393,1.4778000116348267,0.16561000049114227,-1.3735699653625488,-3.034640073776245,-4.382919788360596,-5.572669982910156,-6.282489776611328],
        [99.0,2.0152900218963623,0.974839985370636,-0.4590100049972534,-2.106260061264038,-3.4134191608428948,-4.701019001007079,-5.560719966888428,-6.072020053863525,1.8153400421142578,0.6136299967765808,-0.809568512439724,-2.4642285585403405,-3.7802600860595703,-4.997039794921875,-5.770319938659668],]

    db_bins = pd.DataFrame(data=_rows, columns=_cols, dtype=np.float32)

    nbins = db_bins.shape[0]  # nb_classes-1  # 15
    levels = []
    ranges = []

    for i in range(depth):
        time = [lv_time[i], lv_time[i+1]]
        name = [lv_name[i], lv_name[i]+'_'+lv_name[i+1]]

        with tf.name_scope(name[0]):
            data = inputs[:,-time[0]:,:]
            #idx_test = tf.stack([data[:,0,-2], data[:,-1,-2]], axis=1)

            bins = db_bins[name[0]].values.tolist()
            #print_ndarray('bins = db_bins[{}].values'.format(name[0]), bins,  count=16, frm='%8.3f')  # (15,) (nbin,)
            lv_slow = tf.stack([
                data[:,0,i],
                data[:,-1,i],
                ], axis=1)                      # (?,2)
            #tft.apply_buckets
            #https://www.tensorflow.org/tfx/transform/api_docs/python/tft/apply_buckets
            lv_slow = math_ops._bucketize(lv_slow, boundaries=bins, name='bucketize_slow')

            bins = db_bins[name[1]].values.tolist()
            #print_ndarray('bins = db_bins[{}].values'.format(name[1]), bins,  count=16, frm='%8.3f')  # (15,) (nbin,)
            lv_fast = tf.stack([
                data[:,-time[1],i],
                data[:,-1,i],
                data[:,-time[1],i+1],
                data[:,-1,i+1],
                ], axis=1)                        # (?,4)
            lv_fast = math_ops._bucketize(lv_fast, boundaries=bins, name='bucketize_fast')

            price_values = data[:,:,-1]
            value_range = _value_range(price_values, percentile=p_range, treshold=treshold)  # (2,?,1)  (min|max,?,1)
            #x = tf.transpose(value_range, [1,2,0])  # just as output test case

            with tf.name_scope('stack_range'):
                last_value = price_values[:,-1]   # (?,)  last
                price_range = tf.stack([
                    last_value - treshold,
                    last_value,
                    last_value + treshold,
                    tf.reduce_min(price_values, axis=-1, name='min_slow'),
                    tf.reduce_max(price_values, axis=-1, name='max_slow'),
                    tf.reduce_min(price_values[:,-time[1]:], axis=-1, name='min_fast'),
                    tf.reduce_max(price_values[:,-time[1]:], axis=-1, name='max_fast'),
                    ], axis=1)                    # (?,7)
            price_range = _digitize(price_range, value_range, nbins, name='digitize_range')

            x = tf.concat([
                lv_slow,                          # (?,2)  slow (slow (first, last))
                lv_fast,                          # (?,4)  fast (slow (first, last) fast (first, last))
                price_range,                      # (?,7)
                ], axis=-1)                       # (?,13)

            x = tf.concat([
                _histogram(price_values, value_range, nbins, name='histogram_slow'),
                _histogram(price_values[:,-time[1]*2:-time[1]], value_range, nbins, name='histogram_fast1'),
                _histogram(price_values[:,-time[1]:], value_range, nbins, name='histogram_fast0'),
                tf.one_hot(x, nbins+1, axis=1),   # (?,16,13)
                ], axis=-1)                       # (?,16,3+13)

            levels.append(x)
            ranges.append(value_range)

    x = tf.stack(levels, axis=-1)                  #(?,16,12,5)  # (?,nclasses,channels,depth)

    return x, tf.transpose(tf.concat(ranges, axis=-1), [1,0,2])  #(?,2,5)      # (?,2,depth) min|max
Exemplo n.º 28
0
 def _innerfeat(val):
     bucket_indices = math_ops._bucketize(val, boundaries=boundaries)
     return tf.one_hot(bucket_indices, depth=nbuckets, axis=0)
Exemplo n.º 29
0
def _build_deeplab(FLAGS, samples, outputs_to_num_classes, outputs_to_indices, bin_centers_tensors, bin_centers_list, bin_bounds_list, bin_size_list, dataset, codes, inputs_device, is_training=True):
  """Builds a clone of DeepLab.

  Args:
    inputs_queue: A prefetch queue for images and labels.
    # outputs_to_num_classes: A map from output type to the number of classes.
    #   For example, for the task of semantic segmentation with 21 semantic
    #   classes, we would have outputs_to_num_classes['semantic'] = 21.

  Returns:
    A map of maps from output_type (e.g., semantic prediction) to a
      dictionary of multi-scale logits names to logits. For each output_type,
      the dictionary has keys which correspond to the scales and values which
      correspond to the logits. For example, if `scales` equals [1.0, 1.5],
      then the keys would include 'merged_logits', 'logits_1.00' and
      'logits_1.50'.
  """
  if is_training:
      is_training_prefix = ''
  else:
      is_training_prefix = 'val-'

  # Add name to input and label nodes so we can add to summary.
  model_options = common.ModelOptions(
    outputs_to_num_classes=outputs_to_num_classes,
    crop_size=[dataset.height, dataset.width],
    atrous_rates=FLAGS.atrous_rates,
    output_stride=FLAGS.output_stride)

  ## Inputs and tool functions
  samples[common.IMAGE] = tf.identity(
      samples[common.IMAGE], name=is_training_prefix+common.IMAGE)
  samples[common.IMAGE_NAME] = tf.identity(
      samples[common.IMAGE_NAME], name=is_training_prefix+common.IMAGE_NAME)
  samples['seg'] = tf.identity(samples['seg'], name=is_training_prefix+'seg')
  masks = tf.identity(samples['mask'], name=is_training_prefix+'not_ignore_mask_in_loss')
  masks_rescaled_float = samples['mask_rescaled_float']

  if FLAGS.val_split != 'test':
    samples['vis'] = tf.identity(samples['vis'], name=is_training_prefix+'vis')
    samples['depth'] = tf.identity(samples['depth'], name=is_training_prefix+'depth')
    # samples['pose_map'] = tf.identity(samples['pose_map'], name=is_training_prefix+'pose_map')
    # samples['shape_map'] = tf.identity(samples['shape_map'], name=is_training_prefix+'shape_map')
    samples['pose_dict'] = tf.identity(samples['pose_dict'], name=is_training_prefix+'pose_dict')
    samples['rotuvd_dict'] = tf.identity(samples['rotuvd_dict'], name=is_training_prefix+'rotuvd_dict')
    samples['bbox_dict'] = tf.identity(samples['bbox_dict'], name=is_training_prefix+'bbox_dict')
    samples['shape_dict'] = tf.identity(samples['shape_dict'], name=is_training_prefix+'shape_dict')

    car_nums_list = tf.split(samples['car_nums'], samples['car_nums'].get_shape()[0], axis=0)

    def _unpadding(padded, append_left_idx=False): # input: [batch_size, ?, D], output: [car_num_total, D]
        padded_list = tf.split(padded, padded.get_shape()[0])
        unpadded_list = []
        for idx, (padded, car_num) in enumerate(zip(padded_list, car_nums_list)):
          unpadded = tf.slice(tf.squeeze(padded, 0), [0, 0], [tf.squeeze(car_num), padded.get_shape()[2]])
          if append_left_idx:
            unpadded = tf.concat([tf.zeros_like(unpadded)+idx, unpadded], axis=1)
          unpadded_list.append(unpadded)
        unpadded = tf.concat(unpadded_list, axis=0) # (car_num_total=?_1+...+?_bs, D)
        return unpadded

    idx_xys = _unpadding(samples['idxs'], True) # [N, 2]

    # seg_one_hots_N_flattened = tf.tf.gather_nd(samples['seg_one_hots_flattened'], idx_xys) # [N, 272/4*680/4], tf.int32

    seg_one_hots_list = []
    seg_one_hots_flattened_list = []
    seg_one_hots_flattened_rescaled_list = []
    seg_list = tf.split(samples['seg'], samples['seg'].get_shape()[0])

    for seg_sample, car_num in zip(seg_list, car_nums_list):
      seg_one_hots_sample = tf.one_hot(tf.squeeze(tf.cast(seg_sample, tf.int32)), depth=tf.reshape(car_num+1, []))
      seg_one_hots_sample = tf.slice(seg_one_hots_sample, [0, 0, 1], [-1, -1, -1])
      seg_one_hots_list.append(tf.expand_dims(seg_one_hots_sample, 0)) # (1, 272, 680, ?)
      seg_one_hots_flattened_list.append(tf.reshape(seg_one_hots_sample, [-1, tf.shape(seg_one_hots_sample)[2]])) # (272*680, ?)
      seg_one_hots_sample_rescaled = tf.image.resize_nearest_neighbor(tf.expand_dims(seg_one_hots_sample, 0), [masks_rescaled_float.get_shape()[1], masks_rescaled_float.get_shape()[2]], align_corners=True)
      seg_one_hots_flattened_rescaled_list.append(tf.reshape(seg_one_hots_sample_rescaled, [-1, tf.shape(seg_one_hots_sample)[2]])) # (272/4*680/4, ?)

  def logits_cars_to_map(logits_cars, rescale=False):
    logits_cars_N_list = tf.split(logits_cars, samples['car_nums'])
    logits_samples_list = []
    seg_one_hots_flattened_list_use = seg_one_hots_flattened_list if not(rescale) else seg_one_hots_flattened_rescaled_list
    for seg_one_hots_sample, logits_cars_sample in zip(seg_one_hots_flattened_list_use, logits_cars_N_list): # (272*680, ?) (?, 17)
      logits_sample = tf.matmul(seg_one_hots_sample, tf.cast(logits_cars_sample, seg_one_hots_sample.dtype))
      logits_sample  = tf.cast(logits_sample, logits_cars_sample.dtype)
      logits_samples_list.append(tf.reshape(logits_sample, [dataset.height if not(rescale) else tf.shape(masks_rescaled_float)[1], dataset.width if not(rescale) else tf.shape(masks_rescaled_float)[2], logits_cars_sample.get_shape()[1]]))
    logits_map = tf.stack(logits_samples_list, axis=0) # (3, 272, 680, 17)
    return logits_map

  ## ---- get logits
  outputs_to_logits_N, outputs_to_logits_map, outputs_to_weights_map, outputs_to_areas_N, outputs_to_weightsum_N = model.single_scale_logits(
    FLAGS,
    samples[common.IMAGE],
    samples['seg_rescaled'],
    samples['car_nums'],
    idx_xys,
    bin_centers_tensors,
    outputs_to_indices,
    model_options=model_options,
    weight_decay=FLAGS.weight_decay,
    is_training=is_training,
    fine_tune_batch_norm=FLAGS.fine_tune_batch_norm and is_training,
    fine_tune_feature_extractor=FLAGS.fine_tune_feature_extractor and is_training)
  print outputs_to_logits_N


  ## ---- Get regressed logits for all outputs and masks, weights
  reg_shape_logits_list = []
  reg_pose_logits_list = []
  reg_logits_dict = {}

  for output in dataset.output_names:
      if output not in ['x', 'y', 'z_log_dense', 'z_log_offset']: # assuming FLAGS.if_uvflow==True
          prob_logits = train_utils.logits_cls_to_logits_probReg(
              outputs_to_logits_N[output],
              bin_centers_tensors[outputs_to_indices[output]]) # [car_num_total, 1]
          print '||||||||CLS logits for '+output
      if output in ['x', 'y']:
          prob_logits = outputs_to_logits_N[output]
          print '||||||||REG logits for '+output
      if output in ['z_log_offset']:
          prob_logits = outputs_to_logits_map[output]
          print '||||||||REG logits for '+output
      if output in ['z_log_dense']:
          prob_logits = train_utils.logits_cls_map_to_logits_probReg_map(
                  outputs_to_logits_map[output],
                  bin_centers_tensors[outputs_to_indices[output]]) # [car_num_total, 1]
          print '||||||||CLS logits map for '+output
      print output, prob_logits.get_shape()
      if 'shape' in output:
          reg_shape_logits_list.append(prob_logits)
      if output in ['q1', 'q2', 'q3', 'q4', 'x', 'y']:
          reg_pose_logits_list.append(prob_logits)
      reg_logits_dict[output] = prob_logits
  # reg_logits_concat = tf.concat(reg_logits_list, axis=1) # [car_num_total, 17]
  # reg_logits_concat = tf.where(tf.is_nan(reg_logits_concat), tf.zeros_like(reg_logits_concat)+1e-5, reg_logits_concat) # Hack to process NaN!!!!!!

  # reg_logits_mask = tf.logical_not(tf.is_nan(tf.reduce_sum(reg_logits_concat, axis=1, keepdims=True)))
  areas_masked = outputs_to_areas_N[dataset.output_names[0]]
  masks_float = tf.to_float(tf.not_equal(areas_masked, 0.)) # N; filtered small objects (with zero area after resizing)
  weights_normalized = areas_masked # weights equals area; will be divided by num of all pixels later
  # weights_normalized = tf.ones_like(areas_masked) # NOT weights equals area

  # if FLAGS.val_split == 'test':
  #     scaled_prob_logits_pose = train_utils.scale_for_l1_loss(
  #             tf.gather(reg_logits_concat, [0, 1, 2, 3, 4, 5, 6], axis=3), samples['mask'], samples['mask'], upsample_logits=FLAGS.upsample_logits)
  #     scaled_prob_logits_shape = train_utils.scale_for_l1_loss(
  #             tf.gather(reg_logits_concat, range(7, dataset.SHAPE_DIMS+7), axis=3), samples['mask'], samples['mask'], upsample_logits=FLAGS.upsample_logits)
  #     scaled_prob_logits = tf.concat([scaled_prob_logits_pose, scaled_prob_logits_shape], axis=3)
  #     scaled_prob_logits = tf.identity(scaled_prob_logits, name=is_training_prefix+'scaled_prob_logits_pose_shape_map')
  # return samples['idxs']

  pose_dict_N = tf.gather_nd(samples['pose_dict'], idx_xys) # [N, 7]
  pose_dict_N = tf.identity(pose_dict_N, is_training_prefix+'pose_dict_N')
  rotuvd_dict_N = tf.gather_nd(samples['rotuvd_dict'], idx_xys) # [N, 7]
  rotuvd_dict_N = tf.identity(rotuvd_dict_N, is_training_prefix+'rotuvd_dict_N')
  label_pose_map = tf.identity(logits_cars_to_map(pose_dict_N), name=is_training_prefix+'label_pose_map')
  def within_frame(W, H, u, v):
      within_mask = tf.concat([tf.greater_equal(u, 0), tf.greater_equal(v, 0),
          tf.less_equal(u, W), tf.less_equal(v, H)], axis=1)
      return tf.reduce_all(within_mask, axis=1, keepdims=True)
  def within_range(z_list, zmin, zmax):
      return tf.logical_and(tf.greater_equal(z_list, zmin), tf.less_equal(z_list, zmax))
  rotuvd_dict_N_within = tf.to_float(within_frame(680, 544, tf.gather(rotuvd_dict_N, [4], axis=1), tf.gather(rotuvd_dict_N, [5], axis=1)))
  z_N_within = tf.to_float(within_range(tf.log(tf.gather(rotuvd_dict_N, [6], axis=1)), bin_centers_list[6][0], bin_centers_list[6][-1]))
  masks_float = masks_float * z_N_within # [N, 1] # filtering depth outof range
  weights_normalized = masks_float * weights_normalized
  count_valid = tf.reduce_sum(masks_float)+1e-10

  masks_map_filtered = tf.identity(logits_cars_to_map(masks_float), name=is_training_prefix+'masks_map_filtered')
  masks_map_filtered_rescaled = tf.identity(logits_cars_to_map(masks_float, rescale=True), name=is_training_prefix+'mask_rescaled_float')
  pixels_valid_filtered = tf.reduce_sum(masks_map_filtered_rescaled)+1e-10




  ## ---- uv losses
  if FLAGS.if_uvflow and not(FLAGS.if_depth_only):
    label_uv_map = tf.gather(label_pose_map, [4, 5], axis=3) # (-1, 272, 680, 2)
    label_uv_map = tf.identity(tf.multiply(masks_map_filtered, label_uv_map), name=is_training_prefix+'label_uv_map')
    v_coords = tf.range(tf.shape(label_uv_map)[1])
    u_coords = tf.range(tf.shape(label_uv_map)[2])
    Vs, Us = tf.meshgrid(v_coords, u_coords)
    features_Ys = tf.tile(tf.expand_dims(tf.expand_dims(tf.transpose(Vs + tf.shape(label_uv_map)[1]), -1), 0), [tf.shape(label_uv_map)[0], 1, 1, 1]) # Adding half height because of the crop!
    features_Xs = tf.tile(tf.expand_dims(tf.expand_dims(tf.transpose(Us), -1), 0), [tf.shape(label_uv_map)[0], 1, 1, 1])
    coords_UVs_concat = tf.to_float(tf.concat([features_Xs, features_Ys], axis=3))
    label_uv_flow_map = tf.multiply(masks_map_filtered, label_uv_map - coords_UVs_concat)
    label_uv_flow_map = tf.identity(label_uv_flow_map, name=is_training_prefix+'label_uv_flow_map')

    logits_uv_map = tf.concat([outputs_to_logits_map['x'], outputs_to_logits_map['y']], axis=3)
    coords_UVs_concat_rescaled = tf.image.resize_nearest_neighbor(coords_UVs_concat, [tf.shape(logits_uv_map)[1], tf.shape(logits_uv_map)[2]], align_corners=True)
    logits_uv_map = tf.identity(tf.multiply(masks_map_filtered_rescaled, logits_uv_map), name=is_training_prefix+'logits_uv_map')
    logits_uv_flow_map = tf.identity(tf.multiply(masks_map_filtered_rescaled, logits_uv_map - coords_UVs_concat_rescaled), name=is_training_prefix+'logits_uv_flow_map')

    balance_uv_flow = 1.
    label_uv_map_rescaled = tf.image.resize_nearest_neighbor(label_uv_flow_map, [tf.shape(logits_uv_map)[1], tf.shape(logits_uv_map)[2]], align_corners=True)
    uv_map_diff = tf.multiply(tf.abs(logits_uv_flow_map - label_uv_map_rescaled), masks_map_filtered_rescaled)
    loss_reg_uv_map = tf.reduce_sum(uv_map_diff) / pixels_valid_filtered * balance_uv_flow # L1 loss for uv flow
    loss_reg_uv_map = tf.identity(loss_reg_uv_map, name=is_training_prefix+'loss_reg_uv_map')
    if is_training and not(FLAGS.if_depth_only):
        tf.losses.add_loss(loss_reg_uv_map, loss_collection=tf.GraphKeys.LOSSES)

  ## ---- CLS losses
  label_id_list = []
  loss_slice_crossentropy_list = []
  balance_cls_loss = 1.
  for idx_output, output in enumerate(dataset.output_names):
    if idx_output not in [0, 1, 2, 3, 4,5,6] and not(FLAGS.if_shape): # not adding SHAPE loss
        continue
    if FLAGS.if_depth_only and output != 'z':
        continue

    label_slice = tf.gather(pose_shape_dict_N, [idx_output], axis=1)

    # Add losses for each output names for logging
    prob_logits_slice = tf.gather(prob_logits_pose_shape, [idx_output], axis=1)
    if output != 'z' or ( output == 'z'  and FLAGS.if_depth):
        loss_slice_reg_unsummed = tf.identity(tf.multiply(masks_float, tf.abs(label_slice - prob_logits_slice)), name=is_training_prefix+'loss_slice_reg_vector_'+output)
    else:
        loss_slice_reg_unsummed = tf.identity(tf.multiply(masks_float, tf.abs(1./label_slice - 1./prob_logits_slice)), name=is_training_prefix+'loss_slice_reg_vector_'+output)
    loss_slice_reg = tf.reduce_sum(loss_slice_reg_unsummed) / count_valid # [N, 1]; L1 error
    loss_slice_reg = tf.identity(loss_slice_reg, name=is_training_prefix+'loss_slice_reg_'+output)
    print 'iiiiiiiiiiiiiiiiiiiiiiiiiiii'

    ## Cross-entropy loss for each output http://icode.baidu.com/repos/baidu/personal-code/video_seg_transfer/blob/with_db:Networks/mx_losses.py (L89)
    if output not in ['x', 'y'] or not(FLAGS.if_uvflow):
        print '... adding cls loss for: ', idx_output, output
        bin_centers = bin_centers_list[idx_output]
        if output == 'z' and FLAGS.if_log_depth:
            label_slice = tf.log(label_slice)
            print '.. converting z label from depth to log depth.'
        # label_id_slice = tf.round((label_slice - bin_centers[0]) / bin_size_list[idx_output])
        print bin_bounds_list[idx_output]
        label_id_slice = math_ops._bucketize(label_slice, bin_bounds_list[idx_output]) - 1
        label_id_slice = tf.clip_by_value(label_id_slice, 0, dataset.bin_nums[idx_output]-1)
        tf.assert_greater_equal(label_id_slice, 0, message='label_id not all >=0!')
        tf.assert_less_equal(label_id_slice, dataset.bin_nums[idx_output]-1, message='label_id not all <=dataset.bin_nums[idx_output]-1!')
        if output == 'z':
            label_id_slice = tf.identity(label_id_slice, name=is_training_prefix+'label_id_slice')
            label_slice = tf.identity(label_slice, name=is_training_prefix+'label_log_slice')
        gt_idx = tf.one_hot(tf.reshape(label_id_slice, [-1]), depth=dataset.bin_nums[idx_output], axis=-1)
        if FLAGS.if_log_depth:
            alpha = 15
            weight = [np.exp(-alpha * np.power(bin_centers - x, 2)) for x in bin_centers] # binary multi-class cls loss

            # weight = np.zeros((dataset.bin_nums[idx_output], dataset.bin_nums[idx_output])) # DORN loss
            # for i in range(dataset.bin_nums[idx_output]): weight[i,:i] = 1

            weight = tf.constant(np.asarray(weight,dtype=np.float32))
            lab_l = tf.matmul(gt_idx, weight)
            err_dist = tf.nn.sigmoid_cross_entropy_with_logits(logits=outputs_to_logits_N[output], labels=lab_l)
            loss_slice_crossentropy = tf.reduce_mean(err_dist, 1, keepdims=True)
        else:
            neg_log = -1. * tf.nn.log_softmax(outputs_to_logits_N[output])
            loss_slice_crossentropy = tf.reduce_sum(tf.multiply(gt_idx, neg_log), axis=1, keepdims=True)
        loss_slice_crossentropy= tf.reduce_sum(tf.multiply(weights_normalized, loss_slice_crossentropy)) / pixels_valid_filtered * balance_cls_loss
        loss_slice_crossentropy = tf.identity(loss_slice_crossentropy, name=is_training_prefix+'loss_slice_cls_'+output)
        loss_slice_crossentropy_list.append(loss_slice_crossentropy)
        if is_training:
            tf.losses.add_loss(loss_slice_crossentropy, loss_collection=None)
            # tf.losses.add_loss(loss_slice_crossentropy, loss_collection=tf.GraphKeys.LOSSES)
  # loss_crossentropy = tf.identity(tf.add_n(loss_slice_crossentropy_list), name=is_training_prefix+'loss_cls_ALL')
  loss_crossentropy = tf.identity(tf.constant(0.), name=is_training_prefix+'loss_cls_ALL')

  ## ---- Dense depth loss
  idx_output = 6
  output = 'z_log_dense'
  label_depth_map_rescaled = tf.image.resize_nearest_neighbor(samples['depth'], [masks_map_filtered_rescaled.get_shape()[1], masks_map_filtered_rescaled.get_shape()[2]], align_corners=True)
  depth_rescaled_mask = tf.to_float(tf.where(
      tf.logical_and(label_depth_map_rescaled>np.exp(bin_bounds_list[idx_output][0]), label_depth_map_rescaled<np.exp(bin_bounds_list[idx_output][-1])),
      tf.ones_like(label_depth_map_rescaled),
      tf.zeros_like(label_depth_map_rescaled))) * masks_map_filtered_rescaled
  depth_rescaled_mask = tf.identity(depth_rescaled_mask, name=is_training_prefix+'depth_rescaled_mask_map')
  pixels_valid_filtered_depth_rescaled = tf.reduce_sum(depth_rescaled_mask)+1e-10
  label_depth_map_rescaled = tf.multiply(depth_rescaled_mask, label_depth_map_rescaled)
  label_depth_map_rescaled = tf.identity(label_depth_map_rescaled, name=is_training_prefix+'depth_rescaled_label_map')
  print '... adding Dense cls loss for Depth.'
  bin_centers = bin_centers_list[idx_output]
  label_depth_map_rescaled_log = tf.where(tf.equal(depth_rescaled_mask, 1.), tf.log(label_depth_map_rescaled), tf.zeros_like(label_depth_map_rescaled))
  label_id_slice = tf.clip_by_value(math_ops._bucketize(label_depth_map_rescaled_log, bin_bounds_list[idx_output]) - 1, 0, dataset.bin_nums[idx_output]-1)
  label_id_slice = tf.identity(label_id_slice, name=is_training_prefix+'depth_rescaled_label_id_map')
  if FLAGS.if_log_depth:
      if FLAGS.cls_method == 'multi-cls' or FLAGS.cls_method == 'dorn':
          gt_idx = tf.one_hot(tf.reshape(label_id_slice, [-1]), depth=dataset.bin_nums[idx_output], axis=-1)
          if FLAGS.cls_method == 'multi-cls':
            alpha = 15
            weight = [np.exp(-alpha * np.power(bin_centers - x, 2)) for x in bin_centers] # binary multi-class cls loss
          if FLAGS.cls_method == 'dorn':
            weight = np.zeros((dataset.bin_nums[idx_output], dataset.bin_nums[idx_output])) # DORN loss
            for i in range(dataset.bin_nums[idx_output]): weight[i,:i] = 1
          weight = tf.constant(np.asarray(weight,dtype=np.float32))
          lab_l = tf.reshape(tf.matmul(gt_idx, weight), [-1, tf.shape(label_id_slice)[1], tf.shape(label_id_slice)[2], dataset.bin_nums[idx_output]])
          err_dist = tf.nn.sigmoid_cross_entropy_with_logits(logits=outputs_to_logits_map[output], labels=lab_l)
          loss_slice_crossentropy = tf.reduce_mean(err_dist, -1, keepdims=True)
      elif FLAGS.cls_method == 'cls':
          gt_idx = tf.one_hot(tf.squeeze(label_id_slice, -1), depth=dataset.bin_nums[idx_output], axis=-1)
          neg_log = -1. * tf.nn.log_softmax(outputs_to_logits_map[output])
          loss_slice_crossentropy = tf.multiply(gt_idx, neg_log)
      print '-- [CLS LOSS]: '+FLAGS.cls_method
  else:
      raise ValueError('cls_method not supported!')
  loss_slice_crossentropy = tf.identity(tf.multiply(depth_rescaled_mask, loss_slice_crossentropy), name=is_training_prefix+'depth_rescaled_cls_error_map')
  loss_slice_crossentropy = tf.reduce_sum(loss_slice_crossentropy) / pixels_valid_filtered_depth_rescaled * balance_cls_loss  # cls loss for dense depth map
  loss_slice_crossentropy = tf.identity(loss_slice_crossentropy, name=is_training_prefix+'loss_slice_cls_dense_z')
  if is_training:
    # tf.losses.add_loss(loss_slice_crossentropy, loss_collection=None)
    tf.losses.add_loss(loss_slice_crossentropy, loss_collection=tf.GraphKeys.LOSSES)
  prob_logits_depth_map = tf.identity(tf.multiply(depth_rescaled_mask, tf.exp(reg_logits_dict['z_log_dense'])), name=is_training_prefix+'depth_rescaled_logit_map')
  loss_depth_map_l1 = tf.identity(tf.abs(prob_logits_depth_map - label_depth_map_rescaled), name=is_training_prefix+'depth_rescaled_reg_error_map')

  label_depth_map_object_rescaled = tf.image.resize_nearest_neighbor(tf.slice(label_pose_map, [0, 0, 0, 6], [-1, -1, -1, 1]), [masks_map_filtered_rescaled.get_shape()[1], masks_map_filtered_rescaled.get_shape()[2]], align_corners=True)
  label_depth_map_object_rescaled_log = tf.where(tf.equal(depth_rescaled_mask, 1.), tf.log(label_depth_map_object_rescaled), tf.zeros_like(label_depth_map_object_rescaled))
  label_depth_map_object_rescaled_log_offset = label_depth_map_object_rescaled_log - label_depth_map_rescaled_log
  label_depth_map_object_rescaled_log_offset = tf.identity(tf.multiply(depth_rescaled_mask, label_depth_map_object_rescaled_log_offset), name=is_training_prefix+'depth_log_offset_rescaled_label_map')
  prob_logits_depth_map_object_rescaled_log_offset = tf.identity(tf.multiply(depth_rescaled_mask, reg_logits_dict['z_log_offset']), name=is_training_prefix+'depth_log_offset_rescaled_logit_map')
  depth_log_offset_map_diff = tf.multiply(tf.abs(prob_logits_depth_map_object_rescaled_log_offset - label_depth_map_object_rescaled_log_offset), depth_rescaled_mask)
  loss_depth_log_offset_map_l1 = tf.reduce_sum(depth_log_offset_map_diff) / pixels_valid_filtered_depth_rescaled * balance_cls_loss # L1 loss for depth offset map
  loss_depth_log_offset_map_l1 = tf.identity(loss_depth_log_offset_map_l1, name=is_training_prefix+'loss_slice_l1_offset_z')
  if is_training:
      tf.losses.add_loss(loss_depth_log_offset_map_l1, loss_collection=tf.GraphKeys.LOSSES)

  logits_N_log, weightsum_N, areas_N = model._aggre_logits('z_object',
          reg_logits_dict['z_log_dense'] + outputs_to_logits_map['z_log_offset'],
          # outputs_to_weights_map['z_log_dense'] + outputs_to_weights_map['z_log_offset'],
          outputs_to_weights_map['z_log_offset'],
          samples['seg_rescaled'],
          samples['car_nums'],
          1)
  outputs_to_logits_N['z_object'] = tf.exp(logits_N_log)
  outputs_to_areas_N['z_object'] = areas_N
  outputs_to_weightsum_N['z_object'] = weightsum_N
  outputs_to_weights_map['z_object'] = outputs_to_weights_map['z_log_dense'] + outputs_to_weights_map['z_log_offset']
  reg_pose_logits_list.append(tf.exp(logits_N_log))
  prob_logits_pose = tf.concat(reg_pose_logits_list, axis=1)
  prob_logits_pose = tf.identity(prob_logits_pose, name=is_training_prefix+'prob_logits_pose')
  if FLAGS.if_uvflow:
      prob_logits_pose_xy_from_uv = train_utils.rotuvd_dict_N_2_quat_xy_dinvd_dict_N(FLAGS, prob_logits_pose) # WILL NEED DEPTH FINALZED FIRST BEFORE UV!

  ## ---- Regression loss for pose
  balance_rot_reg_loss = 10.
  balance_trans_reg_loss = 1.
  _, prob_logits_pose, rot_q_angle_error, trans_sqrt_error, depth_diff_abs_error, depth_relative_error, trans_loss_error, rot_q_loss_error, trans_diff_metric_abs = train_utils.add_my_pose_loss_cars(
          FLAGS,
          prob_logits_pose,
          rotuvd_dict_N if FLAGS.if_uvflow else pose_dict_N,
          prob_logits_pose_xy_from_uv if FLAGS.if_uvflow else prob_logits_pose,
          pose_dict_N,
          masks_float,
          weights_normalized,
          # / tf.log(tf.slice(rotuvd_dict_N, [0, 6], [-1, 1])),
          balance_rot=balance_rot_reg_loss,
          balance_trans=balance_trans_reg_loss,
          upsample_logits=FLAGS.upsample_logits,
          name=is_training_prefix + 'loss_reg',
          is_training_prefix = is_training_prefix,
          loss_collection=tf.GraphKeys.LOSSES if is_training else None,
          if_depth=FLAGS.if_depth,
          logits_depth_N_log=logits_N_log,
          labels_depth_N_log=tf.log(tf.clip_by_value(tf.slice(rotuvd_dict_N, [0, 6], [-1, 1]), 1.5, 350.)))
  if not(FLAGS.if_depth_only):
      rot_q_loss_error_map = tf.identity(logits_cars_to_map(rot_q_loss_error), name=is_training_prefix+'rot_q_loss_error_map')
      rot_q_angle_error_map = tf.identity(logits_cars_to_map(rot_q_angle_error), name=is_training_prefix+'rot_q_angle_error_map')
  trans_loss_error_map = tf.identity(logits_cars_to_map(trans_loss_error), name=is_training_prefix+'trans_loss_error_map')
  trans_sqrt_error_map = tf.identity(logits_cars_to_map(trans_sqrt_error), name=is_training_prefix+'trans_sqrt_error_map')
  depth_diff_abs_error_map = tf.identity(logits_cars_to_map(depth_diff_abs_error), name=is_training_prefix+'depth_diff_abs_error_map')
  depth_relative_error_map = tf.identity(logits_cars_to_map(depth_relative_error), name=is_training_prefix+'depth_relative_error_map')
  trans_sqrt_error = tf.identity(trans_sqrt_error, name=is_training_prefix+'trans_sqrt_error')
  trans_loss_error = tf.identity(trans_loss_error, name=is_training_prefix+'trans_loss_error')
  trans_diff_metric_abs = tf.identity(trans_diff_metric_abs, name=is_training_prefix+'trans_diff_metric_abs')
  depth_diff_abs_error = tf.identity(depth_diff_abs_error, name=is_training_prefix+'depth_diff_abs_error')
  depth_relative_error = tf.identity(depth_relative_error, name=is_training_prefix+'depth_relative_error')


  ## ---- Regression loss for shape
  balance_shape_loss = 1.
  shape_dict_N = tf.gather_nd(samples['shape_dict'], idx_xys)
  _, prob_logits_shape = train_utils.add_l1_regression_loss_cars(
          tf.concat(reg_shape_logits_list, axis=1),
          shape_dict_N,
          masks_float,
          weights_normalized,
          balance=balance_shape_loss,
          upsample_logits=FLAGS.upsample_logits,
          name=is_training_prefix + 'loss_reg_shape',
          loss_collection=tf.GraphKeys.LOSSES if (is_training and FLAGS.if_shape) else None
          )
  prob_logits_pose_shape = tf.concat([prob_logits_pose, prob_logits_shape], axis=1)
  prob_logits_pose_shape = tf.identity(prob_logits_pose_shape, name=is_training_prefix+'prob_logits_pose_shape_cars')
  if FLAGS.if_uvflow:
      pose_shape_dict_N = tf.concat([rotuvd_dict_N, shape_dict_N], axis=1)
  else:
      pose_shape_dict_N = tf.concat([pose_dict_N, shape_dict_N], axis=1)


  if FLAGS.save_summaries_images:
    label_shape_map = logits_cars_to_map(shape_dict_N)
    label_pose_shape_map = tf.identity(tf.concat([label_pose_map, label_shape_map], axis=3), name=is_training_prefix+'label_pose_shape_map')
    prob_logits_pose_shape_map = logits_cars_to_map(prob_logits_pose_shape)
    prob_logits_pose_shape_map = tf.identity(prob_logits_pose_shape_map, name=is_training_prefix+'prob_logits_pose_shape_map')


  ## --- Visualize weight map
  for output in dataset.output_names+['z_object']:
      if output in ['z_log_dense', 'z_log_offset']: continue
      weightsum_map = logits_cars_to_map(tf.reciprocal(outputs_to_weightsum_N[output]+1e-10), rescale=True)
      outputs_to_weights_map_exp = tf.exp(outputs_to_weights_map[output])
      outputs_to_weights_map[output] = tf.multiply(masks_map_filtered_rescaled, tf.multiply(outputs_to_weights_map_exp, weightsum_map))
      outputs_to_weights_map[output] = tf.identity(outputs_to_weights_map[output], name=is_training_prefix+'%s_weights_map'%output)

  ## --- Shape summaries
  if FLAGS.if_summary_shape_metrics and FLAGS.if_shape:
      shape_sim_mat = np.loadtxt('./deeplab/dataset-api/car_instance/sim_mat.txt')
      assert shape_sim_mat.shape[0] == shape_sim_mat.shape[1]
      num_cads = shape_sim_mat.shape[0]
      prob_logits_shape_expanded = tf.tile(tf.expand_dims(prob_logits_shape, axis=1), [1, num_cads, 1])
      codes_cons = tf.constant(np.transpose(codes), dtype=tf.float32) # [79, 10]
      codes_expanded = tf.tile(tf.expand_dims(codes_cons, 0), [tf.shape(prob_logits_shape_expanded)[0], 1, 1])
      shape_l2_error_per_cls = tf.reduce_sum(tf.square(prob_logits_shape_expanded - codes_expanded), axis=2)
      shape_id_map_predicts = tf.expand_dims(tf.argmin(shape_l2_error_per_cls, axis=1), axis=-1) # [num_cars, 1]

      shape_id_dict_N = tf.gather_nd(samples['shape_id_dict'], idx_xys)
      shape_cls_metric_error_cars = tf.gather_nd(tf.constant(shape_sim_mat, dtype=tf.float32),
              tf.stack([shape_id_dict_N, shape_id_map_predicts], axis=-1)) # [num_cars, 1]
      if FLAGS.save_summaries_images:
        shape_cls_metric_error_map = tf.identity(logits_cars_to_map(shape_cls_metric_error_cars), name=is_training_prefix+'shape_id_sim_map')

      shape_cls_metric_loss_check = tf.reduce_sum(shape_cls_metric_error_cars * masks_float) / count_valid
      shape_cls_metric_loss_check = tf.identity(shape_cls_metric_loss_check, name=is_training_prefix+'loss_all_shape_id_cls_metric')

  return samples[common.IMAGE_NAME], outputs_to_logits_N['z_object'], outputs_to_weights_map, seg_one_hots_list, weights_normalized, areas_masked, samples['car_nums'], car_nums_list, idx_xys, \
          tf.multiply(masks_float, prob_logits_pose_xy_from_uv) if FLAGS.if_uvflow else prob_logits_pose, \
          tf.multiply(masks_float, pose_dict_N), \
          tf.multiply(masks_float, prob_logits_pose), \
          tf.multiply(masks_float, rotuvd_dict_N), \
          masks_float, \
          tf.multiply(masks_map_filtered, label_uv_map) if (FLAGS.if_uvflow and not(FLAGS.if_depth_only)) else masks_map_filtered, \
          tf.multiply(masks_map_filtered_rescaled, logits_uv_map) if (FLAGS.if_uvflow and not(FLAGS.if_depth_only)) else masks_map_filtered
Exemplo n.º 30
0
 def testEmptyFloat(self):
     op = math_ops._bucketize(array_ops.zeros([0, 3], dtype=dtypes.float32),
                              boundaries=[])
     expected_out = np.zeros([0, 3], dtype=np.float32)
     with self.session():
         self.assertAllEqual(expected_out, self.evaluate(op))
Exemplo n.º 31
0
 def testBoundariesNotList(self):
   with self.test_session():
     with self.assertRaisesRegexp(TypeError, "Expected list.*"):
       p = array_ops.placeholder(dtypes.int32)
       with self.test_scope():
         math_ops._bucketize(p, boundaries=0)