张量流胶囊网络EM路由预测

时间:2018-07-05 20:14:01

标签: python-3.x tensorflow artificial-intelligence predict

我正在尝试基于胶囊网络创建分类器,我能够训练该网络,但是在进行预测时遇到问题。给定任意图像,我的代码应该如何进行预测(我需要一个示例)。重要的一件事是我正在使用 EM路由。在动态路由中,它足以计算出最后一个胶囊层的矢量长度,以获得预测的类,但是在EM路由中情况如何呢?

这是我的代码:

 poses, activations = m_capsules.nets.capsules_net(images, num_classes=10, iterations=3,
                                                      batch_size=batch_size, name='capsules_em')


    global_step = tf.train.get_or_create_global_step()
    loss = m_capsules.nets.spread_loss(
        labels, activations, iterations_per_epoch, global_step, name='spread_loss'
    )
    tf.summary.scalar('losses/spread_loss', loss)

    optimizer = tf.train.AdamOptimizer(learning_rate=0.0005)
    train_tensor = slim.learning.create_train_op(
        loss, optimizer, global_step=global_step, clip_gradient_norm=4.0
    )

    slim.learning.train(
        train_tensor,
        logdir="./log/train",
        log_every_n_steps=1,
        save_summaries_secs=60,
        saver=tf.train.Saver(max_to_keep=2),
        save_interval_secs=600,
    )

到目前为止,我一直在尝试编写估算器,但遇到了麻烦。下面是一个代码:

mnist_classifier = tf.estimator.Estimator(model_fn=m_capsules.nets.capsules_net, model_dir=dir)
prediction = mnist_classifier.predict(input_fn=words_input_fn)

我的模型如下:

def capsules_net(inputs, num_classes, iterations, batch_size, name='ocr-caps'):
"""Define the Capsule Network model
"""

with tf.variable_scope(name) as scope:
    # ReLU Conv1
    # Images shape (24, 28, 28, 1) -> conv 5x5 filters, 32 output channels, strides 2 with padding, ReLU
    # nets -> (?, 14, 14, 32)
    nets = conv2d(
        inputs,
        kernel=5, out_channels=26, stride=2, padding='SAME',
        activation_fn=tf.nn.relu, name='relu_conv1'
    )

    # PrimaryCaps
    # (?, 14, 14, 32) -> capsule 1x1 filter, 32 output capsule, strides 1 without padding
    # nets -> (poses (?, 14, 14, 32, 4, 4), activations (?, 14, 14, 32))
    nets = primary_caps(
        nets,
        kernel_size=1, out_capsules=26, stride=1, padding='VALID',
        pose_shape=[4, 4], name='primary_caps'
    )

    # ConvCaps1
    # (poses, activations) -> conv capsule, 3x3 kernels, strides 2, no padding
    # nets -> (poses (24, 6, 6, 32, 4, 4), activations (24, 6, 6, 32))
    nets = conv_capsule(
        nets, shape=[3, 3, 26, 26], strides=[1, 2, 2, 1], iterations=iterations,
        batch_size=batch_size, name='conv_caps1'
    )

    # ConvCaps2
    # (poses, activations) -> conv capsule, 3x3 kernels, strides 1, no padding
    # nets -> (poses (24, 4, 4, 32, 4, 4), activations (24, 4, 4, 32))
    nets = conv_capsule(
        nets, shape=[3, 3, 26, 26], strides=[1, 1, 1, 1], iterations=iterations,
        batch_size=batch_size, name='conv_caps2'
    )

    # Class capsules
    # (poses, activations) -> 1x1 convolution, 10 output capsules
    # nets -> (poses (24, 10, 4, 4), activations (24, 10))
    nets = class_capsules(nets, num_classes, iterations=iterations,
                          batch_size=batch_size, name='class_capsules')

    # poses (24, 10, 4, 4), activations (24, 10)
    poses, activations = nets

return poses, activations

0 个答案:

没有答案