tfrecord数据集读取问题,tf.image.decode_image解码前后出现数值不一样

问题描述:
自己制作的图像分割label.png,二进制编码打开png图像源文件后,标签全部变为0
原因:
制作标签时,注意图像的存储模式,是RGB还是灰度模式(L)

from PIL import Image
import tensorflow as tf
import numpy as np
#img_path='F:\\VOC2012\\SegmentationClassAug\\2007_000032.png'
img_path='00.png'

#原label.png
img=Image.open(img_path)
img_array=np.asarray(img)
print('1')
print(np.sum(np.int32(img_array==1)))

#二进制打开png图片,再解码
with tf.gfile.GFile(img_path, 'rb') as fid:
  encoded_jpg0 = fid.read()
with tf.Session() as s:
  label0 = tf.image.decode_image(encoded_jpg0,1)
  #label0=tf.decode_raw(encoded_jpg0,out_type=tf.uint8)
  print('2')
  print(np.sum(np.int32(s.run(label0)==1)))

#原label.png
img=Image.open(img_path).convert('L')
img.save('01.png')

img_path1='01.png'
#二进制打开png图片,再解码
with tf.gfile.GFile(img_path1, 'rb') as fid:
  encoded_jpg0 = fid.read()
with tf.Session() as s:
  label0 = tf.image.decode_image(encoded_jpg0,1)
  print('3')
  print(np.sum(np.int32(s.run(label0)==1)))
1
866
2
0
3
866