Densenet-Tensorflow

在寻找densnet网络的时候,我发现了一个结构清晰完整的网络代码,在此作备份。

https://github.com/taki0112/Densenet-Tensorflow

Densenet-Tensorflow

Tensorflow implementation of Densenet using Cifar10, MNIST

  • The code that implements this paper is Densenet.py
  • There is a slight difference, I used AdamOptimizer

If you want to see the original author's code or other implementations, please refer to this link

 

Requirements

  • Tensorflow 1.x
  • Python 3.x
  • tflearn (If you are easy to use global average pooling, you should install tflearn
However, I implemented it using tf.layers, so don't worry

Issue

  • I used tf.contrib.layers.batch_norm
  def Batch_Normalization(x, training, scope):
        with arg_scope([batch_norm],
                       scope=scope,
                       updates_collections=None,
                       decay=0.9,
                       center=True,
                       scale=True,
                       zero_debias_moving_mean=True) :
            return tf.cond(training,
                           lambda : batch_norm(inputs=x, is_training=training, reuse=None),
                           lambda : batch_norm(inputs=x, is_training=training, reuse=True))

 

  • If not enough GPU memory, Please edit the code
with tf.Session() as sess : NO
with tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) as sess : OK

Idea

What is the "Global Average Pooling" ?

    def Global_Average_Pooling(x, stride=1) :
        width = np.shape(x)[1]
        height = np.shape(x)[2]
        pool_size = [width, height]
        return tf.layers.average_pooling2d(inputs=x, pool_size=pool_size, strides=stride) 
        # The stride value does not matter
If you use tflearn, please refer to this link
    def Global_Average_Pooling(x):
        return tflearn.layers.conv.global_avg_pool(x, name='Global_avg_pooling')

 

What is the "Dense Connectivity" ?

Dense_connectivity

What is the "Densenet Architecture" ?

Dense_Architecture

    def Dense_net(self, input_x):
        x = conv_layer(input_x, filter=2 * self.filters, kernel=[7,7], stride=2, layer_name='conv0')
        x = Max_Pooling(x, pool_size=[3,3], stride=2)

        x = self.dense_block(input_x=x, nb_layers=6, layer_name='dense_1')
        x = self.transition_layer(x, scope='trans_1')

        x = self.dense_block(input_x=x, nb_layers=12, layer_name='dense_2')
        x = self.transition_layer(x, scope='trans_2')

        x = self.dense_block(input_x=x, nb_layers=48, layer_name='dense_3')
        x = self.transition_layer(x, scope='trans_3')

        x = self.dense_block(input_x=x, nb_layers=32, layer_name='dense_final') 
        
        x = Batch_Normalization(x, training=self.training, scope='linear_batch')
        x = Relu(x)
        x = Global_Average_Pooling(x)
        x = Linear(x)

        return x

 

What is the "Dense Block" ?

Dense_block

   def dense_block(self, input_x, nb_layers, layer_name):
        with tf.name_scope(layer_name):
            layers_concat = list()
            layers_concat.append(input_x)

            x = self.bottleneck_layer(input_x, scope=layer_name + '_bottleN_' + str(0))

            layers_concat.append(x)

            for i in range(nb_layers - 1):
                x = Concatenation(layers_concat)
                x = self.bottleneck_layer(x, scope=layer_name + '_bottleN_' + str(i + 1))
                layers_concat.append(x)

            return x

 

What is the "Bottleneck Layer" ?

 def bottleneck_layer(self, x, scope):
        with tf.name_scope(scope):
            x = Batch_Normalization(x, training=self.training, scope=scope+'_batch1')
            x = Relu(x)
            x = conv_layer(x, filter=4 * self.filters, kernel=[1,1], layer_name=scope+'_conv1')
            x = Drop_out(x, rate=dropout_rate, training=self.training)

            x = Batch_Normalization(x, training=self.training, scope=scope+'_batch2')
            x = Relu(x)
            x = conv_layer(x, filter=self.filters, kernel=[3,3], layer_name=scope+'_conv2')
            x = Drop_out(x, rate=dropout_rate, training=self.training)
            
            return x

 

What is the "Transition Layer" ?

    def transition_layer(self, x, scope):
        with tf.name_scope(scope):
            x = Batch_Normalization(x, training=self.training, scope=scope+'_batch1')
            x = Relu(x)
            x = conv_layer(x, filter=self.filters, kernel=[1,1], layer_name=scope+'_conv1')
            x = Drop_out(x, rate=dropout_rate, training=self.training)
            x = Average_pooling(x, pool_size=[2,2], stride=2)

            return x

 

