taki0112 / Densenet-Tensorflow

Simple Tensorflow implementation of Densenet using Cifar10, MNIST
MIT License
506 stars 196 forks source link

Got following errors while running Densenet_MNIST.py ? #1

Closed xiaoshumei closed 7 years ago

xiaoshumei commented 7 years ago

Traceback (most recent call last): File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\common_shapes.py", line 671, in _call_cpp_shape_fn_impl input_tensors_as_shapes, status) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\contextlib.py", line 66, in exit next(self.gen) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\errors_impl.py", line 466, in raise_exception_on_not_ok_status pywrap_tensorflow.TF_GetCode(status)) tensorflow.python.framework.errors_impl.InvalidArgumentError: Negative dimension size caused by subtracting 2 from 1 for 'trans_3/average_pooling2d/AvgPool' (op: 'AvgPool') with input shapes: [?,1,1,12].

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "E:/Densenet/Densenet-Tensorflow-master/MNIST/Densenet_MNIST.py", line 176, in logits = DenseNet(x=batch_images, nb_blocks=nb_block, filters=growth_k, training=training_flag).model File "E:/Densenet/Densenet-Tensorflow-master/MNIST/Densenet_MNIST.py", line 84, in init self.model = self.Dense_net(x) File "E:/Densenet/Densenet-Tensorflow-master/MNIST/Densenet_MNIST.py", line 150, in Dense_net x = self.transition_layer(x, scope='trans_3') File "E:/Densenet/Densenet-Tensorflow-master/MNIST/Densenet_MNIST.py", line 110, in transition_layer x = Average_pooling(x, pool_size=[2,2], stride=2) File "E:/Densenet/Densenet-Tensorflow-master/MNIST/Densenet_MNIST.py", line 65, in Average_pooling return tf.layers.average_pooling2d(inputs=x, pool_size=pool_size, strides=stride, padding=padding) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\layers\pooling.py", line 361, in average_pooling2d return layer.apply(inputs) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\layers\base.py", line 492, in apply return self.call(inputs, *args, *kwargs) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\layers\base.py", line 441, in call outputs = self.call(inputs, args, **kwargs) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\layers\pooling.py", line 276, in call data_format=utils.convert_data_format(self.data_format, 4)) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\nn_ops.py", line 1741, in avg_pool name=name) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\gen_nn_ops.py", line 48, in _avg_pool data_format=data_format, name=name) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 767, in apply_op op_def=op_def) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line 2508, in create_op set_shapes_for_outputs(ret) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line 1873, in set_shapes_for_outputs shapes = shape_func(op) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line 1823, in call_with_requiring return call_cpp_shape_fn(op, require_shape_fn=True) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\common_shapes.py", line 610, in call_cpp_shape_fn debug_python_shape_fn, require_shape_fn) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\common_shapes.py", line 676, in _call_cpp_shape_fn_impl raise ValueError(err.message) ValueError: Negative dimension size caused by subtracting 2 from 1 for 'trans_3/average_pooling2d/AvgPool' (op: 'AvgPool') with input shapes: [?,1,1,12].

taki0112 commented 7 years ago

This is because the image size is reduced each time it passes the layer. At MNIST, I set nb_block = 2 so that the dense block and transition layer pass only two times. (See ReadME)

The image size is halved from the transition layer (because, avg pooling) It is also reduced by half in the first conv layer (7 * 7, stride = 2) (also max pooling too)

To summarize, the image size of MNIST is 28 * 28. So,

28 -> 14(first conv) -> 6(max pool, The reason for not half is because of kernel size.) 
-> 3 (dense +transition) -> 1 (dense+transition) -> error !

Therefore, it is necessary to set the number of dense blocks according to the input image.

Thank you

taki0112 commented 7 years ago

I modified the code so that it can be run on MNIST.