Python tensorflow.strided_slice() Examples
The following are 30
code examples of tensorflow.strided_slice().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
tensorflow
, or try the search function
.
Example #1
Source File: ded_detAttn.py From tf-var-attention with MIT License | 6 votes |
def embedding_layer(self): with tf.name_scope("word_embeddings"): self.encoder_embeddings = tf.Variable( initial_value=np.array(self.encoder_embeddings_matrix, dtype=np.float32), dtype=tf.float32, trainable=False) self.enc_embed_input = tf.nn.embedding_lookup(self.encoder_embeddings, self.input_data) # self.enc_embed_input = tf.nn.dropout(self.enc_embed_input, keep_prob=self.keep_prob) with tf.name_scope("decoder_inputs"): self.decoder_embeddings = tf.Variable( initial_value=np.array(self.decoder_embeddings_matrix, dtype=np.float32), dtype=tf.float32, trainable=False) ending = tf.strided_slice(self.target_data, [0, 0], [self.batch_size, -1], [1, 1], name='slice_input') # Minus 1 implies everything till the last dim self.dec_input = tf.concat([tf.fill([self.batch_size, 1], self.decoder_word_index['GO']), ending], 1, name='dec_input') self.dec_embed_input = tf.nn.embedding_lookup(self.decoder_embeddings, self.dec_input) # self.dec_embed_input = tf.nn.dropout(self.dec_embed_input, keep_prob=self.keep_prob)
Example #2
Source File: node_sequence.py From graph-based-image-classification with MIT License | 6 votes |
def node_sequence(sequence, width, stride): """Normalizes a given sequence to have a fixed width by striding over the sequence. The returned sequence is padded with -1 if its length is lower than the requested width. Args: sequence: A 1d tensor. width: The length of the returned sequence. stride: The distance between two selected nodes. Returns: A 1d tensor. """ with tf.name_scope('node_sequence', values=[sequence, width, stride]): # Stride the sequence based on the given stride size. sequence = tf.strided_slice(sequence, [0], [width*stride], [stride]) # Pad right with -1 if the sequence length is lower than width. padding = tf.ones([width - tf.shape(sequence)[0]], dtype=tf.int32) padding = tf.negative(padding) sequence = tf.concat(0, [sequence, padding]) return sequence
Example #3
Source File: ved_detAttn.py From tf-var-attention with MIT License | 6 votes |
def embedding_layer(self): with tf.name_scope("word_embeddings"): self.encoder_embeddings = tf.Variable( initial_value=np.array(self.encoder_embeddings_matrix, dtype=np.float32), dtype=tf.float32, trainable=False) self.enc_embed_input = tf.nn.embedding_lookup(self.encoder_embeddings, self.input_data) # self.enc_embed_input = tf.nn.dropout(self.enc_embed_input, keep_prob=self.keep_prob) with tf.name_scope("decoder_inputs"): self.decoder_embeddings = tf.Variable( initial_value=np.array(self.decoder_embeddings_matrix, dtype=np.float32), dtype=tf.float32, trainable=False) keep = tf.where( tf.random_uniform([self.batch_size, self.decoder_num_tokens]) < self.word_dropout_keep_prob, tf.fill([self.batch_size, self.decoder_num_tokens], True), tf.fill([self.batch_size, self.decoder_num_tokens], False)) ending = tf.cast(keep, dtype=tf.int32) * self.target_data ending = tf.strided_slice(ending, [0, 0], [self.batch_size, -1], [1, 1], name='slice_input') # Minus 1 implies everything till the last dim self.dec_input = tf.concat([tf.fill([self.batch_size, 1], self.decoder_word_index['GO']), ending], 1, name='dec_input') self.dec_embed_input = tf.nn.embedding_lookup(self.decoder_embeddings, self.dec_input) # self.dec_embed_input = tf.nn.dropout(self.dec_embed_input, keep_prob=self.keep_prob)
Example #4
Source File: patchy.py From graph-based-image-classification with MIT License | 6 votes |
def read(self, filename_queue): data, label = read_tfrecord( filename_queue, {'nodes': [-1, self._grapher.num_node_channels], 'neighborhood': [self._num_nodes, self._neighborhood_size]}) nodes = data['nodes'] # Convert the neighborhood to a feature map. def _map_features(node): i = tf.maximum(node, 0) positive = tf.strided_slice(nodes, [i], [i+1], [1]) negative = tf.zeros([1, self._grapher.num_node_channels]) return tf.where(i < 0, negative, positive) data = tf.reshape(data['neighborhood'], [-1]) data = tf.cast(data, tf.int32) data = tf.map_fn(_map_features, data, dtype=tf.float32) shape = [self._num_nodes, self._neighborhood_size, self._grapher.num_node_channels] data = tf.reshape(data, shape) return Record(data, shape, label)
Example #5
Source File: ved_varAttn.py From tf-var-attention with MIT License | 6 votes |
def embedding_layer(self): with tf.name_scope("word_embeddings"): self.encoder_embeddings = tf.Variable( initial_value=np.array(self.encoder_embeddings_matrix, dtype=np.float32), dtype=tf.float32, trainable=False) self.enc_embed_input = tf.nn.embedding_lookup(self.encoder_embeddings, self.input_data) # self.enc_embed_input = tf.nn.dropout(self.enc_embed_input, keep_prob=self.keep_prob) with tf.name_scope("decoder_inputs"): self.decoder_embeddings = tf.Variable( initial_value=np.array(self.decoder_embeddings_matrix, dtype=np.float32), dtype=tf.float32, trainable=False) keep = tf.where( tf.random_uniform([self.batch_size, self.decoder_num_tokens]) < self.word_dropout_keep_prob, tf.fill([self.batch_size, self.decoder_num_tokens], True), tf.fill([self.batch_size, self.decoder_num_tokens], False)) ending = tf.cast(keep, dtype=tf.int32) * self.target_data ending = tf.strided_slice(ending, [0, 0], [self.batch_size, -1], [1, 1], name='slice_input') # Minus 1 implies everything till the last dim self.dec_input = tf.concat([tf.fill([self.batch_size, 1], self.decoder_word_index['GO']), ending], 1, name='dec_input') self.dec_embed_input = tf.nn.embedding_lookup(self.decoder_embeddings, self.dec_input) # self.dec_embed_input = tf.nn.dropout(self.dec_embed_input, keep_prob=self.keep_prob)
Example #6
Source File: util.py From ScanComplete with Apache License 2.0 | 6 votes |
def compute_voxel_group(tensor, group_id): """Extracts voxel group group_id (1-indexed) from (3, 4, or 5-dim) tensor.""" assert group_id >= 1 and group_id <= 8 group_id -= 1 begin = [0, group_id / 4, group_id / 2 % 2, group_id % 2, 0] stride = [1, 2, 2, 2, 1] dim = len(tensor.shape) if dim == 3: begin = begin[1:4] stride = stride[1:4] elif dim == 4: begin = begin[:-1] stride = stride[:-1] return tf.strided_slice(tensor, begin, tensor.shape, stride)
Example #7
Source File: boxes_utils.py From remote_sensing_object_detection_2019 with MIT License | 5 votes |
def get_horizen_minAreaRectangle(boxs, with_label=True): rpn_proposals_boxes_convert = tf.py_func(forward_convert, inp=[boxs, with_label], Tout=tf.float32) if with_label: rpn_proposals_boxes_convert = tf.reshape(rpn_proposals_boxes_convert, [-1, 9]) boxes_shape = tf.shape(rpn_proposals_boxes_convert) x_list = tf.strided_slice(rpn_proposals_boxes_convert, begin=[0, 0], end=[boxes_shape[0], boxes_shape[1] - 1], strides=[1, 2]) y_list = tf.strided_slice(rpn_proposals_boxes_convert, begin=[0, 1], end=[boxes_shape[0], boxes_shape[1] - 1], strides=[1, 2]) label = tf.unstack(rpn_proposals_boxes_convert, axis=1)[-1] y_max = tf.reduce_max(y_list, axis=1) y_min = tf.reduce_min(y_list, axis=1) x_max = tf.reduce_max(x_list, axis=1) x_min = tf.reduce_min(x_list, axis=1) return tf.transpose(tf.stack([x_min, y_min, x_max, y_max, label], axis=0)) else: rpn_proposals_boxes_convert = tf.reshape(rpn_proposals_boxes_convert, [-1, 8]) boxes_shape = tf.shape(rpn_proposals_boxes_convert) x_list = tf.strided_slice(rpn_proposals_boxes_convert, begin=[0, 0], end=[boxes_shape[0], boxes_shape[1]], strides=[1, 2]) y_list = tf.strided_slice(rpn_proposals_boxes_convert, begin=[0, 1], end=[boxes_shape[0], boxes_shape[1]], strides=[1, 2]) y_max = tf.reduce_max(y_list, axis=1) y_min = tf.reduce_min(y_list, axis=1) x_max = tf.reduce_max(x_list, axis=1) x_min = tf.reduce_min(x_list, axis=1) return tf.transpose(tf.stack([x_min, y_min, x_max, y_max], axis=0))
Example #8
Source File: structured_graph_builder.py From Gun-Detector with Apache License 2.0 | 5 votes |
def AddCrossEntropy(batch_size, n): """Adds a cross entropy cost function.""" cross_entropies = [] def _Pass(): return tf.constant(0, dtype=tf.float32, shape=[1]) for beam_id in range(batch_size): beam_gold_slot = tf.reshape( tf.strided_slice(n['gold_slot'], [beam_id], [beam_id + 1]), [1]) def _ComputeCrossEntropy(): """Adds ops to compute cross entropy of the gold path in a beam.""" # Requires a cast so that UnsortedSegmentSum, in the gradient, # is happy with the type of its input 'segment_ids', which # must be int32. idx = tf.cast( tf.reshape( tf.where(tf.equal(n['beam_ids'], beam_id)), [-1]), tf.int32) beam_scores = tf.reshape(tf.gather(n['all_path_scores'], idx), [1, -1]) num = tf.shape(idx) return tf.nn.softmax_cross_entropy_with_logits( labels=tf.expand_dims( tf.sparse_to_dense(beam_gold_slot, num, [1.], 0.), 0), logits=beam_scores) # The conditional here is needed to deal with the last few batches of the # corpus which can contain -1 in beam_gold_slot for empty batch slots. cross_entropies.append(cf.cond( beam_gold_slot[0] >= 0, _ComputeCrossEntropy, _Pass)) return {'cross_entropy': tf.div(tf.add_n(cross_entropies), batch_size)}
Example #9
Source File: coordinate_convert.py From RetinaNet_Tensorflow_Rotation with MIT License | 5 votes |
def get_horizen_minAreaRectangle(boxes, with_label=True): if with_label: boxes = tf.reshape(boxes, [-1, 9]) boxes_shape = tf.shape(boxes) x_list = tf.strided_slice(boxes, begin=[0, 0], end=[boxes_shape[0], boxes_shape[1] - 1], strides=[1, 2]) y_list = tf.strided_slice(boxes, begin=[0, 1], end=[boxes_shape[0], boxes_shape[1] - 1], strides=[1, 2]) label = tf.unstack(boxes, axis=1)[-1] y_max = tf.reduce_max(y_list, axis=1) y_min = tf.reduce_min(y_list, axis=1) x_max = tf.reduce_max(x_list, axis=1) x_min = tf.reduce_min(x_list, axis=1) return tf.transpose(tf.stack([x_min, y_min, x_max, y_max, label], axis=0)) else: boxes = tf.reshape(boxes, [-1, 8]) boxes_shape = tf.shape(boxes) x_list = tf.strided_slice(boxes, begin=[0, 0], end=[boxes_shape[0], boxes_shape[1]], strides=[1, 2]) y_list = tf.strided_slice(boxes, begin=[0, 1], end=[boxes_shape[0], boxes_shape[1]], strides=[1, 2]) y_max = tf.reduce_max(y_list, axis=1) y_min = tf.reduce_min(y_list, axis=1) x_max = tf.reduce_max(x_list, axis=1) x_min = tf.reduce_min(x_list, axis=1) return tf.transpose(tf.stack([x_min, y_min, x_max, y_max], axis=0))
Example #10
Source File: structured_graph_builder.py From yolo_v2 with Apache License 2.0 | 5 votes |
def AddCrossEntropy(batch_size, n): """Adds a cross entropy cost function.""" cross_entropies = [] def _Pass(): return tf.constant(0, dtype=tf.float32, shape=[1]) for beam_id in range(batch_size): beam_gold_slot = tf.reshape( tf.strided_slice(n['gold_slot'], [beam_id], [beam_id + 1]), [1]) def _ComputeCrossEntropy(): """Adds ops to compute cross entropy of the gold path in a beam.""" # Requires a cast so that UnsortedSegmentSum, in the gradient, # is happy with the type of its input 'segment_ids', which # must be int32. idx = tf.cast( tf.reshape( tf.where(tf.equal(n['beam_ids'], beam_id)), [-1]), tf.int32) beam_scores = tf.reshape(tf.gather(n['all_path_scores'], idx), [1, -1]) num = tf.shape(idx) return tf.nn.softmax_cross_entropy_with_logits( labels=tf.expand_dims( tf.sparse_to_dense(beam_gold_slot, num, [1.], 0.), 0), logits=beam_scores) # The conditional here is needed to deal with the last few batches of the # corpus which can contain -1 in beam_gold_slot for empty batch slots. cross_entropies.append(cf.cond( beam_gold_slot[0] >= 0, _ComputeCrossEntropy, _Pass)) return {'cross_entropy': tf.div(tf.add_n(cross_entropies), batch_size)}
Example #11
Source File: boxes_utils.py From R2CNN_Faster-RCNN_Tensorflow with MIT License | 5 votes |
def get_horizen_minAreaRectangle(boxs, with_label=True): rpn_proposals_boxes_convert = tf.py_func(forward_convert, inp=[boxs, with_label], Tout=tf.float32) if with_label: rpn_proposals_boxes_convert = tf.reshape(rpn_proposals_boxes_convert, [-1, 9]) boxes_shape = tf.shape(rpn_proposals_boxes_convert) x_list = tf.strided_slice(rpn_proposals_boxes_convert, begin=[0, 0], end=[boxes_shape[0], boxes_shape[1] - 1], strides=[1, 2]) y_list = tf.strided_slice(rpn_proposals_boxes_convert, begin=[0, 1], end=[boxes_shape[0], boxes_shape[1] - 1], strides=[1, 2]) label = tf.unstack(rpn_proposals_boxes_convert, axis=1)[-1] y_max = tf.reduce_max(y_list, axis=1) y_min = tf.reduce_min(y_list, axis=1) x_max = tf.reduce_max(x_list, axis=1) x_min = tf.reduce_min(x_list, axis=1) return tf.transpose(tf.stack([x_min, y_min, x_max, y_max, label], axis=0)) else: rpn_proposals_boxes_convert = tf.reshape(rpn_proposals_boxes_convert, [-1, 8]) boxes_shape = tf.shape(rpn_proposals_boxes_convert) x_list = tf.strided_slice(rpn_proposals_boxes_convert, begin=[0, 0], end=[boxes_shape[0], boxes_shape[1]], strides=[1, 2]) y_list = tf.strided_slice(rpn_proposals_boxes_convert, begin=[0, 1], end=[boxes_shape[0], boxes_shape[1]], strides=[1, 2]) y_max = tf.reduce_max(y_list, axis=1) y_min = tf.reduce_min(y_list, axis=1) x_max = tf.reduce_max(x_list, axis=1) x_min = tf.reduce_min(x_list, axis=1) return tf.transpose(tf.stack([x_min, y_min, x_max, y_max], axis=0))
Example #12
Source File: test_forward.py From training_results_v0.6 with Apache License 2.0 | 5 votes |
def _test_stridedslice(ip_shape, begin, end, stride, dtype, begin_mask=0, end_mask=0, new_axis_mask=0, shrink_axis_mask=0, ellipsis_mask=0): """ One iteration of a Stridedslice """ tf.reset_default_graph() in_data = tf.placeholder(dtype, ip_shape, name="in_data") tf.strided_slice(in_data, begin, end, stride, begin_mask=begin_mask, end_mask=end_mask, new_axis_mask=new_axis_mask, shrink_axis_mask=shrink_axis_mask, ellipsis_mask=ellipsis_mask, name="strided_slice") np_data = np.random.uniform(size=ip_shape).astype(dtype) compare_tf_with_tvm(np_data, 'in_data:0', 'strided_slice:0')
Example #13
Source File: data_utils.py From D-VAE with MIT License | 5 votes |
def ptb_input_producer(raw_data, batch_size, num_steps, shuffle=False, randomize=False): """ Args: raw_data: np tensor of size [num_words]. batch_size: self-explained. num_steps: number of BPTT steps. """ num_batches_per_epoch = ((np.size(raw_data) // batch_size) - 1) // num_steps raw_data = tf.convert_to_tensor(raw_data, name="raw_data", dtype=tf.int32) data_len = tf.size(raw_data) batch_len = data_len // batch_size data = tf.reshape(raw_data[0 : batch_size * batch_len], [batch_size, batch_len]) epoch_size = (batch_len - 1) // num_steps with tf.device("/cpu:0"): epoch_size = tf.identity(epoch_size, name="epoch_size") if randomize: i = tf.random_uniform([1], minval=0, maxval=batch_len - num_steps, dtype=tf.int32) i = tf.reduce_sum(i) x = tf.strided_slice( data, [0, i], [batch_size, i + num_steps]) y = tf.strided_slice( data, [0, i + 1], [batch_size, i + num_steps + 1]) else: i = tf.train.range_input_producer(epoch_size, shuffle=shuffle).dequeue() x = tf.strided_slice( data, [0, i * num_steps], [batch_size, (i + 1) * num_steps]) y = tf.strided_slice( data, [0, i * num_steps + 1], [batch_size, (i + 1) * num_steps + 1]) x.set_shape([batch_size, num_steps]) y.set_shape([batch_size, num_steps]) return x, y, num_batches_per_epoch
Example #14
Source File: coordinate_convert.py From R3Det_Tensorflow with MIT License | 5 votes |
def get_horizen_minAreaRectangle(boxes, with_label=True): if with_label: boxes = tf.reshape(boxes, [-1, 9]) boxes_shape = tf.shape(boxes) x_list = tf.strided_slice(boxes, begin=[0, 0], end=[boxes_shape[0], boxes_shape[1] - 1], strides=[1, 2]) y_list = tf.strided_slice(boxes, begin=[0, 1], end=[boxes_shape[0], boxes_shape[1] - 1], strides=[1, 2]) label = tf.unstack(boxes, axis=1)[-1] y_max = tf.reduce_max(y_list, axis=1) y_min = tf.reduce_min(y_list, axis=1) x_max = tf.reduce_max(x_list, axis=1) x_min = tf.reduce_min(x_list, axis=1) return tf.transpose(tf.stack([x_min, y_min, x_max, y_max, label], axis=0)) else: boxes = tf.reshape(boxes, [-1, 8]) boxes_shape = tf.shape(boxes) x_list = tf.strided_slice(boxes, begin=[0, 0], end=[boxes_shape[0], boxes_shape[1]], strides=[1, 2]) y_list = tf.strided_slice(boxes, begin=[0, 1], end=[boxes_shape[0], boxes_shape[1]], strides=[1, 2]) y_max = tf.reduce_max(y_list, axis=1) y_min = tf.reduce_min(y_list, axis=1) x_max = tf.reduce_max(x_list, axis=1) x_min = tf.reduce_min(x_list, axis=1) return tf.transpose(tf.stack([x_min, y_min, x_max, y_max], axis=0))
Example #15
Source File: boxes_utils.py From R2CNN-Plus-Plus_Tensorflow with MIT License | 5 votes |
def get_horizen_minAreaRectangle(boxs, with_label=True): rpn_proposals_boxes_convert = tf.py_func(forward_convert, inp=[boxs, with_label], Tout=tf.float32) if with_label: rpn_proposals_boxes_convert = tf.reshape(rpn_proposals_boxes_convert, [-1, 9]) boxes_shape = tf.shape(rpn_proposals_boxes_convert) x_list = tf.strided_slice(rpn_proposals_boxes_convert, begin=[0, 0], end=[boxes_shape[0], boxes_shape[1] - 1], strides=[1, 2]) y_list = tf.strided_slice(rpn_proposals_boxes_convert, begin=[0, 1], end=[boxes_shape[0], boxes_shape[1] - 1], strides=[1, 2]) label = tf.unstack(rpn_proposals_boxes_convert, axis=1)[-1] y_max = tf.reduce_max(y_list, axis=1) y_min = tf.reduce_min(y_list, axis=1) x_max = tf.reduce_max(x_list, axis=1) x_min = tf.reduce_min(x_list, axis=1) return tf.transpose(tf.stack([x_min, y_min, x_max, y_max, label], axis=0)) else: rpn_proposals_boxes_convert = tf.reshape(rpn_proposals_boxes_convert, [-1, 8]) boxes_shape = tf.shape(rpn_proposals_boxes_convert) x_list = tf.strided_slice(rpn_proposals_boxes_convert, begin=[0, 0], end=[boxes_shape[0], boxes_shape[1]], strides=[1, 2]) y_list = tf.strided_slice(rpn_proposals_boxes_convert, begin=[0, 1], end=[boxes_shape[0], boxes_shape[1]], strides=[1, 2]) y_max = tf.reduce_max(y_list, axis=1) y_min = tf.reduce_min(y_list, axis=1) x_max = tf.reduce_max(x_list, axis=1) x_min = tf.reduce_min(x_list, axis=1) return tf.transpose(tf.stack([x_min, y_min, x_max, y_max], axis=0))
Example #16
Source File: patchy_test.py From graph-based-image-classification with MIT License | 5 votes |
def test_node_sequence(self): neighborhood = tf.constant([ [1, 0, 3, -1], [2, 1, 0, -1], ]) nodes = tf.constant([ [0.5, 0.5, 0.5], [1.5, 1.5, 1.5], [2.5, 2.5, 2.5], [3.5, 3.5, 3.5], ]) expected = [ [[1.5, 1.5, 1.5], [0.5, 0.5, 0.5], [3.5, 3.5, 3.5], [0, 0, 0]], [[2.5, 2.5, 2.5], [1.5, 1.5, 1.5], [0.5, 0.5, 0.5], [0, 0, 0]], ] def _map_features(node): i = tf.maximum(node, 0) positive = tf.strided_slice(nodes, [i], [i+1], [1]) negative = tf.zeros([1, 3]) return tf.where(node < 0, negative, positive) with self.test_session() as sess: data = tf.reshape(neighborhood, [-1]) data = tf.map_fn(_map_features, data, dtype=tf.float32) data = tf.reshape(data, [2, 4, 3]) self.assertAllEqual(data.eval(), expected)
Example #17
Source File: cifar_10.py From graph-based-image-classification with MIT License | 5 votes |
def read(self, filename_queue): """Reads and parses examples from CIFAR-10 data files.""" # Read a record, getting filenames from the filename_queue. No header # or footer in the CIFAR-10 format, so we leave header_bytes and # footer_bytes at their default of 0. reader = tf.FixedLengthRecordReader(record_bytes=RECORD_BYTES) _, value = reader.read(filename_queue) # Convert from a string to a vector of uint8 that is RECORD_BYTES long. record_bytes = tf.decode_raw(value, tf.uint8) with tf.name_scope('read_label', values=[record_bytes]): # The first bytes represent the label, which we convert from uint8 # to int64. label = tf.strided_slice(record_bytes, [0], [LABEL_BYTES], [1]) label = tf.cast(label, tf.int64) with tf.name_scope('read_image', values=[record_bytes]): # The reamining bytes after the label represent the image, which we # reshape from [depth * height * width] to [depth, height, width]. image = tf.strided_slice( record_bytes, [LABEL_BYTES], [RECORD_BYTES], [1]) image = tf.reshape(image, [DEPTH, HEIGHT, WIDTH]) # Convert from [depth, height, width] to [height, width, depth]. image = tf.transpose(image, [1, 2, 0]) # Convert from uint8 to float32. image = tf.cast(image, tf.float32) return Record(image, [HEIGHT, WIDTH, DEPTH], label)
Example #18
Source File: cifar10.py From DOTA_models with Apache License 2.0 | 5 votes |
def parser(self, value): """Parse a Cifar10 record from value. Output images are in [height, width, depth] layout. """ # Dimensions of the images in the CIFAR-10 dataset. # See http://www.cs.toronto.edu/~kriz/cifar.html for a description of the # input format. label_bytes = 1 image_bytes = HEIGHT * WIDTH * DEPTH # Every record consists of a label followed by the image, with a # fixed number of bytes for each. record_bytes = label_bytes + image_bytes # Convert from a string to a vector of uint8 that is record_bytes long. record_as_bytes = tf.decode_raw(value, tf.uint8) # The first bytes represent the label, which we convert from # uint8->int32. label = tf.cast( tf.strided_slice(record_as_bytes, [0], [label_bytes]), tf.int32) label.set_shape([1]) # The remaining bytes after the label represent the image, which # we reshape from [depth * height * width] to [depth, height, width]. depth_major = tf.reshape( tf.strided_slice(record_as_bytes, [label_bytes], [record_bytes]), [3, 32, 32]) # Convert from [depth, height, width] to [height, width, depth]. # This puts data in a compatible layout with TF image preprocessing APIs. image = tf.transpose(depth_major, [1, 2, 0]) # Do custom preprocessing here. image = self.preprocess(image) return image, label
Example #19
Source File: ops_test.py From tf-encrypted with Apache License 2.0 | 5 votes |
def test_strided_slice(self): with tf.Session() as sess: t = tf.constant( [ [[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]], [[5, 5, 5], [6, 6, 6]], ] ) out = tf.strided_slice(t, [1, 0, 0], [2, 1, 3], [1, 1, 1]) actual = sess.run(out) tf.reset_default_graph() with tfe.protocol.Pond() as prot: x = np.array( [ [[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]], [[5, 5, 5], [6, 6, 6]], ] ) out = prot.define_private_variable(x) out = prot.strided_slice(out, [1, 0, 0], [2, 1, 3], [1, 1, 1]) with tfe.Session() as sess: sess.run(tf.global_variables_initializer()) final = sess.run(out.reveal()) np.testing.assert_array_equal(final, actual)
Example #20
Source File: convert_test.py From tf-encrypted with Apache License 2.0 | 5 votes |
def test_strided_slice_convert(self): test_input = np.ones((3, 2, 3)) # test_input = np.array([[[1., 1., 1.], [2., 2., 2.]], # [[3., 3., 3.], [4., 4., 4.]], # [[5., 5., 5.], [6., 6., 6.]]]) self._test_with_ndarray_input_fn("strided_slice", test_input, protocol="Pond")
Example #21
Source File: convert_test.py From tf-encrypted with Apache License 2.0 | 5 votes |
def export_strided_slice(filename, input_shape): t = tf.placeholder(tf.float32, shape=input_shape, name="input") out = tf.strided_slice(t, [1, 0, 0], [2, 1, 3], [1, 1, 1]) return export(out, filename)
Example #22
Source File: convert_test.py From tf-encrypted with Apache License 2.0 | 5 votes |
def run_strided_slice(data): a = tf.placeholder(tf.float32, shape=data.shape, name="input") out = tf.strided_slice(a, [1, 0, 0], [2, 1, 3], [1, 1, 1]) with tf.Session() as sess: output = sess.run(out, feed_dict={a: data}) return output
Example #23
Source File: keras2_emitter.py From MMdnn with MIT License | 5 votes |
def _layer_Slice(self): self.add_body(0, ''' def __slice(input, start, end, **kargs): return Lambda(lambda x: tf.strided_slice(x, start, end, **kargs))(input) ''')
Example #24
Source File: fwgrad.py From tensorflow-forward-ad with MIT License | 5 votes |
def StridedSlice_FwGrad(op, dx, dy, dz, du, _op_table=None, _grad_table=None): if dx is None: return None y = op.inputs[1] z = op.inputs[2] u = op.inputs[3] return tf.strided_slice(dx, begin=y, end=z, strides=u) ############################################################################### # Element-wise operators. elemwise. ###############################################################################
Example #25
Source File: inputs.py From TheNumericsOfGANs with MIT License | 5 votes |
def get_input_cifar10(filename_queue): # Dimensions of the images in the CIFAR-10 dataset. # See http://www.cs.toronto.edu/~kriz/cifar.html for a description of the # input format. label_bytes = 1 # 2 for CIFAR-100 image_bytes = 32 * 32 * 3 # Every record consists of a label followed by the image, with a # fixed number of bytes for each. record_bytes = label_bytes + image_bytes # Read a record, getting filenames from the filename_queue. reader = tf.FixedLengthRecordReader(record_bytes=record_bytes) key, value = reader.read(filename_queue) record = tf.decode_raw(value, tf.uint8) # The first bytes represent the label, which we convert from uint8->int32. label = tf.cast(record[0], tf.int32) # The remaining bytes after the label represent the image, which we reshape # from [depth * height * width] to [depth, height, width]. # tf.strided_slice(record, [label_bytes], [label_bytes + image_bytes]) image = tf.reshape(record[label_bytes:label_bytes+image_bytes], [3, 32, 32]) image = tf.cast(image, tf.float32)/255. # Convert from [depth, height, width] to [height, width, depth]. image = tf.transpose(image, [1, 2, 0]) return image, label
Example #26
Source File: reader.py From iss-rnns with Apache License 2.0 | 5 votes |
def ptb_producer(raw_data, batch_size, num_steps, name=None): """Iterate on the raw PTB data. This chunks up raw_data into batches of examples and returns Tensors that are drawn from these batches. Args: raw_data: one of the raw data outputs from ptb_raw_data. batch_size: int, the batch size. num_steps: int, the number of unrolls. name: the name of this operation (optional). Returns: A pair of Tensors, each shaped [batch_size, num_steps]. The second element of the tuple is the same data time-shifted to the right by one. Raises: tf.errors.InvalidArgumentError: if batch_size or num_steps are too high. """ with tf.name_scope(name, "PTBProducer", [raw_data, batch_size, num_steps]): raw_data = tf.convert_to_tensor(raw_data, name="raw_data", dtype=tf.int32) data_len = tf.size(raw_data) batch_len = data_len // batch_size data = tf.reshape(raw_data[0 : batch_size * batch_len], [batch_size, batch_len]) epoch_size = (batch_len - 1) // num_steps assertion = tf.assert_positive( epoch_size, message="epoch_size == 0, decrease batch_size or num_steps") with tf.control_dependencies([assertion]): epoch_size = tf.identity(epoch_size, name="epoch_size") i = tf.train.range_input_producer(epoch_size, shuffle=False).dequeue() x = tf.strided_slice(data, [0, i * num_steps], [batch_size, (i + 1) * num_steps]) x.set_shape([batch_size, num_steps]) y = tf.strided_slice(data, [0, i * num_steps + 1], [batch_size, (i + 1) * num_steps + 1]) y.set_shape([batch_size, num_steps]) return x, y
Example #27
Source File: cifar10.py From hands-detection with MIT License | 5 votes |
def parser(self, value): """Parse a Cifar10 record from value. Output images are in [height, width, depth] layout. """ # Dimensions of the images in the CIFAR-10 dataset. # See http://www.cs.toronto.edu/~kriz/cifar.html for a description of the # input format. label_bytes = 1 image_bytes = HEIGHT * WIDTH * DEPTH # Every record consists of a label followed by the image, with a # fixed number of bytes for each. record_bytes = label_bytes + image_bytes # Convert from a string to a vector of uint8 that is record_bytes long. record_as_bytes = tf.decode_raw(value, tf.uint8) # The first bytes represent the label, which we convert from # uint8->int32. label = tf.cast( tf.strided_slice(record_as_bytes, [0], [label_bytes]), tf.int32) label.set_shape([1]) # The remaining bytes after the label represent the image, which # we reshape from [depth * height * width] to [depth, height, width]. depth_major = tf.reshape( tf.strided_slice(record_as_bytes, [label_bytes], [record_bytes]), [3, 32, 32]) # Convert from [depth, height, width] to [height, width, depth]. # This puts data in a compatible layout with TF image preprocessing APIs. image = tf.transpose(depth_major, [1, 2, 0]) # Do custom preprocessing here. image = self.preprocess(image) return image, label
Example #28
Source File: structured_graph_builder.py From hands-detection with MIT License | 5 votes |
def AddCrossEntropy(batch_size, n): """Adds a cross entropy cost function.""" cross_entropies = [] def _Pass(): return tf.constant(0, dtype=tf.float32, shape=[1]) for beam_id in range(batch_size): beam_gold_slot = tf.reshape( tf.strided_slice(n['gold_slot'], [beam_id], [beam_id + 1]), [1]) def _ComputeCrossEntropy(): """Adds ops to compute cross entropy of the gold path in a beam.""" # Requires a cast so that UnsortedSegmentSum, in the gradient, # is happy with the type of its input 'segment_ids', which # must be int32. idx = tf.cast( tf.reshape( tf.where(tf.equal(n['beam_ids'], beam_id)), [-1]), tf.int32) beam_scores = tf.reshape(tf.gather(n['all_path_scores'], idx), [1, -1]) num = tf.shape(idx) return tf.nn.softmax_cross_entropy_with_logits( labels=tf.expand_dims( tf.sparse_to_dense(beam_gold_slot, num, [1.], 0.), 0), logits=beam_scores) # The conditional here is needed to deal with the last few batches of the # corpus which can contain -1 in beam_gold_slot for empty batch slots. cross_entropies.append(cf.cond( beam_gold_slot[0] >= 0, _ComputeCrossEntropy, _Pass)) return {'cross_entropy': tf.div(tf.add_n(cross_entropies), batch_size)}
Example #29
Source File: structured_graph_builder.py From object_detection_kitti with Apache License 2.0 | 5 votes |
def AddCrossEntropy(batch_size, n): """Adds a cross entropy cost function.""" cross_entropies = [] def _Pass(): return tf.constant(0, dtype=tf.float32, shape=[1]) for beam_id in range(batch_size): beam_gold_slot = tf.reshape( tf.strided_slice(n['gold_slot'], [beam_id], [beam_id + 1]), [1]) def _ComputeCrossEntropy(): """Adds ops to compute cross entropy of the gold path in a beam.""" # Requires a cast so that UnsortedSegmentSum, in the gradient, # is happy with the type of its input 'segment_ids', which # must be int32. idx = tf.cast( tf.reshape( tf.where(tf.equal(n['beam_ids'], beam_id)), [-1]), tf.int32) beam_scores = tf.reshape(tf.gather(n['all_path_scores'], idx), [1, -1]) num = tf.shape(idx) return tf.nn.softmax_cross_entropy_with_logits( labels=tf.expand_dims( tf.sparse_to_dense(beam_gold_slot, num, [1.], 0.), 0), logits=beam_scores) # The conditional here is needed to deal with the last few batches of the # corpus which can contain -1 in beam_gold_slot for empty batch slots. cross_entropies.append(cf.cond( beam_gold_slot[0] >= 0, _ComputeCrossEntropy, _Pass)) return {'cross_entropy': tf.div(tf.add_n(cross_entropies), batch_size)}
Example #30
Source File: structured_graph_builder.py From object_detection_with_tensorflow with MIT License | 5 votes |
def AddCrossEntropy(batch_size, n): """Adds a cross entropy cost function.""" cross_entropies = [] def _Pass(): return tf.constant(0, dtype=tf.float32, shape=[1]) for beam_id in range(batch_size): beam_gold_slot = tf.reshape( tf.strided_slice(n['gold_slot'], [beam_id], [beam_id + 1]), [1]) def _ComputeCrossEntropy(): """Adds ops to compute cross entropy of the gold path in a beam.""" # Requires a cast so that UnsortedSegmentSum, in the gradient, # is happy with the type of its input 'segment_ids', which # must be int32. idx = tf.cast( tf.reshape( tf.where(tf.equal(n['beam_ids'], beam_id)), [-1]), tf.int32) beam_scores = tf.reshape(tf.gather(n['all_path_scores'], idx), [1, -1]) num = tf.shape(idx) return tf.nn.softmax_cross_entropy_with_logits( labels=tf.expand_dims( tf.sparse_to_dense(beam_gold_slot, num, [1.], 0.), 0), logits=beam_scores) # The conditional here is needed to deal with the last few batches of the # corpus which can contain -1 in beam_gold_slot for empty batch slots. cross_entropies.append(cf.cond( beam_gold_slot[0] >= 0, _ComputeCrossEntropy, _Pass)) return {'cross_entropy': tf.div(tf.add_n(cross_entropies), batch_size)}