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

You need to first import keras in order to use keras_preprocessing. For instance, you can do: #41

Open
ThomasChen1220 opened this issue Jun 27, 2018 · 2 comments

Comments

@ThomasChen1220
Copy link

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
@ggegoge
Copy link

ggegoge commented Aug 24, 2020

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 #197 (for which a fix was provided fortunately)

@Lukious
Copy link

Lukious commented Feb 12, 2021

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:

  1. When the keras_applications library version is too old
  2. 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.

  1. 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).

  1. 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..!)

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

3 participants