Python tensorflow 模块,newaxis() 实例源码

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

项目:Master-R-CNN    作者:Mark110    | 项目源码 | 文件源码
def _add_jittered_boxes(rois, scores, batch_inds, gt_boxes, jitter=0.1):
    ws = gt_boxes[:, 2] - gt_boxes[:, 0]
    hs = gt_boxes[:, 3] - gt_boxes[:, 1]
    shape = tf.shape(gt_boxes)[0]
    jitter = tf.random_uniform([shape, 1], minval = -jitter, maxval = jitter)
    jitter = tf.reshape(jitter, [-1])
    ws_offset = ws * jitter
    hs_offset = hs * jitter
    x1s = gt_boxes[:, 0] + ws_offset
    x2s = gt_boxes[:, 2] + ws_offset
    y1s = gt_boxes[:, 1] + hs_offset
    y2s = gt_boxes[:, 3] + hs_offset
    boxes = tf.concat(
            values=[
                x1s[:, tf.newaxis],
                y1s[:, tf.newaxis],
                x2s[:, tf.newaxis],
                y2s[:, tf.newaxis]],
            axis=1)
    new_scores = tf.ones([shape], tf.float32)
    new_batch_inds = tf.zeros([shape], tf.int32)

    return tf.concat(values=[rois, boxes], axis=0), \
           tf.concat(values=[scores, new_scores], axis=0), \
           tf.concat(values=[batch_inds, new_batch_inds], axis=0)
项目:social-scene-understanding    作者:cvlab-epfl    | 项目源码 | 文件源码
def det_net_loss(seg_masks_in, reg_masks_in,
                 seg_preds, reg_preds,
                 reg_loss_weight=10.0,
                 epsilon=1e-5):

  with tf.variable_scope('loss'):
    out_size = seg_preds.get_shape()[1:3]
    seg_masks_in_ds = tf.image.resize_images(seg_masks_in[:,:,:,tf.newaxis],
                                             out_size[0], out_size[1],
                                             tf.image.ResizeMethod.NEAREST_NEIGHBOR)
    reg_masks_in_ds = tf.image.resize_images(reg_masks_in,
                                             out_size[0], out_size[1],
                                             tf.image.ResizeMethod.NEAREST_NEIGHBOR)

    # segmentation loss
    seg_masks_onehot = slim.one_hot_encoding(seg_masks_in_ds[:,:,:,0], 2)
    seg_loss = - tf.reduce_mean(seg_masks_onehot * tf.log(seg_preds + epsilon))

    # regression loss
    mask = tf.to_float(seg_masks_in_ds)
    reg_loss = tf.reduce_sum(mask * (reg_preds - reg_masks_in_ds)**2)
    reg_loss = reg_loss / (tf.reduce_sum(mask) + 1.0)

  return seg_loss + reg_loss_weight * reg_loss
项目:t3f    作者:Bihaqo    | 项目源码 | 文件源码
def multiply_along_batch_dim(batch_tt, weights):
  """Multiply each TensorTrain in a batch by a number.

  Args:
    batch_tt: TensorTrainBatch object, TT-matrices or TT-tensors.
    weights: 1-D tf.Tensor (or something convertible to it like np.array) of size
     tt.batch_sie with weights. 

  Returns:
    TensorTrainBatch
  """
  weights = tf.convert_to_tensor(weights)
  tt_cores = list(batch_tt.tt_cores)
  if batch_tt.is_tt_matrix():
    weights = weights[:, tf.newaxis, tf.newaxis, tf.newaxis, tf.newaxis]
  else:
    weights = weights[:, tf.newaxis, tf.newaxis, tf.newaxis]
  tt_cores[0] = weights * tt_cores[0]
  out_shape = batch_tt.get_raw_shape()
  out_ranks = batch_tt.get_tt_ranks()
  out_batch_size = batch_tt.batch_size
  return TensorTrainBatch(tt_cores, out_shape, out_ranks, out_batch_size)
项目:FastMaskRCNN    作者:CharlesShang    | 项目源码 | 文件源码
def _add_jittered_boxes(rois, scores, batch_inds, gt_boxes, jitter=0.1):
    ws = gt_boxes[:, 2] - gt_boxes[:, 0]
    hs = gt_boxes[:, 3] - gt_boxes[:, 1]
    shape = tf.shape(gt_boxes)[0]
    jitter = tf.random_uniform([shape, 1], minval = -jitter, maxval = jitter)
    jitter = tf.reshape(jitter, [-1])
    ws_offset = ws * jitter
    hs_offset = hs * jitter
    x1s = gt_boxes[:, 0] + ws_offset
    x2s = gt_boxes[:, 2] + ws_offset
    y1s = gt_boxes[:, 1] + hs_offset
    y2s = gt_boxes[:, 3] + hs_offset
    boxes = tf.concat(
            values=[
                x1s[:, tf.newaxis],
                y1s[:, tf.newaxis],
                x2s[:, tf.newaxis],
                y2s[:, tf.newaxis]],
            axis=1)
    new_scores = tf.ones([shape], tf.float32)
    new_batch_inds = tf.zeros([shape], tf.int32)

    return tf.concat(values=[rois, boxes], axis=0), \
           tf.concat(values=[scores, new_scores], axis=0), \
           tf.concat(values=[batch_inds, new_batch_inds], axis=0)
