pythontensorflowgoogle-cloud-platformgoogle-earth-enginetfrecord

Tf.io.TFRecordWriter writes imagery that ends up tiling in unexpected ways (Google Earth Engine)


I have implemented this example notebook -a methodology to segment satellite imagery using Google Earth Engine (GEE) and Tensorflow- on my specific use case.

The final step in this pipeline is uploading inference imagery to GEE using a mixer file that is generated by the GEE export process.

However, when I view the predicted imagery, there is a strange tiling issue that causes prediction images to be jumbled in an irregular grid.

Here is an image of the "ground truth".

This image was exported to the Google Cloud bucket using ee.Image.Export in the form of a TF record

Here is an image of the "ground truth"

Here is an image of the model prediction.

Using the same mixer file generated by the ee.Image.Export method, I generated model predictions and uploaded them as an EE asset.

Here is an image of the "prediction", note strange tiling

I would expect the uploaded image (#2) to be relatively consistent with the ground truth image, but instead, it appears to be split towards the middle, and the top half of the original image is placed on the bottom half.

A GEE link to visualize the issue can be found here.

Here is the code to generate the inference images:

filename = 'test.TFRecord'

with tf.io.TFRecordWriter(filename) as writer:
    for predictionPatch in train_predictions:
        # Store the original shape of the image
        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())),
                }
            )
        )
        # Write the example.
        writer.write(example.SerializeToString())

How can I confirm the order with which the TFRecordWriter writes patches?


Solution

  • Solution: I must sort the TF records before running predictions on them. Make sure they are in the right order according to the index found in the last few characters before the file extension.