Example #1
0
    def __init__(self, name, inputs, tower_setup, n_convs=2, n_features=None, dilations=None, strides=None,
                 filter_size=None, activation="relu", batch_norm_decay=BATCH_NORM_DECAY_DEFAULT, l2=L2_DEFAULT):
        super(ResidualUnit2, self).__init__()
        # TODO: dropout
        curr, n_features_inp = prepare_input(inputs)
        res = curr
        assert n_convs >= 1, n_convs

        if dilations is not None:
            assert strides is None
        elif strides is None:
            strides = [[1, 1]] * n_convs
        if filter_size is None:
            filter_size = [[3, 3]] * n_convs
        if n_features is None:
            n_features = n_features_inp
        if not isinstance(n_features, list):
            n_features = [n_features] * n_convs

        with tf.variable_scope(name):
            curr = self.create_and_apply_batch_norm(curr, n_features_inp, batch_norm_decay, tower_setup, "bn0")
            curr = get_activation(activation)(curr)

            if strides is None:
                strides_res = [1, 1]
            else:
                strides_res = numpy.prod(strides, axis=0).tolist()
            if (n_features[-1] != n_features_inp) or (strides_res != [1, 1]):
                W0 = self.create_weight_variable("W0", [1, 1] + [n_features_inp, n_features[-1]], l2, tower_setup)
                if dilations is None:
                    res = conv2d(curr, W0, strides_res)
                else:
                    res = conv2d(curr, W0)

            W1 = self.create_weight_variable("W1", filter_size[0] + [n_features_inp, n_features[0]], l2, tower_setup)
            if dilations is None:
                curr = conv2d(curr, W1, strides[0])
            else:
                curr = conv2d_dilated(curr, W1, dilations[0])
            for idx in range(1, n_convs):
                curr = self.create_and_apply_batch_norm(curr, n_features[idx - 1], batch_norm_decay,
                                                        tower_setup, "bn" + str(idx + 1))
                curr = get_activation(activation)(curr)
                Wi = self.create_weight_variable("W" + str(idx + 1),
                                                 filter_size[idx] + [n_features[idx - 1], n_features[idx]],
                                                 l2, tower_setup)
                if dilations is None:
                    curr = conv2d(curr, Wi, strides[idx])
                else:
                    curr = conv2d_dilated(curr, Wi, dilations[idx])

        curr += res
        self.outputs = [curr]
Example #2
0
    def __init__(self, name, inputs, n_features, tower_setup, old_order=False, filter_size=(3, 3),
                 strides=(1, 1), dilation=None, pool_size=(1, 1), pool_strides=None, activation="relu", dropout=0.0,
                 batch_norm=False, bias=False, batch_norm_decay=BATCH_NORM_DECAY_DEFAULT, l2=L2_DEFAULT):
        super(Conv, self).__init__()
        # mind the order of dropout, conv, activation and batchnorm!
        # default: batchnorm -> activation -> dropout -> conv -> pool
        # if old_order: dropout -> conv -> batchnorm -> activation -> pool

        curr, n_features_inp = prepare_input(inputs)

        filter_size = list(filter_size)
        strides = list(strides)
        pool_size = list(pool_size)
        if pool_strides is None:
            pool_strides = pool_size

        with tf.variable_scope(name):
            W = self.create_weight_variable("W", filter_size + [n_features_inp, n_features], l2, tower_setup)
            b = None
            if bias:
                b = self.create_bias_variable("b", [n_features], tower_setup)

            if old_order:
                curr = apply_dropout(curr, dropout)
                if dilation is None:
                    curr = conv2d(curr, W, strides)
                else:
                    curr = conv2d_dilated(curr, W, dilation)
                if bias:
                    curr += b
                if batch_norm:
                    curr = self.create_and_apply_batch_norm(curr, n_features, batch_norm_decay, tower_setup)
                curr = get_activation(activation)(curr)
            else:
                if batch_norm:
                    curr = self.create_and_apply_batch_norm(curr, n_features_inp, batch_norm_decay, tower_setup)
                curr = get_activation(activation)(curr)
                curr = apply_dropout(curr, dropout)
                if dilation is None:
                    curr = conv2d(curr, W, strides)
                else:
                    curr = conv2d_dilated(curr, W, dilation)
                if bias:
                    curr += b

            if pool_size != [1, 1]:
                curr = max_pool(curr, pool_size, pool_strides)
        self.outputs = [curr]