项目:TFMaskRCNN    作者:hillox    | 项目源码 | 文件源码
def _add_jittered_boxes(rois, scores, batch_inds, gt_boxes, jitter=0.1):
    ws = gt_boxes[:, 2] - gt_boxes[:, 0]
    hs = gt_boxes[:, 3] - gt_boxes[:, 1]
    shape = tf.shape(gt_boxes)[0]
    jitter = tf.random_uniform([shape, 1], minval = -jitter, maxval = jitter)
    jitter = tf.reshape(jitter, [-1])
    ws_offset = ws * jitter
    hs_offset = hs * jitter
    x1s = gt_boxes[:, 0] + ws_offset
    x2s = gt_boxes[:, 2] + ws_offset
    y1s = gt_boxes[:, 1] + hs_offset
    y2s = gt_boxes[:, 3] + hs_offset
    boxes = tf.concat(
            values=[
                x1s[:, tf.newaxis],
                y1s[:, tf.newaxis],
                x2s[:, tf.newaxis],
                y2s[:, tf.newaxis]],
            axis=1)
    new_scores = tf.ones([shape], tf.float32)
    new_batch_inds = tf.zeros([shape], tf.int32)

    return tf.concat(values=[rois, boxes], axis=0), \
           tf.concat(values=[scores, new_scores], axis=0), \
           tf.concat(values=[batch_inds, new_batch_inds], axis=0)
项目:tf-image-interpreter    作者:ThoughtWorksInc    | 项目源码 | 文件源码
def _combine_box_and_delta(self, bboxes, deltas):
    widths = bboxes[:, 2] - bboxes[:, 0] + 1.0
    heights = bboxes[:, 3] - bboxes[:, 1] + 1.0
    ctr_x = bboxes[:, 0] + 0.5 * widths
    ctr_y = bboxes[:, 1] + 0.5 * heights

    # use 0::4 to make it a [-1, 1] matrix, while the columns are 4
    dx = deltas[:, 0::4]
    dy = deltas[:, 1::4]
    dw = deltas[:, 2::4]
    dh = deltas[:, 3::4]

    # do not understand the transformation
    # TF ?????reshape????????????????
    pred_ctr_x = tf.reshape(dx * widths[:, tf.newaxis] + ctr_x[:, tf.newaxis], (-1,))
    pred_ctr_y = tf.reshape(dy * heights[:, tf.newaxis] + ctr_y[:, tf.newaxis], (-1,))
    pred_w = tf.reshape(tf.exp(dw) * widths[:, tf.newaxis], (-1,))
    pred_h = tf.reshape(tf.exp(dh) * heights[:, tf.newaxis], (-1,))

    pred_boxes = tf.pack(
      [pred_ctr_x - 0.5 * pred_w,
       pred_ctr_y - 0.5 * pred_h,
       pred_ctr_x + 0.5 * pred_w,
       pred_ctr_y + 0.5 * pred_h],
      axis=1
    )

    return pred_boxes
项目:tf-image-interpreter    作者:ThoughtWorksInc    | 项目源码 | 文件源码
def _compute_targets(self, ex_rois, gt_rois, labels):
    targets = self._bbox_transform(ex_rois, gt_rois)
    # TODO: check if cfg.TRAIN.BBOX_NORMALIZE_TARGETS_PRECOMPUTED:
    return tf.concat(1, [tf.cast(labels, dtype=tf.float32)[:, tf.newaxis], targets])
项目:attend_infer_repeat    作者:akosiorek    | 项目源码 | 文件源码
def _build(self, img, transform_params):
        if len(img.get_shape()) == 3:
            img = img[..., tf.newaxis]

        grid_coords = self._warper(transform_params)
        return snt.resampler(img, grid_coords)
项目:Master-R-CNN    作者:Mark110    | 项目源码 | 文件源码
def flip_gt_boxes(gt_boxes, ih, iw):
    x1s, y1s, x2s, y2s, cls = \
            gt_boxes[:, 0], gt_boxes[:, 1], gt_boxes[:, 2], gt_boxes[:, 3], gt_boxes[:, 4]
    x1s = tf.to_float(iw) - x1s
    x2s = tf.to_float(iw) - x2s
    return tf.concat(values=(x2s[:, tf.newaxis], 
                             y1s[:, tf.newaxis], 
                             x1s[:, tf.newaxis], 
                             y2s[:, tf.newaxis], 
                             cls[:, tf.newaxis]), axis=1)
