AttributeError: module 'tensorflow.compat.v2.__internal__' has no attribute 'tf2' - tensorflow2.0

enter image description here
Above is the image:
The error is this:
LOCAL.GENERATED_WITH_V2 = tf.__internal__.tf2.enabled()
AttributeError: module 'tensorflow.compat.v2.__internal__' has no attribute 'tf2'
Thanks

From Tensorflow 2.x onward, keras is no longer maintained and it became a part of Tensorflow. I would recommend instead of import keras, you should try from tensorflow import keras or import tensorflow as tf and use tf.keras.
You can import Sequential module from tensorflow as shown below
import tensorflow as tf
from tf.keras import Sequential
For more information you can refer this and this

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

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

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

Custom Layer not supporting serialized custom activation function

since the release of version 2.6 of Tensorflow I am having a issue I did not have with version 2.5.
The following code works OK:
from tensorflow.keras.utils import get_custom_objects
from tensorflow.keras.layers import Dense
def my_act(x):
return x
get_custom_objects().update({"my_act": my_act})
dense = Dense(3, activation="my_act")
However, if I try to do the same but with a custom layer instead of Tensorflow built-in layers, I have the error:
ValueError: Unknown activation function: my_act. Please ensure this object is passed to the `custom_objects` argument. See https://www.tensorflow.org/guide/keras/save_and_serialize#registering_the_custom_object for details.
Here you have the minimum code to reproduce plus I show that with version 2.5 works ok (You need to restart the runtime to run it tho).
Try to import activations like this:
from tensorflow.keras import activations
instead of from tensorflow.python.keras import activations.
In tensorflow 2.7 and later versions tensorflow.python will no longer exist, and it seems in TF 2.6 already it is not compatible with some other functions.

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.

use tensorflow.GPUOptions within Keras when using tensorflow backend

In tensorflow I can do something like this when creating a session:
tf.GPUOptions(per_process_gpu_memory_fraction=0.333,allow_growth=True)
Is there a way to do the same in keras with the tensorflow backend?
You can set the Keras global tensorflow session with keras.backend.tensorflow_backend.set_session():
import tensorflow as tf
import keras.backend.tensorflow_backend as ktf
def get_session(gpu_fraction=0.333):
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=gpu_fraction,
allow_growth=True)
return tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))
ktf.set_session(get_session())