def _conv_layer(self, bottom, name,atrous_rate=None,downsample=False): with tf.variable_scope(name) as scope: filt = self.get_conv_filter(name) if not atrous_rate: conv = tf.nn.conv2d(bottom, filt, [1, 1, 1, 1], padding='SAME') else: print('---------Using atrous convolution-------') conv = tf.nn.atrous_conv2d(bottom,filt,atrous_rate,padding='SAME') conv_biases = self.get_bias(name) bias = tf.nn.bias_add(conv, conv_biases) bias = spatial_dropout(bias) relu = tf.nn.relu(bias) _activation_summary(relu) return relu
def _conv_layer(self, bottom, name, atrous_rate=None, downsample=False): with tf.variable_scope(name) as scope: filt = self.get_conv_filter(name) if not atrous_rate: conv = tf.nn.conv2d(bottom, filt, [1, 1, 1, 1], padding='SAME') else: print('---------Using atrous convolution---------') conv = tf.nn.atrous_conv2d(bottom, filt, atrous_rate, padding='SAME') conv_biases = self.get_bias(name) bias = tf.nn.bias_add(conv, conv_biases) # add batch normalization norm = tf.contrib.layers.batch_norm(bias) out_ = spatial_dropout(norm) #relu = tf.nn.relu(out_) #_activation_summary(relu) # use parameterized relu output = PReLU(out_, scope=name + "/output") return output
def encoder_bottleneck_asymmetric(self, x, output_depth, drop_prob, scope, proj_ratio=4): input_shape = x.get_shape().as_list() input_depth = input_shape[3] internal_depth = int(output_depth / proj_ratio) # convolution branch: conv_branch = x # # 1x1 projection: W_proj = self.get_variable_weight_decay( scope + "/W_proj", shape=[1, 1, input_depth, internal_depth], # ([filter_height, filter_width, in_depth, out_depth]) initializer=tf.contrib.layers.xavier_initializer(), loss_category="encoder_wd_losses") conv_branch = tf.nn.conv2d(conv_branch, W_proj, strides=[1, 1, 1, 1], padding="VALID") # NOTE! no bias terms # # # batch norm and PReLU: conv_branch = tf.contrib.slim.batch_norm(conv_branch) conv_branch = PReLU(conv_branch, scope=scope + "/proj") # # asymmetric conv: # # # asymmetric conv 1: W_conv1 = self.get_variable_weight_decay( scope + "/W_conv1", shape=[5, 1, internal_depth, internal_depth], # ([filter_height, filter_width, in_depth, out_depth]) initializer=tf.contrib.layers.xavier_initializer(), loss_category="encoder_wd_losses") conv_branch = tf.nn.conv2d(conv_branch, W_conv1, strides=[1, 1, 1, 1], padding="SAME") # NOTE! no bias terms # # # asymmetric conv 2: W_conv2 = self.get_variable_weight_decay( scope + "/W_conv2", shape=[1, 5, internal_depth, internal_depth], # ([filter_height, filter_width, in_depth, out_depth]) initializer=tf.contrib.layers.xavier_initializer(), loss_category="encoder_wd_losses") b_conv2 = self.get_variable_weight_decay( scope + "/b_conv2", shape=[internal_depth], # ([out_depth]) initializer=tf.constant_initializer(0), loss_category="encoder_wd_losses") conv_branch = tf.nn.conv2d( conv_branch, W_conv2, strides=[1, 1, 1, 1 ], padding="SAME") + b_conv2 # # # batch norm and PReLU: conv_branch = tf.contrib.slim.batch_norm(conv_branch) conv_branch = PReLU(conv_branch, scope=scope + "/conv") # # 1x1 expansion: W_exp = self.get_variable_weight_decay( scope + "/W_exp", shape=[1, 1, internal_depth, output_depth], # ([filter_height, filter_width, in_depth, out_depth]) initializer=tf.contrib.layers.xavier_initializer(), loss_category="encoder_wd_losses") conv_branch = tf.nn.conv2d(conv_branch, W_exp, strides=[1, 1, 1, 1], padding="VALID") # NOTE! no bias terms # # # batch norm: conv_branch = tf.contrib.slim.batch_norm(conv_branch) # NOTE! no PReLU here # # regularizer: conv_branch = spatial_dropout(conv_branch, drop_prob) # main branch: main_branch = x # add the branches: merged = conv_branch + main_branch # apply PReLU: output = PReLU(merged, scope=scope + "/output") return output
def encoder_bottleneck_regular(self, x, output_depth, drop_prob, scope, proj_ratio=4, downsampling=False): input_shape = x.get_shape().as_list() input_depth = input_shape[3] internal_depth = int(output_depth / proj_ratio) # convolution branch: conv_branch = x # # 1x1 projection: if downsampling: W_conv = self.get_variable_weight_decay( scope + "/W_proj", shape=[2, 2, input_depth, internal_depth], # ([filter_height, filter_width, in_depth, out_depth]) initializer=tf.contrib.layers.xavier_initializer(), loss_category="encoder_wd_losses") conv_branch = tf.nn.conv2d(conv_branch, W_conv, strides=[1, 2, 2, 1], padding="VALID") # NOTE! no bias terms else: W_proj = self.get_variable_weight_decay( scope + "/W_proj", shape=[1, 1, input_depth, internal_depth], # ([filter_height, filter_width, in_depth, out_depth]) initializer=tf.contrib.layers.xavier_initializer(), loss_category="encoder_wd_losses") conv_branch = tf.nn.conv2d(conv_branch, W_proj, strides=[1, 1, 1, 1], padding="VALID") # NOTE! no bias terms # # # batch norm and PReLU: conv_branch = tf.contrib.slim.batch_norm(conv_branch) conv_branch = PReLU(conv_branch, scope=scope + "/proj") # # conv: W_conv = self.get_variable_weight_decay( scope + "/W_conv", shape=[3, 3, internal_depth, internal_depth], # ([filter_height, filter_width, in_depth, out_depth]) initializer=tf.contrib.layers.xavier_initializer(), loss_category="encoder_wd_losses") b_conv = self.get_variable_weight_decay( scope + "/b_conv", shape=[internal_depth], # ([out_depth]) initializer=tf.constant_initializer(0), loss_category="encoder_wd_losses") conv_branch = tf.nn.conv2d( conv_branch, W_conv, strides=[1, 1, 1, 1], padding="SAME") + b_conv # # # batch norm and PReLU: conv_branch = tf.contrib.slim.batch_norm(conv_branch) conv_branch = PReLU(conv_branch, scope=scope + "/conv") # # 1x1 expansion: W_exp = self.get_variable_weight_decay( scope + "/W_exp", shape=[1, 1, internal_depth, output_depth], # ([filter_height, filter_width, in_depth, out_depth]) initializer=tf.contrib.layers.xavier_initializer(), loss_category="encoder_wd_losses") conv_branch = tf.nn.conv2d(conv_branch, W_exp, strides=[1, 1, 1, 1], padding="VALID") # NOTE! no bias terms # # # batch norm: conv_branch = tf.contrib.slim.batch_norm(conv_branch) # NOTE! no PReLU here # # regularizer: conv_branch = spatial_dropout(conv_branch, drop_prob) # main branch: main_branch = x if downsampling: # max pooling with argmax (for use in max_unpool in the decoder): main_branch, pooling_indices = \ tf.nn.max_pool_with_argmax(main_branch, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="SAME") # (everytime we downsample, we also increase the feature block depth) # pad with zeros so that the feature block depth matches: depth_to_pad = output_depth - input_depth paddings = tf.convert_to_tensor([[0, 0], [0, 0], [0, 0], [0, depth_to_pad]]) # (paddings is an integer tensor of shape [4, 2] where 4 is the rank # of main_branch. For each dimension D (D = 0, 1, 2, 3) of main_branch, # paddings[D, 0] is the no of values to add before the contents of # main_branch in that dimension, and paddings[D, 0] is the no of # values to add after the contents of main_branch in that dimension) main_branch = tf.pad(main_branch, paddings=paddings, mode="CONSTANT") # add the branches: merged = conv_branch + main_branch # apply PReLU: output = PReLU(merged, scope=scope + "/output") if downsampling: return output, pooling_indices else: return output
def line_existance_part(self, x, scope, dilation_rate=4,drop_prob=0.1, verbose=True): # so the output of dilated convolution should be 36 x 100 x 32, if the input was 288 x 800 x 3, encoder output will be 36 x 100 x 128 # # dilated conv: # w h w c shape = x.get_shape().as_list() W_conv = self.get_variable_weight_decay(scope + "/W_dconv", shape=[3, 3, shape[-1], 32], # ([filter_height, filter_width, in_depth, out_depth]) initializer=tf.contrib.layers.xavier_initializer(), loss_category="line_existance_wd_loss") b_conv = self.get_variable_weight_decay(scope + "/b_dconv", shape=[32], # ([out_depth]) initializer=tf.constant_initializer(0), loss_category="line_existance_wd_loss") conv_branch = tf.nn.atrous_conv2d(x, W_conv, rate=dilation_rate, padding="SAME") + b_conv if(verbose): print(conv_branch.get_shape().as_list(), "dilated convolution") # # # batch norm and ReLU: conv_branch = tf.contrib.slim.batch_norm(conv_branch) if(verbose):print(conv_branch.get_shape().as_list(), "batch normalization") conv_branch = tf.nn.relu(conv_branch, name=scope + "/RELU1") if(verbose):print(conv_branch.get_shape().as_list(), "relu") # # regularizer: conv_branch = spatial_dropout(conv_branch, drop_prob) if(verbose):print(conv_branch.get_shape().as_list(), "dropout") # # conv: W_conv = self.get_variable_weight_decay(scope + "/W_conv", shape=[3, 3, 32, 5], # ([filter_height, filter_width, in_depth, out_depth]) initializer=tf.contrib.layers.xavier_initializer(), loss_category="line_existance_wd_loss") b_conv = self.get_variable_weight_decay(scope + "/b_conv", shape=[5], # ([out_depth]) initializer=tf.constant_initializer(0), loss_category="line_existance_wd_loss") conv_branch = tf.nn.conv2d(conv_branch, W_conv, strides=[1, 1, 1, 1], padding="SAME") + b_conv if(verbose):print(conv_branch.get_shape().as_list(), "convolution 1, 1") # spatial softmax conv_branch = spatial_softmax(conv_branch) if(verbose):print(conv_branch.get_shape().as_list(), "spatial softmax") conv_branch = tf.nn.avg_pool(conv_branch, ksize=2, strides=2,padding="SAME") if(verbose):print(conv_branch.get_shape().as_list(), "average pooling") # size of flattened matrix should be 4500 fc = flatten(conv_branch) if(verbose):print(fc.get_shape().as_list(), "flatten") # # fully connected network: W_fc = self.get_variable_weight_decay(scope + "/W_fc", shape=[4500, 128], initializer=tf.contrib.layers.xavier_initializer(), loss_category="line_existance_wd_loss") b_fc = self.get_variable_weight_decay(scope + "/b_fc", shape=[128], initializer=tf.constant_initializer(0), loss_category="line_existance_wd_loss") fc = tf.matmul(fc, W_fc)+ b_fc if(verbose):print(fc.get_shape().as_list(), "fully connected") fc = tf.nn.relu(fc, name=scope + "/RELU2") if(verbose):print(fc.get_shape().as_list(), "relu") # # fully connected network: W_fc = self.get_variable_weight_decay(scope + "/W_fc1", shape=[128, 4], initializer=tf.contrib.layers.xavier_initializer(), loss_category="line_existance_wd_loss") b_fc = self.get_variable_weight_decay(scope + "/b_fc1", shape=[4], initializer=tf.constant_initializer(0), loss_category="line_existance_wd_loss") fc = tf.matmul(fc, W_fc)+ b_fc if(verbose):print(fc.get_shape().as_list(), "fully connected") fc = tf.math.sigmoid(fc,name=scope + '/existance_logits') if(verbose):print(fc.get_shape().as_list(), "sigmoid") return fc