brjathu / deepcaps

Official Implementation of "DeepCaps: Going Deeper with Capsule Networks" paper (CVPR 2019).
MIT License
151 stars 48 forks source link

ValueError: Dimensions must be equal, but are 2560 and 10 for 'digit_caps_2/add' (op: 'AddV2') with input shapes: [?,10,10,2560,32], [10,32]. #14

Closed QuickLearner171998 closed 4 years ago

QuickLearner171998 commented 4 years ago

I am facing this error while training. I have used the following args: class args: numGPU = 1 epochs = 100 batch_size = 8 lr = 0.001 lr_decay = 0.96 lam_recon = 0.4 r = 3 routings = 3 shift_fraction = 0.1 debug = False digit = 5 save_dir = 'model/CIFAR10/13' t = False w = None ep_num = 0 dataset = "CIFAR10"

The error is common to all datasets.

QuickLearner171998 commented 4 years ago

This is the exact error:

InvalidArgumentError: Dimensions must be equal, but are 2560 and 10 for 'digit_caps_2/add' (op: 'AddV2') with input shapes: [?,10,10,2560,32], [10,32].

During handling of the above exception, another exception occurred:

ValueError Traceback (most recent call last) 12 frames

in () 118 119 # model, eval_model = DeepCapsNet(input_shape=x_train.shape[1:], n_class=y_train.shape[1], routings=args.routings) # for 64*64 --> 120 model, eval_model = DeepCapsNet28(input_shape=x_train.shape[1:], n_class=y_train.shape[1], routings=args.routings) #for 28*28 121 122 # plot_model(model, show_shapes=True,to_file=args.save_dir + '/model.png') in DeepCapsNet28(input_shape, n_class, routings) 132 133 # l = Dropout(0.4)(l) --> 134 digits_caps = CapsuleLayer(num_capsule=n_class, dim_capsule=32, routings=routings, channels=0, name='digit_caps')(l) 135 136 l = CapsToScalars(name='capsnet')(digits_caps) /usr/local/lib/python3.6/dist-packages/keras/engine/base_layer.py in __call__(self, inputs, **kwargs) 449 # Actually call the layer, 450 # collecting output(s), mask(s), and shape(s). --> 451 output = self.call(inputs, **kwargs) 452 output_mask = self.compute_mask(inputs, previous_mask) 453 in call(self, inputs, training) 559 # then matmal: [input_num_capsule] x [input_num_capsule, dim_capsule] -> [dim_capsule]. 560 # outputs.shape=[None, num_capsule, dim_capsule] --> 561 outputs = squash(K.batch_dot(c, inputs_hat, [2, 2]) + self.B) # [None, 10, 16] 562 563 if i < self.routings - 1: /usr/local/lib/python3.6/dist-packages/tensorflow_core/python/ops/math_ops.py in binary_op_wrapper(x, y) 910 else: 911 raise --> 912 return func(x, y, name=name) 913 914 def binary_op_wrapper_sparse(sp_x, y): /usr/local/lib/python3.6/dist-packages/tensorflow_core/python/ops/math_ops.py in _add_dispatch(x, y, name) 1195 return gen_math_ops.add(x, y, name=name) 1196 else: -> 1197 return gen_math_ops.add_v2(x, y, name=name) 1198 else: 1199 return gen_math_ops.add(x, y, name=name) /usr/local/lib/python3.6/dist-packages/tensorflow_core/python/ops/gen_math_ops.py in add_v2(x, y, name) 547 # Add nodes to the TensorFlow graph. 548 _, _, _op = _op_def_lib._apply_op_helper( --> 549 "AddV2", x=x, y=y, name=name) 550 _result = _op.outputs[:] 551 _inputs_flat = _op.inputs /usr/local/lib/python3.6/dist-packages/tensorflow_core/python/framework/op_def_library.py in _apply_op_helper(self, op_type_name, name, **keywords) 792 op = g.create_op(op_type_name, inputs, dtypes=None, name=scope, 793 input_types=input_types, attrs=attr_protos, --> 794 op_def=op_def) 795 796 # Conditionally invoke tfdbg v2's op callback(s). /usr/local/lib/python3.6/dist-packages/tensorflow_core/python/util/deprecation.py in new_func(*args, **kwargs) 505 'in a future version' if date is None else ('after %s' % date), 506 instructions) --> 507 return func(*args, **kwargs) 508 509 doc = _add_deprecated_arg_notice_to_docstring( /usr/local/lib/python3.6/dist-packages/tensorflow_core/python/framework/ops.py in create_op(***failed resolving arguments***) 3355 raise TypeError("Input #%d is not a tensor: %s" % (idx, a)) 3356 return self._create_op_internal(op_type, inputs, dtypes, input_types, name, -> 3357 attrs, op_def, compute_device) 3358 3359 def _create_op_internal( /usr/local/lib/python3.6/dist-packages/tensorflow_core/python/framework/ops.py in _create_op_internal(self, op_type, inputs, dtypes, input_types, name, attrs, op_def, compute_device) 3424 input_types=input_types, 3425 original_op=self._default_original_op, -> 3426 op_def=op_def) 3427 self._create_op_helper(ret, compute_device=compute_device) 3428 return ret /usr/local/lib/python3.6/dist-packages/tensorflow_core/python/framework/ops.py in __init__(self, node_def, g, inputs, output_types, control_inputs, input_types, original_op, op_def) 1768 op_def, inputs, node_def.attr) 1769 self._c_op = _create_c_op(self._graph, node_def, grouped_inputs, -> 1770 control_input_ops) 1771 # pylint: enable=protected-access 1772 /usr/local/lib/python3.6/dist-packages/tensorflow_core/python/framework/ops.py in _create_c_op(graph, node_def, inputs, control_inputs) 1608 except errors.InvalidArgumentError as e: 1609 # Convert to ValueError for backwards compatibility. -> 1610 raise ValueError(str(e)) 1611 1612 return c_op ValueError: Dimensions must be equal, but are 2560 and 10 for 'digit_caps_2/add' (op: 'AddV2') with input shapes: [?,10,10,2560,32], [10,32].
brjathu commented 4 years ago

