broadinstitute / keras-resnet

Keras package for deep residual networks
Other
300 stars 126 forks source link

expected average_pooling2d_1 to have 4 dimensions #16

Closed pbalapra closed 7 years ago

pbalapra commented 7 years ago

import keras_resnet.models import keras

shape, classes = (32, 32, 3), 10

x = keras.layers.Input(shape)

print(x)

model = keras_resnet.ResNet50(x) model.summary() model.compile("adam", "categorical_crossentropy", ["accuracy"])

(x, y), (, ) = keras.datasets.cifar10.load_data()

y = keras.utils.np_utils.to_categorical(y)

model.fit(x, y) +++++++++++++++++++++++++++++++++ I ran the above test code in Python 2.7.9 and but I got:

ValueError: Error when checking model target: expected average_pooling2d_1 to have 4 dimensions, but got array with shape (50000, 10)

0x00b1 commented 7 years ago

Hi, @pbalapra!

My bad! I updated the API without updating the README. Here is an example of the new API:


>>> import keras

>>> import keras_resnet

>>> shape, classes = (32, 32, 3), 10

>>> x = keras.layers.Input(shape)

>>> y = keras_resnet.ResNet50(x)

>>> y = keras.layers.Flatten()(y.output)

>>> y = keras.layers.Dense(classes)(y)

>>> model = keras.models.Model(x, y)

>>> model.compile("adam", "categorical_crossentropy", ["accuracy"])

>>> (training_x, training_y), (_, _) = keras.datasets.cifar10.load_data()

>>> training_y = keras.utils.np_utils.to_categorical(training_y)

>>> model.fit(training_x, training_y)

Let me know if you need any additional help!

pbalapra commented 7 years ago

@0x00b1 Allen, thanks a lot for fixing. I still have an error:

Tensorflow

Traceback (most recent call last): File "resnet.py", line 13, in y = keras.layers.Dense(classes)(y) File "/Users/pbalapra/.virtualenvs/keras2/lib/python2.7/site-packages/keras/engine/topology.py", line 534, in call self.assert_input_compatibility(inputs) File "/Users/pbalapra/.virtualenvs/keras2/lib/python2.7/site-packages/keras/engine/topology.py", line 443, in assert_input_compatibility ndim = K.ndim(x) File "/Users/pbalapra/.virtualenvs/keras2/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py", line 439, in ndim dims = x.get_shape()._dims AttributeError: 'ResNet50' object has no attribute 'get_shape'

Theano:

Traceback (most recent call last): File "resnet.py", line 13, in y = keras.layers.Dense(classes)(y) File "/Users/pbalapra/.virtualenvs/keras2/lib/python2.7/site-packages/keras/engine/topology.py", line 534, in call self.assert_input_compatibility(inputs) File "/Users/pbalapra/.virtualenvs/keras2/lib/python2.7/site-packages/keras/engine/topology.py", line 443, in assert_input_compatibility ndim = K.ndim(x) File "/Users/pbalapra/.virtualenvs/keras2/lib/python2.7/site-packages/keras/backend/theano_backend.py", line 216, in ndim return x.ndim AttributeError: 'ResNet50' object has no attribute 'ndim'

0x00b1 commented 7 years ago

Hi, @pbalapra! I made a small typo. Here is the updated example:

>>> import keras

>>> import keras_resnet

>>> shape, classes = (32, 32, 3), 10

>>> x = keras.layers.Input(shape)

>>> y = keras_resnet.ResNet50(x)

>>> y = keras.layers.Flatten()(y.output)

>>> y = keras.layers.Dense(classes)(y)

>>> model = keras.models.Model(x, y)

>>> model.compile("adam", "categorical_crossentropy", ["accuracy"])

>>> (training_x, training_y), (_, _) = keras.datasets.cifar10.load_data()

>>> training_y = keras.utils.np_utils.to_categorical(training_y)

>>> model.fit(training_x, training_y)

Let me know!