Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Runtime error "shape5_variable needs to be an array" #181

Closed
ashishmagar600 opened this issue Sep 19, 2019 · 15 comments
Closed

Runtime error "shape5_variable needs to be an array" #181

ashishmagar600 opened this issue Sep 19, 2019 · 15 comments

Comments

@ashishmagar600
Copy link

I have successfully installed frugally-deep as shown in INSTALL.md.

I compiled a small CPP application to test installation (same code is tested on another machine running frugally-deep), but while running gave error

Loading json ... done. elapsed time: 0.012854 s terminate called after throwing an instance of 'std::runtime_error' what(): shape5_variable needs to be an array Aborted (core dumped)
This is the complete output of error log.

Also, I couldn't do the last step of INSTALL.md
make unnitest

gives error

Traceback (most recent call last): File "/home/code/DIAMLER_PROJECT/KERAS/frugally-deep/keras_export/generate_test_models.py", line 7, in <module> import keras ImportError: No module named 'keras' test/CMakeFiles/test_model_full_test_data.dir/build.make:64: recipe for target 'test/test_model_full.h5' failed make[3]: *** [test/test_model_full.h5] Error 1 CMakeFiles/Makefile2:577: recipe for target 'test/CMakeFiles/test_model_full_test_data.dir/all' failed make[2]: *** [test/CMakeFiles/test_model_full_test_data.dir/all] Error 2 CMakeFiles/Makefile2:138: recipe for target 'test/CMakeFiles/unittest.dir/rule' failed make[1]: *** [test/CMakeFiles/unittest.dir/rule] Error 2 Makefile:173: recipe for target 'unittest' failed make: *** [unittest] Error 2

For some reason, keras is not getting called from python (I have already installed tensorflow, keras and related packages, I am working on the python side of the error), but this could not have been related to building cpp application (I thought so).\

The same application works a charm on other systems.

@Dobiasd
Copy link
Owner

Dobiasd commented Sep 19, 2019

The same application works a charm on other systems.

Thanks for pointing that out explicitly, because that's an important hint to not look for the error in frugally-deep but in the setup of the machine, you are using.

Maybe it has something to do with virtualenv?

I'd first try to re-install Keras and TensorFlow:

sudo pip3 uninstall Keras
sudo pip3 uninstall TensorFlow
sudo pip3 install Keras
sudo pip3 install TensorFlow

and see how that goes.

Then try to run some minimal Keras examples in Python on that machine, i.e., nothing frugally-deep related, to reduce complexity of this test. Something like that:

import numpy as np
from keras.layers import Input, Dense
from keras.models import Model

inputs = Input(shape=(4,))
x = Dense(5, activation='relu')(inputs)
predictions = Dense(3, activation='softmax')(x)
model = Model(inputs=inputs, outputs=predictions)
model.compile(loss='categorical_crossentropy', optimizer='nadam')

If you have that working, try again with frugally-deep.

Oh, and would using Docker be an option for you? 😁

@ashishmagar600
Copy link
Author

Thank you. My python3 was broken and I had to start from installing back pip3, and then keras and TensorFlow.

Above example works fine, there were lot of warnings, but I printed model.summary() at the end so that we know it actually works for me.

I was now able to make unittest , and the complete process of installing frugally-deep is done.
But I am still getting this error on my application:
Loading json ... done. elapsed time: 0.007154 s terminate called after throwing an instance of 'std::runtime_error' what(): shape5_variable needs to be an array Aborted (core dumped)

I am sorry, but I don't know if Docker would help me much, as I am working on linking frugally-deep with another library which is huge and based on CPP.

kerasTestOutput.txt

@Dobiasd
Copy link
Owner

Dobiasd commented Sep 20, 2019

Could you upload your model (h5 and json) and minimal C++ code to reproduce the problem?

@Dobiasd
Copy link
Owner

Dobiasd commented Sep 20, 2019

Mhh, it might be that it does not help that much, because likely my machine is one them those, where the application just works.

Could you try to

  • make sure you are using the latest version of frugally-deep
  • re-convert your model from h5 to json
  • re-compile your C++ code

Maybe this helps.

@ashishmagar600
Copy link
Author

because likely my machine is one them those, where the application just works.

Yes, I think because on my other machine everything is fine.

I tried all of your suggestions, but nothing seems to work. I realized I was using an older version of g++(5), and upgraded it to g++-7, thought this might be the issue but it is not.

However, while converting my model from h5 to json there were some additional warnings, not seen of my other machine:
/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/dtypes.py:516: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'. _np_qint8 = np.dtype([("qint8", np.int8, 1)])

This could be a hint of what is wrong here on my system.

The whole log file for converting model
warnings.txt

@Dobiasd
Copy link
Owner

Dobiasd commented Sep 20, 2019

Could you upload the following?

  • the h5 file
  • the json file resulting when you convert the model on the problem machine
  • the json file resulting when you convert the model on a non-problematic machine

