Include git submodule that is not pip installable - python

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.

Related

Can't import plot_model from keras.utils

I'm trying to visualize a neural network model with plot_model,
but importing plot_model fails with an error message i don't understand.
My code:
# %% setup
import os
import tensorflow as tf
tf_model_target_path = os.path.join(os.getcwd(), 'tfModelDir')
model = tf.keras.models.load_model(tf_model_target_path)
# %% visualize model: save to image file
from keras.utils import plot_model
plot_model(model, to_file='model.jpg')
the line
from keras.utils import plot_model
throws:
Exception has occurred: AttributeError
module 'tensorflow.compat.v2.__internal__' has no attribute 'dispatch'
File "{...}\kerasVisMinError.py", line 9, in <module>
from keras.utils import plot_model
When i run the code from VSCode's IPython interactive shell, i get a more verbose error message:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
{...}\kerasVisMinError.py in <module>
9 # %% visualize model: save to image file
----> 10 from keras.utils import plot_model
11 #from keras.utils.vis_utils import plot_model
12 #plot_model(model, to_file='model.jpg')
13
~\AppData\Roaming\Python\Python38\site-packages\keras\__init__.py in <module>
18 [keras.io](https://keras.io).
19 """
---> 20 from keras import distribute
21 from keras import models
22 from keras.engine.input_layer import Input
~\AppData\Roaming\Python\Python38\site-packages\keras\distribute\__init__.py in <module>
16
17
---> 18 from keras.distribute import sidecar_evaluator
~\AppData\Roaming\Python\Python38\site-packages\keras\distribute\sidecar_evaluator.py in <module>
20 from tensorflow.python.platform import tf_logging as logging
21 from tensorflow.python.util import deprecation
---> 22 from keras.optimizers.optimizer_experimental import (
...
---> 33 #tf.__internal__.dispatch.add_dispatch_support
34 def epsilon():
35 """Returns the value of the fuzz factor used in numeric expressions.
AttributeError: module 'tensorflow.compat.v2.__internal__' has no attribute 'dispatch'
im using VS Code.
my python version is 3.8.3
my packages for keras, etc:
tensorflow 2.4.1
keras 2.10.0
Keras-Preprocessing 1.1.2
keras-utils 1.0.13
I found a similar
question on SO that has no answer yet
I tried replacing the import line with from keras.utils.vis_utils import plot_model as suggested here, but i get the same error message
I created a google colab. There i'm able to import plot_model and then visualise my model
I tried to downgrade tensorflow to 2.3.1 as suggested
here, but now i get this error:
AttributeError Traceback (most recent call last)
c:\Users\Miki\Documents\progi_peldak\pythonTensorflow\kerasVisMinErrorForSO\kerasVisMinError.py in <module>
14 print(tf.version.VERSION)
15 #from keras.utils import plot_model
----> 16 from keras.utils.vis_utils import plot_model
17 #plot_model(model, to_file='model.jpg')
~\AppData\Roaming\Python\Python38\site-packages\keras\__init__.py in <module>
18 [keras.io](https://keras.io).
19 """
---> 20 from keras import distribute
21 from keras import models
22 from keras.engine.input_layer import Input
~\AppData\Roaming\Python\Python38\site-packages\keras\distribute\__init__.py in <module>
16
17
---> 18 from keras.distribute import sidecar_evaluator
~\AppData\Roaming\Python\Python38\site-packages\keras\distribute\sidecar_evaluator.py in <module>
20 from tensorflow.python.platform import tf_logging as logging
21 from tensorflow.python.util import deprecation
---> 22 from keras.optimizers.optimizer_experimental import (
23 optimizer as optimizer_experimental,
...
---> 33 #tf.__internal__.dispatch.add_dispatch_support
34 def epsilon():
35 """Returns the value of the fuzz factor used in numeric expressions.
AttributeError: module 'tensorflow.compat.v2' has no attribute '__internal__'
There are Tensorflow and Keras version mismatched in your system. You need to upgrade the tensorflow version to TF 2.10 as you have installed Keras version 2.10.
!pip install tensorflow==2.10
import tensorflow as tf
tf.__version__
Now import plot_model api by providing a complete alias name:
from tensorflow.keras.utils import plot_model

Error importing binary_weighted_focal_crossentropy from keras backend: Cannot import name

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

Pyimagesearch module not found

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

Unable to import a keras application

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)

module 'tensorflow' has no attribute 'get_default_graph' with Stocks Prediction

I am trying to use the same code in this page but I am getting an error at the middle of the code.
AttributeError Traceback (most recent call last)
<ipython-input-34-9ff70788070d> in <module>()
----> 1 model = Sequential()
2 model.add(LSTM(units=50, return_sequences=True,input_shape=(x_train.shape[1],1)))
3 model.add(LSTM(units=50, return_sequences=False))
4 model.add(Dense(units=25))
5 model.add(Dense(units=1))
1 frames
/usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py in get_uid(prefix)
66 """
67 global _GRAPH_UID_DICTS
---> 68 graph = tf.get_default_graph()
69 if graph not in _GRAPH_UID_DICTS:
70 _GRAPH_UID_DICTS[graph] = defaultdict(int)
AttributeError: module 'tensorflow' has no attribute 'get_default_graph'
Here is my import list:
#Import the libraries
from tensorflow.keras import backend as K
from tensorflow.keras.models import Sequential, load_model
from tensorflow.keras.layers import LSTM, Dense, RepeatVector, Masking, TimeDistributed
from tensorflow. keras.utils import plot_model
import quandl
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.svm import SVR
import pandas_datareader as web
from sklearn.model_selection import train_test_split
import math
from keras.models import Sequential
from keras.layers import Dense, LSTM
from sklearn.preprocessing import MinMaxScaler
#import tensorflow as tf
#newinv=inventory+str(add)
from tensorflow.keras.layers import Embedding
from matplotlib import pyplot as plt
python tensorflow machine-learning keras
Update: After editing the code based on Giorgos' answer, no I get this is the error:
NameError Traceback (most recent call last)
<ipython-input-20-9ff70788070d> in <module>()
----> 1 model = Sequential()
2 model.add(LSTM(units=50, return_sequences=True,input_shape=(x_train.shape[1],1)))
3 model.add(LSTM(units=50, return_sequences=False))
4 model.add(Dense(units=25))
5 model.add(Dense(units=1))
NameError: name 'Sequential' is not defined
Here is my import list:
import math
import pandas_datareader as web
import numpy as np
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
import quandl
import tensorflow as tf
model = tf.keras.Sequential()
from keras.layers import Dense, LSTM
import matplotlib.pyplot as plt
plt.style.use('fivethirtyeight')
Here is where I am getting the error:
#Build the LSTM network model
model = Sequential()
model.add(LSTM(units=50, return_sequences=True,input_shape=(x_train.shape[1],1)))
model.add(LSTM(units=50, return_sequences=False))
model.add(Dense(units=25))
model.add(Dense(units=1))
If you are using tf.keras, instead of
from keras.models import Sequential
from keras.layers import Dense, LSTM
use the following:
import tensorflow as tf
model = tf.keras.Sequential()
model.add(tf.keras.layers.LSTM(units=50, return_sequences=True,input_shape=(x_train.shape[1],1)))
model.add(tf.keras.layers.LSTM(units=50, return_sequences=False))
model.add(tf.keras.layers.Dense(units=25))
model.add(tf.keras.layers.Dense(units=1))
And also make sure to remove your older import from keras.models import Sequential so that Sequential() is not overwritten in namespace. Same applies for from keras.layers import Dense, LSTM.
There might be some incompatibilities between your Keras and TensorFlow. The thing that did the trick for me was uninstalling the Keras using sudo pip uninstall keras and re-installing it.

Categories

Resources