For CIFAR10, you need to uncomment line 122 in train.py and comment line 123 in train.py.

christopher-lw commented 4 years ago

I used the exact train.py file from this repository and ended up in a similar error. I'm using Google Colab with GPU hardware accelerator. The tensorflow version used is 1.15.0.

This is the error message: Traceback (most recent call last): File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/framework/ops.py", line 1607, in _create_c_op c_op = c_api.TF_FinishOperation(op_desc) tensorflow.python.framework.errors_impl.InvalidArgumentError: Dimensions must be equal, but are 640 and 10 for 'digit_caps/add' (op: 'AddV2') with input shapes: [?,10,10,640,32], [10,32].

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "train.py", line 123, in model, eval_model = DeepCapsNet28(input_shape=x_train.shape[1:], n_class=y_train.shape[1], routings=args.routings) #for 28*28 File "/content/deepcaps/deepcaps.py", line 135, in DeepCapsNet28 digits_caps = CapsuleLayer(num_capsule=n_class, dim_capsule=32, routings=routings, channels=0, name='digit_caps')(l) File "/usr/local/lib/python3.6/dist-packages/keras/engine/base_layer.py", line 451, in call output = self.call(inputs, *kwargs) File "/content/deepcaps/capslayers.py", line 563, in call outputs = squash(K.batch_dot(c, inputs_hat, [2, 2]) + self.B) # [None, 10, 16] File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/ops/math_ops.py", line 912, in binary_op_wrapper return func(x, y, name=name) File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/ops/math_ops.py", line 1197, in _add_dispatch return gen_math_ops.add_v2(x, y, name=name) File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/ops/gen_math_ops.py", line 549, in add_v2 "AddV2", x=x, y=y, name=name) File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/framework/op_def_library.py", line 794, in _apply_op_helper op_def=op_def) File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/util/deprecation.py", line 507, in new_func return func(args, **kwargs) File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/framework/ops.py", line 3357, in create_op attrs, op_def, compute_device) File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/framework/ops.py", line 3426, in _create_op_internal op_def=op_def) File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/framework/ops.py", line 1770, in init control_input_ops) File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/framework/ops.py", line 1610, in _create_c_op raise ValueError(str(e)) ValueError: Dimensions must be equal, but are 640 and 10 for 'digit_caps/add' (op: 'AddV2') with input shapes: [?,10,10,640,32], [10,32].

