textgenrnn
textgenrnn copied to clipboard
You need to first `import keras` in order to use `keras_preprocessing`. For instance, you can do:
can't run demo, getting the following error:
ImportError: You need to first import keras
in order to use keras_preprocessing
. For instance, you can do:
import keras
from keras_preprocessing import image
Or, preferably, this equivalent formulation:
from keras import preprocessing
same here. running this via the tweet package and got this traceback:
python3 tweet_generator.py
Traceback (most recent call last):
File "tweet_generator.py", line 6, in <module>
from textgenrnn import textgenrnn
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/textgenrnn/__init__.py", line 1, in <module>
from .textgenrnn import textgenrnn
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/textgenrnn/textgenrnn.py", line 1, in <module>
from tensorflow.keras.callbacks import LearningRateScheduler, Callback
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/keras/__init__.py", line 19, in <module>
from . import datasets
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/keras/datasets/__init__.py", line 14, in <module>
from . import imdb
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/keras/datasets/imdb/__init__.py", line 11, in <module>
from tensorflow.python.keras.datasets.imdb import get_word_index
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/keras/datasets/imdb.py", line 25, in <module>
from tensorflow.python.keras.preprocessing.sequence import _remove_long_seq
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/keras/preprocessing/__init__.py", line 26, in <module>
from tensorflow.python.keras.preprocessing import image
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/keras/preprocessing/image.py", line 24, in <module>
from keras_preprocessing import image
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras_preprocessing/image.py", line 20, in <module>
backend = get_keras_submodule('backend')
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras_preprocessing/__init__.py", line 24, in get_keras_submodule
raise ImportError('You need to first `import keras` '
ImportError: You need to first `import keras` in order to use `keras_preprocessing`. For instance, you can do:
\```
import keras
from keras_preprocessing import image
\```
Or, preferably, this equivalent formulation:
\```
from keras import preprocessing
\```
any ideas how to resolve this?
edit: okay, updating tensor flow fixed that for me and then it turned into another issue minimaxir/textgenrnn#197 (for which a fix was provided fortunately)
Hi, I have spent quite a lot of time solving this problem.
The problem was in keras_applications.
There is two possible way to face this error message:
- When the keras_applications library version is too old
- When keras_applications are new but having trouble with TensorFlow's keras_preprocessing library
Anyway it occurred by .json file we can solve this problem by reinstalling Tensorflow and Keras-related things but I know that this works heavily stressful.
This is my personal recommendation for this kind of errors.
- If you are sure that you installed Keras perfectly then just skipping its check process of Keras backend.
First, find your path of 'keras_applications' are installed (It might be in such 'C:\ProgramData\Anaconda3\Lib\site-packages' Or, just use 'pip show keras_applications')
And then, open (imagenet_utils.py) than edit it like this
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import keras
import json
import warnings
import numpy as np
from . import get_keras_submodule
from . import set_keras_submodules
backend = set_keras_submodules('backend')
backed = 'backend'
keras_utils = get_keras_submodule('utils')
CLASS_INDEX = None
CLASS_INDEX_PATH = ('https://s3.amazonaws.com/deep-learning-models/'
'image-models/imagenet_class_index.json')
# Global tensor of imagenet mean for preprocessing symbolic inputs
_IMAGENET_MEAN = None
...
It's just editing check flag rightly (It should be checked by Keras backend API but because of version issue it impossible to check automatically sometimes).
- This is possibly happening with the 'keras_preprocessing' library for the recent version (because some parts of 'keras_applications' are moved to 'keras_preprocessing) in this case you should edit the same parts on 'keras_preprocessing'
I know that this is not a perfect solution for the code, But it works okay temporarily. (But I still highly recommend resetting your env..!)