Esempio n. 1
0
    def __init__(self, input_tensor, encoder):
        net = input_tensor
        with tf.variable_scope('decoder'):
            with tf.variable_scope('layer-1'):
                net = capsule_layers.DeconvCapsuleLayer(kernel_size=4, num_capsule=4, num_atoms=4, upsamp_type='deconv',
                                                        scaling=2, padding='same', routings=3)(net)
                net = layers.Concatenate(axis=-2)([net, encoder.l3])
                self.l1 = net

            with tf.variable_scope('layer-2'):
                net = capsule_layers.DeconvCapsuleLayer(kernel_size=4, num_capsule=4, num_atoms=2, upsamp_type='deconv',
                                                        scaling=2, padding='same', routings=3)(net)
                net = layers.Concatenate(axis=-2)([net, encoder.l2])
                self.l2 = net

            with tf.variable_scope('layer-3'):
                net = capsule_layers.DeconvCapsuleLayer(kernel_size=4, num_capsule=1, num_atoms=2, upsamp_type='deconv',
                                                        scaling=2, padding='same', routings=3)(net)
                net = layers.Concatenate(axis=-2)([net, encoder.l1])
                self.l3 = net
            with tf.variable_scope('layer-4'):
                net = capsule_layers.DeconvCapsuleLayer(kernel_size=4, num_capsule=1, num_atoms=2, upsamp_type='deconv',
                                                        scaling=1, padding='same', routings=3)(net)

                net = layers.Reshape((encoder.H.value, encoder.W.value, encoder.C.value))(net)

            self.output = net
Esempio n. 2
0
    def __init__(self, input_tensor, encoder):
        net = input_tensor
        with tf.variable_scope('decoder'):
            with tf.variable_scope('layer-1'):
                net = capsule_layers.DeconvCapsuleLayer(
                    kernel_size=4,
                    num_capsule=8,
                    num_atoms=2,
                    upsamp_type='deconv',
                    scaling=2,
                    padding='same',
                    routings=3,
                    name='deconv_cap_1_1')(net)
                self.l1 = net

            with tf.variable_scope('layer-2'):
                net = capsule_layers.DeconvCapsuleLayer(
                    kernel_size=4,
                    num_capsule=1,
                    num_atoms=2,
                    upsamp_type='deconv',
                    scaling=2,
                    padding='same',
                    routings=3,
                    name='deconv_cap_1_1')(net)
                net = layers.Reshape(
                    (encoder.H.value, encoder.W.value, encoder.C.value))(net)
            self.output = net
    def __init__(self, input_tensor, encoder, is_training, reuse):
        net = input_tensor
        with tf.variable_scope('Decoder'):
            # Layer 1 Up: Deconvolutional capsules, skip connection, convolutional capsules
            net = capsule_layers.DeconvCapsuleLayer(kernel_size=4, num_capsule=8, num_atoms=16, upsamp_type='deconv',
                                                    scaling=2, padding='same', routings=3, name='deconv_cap_1')(net)
            net = mf.batch_norm(net, is_training=is_training, reuse=reuse)
            self.upcap_1 = net

            net = layers.Concatenate(axis=-2, name='skip_1')([net, encoder.conv_cap_3])

            # Layer 2 Up: Deconvolutional capsules, skip connection, convolutional capsules
            net = capsule_layers.DeconvCapsuleLayer(kernel_size=4, num_capsule=4, num_atoms=8, upsamp_type='deconv',
                                                    scaling=2, padding='same', routings=3, name='deconv_cap_2')(net)
            net = mf.batch_norm(net, is_training=is_training, reuse=reuse)
            self.upcap_2 = net

            net = layers.Concatenate(axis=-2, name='skip_2')([net, encoder.conv_cap_2])

            # Layer 3 Up: Deconvolutional capsules, skip connection
            net = capsule_layers.DeconvCapsuleLayer(kernel_size=4, num_capsule=2, num_atoms=8, upsamp_type='deconv',
                                                    scaling=2, padding='same', routings=3, name='deconv_cap_3')(net)
            net = mf.batch_norm(net, is_training=is_training, reuse=reuse)
            self.upcap_3 = net

            net = layers.Concatenate(axis=-2, name='skip_3')([net, encoder.primary_caps])

            # Layer 4 Up: Deconvolutional capsules, skip connection
            net = capsule_layers.DeconvCapsuleLayer(kernel_size=4, num_capsule=1, num_atoms=16, upsamp_type='deconv',
                                                    scaling=2, padding='same', routings=3, name='deconv_cap_4')(net)
            self.upcap_4 = net

            # Reconstruction - Reshape, skip connection + 3x conventional Conv2D layers
            _, H, W, C, D = net.get_shape()

            net = layers.Reshape((H.value, W.value, D.value))(net)
            net = layers.Concatenate(axis=-1, name='skip_4')([net, encoder.conv1])

            net = layers.Conv2D(filters=64, kernel_size=1, padding='same', kernel_initializer='he_normal',
                                activation='relu', name='recon_1')(net)

            net = layers.Conv2D(filters=128, kernel_size=1, padding='same', kernel_initializer='he_normal',
                                activation='relu', name='recon_2')(net)

            if tf.rank(encoder.input_tensor) == 3:
                self.out_depth = 1
            else:
                self.out_depth = encoder.input_tensor.shape[3].value

            net = layers.Conv2D(filters=self.out_depth, kernel_size=1, padding='same', kernel_initializer='he_normal',
                                activation='sigmoid', name='out_recon')(net)

            self.output = net
