def inference(inputs,
              num_classes,
              routing_ites=4,
              remake=False,
              training=False,
              name='capsnet_1d'):
    """

    :param inputs:
    :param num_classes:
    :param routing_ites:
    :param remake:
    :param name:
    :return:
    """

    with tf.variable_scope(name) as scope:
        inputs_shape = inputs.get_shape()
        batch_size = inputs_shape[0].value
        image_height = inputs_shape[2].value
        image_width = inputs_shape[3].value

        # ReLU Conv1
        # Images shape (b, 1, 24, 56) -> conv 5x5 filters, 32 output channels, strides 2 with padding, ReLU
        # nets -> (b, 256, 16, 48)
        print('inputs shape: %s' % inputs.get_shape())
        inputs = tf.check_numerics(inputs, message="nan or inf from: inputs")

        conv1 = conv2d(inputs,
                       kernel=3,
                       out_channels=32,
                       stride=1,
                       padding='SAME',
                       activation_fn=tf.nn.relu,
                       normalizer_fn=tf.contrib.layers.batch_norm,
                       name='relu_conv1')
        print('conv1 shape: %s' % conv1.get_shape())
        pool1 = tf.nn.max_pool(conv1,
                               ksize=[1, 1, 2, 2],
                               strides=[1, 1, 2, 2],
                               padding='VALID',
                               data_format='NCHW',
                               name='pool1')
        print('pool1 shape: %s' % pool1.get_shape())

        conv2 = conv2d(pool1,
                       kernel=3,
                       out_channels=64,
                       stride=1,
                       padding='SAME',
                       activation_fn=tf.nn.relu,
                       normalizer_fn=tf.contrib.layers.batch_norm,
                       name='relu_conv2')
        print('conv2 shape: %s' % conv2.get_shape())
        pool2 = tf.nn.max_pool(conv2,
                               ksize=[1, 1, 2, 2],
                               strides=[1, 1, 2, 2],
                               padding='VALID',
                               data_format='NCHW',
                               name='pool2')
        print('pool2 shape: %s' % pool2.get_shape())
        pool2_dropout = tf.layers.dropout(pool2,
                                          0.5,
                                          training=training,
                                          name='pool2_dropout')

        # conv3 = conv2d(
        #     pool2,
        #     kernel=3, out_channels=128, stride=1, padding='SAME',
        #     activation_fn=tf.nn.relu, normalizer_fn=tf.contrib.layers.batch_norm,
        #     name='relu_conv3'
        # )
        # print('conv3 shape: %s' % conv3.get_shape())
        # pool3 = tf.nn.max_pool(
        #     conv3,
        #     ksize=[1, 1, 2, 2], strides=[1, 1, 2, 2],
        #     padding='VALID', data_format='NCHW', name='pool3'
        # )
        # print('pool3 shape: %s' % pool3.get_shape())

        print("\nprimary layer:")
        primary_out_capsules = 32
        primary_caps_activations, conv_primary = primary_caps1d(
            pool2_dropout,
            kernel_size=3,
            out_capsules=primary_out_capsules,
            stride=1,
            padding='VALID',
            activation_length=8,
            name='primary_caps')  # (b, 32, 4, 20, 8)

        print("\nclass capsule layer:")
        class_caps_activations, class_coupling_coeffs = class_caps1d(
            primary_caps_activations,
            num_classes=num_classes,
            activation_length=16,
            routing_ites=routing_ites,
            batch_size=batch_size,
            name='class_capsules')
        # class_coupling_coeffs = tf.Print(class_coupling_coeffs, [class_coupling_coeffs], summarize=50)
        # class_caps_activations = tf.check_numerics(class_caps_activations, message="nan or inf from: class_caps_activations")
        print('class_coupling_coeffs shape: %s' %
              class_coupling_coeffs.get_shape())
        print('class_caps_activations shape: %s' %
              class_caps_activations.get_shape())

        if remake:
            remakes_flatten = _remake(class_caps_activations,
                                      image_height * image_width)
        else:
            remakes_flatten = None

        print("\ndecode layers:")
        label_logits = _decode(primary_caps_activations,
                               primary_out_capsules,
                               coupling_coeffs=class_coupling_coeffs,
                               num_classes=num_classes,
                               batch_size=batch_size,
                               pool1=pool1,
                               pool2=pool2,
                               training=training)
        # label_logits = tf.Print(label_logits, [tf.constant("label_logits"), label_logits[0]], summarize=100)
        # label_logits = tf.check_numerics(label_logits, message="nan or inf from: label_logits")

        labels2d = tf.argmax(label_logits, axis=3)
        labels2d_expanded = tf.expand_dims(labels2d, -1)
        tf.summary.image('labels', tf.cast(labels2d_expanded, tf.uint8))

    return class_caps_activations, remakes_flatten, label_logits
