Module not found using Input function from tensorflow.keras.layers - tensorflow

Im quite new learning machine learning and my first project is the creation of a Neural Network in order to detect key facial points on google colab. Everything has been working ok but today when I wanted to train my neural network I came accross with an error that has never appeared before when I trained my neural network.
The error is:
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-189-47fd3efd0229> in <module>()
5
6
----> 7 X_input = Input(input_shape)
8
9 # Zero-padding
4 frames
/usr/lib/python3.7/importlib/_bootstrap.py in _find_and_load_unlocked(name, import_)
ModuleNotFoundError: No module named 'keras.engine.base_layer_v1'
---------------------------------------------------------------------------
NOTE: If your import is failing due to a missing package, you can
manually install dependencies using either !pip or !apt.
To view examples of installing some common dependencies, click the
"Open Examples" button below.
I don't understand the line ModuleNotFoundError: No module named 'keras.engine.base_layer_v1' because the line that is not working is when I'm using Input from tensorflow.keras.layers.
I really don't know what is going on because I never got this error before. I've seen that it could be the version of TensorFlow or maybe my libraries.
I am using 2.3.0 versions in TensorFlow and Keras and these are the libraries I am importing:
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.applications import DenseNet121
from tensorflow.keras.models import Model, load_model
from tensorflow.keras.initializers import glorot_uniform
from tensorflow.keras.utils import plot_model
from tensorflow.keras.callbacks import ReduceLROnPlateau, EarlyStopping, ModelCheckpoint, LearningRateScheduler
from IPython.display import display
from tensorflow.python.keras import *
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras import layers, optimizers
from tensorflow.keras.applications.resnet50 import ResNet50
from tensorflow.keras.layers import *
from tensorflow.keras import backend as K
from keras import optimizers
I would really appreciate any help :)

Re-installing tensorflow and keras works for me

Related

ImportError: cannot import name 'builder' from 'google.protobuf.internal' while importing tf2onnx

I am following a tutorial to convert keras (.hdf5/.h5) model to onnx model. the link is (https://www.youtube.com/watch?v=7ndUGBzGVvg)
So, the code is
import tensorflow as tf
from tensorflow.keras.models import load_model
print(tf.__version__)
model = load_model('./model/weights.28-3.73.hdf5')
model.summary()
import tf2onnx
I also tried importing import keras2onnx but it generates error saying
module 'tensorflow.python.keras' has no attribute 'applications'. So I tried with tf2onnx.
I am using
keras==2.4.3
tensorflow==2.5.0
python==3.8.5
tf2onnx==1.13.0
onnx==1.13.0

How to import Model and other related classes in Tensorflow version 2.11 or keras version 2.11?

I am trying to import the following using latest version of tensorflow and keras i.e., 2.11:
from keras.models import Model
from keras.layers import Input, Conv2D, MaxPooling2D, UpSampling2D, concatenate, Conv2DTranspose, BatchNormalization, \
Dropout, Lambda
It was working really fine in the previous version I had. It just automatically upgraded and now, I am facing problems. Can somebody help me?
It is not enabled in the latest version or particularly, I am getting the following error:
Traceback (most recent call last):
File "C:\Users\Marco\PycharmProjects\pythonProject\venv\U_NET.py", line 3, in
from keras.models import Model
File "C:\Users\Marco\PycharmProjects\pythonProject\venv\lib\site-packages\keras_init_.py", line 21, in
from keras import models
File "C:\Users\Marco\PycharmProjects\pythonProject\venv\lib\site-packages\keras\models_init_.py", line 18, in
from keras.engine.functional import Functional
File "C:\Users\Marco\PycharmProjects\pythonProject\venv\lib\site-packages\keras\engine\functional.py", line 24, in
import tensorflow.compat.v2 as tf
ModuleNotFoundError: No module named 'tensorflow.compat'

ModuleNotFoundError: No module named 'tf' But the TensorFlow v2.3 is already installed

I'm trying to import some regularizers, with the following code:
import tensorflow as tf
from tensorflow import keras
import numpy as np
# importing regularizers
from tf.keras.regularizers import l2, activity_l2
The problem is that I get the following error:
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_8996/2415362791.py in <module>
4 from tensorflow import keras
5 # importing regularisers
----> 6 from tf.keras.regularizers import l2, activity_l2
ModuleNotFoundError: No module named 'tf'
However, I've already installed TensorFlow, and I can build neural networks, with several layers, without any error showing up...
I'm using Anaconda v2.1, Jupyter Notebooks v6.4.3.
Why is this happening?
You can use tensorflow.keras.regularizers instead of tf.keras.regularizers as below:
from tensorflow.keras.regularizers import l2 (to import specific regularizers)
or
from tensorflow.keras import regularizers (to use and import all other regularizers.)
activity_l2 is deleted and is equal to l2. Find the related github reference here.

still same error, module 'tensorflow' has no attribute 'get_default_graph'

Could you please help me with tensorflow? I have been very frustrated with tensorflow for many months. I know they don't work well with python 3.7. Now I am using python 3.6 with tensorflow 2.0.
What the heck this tensorflow? It is so frustrated to use tensorflow.
Here is my code:
import keras
import keras.backend as K
from keras.layers.core import Activation
from keras.models import Sequential,load_model
from keras.layers import Dense, Dropout, LSTM
The error of AttributeError: module 'tensorflow' has no attribute 'get_default_graph' is for next line:
model = Sequential()
Thank you so much.
Try importing libraries like that:
from tensorflow.keras.layers import Dense, Dropout, LSTM
I solved it. I downgraded tensorflow from 2.0 to 1.8. then it runs fine.

Colab upgraded to TensorFlow 2.0, now am experiencing an error RuntimeError: version does not support TensorFlow 2.0

I have been running my DL models on colab well, until they upgraded the system last Friday.
I get this error:
AttributeError Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py in _get_default_graph()
65 try:
---> 66 return tf.get_default_graph()
67 except AttributeError:
AttributeError: module 'tensorflow' has no attribute 'get_default_graph'
During handling of the above exception, another exception occurred:
RuntimeError Traceback (most recent call last)
8 frames
/usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py in _get_default_graph()
67 except AttributeError:
68 raise RuntimeError(
---> 69 'It looks like you are trying to use '
70 'a version of multi-backend Keras that '
71 'does not support TensorFlow 2.0. We recommend '
RuntimeError: It looks like you are trying to use a version of multi-backend Keras that does not support TensorFlow 2.0. We recommend using tf.keras, or alternatively, downgrading to TensorFlow 1.14.
I am currently running Python 3.7.6 version on my Mac(Mojave).
I am running TensorFlow 2.0.0 version on my machine.
I too had the same issue while using the Google Colab.
So what I did was, imported all the modules from the tensorflow.keras modules, instead of using the traditional method of calling via keras only.
eg. instead of from keras.models import Sequential
use
from tensorflow.keras import Sequential
And,
from keras.layers import Conv2D, MaxPool2D, Dense, Flatten, Dropout use
from tensorflow.keras.layers import Conv2D, MaxPool2D, Flatten, Dropout, Dense
So, my new code looks like this. My new code.
Try using from tensorflow.keras.models import Model
And make sure that all the Keras modules imported are of the same version.
Don't use
from tensorflow.keras.models import Model
and
from Keras import layers,
while building NN in the same notebook else it keeps on throwing some errors.
This solution helped me.