ML.NET fails with 'KeyNotFoundException'

SOmetimes, when training the model with ML.NET, you might expirience follwoing error:

System.Collections.Generic.KeyNotFoundException: 'The given key '5' was not present in the dictionary.'

This exception is thrown for example within following statement:

mlContext.MulticlassClassification.CrossValidate(cvDataView, pipeline,                                     numberOfFolds: 5, labelColumnName: "LabelAsKey");

This happen if you have previously trained the model with enabled caching:

ImageClassificationTrainer.Options()
{
   ReuseTrainSetBottleneckCachedValues = true,
   ReuseValidationSetBottleneckCachedValues = true,
   TrainSetBottleneckCachedValuesFileName = "bottleneck_train_cache.cac",
   ValidationSetBottleneckCachedValuesFileName = "bottleneck_valid_cache.cac",
}

However, if you remove some images (labels), the ML.NET will try to load them and reference to cached values. In this case, delete all cache folders and files, which are usually located outputs cache folder.


comments powered by Disqus