9

Training a classifier in Google Earth Engine is slow and can sometimes show temporary errors like "Capacity exceeded". To combat this, I would like to train the classifier, store it, and then load it and use it in other projects.

I haven't seen an API call to do this, but maybe I'm missing something.

(Or maybe it's not possible now, but one day this question will get a positive answer)

2 Answers 2

5

It's actually not straight forward but possible if you just serialize the classifier object and load it again in the future.

var classifier_serialized = ee.Serializer.toJSON(classifier)
Export.table.toAsset(ee.FeatureCollection(empty_feature.set('classifier',json)),desc,AssetName)

// Load using this
var json = ee.String(ee.Feature(ee.FeatureCollection(assetName).first()).get('classifier'))
var classifier = ee.Deserializer.fromJSON(json)

"empty_feature" is just ee.Feature, empty as the name specified.

Maybe in the future, there will be some APIs to simplify this :)

Edited: If the first approach is not saving learned weights try another approach only works for Random Forest.

var trees = ee.List(ee.Dictionary(classifier.explain()).get('trees'))
var dummy = ee.Feature()
var col = ee.FeatureCollection(trees.map(function(x){return dummy.set('tree',x)}))
Export.table.toAsset(col,'save_classifier',AssetName)

// Load classifier
var trees = ee.FeatureCollection(AssetName).aggregate_array('tree').aside(print)
var classifier = ee.Classifier.decisionTreeEnsemble(trees)
2
  • From my understanding, ee.Serializer.toJSON only encodes the instructions that Earth Engine should perform to reinstate the model. Earth Engine will have to train again once the reinstated model is called for some computation. So, this does not get around the issue of having to retrain. Commented Nov 29, 2020 at 4:27
  • @KelMarkert Hi I have added one more approach, Please check that out. There are some memory limitations also, 32 or 16MB something. Commented Dec 4, 2020 at 12:26
3

If you are not adverse to using Python, there is some functionality to train a Random Forest model locally, encode the trees as a FeatureCollection, and load in the pre-trained model as ee.Classifier.decisionTreeEnsemble. An example of how to do this can be found here: https://github.com/giswqs/geemap/blob/master/examples/notebooks/local_rf_training.ipynb.

It should be noted that this only supports "small" models meaning either not too many trees or not so wide/deep of trees (this is an EE limitation). Also, his approach only works for Decision Trees or a Random Forest models so if you would like to use something like SVM or Naive Bayes, then your only option currently is to retrain using Earth Engine every time.

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.