Matrix 如何从tensorflow中的不同对象类中采样n个像素? 问题
我想从图像中的每个实例类中随机采样Matrix 如何从tensorflow中的不同对象类中采样n个像素? 问题,matrix,tensorflow,image-segmentation,sampling,Matrix,Tensorflow,Image Segmentation,Sampling,我想从图像中的每个实例类中随机采样n像素 假设我的图像为I,宽度为w,高度为h。我还有一个标签为L的图像,描述了与I形状相同的实例类 当前方法 我目前的想法是首先将标签重塑为一个大的形状向量(N_p,1)。然后我重复它们N\u-c次,得到shape(N\u-p,N\u-c)。现在我重复一个向量l,它由所有唯一的标签组成,形状(1,N\u c)到形状(N\u p,N\u c)。将这两个值相等,得到一个矩阵,列y和行x,其中对应于行x的像素属于对应于列y的类别 下一步是将索引位置增加的矩阵与前一个矩
n
像素
假设我的图像为I
,宽度为w
,高度为h
。我还有一个标签为L
的图像,描述了与I
形状相同的实例类
当前方法
我目前的想法是首先将标签重塑为一个大的形状向量(N_p,1)
。然后我重复它们N\u-c
次,得到shape(N\u-p,N\u-c)
。现在我重复一个向量l
,它由所有唯一的标签组成,形状(1,N\u c)
到形状(N\u p,N\u c)
。将这两个值相等,得到一个矩阵,列y
和行x
,其中对应于行x
的像素属于对应于列y
的类别
下一步是将索引位置增加的矩阵与前一个矩阵连接起来。现在我可以将矩阵随机移动到各行
唯一缺少的步骤是提取该矩阵的n*n_c
行,首先每个类都有一行。然后使用矩阵右边的索引,我可以使用
tf.gather_nd
从原始图像I
中提取像素
问题
tf.nn.top_k
下面是一些示例代码,用于从图像的每个实例类中采样k个像素
import tensorflow as tf
seed = 42
width = 10
height = 6
embedding_dim = 3
sample_size = 2
image = tf.random_normal([height, width, embedding_dim], mean=0, stddev=4, seed=seed)
labels = tf.constant([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 0, 2, 2, 2, 0, 0],
[0, 0, 1, 1, 0, 2, 2, 2, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=tf.uint8)
labels = tf.cast(labels, tf.int32)
# First reshape to one vector
image_v = tf.reshape(image, [-1, embedding_dim])
labels_v = tf.reshape(labels, [-1])
# Get classes
classes, indices = tf.unique(labels_v)
# Dimensions
N_c = tf.shape(classes)[0]
N_p = tf.shape(labels_v)[0]
# Helper matrices
I = tf.tile(tf.expand_dims(indices, [-1]), [1, N_c])
C = tf.tile(tf.transpose(tf.expand_dims(tf.range(N_c), [-1])), [N_p, 1])
E = tf.cast(tf.equal(I, C), tf.int32)
P = tf.expand_dims(tf.range(N_p) + 1, [-1])
R = tf.concat([E, P], axis=1)
R_rand = tf.random_shuffle(R, seed = seed)
E_rand, P_rand = tf.split(R_rand, [N_c, 1], axis = 1)
M = tf.transpose(E_rand)
_, topInidices = tf.nn.top_k(M, k = sample_size)
topInidicesFlat = tf.expand_dims(tf.reshape(topInidices, [-1]), [-1])
sampleIndices = tf.gather_nd(P_rand, topInidicesFlat)
samples = tf.gather_nd(image_v, sampleIndices)
sess = tf.Session()
list = [image,
labels,
image_v,
labels_v,
classes,
indices,
N_c,
N_p,
I,
C,
E,
P,
R,
R_rand,
E_rand,
P_rand,
M,
topInidices,
topInidicesFlat,
sampleIndices,
samples
]
list_ = sess.run(list)
print(list_)