Esempio n. 4
0
    def __init__(self, input_tensor, encoder, is_training, reuse):
        net = input_tensor
        with tf.variable_scope('Decoder'):
            with tf.variable_scope('UpCaps_1'):
                # Layer 1 Up: Deconvolutional capsules, skip connection, convolutional capsules
                net = capsule_layers.DeconvCapsuleLayer(
                    kernel_size=4,
                    num_capsule=8,
                    num_atoms=32,
                    upsamp_type='deconv',
                    scaling=2,
                    padding='same',
                    routings=3,
                    name='deconv_cap_1_1')(net)
                self.upcap_1_1 = net

                net = layers.Concatenate(
                    axis=-2, name='up_1')([net, encoder.conv_cap_3_1])

                net = capsule_layers.ConvCapsuleLayer(
                    kernel_size=5,
                    num_capsule=4,
                    num_atoms=32,
                    strides=1,
                    padding='same',
                    routings=3,
                    name='deconv_cap_1_2')(net)
                self.upcap_1_2 = net

            with tf.variable_scope('UpCaps_2'):
                # Layer 2 Up: Deconvolutional capsules, skip connection, convolutional capsules
                net = capsule_layers.DeconvCapsuleLayer(
                    kernel_size=4,
                    num_capsule=4,
                    num_atoms=16,
                    upsamp_type='deconv',
                    scaling=2,
                    padding='same',
                    routings=3,
                    name='deconv_cap_2_1')(net)
                self.upcap_2_1 = net

                net = layers.Concatenate(
                    axis=-2, name='up_2')([net, encoder.conv_cap_2_1])

                net = capsule_layers.ConvCapsuleLayer(
                    kernel_size=5,
                    num_capsule=4,
                    num_atoms=16,
                    strides=1,
                    padding='same',
                    routings=3,
                    name='deconv_cap_2_2')(net)
                self.upcap_2_2 = net

            with tf.variable_scope('UpCaps_3'):
                # Layer 3 Up: Deconvolutional capsules, skip connection
                net = capsule_layers.DeconvCapsuleLayer(
                    kernel_size=4,
                    num_capsule=2,
                    num_atoms=16,
                    upsamp_type='deconv',
                    scaling=2,
                    padding='same',
                    routings=3,
                    name='deconv_cap_3_1')(net)
                self.upcap_3_1 = net

                net = layers.Concatenate(axis=-2,
                                         name='up_3')([net, encoder.conv1])
            with tf.variable_scope('Reconstruction'):
                # Layer 4: Reconstruction - Convolutional Capsule: 1x1, 3x conventional Conv2D layers
                net = capsule_layers.ConvCapsuleLayer(kernel_size=1,
                                                      num_capsule=1,
                                                      num_atoms=16,
                                                      strides=1,
                                                      padding='same',
                                                      routings=3,
                                                      name='seg_caps')(net)

                _, H, W, C, D = net.get_shape()

                net = layers.Reshape((H.value, W.value, D.value))(net)

                net = layers.Conv2D(filters=64,
                                    kernel_size=1,
                                    padding='same',
                                    kernel_initializer='he_normal',
                                    activation='relu',
                                    name='recon_1')(net)

                net = layers.Conv2D(filters=128,
                                    kernel_size=1,
                                    padding='same',
                                    kernel_initializer='he_normal',
                                    activation='relu',
                                    name='recon_2')(net)

                net = layers.Conv2D(filters=1,
                                    kernel_size=1,
                                    padding='same',
                                    kernel_initializer='he_normal',
                                    activation='sigmoid',
                                    name='out_recon')(net)

            self.output = net