Example #3
0
 def __init__(self,
              name,
              inputs,
              n_features,
              tower_setup,
              activation="relu",
              dropout=0.0,
              batch_norm=False,
              batch_norm_decay=BATCH_NORM_DECAY_DEFAULT,
              l2=L2_DEFAULT):
     super(FullyConnected, self).__init__()
     inp, n_features_inp = prepare_collapsed_input_and_dropout(
         inputs, dropout)
     with tf.variable_scope(name):
         if batch_norm:
             inp = tf.expand_dims(inp, axis=0)
             inp = tf.expand_dims(inp, axis=0)
             inp = self.create_and_apply_batch_norm(inp, n_features_inp,
                                                    batch_norm_decay,
                                                    tower_setup)
             inp = tf.squeeze(inp, axis=[0, 1])
         W = self.create_weight_variable("W", [n_features_inp, n_features],
                                         l2, tower_setup)
         b = self.create_bias_variable("b", [n_features], tower_setup)
         z = tf.matmul(inp, W) + b
         h = get_activation(activation)(z)
     self.outputs = [h]
Example #4
0
    def __init__(self,
                 name,
                 inputs,
                 tower_setup,
                 n_features,
                 concat,
                 activation="relu",
                 filter_size=(3, 3),
                 batch_norm_decay=BATCH_NORM_DECAY_DEFAULT,
                 l2=L2_DEFAULT):
        super(Upsampling, self).__init__()
        filter_size = list(filter_size)
        assert isinstance(concat, list)
        assert len(concat) > 0
        curr, n_features_inp = prepare_input(inputs)
        concat_inp, n_features_concat = prepare_input(concat)

        curr = tf.image.resize_nearest_neighbor(curr,
                                                tf.shape(concat_inp)[1:3])
        curr = tf.concat([curr, concat_inp], axis=3)
        n_features_curr = n_features_inp + n_features_concat

        with tf.variable_scope(name):
            W = self.create_weight_variable(
                "W", filter_size + [n_features_curr, n_features], l2,
                tower_setup)
            b = self.create_bias_variable("b", [n_features], tower_setup)
            curr = conv2d(curr, W) + b
            curr = get_activation(activation)(curr)

        self.outputs = [curr]
Example #5
0
 def __init__(self, name, inputs, tower_setup, activation="relu", batch_norm_decay=BATCH_NORM_DECAY_DEFAULT):
     super(Collapse, self).__init__()
     curr, n_features_inp = prepare_input(inputs)
     with tf.variable_scope(name):
         inp = self.create_and_apply_batch_norm(curr, n_features_inp, batch_norm_decay, tower_setup)
         h_act = get_activation(activation)(inp)
         out = global_avg_pool(h_act)
     self.outputs = [out]
Example #6
0
  def __init__(self, name, inputs, targets, n_classes, void_label, tower_setup, filter_size=(1, 1),
               input_activation=None, dilation=None, resize_targets=False, resize_logits=False, loss="ce",
               fraction=None, l2=L2_DEFAULT, dropout=0.0):
    super(SegmentationSoftmax, self).__init__()
    assert targets.get_shape().ndims == 4, targets.get_shape()
    assert not (resize_targets and resize_logits)
    inp, n_features_inp = prepare_input(inputs)

    filter_size = list(filter_size)

    with tf.variable_scope(name):
      if input_activation is not None:
        inp = get_activation(input_activation)(inp)

      inp = apply_dropout(inp, dropout)

      self.W, self.b, self.W_adjustable, self.b_adjustable, self.n_classes_current, W, b = self.create_weights(
        n_classes, filter_size, n_features_inp, l2, tower_setup)
      self.adjustable_output_assign_data = self._create_adjustable_output_assign_data(tower_setup)
      if self.n_classes_current is None:
        self.n_classes_current = n_classes

      if dilation is None:
        y_pred = conv2d(inp, W) + b
      else:
        y_pred = conv2d_dilated(inp, W, dilation) + b
      self.outputs = [tf.nn.softmax(y_pred, -1, 'softmax')]

      if resize_targets:
        targets = tf.image.resize_nearest_neighbor(targets, tf.shape(y_pred)[1:3])
      if resize_logits:
        y_pred = tf.image.resize_images(y_pred, tf.shape(targets)[1:3])

      pred = tf.argmax(y_pred, axis=3)
      targets = tf.cast(targets, tf.int64)
      targets = tf.squeeze(targets, axis=3)

      # TODO: Void label is not considered in the iou calculation.
      if void_label is not None:
        # avoid nan by replacing void label by 0
        # note: the loss for these cases is multiplied by 0 below
        void_label_mask = tf.equal(targets, void_label)
        no_void_label_mask = tf.logical_not(void_label_mask)
        targets = tf.where(void_label_mask, tf.zeros_like(targets), targets)
      else:
        no_void_label_mask = None

      self.measures = self.create_measures(n_classes, pred, targets)
      self.loss = self.create_loss(loss, fraction, no_void_label_mask, targets, tower_setup, void_label, y_pred)
      self.add_scalar_summary(self.loss, "loss")
