ValueError: Tensor Tensor("activation_5/Softmax:0", shape=(?, 2), dtype=float32) is not an element of this graph

There seem to be an issue with predicting using my keras model. I had trained it using the following keras code:

model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=(150, 150,3),padding='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2),padding='same'))

model.add(Conv2D(32, (3, 3),padding='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2),padding='same'))

model.add(Conv2D(64, (3, 3),padding='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2),padding='same'))

model.add(Flatten())  # this converts our 3D feature maps to 1D feature vectors
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(2))
model.add(Activation('softmax'))

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

However when i predict it on my local system after training with the shape (1,150,150,3) . It predicts accurately with an accuracy over 90%. however when i load my model on my raspberry pi and input the image of the same shape (1,150,150,3) it returns an error. Below is the code loaded on the raspberry pi to predict from the keras model.

    data = numpy.fromstring(stream.getvalue() , dtype = numpy.uint8)
    image5 = cv.imdecode(data , 1)
    print(image5.shape)
    #cv.imwrite('uhhu.png',image5)
    img = cv.resize(image5,(150,150))
    x = img_to_array(img)
    x = x.reshape((1,) + x.shape)
    x = x/255
    x = numpy.float32(x)
    print(x.shape)
    score = loaded_model.predict(x)
    print(score)

Topic keras deep-learning neural-network

Category Data Science


The solution to this issue is predict from the keras model when running a tensorflow graph as default.

import tensorflow as tf
graph = tf.get_default_graph()

global graph
with graph.as_default():
    result = loaded_model.predict(x)

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.