@ashishmagar600
Copy link
Author

ashishmagar600 commented Sep 20, 2019

The files are here:

Certainly, by comparing both file there is a difference in converting from h5 to json.

The warning:
/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/dtypes.py:516: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'. _np_qint8 = np.dtype([("qint8", np.int8, 1)])

came as tf is having some issues with higher versions of numpy (1.17). They have discussed this issue here and added a solution here .
The warnings are gone now by using downgraded versions but the problem still persists.

Edit:
The differences reduce if I use version keras==2.2.4, which is same on the other machine.

@Dobiasd
Copy link
Owner

Dobiasd commented Sep 20, 2019

diff model_noProblem.json model_problem.json 
44c44
<                 "seed": 1,
---
>                 "seed": 3,
107c107
<                 "seed": 1,
---
>                 "seed": 3,
112a113
>             "dtype": "float32",
169c170
<                 "seed": 1,
---
>                 "seed": 3,
174a176
>             "dtype": "float32",
231a234
>             "dtype": "float32",
274a278
>             "dtype": "float32",
313c317
<     "keras_version": "2.2.4"
---
>     "keras_version": "2.3.0"
  • model_noProblem.json has "keras_version": "2.2.4"
  • model_problem.json has "keras_version": "2.3.0"

But I just tested here in C++. I get the shape5_variable needs to be an array with both model_problem.json and model_problem.json.

Then I tried to convert my_model.h5 to json again with the convert_model.py from the recent frugally-deep version. It does not event convert:

AttributeError: Layer lstm_1 has multiple inbound nodes, hence the notion of "layer input" is ill-defined. Use `get_input_at(node_index)` instead

The convert_model.py you use (and uploaded with the model files) is old.

Could you create a minimal Python code, that builds this model architecture?

@ashishmagar600
Copy link
Author

The machine which works fine has frugally-deep and other packages installed on June 21. The one which I am working now has an installation of today's version of frugally-deep.

Could you create a minimal Python code, that builds this model architecture?

The model is generated by another team working on it, I have the generated model only. I will ask them if they could provide the code to be shared.

The model is perfectly fine and working on the other machine. I am sure of that.

Maybe I should be using the older version of frugally-deep?

@ashishmagar600
Copy link
Author

ashishmagar600 commented Sep 20, 2019

Alrighty.. I had the older version of frugally-deep and other packages. I have just built and tested the same code and model for both machines and they both work the same now.

Here is the code for generating model:

@Dobiasd
Copy link
Owner

Dobiasd commented Sep 20, 2019

Maybe I should be using the older version of frugally-deep?

That might be a short-term workaround. However, you will be doomed to use the old version forever and will not be able to benefit from possible advances in later versions.

I would very much prefer to find out why the latest version can not handle the model.

I am trying to get the code for generating this model arch. Will be back soon.

Thank would be great. Thanks. 🙂

@Dobiasd
Copy link
Owner

Dobiasd commented Sep 20, 2019

Thanks for sending over the code, which creates the model architecture.

As discussed, I'll not publish it here. But I was able to reduce it to a minimal example, that reproduces the problem fully but does not disclose secrets.

So, the following model

from keras.layers import LSTM
from keras.models import Sequential

model = Sequential()
model.add(LSTM(units=10, activation="relu", return_sequences=True, input_shape=(1, 10)))
model.compile(loss='categorical_crossentropy', optimizer='nadam')
model.save('fdeep_issue_181.h5')

Also results in

AttributeError: Layer lstm_1 has multiple inbound nodes, hence the notion of "layer input" is ill-defined. Use `get_input_at(node_index)` instead.

when trying to convert it with the latest frugally-deep version.

I'll check if I can fix this and get back to you here.

@Dobiasd
Copy link
Owner

Dobiasd commented Sep 20, 2019

OK, one way to make it work, is to express the same architecture not with the sequential interface of Keras, but with the functional API instead.

from keras.layers import Input, LSTM
from keras.models import Model

inputs = Input(shape=(1, 10))
outputs = LSTM(units=10, activation="relu", return_sequences=True)(inputs)
model = Model(inputs=inputs, outputs=outputs)
model.compile(loss='categorical_crossentropy', optimizer='nadam')
model.save('fdeep_issue_181_functional.h5')

So if you need a quick workaround, you can just do this and should be fine.

Nevertheless, I'll check why the sequential version behaves differently.

@Dobiasd
Copy link
Owner

Dobiasd commented Sep 21, 2019

The problem was in the conversion from sequential models to functional ones, that frugally-deep does internally.
I just fixed it, and published a new releaes.
With this, your model works, so I'd recommend using it.

Thanks for reporting the issue and helping debug it with your example.
Things like sometimes can be tedious, but they really help to make the library better. 🙂

@Dobiasd Dobiasd closed this as completed Sep 21, 2019
@ashishmagar600
Copy link
Author

Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants