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
Related
I was following a tutorial about object detection, and it gave me this code:
from imageai.Detection import ObjectDetection
import os
execution_path = os.getcwd()
detector = ObjectDetection()
detector.setModelTypeAsRetinaNet()
detector.setModelPath( os.path.join(execution_path , "resnet50_coco_best_v2.1.0.h5"))
detector.loadModel()
detections = detector.detectObjectsFromImage(input_image=os.path.join(execution_path , "image.jpg"), output_image_path=os.path.join(execution_path , "imagenew.jpg"))
for eachObject in detections:
print(eachObject["name"] , " : " , eachObject["percentage_probability"] )
The problem is, it kept giving me an error like this:
ImportError: cannot import name 'BatchNormalization' from 'keras.layers.normalization'
I searched around, and I think its to do with my tensorflow version, but I never found a solution that worked.
You have to import Batch Normalization from tf.keras.layers
import tensorflow as tf
from tf.keras.layers import BatchNormalization
Hope , this Documentation may help you better.
If my answer finds you well..upvote
.
.
Happy Learning
When you get the error message there should be a directory given to a ini.py file. Open it with your IDE and past this line. It somehow worked for me.
from keras.layers.normalization.batch_normalization import BatchNormalization
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.
import numpy as np
import pandas as pd
import os
import tensorflow as tf
import keras
from keras.applications import VGG19
from keras.models import Sequential
from keras.layers import Dense, Dropout
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
import cv2
Please help me to resolve the error which i m getting on colab. I m new to coding section and have very little knowledge about it. Please help me resolve this error ImportError: cannot import name 'VGG19' from 'keras.applications' (/usr/local/lib/python3.7/dist-packages/keras/applications/init.py).
the right syntax is
from keras.applications.vgg19 import VGG19
You omitted the .vgg19 from your syntax
I made I model with VGG19. Images size for input_shape were 220. And then my model trained 2 diferents images types:
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense, Activation, Dropout, Flatten, Conv2D, MaxPooling2D
from tensorflow.keras.optimizers import RMSprop
from tensorflow.keras.applications import VGG19
modelVGG19_4 = tf.keras.Sequential()
modelVGG19_4.add(tf.keras.applications.VGG19(include_top=False, weights='imagenet',
pooling='avg', input_shape=(220, 220, 3)))
modelVGG19_4.summary()
modelVGG19_4.add(tf.keras.layers.Dense(2, activation="softmax"))
opt = tf.keras.optimizers.SGD(0.004)
modelVGG19_4.compile(loss='categorical_crossentropy',
optimizer=opt,
metrics=['accuracy'])
modelVGG19_4.summary()
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)