【问题标题】:Increasing time of session.run() execution in TensorFlowTensorFlow 中 session.run() 执行时间增加
【发布时间】:2019-07-25 15:17:09
【问题描述】:

我对 Tensorflow 有疑问。我根据本教程 [https://www.tensorflow.org/hub/tutorials/image_retraining][1] 重新训练初始模型,我想对来自相机的图像进行实时分类。问题在于将图像更改为张量。我修改了本教程中的一个函数,不是从文件中加载图像,而是直接从相机中加载图像。我的代码方法 session.run() 的每次迭代都需要越来越长的时间,我不知道为什么。这是我的代码:

def read_tensor_from_camera(image,
                        input_height=299,
                        input_width=299,
                        input_mean=0,
                        input_std=255):

    float_caster = tf.cast(image, tf.float32)
    dims_expander = tf.expand_dims(float_caster, 0)
    resized = tf.image.resize_bilinear(dims_expander, [input_height, input_width])
    normalized = tf.divide(tf.subtract(resized, [input_mean]), [input_std])

    start = time.time()
    sess = tf.compat.v1.Session()
    result = sess.run(normalized)
    stop = time.time()
    print(stop - start)

    return result


cap = cv2.VideoCapture(0)
while (True):

    ret, frame = cap.read()
    image = cv2.resize(frame, (input_height, input_width))
    t = read_tensor_from_camera(image)
    cv2.imshow('frame', image)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

0.024958372116088867 0.021515846252441406 0.024405956268310547 0.024140119552612305 0.02186441421508789 0.023257970809936523 0.02323007583618164 0.024866819381713867 0.030565977096557617 0.025953292846679688 0.025441408157348633 0.026473522186279297 0.023244380950927734 0.025677204132080078 0.024083375930786133 0.024756908416748047 0.024300098419189453 0.023919343948364258 0.026715993881225586 0.02456498146057129 0.027322769165039062 0.02640247344970703 0.02555561065673828 0.0270078182220459 0.0286102294921875 0.02633523941040039 0.02658367156982422 0.02969074249267578 0.026103973388671875 0.02613973617553711 0.02724480628967285 0.026676654815673828 0.02712845802307129 0.02947235107421875 0.030956745147705078 0.03170061111450195 0.027563095092773438 0.03021693229675293 0.028293848037719727 0.03078293800354004 0.02852654457092285 0.03080129623413086 0.032123565673828125 0.03287243843078613

【问题讨论】:

    标签: python tensorflow machine-learning artificial-intelligence


    【解决方案1】:

    我找到了解决方案,我编写了自己的函数来从 cv 图像创建归一化张量:

    def convert_tensor_from_camera(image)
        image = image / 255
        return [image]
    

    图像由 openCV 调整大小,我只能通过将图像除以像素的最大值 (255) 来归一化图像,它可以工作:)

    【讨论】:

      猜你喜欢
      • 2020-08-13
      • 1970-01-01
      • 2018-01-02
      • 2018-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-06
      相关资源
      最近更新 更多