项目:Master-R-CNN    作者:Mark110    | 项目源码 | 文件源码
def resize_gt_boxes(gt_boxes, scale_ratio):
    xys, cls = \
            gt_boxes[:, 0:4], gt_boxes[:, 4]
    xys = xys * scale_ratio 
    return tf.concat(values=(xys, cls[:, tf.newaxis]), axis=1)
项目:Master-R-CNN    作者:Mark110    | 项目源码 | 文件源码
def crop(images, boxes, batch_inds, stride = 1, pooled_height = 7, pooled_width = 7, scope='ROIAlign'):
  """Cropping areas of features into fixed size
  Params:
  --------
  images: a 4-d Tensor of shape (N, H, W, C)
  boxes: rois in the original image, of shape (N, ..., 4), [x1, y1, x2, y2]
  batch_inds: 

  Returns:
  --------
  A Tensor of shape (N, pooled_height, pooled_width, C)
  """
  with tf.name_scope(scope):
    #
    boxes = boxes / (stride + 0.0)
    boxes = tf.reshape(boxes, [-1, 4])

    # normalize the boxes and swap x y dimensions
    shape = tf.shape(images)
    boxes = tf.reshape(boxes, [-1, 2]) # to (x, y)
    xs = boxes[:, 0] 
    ys = boxes[:, 1]
    xs = xs / tf.cast(shape[2], tf.float32)
    ys = ys / tf.cast(shape[1], tf.float32)
    boxes = tf.concat([ys[:, tf.newaxis], xs[:, tf.newaxis]], axis=1)
    boxes = tf.reshape(boxes, [-1, 4])  # to (y1, x1, y2, x2)

    # if batch_inds is False:
    #   num_boxes = tf.shape(boxes)[0]
    #   batch_inds = tf.zeros([num_boxes], dtype=tf.int32, name='batch_inds')
    # batch_inds = boxes[:, 0] * 0
    # batch_inds = tf.cast(batch_inds, tf.int32)

    # assert_op = tf.Assert(tf.greater(tf.shape(images)[0], tf.reduce_max(batch_inds)), [images, batch_inds])
    assert_op = tf.Assert(tf.greater(tf.size(images), 0), [images, batch_inds])
    with tf.control_dependencies([assert_op, images, batch_inds]):
        return  tf.image.crop_and_resize(images, boxes, batch_inds,
                                         [pooled_height, pooled_width],
                                         method='bilinear',
                                         name='Crop')
项目:hart    作者:akosiorek    | 项目源码 | 文件源码
def _bbox_to_mask_fixed_size(yy, region_size, output_size, dtype):

    mask = _bbox_to_mask(yy, region_size, dtype)

    nonzero_region = tf.greater(tf.reduce_prod(tf.shape(mask)), 0)
    mask = tf.cond(nonzero_region, lambda: mask, lambda: tf.zeros(output_size, dtype))
    mask = tf.image.resize_images(mask[..., tf.newaxis], output_size)[..., 0]
    return mask
项目:hart    作者:akosiorek    | 项目源码 | 文件源码
def __init__(self, inpt, bbox0, presence0, batch_size, glimpse_size,
                 feature_extractor, rnn_units, bbox_gain=-4., att_gain=-2.5,
                 zoneout_prob=0., identity_init=True, attention_module=RATMAttention, normalize_glimpse=False,
                 debug=False, clip_bbox=False, transform_init_features=False,
                 transform_init_state=False, dfn_readout=False, feature_shape=None, is_training=True):

        self.inpt = inpt
        self.bbox0 = bbox0
        self.presence0 = presence0
        self.glimpse_size = glimpse_size
        self.feature_extractor = feature_extractor
        self.rnn_units = rnn_units

        self.batch_size = batch_size
        self.inpt_size = convert_shape(inpt.get_shape()[2:], np.int32)
        self.bbox_gain = ensure_array(bbox_gain, 4)[np.newaxis]
        self.att_gain = ensure_array(att_gain, attention_module.n_params)[np.newaxis]
        self.zoneout_prob = zoneout_prob
        self.identity_init = identity_init
        self.attention_module = attention_module
        self.normalize_glimpse = normalize_glimpse
        self.debug = debug
        self.clip_bbox = clip_bbox
        self.transform_init_features = transform_init_features
        self.transform_init_state = transform_init_state
        self.dfn_readout = dfn_readout
        self.feature_shape = feature_shape
        self.is_training = tf.convert_to_tensor(is_training)

        super(HierarchicalAttentiveRecurrentTracker, self).__init__(self.__class__.__name__)
        try:
            self.register(is_training)
        except ValueError: pass