brjathu commented 4 years ago

you need to uncomment line 122 in train.py and comment line 123 in train.py. This is causing the error, DeepCapsNet28 is for MNIST, use DeepCapsNet for CIFAR10.

mrazekv commented 4 years ago

Hi @brjathu I got exactly the same error. I am using DeepCapsNet28 for MNIST dataset (however I tried to change toggle comments on lines 122 and 123 without any impact).

I have tested several environments and no one was working. Can you please help me with this error? I think it may be caused by Keras update.

Thanks in advance!

This is the trackback of the error

WARNING:tensorflow:From $HOME/deepcaps/deepcaps/capslayers.py:346: calling softmax (from tensorflow.python.ops.nn_ops) with dim is deprecated and will be removed in a future version.
Instructions for updating:
dim is deprecated, use axis instead
Traceback (most recent call last):
  File "$HOME/.conda/envs/snowflakes/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1659, in _create_c_op
    c_op = c_api.TF_FinishOperation(op_desc)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Dimensions must be equal, but are 640 and 10 for 'digit_caps/add' (op: 'Add') with input shapes: [?,10,10,640,32], [10,32].

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "train.py", line 123, in <module>
    model, eval_model = DeepCapsNet28(input_shape=x_train.shape[1:], n_class=y_train.shape[1], routings=args.routings)  #for 28*28
  File "$HOME/deepcaps/deepcaps/deepcaps.py", line 135, in DeepCapsNet28
    digits_caps = CapsuleLayer(num_capsule=n_class, dim_capsule=32, routings=routings, channels=0, name='digit_caps')(l)
  File "$HOME/.local/lib/python3.6/site-packages/keras/engine/base_layer.py", line 489, in __call__
    output = self.call(inputs, **kwargs)
  File "$HOME/deepcaps/deepcaps/capslayers.py", line 563, in call
    outputs = squash(K.batch_dot(c, inputs_hat, [2, 2]) + self.B)  # [None, 10, 16]
  File "$HOME/.conda/envs/snowflakes/lib/python3.6/site-packages/tensorflow/python/ops/math_ops.py", line 824, in binary_op_wrapper
    return func(x, y, name=name)
  File "$HOME/.conda/envs/snowflakes/lib/python3.6/site-packages/tensorflow/python/ops/gen_math_ops.py", line 365, in add
    "Add", x=x, y=y, name=name)
  File "$HOME/.conda/envs/snowflakes/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 788, in _apply_op_helper
    op_def=op_def)
  File "$HOME/.conda/envs/snowflakes/lib/python3.6/site-packages/tensorflow/python/util/deprecation.py", line 507, in new_func
    return func(*args, **kwargs)
  File "$HOME/.conda/envs/snowflakes/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3300, in create_op
    op_def=op_def)
  File "$HOME/.conda/envs/snowflakes/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1823, in __init__
    control_input_ops)
  File "$HOME/.conda/envs/snowflakes/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1662, in _create_c_op
    raise ValueError(str(e))
ValueError: Dimensions must be equal, but are 640 and 10 for 'digit_caps/add' (op: 'Add') with input shapes: [?,10,10,640,32], [10,32].

This is the list of installed packages (I am using Anaconda)

