pudae / tensorflow-densenet

Tensorflow-DenseNet with ImageNet Pretrained Models
Apache License 2.0
169 stars 59 forks source link

densenet-121 pre-trained model feature map size does not match #28

Open lizhenstat opened 4 years ago

lizhenstat commented 4 years ago

Hi, thanks for your work. I found the DenseNet-121 on ImageNet's feature map size in each block is [56,28,14,7], however in the pre-trained model is [55,27,13,6]

densenet121/conv1/convolution                                          [-1, 112, 112, 64]
densenet121/dense_block1/conv_block1/x1/Conv/convolution               [-1, 55, 55, 128]
densenet121/dense_block1/conv_block1/x2/Conv/convolution               [-1, 55, 55, 32]
densenet121/dense_block1/conv_block2/x1/Conv/convolution               [-1, 55, 55, 128]
densenet121/dense_block1/conv_block2/x2/Conv/convolution               [-1, 55, 55, 32]
densenet121/dense_block1/conv_block3/x1/Conv/convolution               [-1, 55, 55, 128]
densenet121/dense_block1/conv_block3/x2/Conv/convolution               [-1, 55, 55, 32]
densenet121/dense_block1/conv_block4/x1/Conv/convolution               [-1, 55, 55, 128]
densenet121/dense_block1/conv_block4/x2/Conv/convolution               [-1, 55, 55, 32]
densenet121/dense_block1/conv_block5/x1/Conv/convolution               [-1, 55, 55, 128]
densenet121/dense_block1/conv_block5/x2/Conv/convolution               [-1, 55, 55, 32]
densenet121/dense_block1/conv_block6/x1/Conv/convolution               [-1, 55, 55, 128]
densenet121/dense_block1/conv_block6/x2/Conv/convolution               [-1, 55, 55, 32]
densenet121/transition_block1/blk/Conv/convolution                     [-1, 55, 55, 128]
densenet121/transition_block1/AvgPool2D/AvgPool                        [-1, 27, 27, 128]
densenet121/dense_block2/conv_block1/x1/Conv/convolution               [-1, 27, 27, 128]
densenet121/dense_block2/conv_block1/x2/Conv/convolution               [-1, 27, 27, 32]
densenet121/dense_block2/conv_block2/x1/Conv/convolution               [-1, 27, 27, 128]
densenet121/dense_block2/conv_block2/x2/Conv/convolution               [-1, 27, 27, 32]
densenet121/dense_block2/conv_block3/x1/Conv/convolution               [-1, 27, 27, 128]
densenet121/dense_block2/conv_block3/x2/Conv/convolution               [-1, 27, 27, 32]
densenet121/dense_block2/conv_block4/x1/Conv/convolution               [-1, 27, 27, 128]
densenet121/dense_block2/conv_block4/x2/Conv/convolution               [-1, 27, 27, 32]
densenet121/dense_block2/conv_block5/x1/Conv/convolution               [-1, 27, 27, 128]
densenet121/dense_block2/conv_block5/x2/Conv/convolution               [-1, 27, 27, 32]
densenet121/dense_block2/conv_block6/x1/Conv/convolution               [-1, 27, 27, 128]
densenet121/dense_block2/conv_block6/x2/Conv/convolution               [-1, 27, 27, 32]
densenet121/dense_block2/conv_block7/x1/Conv/convolution               [-1, 27, 27, 128]
densenet121/dense_block2/conv_block7/x2/Conv/convolution               [-1, 27, 27, 32]
densenet121/dense_block2/conv_block8/x1/Conv/convolution               [-1, 27, 27, 128]
densenet121/dense_block2/conv_block8/x2/Conv/convolution               [-1, 27, 27, 32]
densenet121/dense_block2/conv_block9/x1/Conv/convolution               [-1, 27, 27, 128]
densenet121/dense_block2/conv_block9/x2/Conv/convolution               [-1, 27, 27, 32]
densenet121/dense_block2/conv_block10/x1/Conv/convolution              [-1, 27, 27, 128]
densenet121/dense_block2/conv_block10/x2/Conv/convolution              [-1, 27, 27, 32]
densenet121/dense_block2/conv_block11/x1/Conv/convolution              [-1, 27, 27, 128]
densenet121/dense_block2/conv_block11/x2/Conv/convolution              [-1, 27, 27, 32]
densenet121/dense_block2/conv_block12/x1/Conv/convolution              [-1, 27, 27, 128]
densenet121/dense_block2/conv_block12/x2/Conv/convolution              [-1, 27, 27, 32]
densenet121/transition_block2/blk/Conv/convolution                     [-1, 27, 27, 256]
densenet121/transition_block2/AvgPool2D/AvgPool                        [-1, 13, 13, 256]
densenet121/dense_block3/conv_block1/x1/Conv/convolution               [-1, 13, 13, 128]
densenet121/dense_block3/conv_block1/x2/Conv/convolution               [-1, 13, 13, 32]
densenet121/dense_block3/conv_block2/x1/Conv/convolution               [-1, 13, 13, 128]
densenet121/dense_block3/conv_block2/x2/Conv/convolution               [-1, 13, 13, 32]
densenet121/dense_block3/conv_block3/x1/Conv/convolution               [-1, 13, 13, 128]
densenet121/dense_block3/conv_block3/x2/Conv/convolution               [-1, 13, 13, 32]
densenet121/dense_block3/conv_block4/x1/Conv/convolution               [-1, 13, 13, 128]
densenet121/dense_block3/conv_block4/x2/Conv/convolution               [-1, 13, 13, 32]
densenet121/dense_block3/conv_block5/x1/Conv/convolution               [-1, 13, 13, 128]
densenet121/dense_block3/conv_block5/x2/Conv/convolution               [-1, 13, 13, 32]
densenet121/dense_block3/conv_block6/x1/Conv/convolution               [-1, 13, 13, 128]
densenet121/dense_block3/conv_block6/x2/Conv/convolution               [-1, 13, 13, 32]
densenet121/dense_block3/conv_block7/x1/Conv/convolution               [-1, 13, 13, 128]
densenet121/dense_block3/conv_block7/x2/Conv/convolution               [-1, 13, 13, 32]
densenet121/dense_block3/conv_block8/x1/Conv/convolution               [-1, 13, 13, 128]
densenet121/dense_block3/conv_block8/x2/Conv/convolution               [-1, 13, 13, 32]
densenet121/dense_block3/conv_block9/x1/Conv/convolution               [-1, 13, 13, 128]
densenet121/dense_block3/conv_block9/x2/Conv/convolution               [-1, 13, 13, 32]
densenet121/dense_block3/conv_block10/x1/Conv/convolution              [-1, 13, 13, 128]
densenet121/dense_block3/conv_block10/x2/Conv/convolution              [-1, 13, 13, 32]
densenet121/dense_block3/conv_block11/x1/Conv/convolution              [-1, 13, 13, 128]
densenet121/dense_block3/conv_block11/x2/Conv/convolution              [-1, 13, 13, 32]
densenet121/dense_block3/conv_block12/x1/Conv/convolution              [-1, 13, 13, 128]
densenet121/dense_block3/conv_block12/x2/Conv/convolution              [-1, 13, 13, 32]
densenet121/dense_block3/conv_block13/x1/Conv/convolution              [-1, 13, 13, 128]
densenet121/dense_block3/conv_block13/x2/Conv/convolution              [-1, 13, 13, 32]
densenet121/dense_block3/conv_block14/x1/Conv/convolution              [-1, 13, 13, 128]
densenet121/dense_block3/conv_block14/x2/Conv/convolution              [-1, 13, 13, 32]
densenet121/dense_block3/conv_block15/x1/Conv/convolution              [-1, 13, 13, 128]
densenet121/dense_block3/conv_block15/x2/Conv/convolution              [-1, 13, 13, 32]
densenet121/dense_block3/conv_block16/x1/Conv/convolution              [-1, 13, 13, 128]
densenet121/dense_block3/conv_block16/x2/Conv/convolution              [-1, 13, 13, 32]
densenet121/dense_block3/conv_block17/x1/Conv/convolution              [-1, 13, 13, 128]
densenet121/dense_block3/conv_block17/x2/Conv/convolution              [-1, 13, 13, 32]
densenet121/dense_block3/conv_block18/x1/Conv/convolution              [-1, 13, 13, 128]
densenet121/dense_block3/conv_block18/x2/Conv/convolution              [-1, 13, 13, 32]
densenet121/dense_block3/conv_block19/x1/Conv/convolution              [-1, 13, 13, 128]
densenet121/dense_block3/conv_block19/x2/Conv/convolution              [-1, 13, 13, 32]
densenet121/dense_block3/conv_block20/x1/Conv/convolution              [-1, 13, 13, 128]
densenet121/dense_block3/conv_block20/x2/Conv/convolution              [-1, 13, 13, 32]
densenet121/dense_block3/conv_block21/x1/Conv/convolution              [-1, 13, 13, 128]
densenet121/dense_block3/conv_block21/x2/Conv/convolution              [-1, 13, 13, 32]
densenet121/dense_block3/conv_block22/x1/Conv/convolution              [-1, 13, 13, 128]
densenet121/dense_block3/conv_block22/x2/Conv/convolution              [-1, 13, 13, 32]
densenet121/dense_block3/conv_block23/x1/Conv/convolution              [-1, 13, 13, 128]
densenet121/dense_block3/conv_block23/x2/Conv/convolution              [-1, 13, 13, 32]
densenet121/dense_block3/conv_block24/x1/Conv/convolution              [-1, 13, 13, 128]
densenet121/dense_block3/conv_block24/x2/Conv/convolution              [-1, 13, 13, 32]
densenet121/transition_block3/blk/Conv/convolution                     [-1, 13, 13, 512]
densenet121/transition_block3/AvgPool2D/AvgPool                        [-1, 6, 6, 512]
densenet121/dense_block4/conv_block1/x1/Conv/convolution               [-1, 6, 6, 128]
densenet121/dense_block4/conv_block1/x2/Conv/convolution               [-1, 6, 6, 32]
densenet121/dense_block4/conv_block2/x1/Conv/convolution               [-1, 6, 6, 128]
densenet121/dense_block4/conv_block2/x2/Conv/convolution               [-1, 6, 6, 32]
densenet121/dense_block4/conv_block3/x1/Conv/convolution               [-1, 6, 6, 128]
densenet121/dense_block4/conv_block3/x2/Conv/convolution               [-1, 6, 6, 32]
densenet121/dense_block4/conv_block4/x1/Conv/convolution               [-1, 6, 6, 128]
densenet121/dense_block4/conv_block4/x2/Conv/convolution               [-1, 6, 6, 32]
densenet121/dense_block4/conv_block5/x1/Conv/convolution               [-1, 6, 6, 128]
densenet121/dense_block4/conv_block5/x2/Conv/convolution               [-1, 6, 6, 32]
densenet121/dense_block4/conv_block6/x1/Conv/convolution               [-1, 6, 6, 128]
densenet121/dense_block4/conv_block6/x2/Conv/convolution               [-1, 6, 6, 32]
densenet121/dense_block4/conv_block7/x1/Conv/convolution               [-1, 6, 6, 128]
densenet121/dense_block4/conv_block7/x2/Conv/convolution               [-1, 6, 6, 32]
densenet121/dense_block4/conv_block8/x1/Conv/convolution               [-1, 6, 6, 128]
densenet121/dense_block4/conv_block8/x2/Conv/convolution               [-1, 6, 6, 32]
densenet121/dense_block4/conv_block9/x1/Conv/convolution               [-1, 6, 6, 128]
densenet121/dense_block4/conv_block9/x2/Conv/convolution               [-1, 6, 6, 32]
densenet121/dense_block4/conv_block10/x1/Conv/convolution              [-1, 6, 6, 128]
densenet121/dense_block4/conv_block10/x2/Conv/convolution              [-1, 6, 6, 32]
densenet121/dense_block4/conv_block11/x1/Conv/convolution              [-1, 6, 6, 128]
densenet121/dense_block4/conv_block11/x2/Conv/convolution              [-1, 6, 6, 32]
densenet121/dense_block4/conv_block12/x1/Conv/convolution              [-1, 6, 6, 128]
densenet121/dense_block4/conv_block12/x2/Conv/convolution              [-1, 6, 6, 32]
densenet121/dense_block4/conv_block13/x1/Conv/convolution              [-1, 6, 6, 128]
densenet121/dense_block4/conv_block13/x2/Conv/convolution              [-1, 6, 6, 32]
densenet121/dense_block4/conv_block14/x1/Conv/convolution              [-1, 6, 6, 128]
densenet121/dense_block4/conv_block14/x2/Conv/convolution              [-1, 6, 6, 32]
densenet121/dense_block4/conv_block15/x1/Conv/convolution              [-1, 6, 6, 128]
densenet121/dense_block4/conv_block15/x2/Conv/convolution              [-1, 6, 6, 32]
densenet121/dense_block4/conv_block16/x1/Conv/convolution              [-1, 6, 6, 128]
densenet121/dense_block4/conv_block16/x2/Conv/convolution              [-1, 6, 6, 32]

