• 大小: 642KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-05-12
  • 语言: 其他
  • 标签: capsule  

资源简介

基于tensorflow搭建的capsule深度学习网络,并在mnist数据集进行训练

资源截图

代码片段和文件信息

import numpy as np
import tensorflow as tf

from config import cfg


epsilon = 1e-9


class Capslayer(object):
    ‘‘‘ Capsule layer.
    Args:
        input: A 4-D tensor.
        num_outputs: the number of capsule in this layer.
        vec_len: integer the length of the output vector of a capsule.
        layer_type: string one of ‘FC‘ or “CONV“ the type of this layer
            fully connected or convolution for the future expansion capability
        with_routing: boolean this capsule is routing with the
                      lower-level layer capsule.

    Returns:
        A 4-D tensor.
    ‘‘‘
    def __init__(self num_outputs vec_len with_routing=True layer_type=‘FC‘):
        self.num_outputs = num_outputs
        self.vec_len = vec_len
        self.with_routing = with_routing
        self.layer_type = layer_type

    def __call__(self input kernel_size=None stride=None):
        ‘‘‘
        The parameters ‘kernel_size‘ and ‘stride‘ will be used while ‘layer_type‘ equal ‘CONV‘
        ‘‘‘
        if self.layer_type == ‘CONV‘:
            self.kernel_size = kernel_size
            self.stride = stride

            if not self.with_routing:
                # the PrimaryCaps layer a convolutional layer
                # input: [batch_size 20 20 256]
                assert input.get_shape() == [cfg.batch_size 20 20 256]

                ‘‘‘
                # version 1 computational expensive
                capsules = []
                for i in range(self.vec_len):
                    # each capsule i: [batch_size 6 6 32]
                    with tf.variable_scope(‘ConvUnit_‘ + str(i)):
                        caps_i = tf.contrib.layers.conv2d(input self.num_outputs
                                                          self.kernel_size self.stride
                                                          padding=“VALID“ activation_fn=None)
                        caps_i = tf.reshape(caps_i shape=(cfg.batch_size -1 1 1))
                        capsules.append(caps_i)
                assert capsules[0].get_shape() == [cfg.batch_size 1152 1 1]
                capsules = tf.concat(capsules axis=2)
                ‘‘‘

                # version 2 equivalent to version 1 but higher computational
                # efficiency.
                # NOTE: I can‘t find out any words from the paper whether the
                # PrimaryCap convolution does a ReLU activation or not before
                # squashing function but experiment show that using ReLU get a
                # higher test accuracy. So which one to use will be your choice
                capsules = tf.contrib.layers.conv2d(input self.num_outputs * self.vec_len
                                                    self.kernel_size self.stride padding=“VALID“
                                                    activation_fn=tf.nn.relu)
                # capsules = tf.contrib.layers.conv2d(input self.num_outputs * self.vec_len
          

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2017-11-10 02:53  CapsNet-Tensorflow-master\
     文件          40  2017-11-10 02:53  CapsNet-Tensorflow-master\.gitignore
     文件         321  2017-11-10 02:53  CapsNet-Tensorflow-master\CONTRIBUTING.md
     文件       11357  2017-11-10 02:53  CapsNet-Tensorflow-master\LICENSE
     文件        3914  2017-11-10 02:53  CapsNet-Tensorflow-master\README.md
     文件        8946  2017-11-10 02:53  CapsNet-Tensorflow-master\capslayer.py
     文件        7090  2017-11-10 02:53  CapsNet-Tensorflow-master\capsNet.py
     文件        1631  2017-11-10 02:53  CapsNet-Tensorflow-master\config.py
     目录           0  2017-11-10 02:53  CapsNet-Tensorflow-master\imgs\
     文件      207236  2017-11-10 02:53  CapsNet-Tensorflow-master\imgs\capsuleVSneuron.png
     文件       26606  2017-11-10 02:53  CapsNet-Tensorflow-master\imgs\my_wechat_QR.png
     文件       21477  2017-11-10 02:53  CapsNet-Tensorflow-master\imgs\nb312_wechat.png
     文件       21824  2017-11-10 02:53  CapsNet-Tensorflow-master\imgs\wechat_group.png
     文件        2164  2017-11-10 02:53  CapsNet-Tensorflow-master\main.py
     文件         369  2017-11-10 02:53  CapsNet-Tensorflow-master\plot_acc.R
     目录           0  2017-11-10 02:53  CapsNet-Tensorflow-master\results\
     文件        6442  2017-11-10 02:53  CapsNet-Tensorflow-master\results\accuracy.csv
     文件        8583  2017-11-10 02:53  CapsNet-Tensorflow-master\results\accuracy.png
     文件       19152  2017-11-10 02:53  CapsNet-Tensorflow-master\results\margin_loss.png
     文件       14237  2017-11-10 02:53  CapsNet-Tensorflow-master\results\reconstruction_loss.png
     文件       18962  2017-11-10 02:53  CapsNet-Tensorflow-master\results\test_000.png
     文件       19819  2017-11-10 02:53  CapsNet-Tensorflow-master\results\test_005.png
     文件       18659  2017-11-10 02:53  CapsNet-Tensorflow-master\results\test_010.png
     文件       19282  2017-11-10 02:53  CapsNet-Tensorflow-master\results\test_015.png
     文件       19239  2017-11-10 02:53  CapsNet-Tensorflow-master\results\test_020.png
     文件       18726  2017-11-10 02:53  CapsNet-Tensorflow-master\results\test_025.png
     文件       20228  2017-11-10 02:53  CapsNet-Tensorflow-master\results\test_030.png
     文件       20322  2017-11-10 02:53  CapsNet-Tensorflow-master\results\test_035.png
     文件       17620  2017-11-10 02:53  CapsNet-Tensorflow-master\results\test_040.png
     文件       19385  2017-11-10 02:53  CapsNet-Tensorflow-master\results\test_045.png
     文件       19796  2017-11-10 02:53  CapsNet-Tensorflow-master\results\test_050.png
............此处省略7个文件信息

评论

共有 条评论

相关资源