raghakot / keras-resnet

Residual networks implementation using Keras-1.0 functional API
Other
1.39k stars 617 forks source link

Got zeroDivisionError on running default main #15

Closed pszyu closed 7 years ago

pszyu commented 7 years ago

After running default main() function with nothing changed, I got the following error. So wondering whether anyone else got this error as well and how to revise it. self.subsample[1] = 0. Thank you! Using Theano backend.

Traceback (most recent call last): File "/Users/PaulYu/Documents/git/Deep Residual Networks/keras-resnet/resnet.py", line 163, in main() File "/Users/PaulYu/Documents/git/Deep Residual Networks/keras-resnet/resnet.py", line 157, in main model = ResNetBuilder.build_resnet_18((3, 224, 224), 1000) File "/Users/PaulYu/Documents/git/Deep Residual Networks/keras-resnet/resnet.py", line 137, in build_resnet_18 return ResNetBuilder.build(input_shape, num_outputs, basic_block, [2, 2, 2, 2]) File "/Users/PaulYu/Documents/git/Deep Residual Networks/keras-resnet/resnet.py", line 124, in build block = _residual_block(block_fn, nb_filters=nb_filters, repetitions=r, is_first_layer=i == 0)(block) File "/Users/PaulYu/Documents/git/Deep Residual Networks/keras-resnet/resnet.py", line 66, in f input = block_function(nb_filters=nb_filters, init_subsample=init_subsample)(input) File "/Users/PaulYu/Documents/git/Deep Residual Networks/keras-resnet/resnet.py", line 79, in f return _shortcut(input, residual) File "/Users/PaulYu/Documents/git/Deep Residual Networks/keras-resnet/resnet.py", line 54, in _shortcut init="he_normal", border_mode="valid")(input) File "/Library/Python/2.7/site-packages/keras/engine/topology.py", line 514, in call self.add_inbound_node(inbound_layers, node_indices, tensor_indices) File "/Library/Python/2.7/site-packages/keras/engine/topology.py", line 572, in add_inbound_node Node.create_node(self, inbound_layers, node_indices, tensor_indices) File "/Library/Python/2.7/site-packages/keras/engine/topology.py", line 152, in create_node output_shapes = to_list(outbound_layer.get_output_shape_for(input_shapes[0])) File "/Library/Python/2.7/site-packages/keras/layers/convolutional.py", line 453, in get_output_shape_for self.border_mode, self.subsample[1]) File "/Library/Python/2.7/site-packages/keras/utils/np_utils.py", line 131, in conv_output_length return (output_length + stride - 1) // stride ZeroDivisionError: integer division or modulo by zero

Process finished with exit code 1

DanlanChen commented 7 years ago

what is your input size

pszyu commented 7 years ago

Hi Chen, my input size is the original one - (3, 224, 224)

def main(): model = ResNetBuilder.build_resnet_18((3, 224, 224), 1000) model.compile(loss="categorical_crossentropy", optimizer="sgd") model.summary()

Do you have idea of what the problem is? Thank you! @DanlanChen

pszyu commented 7 years ago

I think it is some problem for my Theano or Keras library. Even the following code:

apply a 3x3 convolution with 64 output filters on a 256x256 image:

model = Sequential()
model.add(Convolution2D(64, 3, 3, border_mode='same', input_shape=(3, 256, 256)))

gives model.output_shape as (None, 3, 256, 64), instead of (None, 64, 256, 256)

pszyu commented 7 years ago

I found the cause of my error in case someone else also encounter the same problem. It is about the ordering of the input - somehow my system recognise my input as (3, 256) with 256 channels. The solution is from:

https://stackoverflow.com/questions/40135370/keras-getting-wrong-output-shape/40137162#40137162?newreg=04a6c044f40343c6bd979d1f8609cb09

I tried the third one that "Modify the keras configuration file by changing 'tf' to 'th' in your ~/.keras/keras.json" and it works.