3
votes

I am trying to use tensorboard in keras. Following is my code :

from keras.layers import merge, Dropout, Convolution2D, MaxPooling2D, Input, Dense, Flatten, Merge
from keras.models import Model
from keras.callbacks import EarlyStopping, ReduceLROnPlateau,TensorBoard
import pickle
from sklearn.utils import shuffle
import numpy as np
import random
from keras.optimizers import Adam
import tensorflow as tf
import keras.backend.tensorflow_backend as KTF


np.random.seed(1000) 

def load_pickled_data(file, columns):
    with open(file, mode='rb') as f:
        dataset = pickle.load(f)
    return tuple(map(lambda c: dataset[c], columns))

train_preprocessed_dataset_file = "train.p"
test_preprocessed_dataset_file = "test.p"

X_train, y_train_64 = load_pickled_data(train_preprocessed_dataset_file, columns = ['features', 'labels'])
X_test, y_test_64 = load_pickled_data(test_preprocessed_dataset_file, columns = ['features', 'labels'])

y_train = y_train_64.astype(np.float32)
y_test = y_test_64.astype(np.float32)

old_session = KTF.get_session()

with tf.Graph().as_default():
    session = tf.Session('')
    KTF.set_session(session)
    KTF.set_learning_phase(1)
    ###CNN model###
    input_img = Input(shape=(32, 32, 1))

    conv_1 = Convolution2D(32, 5, 5, border_mode='same',     activation='relu')(input_img)
    pool_1 = MaxPooling2D((2, 2))(conv_1)
    pool_1 = Dropout(0.1)(pool_1)

    conv_2 = Convolution2D(64, 5, 5, border_mode='same', activation='relu')(pool_1)
    pool_2 = MaxPooling2D((2, 2))(conv_2)
    pool_2 = Dropout(0.2)(pool_2)

    conv_3 = Convolution2D(128, 5, 5, border_mode='same', activation='relu')(pool_2)
    pool_3 = MaxPooling2D((2, 2))(conv_3)
    pool_3 = Dropout(0.3)(pool_3)
    pool_3 = Flatten()(pool_3)

    pool_1 = MaxPooling2D((4, 4))(pool_1)
    pool_1 = Flatten()(pool_1)

    pool_2 = MaxPooling2D((2, 2))(pool_2)
    pool_2 =Flatten()(pool_2)

    all_features =  merge([pool_1, pool_2, pool_3], mode='concat')

    logits = Dense(500,activation='relu')(all_features)
    logits = Dropout(0.5)(logits)
    res = Dense(43,activation='softmax')(logits)

    c_model = Model(input_img, res)
    c_model.summary()

    adam = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08)
    c_model.compile(loss='categorical_crossentropy', optimizer= adam, metrics=['accuracy'])
    tensor_board = TensorBoard(log_dir='./logs', histogram_freq=1)

    history = c_model.fit(X_train, y_train, batch_size=128,nb_epoch=3,shuffle=True,verbose=1,validation_split=0.25,callbacks=[tensor_board])

    loss_and_metrics = c_model.evaluate(X_test, y_test, batch_size=128)

KTF.set_session(old_session)

BUT error happens as follows:

File "/home/jasontian/enter/lib/python3.5/site-packages/spyder/utils/site/sitecustomize.py", line 866, in runfile execfile(filename, namespace)

File "/home/jasontian/enter/lib/python3.5/site-packages/spyder/utils/site/sitecustomize.py", line 102, in execfile exec(compile(f.read(), filename, 'exec'), namespace)

File "/media/jasontian/keras_tf.py", line 111, in history = c_model.fit(X_train, y_train, batch_size=128,nb_epoch=3,shuffle=True,verbose=1,validation_split=0.25,callbacks=[tensor_board])

File "/home/jasontian/enter/lib/python3.5/site-packages/keras/engine/training.py", line 1196, in fit initial_epoch=initial_epoch) File "/home/jasontian/enter/lib/python3.5/site-packages/keras/engine/training.py", line 911, in _fit_loop callbacks.on_epoch_end(epoch, epoch_logs)