def inference(inputs,
              num_classes,
              routing_ites=3,
              remake=False,
              training=False,
              name='capsnet_1d'):
    """

    :param inputs:
    :param num_classes:
    :param routing_ites:
    :param remake:
    :param name:
    :return:
    """

    with tf.variable_scope(name) as scope:
        inputs_shape = inputs.get_shape()
        batch_size = inputs_shape[0].value
        image_height = inputs_shape[2].value
        image_width = inputs_shape[3].value

        # ReLU Conv1
        # Images shape (b, 1, 24, 56) -> conv 5x5 filters, 32 output channels, strides 2 with padding, ReLU
        # nets -> (b, 256, 16, 48)
        print('inputs shape: %s' % inputs.get_shape())
        inputs = tf.check_numerics(inputs, message="nan or inf from: inputs")

        print("\nconv1 layer:")
        conv1 = conv2d(inputs,
                       kernel=9,
                       out_channels=256,
                       stride=1,
                       padding='VALID',
                       activation_fn=tf.nn.relu,
                       name='relu_conv1')
        # conv1 = tf.check_numerics(conv1, message="nan or inf from: conv1")
        print('conv1 shape: %s' % conv1.get_shape())

        # print("\nconv2 layer:")
        # conv2 = conv2d(
        #     conv1,
        #     kernel=5, out_channels=256, stride=1, padding='VALID',
        #     activation_fn=tf.nn.relu, name='relu_conv2'
        # )
        # # conv2 = tf.check_numerics(conv2, message="nan or inf from: conv2")
        # print('conv2 shape: %s' % conv2.get_shape())

        # PrimaryCaps
        # (b, 256, 16, 48) -> capsule 1x1 filter, 32 output capsule, strides 1 without padding
        # nets -> activations (?, 14, 14, 32))
        print("\nprimary layer:")
        primary_out_capsules = 24
        primary_caps_activations, conv2 = primary_caps1d(
            conv1,
            kernel_size=7,
            out_capsules=primary_out_capsules,
            stride=2,
            padding='VALID',
            activation_length=8,
            name='primary_caps')  # (b, 32, 4, 20, 8)

        # (b, 32, 4, 20, 8) -> # (b, 32*4*20, 2*64)
        print("\nclass capsule layer:")
        class_caps_activations, class_coupling_coeffs = class_caps1d(
            primary_caps_activations,
            num_classes=num_classes,
            activation_length=16,
            routing_ites=routing_ites,
            batch_size=batch_size,
            name='class_capsules')
        # class_coupling_coeffs = tf.Print(class_coupling_coeffs, [class_coupling_coeffs], summarize=50)
        # class_caps_activations = tf.check_numerics(class_caps_activations, message="nan or inf from: class_caps_activations")
        print('class_coupling_coeffs shape: %s' %
              class_coupling_coeffs.get_shape())
        print('class_caps_activations shape: %s' %
              class_caps_activations.get_shape())

        if remake:
            remakes_flatten = _remake(class_caps_activations,
                                      image_height * image_width)
        else:
            remakes_flatten = None

        print("\ndecode layers:")
        label_logits = _decode(primary_caps_activations,
                               primary_out_capsules,
                               coupling_coeffs=class_coupling_coeffs,
                               num_classes=num_classes,
                               batch_size=batch_size,
                               conv1=conv1,
                               conv2=conv2)
        # label_logits = tf.Print(label_logits, [tf.constant("label_logits"), label_logits[0]], summarize=100)
        # label_logits = tf.check_numerics(label_logits, message="nan or inf from: label_logits")

        labels2d = tf.argmax(label_logits, axis=3)
        labels2d_expanded = tf.expand_dims(labels2d, -1)
        tf.summary.image('labels', tf.cast(labels2d_expanded, tf.uint8))

    return class_caps_activations, remakes_flatten, label_logits
