haagn
haagn

Reputation: 163

Loading a model in TFLearn - predicting same value each time

I have trained a model on some data using tflearn to do binary classification. The model was trained to a 97% accuracy.

I want to use model.load() in another program to predict the class of some test input data.

However, model.load() only works when I include the argument weights_only=True. When I omit that argument from model.load(), it throws an error:

NotFoundError (see above for traceback): Key is_training not found in checkpoint

When I get the model loaded and run some predictions on my small test set - the classifications seem strange.. The model predicts a perfect 1 in the 1st index every single time. To me this shouldn't be happening if the model was trained to a very high accuracy. Here's what the predictions look like (expected output on the right):

[[  5.59889193e-22   1.00000000e+00]    [0, 1]
 [  4.25160435e-22   1.00000000e+00]    [0, 1]
 [  6.65333618e-23   1.00000000e+00]    [0, 1]
 [  2.07748895e-21   1.00000000e+00]    [0, 1]
 [  1.77639440e-21   1.00000000e+00]    [0, 1]
 [  5.77486922e-18   1.00000000e+00]    [1, 0]
 [  2.70562403e-19   1.00000000e+00]    [1, 0]
 [  2.78288828e-18   1.00000000e+00]    [1, 0]
 [  6.10306495e-17   1.00000000e+00]    [1, 0]
 [  2.35787162e-19   1.00000000e+00]]   [1, 0]

Note: This test data was data used to train the model so should be able to classify correctly with high accuracy.

The code for training the model:

tf.reset_default_graph()

train = pd.read_csv("/Users/darrentaggart/Library/Mobile Documents/com~apple~CloudDocs/Uni Documents/MEE4040 - Project 4/Coding Related Stuff/Neural Networks/modeltraindata_1280.csv")
test = pd.read_csv("/Users/darrentaggart/Library/Mobile Documents/com~apple~CloudDocs/Uni Documents/MEE4040 - Project 4/Coding Related Stuff/Neural Networks/modeltestdata_320.csv")

X = train.iloc[:,1:].values.astype(np.float32)
Y = np.array([np.array([int(i == l) for i in range(2)]) for l in 
train.iloc[:,:1].values])
test_x = test.iloc[:,1:].values.astype(np.float32)
test_y = np.array([np.array([int(i == l) for i in range(2)]) for l in 
test.iloc[:,:1].values])

X = X.reshape([-1, 16, 16, 1])
test_x = test_x.reshape([-1, 16, 16, 1])

convnet = input_data(shape=[None, 16, 16, 1], name='input')

initialization = tf.contrib.layers.variance_scaling_initializer(factor=1.0, mode='FAN_IN', uniform=False)

convnet = conv_2d(convnet, 32, 2, activation='elu', 
weights_init=initialization)
convnet = max_pool_2d(convnet, 2)

convnet = tflearn.layers.normalization.batch_normalization(convnet, beta=0.0, gamma=1.0, epsilon=1e-05, 
decay=0.9, stddev=0.002, trainable=True, restore=True, reuse=False, scope=None, name='BatchNormalization')

convnet = conv_2d(convnet, 64, 2, activation='elu', 
weights_init=initialization)
convnet = max_pool_2d(convnet, 2)

convnet = tflearn.layers.normalization.batch_normalization(convnet, beta=0.0, gamma=1.0, epsilon=1e-05, 
decay=0.9, stddev=0.002, trainable=True, restore=True, reuse=False, scope=None, name='BatchNormalization')

convnet = fully_connected(convnet, 254, activation='elu', weights_init=initialization)
convnet = dropout(convnet, 0.8)

convnet = tflearn.layers.normalization.batch_normalization(convnet, beta=0.0, gamma=1.0, epsilon=1e-05, 
decay=0.9, stddev=0.002, trainable=True, restore=True, reuse=False, scope=None, name='BatchNormalization')

convnet = fully_connected(convnet, 2, activation='softmax')
adam = tflearn.optimizers.Adam(learning_rate=0.00065, beta1=0.9, beta2=0.999, epsilon=1e-08)
convnet = regression(convnet, optimizer=adam, loss='categorical_crossentropy', name='targets')

model = tflearn.DNN(convnet, tensorboard_dir='/Users/darrentaggart/Library/Mobile Documents/com~apple~CloudDocs/Uni Documents/MEE4040 - Project 4/Coding Related Stuff/Neural Networks/latest logs',
tensorboard_verbose=3)

model.fit({'input': X}, {'targets': Y}, n_epoch=100, batch_size=16, 
validation_set=({'input': test_x}, {'targets': test_y}), snapshot_step=10, show_metric=True, run_id='1600 - ConvConvFC254 LR0.00065decay BN VSinit 16batchsize 100epochs')

model.save('tflearncnn.model')

Code for loading and generating predictions:

test = pd.read_csv("/Users/darrentaggart/Library/Mobile Documents/com~apple~CloudDocs/Uni Documents/MEE4040 - Project 4/Coding Related Stuff/Neural Networks/modelpredictiondata.csv")

