Google Earth Engine Error – Image Select Pattern Did Not Match Any Bands

google-earth-enginepython

I am trying to combine two images one containing various bands of the sentinel 2 mission and the other one is created from a feature collection containing two types of polygons that have the values "test" of 0 and 1 as can be seen here:

Polygons converted to map

The combined image should then be exported following the tutorial here (https://colab.research.google.com/github/google/earthengine-api/blob/master/python/examples/ipynb/UNET_regression_demo.ipynb#scrollTo=FyRpvwENxE-A)

Here is my code:

RESPONSE = 'test'

trainingMap = ee.FeatureCollection('users/xxx/xxx')
trainingMapReduced = trainingMap.reduceToImage([RESPONSE], 'max');

# combine the images
featureStack = ee.Image.cat([
  image.select(BANDS),
  trainingMapReduced.select([RESPONSE])
]).float()

# consider a kernel of KERNEL_SIZE x KERNEL_SIZE around each pixel
list = ee.List.repeat(1, KERNEL_SIZE)
lists = ee.List.repeat(list, KERNEL_SIZE)
kernel = ee.Kernel.fixed(KERNEL_SIZE, KERNEL_SIZE, lists)

arrays = featureStack.neighborhoodToArray(kernel)

# load training and evaluation areas

trainingPolys = ee.FeatureCollection('users/xxx/trainingRegion')
evalPolys = ee.FeatureCollection('users/xxx/testingRegion')

trainingPolysList = trainingPolys.toList(trainingPolys.size())
evalPolysList = trainingPolys.toList(trainingPolys.size())

# These numbers determined experimentally.
n = 100 # Number of shards in each polygon.
N = 2000 # Total sample size in each polygon.

# Export all the training data (in many pieces), with one task 
# per geometry.
for g in range(trainingPolys.size().getInfo()):
  geomSample = ee.FeatureCollection([])
  for i in range(n):
    sample = arrays.sample(
      region = ee.Feature(trainingPolysList.get(g)).geometry(), 
      scale = 30, 
      numPixels = N / n, # Size of the shard.
      seed = i,
      tileScale = 8
    )
    geomSample = geomSample.merge(sample)

  desc = TRAINING_BASE + '_g' + str(g)
  task = ee.batch.Export.table.toDrive(
    collection = geomSample,
    description = desc, 
    folder = BUCKET, 
    fileNamePrefix = FOLDER + '_' + desc,
    fileFormat = 'TFRecord',
    selectors = BANDS + [RESPONSE]
  )
  task.start()

The task starts but it fails with the following error message "Error: Image.select: Pattern 'test' did not match any bands."

When I use

featureStack = image.addBands(trainingMapReduced, [RESPONSE])

instead of

featureStack = ee.Image.cat([
  image.select(BANDS),
  trainingMapReduced.select([RESPONSE])
]).float()

I get the error message
"Error: Image.addBands: Cannot add band 'test' because it doesn't appear in srcImg.
"

The print(trainingMapReduced) looks like this:

ee.Image({
"type": "Invocation",
"arguments": {
"input": {
"type": "Invocation",
"arguments": {
"collection": {
"type": "Invocation",
"arguments": {
"tableId": "users/xxx/xxx"
},
"functionName": "Collection.loadTable"
},
"properties": [
"test"
],
"reducer": {
"type": "Invocation",
"arguments": {},
"functionName": "Reducer.max"
}
},
"functionName": "Collection.reduceToImage"
},
"bandSelectors": [
"test"
]
},
"functionName": "Image.select"
})

Does anybody have an idea how to solve this error?

Best Answer

The problem was that the max reducer renames the bandname to "max" It had to be renamed again.

trainingMap = ee.FeatureCollection('users/xxx/xxx')
trainingMapReduced = trainingMap.reduceToImage([RESPONSE], 'max')
trainingMapReduced = trainingMapReduced.rename(RESPONSE)