Python tensorflow.contrib.slim 模块,softmax() 实例源码

我们从Python开源项目中,提取了以下8个代码示例,用于说明如何使用tensorflow.contrib.slim.softmax()

项目:social-scene-understanding    作者:cvlab-epfl    | 项目源码 | 文件源码
def attention_weights(features, tau=10.0, num_hidden=512):
  """computing attention weights
  Args:
    features: [B,N,F]
  Returns:
    [B,N] tensor with soft attention weights for each sample
  """
  B, N, F = features.get_shape().as_list()

  with tf.variable_scope('attention'):
    x = tf.reshape(features, [-1, F])
    x = slim.fully_connected(x, num_hidden, scope='fc0')
    x = slim.fully_connected(x, 1, activation_fn=None, scope='fc1')
    x = tf.reshape(x, features.get_shape()[:2])
    alpha = tf.reshape(slim.softmax(x / tau), [B,N,])
  return alpha
项目:seglink    作者:dengdan    | 项目源码 | 文件源码
def _add_seglink_layers(self):
        all_seg_scores = []
        all_seg_offsets = []
        all_within_layer_link_scores = []
        all_cross_layer_link_scores  = []
        for layer_name in self.feat_layers:
            with tf.variable_scope(layer_name):
                seg_scores, seg_offsets, within_layer_link_scores, cross_layer_link_scores = self._build_seg_link_layer(layer_name)
            all_seg_scores.append(seg_scores)
            all_seg_offsets.append(seg_offsets)
            all_within_layer_link_scores.append(within_layer_link_scores)
            all_cross_layer_link_scores.append(cross_layer_link_scores)

        self.seg_score_logits = reshape_and_concat(all_seg_scores) # (batch_size, N, 2)
        self.seg_scores = slim.softmax(self.seg_score_logits) # (batch_size, N, 2)
        self.seg_offsets = reshape_and_concat(all_seg_offsets) # (batch_size, N, 5)
        self.cross_layer_link_scores = reshape_and_concat(all_cross_layer_link_scores)  # (batch_size, 8N, 2)
        self.within_layer_link_scores = reshape_and_concat(all_within_layer_link_scores)  # (batch_size, 4(N - N_conv4_3), 2)
        self.link_score_logits = tf.concat([self.within_layer_link_scores, self.cross_layer_link_scores], axis = 1)
        self.link_scores = slim.softmax(self.link_score_logits)

        tf.summary.histogram('link_scores', self.link_scores)
        tf.summary.histogram('seg_scores', self.seg_scores)
项目:social-scene-understanding    作者:cvlab-epfl    | 项目源码 | 文件源码
def det_net(features, num_resnet_blocks, num_resnet_features,
            num_keep, in_size,
            nms_kind='greedy',
            scope=None):

  with tf.variable_scope(scope, 'DetNet'):
    out_size = features.get_shape()[1:3]

    x = nnutil.stack(features,
                     num_resnet_blocks,
                     num_resnet_features,
                     downsample=False)

    with tf.variable_scope('seg'):
      seg_logits = slim.conv2d(x, 2, [1, 1],
                               activation_fn=None,
                               weights_initializer=tf.random_normal_initializer(stddev=1e-1),
                               scope='logits')
      seg_preds = slim.softmax(seg_logits)

    with tf.variable_scope('reg'):
      # TODO: use reg masks instead
      reg_preds = slim.conv2d(x, 4, [1, 1],
                              weights_initializer=tf.random_normal_initializer(stddev=1e-3),
                              activation_fn=tf.nn.relu,
                              scope='reg_preds')

    with tf.variable_scope('boxes'):
      boxes_proposals = reg_to_boxes(reg_preds, in_size, out_size)
      boxes_preds = compute_detections_batch(seg_preds, boxes_proposals,
                                             num_keep, nms_kind=nms_kind)

  return seg_preds, reg_preds, boxes_proposals, boxes_preds
项目:vessel-classification    作者:GlobalFishingWatch    | 项目源码 | 文件源码
def build(self, net):
        self.logits = slim.fully_connected(
            net, self.num_classes, activation_fn=None)
        self.prediction = slim.softmax(self.logits)
项目:vessel-classification    作者:GlobalFishingWatch    | 项目源码 | 文件源码
def build_from_logits(self, logits):
        self.logits = logits
        self.prediction = slim.softmax(self.logits)
