在 TensorFlow 中将 RGB 语义映射映射到一个热编码,反之亦然 [英] Map RGB Semantic Maps to One Hot Encodings and vice versa in TensorFlow

查看:29
本文介绍了在 TensorFlow 中将 RGB 语义映射映射到一个热编码,反之亦然的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下图是来自

解决方案

我的解决方案如下.期待有关如何提高效率的建议,或者更有效的答案.

将tensorflow导入为tf将 numpy 导入为 np导入 scipy.miscimg = scipy.misc.imread('aachen_000000_000019_gtFine_color.png', mode = 'RGB')调色板 = np.array([[128, 64, 128],[244, 35, 232],[ 70, 70, 70],[102, 102, 156],[190, 153, 153],[153, 153, 153],[250, 170, 30],[220, 220, 0],[107, 142, 35],[152, 251, 152],[ 70, 130, 180],[220, 20, 60],[255, 0, 0],[ 0, 0, 142],[ 0, 0, 70],[ 0, 60, 100],[ 0, 80, 100],[ 0, 0, 230],[119, 11, 32],[ 0, 0, 0],[255, 255, 255]], np.uint8)语义地图 = []对于调色板中的颜色:class_map = tf.reduce_all(tf.equal(img, colour),axis=-1)语义映射.附加(类映射)语义映射 = tf.stack(语义映射,轴=-1)# 注意转换为 tf.float32,因为大多数神经网络在 float32 中运行.语义映射 = tf.cast(语义映射,tf.float32)magic_number = tf.reduce_sum(semantic_map)打印semantic_map.shape调色板 = tf.constant(调色板,dtype=tf.uint8)class_indexes = tf.argmax(semantic_map,axis=-1)# 注意这个操作会压平 class_indexesclass_indexes = tf.reshape(class_indexes, [-1])color_image = tf.gather(调色板,class_indexes)color_image = tf.reshape(color_image, [1024, 2048, 3])sess = tf.Session()# NOTE magic_number 检查整个中只有 1024*2048 个 1# 1024*2048*21 张量.magic_number_val = sess.run(magic_number)断言 magic_number_val == 1024*2048color_image_val = sess.run(color_image)scipy.misc.imsave('test.png', color_image_val)

The image below is a sample semantic map from the Cityscapes Dataset. It's provided in the form of an RGB image where each specific colour represents a class.

In some deep learning tasks, we would like to map this into a one hot encoding. For example, if it has 20 classes, then this image would be mapped from H x W x 3 to H x W x 20.

How do we do this in TensorFlow?

解决方案

My solution is below. Looking forward to suggestions on how to make this more efficient or perhaps an answer that's more efficient.

import tensorflow as tf
import numpy as np
import scipy.misc

img = scipy.misc.imread('aachen_000000_000019_gtFine_color.png', mode = 'RGB')
palette = np.array(
[[128,  64, 128],
 [244,  35, 232],
 [ 70,  70,  70],
 [102, 102, 156],
 [190, 153, 153],
 [153, 153, 153],
 [250, 170,  30],
 [220, 220,   0],
 [107, 142,  35],
 [152, 251, 152],
 [ 70, 130, 180],
 [220,  20,  60],
 [255,   0,   0],
 [  0,   0, 142],
 [  0,   0,  70],
 [  0,  60, 100],
 [  0,  80, 100],
 [  0,   0, 230],
 [119,  11,  32],
 [  0,   0,   0],
 [255, 255, 255]], np.uint8)

semantic_map = []
for colour in palette:
  class_map = tf.reduce_all(tf.equal(img, colour), axis=-1)
  semantic_map.append(class_map)
semantic_map = tf.stack(semantic_map, axis=-1)
# NOTE cast to tf.float32 because most neural networks operate in float32.
semantic_map = tf.cast(semantic_map, tf.float32)
magic_number = tf.reduce_sum(semantic_map)
print semantic_map.shape

palette = tf.constant(palette, dtype=tf.uint8)
class_indexes = tf.argmax(semantic_map, axis=-1)
# NOTE this operation flattens class_indexes
class_indexes = tf.reshape(class_indexes, [-1])
color_image = tf.gather(palette, class_indexes)
color_image = tf.reshape(color_image, [1024, 2048, 3])

sess = tf.Session()
# NOTE magic_number checks that there are only 1024*2048 1s in the entire
# 1024*2048*21 tensor.
magic_number_val = sess.run(magic_number)
assert magic_number_val == 1024*2048
color_image_val = sess.run(color_image)
scipy.misc.imsave('test.png', color_image_val)

这篇关于在 TensorFlow 中将 RGB 语义映射映射到一个热编码,反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