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')
python类newaxis()的实例源码
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
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
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)