def build_model(data_tensor, reuse, training, output_shape):
    """Create the hgru from Learning long-range..."""
    if isinstance(output_shape, list):
        output_shape = output_shape[0]
    with tf.variable_scope('cnn', reuse=reuse):
        with tf.variable_scope('input', reuse=reuse):
            x = tf.layers.conv2d(inputs=data_tensor,
                                 filters=24,
                                 kernel_size=11,
                                 name='l0',
                                 strides=(1, 1),
                                 padding='same',
                                 activation=tf.nn.relu,
                                 trainable=training,
                                 use_bias=True)
            layer_hgru = hgru.hGRU('hgru_1',
                                   x_shape=x.get_shape().as_list(),
                                   timesteps=8,
                                   h_ext=15,
                                   strides=[1, 1, 1, 1],
                                   padding='SAME',
                                   aux={
                                       'reuse': False,
                                       'constrain': False
                                   },
                                   train=training)
            h2, inh = layer_hgru.build(x)

    layers = [[x, inh]]
    ds_list = []
    bs = data_tensor.get_shape().as_list()[0]
    alpha = 5.
    beta = 1.
    dist = tf.distributions.Beta(alpha, beta)
    for idx, acts in enumerate(zip(layers)):
        (xs, inhs) = acts[0]
        # xs *= mask
        # inhs *= tf.abs(1 - mask)
        rand = dist.sample([])
        sel = tf.cast(tf.round(tf.random_uniform([], minval=1, maxval=bs - 1)),
                      tf.int32)
        xs = tf.stop_gradient(rand * xs + ((1 - rand) * tf.roll(xs, sel, 0)))
        xs = tf.reshape(xs, [bs, -1])
        inhs = tf.reshape(inhs, [bs, -1])

        # Augmentation
        rolled_xs = []
        for r in range(1, bs - 1):
            rolled_xs += [tf.roll(xs, r, 0)]
        xs = tf.concat([xs] + rolled_xs, 0)
        inhs = tf.tile(inhs, [bs - 1, 1])

        # CPC distances
        # xs = tf.nn.l2_normalize(xs, -1)
        # inhs = tf.nn.l2_normalize(inhs, -1)
        ds = tf.reduce_sum((xs * inhs), axis=-1)
        ds = tf.reshape(ds, [bs - 1, -1])
        ds_list += [ds]
    ds_list = tf.transpose(tf.concat(ds_list, -1))
    return ds_list, {}
Пример #2
0
def build_model(data_tensor, reuse, training, output_shape):
    """Create the hgru from Learning long-range..."""
    if isinstance(output_shape, list):
        output_shape = output_shape[0]
    with tf.variable_scope('cnn', reuse=reuse):
        with tf.variable_scope('input', reuse=reuse):
            o_x = tf.layers.conv2d(inputs=data_tensor,
                                   filters=24,
                                   kernel_size=11,
                                   name='l0',
                                   strides=(1, 1),
                                   padding='same',
                                   activation=tf.nn.relu,
                                   trainable=training,
                                   use_bias=True)
            n, h, w, c = o_x.get_shape().as_list()
            h = h - 15
            w = w - 15
            x = tf.image.resize_image_with_crop_or_pad(o_x, h, w)
            h = h - 5
            w = w - 5
            x = tf.image.random_crop(x, [n, h, w, c])
            layer_hgru = hgru.hGRU('hgru_1',
                                   x_shape=x.get_shape().as_list(),
                                   timesteps=8,
                                   h_ext=15,
                                   strides=[1, 1, 1, 1],
                                   padding='SAME',
                                   aux={
                                       'reuse': False,
                                       'constrain': False
                                   },
                                   train=training)
            h2, inh = layer_hgru.build(x)

    layers = [[[o_x, x], inh]]
    ds_list = []
    nulls = 32
    for idx, acts in enumerate(zip(layers)):
        (x, inhs) = acts[0]
        xs = [x[1]]
        x = x[0]
        crop_size = o_x.get_shape().as_list()
        crop_size[1] = h
        crop_size[2] = w
        for idx in range(nulls):
            xs += [tf.random_crop(o_x, crop_size)]
        xs = tf.reshape(tf.concat(xs, 0),
                        [crop_size[0] * nulls + crop_size[0], -1])
        inhs = tf.reshape(inhs, [crop_size[0], -1])
        inhs = tf.tile(inhs, [nulls + 1, 1])

        # CPC distances
        ds = tf.reduce_sum((xs * inhs), axis=-1)
        ds = tf.reshape(ds, [crop_size[0], -1])
        ds_list += [ds]
    ds_list = tf.transpose(tf.concat(ds_list, -1))
    return ds_list, {}