项目:hart    作者:akosiorek    | 项目源码 | 文件源码
def gaussian_mask(params, R, C):
    """Define a mask of size RxC given by one 1-D Gaussian per row.

    u, s and d must be 1-dimensional vectors"""
    u, s, d = (params[..., i] for i in xrange(3))

    for i in (u, s, d):
        assert len(u.get_shape()) == 1, i

    batch_size = tf.to_int32(tf.shape(u)[0])

    R = tf.range(tf.to_int32(R))
    C = tf.range(tf.to_int32(C))
    R = tf.to_float(R)[tf.newaxis, tf.newaxis, :]
    C = tf.to_float(C)[tf.newaxis, :, tf.newaxis]
    C = tf.tile(C, (batch_size, 1, 1))

    u, d = u[:, tf.newaxis, tf.newaxis], d[:, tf.newaxis, tf.newaxis]
    s = s[:, tf.newaxis, tf.newaxis]

    ur = u + (R - 0.) * d
    sr = tf.ones_like(ur) * s

    mask = C - ur
    mask = tf.exp(-.5 * (mask / sr) ** 2)

    mask /= tf.reduce_sum(mask, 1, keep_dims=True) + 1e-8
    return mask
项目:hart    作者:akosiorek    | 项目源码 | 文件源码
def extract_glimpse(inpt, attention_params, glimpse_size):
    """Extracts an attention glimpse

    :param inpt: tensor of shape == (batch_size, img_height, img_width)
    :param attention_params: tensor of shape = (batch_size, 6) as
        [uy, sy, dy, ux, sx, dx] with u - mean, s - std, d - stride"
    :param glimpse_size: 2-tuple of ints as (height, width),
        size of the extracted glimpse
    :return: tensor
    """

    ap = attention_params
    shape = inpt.get_shape()
    rank = len(shape)

    assert rank in (3, 4), "Input must be 3 or 4 dimensional tensor"

    inpt_H, inpt_W = shape[1:3]
    if rank == 3:
        inpt = inpt[..., tf.newaxis]
        rank += 1

    Fy = gaussian_mask(ap[..., 0::2], glimpse_size[0], inpt_H)
    Fx = gaussian_mask(ap[..., 1::2], glimpse_size[1], inpt_W)

    gs = []
    for channel in tf.unstack(inpt, axis=rank - 1):
        g = tf.matmul(tf.matmul(Fy, channel, adjoint_a=True), Fx)
        gs.append(g)
    g = tf.stack(gs, axis=rank - 1)

    g.set_shape([shape[0]] + list(glimpse_size))
    return g
项目:hart    作者:akosiorek    | 项目源码 | 文件源码
def bbox_to_attention(self, bbox):
        with tf.variable_scope('ratm_bbox_to_attention'):
            us = bbox[..., :2] / self.inpt_size[np.newaxis, :2]
            ss = 0.5 * bbox[..., 2:] / self.inpt_size[np.newaxis, :2]
            ds = bbox[..., 2:] / (self.inpt_size[np.newaxis, :2] - 1.)

            att = tf.concat(axis=tf.rank(bbox) - 1, values=(us, ss, ds))
        return att
项目:hart    作者:akosiorek    | 项目源码 | 文件源码
def bbox_to_attention(self, bbox):
        with tf.variable_scope('fixed_std_bbox_to_attention'):
            us = bbox[..., :2] / self.inpt_size[np.newaxis, :2]
            ds = bbox[..., 2:] / (self.inpt_size[np.newaxis, :2] - 1.)

            att = tf.concat(axis=tf.rank(bbox) - 1, values=(us, ds))
            att.set_shape(bbox.get_shape()[:-1].concatenate([4]))
        return att
项目:hart    作者:akosiorek    | 项目源码 | 文件源码
def _stride_to_std(self, stride):
        shape = convert_shape(stride.get_shape())
        stride_flat = tf.reshape(stride, (-1, shape[-1]))
        y, x = stride_flat[..., 0], stride_flat[..., 1]
        features = [
            tf.ones_like(y),
            y, y ** 2, y ** 3, y ** 4,
            x, x ** 2, x ** 3, x ** 4,
               y * x, y * x ** 2, y ** 2 * x,
               y * x ** 3, y ** 2 * x ** 2, y ** 3 * x
        ]

        features = tf.concat(axis=1, values=[f[..., tf.newaxis] for f in features])
        sigma_flat = tf.matmul(features, self.weights)
        return tf.reshape(sigma_flat, shape)
