Search code examples
tensorflowtheanokerastensorboardautoencoder

how to use TensorBoard callback AND TensorBoard server?


keras blog autoencoder code I am trying to run the code for Convolutional Autoencode from

https://blog.keras.io/building-autoencoders-in-keras.html

from keras.layers import Input, Dense, Convolution2D, MaxPooling2D, UpSampling2D
from keras.models import Model

input_img = Input(shape=(1, 28, 28))

x = Convolution2D(16, 3, 3, activation='relu', border_mode='same')(input_img)

    x = MaxPooling2D((2, 2), border_mode='same')(x)
    x = Convolution2D(8, 3, 3, activation='relu', border_mode='same')(x)
    x = MaxPooling2D((2, 2), border_mode='same')(x)
    x = Convolution2D(8, 3, 3, activation='relu', border_mode='same')(x)
    encoded = MaxPooling2D((2, 2), border_mode='same')(x)

# at this point the representation is (8, 4, 4) i.e. 128-dimensional


Convolution2D(8, 3, 3, activation='relu', border_mode='same')(encoded)
x = UpSampling2D((2, 2))(x)
x = Convolution2D(8, 3, 3, activation='relu', border_mode='same')(x)
x = UpSampling2D((2, 2))(x)
x = Convolution2D(16, 3, 3, activation='relu')(x)
x = UpSampling2D((2, 2))(x)
decoded = Convolution2D(1, 3, 3, activation='sigmoid', border_mode='same')(x)

autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')

after running it I run this code for training :

from keras.datasets import mnist
import numpy as np

(x_train, _), (x_test, _) = mnist.load_data()

x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.
x_train = np.reshape(x_train, (len(x_train), 1, 28, 28))
x_test = np.reshape(x_test, (len(x_test), 1, 28, 28))

now I want to plot the result I using callback ! I type this

tensorboard --logdir=/tmp/autoencoder

in my terminal and it successfully switch back to theano but when I run

from keras.callbacks import TensorBoard

autoencoder.fit(x_train, x_train,
                nb_epoch=50,
                batch_size=128,
                shuffle=True,
                validation_data=(x_test, x_test),
                callbacks=[TensorBoard(log_dir='/tmp/autoencoder')])

it still imply that not switch back to tensorflow. Does anyone know how to fix it?

RuntimeError                              Traceback (most recent call last)
<ipython-input-4-fc8458b2c2ba> in <module>()
      6                 shuffle=True,
      7                 validation_data=(x_test, x_test),
----> 8                 callbacks=[TensorBoard(log_dir='/tmp/autoencoder')])

/home/hoda/anaconda2/lib/python2.7/site-packages/keras/callbacks.pyc in __init__(self, log_dir, histogram_freq, write_graph, write_images)
    487         super(TensorBoard, self).__init__()
    488         if K._BACKEND != 'tensorflow':
--> 489             raise RuntimeError('TensorBoard callback only works '
    490                                'with the TensorFlow backend.')
    491         self.log_dir = log_dir

RuntimeError: TensorBoard callback only works with the TensorFlow backend.

Solution

  • To switch to the Tensorflow backend you have to edit the keras.json file located in ~/.keras.

    You should see a line "backend": "theano", change "theano" to "tensorflow" and if Tensorflow is properly installed it should work and the line "Using TensorFlow backend." should appear when you import Keras.