Arbitrary filters for conv2d (as opposed to rectangular)

Viewed 1407

I using convolutional layers on data samples that consist of 2d images. One option for the shape of the filter is 1x2, which acts on a 1x2 contiguous block of two neighbor pixels. What if I want to have a filter which also acts on 2 pixels, but the pixels are separated by another one between them? Is it possible to encode such a filter for convolutions in the neural network?

3 Answers

This isn't really an answer, but a code snippet to show how I used Panchishin's answer to code for a Hexagonal game (Moderators: if there's a better way to expand on a comment, or send a private message to someone, please let me know)

Caveat: I'm a beginner in deep learning.

The game is Blackhole, used in the 2018 CodeCup competition: http://archive.codecup.nl/2018/60/rules_blackhole.html It is a hexagonal game played in a triangle shape. The code below defines the convolutional and relu layers.

def conv_weight_variable(w, h, in_ch, out_ch, learn):
    d = 1.0 / np.sqrt(in_ch * w * h)
    initial = tf.truncated_normal([w, h, in_ch, out_ch], stddev=d)
    return tf.Variable(initial, trainable=learn), iEnd

def conv_bias_variable(w, h, in_ch, out_ch, learn):
    d = 1.0 / np.sqrt(in_ch * w * h)
    initial = tf.constant([0.1*d]*out_ch, dtype=tf.float32)
    return tf.Variable(initial, trainable=learn), iEnd

def relu_conv_layer(input, in_ch, out_ch, learn):
    W, iEnd = conv_weight_variable(3, 3, in_ch, out_ch, learn)
    o = np.zeros((in_ch, out_ch), np.float32)
    i = np.ones((in_ch, out_ch), np.float32)
    maskW = np.array([[ o, i, i],
                      [ i, i, i],
                      [ i, i, o]])
    maskW = tf.constant(maskW, dtype=tf.float32)
    b, iEnd = conv_bias_variable(3, 3, in_ch, out_ch, learn)
    conv = tf.nn.conv2d(input, W * maskW, strides=[1, 1, 1, 1], padding='SAME')

    o = np.zeros(out_ch, np.float32)
    i = np.ones(out_ch, np.float32)
    maskO = np.array([[ i, i, i, i, i, i, i, i],
                      [ i, i, i, i, i, i, i, o],
                      [ i, i, i, i, i, i, o, o],
                      [ i, i, i, i, i, o, o, o],
                      [ i, i, i, i, o, o, o, o],
                      [ i, i, i, o, o, o, o, o],
                      [ i, i, o, o, o, o, o, o],
                      [ i, o, o, o, o, o, o, o]])
    maskO = tf.constant(maskO, dtype=tf.float32)
    return tf.nn.relu(conv + b)*maskO, W, b, iEnd
Related