项目:hart    作者:akosiorek    | 项目源码 | 文件源码
def _to_attention(self, raw_att, with_bias=True):
        bbox = FixedStdAttention.attention_to_bbox(self, raw_att)
        us = bbox[..., :2]
        if with_bias:
            us += self.offset_bias

        ds = bbox[..., 2:4] / (self.glimpse_size[np.newaxis, :2] - 1)
        ss = self._stride_to_std(ds)

        ap = tf.concat(axis=tf.rank(raw_att) - 1, values=(us, ss, ds), name='attention')
        ap.set_shape(raw_att.get_shape()[:-1].concatenate((6,)))
        return ap
项目:foolbox    作者:bethgelab    | 项目源码 | 文件源码
def predictions_and_gradient(self, image, label):
        image = self._process_input(image)
        predictions, gradient = self._session.run(
            [self._logits, self._gradient],
            feed_dict={
                self._images: image[np.newaxis],
                self._label: label})
        gradient = self._process_gradient(gradient)
        return predictions, gradient
项目:foolbox    作者:bethgelab    | 项目源码 | 文件源码
def gradient(self, image, label):
        image = self._process_input(image)
        g = self._session.run(
            self._gradient,
            feed_dict={
                self._images: image[np.newaxis],
                self._label: label})
        g = self._process_gradient(g)
        return g
项目:foolbox    作者:bethgelab    | 项目源码 | 文件源码
def _loss_fn(self, image, label):
        image = self._process_input(image)
        loss = self._session.run(
            self._loss,
            feed_dict={
                self._images: image[np.newaxis],
                self._label: label})
        return loss
项目:foolbox    作者:bethgelab    | 项目源码 | 文件源码
def backward(self, gradient, image):
        assert gradient.ndim == 1
        image = self._process_input(image)
        g = self._session.run(
            self._bw_gradient,
            feed_dict={
                self._images: image[np.newaxis],
                self._bw_gradient_pre: gradient})
        g = self._process_gradient(g)
        assert g.shape == image.shape
        return g
项目:social-scene-understanding    作者:cvlab-epfl    | 项目源码 | 文件源码
def compute_detections_greedy(seg_preds, boxes_preds, num_outputs,
                              seg_threshold=0.2,
                              sigma=5e-3, step=0.2, num_iters=20,
                              dist_threshold=20.0):

  mask_flat = tf.reshape(seg_preds[:,:,1], [-1])
  boxes_flat = tf.reshape(boxes_preds, [-1, 4])

  # TODO: also collect (y,x) coordinates
  idxs = tf.where(mask_flat > seg_threshold)[:,0]
  boxes = tf.gather(boxes_flat, idxs)
  boxes, confidence = refine_boxes(boxes, num_iters, step, sigma)

  num_boxes = tf.shape(boxes)[0]

  dists = tf.nn.relu(nnutil.pairwise_distance(boxes / sigma))
  weights = tf.exp(-dists)

  def _next_detection(prev, i):
    _, _, presence = prev
    confidence_curr = tf.reduce_sum(weights * presence, [1], True)
    idx = tf.to_int32(tf.argmax(confidence_curr, 0)[0])
    mask = tf.to_float(tf.gather(dists, idx) > dist_threshold)[:,tf.newaxis]
    presence = presence * mask
    confidence = tf.gather(confidence_curr, idx)[0]
    return idx, confidence, presence

  idxs, confidence, presences = tf.scan(_next_detection,
                                         tf.range(0, num_outputs),
                                         initializer=(0,
                                                      0.0,
                                                      tf.ones([num_boxes,1])))
  return tf.gather(boxes, idxs), confidence
项目:FastMaskRCNN    作者:CharlesShang    | 项目源码 | 文件源码
def flip_gt_boxes(gt_boxes, ih, iw):
    x1s, y1s, x2s, y2s, cls = \
            gt_boxes[:, 0], gt_boxes[:, 1], gt_boxes[:, 2], gt_boxes[:, 3], gt_boxes[:, 4]
    x1s = tf.to_float(iw) - x1s
    x2s = tf.to_float(iw) - x2s
    return tf.concat(values=(x2s[:, tf.newaxis], 
                             y1s[:, tf.newaxis], 
                             x1s[:, tf.newaxis], 
                             y2s[:, tf.newaxis], 
                             cls[:, tf.newaxis]), axis=1)
项目:FastMaskRCNN    作者:CharlesShang    | 项目源码 | 文件源码
def resize_gt_boxes(gt_boxes, scale_ratio):
    xys, cls = \
            gt_boxes[:, 0:4], gt_boxes[:, 4]
    xys = xys * scale_ratio 
    return tf.concat(values=(xys, cls[:, tf.newaxis]), axis=1)
