Python Tensorflow 2:对掩码应用一个热编码以进行语义分割
我正在尝试处理我的地面真相图像,以创建一个热编码张量:Python Tensorflow 2:对掩码应用一个热编码以进行语义分割,python,tensorflow,Python,Tensorflow,我正在尝试处理我的地面真相图像,以创建一个热编码张量: def one_hot(img, nclasses): result = np.zeros((img.shape[0], img.shape[1], nclasses)) img_unique = img.reshape(512*512, img.shape[2]) unique = np.unique(img_unique, axis=0) for i in range(img.shape[0]): for j i
def one_hot(img, nclasses):
result = np.zeros((img.shape[0], img.shape[1], nclasses))
img_unique = img.reshape(512*512, img.shape[2])
unique = np.unique(img_unique, axis=0)
for i in range(img.shape[0]):
for j in range(img.shape[1]):
for k, unique_val in enumerate(unique):
if (np.array_equal(img[i,j], unique_val)):
result[i,j,k] = 1
break
return result
这是从WxHx3图像创建WxHxN张量。我真的不喜欢这种方法,因为它的性能。你能建议更有效的方法吗
我尝试使用tf.one_hot,但它将图像转换为WxHx3xN张量 对于已知3个类的特定场景,这应该会更快
def one_hot2(img):
class1 = [255,0,0]
class2 = [0,0,255]
class3 = [255,255,255]
label = np.zeros_like(img)
label[np.sum(img==np.array([[class2]]), 2)==3] = 1
label[np.sum(img==np.array([[class3]]), 2)==3] = 2
onehot = np.eye(3)[label]
return onehot
对于已知3个类的特定场景,这应该可以更快地工作
def one_hot2(img):
class1 = [255,0,0]
class2 = [0,0,255]
class3 = [255,255,255]
label = np.zeros_like(img)
label[np.sum(img==np.array([[class2]]), 2)==3] = 1
label[np.sum(img==np.array([[class3]]), 2)==3] = 2
onehot = np.eye(3)[label]
return onehot
对于纯TF2.x方法,还可以执行以下操作
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
import tensorflow as tf
@tf.function # Remove this to see the tf.print array values
def get_one_hot():
label_ids = [0,5,10]
mask_orig = tf.constant([[0,10], [0,10]], dtype=tf.float32) # [2,2]
mask_onehot = tf.concat([tf.expand_dims(tf.math.equal(mask_orig, label_id),axis=-1) for label_id in label_ids], axis=-1) # [2,2,2]
mask_label_present = tf.reduce_any(mask_onehot, axis=[0,1]) # [2]
tf.print('\n - label_ids:{}'.format(label_ids))
tf.print('\n - mask_orig:\n{}\n'.format(mask_orig))
for id_, label_id in enumerate(label_ids):
tf.print(' - mask_onehot:{}\n{}'.format(label_id, mask_onehot[:,:,id_]))
tf.print('\n - mask_label_present:\n ', mask_label_present)
get_one_hot()
对于纯TF2.x方法,还可以执行以下操作
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
import tensorflow as tf
@tf.function # Remove this to see the tf.print array values
def get_one_hot():
label_ids = [0,5,10]
mask_orig = tf.constant([[0,10], [0,10]], dtype=tf.float32) # [2,2]
mask_onehot = tf.concat([tf.expand_dims(tf.math.equal(mask_orig, label_id),axis=-1) for label_id in label_ids], axis=-1) # [2,2,2]
mask_label_present = tf.reduce_any(mask_onehot, axis=[0,1]) # [2]
tf.print('\n - label_ids:{}'.format(label_ids))
tf.print('\n - mask_orig:\n{}\n'.format(mask_orig))
for id_, label_id in enumerate(label_ids):
tf.print(' - mask_onehot:{}\n{}'.format(label_id, mask_onehot[:,:,id_]))
tf.print('\n - mask_label_present:\n ', mask_label_present)
get_one_hot()
你能提供更多关于你输入图像的细节吗?3个频道是什么?通常情况下,地面真实图像只有一个通道,其中的值表示每个类别。@DMolony这三个通道是RGB。共有3个类别,由3种颜色表示:[255,0,0],[0,0255],[255255]。所以我想用一个热编码值来替换这些颜色。你能提供输入图像的更多细节吗?3个频道是什么?通常情况下,地面真实图像只有一个通道,其中的值表示每个类别。@DMolony这三个通道是RGB。共有3个类别,由3种颜色表示:[255,0,0],[0,0255],[255255]。所以我想用一个热编码值替换这些颜色