Example #7
0
  def __init__(self, name, inputs, targets, n_classes, n_features, tower_setup, activation="linear", dropout=0.0,
               batch_norm=False,
               batch_norm_decay=BATCH_NORM_DECAY_DEFAULT, l2=L2_DEFAULT):
    super(FullyConnectedWithTripletLoss, self).__init__()
    self.measures = {}
    inp, n_features_inp = prepare_collapsed_input_and_dropout(inputs, dropout)
    with tf.variable_scope(name):
      if batch_norm:
        inp = tf.expand_dims(inp, axis=0)
        inp = tf.expand_dims(inp, axis=0)
        inp = self.create_and_apply_batch_norm(inp, n_features_inp, batch_norm_decay, tower_setup)
        inp = tf.squeeze(inp, axis=[0, 1])
      W = self.create_weight_variable("W", [n_features_inp, n_features], l2, tower_setup)
      b = self.create_bias_variable("b", [n_features], tower_setup)
      z = tf.matmul(inp, W) + b
      h = get_activation(activation)(z)
      self.outputs = [h]

      size = smart_shape(h)[0]

      def get_loss(idx):
        anchor = h[idx, :]
        anchor_class = targets[idx]
        class_division = tf.cast(tf.equal(targets, anchor_class), tf.int32)
        partitioned_output = tf.dynamic_partition(h, class_division, 2)

        # Positives
        positive_distances = tf.abs(anchor - partitioned_output[1])
        pos_dis_val = tf.norm(positive_distances,axis=1)
        hardest_positive_idx = tf.arg_max(pos_dis_val,0)
        pos_div_size = smart_shape(partitioned_output[1])[0]
        pos_divider = tf.one_hot(hardest_positive_idx,pos_div_size,dtype=tf.int32)
        hardest_positive = tf.dynamic_partition(positive_distances,pos_divider,2)[1]
        hardest_positive_class = tf.gather(targets, hardest_positive_idx)
        hardest_positive = tf.norm(hardest_positive, axis=1)

        # Negatives
        negative_distances = tf.abs(anchor - partitioned_output[0])
        neg_dis_val = tf.norm(negative_distances, axis=1)
        hardest_negative_idx = tf.arg_min(neg_dis_val,0)
        neg_div_size = smart_shape(partitioned_output[0])[0]
        neg_divider = tf.one_hot(hardest_negative_idx,neg_div_size,dtype=tf.int32)
        hardest_negative = tf.dynamic_partition(negative_distances,neg_divider,2)[1]
        hardest_negative_class = tf.gather(targets,hardest_negative_idx)
        hardest_negative = tf.norm(hardest_negative, axis=1)

        # margin = 1
        # loss = tf.maximum(0., margin + hardest_positive - hardest_negative)
        loss = tf.log1p(tf.exp(hardest_positive - hardest_negative))

        return loss

      loss = tf.map_fn(get_loss, tf.range(0, size), dtype=tf.float32)

      self.loss = tf.reduce_sum(loss)
      self.add_scalar_summary(self.loss, "loss")
      self.n_features = n_features

      ## Print - debug example code
      # def test(a):
      #   print(a)
      #   return numpy.array([5], dtype="int32")
      #
      # t, = tf.py_func(test, [WHATEVER YOU WANT TO PRINT], [tf.int32])
      # with tf.control_dependencies([t]):
      #   loss = tf.identity(loss)