项目:FastMaskRCNN    作者:CharlesShang    | 项目源码 | 文件源码
def crop(images, boxes, batch_inds, stride = 1, pooled_height = 7, pooled_width = 7, scope='ROIAlign'):
  """Cropping areas of features into fixed size
  Params:
  --------
  images: a 4-d Tensor of shape (N, H, W, C)
  boxes: rois in the original image, of shape (N, ..., 4), [x1, y1, x2, y2]
  batch_inds: 

  Returns:
  --------
  A Tensor of shape (N, pooled_height, pooled_width, C)
  """
  with tf.name_scope(scope):
    #
    boxes = boxes / (stride + 0.0)
    boxes = tf.reshape(boxes, [-1, 4])

    # normalize the boxes and swap x y dimensions
    shape = tf.shape(images)
    boxes = tf.reshape(boxes, [-1, 2]) # to (x, y)
    xs = boxes[:, 0] 
    ys = boxes[:, 1]
    xs = xs / tf.cast(shape[2], tf.float32)
    ys = ys / tf.cast(shape[1], tf.float32)
    boxes = tf.concat([ys[:, tf.newaxis], xs[:, tf.newaxis]], axis=1)
    boxes = tf.reshape(boxes, [-1, 4])  # to (y1, x1, y2, x2)

    # if batch_inds is False:
    #   num_boxes = tf.shape(boxes)[0]
    #   batch_inds = tf.zeros([num_boxes], dtype=tf.int32, name='batch_inds')
    # batch_inds = boxes[:, 0] * 0
    # batch_inds = tf.cast(batch_inds, tf.int32)

    # assert_op = tf.Assert(tf.greater(tf.shape(images)[0], tf.reduce_max(batch_inds)), [images, batch_inds])
    assert_op = tf.Assert(tf.greater(tf.size(images), 0), [images, batch_inds])
    with tf.control_dependencies([assert_op, images, batch_inds]):
        return  tf.image.crop_and_resize(images, boxes, batch_inds,
                                         [pooled_height, pooled_width],
                                         method='bilinear',
                                         name='Crop')
项目:FastMaskRCNN    作者:CharlesShang    | 项目源码 | 文件源码
def crop_(images, boxes, batch_inds, ih, iw, stride = 1, pooled_height = 7, pooled_width = 7, scope='ROIAlign'):
  """Cropping areas of features into fixed size
  Params:
  --------
  images: a 4-d Tensor of shape (N, H, W, C)
  boxes: rois in the original image, of shape (N, ..., 4), [x1, y1, x2, y2]
  batch_inds: 

  Returns:
  --------
  A Tensor of shape (N, pooled_height, pooled_width, C)
  """
  with tf.name_scope(scope):
    #
    boxes = boxes / (stride + 0.0)
    boxes = tf.reshape(boxes, [-1, 4])

    # normalize the boxes and swap x y dimensions
    shape = tf.shape(images)
    boxes = tf.reshape(boxes, [-1, 2]) # to (x, y)
    xs = boxes[:, 0] 
    ys = boxes[:, 1]
    xs = xs / tf.cast(shape[2], tf.float32)
    ys = ys / tf.cast(shape[1], tf.float32)
    boxes = tf.concat([ys[:, tf.newaxis], xs[:, tf.newaxis]], axis=1)
    boxes = tf.reshape(boxes, [-1, 4])  # to (y1, x1, y2, x2)

    # if batch_inds is False:
    #   num_boxes = tf.shape(boxes)[0]
    #   batch_inds = tf.zeros([num_boxes], dtype=tf.int32, name='batch_inds')
    # batch_inds = boxes[:, 0] * 0
    # batch_inds = tf.cast(batch_inds, tf.int32)

    # assert_op = tf.Assert(tf.greater(tf.shape(images)[0], tf.reduce_max(batch_inds)), [images, batch_inds])
    assert_op = tf.Assert(tf.greater(tf.size(images), 0), [images, batch_inds])
    with tf.control_dependencies([assert_op, images, batch_inds]):
        return  [tf.image.crop_and_resize(images, boxes, batch_inds,
                                         [pooled_height, pooled_width],
                                         method='bilinear',
                                         name='Crop')] + [boxes]
项目:TFMaskRCNN    作者:hillox    | 项目源码 | 文件源码
def flip_gt_boxes(gt_boxes, ih, iw):
    x1s, y1s, x2s, y2s, cls = \
            gt_boxes[:, 0], gt_boxes[:, 1], gt_boxes[:, 2], gt_boxes[:, 3], gt_boxes[:, 4]
    x1s = tf.to_float(iw) - x1s
    x2s = tf.to_float(iw) - x2s
    return tf.concat(values=(x2s[:, tf.newaxis], 
                             y1s[:, tf.newaxis], 
                             x1s[:, tf.newaxis], 
                             y2s[:, tf.newaxis], 
                             cls[:, tf.newaxis]), axis=1)
