Search code examples
tensorflowtensorflow-servingtensorflow-estimator

Tensorflow: When I load a saved model and use predict it gives really bad results. Why? (I'm using the estimator API)


I'm using the estimator API to train a CNN that classifies images of shapes.

I'm able to successfully train the CNN using custom input_fn() which trains from a tfrecord file. I'm then able to predict using model.predict(predict_input_fn). The accuracy after a few epochs is >80% and then when I use model.predict() on some test data. I get >80% also. So that seems to work fine.

I wanted to save the model and then load the model and predict using that because that's what my goal is. So basically inference. When I do this and predict on my test data I am getting abysmal results. I have stripped out all preprocessing from my input_fn() and retrained. So that I am essentially passing in raw data when I am predicting. The problem persists. I'd like to know why this is happening or if I am doing something wrong. Thank you for any insights.

I'll link the relevant code My model_fn

def model_fn(features, labels, mode, params):

    x = features['image_raw']  
    net = tf.reshape(x, [-1, 824, 463, num_channels])
    net = tf.layers.conv2d(inputs=net, name='layer_conv1',
                           filters=32, kernel_size=11, strides=4,
                           padding='same', activation=tf.nn.relu)
    net = tf.layers.conv2d(inputs=net, name='layer_conv2',
                           filters=32, kernel_size=11, strides=4,
                           padding='same', activation=tf.nn.relu)
    net = tf.layers.conv2d(inputs=net, name='layer_conv3',
                           filters=32, kernel_size=5, strides=2,
                           padding='same', activation=tf.nn.relu)
    net = tf.layers.max_pooling2d(inputs=net, pool_size=2, strides=2,padding='SAME')    
    net = tf.layers.conv2d(inputs=net, name='layer_conv4',
                           filters=32, kernel_size=3,
                           padding='same', activation=tf.nn.relu)
    net = tf.contrib.layers.flatten(net)
    net = tf.layers.dense(inputs=net, name='layer_fc1',
                          units=256, activation=tf.nn.relu)
    net = tf.nn.dropout(net, 0.5)
    net = tf.layers.dense(inputs=net, name='layer_fc_2',
                          units=num_classes)
    logits = net
    y_pred = tf.nn.softmax(logits=logits)
    y_pred_cls = tf.argmax(y_pred, axis=1)
    if mode == tf.estimator.ModeKeys.PREDICT:
        export_outputs = {'classes': tf.estimator.export.PredictOutput({"classes": y_pred_cls})}

        spec = tf.estimator.EstimatorSpec(mode=mode,predictions=y_pred_cls,export_outputs=export_outputs)
    else:

        cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=labels,logits=logits)

        loss = tf.reduce_mean(cross_entropy)

        optimizer = tf.train.AdamOptimizer(learning_rate=0.001,beta1=0.9,beta2=0.999,epsilon=1e-8,name="Adam")

        train_op = optimizer.minimize(loss=loss, global_step=tf.train.get_global_step())

        metrics = {"accuracy": tf.metrics.accuracy(labels, y_pred_cls)}     
        # Wrap all of this in an EstimatorSpec.
        spec = tf.estimator.EstimatorSpec(
            mode=mode,
            loss=loss,
            train_op=train_op,
            eval_metric_ops=metrics
            )      
    return spec

My serving function:

def serving_input_receiver_fn(): 
  inputs = {"image_raw": tf.placeholder(shape=[824, 463], dtype=tf.float32)}
  return tf.estimator.export.ServingInputReceiver(inputs, inputs)

How I save my trained model:

export_dir = model.export_savedmodel(
    export_dir_base="./saved_model/",
    serving_input_receiver_fn=serving_input_receiver_fn,
    as_text=True) 

How I predict from the saved model:

from tensorflow.contrib import predictor
predict_fn = predictor.from_saved_model('./saved_model/1518601120/')
a = np.ones(shape=(824,463),dtype=np.float32)    
image = Image.open((os.path.join(prediction_dir,subdir,file)))
image = np.array(image)
image=image.swapaxes(0,1)
a[:,:]=image[:,:,0]  #The input is an RGBa PNG. only 1 channel is populated #with data from our shape.
prediction = predict_fn({"image_raw": a})
predictions.append((prediction['classes'][0]))

Solution

  • It turns out I was passing the predict function a tensor with heightwidth swapped. This was ok because my placeholder was the same shape. But once the tensor went into my model_fn() it was then reshaped to a size widthheight. Causing the image to be "squashed" before passing it through the model. This was causing the bad prediction results I was experiencing.