Estimator predict infinite loop
Asked Answered
A

2

6

I don't understand how to make a single prediction using TensorFlow Estimator API - my code results in an endless loop that keeps predicting for the same input.

According to the documentation, the prediction is supposed to stop when input_fn raises a StopIteration exception:

input_fn: Input function returning features which is a dictionary of string feature name to Tensor or SparseTensor. If it returns a tuple, first item is extracted as features. Prediction continues until input_fn raises an end-of-input exception (OutOfRangeError or StopIteration).

Here's the relevant part in my code:

classifier = tf.estimator.Estimator(model_fn=image_classifier, model_dir=output_dir,
                                    config=training_config, params=hparams)

def make_predict_input_fn(filename):
    queue = [ filename ]
    def _input_fn():
        if len(queue) == 0:
            raise StopIteration
        image = model.read_and_preprocess(queue.pop())
        return {'image': image}
    return _input_fn

predictions = classifier.predict(make_predict_input_fn('garden-rose-red-pink-56866.jpeg'))
for i, p in enumerate(predictions):
    print("Prediction %s: %s" % (i + 1, p["class"]))

What am I missing?

Araiza answered 17/12, 2017 at 15:57 Comment(0)
H
0

That's because input_fn() needs to be a generator. Change your function to (yield instead of return):

def make_predict_input_fn(filename):
    queue = [ filename ]
    def _input_fn():
        if len(queue) == 0:
            raise StopIteration
        image = model.read_and_preprocess(queue.pop())
        yield {'image': image}
    return _input_fn
Hypophysis answered 27/11, 2018 at 0:54 Comment(0)
S
0

one solution is to use itertools.islice:

import itertools

predictions = itertools.islice(predictions, number_of_samples)

for i, p in enumerate(predictions):
    print("Prediction %s: %s" % (i + 1, p["class"]))

number_of_samples is an int which is the stop point of the iterator.

Snuff answered 19/9, 2019 at 18:12 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.