项目:TFMaskRCNN    作者:hillox    | 项目源码 | 文件源码
def resize_gt_boxes(gt_boxes, scale_ratio):
    xys, cls = \
            gt_boxes[:, 0:4], gt_boxes[:, 4]
    xys = xys * scale_ratio 
    return tf.concat(values=(xys, cls[:, tf.newaxis]), axis=1)
项目:TFMaskRCNN    作者:hillox    | 项目源码 | 文件源码
def crop(images, boxes, batch_inds, stride = 1, pooled_height = 7, pooled_width = 7, scope='ROIAlign'):
  """Cropping areas of features into fixed size
  Params:
  --------
  images: a 4-d Tensor of shape (N, H, W, C)
  boxes: rois in the original image, of shape (N, ..., 4), [x1, y1, x2, y2]
  batch_inds: 

  Returns:
  --------
  A Tensor of shape (N, pooled_height, pooled_width, C)
  """
  with tf.name_scope(scope):
    #
    boxes = boxes / (stride + 0.0)
    boxes = tf.reshape(boxes, [-1, 4])

    # normalize the boxes and swap x y dimensions
    shape = tf.shape(images)
    boxes = tf.reshape(boxes, [-1, 2]) # to (x, y)
    xs = boxes[:, 0] 
    ys = boxes[:, 1]
    xs = xs / tf.cast(shape[2], tf.float32)
    ys = ys / tf.cast(shape[1], tf.float32)
    boxes = tf.concat([ys[:, tf.newaxis], xs[:, tf.newaxis]], axis=1)
    boxes = tf.reshape(boxes, [-1, 4])  # to (y1, x1, y2, x2)

    # if batch_inds is False:
    #   num_boxes = tf.shape(boxes)[0]
    #   batch_inds = tf.zeros([num_boxes], dtype=tf.int32, name='batch_inds')
    # batch_inds = boxes[:, 0] * 0
    # batch_inds = tf.cast(batch_inds, tf.int32)

    # assert_op = tf.Assert(tf.greater(tf.shape(images)[0], tf.reduce_max(batch_inds)), [images, batch_inds])
    assert_op = tf.Assert(tf.greater(tf.size(images), 0), [images, batch_inds])
    with tf.control_dependencies([assert_op, images, batch_inds]):
        return  tf.image.crop_and_resize(images, boxes, batch_inds,
                                         [pooled_height, pooled_width],
                                         method='bilinear',
                                         name='Crop')
项目:hart    作者:akosiorek    | 项目源码 | 文件源码
def _build(self):
        self.cell = AttentionCell(self.feature_extractor,
                                  self.rnn_units, self.att_gain, self.glimpse_size, self.inpt_size,
                                  self.batch_size, self.zoneout_prob,
                                  self.attention_module, self.normalize_glimpse, self.identity_init,
                                  self.debug, self.dfn_readout, self.feature_shape, is_training=self.is_training)

        first_state = self.cell.zero_state(self.batch_size, tf.float32, self.bbox0, self.presence0, self.inpt[0],
                                           self.transform_init_features, self.transform_init_state)

        raw_outputs, state = tf.nn.dynamic_rnn(self.cell, self.inpt,
                                                        initial_state=first_state,
                                                        time_major=True,
                                                        scope=tf.get_variable_scope())

        if self.debug:
            (outputs, attention, presence, glimpse) = raw_outputs[:4]
            shape = (-1, self.batch_size, 1) + tuple(self.glimpse_size)
            self.glimpse = tf.reshape(glimpse, shape, 'glimpse_shape')
            tf.summary.histogram('rnn_outputs', outputs)
        else:
            (outputs, attention, presence) = raw_outputs[:3]

        if self.dfn_readout:
            self.obj_mask_logit = tf.reshape(raw_outputs[-3], (-1, self.batch_size, 1) + tuple(self.feature_shape))
            self.obj_mask = tf.nn.sigmoid(self.obj_mask_logit)
            obj_mask_features_flat = tf.reshape(raw_outputs[-2][1:], (-1, 10))
            self.dfn_weight_decay = raw_outputs[-1]

        self.rnn_output = outputs
        self.hidden_state = state[-1]
        self.raw_presence = presence
        self.presence = tf.nn.sigmoid(self.raw_presence)

        states_flat = tf.reshape(outputs[1:], (-1, self.rnn_units), 'flatten_states')
        if self.dfn_readout:
            states_flat = tf.concat(axis=1, values=(states_flat, obj_mask_features_flat))

        hidden_to_bbox = MLP(states_flat, self.rnn_units, 4, transfer=tf.nn.tanh, name='fc_h2bbox',
                             weight_init=self.cell._rec_init, bias_init=tf.constant_initializer())

        if self.debug:
            tf.summary.histogram('bbox_diff', hidden_to_bbox)

        attention = tf.reshape(attention, (-1, self.batch_size, 1, self.cell.att_size), 'shape_attention')
        self.attention = tf.concat(axis=0, values=(self.cell.att0[tf.newaxis], attention[:-1]))
        self.att_pred_bbox = self.cell.attention.attention_to_bbox(self.attention)
        self.att_pred_bbox_wo_bias = self.cell.attention.attention_to_bbox(self.attention - self.cell.att_bias)
        self.att_region = self.cell.attention.attention_region(self.attention)

        pred_bbox_delta = tf.reshape(hidden_to_bbox.output, (-1, self.batch_size, 1, 4), 'shape_pred_deltas')
        p = tf.zeros_like(pred_bbox_delta[0])[tf.newaxis]
        p = tf.concat(axis=0, values=(p, pred_bbox_delta))

        self.corr_pred_bbox = p * np.tile(self.inpt_size[:2], (2,)).reshape(1, 4)
        self.pred_bbox = self.att_pred_bbox_wo_bias + self.corr_pred_bbox
