def texture_loss(x, p=16): _, h, w, c = x.get_shape().as_list() x = normalize(x) assert h % p == 0 and w % p == 0 logger.info( 'Create texture loss for layer {} with shape {}'. format(x.name, x.get_shape())) x = tf.space_to_batch_nd( x, [p, p], [[0, 0], [0, 0]]) # [b * ?, h/p, w/p, c] x = tf.reshape(x, [p, p, -1, h // p, w // p, c ]) # [p, p, b, h/p, w/p, c] x = tf.transpose( x, [2, 3, 4, 0, 1, 5]) # [b * ?, p, p, c] patches_a, _, patches_b = tf.split( x, 3, axis=0 ) # each is b,h/p,w/p,p,p,c; split to render, _image, style patches_a = tf.reshape( patches_a, [-1, p, p, c]) # [b * ?, p, p, c] patches_b = tf.reshape( patches_b, [-1, p, p, c]) # [b * ?, p, p, c] return tf.losses.mean_squared_error( gram_matrix(patches_a), gram_matrix(patches_b), reduction=tf.losses.Reduction.MEAN)
def __enter__(self): if self.dilation_factor > 1: self._tensor = tf.space_to_batch_nd(self._tensor, self.block_shape, self.zero_paddings, name='dilated') return self
def build_graph(parameters): """Build a space_to_batch graph given `parameters`.""" input_tensor = tf.placeholder(dtype=parameters["dtype"], name="input", shape=parameters["input_shape"]) input_tensors = [input_tensor] # Get block_shape either as a const or as a placeholder (tensor). if parameters["constant_block_shape"]: block_shape = parameters["block_shape"] else: shape = [len(parameters["block_shape"])] block_shape = tf.placeholder(dtype=tf.int32, name="shape", shape=shape) input_tensors.append(block_shape) # Get paddings either as a const or as a placeholder (tensor). if parameters["constant_paddings"]: paddings = parameters["paddings"] else: shape = [len(parameters["paddings"]), 2] paddings = tf.placeholder(dtype=tf.int32, name="paddings", shape=shape) input_tensors.append(paddings) out = tf.space_to_batch_nd(input_tensor, block_shape, paddings) return input_tensors, [out]
def tf_op(patch_size, x): # [256,64,16,16] -> [256,16,16,64] x = torch.transpose(x, 1, 3) a, b, crop_size, c = x.size() c = x.shape[3] # x.numpy() <- pytorch_Tensor 轉 numpy # tf.convert_to_tensor() <- numpy 轉 tf tf_x = tf.convert_to_tensor(x.cpu().numpy()) #tf_x = normalize(tf_x) assert crop_size % patch_size == 0 and crop_size % patch_size == 0 # [b * ?, h/p, w/p, c] [32,128,128,64]->[8192,8,8,64] tf_x = tf.space_to_batch_nd(tf_x, [patch_size, patch_size], [[0, 0], [0, 0]]) # [p, p, b, h/p, w/p, c] [8192,8,8,64]->[16,16,32,8,8,64] tf_x = tf.reshape(tf_x, [ patch_size, patch_size, -1, crop_size // patch_size, crop_size // patch_size, c ]) # [b * ?, p, p, c] [16,16,32,8,8,64]->[32,8,8,16,16,64] tf_x = tf.transpose(tf_x, [2, 3, 4, 0, 1, 5]) patches_tf_x = tf.reshape(tf_x, [-1, patch_size, patch_size, c]) return patches_tf_x
def space_to_batch_execution(): # about batch # https://stackoverflow.com/questions/41175401/what-is-a-batch-in-tensorflow with tf.Graph().as_default(), tf.Session(): a = tf.constant([[[[1], [2]], [[3], [4]]]]) print(a.get_shape()) # from a.get_shape() possible to get data for such things as 4-D with shape [batch, height, width, depth]. print("##########") # paddings in general change indexes by step equal padding/block_size # add additional zeros # block_size: Non-overlapping blocks of size block_size x block size in # the height and width dimensions are rearranged into the batch dimension at each location. # (so should save indexes of element from old to new?) # use to understand how much parts will be created from initial data stb = tf.space_to_batch(a, paddings=[[0, 0], [0, 0]], block_size=2) print(stb.get_shape()) print(stb.eval()) print("##########") # customize out put # divides "spatial" dimensions [1, ..., M] of the input into a grid of blocks of shape block_shape stbn = tf.space_to_batch_nd(a, block_shape=[1, 2], paddings=[[0, 0], [0, 0]]) print(stbn.get_shape()) print(stbn.eval())
def build_graph(parameters): input_tensor = tf.placeholder( dtype=parameters["dtype"], name="input", shape=parameters["input_shape"]) out = tf.space_to_batch_nd(input_tensor, parameters["block_shape"], parameters["paddings"]) return [input_tensor], [out]
def _testStaticShape(self, input_shape, block_shape, paddings, error): block_shape = np.array(block_shape) paddings = np.array(paddings) # Try with sizes known at graph construction time. with self.assertRaises(error): _ = tf.space_to_batch_nd( np.zeros(input_shape, np.float32), block_shape, paddings)
def space_to_batch_nd(self, block_shape, paddings): with tf.name_scope("crt_space_to_batch_nd"): backing = [ tf.space_to_batch_nd(xi, block_shape=block_shape, paddings=paddings) for xi in self.backing ] return DenseTensor(backing)
def _testPad(self, inputs, block_shape, paddings, outputs): block_shape = np.array(block_shape) paddings = np.array(paddings).reshape((len(block_shape), 2)) for use_gpu in [False, True]: with self.test_session(use_gpu=use_gpu): # outputs = space_to_batch(inputs) x_tf = tf.space_to_batch_nd(tf.to_float(inputs), block_shape, paddings) self.assertAllEqual(x_tf.eval(), outputs) # inputs = batch_to_space(outputs) x_tf = tf.batch_to_space_nd(tf.to_float(outputs), block_shape, paddings) self.assertAllEqual(x_tf.eval(), inputs)
def extract_patches(image, patch_size, padding="VALID", rates=[1, 1, 1, 1], strides=None, ksizes=None): """ Function which splits image of shape [height, width, channels] into patches with more convenient ordering than the built in tensorflow function tf.image.extract_image_patches (https://www.tensorflow.org/versions/r1.12/api_docs/python/tf/image/extract_image_patches) Returns ------- patches: image patches of shape [num_patches, patch_size, patch_size, channels] """ # Set strides and ksizes if not passed as arguments if strides is None: strides = [1, patch_size, patch_size, 1] if ksizes is None: ksizes = [1, patch_size, patch_size, 1] height, width, channels = image.get_shape().as_list() # Extract patches from the image using tensorflow function patches = tf.image.extract_image_patches(tf.split(image, channels, axis=2), ksizes, strides, rates, padding) patches = tf.expand_dims(patches, 0) #num_patches = [int(image.shape.as_list()[0] / patch_size), int(image.shape.as_list()[1] / patch_size)] num_patches = [patches.shape.as_list()[2], patches.shape.as_list()[3]] num_patches = tf.shape(patches)[2:4] # Change grid layout of [1, channels, x, y, pixels] to [patches, channels, 1, 1, pixels] patches = tf.space_to_batch_nd( patches, tf.concat([tf.ones([1], dtype=tf.int32), num_patches], 0), [[0, 0], [0, 0], [0, 0]]) # Squeeze [patches, channels, 1, 1, pixels] to [patches, channels, pixels] patches = tf.squeeze(patches, axis=[2, 3]) # Change shape to channels last: [patches, channels, pixels] to [patches, pixels, channels] patches = tf.transpose(patches, perm=[0, 2, 1]) # Reshape from [patches, pixels, channels] to [patches, rows, cols, channels] patches = tf.reshape( patches, tf.stack( [tf.reduce_prod(num_patches), patch_size, patch_size, channels])) return patches
def _testDynamicShape(self, input_shape, block_shape, paddings): block_shape = np.array(block_shape) paddings = np.array(paddings) # Try with sizes unknown at graph construction time. input_placeholder = tf.placeholder(tf.float32) block_shape_placeholder = tf.placeholder(tf.int32, shape=block_shape.shape) paddings_placeholder = tf.placeholder(tf.int32) t = tf.space_to_batch_nd(input_placeholder, block_shape_placeholder, paddings_placeholder) with self.assertRaises(ValueError): _ = t.eval({input_placeholder: np.zeros(input_shape, np.float32), block_shape_placeholder: block_shape, paddings_placeholder: paddings})
def extract_patches(image, p, shape=None): if shape: bs, h, w, c = shape else: bs, h, w, c = image.shape # Image to Patches Conversion pad = [[0, 0], [0, 0]] patches = tf.space_to_batch_nd(image, [p, p], pad) patches = tf.split(patches, p * p, 0) patches = tf.stack(patches, 3) patches = tf.reshape(patches, [bs * (h // p) * (w // p), p, p, c]) return patches
def create_space_to_batch_net(self, in_shape, pads_value, block_shape_value, out_shape, ir_version, use_new_frontend): """ Tensorflow net IR net Input->SpaceToBatch => Input->SpaceToBatch """ # # Create Tensorflow model # import tensorflow as tf tf.compat.v1.reset_default_graph() # Create the graph and model with tf.compat.v1.Session() as sess: x = tf.compat.v1.placeholder(tf.float32, in_shape, 'Input') pads = tf.constant(pads_value) block_shape = tf.constant(block_shape_value) tf.space_to_batch_nd(x, block_shape, pads, name='Operation') tf.compat.v1.global_variables_initializer() tf_net = sess.graph_def # # Create reference IR net # Please, specify 'type': 'Input' for input node # Moreover, do not forget to validate ALL layer attributes!!! # ref_net = None return tf_net, ref_net
def get_img_patches(img, patch_size): # getinitial data p = patch_size #patch size h = tf.shape(img)[1] #image size c = tf.shape(img)[3] #image channels # Image to Patches Conversion pad = [[0, 0], [0, 0]] patches = tf.space_to_batch_nd(img, [p, p], pad) patches = tf.split(patches, p * p, 0) patches = tf.stack(patches, 3) patches = tf.reshape(patches, [(h / p)**2, p, p, c]) #ueError: Cannot reshape a tensor with 12582912 elements to shape [0,128,128,2048] (0 elements) for 'Reshape' (op: 'Reshape') with input shapes: [1,16,16,16384,3], [4] and with input tensors computed as partial shapes: input[1] = [0,128,128,2048]. return patches
def test_conv_4(): image_input = ImageInput(seed=713, batch_size=3, image_h=4, image_w=4, image_c=5) in_node = image_input.get_placeholder("image", data_type=tf.float32) constr = NNImageOps(in_node) constr.set_filter_size([7, 7, 16, 5]) constr.set_output_shape([3, 8, 8, 16]) constr.set_stride_size([1, 2, 2, 1]) in1 = constr.execute("conv2d_transpose") # size is (3, 8, 8, 16) constr.set_image(in1) constr.set_filter_size([2, 2, 32, 16]) constr.set_output_shape([3, 8, 8, 32]) constr.set_rate(2) in02 = constr.execute("atrous_conv2d_transpose") # size is (3,8,8,32) constr.set_image(in02) constr.set_filter_size([2, 2, 32, 2]) constr.set_pointwise_filter([1, 1, 64, 8]) constr.set_stride_size([1, 1, 1, 1]) constr.set_rate([1, 2]) in2 = constr.execute("separable_conv2d") # (3, 8, 8, 8) constr.set_image(in2) constr.set_filter_size([2, 2, 8, 4]) constr.set_stride_size([1, 1, 1, 1]) constr.set_rate([3, 2]) in3 = constr.execute("depthwise_conv2d") # size (3,8,8,32) in33 = tf.space_to_batch_nd(in3, [3, 3], paddings=[[1, 0], [0, 1]]) # size (27,3,3,32) constr.set_image(tf.reshape(in33, [27 * 2, 12, 12])) constr.set_filter_size([2, 12, 2]) constr.set_stride_size(1) in4 = constr.execute("conv1d") # size (54,12,2) out_node = tf.identity(in4, name="output") placeholders = [in_node] predictions = [out_node] tfp = TensorFlowPersistor(save_dir="conv_4") predictions_after_freeze = tfp \ .set_placeholders(placeholders) \ .set_output_tensors(predictions) \ .set_test_data(image_input.get_test_data()) \ .build_save_frozen_graph() print(predictions_after_freeze[0].shape) if __name__ == "main": test_conv_4()
def generate_patches(image, patch_h, patch_w): '''Splits an image into patches of size patch_h x patch_w Input: image of shape [image_h, image_w, image_ch] Output: batch of patches shape [n, patch_h, patch_w, image_ch] ''' pad = [[0, 0], [0, 0]] image_ch = 3 p_area = patch_h * patch_w patches = tf.space_to_batch_nd(image, [patch_h, patch_w], pad) patches = tf.split(patches, p_area, 0) patches = tf.stack(patches, 3) patches = tf.reshape(patches, [-1, patch_h, patch_w, image_ch]) return patches
def _checkGrad(self, x, block_shape, paddings): block_shape = np.array(block_shape) paddings = np.array(paddings).reshape((len(block_shape), 2)) with self.test_session(): tf_x = tf.convert_to_tensor(x) tf_y = tf.space_to_batch_nd(tf_x, block_shape, paddings) epsilon = 1e-5 ((x_jacob_t, x_jacob_n)) = tf.test.compute_gradient( tf_x, x.shape, tf_y, tf_y.get_shape().as_list(), x_init_value=x, delta=epsilon) self.assertAllClose(x_jacob_t, x_jacob_n, rtol=1e-2, atol=epsilon)
def _generic_masked_test(t, block_shape, paddings): with tf.Session() as sess: out = tf.space_to_batch_nd(t, block_shape=block_shape, paddings=paddings) actual = sess.run(out) with tfe.protocol.Pond() as prot: b = prot.mask(prot.define_private_variable(t)) out = prot.space_to_batch_nd(b, block_shape=block_shape, paddings=paddings) with tfe.Session() as sess: sess.run(tf.global_variables_initializer()) final = sess.run(out.reveal()) np.testing.assert_array_almost_equal(final, actual, decimal=3)
def texture_loss(x, p=16): _, h, w, c = x.get_shape().as_list() x = normalize(x) assert h % p == 0 and w % p == 0 logger.info('Create texture loss for layer {} with shape {}'.format(x.name, x.get_shape())) x = tf.space_to_batch_nd(x, [p, p], [[0, 0], [0, 0]]) # [b * ?, h/p, w/p, c] x = tf.reshape(x, [p, p, -1, h // p, w // p, c]) # [p, p, b, h/p, w/p, c] x = tf.transpose(x, [2, 3, 4, 0, 1, 5]) # [b * ?, p, p, c] patches_a, patches_b = tf.split(x, 2, axis=0) # each is b,h/p,w/p,p,p,c patches_a = tf.reshape(patches_a, [-1, p, p, c]) # [b * ?, p, p, c] patches_b = tf.reshape(patches_b, [-1, p, p, c]) # [b * ?, p, p, c] return tf.losses.mean_squared_error( gram_matrix(patches_a), gram_matrix(patches_b), reduction=Reduction.MEAN )
def forward(self): pad = [[self.lay.pad, self.lay.pad]] * 2 #print([[0, 0]] + pad + [[0, 0]]) #temp = tf.pad(self.inp.out, [[0, 0]] + pad + [[0, 0]]) temp = tf.space_to_batch_nd(self.inp.out, [1, 1], pad, name=None) if self.lay.groups == 1: temp = tf.nn.conv2d(temp, self.lay.w['kernel'], padding='VALID', name=self.scope, strides=[1] + [self.lay.stride] * 2 + [1]) else: temp = tf.nn.depthwise_conv2d(temp, self.lay.w['kernel'], padding='VALID', strides=[1] + [self.lay.stride] * 2 + [1]) if self.lay.batch_norm: temp = self.batchnorm(self.lay, temp) self.out = tf.nn.bias_add(temp, self.lay.w['biases'])
def texture_loss(x, p=16): x = normalize(x) _, h, w, c = x.get_shape().as_list() assert h % p == 0 and w % p == 0 logger.info( 'Create texture loss for layer {} with shape {}'. format(x.name, x.get_shape())) x = tf.space_to_batch_nd(x, [p, p], [[0, 0], [0, 0]]) x = tf.reshape(x, [p, p, -1, h // p, w // p, c]) x = tf.transpose(x, [2, 3, 4, 0, 1, 5]) patches_a, patches_b = tf.split( x, 2) # each is b,h/p,w/p,p,p,c patches_a = tf.reshape(patches_a, [-1, p, p, c]) patches_b = tf.reshape(patches_b, [-1, p, p, c]) return tf.losses.mean_squared_error( gram_matrix(patches_a), gram_matrix(patches_b), reduction=Reduction.SUM) * (1.0 / BATCH_SIZE)
def _texture_loss(self, features, patch_size=16): ''' the front part of features : gt features the latter part of features : pred features I will do calculating gt and pred features at once! ''' #features = self._normalize(features) batch_size, h, w, c = features.get_shape().as_list() features = tf.space_to_batch_nd(features, [patch_size, patch_size], [[0, 0], [0, 0]]) features = tf.reshape(features, [patch_size, patch_size, -1, h // patch_size, w // patch_size, c]) features = tf.transpose(features, [2, 3, 4, 0, 1, 5]) patches_gt, patches_pred = tf.split(features, 2, axis=0) patches_gt = tf.reshape(patches_gt, [-1, patch_size, patch_size, c]) patches_pred = tf.reshape(patches_pred, [-1, patch_size, patch_size, c]) gram_matrix_gt = self._gram_matrix(patches_gt) gram_matrix_pred = self._gram_matrix(patches_pred) tl_features = tf.reduce_mean(tf.reduce_sum(tf.square(gram_matrix_gt - gram_matrix_pred), axis=-1)) return tl_features
def apply_dna_kernels_dilated(image, kernels, dilation_rate=(1, 1)): dilation_rate = list(dilation_rate) if isinstance(dilation_rate, (tuple, list)) else [dilation_rate] * 2 batch_size, height, width, color_channels = image.get_shape().as_list() batch_size, kernel_height, kernel_width, kernel_size, num_transformed_images = kernels.get_shape().as_list() # Flatten the spatial dimensions. kernels_reshaped = tf.reshape(kernels, [batch_size, kernel_height, kernel_width, kernel_size[0] * kernel_size[1], num_transformed_images]) image_padded = pad2d(image, kernel_size, rate=dilation_rate, padding='SAME', mode='SYMMETRIC') # for dilation = [2, 2], this is equivalent to this: # small_images = [image[:, 0::2, 0::2, :], image[:, 0::2, 1::2, :], image[:, 1::2, 0::2, :], image[:, 1::2, 1::2, :]] small_images = tf.space_to_batch_nd(image_padded, dilation_rate, paddings=[[0, 0]] * 2) small_images = tf.reshape(small_images, [dilation_rate[0] * dilation_rate[1], batch_size, image_padded.get_shape().as_list()[1] // dilation_rate[0], image_padded.get_shape().as_list()[2] // dilation_rate[1], color_channels]) small_images = tf.unstack(small_images, axis=0) small_outputs = [] for small_image in small_images: # Combine channel and batch dimensions into the first dimension. image_transposed = tf.transpose(small_image, [3, 0, 1, 2]) image_reshaped = flatten(image_transposed, 0, 1)[..., None] patches_reshaped = tf.extract_image_patches(image_reshaped, ksizes=[1] + kernel_size + [1], strides=[1] * 4, rates=[1] * 4, padding='VALID') # Separate channel and batch dimensions. patches = tf.reshape(patches_reshaped, [color_channels, batch_size, height // dilation_rate[0], width // dilation_rate[1], kernel_size[0] * kernel_size[1]]) # Reduce along the spatial dimensions of the kernel. outputs = tf.reduce_sum(patches[..., None] * kernels_reshaped[None, ...], axis=-2) # Swap channel and transformation dimensions. outputs = tf.transpose(outputs, [4, 1, 2, 3, 0]) outputs = tf.unstack(outputs, axis=0) small_outputs.append(outputs) small_outputs = list(zip(*small_outputs)) small_outputs = [tf.reshape(small_output, [dilation_rate[0] * dilation_rate[1] * batch_size, height // dilation_rate[0], width // dilation_rate[1], color_channels]) for small_output in small_outputs] outputs = [tf.batch_to_space_nd(small_output, dilation_rate, crops=[[0, 0]] * 2) for small_output in small_outputs] return outputs
def testUnknown(self): # Verify that input shape and paddings shape can be unknown. _ = tf.space_to_batch_nd( tf.placeholder(tf.float32), tf.placeholder(tf.int32, shape=(2,)), tf.placeholder(tf.int32)) # Only number of input dimensions is known. t = tf.space_to_batch_nd( tf.placeholder(tf.float32, shape=(None, None, None, None)), tf.placeholder(tf.int32, shape=(2,)), tf.placeholder(tf.int32)) self.assertEqual(4, t.get_shape().ndims) # Dimensions are partially known. t = tf.space_to_batch_nd( tf.placeholder(tf.float32, shape=(None, None, None, 2)), tf.placeholder(tf.int32, shape=(2,)), tf.placeholder(tf.int32)) self.assertEqual([None, None, None, 2], t.get_shape().as_list()) # Dimensions are partially known. t = tf.space_to_batch_nd( tf.placeholder(tf.float32, shape=(3, None, None, 2)), [2, 3], tf.placeholder(tf.int32)) self.assertEqual([3 * 2 * 3, None, None, 2], t.get_shape().as_list()) # Dimensions are partially known. t = tf.space_to_batch_nd( tf.placeholder(tf.float32, shape=(3, None, 2, 2)), [2, 3], [[1, 1], [0, 1]]) self.assertEqual([3 * 2 * 3, None, 1, 2], t.get_shape().as_list()) # Dimensions are fully known. t = tf.space_to_batch_nd( tf.placeholder(tf.float32, shape=(3, 2, 3, 2)), [2, 3], [[1, 1], [0, 0]]) self.assertEqual([3 * 2 * 3, 2, 1, 2], t.get_shape().as_list())
def __call__(self, inputs, seq_length, dilation_rate, scope=None): ''' Create the variables and do the forward computation Args: inputs: the input to the layer as a 4D [batch_size, max_length, feature_dim, in_channels] tensor seq_length: the length of the input sequences dilation_rate: the rate of dilation scope: The variable scope sets the namespace under which the variables created during this call will be stored. Returns: the outputs which is a [batch_size, max_length, feature_dim, out_channels] ''' with tf.variable_scope(scope or type(self).__name__): in_channels = int(inputs.get_shape()[3]) stddev = 1 / (self.fwidth * self.fheight * in_channels)**0.5 #the filter parameters w = tf.get_variable( 'filter', [self.fheight, self.fwidth, in_channels, self.out_channels], initializer=tf.random_normal_initializer(stddev=stddev)) #the bias parameters b = tf.get_variable( 'bias', [self.out_channels], initializer=tf.random_normal_initializer(stddev=stddev)) if dilation_rate == 1: #do a 2D convolution out = tf.nn.conv2d(inputs, w, [1, 1, 1, 1], self.padding) #add the bias outputs = tf.nn.bias_add(out, b) return outputs #arrange the padding, see tensorflow github code in nn_ops.py if self.padding == "SAME": filter_shape = tf.Variable.get_shape(w) filter_height, filter_width = int(filter_shape[0]), int( filter_shape[1]) filter_height_up = filter_height + (filter_height - 1) * (dilation_rate - 1) pad_height = filter_height_up - 1 pad_width = filter_width - 1 pad_top = pad_height // 2 pad_bottom = pad_height - pad_top pad_left = pad_width // 2 pad_right = pad_width - pad_left elif self.padding == "VALID": pad_top = 0 pad_bottom = 0 pad_left = 0 pad_right = 0 else: raise ValueError("Invalid padding") input_shape = tf.shape(inputs) in_height = input_shape[1] + pad_top + pad_bottom in_width = input_shape[2] + pad_left + pad_right # More padding so that rate divides the height of the input. pad_bottom_extra = (dilation_rate - in_height % dilation_rate) % dilation_rate pad_right_extra = 0 # The paddings argument to space_to_batch includes both padding components. space_to_batch_pad = [[pad_top, pad_bottom + pad_bottom_extra], [pad_left, pad_right + pad_right_extra]] outputs = tf.space_to_batch_nd(input=inputs, paddings=space_to_batch_pad, block_shape=[dilation_rate, 1]) #Do the convolution outputs = tf.nn.conv2d(input=outputs, filter=w, strides=[1, 1, 1, 1], padding="VALID", name=scope) # The crops argument to batch_to_space is just the extra padding component. batch_to_space_crop = [[0, pad_bottom_extra], [0, pad_right_extra]] outputs = tf.batch_to_space_nd(input=outputs, crops=batch_to_space_crop, block_shape=[dilation_rate, 1]) #Add the bias outputs = tf.nn.bias_add(outputs, b) return outputs
def _construct_space_to_batch_nd(input_shape): a = tf.placeholder(tf.float32, shape=input_shape, name="input") block_shape = tf.constant([2, 2], dtype=tf.int32) paddings = tf.constant([[0, 0], [2, 0]], dtype=tf.int32) x = tf.space_to_batch_nd(a, block_shape=block_shape, paddings=paddings) return x, a
import tensorflow as tf in_ = tf.compat.v1.placeholder(tf.float32, shape=[1, 2, 2, 1], name="Hole") bs_ = tf.constant([2, 2], name="Hole") pd_ = tf.constant([[0, 0], [0, 0]], name="Hole") op_ = tf.space_to_batch_nd(in_, bs_, pd_)
def inception_resnet_v2_fcn8(inputs, num_classes=20, is_training=True, dropout_keep_prob=0.8, reuse=None, scope='InceptionResnetV2'): """Creates the Inception Resnet V2 model. Args: inputs: a 4-D tensor of size [batch_size, height, width, 3]. num_classes: number of predicted classes. is_training: whether is training or not. dropout_keep_prob: float, the fraction to keep before final layer. reuse: whether or not the network and its variables should be reused. To be able to reuse 'scope' must be given. scope: Optional variable_scope. Returns: logits: the logits outputs of the model. (end_points: the set of end_points from the inception model.) """ # end_points = {} with tf.variable_scope(scope, 'InceptionResnetV2', [inputs], reuse=reuse): with slim.arg_scope([slim.batch_norm, slim.dropout], is_training=is_training): with slim.arg_scope([slim.conv2d, slim.max_pool2d, slim.avg_pool2d], stride=1, padding='SAME'): # 149 x 149 x 32 net = slim.conv2d(inputs, 32, 3, stride=2, padding='VALID', scope='Conv2d_1a_3x3') #end_points['Conv2d_1a_3x3'] = net # 147 x 147 x 32 net = slim.conv2d(net, 32, 3, padding='VALID', scope='Conv2d_2a_3x3') #end_points['Conv2d_2a_3x3'] = net # 147 x 147 x 64 net = slim.conv2d(net, 64, 3, scope='Conv2d_2b_3x3') #end_points['Conv2d_2b_3x3'] = net # 73 x 73 x 64 net = slim.max_pool2d(net, 3, stride=2, padding='VALID', scope='MaxPool_3a_3x3') #end_points['MaxPool_3a_3x3'] = net # 73 x 73 x 80 net = slim.conv2d(net, 80, 1, padding='VALID', scope='Conv2d_3b_1x1') #end_points['Conv2d_3b_1x1'] = net # 71 x 71 x 192 net = slim.conv2d(net, 192, 3, padding='VALID', scope='Conv2d_4a_3x3') #end_points['Conv2d_4a_3x3'] = net # 35 x 35 x 192 net = slim.max_pool2d(net, 3, stride=2, padding='VALID', scope='MaxPool_5a_3x3') #end_points['MaxPool_5a_3x3'] = net # 35 x 35 x 320 with tf.variable_scope('Mixed_5b'): with tf.variable_scope('Branch_0'): tower_conv = slim.conv2d(net, 96, 1, scope='Conv2d_1x1') with tf.variable_scope('Branch_1'): tower_conv1_0 = slim.conv2d(net, 48, 1, scope='Conv2d_0a_1x1') tower_conv1_1 = slim.conv2d(tower_conv1_0, 64, 5, scope='Conv2d_0b_5x5') with tf.variable_scope('Branch_2'): tower_conv2_0 = slim.conv2d(net, 64, 1, scope='Conv2d_0a_1x1') tower_conv2_1 = slim.conv2d(tower_conv2_0, 96, 3, scope='Conv2d_0b_3x3') tower_conv2_2 = slim.conv2d(tower_conv2_1, 96, 3, scope='Conv2d_0c_3x3') with tf.variable_scope('Branch_3'): tower_pool = slim.avg_pool2d(net, 3, stride=1, padding='SAME', scope='AvgPool_0a_3x3') tower_pool_1 = slim.conv2d(tower_pool, 64, 1, scope='Conv2d_0b_1x1') net = tf.concat(axis=3, values=[tower_conv, tower_conv1_1, tower_conv2_2, tower_pool_1]) #end_points['Mixed_5b'] = net net = slim.repeat(net, 10, block35, scale=0.17) #_8stride_end = net # use bilinear interpolation instead of off-centered zero padding net = tf.image.resize_images(net, [tf.shape(net)[1]+1,tf.shape(net)[2]+1], method=tf.image.ResizeMethod.BILINEAR, align_corners=True) # 17 x 17 x 1024 with tf.variable_scope('Mixed_6a'): with tf.variable_scope('Branch_0'): tower_conv = slim.conv2d(net, 384, 3, stride=1, padding='VALID', scope='Conv2d_1a_3x3') with tf.variable_scope('Branch_1'): tower_conv1_0 = slim.conv2d(net, 256, 1, scope='Conv2d_0a_1x1') tower_conv1_1 = slim.conv2d(tower_conv1_0, 256, 3, scope='Conv2d_0b_3x3') tower_conv1_2 = slim.conv2d(tower_conv1_1, 384, 3, stride=1, padding='VALID', scope='Conv2d_1a_3x3') with tf.variable_scope('Branch_2'): tower_pool = slim.max_pool2d(net, 3, stride=1, padding='VALID', scope='MaxPool_1a_3x3') net = tf.concat(axis=3, values=[tower_conv, tower_conv1_2, tower_pool]) net = tf.space_to_batch_nd(input=net, block_shape=[2,2], paddings=[[0,0],[0,0]]) #end_points['Mixed_6a'] = net net = slim.repeat(net, 15, block17, scale=0.10) #_16stride_end = net # Auxillary tower # with tf.variable_scope('AuxLogits'): # aux = slim.avg_pool2d(net, 5, stride=3, padding='VALID', # scope='Conv2d_1a_3x3') # aux = slim.conv2d(aux, 128, 1, scope='Conv2d_1b_1x1') # aux = slim.conv2d(aux, 768, aux.get_shape()[1:3], # padding='VALID', scope='Conv2d_2a_5x5') # aux = slim.flatten(aux) # aux = slim.fully_connected(aux, num_classes, activation_fn=None, # scope='Logits') # end_points['AuxLogits'] = aux #-------------------------------------------------- # # use bilinear interpolation instead of off-centered zero padding # net = tf.image.resize_images(net, # [tf.shape(net)[1]+1,tf.shape(net)[2]+1], # method=tf.image.ResizeMethod.BILINEAR, # align_corners=True) # # net = tf.space_to_batch_nd(input=net, block_shape=[2,2], paddings=[[0,0],[0,0]]) # # with tf.variable_scope('Mixed_7a'): # with tf.variable_scope('Branch_0'): # tower_conv = slim.conv2d(net, 256, 1, scope='Conv2d_0a_1x1') # tower_conv_1 = slim.conv2d(tower_conv, 384, 3, stride=1, # padding='VALID', scope='Conv2d_1a_3x3') # with tf.variable_scope('Branch_1'): # tower_conv1 = slim.conv2d(net, 256, 1, scope='Conv2d_0a_1x1') # tower_conv1_1 = slim.conv2d(tower_conv1, 288, 3, stride=1, # padding='VALID', scope='Conv2d_1a_3x3') # with tf.variable_scope('Branch_2'): # tower_conv2 = slim.conv2d(net, 256, 1, scope='Conv2d_0a_1x1') # tower_conv2_1 = slim.conv2d(tower_conv2, 288, 3, # scope='Conv2d_0b_3x3') # tower_conv2_2 = slim.conv2d(tower_conv2_1, 320, 3, stride=1, # padding='VALID', scope='Conv2d_1a_3x3') # with tf.variable_scope('Branch_3'): # tower_pool = slim.max_pool2d(net, 3, stride=1, padding='VALID', # scope='MaxPool_1a_3x3') # net = tf.concat(axis=3, values=[tower_conv_1, tower_conv1_1, # tower_conv2_2, tower_pool]) # # end_points['Mixed_7a'] = net # # net = slim.repeat(net, 9, block8, scale=0.20) # net = block8(net, activation_fn=None) # # net = slim.conv2d(net, 1536, 1, scope='Conv2d_7b_1x1') # net = tf.batch_to_space_nd(input=net, block_shape=[2,2], crops=[[0,0],[0,0]]) # # net = tf.batch_to_space_nd(input=net, block_shape=[2,2], crops=[[0,0],[0,0]]) # _32stride_end = net # # end_points['Conv2d_7b_1x1'] = net #-------------------------------------------------- # comment final classifier stage # with tf.variable_scope('Logits'): # end_points['PrePool'] = net # net = slim.avg_pool2d(net, net.get_shape()[1:3], padding='VALID', # scope='AvgPool_1a_8x8') # net = slim.flatten(net) # # net = slim.dropout(net, dropout_keep_prob, is_training=is_training, # scope='Dropout') # # end_points['PreLogitsFlatten'] = net # logits = slim.fully_connected(net, num_classes, activation_fn=None, # scope='Logits') # end_points['Logits'] = logits # end_points['Predictions'] = tf.nn.softmax(logits, name='Predictions') with tf.variable_scope('output_generation'): redim_s16_convlayer = slim.conv2d(_32stride_end, num_classes, 1, scope='redim_Conv2d_1x1', activation_fn=None) ksize=8 stride=4 strides = [1, stride, stride, 1] in_features = num_classes f_shape = [ksize, ksize, in_features, in_features] weights = get_deconv_filter(f_shape) in_shape = tf.shape(redim_s16_convlayer) h = ((in_shape[1] - 1) * stride) + 1 w = ((in_shape[2] - 1) * stride) + 1 new_shape = [in_shape[0], h, w, num_classes] upsample_s8_deconvlayer = tf.nn.conv2d_transpose(redim_s16_convlayer, weights, output_shape=tf.stack(new_shape), strides=strides, padding='SAME') upsample_s8_final_resize = tf.image.resize_images(upsample_s8_deconvlayer, [tf.shape(inputs)[1],tf.shape(inputs)[2]], method=tf.image.ResizeMethod.BILINEAR, align_corners=True) logits = tf.reshape(tensor=upsample_s8_final_resize, shape=(-1, num_classes)) # prediction = upsample_s8_final_resize # argmax_prediction = tf.argmax(prediction, dimension=3) # end_points['Predictions'] = prediction # end_points['Argmax_Predictions'] = argmax_prediction return logits
def l2norm(x, name): with tf.variable_scope(name): layer = dnnLayer(name) w = tf.Variable(layer.blobs[0].flatten(), dtype=dtype, name='mul') return tf.nn.l2_normalize(x, 3, epsilon=1e-10) * w ### Graph definition ########################################################### inp = tf.placeholder(dtype, [1, 300, 300, 3], 'data') data_bn = batch_norm(inp, 'data_bn') data_scale = scale(data_bn, 'data_scale') # Instead of tf.pad we use tf.space_to_batch_nd layers which override convolution's padding strategy to explicit numbers # data_scale = tf.pad(data_scale, [[0, 0], [3, 3], [3, 3], [0, 0]]) data_scale = tf.space_to_batch_nd(data_scale, [1, 1], [[3, 3], [3, 3]], name='Pad') conv1_h = conv(data_scale, stride=2, pad='VALID', name='conv1_h') conv1_bn_h = batch_norm(conv1_h, 'conv1_bn_h') conv1_scale_h = scale(conv1_bn_h, 'conv1_scale_h') conv1_relu = tf.nn.relu(conv1_scale_h) conv1_pool = tf.layers.max_pooling2d(conv1_relu, pool_size=(3, 3), strides=(2, 2), padding='SAME', name='conv1_pool') layer_64_1_conv1_h = conv(conv1_pool, 'layer_64_1_conv1_h') layer_64_1_bn2_h = batch_norm(layer_64_1_conv1_h, 'layer_64_1_bn2_h') layer_64_1_scale2_h = scale(layer_64_1_bn2_h, 'layer_64_1_scale2_h') layer_64_1_relu2 = tf.nn.relu(layer_64_1_scale2_h)
def space_to_batch_nd(self, block_shape, paddings): backing = tf.space_to_batch_nd(self.value, block_shape, paddings) return int32factory.tensor(backing)
return bn def l2norm(x, name): with tf.variable_scope(name): layer = dnnLayer(name) w = tf.Variable(layer.blobs[0].flatten(), dtype=dtype, name='mul') return tf.nn.l2_normalize(x, 3, epsilon=1e-10) * w ### Graph definition ########################################################### inp = tf.placeholder(dtype, [1, 300, 300, 3], 'data') data_bn = batch_norm(inp, 'data_bn') data_scale = scale(data_bn, 'data_scale') # Instead of tf.pad we use tf.space_to_batch_nd layers which override convolution's padding strategy to explicit numbers # data_scale = tf.pad(data_scale, [[0, 0], [3, 3], [3, 3], [0, 0]]) data_scale = tf.space_to_batch_nd(data_scale, [1, 1], [[3, 3], [3, 3]], name='Pad') conv1_h = conv(data_scale, stride=2, pad='VALID', name='conv1_h') conv1_bn_h = batch_norm(conv1_h, 'conv1_bn_h') conv1_scale_h = scale(conv1_bn_h, 'conv1_scale_h') conv1_relu = tf.nn.relu(conv1_scale_h) conv1_pool = tf.layers.max_pooling2d(conv1_relu, pool_size=(3, 3), strides=(2, 2), padding='SAME', name='conv1_pool') layer_64_1_conv1_h = conv(conv1_pool, 'layer_64_1_conv1_h') layer_64_1_bn2_h = batch_norm(layer_64_1_conv1_h, 'layer_64_1_bn2_h') layer_64_1_scale2_h = scale(layer_64_1_bn2_h, 'layer_64_1_scale2_h') layer_64_1_relu2 = tf.nn.relu(layer_64_1_scale2_h) layer_64_1_conv2_h = conv(layer_64_1_relu2, 'layer_64_1_conv2_h') layer_64_1_sum = layer_64_1_conv2_h + conv1_pool
def space_to_batch_nd(self, block_shape, paddings): value = tf.space_to_batch_nd(self.value, block_shape, paddings) return DenseTensor(value)
# -*- coding: utf-8 -*- import tensorflow as tf import numpy as np np.random.seed(1) b = np.linspace(1, 36, num=36).reshape((1, 6, 6, 1)) a = tf.constant(b, dtype=tf.int32) c = tf.space_to_batch_nd(a, block_shape=[3, 3], paddings=[[0, 0], [0, 0]]) d = tf.batch_to_space_nd(c, block_shape=[3, 3], crops=[[0, 0], [0, 0]]) with tf.Session() as sess: print(sess.run(a)) print(sess.run(c).shape) print(sess.run(c)) print(sess.run(d).shape) print(sess.run(d))