我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用tensorflow.slice()。
def omniglot(): sess = tf.InteractiveSession() """ def wrapper(v): return tf.Print(v, [v], message="Printing v") v = tf.Variable(initial_value=np.arange(0, 36).reshape((6, 6)), dtype=tf.float32, name='Matrix') sess.run(tf.global_variables_initializer()) sess.run(tf.local_variables_initializer()) temp = tf.Variable(initial_value=np.arange(0, 36).reshape((6, 6)), dtype=tf.float32, name='temp') temp = wrapper(v) #with tf.control_dependencies([temp]): temp.eval() print 'Hello'""" def update_tensor(V, dim2, val): # Update tensor V, with index(:,dim2[:]) by val[:] val = tf.cast(val, V.dtype) def body(_, (v, d2, chg)): d2_int = tf.cast(d2, tf.int32) return tf.slice(tf.concat_v2([v[:d2_int],[chg] ,v[d2_int+1:]], axis=0), [0], [v.get_shape().as_list()[0]]) Z = tf.scan(body, elems=(V, dim2, val), initializer=tf.constant(1, shape=V.get_shape().as_list()[1:], dtype=tf.float32), name="Scan_Update") return Z
def resize_axis(tensor, axis, new_size, fill_value=0): tensor = tf.convert_to_tensor(tensor) shape = tf.unstack(tf.shape(tensor)) pad_shape = shape[:] pad_shape[axis] = tf.maximum(0, new_size - shape[axis]) shape[axis] = tf.minimum(shape[axis], new_size) shape = tf.stack(shape) resized = tf.concat([ tf.slice(tensor, tf.zeros_like(shape), shape), tf.fill(tf.stack(pad_shape), tf.cast(fill_value, tensor.dtype)) ], axis) # Update shape. new_shape = tensor.get_shape().as_list() # A copy is being made. new_shape[axis] = new_size resized.set_shape(new_shape) return resized
def _crop_pool_layer(self, bottom, rois, name): with tf.variable_scope(name) as scope: batch_ids = tf.squeeze(tf.slice(rois, [0, 0], [-1, 1], name="batch_id"), [1]) # Get the normalized coordinates of bboxes bottom_shape = tf.shape(bottom) height = (tf.to_float(bottom_shape[1]) - 1.) * np.float32(self._feat_stride[0]) width = (tf.to_float(bottom_shape[2]) - 1.) * np.float32(self._feat_stride[0]) x1 = tf.slice(rois, [0, 1], [-1, 1], name="x1") / width y1 = tf.slice(rois, [0, 2], [-1, 1], name="y1") / height x2 = tf.slice(rois, [0, 3], [-1, 1], name="x2") / width y2 = tf.slice(rois, [0, 4], [-1, 1], name="y2") / height # Won't be back-propagated to rois anyway, but to save time bboxes = tf.stop_gradient(tf.concat([y1, x1, y2, x2], 1)) if cfg.RESNET.MAX_POOL: pre_pool_size = cfg.POOLING_SIZE * 2 crops = tf.image.crop_and_resize(bottom, bboxes, tf.to_int32(batch_ids), [pre_pool_size, pre_pool_size], name="crops") crops = slim.max_pool2d(crops, [2, 2], padding='SAME') else: crops = tf.image.crop_and_resize(bottom, bboxes, tf.to_int32(batch_ids), [cfg.POOLING_SIZE, cfg.POOLING_SIZE], name="crops") return crops # Do the first few layers manually, because 'SAME' padding can behave inconsistently # for images of different sizes: sometimes 0, sometimes 1
def _crop_pool_layer(self, bottom, rois, name): with tf.variable_scope(name) as scope: batch_ids = tf.squeeze(tf.slice(rois, [0, 0], [-1, 1], name="batch_id"), [1]) # Get the normalized coordinates of bounding boxes bottom_shape = tf.shape(bottom) height = (tf.to_float(bottom_shape[1]) - 1.) * np.float32(self._feat_stride[0]) width = (tf.to_float(bottom_shape[2]) - 1.) * np.float32(self._feat_stride[0]) x1 = tf.slice(rois, [0, 1], [-1, 1], name="x1") / width y1 = tf.slice(rois, [0, 2], [-1, 1], name="y1") / height x2 = tf.slice(rois, [0, 3], [-1, 1], name="x2") / width y2 = tf.slice(rois, [0, 4], [-1, 1], name="y2") / height # Won't be back-propagated to rois anyway, but to save time bboxes = tf.stop_gradient(tf.concat([y1, x1, y2, x2], axis=1)) pre_pool_size = cfg.POOLING_SIZE * 2 crops = tf.image.crop_and_resize(bottom, bboxes, tf.to_int32(batch_ids), [pre_pool_size, pre_pool_size], name="crops") return slim.max_pool2d(crops, [2, 2], padding='SAME')
def get_image_summary(img, idx=0): """ Make an image summary for 4d tensor image with index idx """ V = tf.slice(img, (0, 0, 0, idx), (1, -1, -1, 1)) V -= tf.reduce_min(V) V /= tf.reduce_max(V) V *= 255 img_w = tf.shape(img)[1] img_h = tf.shape(img)[2] V = tf.reshape(V, tf.stack((img_w, img_h, 1))) V = tf.transpose(V, (2, 0, 1)) V = tf.reshape(V, tf.stack((-1, img_w, img_h, 1))) return V
def call(self, inputs, mask=None, initial_state=None, training=None): inputs_shape = K.shape(inputs) zeros = tf.zeros( shape=[ inputs_shape[0], inputs_shape[1] - 1, self.layer.units ] ) outputs = self.layer.call( inputs=inputs, mask=mask, initial_state=initial_state, training=training ) outputs = K.reshape( tf.slice(outputs, [0, inputs_shape[1] - 1, 0], [-1, 1, -1]), shape=(inputs_shape[0], 1, self.layer.units) ) outputs = K.concatenate([outputs, zeros], axis=1) if 0 < self.layer.dropout + self.layer.recurrent_dropout: outputs._uses_learning_phase = True return outputs
def adjust_hue(image, delta, name=None): with ops.op_scope([image], name, 'adjust_hue') as name: # Remember original dtype to so we can convert back if needed orig_dtype = image.dtype flt_image = tf.image.convert_image_dtype(image, tf.float32) hsv = gen_image_ops.rgb_to_hsv(flt_image) hue = tf.slice(hsv, [0, 0, 0, 0], [-1, -1, -1, 1]) saturation = tf.slice(hsv, [0, 0, 0, 1], [-1, -1, -1, 1]) value = tf.slice(hsv, [0, 0, 0, 2], [-1, -1, -1, 1]) # Note that we add 2*pi to guarantee that the resulting hue is a positive # floating point number since delta is [-0.5, 0.5]. hue = math_ops.mod(hue + (delta + 1.), 1.) hsv_altered = tf.concat(3, [hue, saturation, value]) rgb_altered = gen_image_ops.hsv_to_rgb(hsv_altered) return tf.image.convert_image_dtype(rgb_altered, orig_dtype)
def adjust_saturation(image, saturation_factor, name=None): with ops.op_scope([image], name, 'adjust_saturation') as name: # Remember original dtype to so we can convert back if needed orig_dtype = image.dtype flt_image = tf.image.convert_image_dtype(image, tf.float32) hsv = gen_image_ops.rgb_to_hsv(flt_image) hue = tf.slice(hsv, [0, 0, 0, 0], [-1, -1, -1, 1]) saturation = tf.slice(hsv, [0, 0, 0, 1], [-1, -1, -1, 1]) value = tf.slice(hsv, [0, 0, 0, 2], [-1, -1, -1, 1]) saturation *= saturation_factor saturation = clip_ops.clip_by_value(saturation, 0.0, 1.0) hsv_altered = tf.concat(3, [hue, saturation, value]) rgb_altered = gen_image_ops.hsv_to_rgb(hsv_altered) return tf.image.convert_image_dtype(rgb_altered, orig_dtype)
def split(x, split_dim, split_sizes): n = len(list(x.get_shape())) dim_size = np.sum(split_sizes) assert int(x.get_shape()[split_dim]) == dim_size ids = np.cumsum([0] + split_sizes) ids[-1] = -1 begin_ids = ids[:-1] ret = [] for i in range(len(split_sizes)): cur_begin = np.zeros([n], dtype=np.int32) cur_begin[split_dim] = begin_ids[i] cur_end = np.zeros([n], dtype=np.int32) - 1 cur_end[split_dim] = split_sizes[i] ret += [tf.slice(x, cur_begin, cur_end)] return ret
def __init__(self, lr, s_size, a_size): self.state_in = tf.placeholder(shape=[1], dtype=tf.int32) state_in_OH = slim.one_hot_encoding(self.state_in, s_size) output = slim.fully_connected(state_in_OH, a_size, biases_initializer=None, activation_fn=tf.nn.sigmoid, weights_initializer=tf.ones_initializer()) self.output = tf.reshape(output, [-1]) self.chosen_action = tf.argmax(self.output, 0) self.reward_holder = tf.placeholder(shape=[1], dtype=tf.float32) self.action_holder = tf.placeholder(shape=[1], dtype=tf.int32) self.responsible_weight = tf.slice(self.output, self.action_holder, [1]) self.loss = -(tf.log(self.responsible_weight) * self.reward_holder) optimizer = tf.train.GradientDescentOptimizer(learning_rate=lr) self.update = optimizer.minimize(self.loss)
def __call__(self, inputs, state, scope=None): num_proj = self._num_units if self._num_proj is None else self._num_proj c_prev = tf.slice(state, [0, 0], [-1, self._num_units]) m_prev = tf.slice(state, [0, self._num_units], [-1, num_proj]) input_size = inputs.get_shape().with_rank(2)[1] if input_size.value is None: raise ValueError("Could not infer input size from inputs.get_shape()[-1]") with tf.variable_scope(type(self).__name__, initializer=self._initializer): # "LSTMCell" # i = input_gate, j = new_input, f = forget_gate, o = output_gate cell_inputs = tf.concat(1, [inputs, m_prev]) lstm_matrix = tf.nn.bias_add(tf.matmul(cell_inputs, self._concat_w), self._b) i, j, f, o = tf.split(1, 4, lstm_matrix) c = tf.sigmoid(f + 1.0) * c_prev + tf.sigmoid(i) * tf.tanh(j) m = tf.sigmoid(o) * tf.tanh(c) if self._num_proj is not None: m = tf.matmul(m, self._concat_w_proj) new_state = tf.concat(1, [c, m]) return m, new_state
def process_image(img, scale, isotropic, crop, mean): '''Crops, scales, and normalizes the given image. scale : The image wil be first scaled to this size. If isotropic is true, the smaller side is rescaled to this, preserving the aspect ratio. crop : After scaling, a central crop of this size is taken. mean : Subtracted from the image ''' # Rescale if isotropic: img_shape = tf.to_float(tf.shape(img)[:2]) min_length = tf.minimum(img_shape[0], img_shape[1]) new_shape = tf.to_int32((scale / min_length) * img_shape) else: new_shape = tf.pack([scale, scale]) img = tf.image.resize_images(img, new_shape[0], new_shape[1]) # Center crop # Use the slice workaround until crop_to_bounding_box supports deferred tensor shapes # See: https://github.com/tensorflow/tensorflow/issues/521 offset = (new_shape - crop) / 2 img = tf.slice(img, begin=tf.pack([offset[0], offset[1], 0]), size=tf.pack([crop, crop, -1])) # Mean subtraction return tf.to_float(img) - mean
def test_fgm_gradient_max(): input_dim = 2 num_classes = 3 batch_size = 4 rng = np.random.RandomState([2017, 8, 23]) x = tf.placeholder(tf.float32, [batch_size, input_dim]) weights = tf.placeholder(tf.float32, [input_dim, num_classes]) logits = tf.matmul(x, weights) probs = tf.nn.softmax(logits) adv_x = fgm(x, probs) random_example = rng.randint(batch_size) random_feature = rng.randint(input_dim) output = tf.slice(adv_x, [random_example, random_feature], [1, 1]) dx, = tf.gradients(output, x) # The following line catches GitHub issue #243 assert dx is not None sess = tf.Session() dx = sess.run(dx, feed_dict=random_feed_dict(rng, [x, weights])) ground_truth = np.zeros((batch_size, input_dim)) ground_truth[random_example, random_feature] = 1. assert np.allclose(dx, ground_truth), (dx, ground_truth)
def pre(self, inputs, scope=None): """Preprocess inputs to be used by the cell. Assumes [N, J, *] [x, u]""" is_train = self._is_train keep_prob = self._keep_prob gate_size = self._gate_size with tf.variable_scope(scope or "pre"): x, u, _, _ = tf.split(2, 4, tf.slice(inputs, [0, 0, gate_size], [-1, -1, -1])) # [N, J, d] a_raw = linear([x * u], gate_size, True, scope='a_raw', var_on_cpu=self._var_on_cpu, wd=self._wd, initializer=self._initializer) a = tf.sigmoid(a_raw - self._forget_bias, name='a') if keep_prob < 1.0: x = tf.cond(is_train, lambda: tf.nn.dropout(x, keep_prob), lambda: x) u = tf.cond(is_train, lambda: tf.nn.dropout(u, keep_prob), lambda: u) v_t = tf.nn.tanh(linear([x, u], self._num_units, True, var_on_cpu=self._var_on_cpu, wd=self._wd, scope='v_raw'), name='v') new_inputs = tf.concat(2, [a, x, u, v_t]) # [N, J, 3*d + 1] return new_inputs
def __call__(self, inputs, state, scope=None): gate_size = self._gate_size with tf.variable_scope(scope or type(self).__name__): # "RSMCell" with tf.name_scope("Split"): # Reset gate and update gate. a = tf.slice(inputs, [0, 0], [-1, gate_size]) x, u, v_t = tf.split(1, 3, tf.slice(inputs, [0, gate_size], [-1, -1])) o = tf.slice(state, [0, 0], [-1, 1]) h, v = tf.split(1, 2, tf.slice(state, [0, gate_size], [-1, -1])) with tf.variable_scope("Main"): r_raw = linear([x * u], 1, True, scope='r_raw', var_on_cpu=self._var_on_cpu, initializer=self._initializer) r = tf.sigmoid(r_raw, name='a') new_o = a * r + (1 - a) * o new_v = a * v_t + (1 - a) * v g = r * v_t new_h = a * g + (1 - a) * h with tf.name_scope("Concat"): new_state = tf.concat(1, [new_o, new_h, new_v]) outputs = tf.concat(1, [a, r, x, new_h, new_v, g]) return outputs, new_state
def __call__(self, u_t, a, b, scope=None): """ :param u_t: [N, M, d] :param a: [N, M. d] :param b: [N, M. d] :param mask: [N, M] :return: """ N, M, d = self.batch_size, self.mem_size, self.hidden_size L, sL = self.L, self.sL with tf.name_scope(scope or self.__class__.__name__): L = tf.tile(tf.expand_dims(tf.expand_dims(L, 0), 0), [N, d, 1, 1]) sL = tf.tile(tf.expand_dims(tf.expand_dims(sL, 0), 0), [N, d, 1, 1]) logb = tf.log(b + 1e-9) # [N, M, d] logb = tf.concat(1, [tf.zeros([N, 1, d]), tf.slice(logb, [0, 1, 0], [-1, -1, -1])]) # [N, M, d] logb = tf.expand_dims(tf.transpose(logb, [0, 2, 1]), -1) # [N, d, M, 1] left = L * tf.exp(tf.batch_matmul(L, logb * sL)) # [N, d, M, M] right = a * u_t # [N, M, d] right = tf.expand_dims(tf.transpose(right, [0, 2, 1]), -1) # [N, d, M, 1] u = tf.batch_matmul(left, right) # [N, d, M, 1] u = tf.transpose(tf.squeeze(u, [3]), [0, 2, 1]) # [N, M, d] return u
def __call__(self, u_t, a, b, scope=None): """ :param u_t: [N, M, d] :param a: [N, M. 1] :param b: [N, M. 1] :param mask: [N, M] :return: """ N, M, d = self.batch_size, self.mem_size, self.hidden_size L, sL = self.L, self.sL with tf.name_scope(scope or self.__class__.__name__): L = tf.tile(tf.expand_dims(L, 0), [N, 1, 1]) sL = tf.tile(tf.expand_dims(sL, 0), [N, 1, 1]) logb = tf.log(b + 1e-9) logb = tf.concat(1, [tf.zeros([N, 1, 1]), tf.slice(logb, [0, 1, 0], [-1, -1, -1])]) left = L * tf.exp(tf.batch_matmul(L, logb * sL)) # [N, M, M] right = a * u_t # [N, M, d] u = tf.batch_matmul(left, right) # [N, M, d] return u
def categories_loss(self, categories, layer): gan = self.gan loss = 0 batch_size = gan.batch_size() def split(layer): start = 0 ret = [] for category in categories: count = int(category.get_shape()[1]) ret.append(tf.slice(layer, [0, start], [batch_size, count])) start += count return ret for category,layer_s in zip(categories, split(layer)): size = int(category.get_shape()[1]) category_prior = tf.ones([batch_size, size])*np.float32(1./size) logli_prior = tf.reduce_sum(tf.log(category_prior + TINY) * category, axis=1) layer_softmax = tf.nn.softmax(layer_s) logli = tf.reduce_sum(tf.log(layer_softmax+TINY)*category, axis=1) disc_ent = tf.reduce_mean(-logli_prior) disc_cross_ent = tf.reduce_mean(-logli) loss += disc_ent - disc_cross_ent return loss
def add_bw(gan, config, net): x = gan.inputs.x s = [int(x) for x in net.get_shape()] print("S IS ", s) shape = [s[1], s[2]] x = tf.image.resize_images(x, shape, 1) bwnet = tf.slice(net, [0, 0, 0, 0], [s[0],s[1],s[2], 3]) if not gan.config.add_full_image: print( "[colorizer] Adding black and white image", x) x = tf.image.rgb_to_grayscale(x) if config.colorizer_noise is not None: x += tf.random_normal(x.get_shape(), mean=0, stddev=config.colorizer_noise, dtype=tf.float32) #bwnet = tf.image.rgb_to_grayscale(bwnet) #x = tf.concat(axis=3, values=[x, bwnet]) else: print( "[colorizer] Adding full image", x) return x
def testComparison(self): # Here we compare the output with the tf.slice equivalent. in_shape = [2, 3, 4] inputs = tf.random_uniform(shape=in_shape) dims = [0, 2] begin = [1, 2] size = [1, 2] mod = snt.SliceByDim(dims=dims, begin=begin, size=size) output = mod(inputs) begin_tf = [1, 0, 2] size_tf = [1, -1, 2] ref_output = tf.slice(inputs, begin=begin_tf, size=size_tf) with self.test_session() as sess: actual, expected = sess.run([output, ref_output]) self.assertAllEqual(actual, expected)
def __call__(self, inputs, state, scope=None): """ :param inputs: [N*B, I + B] :param state: [N*B, d] :param scope: :return: [N*B, d] """ with tf.variable_scope(scope or self.__class__.__name__): d = self.state_size x = tf.slice(inputs, [0, 0], [-1, self._input_size]) # [N*B, I] mask = tf.slice(inputs, [0, self._input_size], [-1, -1]) # [N*B, B] B = tf.shape(mask)[1] prev_state = tf.expand_dims(tf.reshape(state, [-1, B, d]), 1) # [N, B, d] -> [N, 1, B, d] mask = tf.tile(tf.expand_dims(tf.reshape(mask, [-1, B, B]), -1), [1, 1, 1, d]) # [N, B, B, d] # prev_state = self._reduce_func(tf.tile(prev_state, [1, B, 1, 1]), 2) prev_state = self._reduce_func(exp_mask(prev_state, mask), 2) # [N, B, d] prev_state = tf.reshape(prev_state, [-1, d]) # [N*B, d] return self._cell(x, prev_state)
def __call__(self, inputs, state, scope=None): """ :param inputs: [N, d + JQ + JQ * d] :param state: [N, d] :param scope: :return: """ with tf.variable_scope(scope or self.__class__.__name__): c_prev, h_prev = state x = tf.slice(inputs, [0, 0], [-1, self._input_size]) q_mask = tf.slice(inputs, [0, self._input_size], [-1, self._q_len]) # [N, JQ] qs = tf.slice(inputs, [0, self._input_size + self._q_len], [-1, -1]) qs = tf.reshape(qs, [-1, self._q_len, self._input_size]) # [N, JQ, d] x_tiled = tf.tile(tf.expand_dims(x, 1), [1, self._q_len, 1]) # [N, JQ, d] h_prev_tiled = tf.tile(tf.expand_dims(h_prev, 1), [1, self._q_len, 1]) # [N, JQ, d] f = tf.tanh(linear([qs, x_tiled, h_prev_tiled], self._input_size, True, scope='f')) # [N, JQ, d] a = tf.nn.softmax(exp_mask(linear(f, 1, True, squeeze=True, scope='a'), q_mask)) # [N, JQ] q = tf.reduce_sum(qs * tf.expand_dims(a, -1), 1) z = tf.concat(1, [x, q]) # [N, 2d] return self._cell(z, state)
def _crop(image, offset_height, offset_width, crop_height, crop_width): original_shape = tf.shape(image) rank_assertion = tf.Assert( tf.equal(tf.rank(image), 3), ['Rank of image must be equal to 3.']) cropped_shape = control_flow_ops.with_dependencies( [rank_assertion], tf.stack([crop_height, crop_width, original_shape[2]])) size_assertion = tf.Assert( tf.logical_and( tf.greater_equal(original_shape[0], crop_height), tf.greater_equal(original_shape[1], crop_width)), ['Crop size greater than the image size.']) offsets = tf.to_int32(tf.stack([offset_height, offset_width, 0])) # Use tf.slice instead of crop_to_bounding box as it accepts tensors to # define the crop size. image = control_flow_ops.with_dependencies([size_assertion], tf.slice(image, offsets, cropped_shape)) return tf.reshape(image, cropped_shape)
def get_filled_box_idx(idx, top_left, bot_right): """Fill a box with top left and bottom right coordinates. Args: idx: [B, T, H, W, 2] or [B, H, W, 2] or [H, W, 2] top_left: [B, T, 2] or [B, 2] or [2] bot_right: [B, T, 2] or [B, 2] or [2] """ ss = tf.shape(idx) ndims = tf.shape(ss) batch = tf.slice(ss, [0], ndims - 3) coord_shape = tf.concat(0, [batch, tf.constant([1, 1, 2])]) top_left = tf.reshape(top_left, coord_shape) bot_right = tf.reshape(bot_right, coord_shape) lower = tf.reduce_prod(tf.to_float(idx >= top_left), ndims - 1) upper = tf.reduce_prod(tf.to_float(idx <= bot_right), ndims - 1) box = lower * upper return box
def get_opt_output(self): cost1 = tf.reduce_sum(tf.pow(self._cleaned1-self._labels1,2),2)+tf.reduce_sum(tf.pow(self._cleaned2-self._labels2,2),2) cost2 = tf.reduce_sum(tf.pow(self._cleaned2-self._labels1,2),2)+tf.reduce_sum(tf.pow(self._cleaned1-self._labels2,2),2) idx = tf.slice(cost1, [0, 0], [1, -1]) > tf.slice(cost2, [0, 0], [1, -1]) idx = tf.cast(idx, tf.float32) idx = tf.reduce_mean(idx,reduction_indices=0) idx = tf.reshape(idx, [tf.shape(idx)[0], 1]) x1 = self._cleaned1[0,:,:] * (1-idx) + self._cleaned2[0,:, :]*idx x2 = self._cleaned1[0,:,:]*idx + self._cleaned2[0,:,:]*(1-idx) row = tf.shape(x1)[0] col = tf.shape(x1)[1] x1 = tf.reshape(x1, [1, row, col]) x2 = tf.reshape(x2, [1, row, col]) return x1, x2
def _random_crop_and_resize_image(cls, image, bbox, height, width, val=False): with tf.name_scope('random_crop_and_resize'): if not val: # bbox_begin, bbox_size, distorted_bbox = \ bbox_begin, bbox_size, _ = \ tf.image.sample_distorted_bounding_box( tf.shape(image), bounding_boxes=bbox, min_object_covered=0.1, aspect_ratio_range=[0.8, 1.25], area_range=[0.1, 1.0], max_attempts=100, use_image_if_no_bounding_boxes=True) # Crop the image to the distorted bounding box image = tf.slice(image, bbox_begin, bbox_size) # Resize to the desired output size image = tf.image.resize_images( image, [height, width], tf.image.ResizeMethod.BILINEAR, align_corners=False) image.set_shape([height, width, 3]) return image
def _dynamic_crop(cls, inputs, static_shape, dynamic_shape, data_format='channels_last'): input_shape = cls.spatial_shape(inputs, data_format, True) n_channels = cls.num_channels(inputs, data_format) if data_format == 'channels_last': slice_size = [(-1,), dynamic_shape, (n_channels,)] output_shape = [None] * (len(static_shape) + 1) + [n_channels] else: slice_size = [(-1, n_channels), dynamic_shape] output_shape = [None, n_channels] + [None] * len(static_shape) begin = [0] * len(inputs.get_shape().as_list()) size = tf.concat(slice_size, axis=0) cond = tf.reduce_sum(tf.abs(input_shape - dynamic_shape)) > 0 x = tf.cond(cond, lambda: tf.slice(inputs, begin=begin, size=size), lambda: inputs) x.set_shape(output_shape) return x
def total_variation(image_batch): """ :param image_batch: A 4D tensor of shape [batch_size, width, height, channels] """ batch_shape = image_batch.get_shape().as_list() width = batch_shape[1] left = tf.slice(image_batch, [0, 0, 0, 0], [-1, width - 1, -1, -1]) right = tf.slice(image_batch, [0, 1, 0, 0], [-1, -1, -1, -1]) height = batch_shape[2] top = tf.slice(image_batch, [0, 0, 0, 0], [-1, -1, height - 1, -1]) bottom = tf.slice(image_batch, [0, 0, 1, 0], [-1, -1, -1, -1]) # left and right are 1 less wide than the original, top and bottom 1 less tall # In order to combine them, we take 1 off the height of left-right, and 1 off width of top-bottom horizontal_diff = tf.slice(tf.sub(left, right), [0, 0, 0, 0], [-1, -1, height - 1, -1]) vertical_diff = tf.slice(tf.sub(top, bottom), [0, 0, 0, 0], [-1, width - 1, -1, -1]) sum_of_pixel_diffs_squared = tf.add(tf.square(horizontal_diff), tf.square(vertical_diff)) total_variation = tf.reduce_sum(tf.sqrt(sum_of_pixel_diffs_squared)) # TODO: Should this be normalized by the number of pixels? return total_variation
def style_loss(self, layers): activations = [self.activations_for_layer(i) for i in layers] gramians = [self.gramian_for_layer(x) for x in layers] # Slices are for style and synth image gramian_diffs = [ tf.sub( tf.tile(tf.slice(g, [0, 0, 0], [self.num_style, -1, -1]), [self.num_synthesized - self.num_style + 1, 1, 1]), tf.slice(g, [self.num_style + self.num_content, 0, 0], [self.num_synthesized, -1, -1])) for g in gramians] Ns = [g.get_shape().as_list()[2] for g in gramians] Ms = [a.get_shape().as_list()[1] * a.get_shape().as_list()[2] for a in activations] scaled_diffs = [tf.square(g) for g in gramian_diffs] style_loss = tf.div( tf.add_n([tf.div(tf.reduce_sum(x), 4 * (N ** 2) * (M ** 2)) for x, N, M in zip(scaled_diffs, Ns, Ms)]), len(layers)) return style_loss
def _crop(image, offset_height, offset_width, crop_height, crop_width): original_shape = tf.shape(image) rank_assertion = tf.Assert( tf.equal(tf.rank(image), 3), ['Rank of image must be equal to 3.']) cropped_shape = control_flow_ops.with_dependencies( [rank_assertion], tf.stack([crop_height, crop_width, original_shape[2]])) size_assertion = tf.Assert( tf.logical_and( tf.greater_equal(original_shape[0], crop_height), tf.greater_equal(original_shape[1], crop_width)), ['Crop size greater than the image size.']) offsets = tf.to_int32(tf.stack([offset_height, offset_width, 0])) # Use tf.slice instead of crop_to_bounding box as it accepts tensors to # define the crop size. image = control_flow_ops.with_dependencies( [size_assertion], tf.slice(image, offsets, cropped_shape)) return tf.reshape(image, cropped_shape)
def build_discriminator(x_data, x_generated, keep_prob): x_data=tf.unstack(x_data,seq_size,1); x_generated=list(x_generated); x_in = tf.concat([x_data, x_generated],1); x_in=tf.unstack(x_in,seq_size,0); lstm_cell = tf.contrib.rnn.MultiRNNCell([tf.contrib.rnn.DropoutWrapper(tf.contrib.rnn.BasicLSTMCell(n_hidden), output_keep_prob=keep_prob) for _ in range(d_num_layers)]); with tf.variable_scope("dis") as dis: weights=tf.Variable(tf.random_normal([n_hidden, 1])); biases=tf.Variable(tf.random_normal([1])); outputs, states = tf.contrib.rnn.static_rnn(lstm_cell, x_in, dtype=tf.float32); res=tf.matmul(outputs[-1], weights) + biases; y_data = tf.nn.sigmoid(tf.slice(res, [0, 0], [batch_size, -1], name=None)); y_generated = tf.nn.sigmoid(tf.slice(res, [batch_size, 0], [-1, -1], name=None)); d_params=[v for v in tf.global_variables() if v.name.startswith(dis.name)]; with tf.name_scope("desc_params"): for param in d_params: variable_summaries(param); return y_data, y_generated, d_params;
def random_shift(self, images, states, actions): print 'shifting the video sequence randomly in time' tshift = 2 uselen = self.conf['use_len'] fulllength = self.conf['sequence_length'] nshifts = (fulllength - uselen) / 2 + 1 rand_ind = tf.random_uniform([1], 0, nshifts, dtype=tf.int64) self.rand_ind = rand_ind start = tf.concat(axis=0,values=[tf.zeros(1, dtype=tf.int64), rand_ind * tshift, tf.zeros(3, dtype=tf.int64)]) images_sel = tf.slice(images, start, [-1, uselen, -1, -1, -1]) start = tf.concat(axis=0, values=[tf.zeros(1, dtype=tf.int64), rand_ind * tshift, tf.zeros(1, dtype=tf.int64)]) actions_sel = tf.slice(actions, start, [-1, uselen, -1]) start = tf.concat(axis=0, values=[tf.zeros(1, dtype=tf.int64), rand_ind * tshift, tf.zeros(1, dtype=tf.int64)]) states_sel = tf.slice(states, start, [-1, uselen, -1]) return images_sel, states_sel, actions_sel
def __init__(self, conf, gpu_id, start_images, actions, start_states, pix_distrib1,pix_distrib2): nsmp_per_gpu = conf['batch_size']/ conf['ngpu'] # picking different subset of the actions for each gpu startidx = gpu_id * nsmp_per_gpu actions = tf.slice(actions, [startidx, 0, 0], [nsmp_per_gpu, -1, -1]) start_images = tf.slice(start_images, [startidx, 0, 0, 0, 0], [nsmp_per_gpu, -1, -1, -1, -1]) start_states = tf.slice(start_states, [startidx, 0, 0], [nsmp_per_gpu, -1, -1]) pix_distrib1 = tf.slice(pix_distrib1, [startidx, 0, 0, 0, 0], [nsmp_per_gpu, -1, -1, -1, -1]) pix_distrib2 = tf.slice(pix_distrib2, [startidx, 0, 0, 0, 0], [nsmp_per_gpu, -1, -1, -1, -1]) print 'startindex for gpu {0}: {1}'.format(gpu_id, startidx) from prediction_train_sawyer import Model if 'ndesig' in conf: self.model = Model(conf, start_images, actions, start_states, pix_distrib=pix_distrib1,pix_distrib2=pix_distrib2, inference=True) # self.model = Model(conf, start_images, actions, start_states, pix_distrib=pix_distrib1, # pix_distrib2=pix_distrib2, # reuse_scope=reuse_scope) else: # self.model = Model(conf,start_images,actions,start_states, pix_distrib=pix_distrib1, reuse_scope= reuse_scope) self.model = Model(conf, start_images, actions, start_states, pix_distrib=pix_distrib1, inference=True)
def _attention(query, attn_states, is_training, reuse, attn_size, attn_vec_size, attn_length, trainable=True, name='attention'): with tf.variable_scope(name, reuse=reuse): v = tf.get_variable( name="V", shape=[attn_vec_size], trainable=trainable) attn_states_reshaped = tf.reshape( attn_states, shape=[-1, attn_length, 1, attn_size]) attn_conv = conv2d(attn_states_reshaped, attn_vec_size, is_training, reuse, filter_size=( 1, 1), stride=(1, 1), trainable=trainable, use_bias=False) y = _linear(query, attn_vec_size, reuse) y = tf.reshape(y, [-1, 1, 1, attn_vec_size]) s = tf.reduce_sum(v * tf.tanh(attn_conv + y), [2, 3]) a = softmax(s) d = tf.reduce_sum(tf.reshape( a, [-1, attn_length, 1, 1]) * attn_states_reshaped, [1, 2]) new_attns = tf.reshape(d, [-1, attn_size]) new_attn_states = tf.slice(attn_states, [0, 1, 0], [-1, -1, -1]) return new_attns, new_attn_states
def crop_and_concat(inputs1, inputs2, name='crop_concat'): """Concates two features maps concates different sizes feature maps cropping the larger map concatenation across output channels Args: inputs1: A `Tensor` inputs2: A `Tensor` Returns: concated output tensor """ with tf.name_scope(name): inputs1_shape = tf.shape(inputs1) inputs2_shape = tf.shape(inputs2) # offsets for the top left corner of the crop offsets = [0, (inputs1_shape[1] - inputs2_shape[1]) // 2, (inputs1_shape[2] - inputs2_shape[2]) // 2, 0] size = [-1, inputs2_shape[1], inputs2_shape[2], -1] inputs1_crop = tf.slice(inputs1, offsets, size) return tf.concat([inputs1_crop, inputs2], axis=3)
def resize_axis(tensor, axis, new_size, fill_value=0): """Truncates or pads a tensor to new_size on on a given axis. Truncate or extend tensor such that tensor.shape[axis] == new_size. If the size increases, the padding will be performed at the end, using fill_value. Args: tensor: The tensor to be resized. axis: An integer representing the dimension to be sliced. new_size: An integer or 0d tensor representing the new value for tensor.shape[axis]. fill_value: Value to use to fill any new entries in the tensor. Will be cast to the type of tensor. Returns: The resized tensor. """ tensor = tf.convert_to_tensor(tensor) shape = tf.unstack(tf.shape(tensor)) pad_shape = shape[:] pad_shape[axis] = tf.maximum(0, new_size - shape[axis]) shape[axis] = tf.minimum(shape[axis], new_size) shape = tf.stack(shape) resized = tf.concat([ tf.slice(tensor, tf.zeros_like(shape), shape), tf.fill(tf.stack(pad_shape), tf.cast(fill_value, tensor.dtype)) ], axis) # Update shape. new_shape = tensor.get_shape().as_list() # A copy is being made. new_shape[axis] = new_size resized.set_shape(new_shape) return resized
def get_support(self, labels, support_type=None): if support_type == None: support_type = FLAGS.support_type if "," in support_type: new_labels = [] for st in support_type.split(","): new_labels.append(tf.cast(self.get_support(labels, st), dtype=tf.float32)) support_labels = tf.concat(new_labels, axis=1) return support_labels elif support_type == "vertical": num_classes = FLAGS.num_classes num_verticals = FLAGS.num_verticals vertical_file = FLAGS.vertical_file vertical_mapping = np.zeros([num_classes, num_verticals], dtype=np.float32) float_labels = tf.cast(labels, dtype=tf.float32) with open(vertical_file) as F: for line in F: group = map(int, line.strip().split()) if len(group) == 2: x, y = group vertical_mapping[x, y] = 1 vm_init = tf.constant_initializer(vertical_mapping) vm = tf.get_variable("vm", shape = [num_classes, num_verticals], trainable=False, initializer=vm_init) vertical_labels = tf.matmul(float_labels, vm) return tf.cast(vertical_labels > 0.2, tf.float32) elif support_type == "frequent": num_frequents = FLAGS.num_frequents frequent_labels = tf.slice(labels, begin=[0, 0], size=[-1, num_frequents]) frequent_labels = tf.cast(frequent_labels, dtype=tf.float32) return frequent_labels elif support_type == "label": float_labels = tf.cast(labels, dtype=tf.float32) return float_labels else: raise NotImplementedError()
def model_from_position(cls, layer_descriptions, position_tensor, input_tensor, use_softmax=False): """ Creates TF model from the specified position and description. """ offset = 0 model = input_tensor for i in range(1, len(layer_descriptions)): previous_layer = layer_descriptions[i - 1] current_layer = layer_descriptions[i] previous_layer_size = previous_layer[0] current_layer_size = current_layer[0] weights_size = previous_layer_size * current_layer_size biases_size = current_layer_size weights = tf.slice(position_tensor, [0, offset], [1, weights_size]) weights = tf.reshape(weights, shape=[previous_layer_size, current_layer_size]) offset += weights_size biases = tf.slice(position_tensor, [0, offset], [1, biases_size]) biases = tf.reshape(biases, shape=[1, biases_size]) offset += biases_size model = tf.matmul(model, weights) + biases if i != len(layer_descriptions) - 1: model = tf.nn.relu(model) elif use_softmax and layer_descriptions[-1][0] > 1: model = tf.nn.softmax(model) return model
def crop_and_concat(x1, x2): x1_shape = x1.get_shape().as_list() x2_shape = x2.get_shape().as_list() offsets = [0, (x1_shape[1] - x2_shape[1]) // 2, (x1_shape[2] - x2_shape[2]) // 2, (x1_shape[3] - x2_shape[3]) // 2, 0] size = [-1, x2_shape[1], x2_shape[2], x2_shape[3], -1] x1_crop = tf.slice(x1, offsets, size) return tf.concat([x1_crop, x2], 4) # Some code from https://github.com/shiba24/3d-unet.git