Пример #3
0
def build_model(data_tensor, reuse, training, output_shape):
    """Create the hgru from Learning long-range..."""
    if isinstance(output_shape, list):
        output_shape = output_shape[0]
    alpha = 5.
    beta = 1.
    bs = data_tensor.get_shape().as_list()[0]
    dist = tf.distributions.Beta(alpha, beta)
    rand = dist.sample([])
    sel = tf.cast(tf.round(tf.random_uniform([], minval=1, maxval=bs - 1)),
                  tf.int32)
    with tf.variable_scope('cnn', reuse=reuse):
        with tf.variable_scope('input', reuse=reuse):
            o_x = tf.layers.conv2d(inputs=data_tensor,
                                   filters=24,
                                   kernel_size=11,
                                   name='l0',
                                   strides=(1, 1),
                                   padding='same',
                                   activation=tf.nn.relu,
                                   trainable=training,
                                   use_bias=True)
            x = rand * o_x + ((1 - rand) * tf.roll(o_x, sel, 0))
            layer_hgru = hgru.hGRU('hgru_1',
                                   x_shape=x.get_shape().as_list(),
                                   timesteps=8,
                                   h_ext=15,
                                   strides=[1, 1, 1, 1],
                                   padding='SAME',
                                   aux={
                                       'reuse': False,
                                       'constrain': False
                                   },
                                   train=training)
            h2, inh = layer_hgru.build(x)

    with tf.variable_scope('ro', reuse=reuse):
        h2 = normalization.batch(bottom=h2,
                                 renorm=False,
                                 name='hgru_bn',
                                 training=training)
        activity = conv.readout_layer(activity=h2,
                                      reuse=reuse,
                                      training=training,
                                      output_shape=output_shape)
    extra_activities = {'activity': h2}
    return activity, extra_activities
Пример #4
0
def build_model(data_tensor, reuse, training, output_shape):
    """Create the hgru from Learning long-range..."""
    if isinstance(output_shape, list):
        output_shape = output_shape[0]
    with tf.variable_scope('cnn', reuse=reuse):
        with tf.variable_scope('input', reuse=reuse):
            x = tf.layers.conv2d(
                inputs=data_tensor,
                filters=24,
                kernel_size=11,
                name='l0',
                strides=(1, 1),
                padding='same',
                activation=tf.nn.relu,
                trainable=training,
                use_bias=True)
            layer_hgru = hgru.hGRU(
                'hgru_1',
                x_shape=x.get_shape().as_list(),
                timesteps=8,
                h_ext=15,
                strides=[1, 1, 1, 1],
                padding='SAME',
                aux={'reuse': False, 'constrain': False},
                train=training)
            h2, inh = layer_hgru.build(x)
            # h2 = normalization.batch(
            #     bottom=h2,
            #     renorm=True,
            #     name='hgru_bn',
            #     training=training)
            # inh = normalization.batch(
            #     bottom=inh,
            #     renorm=True,
            #     name='hgru_inh',
            #     training=training)

    layers = [[x, tf.nn.relu(inh)]]
    ds_list = []
    bs = data_tensor.get_shape().as_list()[0]
    for idx, acts in enumerate(zip(layers)):
        (xs, inhs) = acts[0]
        xs = tf.reshape(xs, [bs, -1])
        inhs = tf.reshape(inhs, [bs, -1])

        # Augmentation
        # roll_choices = tf.range(
        #     bs - 1,
        #     dtype=tf.int32)
        # samples = tf.multinomial(
        #     tf.log([tf.ones_like(tf.cast(roll_choices, tf.float32))]), 1)
        # rand_roll = roll_choices[tf.cast(samples[0][0], tf.int32)]
        # rolled_xs = tf.roll(xs, rand_roll, 0)
        # xs = tf.concat([xs, rolled_xs], 0)
        # inhs = tf.concat([inhs, inhs], 0)
        rolled_xs = []
        for r in range(1, bs - 1):
            rolled_xs += [tf.roll(xs, r, 0)]
        xs = tf.concat([xs] + rolled_xs, 0)
        inhs = tf.tile(inhs, [bs - 1, 1])

        # CPC distances
        # denom_xs = tf.sqrt(
        #     tf.reduce_sum(tf.matmul(xs, xs, transpose_b=True), axis=-1))
        # denom_inhs = tf.sqrt(
        #     tf.reduce_sum(tf.matmul(inhs, inhs, transpose_b=True), axis=-1))
        # num = tf.reduce_sum(xs * inhs, axis=-1)
        # ds = num / (denom_xs * denom_inhs)
        xs = tf.nn.l2_normalize(xs, 0)
        inhs = tf.nn.l2_normalize(inhs, 0)
        # xs = tf.nn.l2_normalize(xs, -1)
        # inhs = tf.nn.l2_normalize(inhs, -1)

        # ds = 1 - tf.losses.cosine_distance(
        #     xs,
        #     inhs,
        #     axis=-1,
        #     reduction=tf.losses.Reduction.NONE)
        ds = tf.reduce_sum(xs * inhs, axis=-1)
        ds = tf.reshape(ds, [bs - 1, -1])
        # ds = tf.nn.softmax(ds, 0)
        ds_list += [ds]
    ds_list = tf.transpose(tf.concat(ds_list, -1))
    return ds_list, {}