Compare Structure (CNN, ResNet, DenseNet)

compare

Results

  • (MNIST) The highest test accuracy is 99.2% (This result does not use dropout)
  • The number of dense block layers is fixed to 4
    for i in range(self.nb_blocks) :
        # original : 6 -> 12 -> 48

        x = self.dense_block(input_x=x, nb_layers=4, layer_name='dense_'+str(i))
        x = self.transition_layer(x, scope='trans_'+str(i))

 

CIFAR-10

cifar_10

CIFAR-100

cifar_100

Image Net

image_net

Related works

References

Author

Junho Kim

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 您好!DenseNet是一种卷积神经网络结构,它通过密集连接(dense connection)来改进传统的卷积神经网络,可以有效地提高模型的准确性和稳定性。 如果您想在TensorFlow中使用DenseNet,可以使用TensorFlow官方提供的tf.keras模块中的DenseNet实现。具体步骤如下: 1.导入相关模块 ``` import tensorflow as tf from tensorflow.keras.applications.densenet import DenseNet121 ``` 2.实例化DenseNet模型 ``` model = DenseNet121(input_shape=(224,224,3), include_top=True, weights=None, classes=1000) ``` 其中,input_shape指定输入张量的形状,include_top表示是否包含顶部的全连接层,weights表示权重的初始化方式,classes表示分类数。 3.编译模型 ``` model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) ``` 4.训练模型 ``` model.fit(x_train, y_train, epochs=10, batch_size=32, validation_data=(x_val, y_val)) ``` 其中,x_train和y_train是训练集的特征和标签,x_val和y_val是验证集的特征和标签,epochs表示训练轮数,batch_size表示批次大小。 以上是使用DenseNet在TensorFlow中的基本操作,您可以根据实际需求进行调整和优化。 ### 回答2: DenseNet是一种深度卷积神经网络模型,它采用密集连接的方式来建立网络层次结构。在传统的卷积神经网络中,信息只能在相邻的层之间传递,缺乏深层次的信息交流。而DenseNet通过将每层的输入与输出进行连接,使得信息能够在不同层之间高效地传递和共享,从而提高网络的训练效率和准确率。 TensorFlow是Google开源的一款机器学习框架,提供了一系列的API接口和开发工具,使得开发者可以方便地实现各种机器学习算法。TensorFlow具有分布式计算和GPU加速的功能,可以处理大量的数据和复杂的算法模型。在实现DenseNet时,TensorFlow提供了丰富的卷积、池化、全连接和激活函数等基本操作,使得可以快速地搭建DenseNet模型并进行训练和测试。 DenseNet采用了一种新的模型结构,使得网络可以通过高效的信息传递来提高训练效率和准确率。在实现DenseNet时,可以使用TensorFlow提供的“Dense”函数来实现密集连接,构建紧凑的网络层次结构,从而避免了深度神经网络训练过程中的梯度消失问题。 总的来说,DenseNet对于深度卷积神经网络的发展起到了重要的推动作用,而TensorFlow的开源机器学习框架为深度学习的研究和应用提供了优秀的基础设施支持。因此,结合使用DenseNet和TensorFlow可以实现一些很有价值的研究和应用。 ### 回答3: DenseNet是图像识别领域的一种深度神经网络模型,能够在高维度特征上达到非常好的效果。其特点是在卷积层中引入了特殊的连接方式,使得每一层的输入不仅仅来自于上一层的输出,还来自于之前所有层的输出,从而增强了模型的有效性。TensorFlow是当前最流行的深度学习框架之一,深受广大研究者和程序员的推崇。 DenseNet与TensorFlow的结合可以为图像识别问题带来巨大的性能提升。在TensorFlow平台上使用DenseNet模型的过程中,需要先构建模型架构并定义计算图的结构,包括输入占位符、网络层结构、损失函数、优化器。建议使用Keras库或其他相关的开源软件包,这些软件包可以大大简化模型的搭建、训练和评估的过程,并且都是基于TensorFlow开发的。 在配置完成模型架构后,可以使用TensorFlow提供的基本操作来进行训练和测试。训练时,需要构造训练用的数据集和标签数据,并使用梯度下降等算法来优化损失函数。测试时,则需要用训练好的模型来预测新的输入数据。可以使用Kaggle等公开竞赛数据集进行测试和比较不同算法的性能表现,以寻找最优的DenseNet模型及其参数设置。 总之,DenseNet在TensorFlow平台上的表现非常优秀,可以帮助人们更好地解决复杂的图像识别问题,具有较好的发展前景。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值