You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用TF.io.TFRecordWriter写入影像在GEE中出现异常瓦片问题

确认TFRecordWriter写入影像瓦片顺序的方法

TFRecordWriter是严格按照你遍历train_predictions的顺序逐个写入瓦片的,要确认这个顺序,可以用以下几种方式:

  • 给每个TFRecord Example添加索引字段
    修改写入代码,为每个patch添加唯一的索引值,后续读取时直接查看索引即可确认顺序:

    filename = 'test.TFRecord'
    
    with tf.io.TFRecordWriter(filename) as writer:
        for idx, predictionPatch in enumerate(train_predictions):
            image_shape = predictionPatch.shape
            example = tf.train.Example(
                features=tf.train.Features(
                    feature={
                        'ag_or_not': tf.train.Feature(
                            float_list=tf.train.FloatList(value=predictionPatch.flatten())),
                        'patch_index': tf.train.Feature(
                            int64_list=tf.train.Int64List(value=[idx])
                        )
                    }
                )
            )
            writer.write(example.SerializeToString())
    

    读取TFRecord时,解析patch_index字段就能直接得到每个瓦片的写入顺序。

  • 读取已生成的TFRecord并验证顺序
    如果已经生成了TFRecord文件,可编写读取代码,将读取到的瓦片与原始train_predictions的元素逐一对比:

    def parse_example(example_proto):
        feature_description = {
            'ag_or_not': tf.io.FixedLenFeature([], tf.string),
            # 若之前添加了索引,需加上下面的字段
            # 'patch_index': tf.io.FixedLenFeature([], tf.int64),
        }
        parsed_features = tf.io.parse_single_example(example_proto, feature_description)
        # 替换为你实际的patch尺寸,比如(256,256,1)
        patch = tf.io.decode_raw(parsed_features['ag_or_not'], tf.float32)
        patch = tf.reshape(patch, (256, 256, 1))
        return patch
    
    # 读取TFRecord数据集
    dataset = tf.data.TFRecordDataset('test.TFRecord')
    dataset = dataset.map(parse_example)
    
    # 对比读取顺序与原始顺序
    for read_idx, read_patch in enumerate(dataset):
        original_patch = train_predictions[read_idx]
        if not tf.reduce_all(tf.equal(read_patch, original_patch)):
            print(f"第{read_idx}个瓦片顺序不匹配")
    
  • 检查train_predictions的生成顺序
    写入顺序完全由train_predictions的遍历顺序决定,因此可以在写入前,打印每个瓦片的标识信息(比如固定位置的像素值、生成时的地理坐标),确认这些标识的顺序是否与你预期的GEE瓦片分块顺序一致——这也是解决导出后瓦片错乱问题的核心,若train_predictions本身顺序就与GEE的瓦片顺序不匹配,导出后必然会出现错位。

内容的提问来源于stack exchange,提问作者Sean Carter

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 19:52:39