X = test.iloc[:,1:].values.astype(np.float32)

sess=tf.InteractiveSession()

tflearn.is_training(False)

convnet = input_data(shape=[None, 16, 16, 1], name='input')

initialization = tf.contrib.layers.variance_scaling_initializer(factor=1.0, mode='FAN_IN', uniform=False)

convnet = conv_2d(convnet, 32, 2, activation='elu', weights_init=initialization)
convnet = max_pool_2d(convnet, 2)

convnet = tflearn.layers.normalization.batch_normalization(convnet, beta=0.0, gamma=1.0, epsilon=1e-05, 
decay=0.9, stddev=0.002, trainable=True, restore=True, reuse=False, scope=None, name='BatchNormalization')

convnet = conv_2d(convnet, 64, 2, activation='elu', weights_init=initialization)
convnet = max_pool_2d(convnet, 2)

convnet = tflearn.layers.normalization.batch_normalization(convnet, beta=0.0, gamma=1.0, epsilon=1e-05, 
decay=0.9, stddev=0.002, trainable=True, restore=True, reuse=False, scope=None, name='BatchNormalization')

convnet = fully_connected(convnet, 254, activation='elu', weights_init=initialization)


convnet = tflearn.layers.normalization.batch_normalization(convnet, beta=0.0, gamma=1.0, epsilon=1e-05, 
decay=0.9, stddev=0.002, trainable=True, restore=True, reuse=False, scope=None, name='BatchNormalization')

convnet = fully_connected(convnet, 2, activation='softmax')
adam = tflearn.optimizers.Adam(learning_rate=0.00065, beta1=0.9, beta2=0.999, epsilon=1e-08)
convnet = regression(convnet, optimizer=adam, loss='categorical_crossentropy', name='targets')

model = tflearn.DNN(convnet)

if os.path.exists('{}.meta'.format('tflearncnn.model')):
    model.load('tflearncnn.model', weights_only=False)
    print('model loaded!')

for i in enumerate(X):

    X = X.reshape([-1, 16, 16, 1])

    model_out = model.predict(X)

    if np.argmax(model_out) == 1: str_label='Boss'
    else: str_label = 'Slot'

print(model_out)

I know it's a long shot but thought someone might be able to shed some light on the matter. Thanks.

Upvotes: 3

Views: 1031

Answers (2)

Iorek
Iorek

Reputation: 581

It's been a year and a half since this question was asked, but sharing is caring after all. Using tflearn and Alexnet to binary classify an image.

The trick is to normalize after conversion to nparray. Don't forget to change the directory paths.

from __future__ import division, print_function, absolute_import
import tflearn
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.normalization import local_response_normalization
from tflearn.layers.estimator import regression
from data_utils import * 
import os
from PIL import Image
from numpy import array

def res_image(f, image_shape=[224,224], grayscale=False, normalize=True):
    img = load_image(f)
    width, height = img.size
    if width != image_shape[0] or height != image_shape[1]:
        img = resize_image(img, image_shape[0], image_shape[1])
    if grayscale:
        img = convert_color(img, 'L')
    elif img.mode == 'L':
        img = convert_color(img, 'RGB')

    img = pil_to_nparray(img)
    if normalize: # << this here is what you need
        img /= 255.
    img = array(img).reshape(1, image_shape[0], image_shape[1], 3)
    return img

# Building the network
network = input_data(shape=[None, 227, 227, 3])
network = conv_2d(network, 96, 11, strides=4, activation='relu')
network = max_pool_2d(network, 3, strides=2)
network = local_response_normalization(network)
network = conv_2d(network, 256, 5, activation='relu')
network = max_pool_2d(network, 3, strides=2)
network = local_response_normalization(network)
network = conv_2d(network, 384, 3, activation='relu')
network = conv_2d(network, 384, 3, activation='relu')
network = conv_2d(network, 256, 3, activation='relu')
network = max_pool_2d(network, 3, strides=2)
network = local_response_normalization(network)
network = fully_connected(network, 4096, activation='tanh')
network = dropout(network, 0.5)
network = fully_connected(network, 4096, activation='tanh')
network = dropout(network, 0.5)
network = fully_connected(network, 2, activation='softmax') # output is the number of outcomes
network = regression(network, optimizer='momentum',
                     loss='categorical_crossentropy',
                     learning_rate=0.001)

# Training
model = tflearn.DNN(network, 
                    tensorboard_dir=R'C:\Users\b0588718\Source\Repos\AlexNet\AlexNet')

model.load('model.tfl')

f = r'C:\Users\b0588718\Source\Repos\AlexNet\AlexNet\rawdata\jpg\0\P1170047.jpg'
img = res_image(f, [227,227], grayscale=False, normalize=True)

pred = model.predict(img)
print(" %s" % pred[0])

Upvotes: 1

Lasith Niroshan
Lasith Niroshan

Reputation: 1083

Didn't you try model.load(<path-to-saved-model>).
Ex : model.load("./model.tflearn")
I think this will solve your problem.

Upvotes: 0

Related Questions