Esempio n. 5
0
    def __init__(self, input_tensor, encoder, is_training, reuse):
        net = input_tensor
        with tf.variable_scope('Decoder'):
            #net = capsule_layers.DeconvCapsuleLayer(kernel_size=5, num_capsule=32, num_atoms=2, upsamp_type='deconv',
            #                                        scaling=2, padding='same', routings=3,
            #                                        name='caps_deconv5')(net)
            #self.upcap_1 = net

            #net = layers.Concatenate(axis=3, name='up4')([net, encoder.caps_conv5])
            #net = capsule_layers.DeconvCapsuleLayer(kernel_size=5, num_capsule=16, num_atoms=2, upsamp_type='deconv',
            #                                        scaling=2, padding='same', routings=3,
            #                                        name='caps_deconv4')(net)
            #self.upcap_2 = net

            #net = layers.Concatenate(axis=3, name='up3')([net, encoder.caps_conv4])
            net = capsule_layers.DeconvCapsuleLayer(kernel_size=5,
                                                    num_capsule=8,
                                                    num_atoms=8,
                                                    upsamp_type='deconv',
                                                    scaling=2,
                                                    padding='same',
                                                    routings=3,
                                                    name='caps_deconv3')(net)
            net = mf.batch_norm(net, is_training=is_training, reuse=reuse)
            self.upcap_3 = net

            net = layers.Concatenate(axis=3,
                                     name='up2')([net, encoder.caps_conv3])
            net = capsule_layers.DeconvCapsuleLayer(kernel_size=5,
                                                    num_capsule=4,
                                                    num_atoms=4,
                                                    upsamp_type='deconv',
                                                    scaling=2,
                                                    padding='same',
                                                    routings=3,
                                                    name='caps_deconv2')(net)
            net = mf.batch_norm(net, is_training=is_training, reuse=reuse)
            self.upcap_4 = net

            net = layers.Concatenate(axis=3,
                                     name='up1')([net, encoder.caps_conv2])
            net = capsule_layers.DeconvCapsuleLayer(kernel_size=5,
                                                    num_capsule=2,
                                                    num_atoms=2,
                                                    upsamp_type='deconv',
                                                    scaling=2,
                                                    padding='same',
                                                    routings=3,
                                                    name='caps_deconv1')(net)
            net = mf.batch_norm(net, is_training=is_training, reuse=reuse)
            self.upcap_5 = net

            net = layers.Concatenate(axis=3,
                                     name='up0')([net, encoder.caps_conv1])
            net = capsule_layers.DeconvCapsuleLayer(kernel_size=5,
                                                    num_capsule=1,
                                                    num_atoms=2,
                                                    upsamp_type='deconv',
                                                    scaling=2,
                                                    padding='same',
                                                    routings=3,
                                                    name='caps_deconv0')(net)
            self.upcap_6 = net

            _, H, W, C, A = net.get_shape()
            net = layers.Reshape((H.value, W.value, A.value))(net)
            #net = layers.Concatenate(axis=-1, name='skip_4')([net, encoder.conv1])

            net = layers.Conv2D(filters=64,
                                kernel_size=1,
                                padding='same',
                                kernel_initializer='he_normal',
                                activation='relu',
                                name='recon_1')(net)

            net = layers.Conv2D(filters=128,
                                kernel_size=1,
                                padding='same',
                                kernel_initializer='he_normal',
                                activation='relu',
                                name='recon_2')(net)

            if tf.rank(encoder.input_tensor) == 3:
                self.out_depth = 1
            else:
                self.out_depth = encoder.input_tensor.shape[3].value

            net = layers.Conv2D(filters=self.out_depth,
                                kernel_size=1,
                                padding='same',
                                kernel_initializer='he_normal',
                                activation='sigmoid',
                                name='out_recon')(net)

            self.output = net