我正在尝试在一些遥感数据上运行 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
如果有人有一些见识,我将不胜感激。