do we need to change the this line to the following in densenet.py

net = slim.conv2d(net, num_filters, 7, stride=2, scope='conv1')

to

net = slim.conv2d(net, num_filters, 7, stride=2, scope='conv1',padding="VALID")

Looking forward to your reply

lizhenstat commented 4 years ago

@pudae Hi, any suggestion would be appreciated

pudae commented 4 years ago

Hi @lizhenstat,

Do you mean the feature map sizes of this implementation are [55,27,13,6]? right? If so, the difference is caused by net = slim.max_pool2d(net, 3, stride=2, padding='SAME'), not first convolution. But it's strange because following the padding algorithm, 'SAME' padding is right. (from https://www.tensorflow.org/api_docs/python/tf/nn/convolution)

if data_format starts with "NC", where output_spatial_shape depends on the value of padding. If padding == "SAME": output_spatial_shape[i] = ceil(input_spatial_shape[i] / strides[i]) If padding == "VALID": output_spatial_shape[i] = ceil((input_spatial_shape[i] - (spatial_filter_shape[i]-1) * dilation_rate[i]) / strides[i]).

Here's a test code using tf 2.0. (I'm sorry that I don't have an environment to test my code.)

import tensorflow as tf

input_value = tf.constant(0, shape=(1,224,224,3), dtype=tf.float32)
filter_value = tf.constant(0, shape=(7,7,3,64), dtype=tf.float32)

