我目前有 16 張影像(A、B、C、D、E、F、G 等),它們必須作為 Tensorflow 資料集作業流的一部分連接成一張。每張圖片都是 128 x 128,形狀為 (128, 128, 3)。最終輸出應該是形狀為 (512,512,3) 的 512 x 512 影像。所有影像都來自影像序列,稱為img_seq. 此 img_seq 的形狀為 (None, 128, 128, 3)
現在,這是通過以下代碼完成的:
@tf.function
def glue_to_one(imgs_seq):
first_row= tf.concat((imgs_seq[0], imgs_seq[1],imgs_seq[2],imgs_seq[3]), 0)
second_row = tf.concat((imgs_seq[4], imgs_seq[5], imgs_seq[6], imgs_seq[7]), 0)
third_row = tf.concat((imgs_seq[8], imgs_seq[9], imgs_seq[10], imgs_seq[11]), 0)
fourth_row = tf.concat((imgs_seq[12], imgs_seq[13], imgs_seq[14], imgs_seq[15]), 0)
img_glue = tf.stack((first_row, second_row, third_row, fourth_row), axis=1)
img_glue = tf.reshape(img_glue, [512,512,3])
return img_glue
懷疑這種方法效率低下并且正在學習到瓶頸。一種不同的方法是分配一個 512 x 512 張量,然后替換元素。這樣會更有效率嗎?怎么做?你能推薦一個更好的方法嗎?
uj5u.com熱心網友回復:
只需使用 tf.split 方法而不是撰寫那么多代碼......,
**Your Inputs seems to be a list of inputs**
def stack_and_concat(x):
t = tf.split(x , 16 , axis=0)
t = tf.reshape(tf.stack([tf.concat(t[(i*4): 4 * (i 1)] , axis=1) for i in range(4)],axis=2) , (512,512,3))
return t
concat_inputs(x).shape
TensorShape([512, 512, 3])
對于千次迭代,我的方法用了3.28 secs,但你用了10.35 secs
uj5u.com熱心網友回復:
你可以使用這樣的方法將它改進大約 3 次:
def glue_answer(imgs_seq):
image = tf.reshape(imgs_seq, (4, 4, 128, 128, 3))
image = tf.concat(image, axis=1)
image = tf.concat(image, axis=1)
return image
我測驗了如下性能:
def glue_to_one(imgs_seq):
first_row= tf.concat((imgs_seq[0], imgs_seq[1],imgs_seq[2],imgs_seq[3]), 0)
second_row = tf.concat((imgs_seq[4], imgs_seq[5], imgs_seq[6], imgs_seq[7]), 0)
third_row = tf.concat((imgs_seq[8], imgs_seq[9], imgs_seq[10], imgs_seq[11]), 0)
fourth_row = tf.concat((imgs_seq[12], imgs_seq[13], imgs_seq[14], imgs_seq[15]), 0)
img_glue = tf.stack((first_row, second_row, third_row, fourth_row), axis=1)
img_glue = tf.reshape(img_glue, [512,512,3])
return img_glue
def glue_answer(imgs_seq):
image = tf.reshape(imgs_seq, (4, 4, 128, 128, 3))
image = tf.concat(image, axis=1)
image = tf.concat(image, axis=1)
return image
print("Method in question:")
%timeit -n 1000 -r 10 glue_to_one(imgs_seq)
print("Method in answe:")
%timeit -n 1000 -r 10 glue_answer(imgs_seq)
輸出:
Method in question:
1.7 ms ± 212 μs per loop (mean ± std. dev. of 10 runs, 1,000 loops each)
Method in answe:
540 μs ± 28.8 μs per loop (mean ± std. dev. of 10 runs, 1,000 loops each)
uj5u.com熱心網友回復:
tf.squeeze(tf.concat(tf.split(tf.concat(imgs_seq, axis=0),4),axis=1))
測驗:
imgs_seq = [ tf.random.normal(shape=(128,128,3)) for _ in range(16)]
out1 = glue_to_one(imgs_seq)
out2 = tf.squeeze(tf.concat(tf.split(tf.concat(imgs_seq, axis=0),4),axis=1))
#check whether both outputs are same.
np.testing.assert_allclose(out1.numpy(), out2.numpy())
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/534013.html
標籤:Python张量流
