Can not install TensorBoard as Keras callback - tensorflow

I get the following error when attempting to create a TB callback.
I am using PlaidML in a Keras sequential model in a Jupiter notebook in MacOS 10.15.7 with
Keras version 2.2.4, &
TensorFlow 2.4.1
import plaidml.keras
plaidml.keras.install_backend()
import os
os.environ['KERAS_BACKEND'] = 'plaidml.keras.backend'
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K
import numpy as np
import pathlib
import PIL
import PIL.Image
# tensorflow for TensorBoard
import tensorflow
logs_dir = '/Users/x/TensorFlowFolder/logs'
from keras.callbacks import TensorBoard
callbacks_list = [
keras.callbacks.EarlyStopping(
monitor='val_loss',
patience=2,
),
keras.callbacks.ModelCheckpoint(
'best_modelFour.h5',
monitor='val_loss',
verbose=1,
save_best_only=True
),
keras.callbacks.TensorBoard(
log_dir=logs_dir,
histogram_freq=0,
write_graph=True, write_images=True
)
]
The Error chain is
> ModuleNotFoundError Traceback (most recent call
> last) ~/opt/anaconda3/lib/python3.8/site-packages/keras/callbacks.py
> in __init__(self, log_dir, histogram_freq, batch_size, write_graph,
> write_grads, write_images, embeddings_freq, embeddings_layer_names,
> embeddings_metadata, embeddings_data, update_freq)
> 744 import tensorflow as tf
> --> 745 from tensorflow.contrib.tensorboard.plugins import projector
> 746 except ImportError:
>
> ModuleNotFoundError: No module named 'tensorflow.contrib'
>
> During handling of the above exception, another exception occurred:
>
> ImportError Traceback (most recent call
> last) <ipython-input-4-93b8c44b3056> in <module>
> 15 save_best_only=True
> 16 ),
> ---> 17 keras.callbacks.TensorBoard(
> 18 log_dir=logs_dir,
> 19 histogram_freq=0,
>
> ~/opt/anaconda3/lib/python3.8/site-packages/keras/callbacks.py in
> __init__(self, log_dir, histogram_freq, batch_size, write_graph, write_grads, write_images, embeddings_freq, embeddings_layer_names,
> embeddings_metadata, embeddings_data, update_freq)
> 745 from tensorflow.contrib.tensorboard.plugins import projector
> 746 except ImportError:
> --> 747 raise ImportError('You need the TensorFlow module installed to '
> 748 'use TensorBoard.')
> 749
>
> ImportError: You need the TensorFlow module installed to use
> TensorBoard.
TensorBoard is apparently trying to get the module Projector from TensorFlow.contrib. TensorFlow.contrib has apparently been removed from TensorFlow for quite some time. The second error appears to be spurious since TF is successfully installed. I imagine that if this really broke TensorBoard there would be a big squawk, so what am I doing wrong?

Keras 2.2 does not support TensorFlow 2. Excerpt from the release of the 2.2.5:
Keras 2.2.5 is the last release of Keras that implements the 2.2.* API. It is the last release to only support TensorFlow 1 (as well as Theano and CNTK).
Downgrade your TensorFlow version to the 1.15.

Related

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'

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

ImportError: cannot import name 'get_source_inputs' from 'keras.engine' (/usr/local/lib/python3.7/dist-packages/keras/engine/__init__.py)

Here when i import the library i found this type of error, i tried everythings but i can not slove the problem
import segmentation_models as sm
from segmentation_models import Unet
# sm.set_framework('tf.keras')
tf.keras.backend.set_image_data_format('channels_last')
while importing the library I got this type of errors
ImportError Traceback (most recent call last)
<ipython-input-98-9567886360a6> in <module>()
2 # we are importing the pretrained unet from the segmentation models
3 # https://github.com/qubvel/segmentation_models
----> 4 import segmentation_models as sm
5 from segmentation_models import Unet
6 # sm.set_framework('tf.keras')
7 frames
/usr/local/lib/python3.7/dist-packages/classification_models/resnet/builder.py in <module>()
9 from keras.layers import Dense
10 from keras.models import Model
---> 11 from keras.engine import get_source_inputs
12
13 from .params import get_conv_params
ImportError: cannot import name 'get_source_inputs' from 'keras.engine' (/usr/local/lib/python3.7/dist-packages/keras/engine/__init__.py)
import tensorflow at the top and change
from keras.engine import get_source_inputs
to
from tensorflow.keras.utils import get_source_inputs

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.

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.