out_conv = tf.nn.conv2d(input_value, filter_value, strides=2, padding='SAME')
out_maxpool = tf.nn.max_pool_2d(out_conv, ksize=3, strides=2, padding='SAME')

print('input_value:', input_value.shape)
print('out_conv:', out_conv.shape)
print('out_maxpool:', out_maxpool.shape)

The output is

input_value: (1, 224, 224, 3)
out_conv: (1, 112, 112, 64)
out_max_pool: (1, 56, 56, 64)
lizhenstat commented 4 years ago

@pudae Yes, the implementation of this code is [55,27,13,6]. Thanks for your reply, since I want to do some visualization based on your ImageNet pre-trained model. The nets/densenet.py is the code for training the densenet-121 on ImageNet right? (The script is using net = slim.max_pool2d(net, 3, stride=2, padding='VALID') therefore causing this [55,27,13,6] output feature map size)

I got confused since in the original denseNet paper, the feature map size after each block is [112,56,28,14,7] DenseNet-ImageNet architecture After this net = slim.max_pool2d(net, 3, stride=2, padding='SAME')code, the output size match the original paper.

Yes, as you said, I think the "SAME" padding is more reasonable.

Besides, is it okay to cite your pre-trained model based on your Github repo? As I posted in this issue https://github.com/pudae/tensorflow-densenet/issues/27 Thanks a lot