raghakot / keras-resnet

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

A very beginner question #13

Closed pszyu closed 7 years ago

pszyu commented 8 years ago

I have a two questions to consult.

  1. I am trying to find code to do experiments on residual deep networks. I am wondering whether this code is used to train a residual deep network or is used only for creating model visualisation? If I can use it to train a model, how should I input my training data?

  2. I used both Theano and TensorFlow as backend of Keras to run the original code, but it gives me same error (using Theano as an example): Using Theano backend. Traceback (most recent call last): File "resnet.py", line 147, in main() File "resnet.py", line 127, in main model = resnet() File "resnet.py", line 110, in resnet block1 = _residual_block(block_fn, nb_filters=64, repetations=3, is_first_layer=True)(pool1) File "resnet.py", line 94, in f input = block_function(nb_filters=nb_filters, init_subsample=init_subsample)(input) File "resnet.py", line 51, in f return _shortcut(input, residual) File "resnet.py", line 84, in _shortcut return merge([shortcut, residual], mode="sum") File "/Library/Python/2.7/site-packages/keras/engine/topology.py", line 1528, in merge name=name) File "/Library/Python/2.7/site-packages/keras/engine/topology.py", line 1186, in init node_indices, tensor_indices) File "/Library/Python/2.7/site-packages/keras/engine/topology.py", line 1221, in _arguments_validation 'Layer shapes: %s' % input_shapes) Exception: Only layers of same output shape can be merged using sum mode. Layer shapes: [(None, 1, 56, 64), (None, 1, 56, 256)]

It seems that the code already tried to match the input size, but the error is still there. I am wondering whether anyone has also met this problem. I used Python 2.7, Keras 1.1.1, tensorflow-0.11.0, and Theano 0.8.2.

Thank you!

paul-o-alto commented 7 years ago

I, too, am getting this error. Still trying to work around it.

raghakot commented 7 years ago
  1. It's simply a code block to compose resnets using shortcuts and repeatable blocks easily. You would need to train it yourself. If you see: https://github.com/raghakot/keras-resnet/blob/master/resnet.py#L103, the input is an RGB image (3 channels) of size 224, 224. You can easily change this yourself as needed, but might need to adjust layer depth etc.

looking into 2.

raghakot commented 7 years ago

Did you change input size by any chance? I am not able to repro this issue.

paul-o-alto commented 7 years ago

For me the shape of my input is 320 x 160 (3 channels).

Same stack trace, but with:

Exception: Only layers of same output shape can be merged using sum mode. Layer shapes: [(None, 1, 80, 64), (None, 1, 80, 256)]

raghakot commented 7 years ago

The last avg pooling layer needs to be 10, 5 instead of 7, 7. By the time input reaches block 4, it has volume 2048 * 10 * 5

raghakot commented 7 years ago

I updated the code to provide a factory class to assist building resnets. Now you can simply do: model = ResNetBuilder.build_resnet_50((3, 320, 160), 1000)