$ conda list
# packages in environment at $HOME/.conda/envs/snowflakes:
#
# Name                    Version                   Build  Channel
_tflow_select             2.1.0                       gpu    anaconda
absl-py                   0.9.0                    py36_0    anaconda
astor                     0.8.0                    py36_0    anaconda
blas                      1.0                         mkl    anaconda
c-ares                    1.15.0            h7b6447c_1001    anaconda
ca-certificates           2020.1.1                      0    anaconda
certifi                   2019.11.28               py36_0    anaconda
cudatoolkit               10.0.130                      0    anaconda
cudnn                     7.6.5                cuda10.0_0    anaconda
cupti                     10.0.130                      0    anaconda
freetype                  2.9.1                h8a8886c_1    anaconda
gast                      0.3.3                      py_0    anaconda
grpcio                    1.27.2           py36hf8bcb03_0    anaconda
h5py                      2.10.0           py36h7918eee_0    anaconda
hdf5                      1.10.4               hb1b8bf9_0    anaconda
intel-openmp              2020.0                      166    anaconda
jpeg                      9b                   habf39ab_1    anaconda
keras-applications        1.0.8                      py_0    anaconda
keras-base                2.3.1                    py36_0    anaconda
keras-gpu                 2.1.6                    py36_0    anaconda
keras-preprocessing       1.1.0                      py_1    anaconda
ld_impl_linux-64          2.33.1               h53a641e_7    anaconda
libedit                   3.1.20181209         hc058e9b_0    anaconda
libffi                    3.2.1                h4deb6c0_3    anaconda
libgcc-ng                 9.1.0                hdf63c60_0    anaconda
libgfortran-ng            7.3.0                hdf63c60_0    anaconda
libpng                    1.6.37               hbc83047_0    anaconda
libprotobuf               3.11.4               hd408876_0    anaconda
libstdcxx-ng              9.1.0                hdf63c60_0    anaconda
libtiff                   4.1.0                h2733197_0    anaconda
markdown                  3.1.1                    py36_0    anaconda
mkl                       2019.5                      281    anaconda
mkl-service               2.3.0            py36he904b0f_0    anaconda
mkl_fft                   1.0.15           py36ha843d7b_0    anaconda
mkl_random                1.1.0            py36hd6b4f25_0    anaconda
mock                      4.0.1                      py_0    anaconda
ncurses                   6.2                  he6710b0_0    anaconda
numpy                     1.18.1           py36h4f9e942_0    anaconda
numpy-base                1.18.1           py36hde5b4d6_1    anaconda
olefile                   0.46                       py_0    anaconda
openssl                   1.1.1                h7b6447c_0    anaconda
pillow                    7.0.0            py36hb39fc2d_0    anaconda
pip                       20.0.2                   py36_1    anaconda
protobuf                  3.11.4           py36he6710b0_0    anaconda
python                    3.6.10               h0371630_0    anaconda
pyyaml                    5.3              py36h7b6447c_0    anaconda
readline                  7.0                  h7b6447c_5    anaconda
scipy                     1.4.1            py36h0b6359f_0    anaconda
setuptools                45.2.0                   py36_0    anaconda
six                       1.14.0                   py36_0    anaconda
sqlite                    3.31.1               h7b6447c_0    anaconda
tensorboard               1.13.1           py36hf484d3e_0    anaconda
tensorflow                1.13.1          gpu_py36h3991807_0    anaconda
tensorflow-base           1.13.1          gpu_py36h8d69cac_0    anaconda
tensorflow-estimator      1.13.0                     py_0    anaconda
tensorflow-gpu            1.13.1               h0d30ee6_0    anaconda
termcolor                 1.1.0                    py36_1    anaconda
tk                        8.6.8                hbc83047_0    anaconda
werkzeug                  1.0.0                      py_0    anaconda
wheel                     0.34.2                   py36_0    anaconda
xz                        5.2.4                h14c3975_4    anaconda
yaml                      0.1.7                h96e3832_1    anaconda
zlib                      1.2.11               h7b6447c_3    anaconda
zstd                      1.3.7                h0b5b093_0    anaconda
mrazekv commented 4 years ago

Maybe I have found the problem!

Problem was in keras versions. With following modules

keras-base                2.2.4                    py36_0
keras-gpu                 2.2.4                         0
keras-preprocessing       1.1.0                      py_1    anaconda

and update in capslayers.py

#        self.data_format = K.normalize_data_format(data_format)
        self.data_format = conv_utils.normalize_data_format(data_format)

it seems to be working!

I recommend to update README with specification of keras version (in conda install commands) and this update in capslayers.py. I can create a pull request, if you want ;)

brjathu commented 4 years ago

Hi @mrazekv, First of all, sorry for the late reply, and Thank you for figuring out the issue. It would be great if can create the pull request.