I am trying to use a keras application in pycharm. I start my script off with the following imports:
from keras_vggface.vggface import VGGFace
from keras_vggface.utils import preprocess_input
from keras_vggface.utils import decode_predictions
Upon running this block of code, I get this error:
ImportError: You need to first `import keras` in order to use `keras_applications`. For instance, you can do:
```
import keras
from keras_applications import vgg16
```
Or, preferably, this equivalent formulation:
```
from keras import applications
```
I have tried importing the appropriate keras libraries as suggested, but the problem persists. I have also tried checking the json file to see if it contains the correct backend(it does).
How can I resolve this issue?
"edit for clarity"
My full imports go as follows:
from PIL import Image # for extracting image
from numpy import asarray
from numpy import expand_dims
from matplotlib import pyplot
from mtcnn.mtcnn import MTCNN # because i am too lazy to make one myself
import keras
from keras_applications import vgg16
from keras_vggface.vggface import VGGFace
from keras_vggface.utils import preprocess_input
from keras_vggface.utils import decode_predictions
Traceback:
Traceback (most recent call last):
File "C:/Users/###/PycharmProjects/##/#.py", line 17, in <module>
from keras_applications import vgg16
File "C:\Users\###\anaconda3\envs\tensor\lib\site-packages\keras_applications\vgg16.py", line 17, in <module>
backend = get_keras_submodule('backend')
File "C:\Users\###\anaconda3\envs\tensor\lib\site-packages\keras_applications\__init__.py", line 39, in get_keras_submodule
raise ImportError('You need to first `import keras` '
ImportError: You need to first `import keras` in order to use `keras_applications`. For instance, you can do:
```
import keras
from keras_applications import vgg16
```
Or, preferably, this equivalent formulation:
```
from keras import applications
```
Process finished with exit code 1
Are you planning to use the Tensorflow framework for executing the model. If it is tensorflow then i suggest using
import tensorflow as tf \ from tensorflow.keras.applications.vgg16 import VGG. Keras comes in-built in latest TF framework and hence we dont have to do an explicit import
even otherwise if you want to use Keras directly i believe the code should be :
import keras \ from keras.applications.vgg16 import VGG16 \ vggmodel = VGG16(weights='imagenet', include_top=True)
Related
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
Input In [20], in <cell line: 1>()
----> 1 from tensorflow.keras import layers
2 import keras.backend as backend
3 import tensorflow as tf
File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\api\_v2\keras\__init__.py:13, in <module>
10 import sys as _sys
12 from keras import __version__
---> 13 from keras.api._v2.keras import __internal__
14 from keras.api._v2.keras import activations
15 from keras.api._v2.keras import applications
File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\api\__init__.py:8, in <module>
3 """Public API for tf. namespace.
4 """
6 import sys as _sys
----> 8 from keras.api import keras
9 from tensorflow.python.util import module_wrapper as _module_wrapper
11 if not isinstance(_sys.modules[__name__], _module_wrapper.TFModuleWrapper):
File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\api\keras\__init__.py:16, in <module>
14 from keras.api.keras import activations
15 from keras.api.keras import applications
---> 16 from keras.api.keras import backend
17 from keras.api.keras import callbacks
18 from keras.api.keras import constraints
File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\api\keras\backend\__init__.py:24, in <module>
22 from keras.backend import binary_crossentropy
23 from keras.backend import binary_focal_crossentropy
---> 24 from keras.backend import binary_weighted_focal_crossentropy
25 from keras.backend import cast
26 from keras.backend import cast_to_floatx
ImportError: cannot import name 'binary_weighted_focal_crossentropy' from 'keras.backend' (D:\Users\DGong\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\backend.py)
I can't figure out why I'm getting an import error here. I've uninstalled and reinstalled tensorflow and keras many times, as well as used both pip and pip3. I checked the file and nothing seems to be different between the way binary_focal_crossentropy and binary_weighted_focal_crossentropy is exported. But the former seems to import without error.
You can import and use tf.keras.metrics.binary_focal_crossentropy by importing the metrics library below.
Also, Instead of pip installing each package(TensorFlow, Keras) separately, the recommended approach is to install Keras as part of the TensorFlow installation. When you install TensorFlow >2.0, Keras will be automatically installed, as well.
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.metrics import binary_focal_crossentropy
You can check more about binary_focal_crossentropy in the given link.
Below is the code I am trying to execute and getting import name emd not found. Whereas I have installed pyemd package.
import gensim.downloader as api
import json
from gensim.models.word2vec import Word2Vec
import pyemd
model = Word2Vec.load("final.modelall")
wordlistA = ['data','science']
wordlistB = ['python']
model.wv.wmdistance(wordlistA, wordlistB)
I am working on an object detection tutorial using keras, tensorlow on pyimagesearch, i was not able to subscribe on the pyimagesearch site therefore i couldn't get the pyimagesearch folder. Please is there a way i could run the code without using the folder?.
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-2-8215065cc4e1> in <module>()
----> 1 from pyimagesearch import config
2 from tensorflow.keras.applications import VGG16
3 from tensorflow.keras.layers import Flatten, Dense, Input
4 from tensorflow.keras.models import Model
5 from tensorflow.keras.optimizers import Adam
ModuleNotFoundError: No module named 'pyimagesearch'
because, its implemented by user defined class and import from that pyimagesearch Gsearch for adrianrosebrock
I'm very new to git and GitHub, and I'm trying to include a repository from GitHub to learn some basics of Graph Convolutional Neural Network. I have used "git init" to initialize the folder on my PC and used git submodule add https://github.com/vermaMachineLearning/keras-deep-graph-learning.git to add the repository as a subfolder, but when I tried to import the package using the following code
import os, sys
sys.path.append(os.path.join(os.getcwd(), "keras-deep-graph-learning")) # Adding the submodule to the module search path
sys.path.append(os.path.join(os.getcwd(), "keras-deep-graph-learning/examples")) # Adding the submodule to the module search path
import numpy as np
from keras.layers import Dense, Activation, Dropout
from keras.models import Model, Sequential
from keras.regularizers import l2
from tensorflow.keras.optimizers import Adam
from keras_dgl.layers import GraphCNN
import keras.backend as K
from keras.utils import to_categorical
It will give me the following trackback.
ModuleNotFoundError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_22224/914478576.py in <module>
7 from keras.regularizers import l2
8 from tensorflow.keras.optimizers import Adam
----> 9 from keras_dgl.layers import GraphCNN
10 import keras.backend as K
11 from keras.utils import to_categorical
~\python\GNN\keras-deep-graph-learning\keras_dgl\layers\__init__.py in <module>
----> 1 from .graph_cnn_layer import GraphCNN
2 from .multi_graph_cnn_layer import MultiGraphCNN
3 from .graph_attention_cnn_layer import GraphAttentionCNN
4 from .multi_graph_attention_cnn_layer import MultiGraphAttentionCNN
5 from .graph_convolutional_recurrent_layer import GraphConvLSTM
~\python\GNN\keras-deep-graph-learning\keras_dgl\layers\graph_cnn_layer.py in <module>
2 from keras import regularizers
3 import keras.backend as K
----> 4 from keras.engine.topology import Layer
5 import tensorflow as tf
6 from .graph_ops import graph_conv_op
ModuleNotFoundError: No module named 'keras.engine.topology'
My experience with deep learning and GitHub is very limited. Could anyone please kindly let me know if I made any silly mistakes? Thanks a lot for your great support and help.
When I run this line in my code,
from keras.utils import plot_model
I get the following:
"ImportError: cannot import name 'plot_model' from 'keras.utils' (/usr/local/lib/python3.7/dist-packages/keras/utils/__init__.py)"
When I went to bed last night it was working. This morning it throws an error. What happened and what should I do? Thank you.
Any suggestion would be appreciated
Try to import in the below format
from keras.utils.vis_utils import plot_model
This week I've had the same problem, with this import it works.
This worked taking from TensorFlow documentation
from keras.utils.vis_utils import plot_model
tf.keras.utils.plot_model(
model, to_file='model.png', show_shapes=False, show_dtype=False,
show_layer_names=True, rankdir='TB', expand_nested=False, dpi=96
)
In the recent version of keras, plot_model function has been moved to vis_utils (visualization utilities) under the utils module. So, you can get your plot_model function to work using either of these imports:
from keras.utils.vis_utils import plot_model
or
from tensorflow.keras.utils import plot_model
from keras.utils.vis_utils import plot_model
The plot_model was previously in Keras.utils, but it can be fetched from vis.utils
Copy paste the above code, and it will work