Tensorboard import error in keras.callbacks

Issue

I’m following sentdex’s video on Neural Networks with Keras.

My tensorflow version is 2.0.0.

I’m on Windows 10 and running this code in an Anaconda Jupyter environment.

I have tried searching up or this error but I have got no results.

Code:

import tensorflow as tf
from tensorflow.keras.datasets import cifar10
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten
from tensorflow.keras.layers import Conv2D, MaxPooling2D
from tensorflow.keras.callbacks import Tensorboard

import pickle
import numpy as np
import os
import time

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

NAME = "CATS-VS-DOGS-CNN-{}".format(int(time.time()))

tensorboard = TensorBoard(log_dir="logs/{}".format(NAME))

pickle_in = open("X.pickle","rb")
X = pickle.load(pickle_in)

pickle_in = open("y.pickle","rb")
y = pickle.load(pickle_in)

X = X/255.0

model = Sequential()

model.add(Conv2D(256, (3, 3), input_shape=X.shape[1:]))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(256, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Flatten())  # this converts our 3D feature maps to 1D feature vectors

model.add(Dense(64))
model.add(Activation('relu'))

model.add(Dense(1))
model.add(Activation('sigmoid'))

model.compile(loss='binary_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])

model.fit(X, y, batch_size=8, epochs=10, validation_split=0.3, callbacks = [tensorboard])

Error:

ImportError                               Traceback (most recent call last)
<ipython-input-2-a32bf5fe83d7> in <module>
      5 from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten
      6 from tensorflow.keras.layers import Conv2D, MaxPooling2D
----> 7 from tensorflow.keras.callbacks import Tensorboard
      8 
      9 import pickle

ImportError: cannot import name 'Tensorboard' from 'tensorflow.keras.callbacks' (C:\Users\anves\Anaconda3\lib\site-packages\tensorflow_core\python\keras\api\_v2\keras\callbacks\__init__.py)

Solution

If you notice, you’re importing Tensorboard (with a small ‘b’):

from tensorflow.keras.callbacks import Tensorboard

but when you’re creating the callback, you’re using TensorBoard (with a capital ‘B’):

tensorboard = TensorBoard(log_dir="logs/{}".format(NAME))

So, you need to import TensorBoard (with a capital ‘B’) as follows:

from tensorflow.keras.callbacks import TensorBoard

Below’s a screenshot that shows the same correction:

enter image description here

Hope this helps! 🙂

Answered By – Balraj Ashwath

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published