mathDR / reading-text-in-the-wild

A Keras/Theano implementation of "Reading Text in the Wild with Convolutional Neural Networks" by M Jaderberg et.al.
GNU General Public License v3.0
116 stars 30 forks source link

AttributeError: 'module' object has no attribute 'custom_spatial_2d_padding' #14

Open fgarcd03 opened 7 years ago

fgarcd03 commented 7 years ago

Hi, I'm getting this error when running 'make_keras_charnet_model.py'. I have the same version of Keras and Theano, and i updated the Keras as it's said in the readme:

runfile('/home/fer/Documentos/reading-text-in-the-wild-master/DICT2/extract_dictnet_weights.py',` wdir='/home/fer/Documentos/reading-text-in-the-wild-master/DICT2')

runfile('/home/fer/Documentos/reading-text-in-the-wild-master/CHAR2/extract_charnet_weights.py', wdir='/home/fer/Documentos/reading-text-in-the-wild-master/CHAR2')

runfile('/home/fer/Documentos/reading-text-in-the-wild-master/CHAR2/make_keras_charnet_model.py', wdir='/home/fer/Documentos/reading-text-in-the-wild-master/CHAR2')
Using TensorFlow backend.
Traceback (most recent call last):

  File "<ipython-input-3-eb1114016b7c>", line 1, in <module>
    runfile('/home/fer/Documentos/reading-text-in-the-wild-master/CHAR2/make_keras_charnet_model.py', wdir='/home/fer/Documentos/reading-text-in-the-wild-master/CHAR2')

  File "/home/fer/anaconda2/lib/python2.7/site-packages/spyder/utils/site/sitecustomize.py", line 880, in runfile
    execfile(filename, namespace)

  File "/home/fer/anaconda2/lib/python2.7/site-packages/spyder/utils/site/sitecustomize.py", line 94, in execfile
    builtins.execfile(filename, *where)

  File "/home/fer/Documentos/reading-text-in-the-wild-master/CHAR2/make_keras_charnet_model.py", line 92, in <module>
    model.compile(loss='categorical_crossentropy', optimizer=sgd)

  File "/home/fer/anaconda2/lib/python2.7/site-packages/keras/models.py", line 507, in compile
    self.y_train = self.get_output(train=True)

  File "/home/fer/anaconda2/lib/python2.7/site-packages/keras/layers/containers.py", line 130, in get_output
    return self.layers[-1].get_output(train)

  File "/home/fer/anaconda2/lib/python2.7/site-packages/keras/layers/core.py", line 1030, in get_output
    X = self.get_input(train)

  File "/home/fer/anaconda2/lib/python2.7/site-packages/keras/layers/core.py", line 241, in get_input
    previous_output = self.previous.get_output(train=train)

  File "/home/fer/anaconda2/lib/python2.7/site-packages/keras/layers/core.py", line 703, in get_output
    X = self.get_input(train)

  File "/home/fer/anaconda2/lib/python2.7/site-packages/keras/layers/core.py", line 241, in get_input
    previous_output = self.previous.get_output(train=train)

  File "/home/fer/anaconda2/lib/python2.7/site-packages/keras/layers/core.py", line 1030, in get_output
    X = self.get_input(train)

  File "/home/fer/anaconda2/lib/python2.7/site-packages/keras/layers/core.py", line 241, in get_input
    previous_output = self.previous.get_output(train=train)

  File "/home/fer/anaconda2/lib/python2.7/site-packages/keras/layers/core.py", line 703, in get_output
    X = self.get_input(train)

  File "/home/fer/anaconda2/lib/python2.7/site-packages/keras/layers/core.py", line 241, in get_input
    previous_output = self.previous.get_output(train=train)

  File "/home/fer/anaconda2/lib/python2.7/site-packages/keras/layers/core.py", line 1030, in get_output
    X = self.get_input(train)

  File "/home/fer/anaconda2/lib/python2.7/site-packages/keras/layers/core.py", line 241, in get_input
    previous_output = self.previous.get_output(train=train)

  File "/home/fer/anaconda2/lib/python2.7/site-packages/keras/layers/core.py", line 900, in get_output
    X = self.get_input(train)

  File "/home/fer/anaconda2/lib/python2.7/site-packages/keras/layers/core.py", line 241, in get_input
    previous_output = self.previous.get_output(train=train)

  File "/home/fer/anaconda2/lib/python2.7/site-packages/keras/layers/convolutional.py", line 310, in get_output
    X = self.get_input(train)

  File "/home/fer/anaconda2/lib/python2.7/site-packages/keras/layers/core.py", line 241, in get_input
    previous_output = self.previous.get_output(train=train)

  File "/home/fer/anaconda2/lib/python2.7/site-packages/keras/layers/convolutional.py", line 683, in get_output
    X = self.get_input(train)

  File "/home/fer/anaconda2/lib/python2.7/site-packages/keras/layers/core.py", line 241, in get_input
    previous_output = self.previous.get_output(train=train)

File "/home/fer/anaconda2/lib/python2.7/site-packages/keras/layers/convolutional.py", line 1225, in get_output
    return K.custom_spatial_2d_padding(X, padding=self.padding,

AttributeError: 'module' object has no attribute 'custom_spatial_2d_padding'
mathDR commented 7 years ago

So this is not finding the custom_spatial_2d_padding function that should have been added to Keras.

Can you confirm that running

import keras
from keras import custom_spatial_2d_padding

produces no errors?

fgarcd03 commented 7 years ago

It's giving me an error:

>>> import keras
>>> from keras import custom_spatial_2d_padding
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name custom_spatial_2d_padding

Moreover, i forgoted to say that I'm using spyder to run the files althougt it gives me the same error as when I'm running it in the terminal

mathDR commented 7 years ago

So when you import keras, it is not finding the new CustomZeroPadding2D() class you added to keras/layers/convolutional.py nor the custom_spatial_2d_padding() function you added to keras/backend/theano_backend.py

Did you recompile keras after adding the new functionality?

fgarcd03 commented 7 years ago

No, but I did it and I'm getting the same error

leifsyliongka commented 7 years ago

@mathDR @fgarcd03 I'm also getting the same error. Were you able to resolve it? I have Keras=0.3.3 and Theano=0.8.1

mathDR commented 7 years ago

@leifsyliongka Can you state what you did when augmenting Keras 0.3.3?

leifsyliongka commented 7 years ago

@mathDR I'm using the virtualenv setup in Ubuntu 16.04 for Tensorflow v1.0.1, and then I used pip to install Keras and Theano using the ff. command: pip install keras==0.3.3 theano==0.8.1

After which, I copied the modified Keras convolution.py and theano_backend.py to keras/layers/ and keras/backend/, respectively.

mathDR commented 7 years ago

@leifsyliongka Okay, pip installing the right keras then copying the files is not enough. After copying the modified files, you need to cd to your keras directory and reinstall. To do so, try

pip install -e .

while inside the keras directory.

What is happening is that the keras egg files you installed only have the unmodified versions.

leifsyliongka commented 7 years ago

The pip version doesn't have the setup.py in its directory so I can't do the the: pip install -e . command. So I uninstalled it, and downloaded Keras from https://testpypi.python.org/pypi/Keras/0.3.3. Then I overwritten the tweak files and did the install command from there.

I also deleted the *.pyc files of convolution.py and theano_backend.py to force it to recompile. What I found out though is that the theano_backend.py didn't compile. I also tried importing the non-customize functions and Keras wasn't able to find it too. I think this is causing the problem.

from keras import squeeze
from keras import spatial_2d_padding
leifsyliongka commented 7 years ago

Ok, I've managed to solve the issue. As it turns out, Keras, by default, uses Tensorflow as its backend. You'll have to force it to use Theano. I followed the instructions in https://keras.io/backend/ on how to do so and modified my $HOME/.keras/keras.json to:

{
    "image_dim_ordering": "th",
    "epsilon": 1e-07,
    "floatx": "float32",
    "image_data_format": "channels_last",
    "backend": "theano"
}

Then I added from keras import backend as K in the make_keras_charnet_model.py file. After that I was able to run python make_keras_charnet_model.py and produce the char2_architecture.json file.

mathDR commented 7 years ago

@leifsyliongka Great catch! I forgot that Keras switched to TF as default. I will update the README