项目:hart    作者:akosiorek    | 项目源码 | 文件源码
def get_minibatch(self):

        if self.minibatch is None:

            self.img_store = ImageStore(self.img_size, self.in_memory, self.storage_dtype)

            def get_single_sample():
                return process_entry(self.get(), 1, self.img_store,
                                     self.depth_folder, self.bbox_scale)

            n_channels = 3 + int(self.depth_folder is not None)
            shapes = [(None,) + tuple(self.img_size) + (n_channels,), (None, 1, 4),
                      (None, 1)]
            dtypes = [self.storage_dtype, tf.float32, tf.uint8]
            names = ['img', 'bbox', 'presence']

            sample, sample_queue_size = nct.run_py2tf_queue(get_single_sample, dtypes, shapes=shapes,
                                                            names=names, n_threads=self.n_threads,
                                                            capacity=2 * self.batch_size,
                                                            name='{}/py2tf_queue'.format(self.name))

            minibatch = tf.train.batch(sample, self.batch_size, dynamic_pad=True, capacity=2)

            for k, v in minibatch.iteritems():
                unpacked = tf.unstack(v)
                unpacked = [u[:, tf.newaxis] for u in unpacked]
                minibatch[k] = tf.concat(axis=1, values=unpacked)

            if self.storage_dtype != tf.float32:
                minibatch[names[0]] = tf.to_float(minibatch[names[0]])
                dtypes[0] = tf.float32

            queue = tf.FIFOQueue(2, dtypes, names=names)
            enqeue_op = queue.enqueue(minibatch)
            runner = tf.train.QueueRunner(queue, [enqeue_op] * 2)
            tf.train.add_queue_runner(runner)
            minibatch = queue.dequeue()
            for name, shape in zip(names, shapes):
                minibatch[name].set_shape((shape[0], self.batch_size) + shape[1:])

            self.minibatch = minibatch

        return self.minibatch
项目:foolbox    作者:bethgelab    | 项目源码 | 文件源码
def __init__(
            self,
            images,
            logits,
            bounds,
            channel_axis=3,
            preprocessing=(0, 1)):

        super(TensorFlowModel, self).__init__(bounds=bounds,
                                              channel_axis=channel_axis,
                                              preprocessing=preprocessing)

        # delay import until class is instantiated
        import tensorflow as tf

        session = tf.get_default_session()
        if session is None:
            session = tf.Session(graph=images.graph)
            self._created_session = True
        else:
            self._created_session = False

        with session.graph.as_default():
            self._session = session
            self._images = images
            self._batch_logits = logits
            self._logits = tf.squeeze(logits, axis=0)
            self._label = tf.placeholder(tf.int64, (), name='label')

            loss = tf.nn.sparse_softmax_cross_entropy_with_logits(
                labels=self._label[tf.newaxis],
                logits=self._logits[tf.newaxis])
            self._loss = tf.squeeze(loss, axis=0)
            gradients = tf.gradients(loss, images)
            assert len(gradients) == 1
            self._gradient = tf.squeeze(gradients[0], axis=0)

            self._bw_gradient_pre = tf.placeholder(tf.float32, self._logits.shape)  # noqa: E501
            bw_loss = tf.reduce_sum(self._logits * self._bw_gradient_pre)
            bw_gradients = tf.gradients(bw_loss, images)
            assert len(bw_gradients) == 1
            self._bw_gradient = tf.squeeze(bw_gradients[0], axis=0)