예제 #3
0
def inference(inputs, num_classes, routing_ites=3, remake=False, training=False, name='capsnet_1d'):
    """

    :param inputs:
    :param num_classes:
    :param routing_ites:
    :param remake:
    :param name:
    :return:
    """

    with tf.variable_scope(name) as scope:
        inputs_shape = inputs.get_shape()
        batch_size = inputs_shape[0].value
        image_height = inputs_shape[2].value
        image_width = inputs_shape[3].value

        # ReLU Conv1
        # Images shape (b, 1, 24, 56) -> conv 5x5 filters, 32 output channels, strides 2 with padding, ReLU
        # nets -> (b, 256, 16, 48)
        print('inputs shape: %s' % inputs.get_shape())
        inputs = tf.check_numerics(inputs, message="nan or inf from: inputs")

        conv1 = conv2d(
            inputs,
            kernel=3, out_channels=32, stride=1, padding='SAME',
            activation_fn=tf.nn.relu, normalizer_fn=tf.contrib.layers.batch_norm,
            name='relu_conv1'
        )
        print('conv1 shape: %s' % conv1.get_shape())
        pool1 = tf.nn.max_pool(
            conv1,
            ksize=[1, 1, 2, 2], strides=[1, 1, 2, 2],
            padding='VALID', data_format='NCHW', name='pool1'
        )
        print('pool1 shape: %s' % pool1.get_shape())
        # pool1_dropout = tf.layers.dropout(pool1, 0.5, training=training, name='pool1_dropout')

        # conv2 = conv2d(
        #     pool1,
        #     kernel=3, out_channels=64, stride=1, padding='VALID',
        #     activation_fn=tf.nn.relu, normalizer_fn=tf.contrib.layers.batch_norm,
        #     name='relu_conv2'
        # )
        # print('conv2 shape: %s' % conv2.get_shape())
        # pool2 = tf.nn.max_pool(
        #     conv2,
        #     ksize=[1, 1, 2, 2], strides=[1, 1, 2, 2],
        #     padding='VALID', data_format='NCHW', name='pool2'
        # )
        # print('pool2 shape: %s' % pool2.get_shape())
        # pool2_dropout = tf.layers.dropout(pool2, 0.5, training=training, name='pool2_dropout')

        # conv3 = conv2d(
        #     pool2,
        #     kernel=3, out_channels=128, stride=1, padding='SAME',
        #     activation_fn=tf.nn.relu, normalizer_fn=tf.contrib.layers.batch_norm,
        #     name='relu_conv3'
        # )
        # print('conv3 shape: %s' % conv3.get_shape())
        # pool3 = tf.nn.max_pool(
        #     conv3,
        #     ksize=[1, 1, 2, 2], strides=[1, 1, 2, 2],
        #     padding='VALID', data_format='NCHW', name='pool3'
        # )
        # print('pool3 shape: %s' % pool3.get_shape())

        print("\nprimary layer:")
        primary_out_capsules = 64
        primary_caps_activations, _ = primary_caps1d(
            pool1,
            kernel_size=3, out_capsules=primary_out_capsules, stride=1,
            padding='VALID', activation_length=3, name='primary_caps'
        )  # (b, 32, 4, 20, 8)
        # primary_caps_activations = tf.check_numerics(primary_caps_activations,
        #                                              message="nan or inf from: primary_caps_activations")

        print("\nconvolutional capsule layer 1:")
        conv_out_capsules_1 = 32
        conv_kernel_size_1, conv_stride_1 = 4, 2
        conv_caps_activations_1, conv_coupling_coeffs_1 = conv_capsule1d(
            primary_caps_activations,
            kernel_size=conv_kernel_size_1, stride=conv_stride_1, routing_ites=3,
            in_capsules=primary_out_capsules, out_capsules=conv_out_capsules_1,
            activation_length=4, training=training, name="conv_caps_1"
        )  # (b, 32, 6, 6, 8), (b*6*6, 32*9, 32)
        conv_caps_activations_1 = tf.check_numerics(conv_caps_activations_1,
                                                  message="nan or inf from: conv_caps_activations_1")

        print("\nconvolutional capsule layer 2:")
        conv_out_capsules_2 = 24
        conv_kernel_size_2, conv_stride_2 = 3, 1
        conv_caps_activations_2, conv_coupling_coeffs_2 = conv_capsule1d(
            conv_caps_activations_1,
            kernel_size=conv_kernel_size_2, stride=conv_stride_2, routing_ites=3,
            in_capsules=conv_out_capsules_1, out_capsules=conv_out_capsules_2,
            activation_length=8, training=training, name="conv_caps_2"
        )  # (b, 32, 6, 6, 8), (b*6*6, 32*9, 32)
        conv_caps_activations_2 = tf.check_numerics(conv_caps_activations_2,
                                                  message="nan or inf from: conv_caps_activations_2")

        print("\nclass capsule layer:")
        class_caps_activations, class_coupling_coeffs = class_caps1d(
            conv_caps_activations_2,
            num_classes=num_classes, activation_length=16, routing_ites=routing_ites,
            batch_size=batch_size, training=training, name='class_capsules')
        # class_coupling_coeffs = tf.Print(class_coupling_coeffs, [class_coupling_coeffs], summarize=50)
        class_caps_activations = tf.check_numerics(class_caps_activations,
                                                   message="nan or inf from: class_caps_activations")
        print('class_coupling_coeffs shape: %s' % class_coupling_coeffs.get_shape())
        print('class_caps_activations shape: %s' % class_caps_activations.get_shape())

        if remake:
            remakes_flatten = _remake(class_caps_activations, image_height * image_width)
        else:
            remakes_flatten = None

        print("\ntraceback layer 1:")
        conv_activations_2_shape = conv_caps_activations_2.get_shape()  # (b, 32, 4, 20, 8),
        conv_height_2, conv_width_2 = conv_activations_2_shape[2].value, conv_activations_2_shape[3].value
        conv_1_cond_prob = trace_conv_cond_prob(class_coupling_coeffs, conv_coupling_coeffs_2,
                                                conv_height_2, conv_width_2, conv_kernel_size_2, conv_stride_2,
                                                conv_out_capsules_2, conv_out_capsules_1)
        print("\ntraceback layer 2:")
        conv_activations_1_shape = conv_caps_activations_1.get_shape()  # (b, 32, 4, 20, 8),
        conv_height_1, conv_width_1 = conv_activations_1_shape[2].value, conv_activations_1_shape[3].value
        primary_cond_prob = trace_conv_cond_prob(conv_1_cond_prob, conv_coupling_coeffs_1,
                                                 conv_height_1, conv_width_1, conv_kernel_size_1, conv_stride_1,
                                                 conv_out_capsules_1, primary_out_capsules)

        primary_labels = trace_labels(primary_caps_activations, primary_cond_prob, num_classes)

        print('primary_caps_labels shape: %s' % primary_labels.get_shape())
        # class_labels = tf.Print(class_labels, [tf.constant("class_labels"), class_labels])
        # class_labels = tf.check_numerics(class_labels, message="nan or inf from: class_labels")
        # primary_labels = tf.reduce_sum(caps_probs_tiled, 1)

        print("\ndeconv layers:")
        primary_conv = conv2d(
            tf.transpose(primary_labels, perm=[0, 3, 1, 2]),
            kernel=3, out_channels=256, stride=1, padding='SAME',
            activation_fn=tf.nn.relu, name='primary_conv'
        )
        print('primary_conv shape: %s' % primary_conv.get_shape())

        deconv1 = deconv(
            primary_conv,
            kernel=3, out_channels=128, stride=1, data_format='NCHW',
            activation_fn=tf.nn.relu, normalizer_fn=tf.contrib.layers.batch_norm,
            name='deconv1'
        )
        print('deconv2 shape: %s' % deconv1.get_shape())
        concat1 = tf.concat([pool1, deconv1], axis=1, name='concat1')
        # dropout2 = tf.layers.dropout(concat2, 0.5, training=training, name='dropout2')
        concat1_conv = conv2d(
            concat1,
            kernel=3, out_channels=128, stride=1, padding='SAME',
            activation_fn=tf.nn.relu, normalizer_fn=tf.contrib.layers.batch_norm,
            name='concat1_conv'
        )
        print('deconv1_conv shape: %s' % concat1_conv.get_shape())

        deconv2 = deconv(
            concat1_conv,
            kernel=4, out_channels=128, stride=2, data_format='NCHW',
            activation_fn=tf.nn.relu, normalizer_fn=tf.contrib.layers.batch_norm,
            name='deconv2'
        )
        print('deconv2 shape: %s' % deconv2.get_shape())

        class_conv = conv2d(
            deconv2,
            kernel=3, out_channels=num_classes, stride=1, padding='VALID',
            name='class_conv'
        )
        print('class_conv shape: %s' % class_conv.get_shape())

        label_logits = tf.transpose(class_conv, perm=[0, 2, 3, 1])
        print('label_logits shape: %s' % label_logits.get_shape())
        # label_logits = tf.Print(label_logits, [tf.constant("label_logits"), label_logits])

        labels2d = tf.argmax(label_logits, axis=3)
        labels2d_expanded = tf.expand_dims(labels2d, -1)
        tf.summary.image('labels', tf.cast(labels2d_expanded, tf.uint8))

    return class_caps_activations, remakes_flatten, label_logits
