简体   繁体   English

使用预训练模型预测变得越来越慢

[英]predicting using pre-trained model becomes slower and slower

I'm using a very naive way to make predictions based on pre-trained model in keras. 我正在使用一种非常天真的方式根据keras中预先训练的模型进行预测。 But it becomes much slower later. 但是后来变得慢得多。 Anyone knows why? 谁知道为什么? I'm very very very new to tensorflow. 我对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)

I doubt the TF is slowing down. 我怀疑TF正在放缓。 However there is another stack overflow question showing that to_csv slows down on append. 然而,还有另一个堆栈溢出问题显示to_csv减慢了追加。

Performance: Python pandas DataFrame.to_csv append becomes gradually slower 性能:Python pandas DataFrame.to_csv追加变得逐渐变慢

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. 您还可以浏览tf.data以获得更好的数据流水线。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM