0

I'm using a very naive way to make predictions based on pre-trained model in keras. But it becomes much slower later. Anyone knows why? I'm very very very new to tensorflow.

count = 0
first = True
for nm in image_names:
    img = image.load_img(TEST_PATH + nm, target_size=(299, 299))
    img = image.img_to_array(img)

    image_batch = np.expand_dims(img, axis=0)

    processed_image = inception_v3.preprocess_input(image_batch.copy())
    prob = inception_model.predict(processed_image)

    df1 = pd.DataFrame({'photo_id': [nm]})
    df2 = pd.DataFrame(prob, columns=['feat' + str(j + 1) for j in range(prob.shape[1])])
    df = pd.concat([df1, df2], axis=1)

    header = first
    mode = 'w' if first else 'a'
    df.to_csv(outfile, index=False, header=header, mode=mode)
    first = False

    count += 1

    if count % 100 == 0:
        print('%d processed' % count)
Russ
  • 1

1 Answers1

0

I doubt the TF is slowing down. However there is another stack overflow question showing that to_csv slows down on append.

Performance: Python pandas DataFrame.to_csv append becomes gradually slower

If the images come batched you may also benefit from making larger batches rather than predicting one image at a time.

You can also explore tf.data for better data pipelining.

Ilya
  • 561
  • 2
  • 17