use your AI model in earth engine

Import your model from the AI platform

ee.Model.fromAiPlatformPredictor is used to import your model from the ai platform.

model.predictImage is used to classify your image

Note that you need cloud credits to run the model!.

// set variables
var bandsIn = ee.List(['B1','B2','B3','B4','B5','B6','B7','B8','B8A','B9','B10','B11','B12']);
var bandsOut = ee.List(['cb','blue','green','red','re1','re2','re3','nir','re4','waterVapor','cirrus','swir1','swir2']);
var bandsOutSort = ee.List(['cb','blue','green','red','re1','re2','re3','nir','re4','waterVapor','cirrus','swir1','swir2','ndvi']).sort();
var label = 'land_class';

// get the image
var image = ee.Image("COPERNICUS/S2/20171220T034151_20171220T034145_T48QWJ").select(bandsIn,bandsOut).divide((10000)).toFloat();

// calculate ndvi and add bands
var ndvi = image.normalizedDifference(['nir','red']).rename(['ndvi']);
var image = image.addBands(ndvi).select(bandsOutSort);
// Load the trained model and use it for prediction.
var model = ee.Model.fromAiPlatformPredictor({
  projectName : 'your project',
  modelName : 'your model',
  version : 'your version',
  inputTileSize : [12, 12],
  proj : ee.Projection('EPSG:4326').atScale(30),
  fixInputProj : true,
  outputBands : {'land_class': {
  'type': ee.PixelType.float(),
  'dimensions': 1

// predict using the model
var predictions = model.predictImage(image.toArray()).arrayFlatten([['urban','water',"barren","forest","cropland"]]);

Map.addLayer("urban"), {min:0.05,max:0.95,palette:"white,gray,black"},'urban');
Map.addLayer("water"), {min:0.05,max:0.95,palette:"white,gray,black"},'water');
Map.addLayer("barren"), {min:0.05,max:0.95,palette:"white,gray,black"},'barren');
Map.addLayer("forest"), {min:0.05,max:0.95,palette:"white,gray,black"},'forest');
Map.addLayer("cropland"), {min:0.05,max:0.95,palette:"white,gray,black"},'cropland');

One comment

  1. Thank you very much, Very interesting nowadays. are you voluntary to support/help me? because I am working on research for publication related to this.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s