File "/home/jasontian/enter/lib/python3.5/site-packages/keras/callbacks.py", line 76, in on_epoch_end callback.on_epoch_end(epoch, logs)

File "/home/jasontian/enter/lib/python3.5/site-packages/keras/callbacks.py", line 653, in on_epoch_end result = self.sess.run([self.merged], feed_dict=feed_dict)

File "/home/jasontian/enter/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 766, in run run_metadata_ptr)

File "/home/jasontian/enter/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 921, in _run + e.args[0]) TypeError: Cannot interpret feed_dict key as Tensor: Can not convert a int into a Tensor.

At first I thought it might be y_train.dtype (it is float64), but I found it works well in an example. update:the shape of X_train is (39209,32,32,1). So how can I solve this?

1
Could you print the shape of X?Marcin Możejko
yes,the shape of X_train is (39209,32,32,1)JasonTian

1 Answers

1
votes

Doesn't it work without a tf session? If you don't really need the session you can try this :

from keras.layers import merge, Dropout, Convolution2D, MaxPooling2D, Input, Dense, Flatten, Merge
from keras.models import Model
from keras.callbacks import EarlyStopping, ReduceLROnPlateau,TensorBoard
import pickle
from sklearn.utils import shuffle
import numpy as np
import random
from keras.optimizers import Adam

np.random.seed(1000) 

def load_pickled_data(file, columns):
    with open(file, mode='rb') as f:
        dataset = pickle.load(f)
    return tuple(map(lambda c: dataset[c], columns))

train_preprocessed_dataset_file = "train.p"
test_preprocessed_dataset_file = "test.p"

X_train, y_train_64 = load_pickled_data(train_preprocessed_dataset_file, columns = ['features', 'labels'])
X_test, y_test_64 = load_pickled_data(test_preprocessed_dataset_file, columns = ['features', 'labels'])

y_train = y_train_64.astype(np.float32)
y_test = y_test_64.astype(np.float32)


###CNN model###
input_img = Input(shape=(32, 32, 1))

conv_1 = Convolution2D(32, 5, 5, border_mode='same',     activation='relu')(input_img)
pool_1 = MaxPooling2D((2, 2))(conv_1)
pool_1 = Dropout(0.1)(pool_1)

conv_2 = Convolution2D(64, 5, 5, border_mode='same', activation='relu')(pool_1)
pool_2 = MaxPooling2D((2, 2))(conv_2)
pool_2 = Dropout(0.2)(pool_2)

conv_3 = Convolution2D(128, 5, 5, border_mode='same', activation='relu')(pool_2)
pool_3 = MaxPooling2D((2, 2))(conv_3)
pool_3 = Dropout(0.3)(pool_3)
pool_3 = Flatten()(pool_3)

pool_1 = MaxPooling2D((4, 4))(pool_1)
pool_1 = Flatten()(pool_1)

pool_2 = MaxPooling2D((2, 2))(pool_2)
pool_2 =Flatten()(pool_2)

all_features =  merge([pool_1, pool_2, pool_3], mode='concat')

logits = Dense(500,activation='relu')(all_features)
logits = Dropout(0.5)(logits)
res = Dense(43,activation='softmax')(logits)

c_model = Model(input_img, res)
c_model.summary()

adam = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08)
c_model.compile(loss='categorical_crossentropy', optimizer= adam, metrics=['accuracy'])
tensor_board = TensorBoard(log_dir='./logs', histogram_freq=1)

history = c_model.fit(X_train, y_train, batch_size=128,nb_epoch=3,shuffle=True,verbose=1,validation_split=0.25,callbacks=[tensor_board])

loss_and_metrics = c_model.evaluate(X_test, y_test, batch_size=128)

If your keras default backend is Tensorflow, you don't have to specify it.