【问题标题】:predicting using pre-trained model becomes slower and slower使用预训练模型进行预测变得越来越慢
【发布时间】:2019-04-22 17:39:11
【问题描述】:

我正在使用一种非常天真的方式根据 keras 中的预训练模型进行预测。但后来它变得慢得多。有谁知道为什么?我对 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)

【问题讨论】:

    标签: tensorflow


    【解决方案1】:

    我怀疑 TF 正在减速。然而,还有另一个堆栈溢出问题表明 to_csv 在追加时变慢了。

    Performance: Python pandas DataFrame.to_csv append becomes gradually slower

    如果图像是批量处理的,您还可以从批量处理中受益,而不是一次预测一张图像。

    您还可以探索 tf.data 以获得更好的数据管道。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-03
      • 2016-06-12
      • 2021-08-11
      • 1970-01-01
      • 2020-07-04
      • 2017-07-16
      • 2017-08-19
      相关资源
      最近更新 更多