簡體   English   中英

放大張量流中的張量

[英]Enlarge a tensor in tensorflow

我正在尋找一個tensorflow python方法來放大(調整大小)張量,以沿兩個軸將每個特征圖中的每個元素加倍,例如:

 ([[1, 2, 3],
   [4, 5, 6],
   [7, 8, 9]])

=>

 ([[1, 1, 2, 2, 3, 3],
   [1, 1, 2, 2, 3, 3],
   [4, 4, 5, 5, 6, 6],
   [4, 4, 5, 5, 6, 6],
   [7, 7, 8, 8, 9, 9],
   [7, 7, 8, 8, 9, 9]])

我看到了tf.tiletf.pad,但是我不知道如何使用這種方法來獲得結果。

感謝您的提示!

更新:

感謝sygi提供的有用提示,這里是適用於使用python3內核的jupyter notebook的形狀獨立解決方案:

import tensorflow as tf
import numpy as np

i = np.array([[1, 2, 3],
              [4, 5, 6],
              [7, 8, 9]])

j = np.array([[1,  2,  3,  4],
              [5,  6,  7,  8],
              [9, 10, 11, 12]])

k = np.array([[1, 2],
              [3, 4],
              [5, 6]])

a = tf.placeholder(tf.int64, shape=(None, None))
a_shape = tf.shape(a)

b = tf.reshape(a, [a_shape[0], a_shape[1], 1])
c = tf.tile(b, [1, 1, 2])
d = tf.reshape(c, [a_shape[0], a_shape[1]*2])

e = tf.reshape(d, [a_shape[0], a_shape[1]*2, 1])
f = tf.tile(e, [1, 1, 2])
g = tf.transpose(f, [0, 2, 1])

h = tf.reshape(g, [a_shape[0]*2, a_shape[1]*2])

session = tf.InteractiveSession()
session.run(tf.initialize_all_variables())

print(h.eval(feed_dict={a: i}))
print(h.eval(feed_dict={a: j}))
print(h.eval(feed_dict={a: k}))

session.close()

結果是

[[1 1 2 2 3 3]
 [1 1 2 2 3 3]
 [4 4 5 5 6 6]
 [4 4 5 5 6 6]
 [7 7 8 8 9 9]
 [7 7 8 8 9 9]]

[[ 1  1  2  2  3  3  4  4]
 [ 1  1  2  2  3  3  4  4]
 [ 5  5  6  6  7  7  8  8]
 [ 5  5  6  6  7  7  8  8]
 [ 9  9 10 10 11 11 12 12]
 [ 9  9 10 10 11 11 12 12]]

[[1 1 2 2]
 [1 1 2 2]
 [3 3 4 4]
 [3 3 4 4]
 [5 5 6 6]
 [5 5 6 6]]
a = tf.convert_to_tensor([[1, 2, 3],
                          [4, 5, 6],
                          [7, 8, 9]])
b = tf.reshape(a, [3, 3, 1])
c = tf.tile(b, [1, 1, 2])
d = tf.reshape(c, [3, 6])
print(d.eval())
array([[1, 1, 2, 2, 3, 3],
       [4, 4, 5, 5, 6, 6],
       [7, 7, 8, 8, 9, 9]], dtype=int32)

e = tf.reshape(d, [3, 6, 2])
f = tf.tile(e, [1, 1, 2])
g = tf.transpose(f, [0, 2, 1])
print(g.eval())
array([[[1, 1, 2, 2, 3, 3],
        [1, 1, 2, 2, 3, 3]],

       [[4, 4, 5, 5, 6, 6],
        [4, 4, 5, 5, 6, 6]],

       [[7, 7, 8, 8, 9, 9],
        [7, 7, 8, 8, 9, 9]]], dtype=int32)

h = tf.reshape(g, [6, 6])
print(h.eval())
array([[1, 1, 2, 2, 3, 3],
       [1, 1, 2, 2, 3, 3],
       [4, 4, 5, 5, 6, 6],
       [4, 4, 5, 5, 6, 6],
       [7, 7, 8, 8, 9, 9],
       [7, 7, 8, 8, 9, 9]], dtype=int32)

你可以得到的形狀a使用張量(如果它定義):

shape = a.get_shape().as_list()

只需將tf.image.ResizeMethod與最近鄰插值一起使用

array = tf.image.resize_images(old_array, (old_size*2, old_size*2),
                               method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)

方法的輸入必須是形狀[批處理,高度,寬度,通道]的4維張量或形狀[高度,寬度,通道]的3-D張量。

https://www.tensorflow.org/api_docs/python/tf/image/resize

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM