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(image,{min:0,max:0.60,bands:['swir1','nir','red']},"Image");
Map.addLayer(predictions.select("urban"), {min:0.05,max:0.95,palette:"white,gray,black"},'urban');
Map.addLayer(predictions.select("water"), {min:0.05,max:0.95,palette:"white,gray,black"},'water');
Map.addLayer(predictions.select("barren"), {min:0.05,max:0.95,palette:"white,gray,black"},'barren');
Map.addLayer(predictions.select("forest"), {min:0.05,max:0.95,palette:"white,gray,black"},'forest');
Map.addLayer(predictions.select("cropland"), {min:0.05,max:0.95,palette:"white,gray,black"},'cropland');

Leave a Reply