图片多分类会将不同的数字写在同一副图片上面,例如有3类【0、1、2】,为了输入模型,需要将三个类的结果分别映射到三个通道:
- 0 类:第 0 通道,对于是 0 的像素点设置为 1,其他均为 0
- 1 类:第 1 通道,对于是 1 的像素点设置为 1,其他均为 0
- 2 类:第 2 通道,对于是 2 的像素点设置为 1,其他均为 0
假设我们的图片是 4*4 的,通过下面的操作可以实现:
# 设置图片像素值,通过 numpy.array 表示 >>> png = [[1, 2, 0, 2], [2, 1, 0, 1], [0, 1, 2, 0], [1, 0, 2, 1]] >>> png = np.array(png) >>> png array([[1, 2, 0, 2], [2, 1, 0, 1], [0, 1, 2, 0], [1, 0, 2, 1]]) # 用来表示不同分类对应的 one-hot 编码 # 0 类:对应于 row0, [1, 0, 0] # 1 类:对应于 row1, [0, 1, 0] # 2 类:对应于 row2, [0, 0, 1] >>> np.eye(3) array([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]]) # 通过下面的操作,可以将原始像素值映射为 one-hot # np_arr_1[np_arr_2], 对于 np_arr_2 的每一个值,获取 np_arr_1 对应的 row 来竖着显示 >>> np.eye(3)[png.reshape([-1])] array([[0., 1., 0.], [0., 0., 1.], [1., 0., 0.], [0., 0., 1.], [0., 0., 1.], [0., 1., 0.], [1., 0., 0.], [0., 1., 0.], [1., 0., 0.], [0., 1., 0.], [0., 0., 1.], [1., 0., 0.], [0., 1., 0.], [1., 0., 0.], [0., 0., 1.], [0., 1., 0.]]) # 在转换回 H * W * Channel 的形式,不过这个看起来不直观 >>> np.eye(3)[png.reshape([-1])].reshape((4,4,3)) array([[[0., 1., 0.], [0., 0., 1.], [1., 0., 0.], [0., 0., 1.]], [[0., 0., 1.], [0., 1., 0.], [1., 0., 0.], [0., 1., 0.]], [[1., 0., 0.], [0., 1., 0.], [0., 0., 1.], [1., 0., 0.]], [[0., 1., 0.], [1., 0., 0.], [0., 0., 1.], [0., 1., 0.]]]) # 只显示 channel 1,显示如下: >>> np.eye(3)[png.reshape([-1])].reshape((4,4,3))[:,:,0] array([[0., 0., 1., 0.], [0., 0., 1., 0.], [1., 0., 0., 1.], [0., 1., 0., 0.]]) # channel 2 >>> np.eye(3)[png.reshape([-1])].reshape((4,4,3))[:,:,1] array([[1., 0., 0., 0.], [0., 1., 0., 1.], [0., 1., 0., 0.], [1., 0., 0., 1.]]) # channel 3 >>> np.eye(3)[png.reshape([-1])].reshape((4,4,3))[:,:,2] array([[0., 1., 0., 1.], [1., 0., 0., 0.], [0., 0., 1., 0.], [0., 0., 1., 0.]]) # 对应的分类 >>> png array([[1, 2, 0, 2], [2, 1, 0, 1], [0, 1, 2, 0], [1, 0, 2, 1]])