项目:tensorflow_ocr    作者:BowieHsu    | 项目源码 | 文件源码
def loss(y_true_pixel, y_pred_pixel,
         y_true_link, y_pred_link,
         training_mask):
    '''
    return pixel loss and link loss 
    add OHEM mode
    '''

    pixel_shape = tf.shape(y_pred_pixel)
    pixel_label = tf.cast(tf.reshape(y_true_pixel,[pixel_shape[0],-1]), dtype = tf.int32)
    pixel_pred = tf.reshape(y_pred_pixel, [pixel_shape[0],-1, 2])

    pixel_scores = slim.softmax(pixel_pred)
    pixel_neg_scores = pixel_scores[:,:,0]
    pixel_pos_mask, pixel_neg_mask = get_pos_and_neg_masks(pixel_label)

    pixel_selected_mask = OHNM_batch(14, pixel_neg_scores, pixel_pos_mask, pixel_neg_mask)
    n_seg_pos = tf.reduce_sum(tf.cast(pixel_pos_mask, tf.float32))
    # classification_loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(labels = pixel_label, logits = pixel_pred))
    # classification_loss *= 2

    #cls_mining_loss_function
    with tf.name_scope('ohem_pixel_loss'):
        def has_pos():
            pixel_loss = tf.nn.sparse_softmax_cross_entropy_with_logits(logits = pixel_pred, labels = pixel_label)
            return tf.reduce_sum(pixel_loss * pixel_selected_mask)/n_seg_pos
        def no_pos():
            return tf.constant(.0)

        classification_loss = tf.cond(n_seg_pos > 0, has_pos, no_pos)

    #link_pos and link_neg loss function
    link_shape = tf.shape(y_pred_pixel)
    total_link_loss = []
    with tf.name_scope('link_loss'):
        for i in range(8):
            y_link = y_true_link[:,:,:,i]
            pred_link = y_pred_link[:, :, :, 2 * i: 2 * (i + 1)]
            link_label = tf.cast(tf.reshape(y_link, [-1]), dtype = tf.int32)
            link_pred = tf.reshape(pred_link, [-1, 2])
            link_loss = tf.nn.sparse_softmax_cross_entropy_with_logits(logits = link_pred, labels = link_label)
            link_pos_mask, link_neg_mask = get_pos_and_neg_masks(link_label)
            W_pixel = tf.reshape(pixel_selected_mask, [-1])
            W_link_pos = tf.cast(link_pos_mask, dtype = tf.float32) * W_pixel 
            W_link_neg = tf.cast(link_neg_mask, dtype = tf.float32) * W_pixel 
            link_pos_n = tf.reduce_sum(tf.cast(W_link_pos, dtype = tf.float32))
            link_neg_n = tf.reduce_sum(tf.cast(W_link_neg, dtype = tf.float32))

            link_pos_loss = tf.reduce_sum(link_loss * W_link_pos)/link_pos_n
            link_neg_loss = tf.reduce_sum(link_loss * W_link_neg)/link_neg_n
            total_link_loss.append(link_pos_loss + link_neg_loss)

    weight_link_loss = tf.reduce_sum(total_link_loss)

    tf.summary.scalar('classification_loss', classification_loss)
    tf.summary.scalar('link_loss', weight_link_loss)

    return weight_link_loss + 2 * classification_loss
项目:social-scene-understanding    作者:cvlab-epfl    | 项目源码 | 文件源码
def _construct_sequence(batch):
        hidden, boxes = batch
        # initializing the state with features
        states = [hidden[0]]
        # TODO: make this dependent on the data
        # TODO: make it with scan ?
        for t in range(1, T):
          # find the matching boxes. TODO: try with the soft matching function
          if c.match_kind == 'boxes':
            dists = nnutil.cdist(boxes[t-1], boxes[t])
            idxs = tf.argmin(dists, 1, 'idxs')
            state_prev = tf.gather(states[t-1], idxs)
          elif c.match_kind == 'hidden':
            # TODO: actually it makes more sense to compare on states
            dists = nnutil.cdist(hidden[t-1], hidden[t])
            idxs = tf.argmin(dists, 1, 'idxs')
            state_prev = tf.gather(states[t-1], idxs)
          elif c.match_kind == 'hidden-soft':
            dists = nnutil.cdist(hidden[t-1], hidden[t])
            weights = slim.softmax(dists)
            state_prev = tf.matmul(weights, states[t-1])
          else:
            raise RuntimeError('Unknown match_kind: %s' % c.match_kind)

          def _construct_update(reuse):
            state = tf.concat(1, [state_prev, hidden[t]])
            # TODO: initialize jointly
            reset = slim.fully_connected(state, NFH, tf.nn.sigmoid,
                                         reuse=reuse,
                                         scope='reset')
            step = slim.fully_connected(state, NFH, tf.nn.sigmoid,
                                        reuse=reuse,
                                        scope='step')
            state_r = tf.concat(1, [reset * state_prev, hidden[t]])
            state_up = slim.fully_connected(state_r, NFH, tf.nn.tanh,
                                            reuse=reuse,
                                            scope='state_up')
            return state_up, step
          try:
            state_up, step = _construct_update(reuse=True)
          except ValueError:
            state_up, step = _construct_update(reuse=False)

          state = step * state_up + (1.0 - step) * state_prev
          states.append(state)
        return tf.pack(states)
项目:sact    作者:mfigurnov    | 项目源码 | 文件源码
def resnet_v2(inputs,
              blocks,
              num_classes=None,
              global_pool=True,
              model_type='vanilla',
              scope=None,
              reuse=None,
              end_points=None):
  with tf.variable_scope(scope, 'resnet_v2', [inputs], reuse=reuse) as sc:
    if end_points is None:
      end_points = {}
    end_points['inputs'] = inputs
    end_points['flops'] = end_points.get('flops', 0)
    net = inputs
    # We do not include batch normalization or activation functions in conv1
    # because the first ResNet unit will perform these. Cf. Appendix of [2].
    with slim.arg_scope([slim.conv2d], activation_fn=None, normalizer_fn=None):
      net, current_flops = flopsometer.conv2d_same(
          net, 64, 7, stride=2, scope='conv1')
      end_points['flops'] += current_flops
    net = slim.max_pool2d(net, [3, 3], stride=2, scope='pool1')
    # Early stopping is broken in distributed training.
    net, end_points = resnet_act.stack_blocks(
        net,
        blocks,
        model_type=model_type,
        end_points=end_points)

    if global_pool or num_classes is not None:
      # This is needed because the pre-activation variant does not have batch
      # normalization or activation functions in the residual unit output. See
      # Appendix of [2].
      net = slim.batch_norm(net, activation_fn=tf.nn.relu, scope='postnorm')

    if global_pool:
      # Global average pooling.
      net = tf.reduce_mean(net, [1, 2], name='pool5', keep_dims=True)

    if num_classes is not None:
      net, current_flops = flopsometer.conv2d(
          net,
          num_classes, [1, 1],
          activation_fn=None,
          normalizer_fn=None,
          scope='logits')
      end_points['flops'] += current_flops
      end_points['predictions'] = slim.softmax(net, scope='predictions')
    return net, end_points