1

I am trying to run a Random Forest Model and was running into memory issues. I was able to work around this by exporting and importing but now I am getting the following error. I have broken the scripts up so this is the second script that include the large composite image I created that I want to classify. Any idea on the best way to convert my featurecollection into float?

Dictionary (Error)
Property 'Class' of feature '00000000000000000000_0': Invalid type.
Expected type: Float.
Actual type: String.
Actual value: 1

My script is below and also found in the link (I believe I shared my assets) https://code.earthengine.google.com/7e8fbd7eeb7486fbe3fae2e2380f1e3a:

    //Step 2 Import Predictor Variable composite into this script to work around
//memory issues
//Map.addLayer(NSSentComp);
//Calculate mean and standard deviation within cluster segments

//Display NSSentComp to confirm it was exported/imported correctly
Map.addLayer(NSSentComp)
//SEGMENTATION - Set Sentinel-2 4 band mosaic for segmentation
//Set seeds for segmentation and apply softening
var seeds = ee.Algorithms.Image.Segmentation.seedGrid(36, 'hex');
var kernel = ee.Kernel.gaussian(3);
//Use RGBI and slope bands to create objects.
var segcomp = NSSentComp.select(['B2_median', 'B3_median', 'B4_median', 'B8_median', 'slope']).bandNames();
print(segcomp);
//Smooth the segmented image
var segcompsmoo = NSSentComp.convolve(kernel);

//Segment Sent/Slope Composite
var snic2 = ee.Algorithms.Image.Segmentation.SNIC({
  image: segcompsmoo,
  size:6,
  compactness: 0.2,  
  connectivity: 4,
  neighborhoodSize: 64,
  seeds: seeds
}).reproject({crs: 'EPSG: 26920',
scale: 5
});
var clusters2 = snic2.select('clusters')
Map.addLayer(snic2.randomVisualizer(), null, 'clusters2', false);
print(clusters2, "clusters2")
//Calculate stats per cluster for all predictor variables calculating mean and Stdev
var allStats = NSSentComp.reduceConnectedComponents({
  reducer: ee.Reducer.mean().combine({
 reducer2:ee.Reducer.stdDev(),
  sharedInputs:true
  }),
  labelBand: 'clusters2',
  maxSize: 512  
  })
print(allStats, "allstats")
var Clusterstats = allStats; 
print (Clusterstats, "Clusterstats");  //print to check bands were created as expected
//Map.addLayer(Clusterstats);

//Import training samples and train reduced image above

var trainVector = ee.FeatureCollection ('projects/ee-jrgallop/assets/RFTrainingDT');
var trainPoints = Clusterstats.sampleRegions({
  collection: trainVector,
  scale: 5,  //This is the scale we want the clusters to be processed at
  tileScale: 16, //want tilesize to be large enough to contain clusters, but too large will produce memory errors
  geometries: true,  //adding this enables you to export to asset
  });
  //print(trainPoints, "trainPoints")
  // Export to Asset requires the feature collection to have geometries, 
Export.table.toAsset({
  collection: trainPoints,
  description:'trainPoints',
  assetId: 'trainPoints',
});
  //Split training points into training and validation
  //var trainPoints = Table;
  var trainPoints = trainPoints.randomColumn();
  var split = 0.8;
  var training = trainPoints.filter(ee.Filter.lt('random', split));  //80% training
var validation = trainPoints.filter(ee.Filter.gte('random', split));  //20% validation
//print(trainPoints)
//Train the Classifier

var predictionVar = 

['B2_median_mean', 'B2_median_stdDev', 'B3_median_mean', 'B3_median_stdDev', 'B4_median_mean','B4_median_stdDev', 
'B8_median_mean','B8_median_stdDev','slope_mean', 'slope_stdDev','DEV150_mean', 'DEV150_stdDev', 'DEV500_mean', 
'DEV500_stdDev','DEV1000_mean', 'DEV1000_stdDev', 'WAM_mean', 'WAM_stdDev','REIP_mean', 'REIP_stdDev', 'NARI_mean', 
'NARI_stdDev','NDWI_mean', 'NDWI_stdDev', 'EVI_mean', 'EVI_stdDev', 'ndvi_mean', 'ndvi_stdDev', 'CHM_mean', 
'CHM_stdDev'];

var trainedRF = ee.Classifier.smileRandomForest({numberOfTrees: 100}).train({
  features: training,
  classProperty: 'Class',  //Name of the field with the class labels
  inputProperties:predictionVar
});
print (trainedRF.explain());
//Classify

var classifiedRF = Clusterstats.classify(trainedRF).float();

//Map.addLayer(classifiedRF)

1 Answer 1

1

As it can be observed in the error message, your Feature Collection named training has a property with invalid type. You can fix this with following function before trainedRF variable.

var training = training.map(function (ele){
  
  var class_as_number = ee.Number.parse((ele.get('Class')));
  
  return ele.set('Class', class_as_number);
  
});

After that, your code run without any problem (dictionary is printed as expected); as it can be observed in following picture.

enter image description here

Complete code here.

2
  • this works! Great! Thanks!
    – Erioderma
    Jan 16 at 0:50
  • So, could you mark as accepted the answer? Thanks.
    – xunilk
    Jan 16 at 3:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.