def inference(inputs,
              num_classes,
              training=True,
              routing_ites=3,
              name='unet_pascal'):
    with tf.variable_scope(name) as scope:
        inputs_shape = inputs.get_shape()
        batch_size = inputs_shape[0].value

        combine_conv1 = _combine_conv(inputs, '1', training, out_channels=16)
        combine_conv2 = _combine_conv(combine_conv1,
                                      '2',
                                      training,
                                      out_channels=32)
        combine_conv3 = _combine_conv(combine_conv2,
                                      '3',
                                      training,
                                      out_channels=64)
        combine_conv4 = _combine_conv(combine_conv3,
                                      '4',
                                      training,
                                      out_channels=128)

        print("\nprimary layer:")
        primary_out_capsules = 32
        primary_caps_activations, primary_conv = primary_caps1d(
            combine_conv4,
            kernel_size=5,
            out_capsules=primary_out_capsules,
            stride=2,
            padding='VALID',
            activation_length=8,
            name='primary_caps')
        print('primary_conv shape: %s' % primary_conv.get_shape())

        print("\nclass capsule layer:")
        class_caps_activations, class_coupling_coeffs = class_caps1d(
            primary_caps_activations,
            num_classes=num_classes,
            activation_length=16,
            routing_ites=routing_ites,
            batch_size=batch_size,
            name='class_capsules')
        print('class_coupling_coeffs shape: %s' %
              class_coupling_coeffs.get_shape())
        print('class_caps_activations shape: %s' %
              class_caps_activations.get_shape())

        capsule_probs = tf.norm(primary_caps_activations, axis=-1)
        caps_probs_tiled = tf.tile(tf.expand_dims(capsule_probs, -1),
                                   [1, 1, 1, 1, num_classes])

        primary_activations_shape = primary_caps_activations.get_shape()
        height, width = primary_activations_shape[
            2].value, primary_activations_shape[3].value
        coupling_coeff_reshaped = tf.reshape(
            class_coupling_coeffs,
            [batch_size, primary_out_capsules, height, width, num_classes])

        primary_labels = tf.reduce_sum(
            coupling_coeff_reshaped * caps_probs_tiled, 1)
        print('\nprimary_labels shape: %s\n' % primary_labels.get_shape())
        concat1 = tf.concat(
            [primary_conv,
             tf.transpose(primary_labels, perm=[0, 3, 1, 2])],
            axis=1,
            name='concat1')
        primary_label_conv = conv2d(concat1,
                                    kernel=3,
                                    out_channels=128,
                                    stride=1,
                                    padding='SAME',
                                    activation_fn=tf.nn.relu,
                                    data_format='NCHW',
                                    name='primary_label_conv')

        combine_deconv5 = _combine_deconv(primary_label_conv,
                                          '5',
                                          training,
                                          combine_conv4,
                                          deconv_out_channels=256)
        combine_deconv4 = _combine_deconv(combine_deconv5,
                                          '4',
                                          training,
                                          combine_conv3,
                                          kernel_deconv=[7, 7],
                                          deconv_out_channels=128)
        combine_deconv3 = _combine_deconv(combine_deconv4,
                                          '3',
                                          training,
                                          combine_conv2,
                                          kernel_deconv=[6, 6],
                                          deconv_out_channels=64)
        combine_deconv2 = _combine_deconv(combine_deconv3,
                                          '2',
                                          training,
                                          combine_conv1,
                                          kernel_deconv=[6, 7],
                                          deconv_out_channels=64)
        combine_deconv1 = _combine_deconv(combine_deconv2,
                                          '1',
                                          training,
                                          None,
                                          kernel_deconv=[6, 7],
                                          deconv_out_channels=32)

        conv = conv2d(combine_deconv1,
                      kernel=3,
                      out_channels=num_classes,
                      stride=1,
                      padding='SAME',
                      name='label_conv')

        label_logits = tf.transpose(conv, perm=[0, 2, 3, 1])
        # label_logits = tf.check_numerics(label_logits, message="nan or inf from: label_logits")
        print('label_logits shape: %s' % label_logits.get_shape())
        return class_caps_activations, None, label_logits