问题
I want to convert a collection of 3 Landsat images (each 12 band) to a single image array and then export it in TFRecord format. I used below code. My input collection is named images
. imageT
is the accumulated image, starting with a zero band which is dropped at the end. Each pixel of final imageOfSeries
image contain a matrix of size 3x12:
var imageT = ee.Image(0)
images = images.map(function(image){
return image.toArray();
})
var accumulate = function(image, imageT) {
return(ee.Image(imageT).addBands(image))
};
var imageOfSeries = ee.Image(images.iterate(accumulate, imageT))
.slice(1).toArray(1).matrixTranspose()
Export.image.toDrive({
image: imageOfSeries,
description: 'imageOfSeriesExample',
scale: 30,
region: geometry,
fileFormat: 'TFRecord',
formatOptions: {
patchDimensions: [10,10],
tensorDepths: [3,12]
}
});
But GEE returns an error when running the export task and says that Arrays must have dimensions = 1
.
How can I do my task? I also need to have more information on how to decode the TFRecord file in such a case, for which I couldn't find any example in GEE tutorial.
回答1:
To do what you want you'll need to flatten your matrix for export: export to TFRecord only supports 1D arrays (as pointed out by the error). For now EE doesn't support direct array flattening and wants labels per flattened element of the array.
Also tensor depth wants the 1D length of your array bands in order.
Assuming your image has exactly one array band, this should work (verbose to make a point):
var imageT = ee.Image(0)
images = images.map(function(image){
return image.toArray();
})
var accumulate = function(image, imageT) {
return(ee.Image(imageT).addBands(image))
};
var imageOfSeries = ee.Image(images.iterate(accumulate, imageT))
.slice(1).toArray(1).matrixTranspose()
imageOfSeriesFlattened = imageOfSeries
.arrayFlatten([
['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'],
['13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24'],
['25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36']])
.toArray();
Export.image.toDrive({
image: imageOfSeriesFlattened,
description: 'imageOfSeriesExample',
scale: 30,
region: geometry,
fileFormat: 'TFRecord',
formatOptions: {
patchDimensions: [10,10],
tensorDepths: [36]
}
});
来源:https://stackoverflow.com/questions/56691978/exporting-image-array-to-tfrecord-in-google-earth-engine