我试图在某些遥感数据上运行Deeplab v3 +(标准tensorflow版本)以执行二进制分类(对冲或无对冲),但是我发现输出非常奇怪,使我相信读取我的输入数据可能出问题了。
运行vis.py脚本后,我在segmentation_results文件夹中得到以下内容作为000000_image.png输出。据我了解,名为xxxx_image的图像应该代表原始图像?这里的像素值范围是0-3,在其他图像中,像素值可以是0-7。
但是我的原始图像看起来像这样(不是完全相同的文件,而只是原始数据的一个示例,因此您可以有所了解)。
此文件夹中还有预测文件:
因此,我假设预测=分类,图像=原文件。知道我为什么要将此作为原始文件吗?
要构建TFRecords数据,请使用以下脚本:
import math
import os.path
import sys
import build_data
import tensorflow as tf
FLAGS = tf.app.flags.FLAGS
tf.app.flags.DEFINE_string('image_folder',
'./VOCdevkit/VOC2012/JPEGImages',
'Folder containing images.')
tf.app.flags.DEFINE_string(
'semantic_segmentation_folder',
'./VOCdevkit/VOC2012/SegmentationClassRaw',
'Folder containing semantic segmentation annotations.')
tf.app.flags.DEFINE_string(
'list_folder',
'./VOCdevkit/VOC2012/ImageSets/Segmentation',
'Folder containing lists for training and validation')
tf.app.flags.DEFINE_string(
'output_dir',
'./tfrecord',
'Path to save converted SSTable of TensorFlow examples.')
_NUM_SHARDS = 4
def _convert_dataset(dataset_split):
"""Converts the specified dataset split to TFRecord format.
Args:
dataset_split: The dataset split (e.g., train, test).
Raises:
RuntimeError: If loaded image and label have different shape.
"""
dataset = os.path.basename(dataset_split)[:-4]
sys.stdout.write('Processing ' + dataset)
filenames = [x.strip('\n') for x in open(dataset_split, 'r')]
num_images = len(filenames)
num_per_shard = int(math.ceil(num_images / float(_NUM_SHARDS)))
image_reader = build_data.ImageReader('png', channels=3)
label_reader = build_data.ImageReader('png', channels=1)
for shard_id in range(_NUM_SHARDS):
output_filename = os.path.join(
FLAGS.output_dir,
'%s-%05d-of-%05d.tfrecord' % (dataset, shard_id, _NUM_SHARDS))
with tf.python_io.TFRecordWriter(output_filename) as tfrecord_writer:
start_idx = shard_id * num_per_shard
end_idx = min((shard_id + 1) * num_per_shard, num_images)
for i in range(start_idx, end_idx):
sys.stdout.write('\r>> Converting image %d/%d shard %d' % (
i + 1, len(filenames), shard_id))
sys.stdout.flush()
# Read the image.
image_filename = os.path.join(
FLAGS.image_folder, filenames[i] + '.' + FLAGS.image_format)
image_data = tf.gfile.FastGFile(image_filename, 'rb').read()
height, width = image_reader.read_image_dims(image_data)
# Read the semantic segmentation annotation.
seg_filename = os.path.join(
FLAGS.semantic_segmentation_folder,
filenames[i] + '.' + FLAGS.label_format)
seg_data = tf.gfile.FastGFile(seg_filename, 'rb').read()
seg_height, seg_width = label_reader.read_image_dims(seg_data)
if height != seg_height or width != seg_width:
raise RuntimeError('Shape mismatched between image and label.')
# Convert to tf example.
example = build_data.image_seg_to_tfexample(
image_data, filenames[i], height, width, seg_data)
tfrecord_writer.write(example.SerializeToString())
sys.stdout.write('\n')
sys.stdout.flush()
def main(unused_argv):
dataset_splits = tf.gfile.Glob(os.path.join(FLAGS.list_folder, '*.txt'))
for dataset_split in dataset_splits:
_convert_dataset(dataset_split)
if __name__ == '__main__':
tf.app.run()
在build_data.py脚本中,我更改了一个细节,因为我的输入数据是png uint16。
elif self._image_format == 'png':
self._decode = tf.image.decode_png(self._decode_data,
channels=channels, dtype=tf.uint16)
为了进行培训,我使用了您可以在此链接上找到的脚本(感觉有点大,可以粘贴到这里)https://github.com/tensorflow/models/blob/master/research/deeplab/train.py
对于随后导致该输出的可视化,我已经展示了我使用此处https://github.com/tensorflow/models/blob/master/research/deeplab/vis.py
处的脚本如果有人有见识,我将不胜感激。
答案 0 :(得分:0)
我将其修复后,发现这些模型并不是为将16位数据作为输入而构建的,因此您需要更改图像解码器以显式读取16位图像。在与数据生成相关的脚本以及在model_export中,有很多地方需要执行此操作,否则以后的推理图像也将被弄乱。
对于vis.py生成的输出图像,在save_annotations中,我必须更改最终图像写入器,以使其在写入原始图像时使用cv2,并且在写入遮罩时使用常规方法
if original:
cv2.imwrite('%s/%s.png' % (save_dir, filename),colored_label.astype(np.uint16))
else:
pil_image = img.fromarray(colored_label.astype(dtype=np.uint8))
with tf.gfile.Open('%s/%s.png' % (save_dir, filename), mode='w') as f:
pil_image.save(f, 'PNG')