2

I know this question has been asked before, however I've a specific question which has not been answered before.

I am trying to define a Siamese network in Tensorflow as follows:

def conv(self, x, num_out_maps, ksize, stride, activation_fn=tf.nn.relu):
    padding_length = np.floor((ksize-1)/2).astype(np.int32)
    padded_input = tf.pad(x, [[0, 0], [padding_length, padding_length], [padding_length, padding_length], [0, 0]])
    return slim.conv2d(padded_input, num_out_maps, ksize, stride, padding='VALID', activation_fn=activation_fn)

def resconv(self, x, num_out_maps, ksize, stride):
    # Our residual block is: conv-relu-conv, then element-wise sum

    shortcut = None
    flag = tf.shape(x)[3] != num_out_maps or stride != 1

    conv1 = self.conv(x, num_out_maps, ksize, stride)
    conv2 = self.conv(conv1, num_out_maps, ksize, stride, activation_fn=None)

    if flag==1:
        shortcut = self.conv(x, num_out_maps, ksize, stride)
    else:
        shortcut = x

    return shortcut + conv2

def resblock(self, x, num_blocks, num_out_maps, ksize, stride):
    out = x
    for i in range(num_blocks):
        out = self.resconv(out, num_out_maps, ksize, stride)
    return out

def get_features(self, input_image):
    conv1 = self.conv(input_image, 32, 5, 2, activation_fn=None)
    # 5 residual blocks
    out = self.resblock(conv1, 5, 32, 3, 1)
    return out

def build_model1(self): # Siamese Code
    with tf.variable_scope('siamese', reuse=False):
        self.left_features = self.get_features(self.left)
    with tf.variable_scope('siamese', reuse=True):
        self.right_features = self.get_features(self.right)

As you can see, I am defining Siamese network in build_model1() function. The wrapper functions get_features(), conv(), resblock() and resconv() have been included for proper understanding.

I want to ask, is my implementation correct? I have always seen people using tf.get_variable() for initializing weights and biases to define a Siamese network (such as, in the answer to this SO question). I am assuming I don't have to use tf.get_variable('weights', shape=(x, y), ..) because slim.conv2d() might be doing it internally. Please help me with my doubt.

kunal18
  • 1,935
  • 5
  • 33
  • 58
  • Those who have down voted, please add comment so that I can improve the question! – kunal18 Jan 15 '18 at 19:35
  • You should use the `tf.layers` package instead of slim. Slim hasn't been updated in a while. You can check if what you do work by comparing the output of left and right given the same input. – Olivier Moindrot Jan 15 '18 at 20:58

0 Answers0