复制速度极慢(?)到tensorflow

时间:2017-07-22 14:44:47

标签: python tensorflow

我正在处理保存在.mat文件中的here大小[610,340,102]的高光谱图像。用scipy加载后,它表示为数组。在进行计算之前,我必须将其重塑为[610 * 340,102]。以下代码在带有i5-6400处理器的台式机上运行:

import tensorflow as tf
import scipy.io as spio
import time

pavia_u = spio.loadmat("./../data/PaviaU.mat")["paviaU"]
original_shape = tf.shape(pavia_u)
pavia_u_reshaped = tf.to_float(tf.squeeze(tf.reshape(pavia_u, [-1, original_shape[0] * original_shape[1], original_shape[2]])))

with tf.Session() as session:
    start = time.clock()
    pavia_u_reshaped_value = session.run(pavia_u_reshaped)
    end = time.clock()
    print("Reshape time: {}".format(end - start))

并打印:Reshape time: 157.7449091748693

似乎对如此相对较小的数据集进行重塑操作不应该太慢。无论如何,我把重塑变成了numpy a

pavia_u = spio.loadmat("./../data/PaviaU.mat")["paviaU"]
original_shape = pavia_u.shape
pavia_u_reshaped = pavia_u.reshape([-1, original_shape[0] * original_shape[1], original_shape[2]]).squeeze())

with tf.Session() as session:
    start = time.clock()
    session.run(tf.identity(pavia_u_reshaped))
    end = time.clock()
    print("Test time: {}".format(end - start))

打印:Test time: 143.26529380869727

现在消耗这么多时间?如何解决这个问题。

1 个答案:

答案 0 :(得分:1)

重塑不是时间慢的原因,它的初始化需要时间。将代码更改为:

original_shape = tf.shape(pavia_u)
X = tf.Variable(pavia_u, dtype=tf.float32)

pavia_u_reshaped = tf.to_float(tf.squeeze(tf.reshape(X, [-1, original_shape[0] * original_shape[1], original_shape[2]])))


sess = tf.InteractiveSession()
start = time.clock()
tf.global_variables_initializer().run()
end = time.clock()
print("Initialization time: {}".format(end - start))

start = time.clock()
pavia_u_reshaped_value = sess.run(pavia_u_reshaped)
end = time.clock()
print("Reshape time: {}".format(end - start))

# Initialization time: 734.495346
# Reshape time: 1.897106