Using convolutional neural networks part 3

Running Inference

In the previous post we uploaded the model to the Google AI platform. Now we can import the model into the Google Earth Engine and run the inference on our Landsat image

Use the code below or click here and replace the Project MODEL_NAME and VERSION_NAME

var l8 = ee.ImageCollection("LANDSAT/LC08/C01/T1_RT_TOA");

var BANDS = ['B2', 'B3', 'B4', 'B5', 'B6', 'B7'];

// import landsat 8 and filter for your period and time period of interest
var image = ee.Image(l8.filterBounds(geom)
  .filterDate("2018-01-01","2023-04-01")
  .filter(ee.Filter.lt("CLOUD_COVER",20))
  .first()).select(BANDS);


// set some ee.Model parameters
var PROJECT = 'yourProject';
var MODEL_NAME = 'yourModel';
var VERSION_NAME = 'yourVersion';


//# Load the trained model and use it for prediction.
var model = ee.Model.fromAiPlatformPredictor({
    'projectName': PROJECT,
    'modelName': MODEL_NAME,
    'version': VERSION_NAME,
    'inputTileSize': [128,128],
    'inputOverlapSize': [32,32],
    'proj': ee.Projection('EPSG:4326').atScale(30),
    'fixInputProj': true,
    'outputBands': {'landclass': {
        'type': ee.PixelType.float(),
        'dimensions': 1
      }
    }
});

//# run the predictions
var predictions = model.predictImage(image.toFloat().toArray());

//# find highest probability class
var predClasses = predictions.arrayArgmax() .arrayFlatten([['qa']]);

//# flatten probability array to image with bands  
var predProbs = predictions.arrayFlatten([['cloud','shadow','snow','water','land','nodata']]).toFloat();

//# mask out the clear class
var predClasses = predClasses.updateMask(predClasses.neq(4));

Map.addLayer(image,{min:0,max:0.3,bands:"B4,B3,B2"},"l8 toa");
Map.addLayer(image,{min:0,max:0.6,bands:"B6,B5,B4"},"l8 toa");
Map.addLayer(predClasses,{'min':0,'max':5,'palette':'yellow,purple,cyan,blue,green,black','opacity':0.7},"pred");

Leave a Reply