ImageClassification.Fit-IndexOutOfRangeException
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ImageClassification.Fit-IndexOutOfRangeException相关的知识,希望对你有一定的参考价值。
我已经使用Fit方法来训练我的模型,这会生成异常“索引在数组的边界之外”。
我创建了一个库.Net Core 3.1和一个UnitTesting(.Net Core 3.1),其中UnitTesting在我的库中将属性称为“ Training()”。
public void Training()
{
MLContext mlc = new MLContext();
try
{
IEnumerable<ImageData> images = LoadImagesFromDirectory(folder: AssetsRelativePath, useFolderNameAsLabel: true);
IDataView imageData = mlc.Data.LoadFromEnumerable(images);
IDataView shuffledData = mlc.Data.ShuffleRows(imageData);
var preprocessingPipeline = mlc.Transforms.Conversion.MapValueToKey(inputColumnName: "Label", outputColumnName: "LabelAsKey")
.Append(mlc.Transforms.LoadImages(outputColumnName: "Image", imageFolder: AssetsRelativePath, useImageType: false, inputColumnName: "ImagePath"));
IDataView preProcessedData = preprocessingPipeline.Fit(shuffledData).Transform(shuffledData);
TrainTestData trainSplit = mlc.Data.TrainTestSplit(data: preProcessedData, testFraction: 0.3);
TrainTestData validationTestSplit = mlc.Data.TrainTestSplit(trainSplit.TestSet);
IDataView trainSet = trainSplit.TrainSet;
IDataView validationSet = validationTestSplit.TrainSet;
IDataView testSet = validationTestSplit.TestSet;
var trainingPipeline = mlc.Model.ImageClassification(
featuresColumnName: "Image",
labelColumnName: "LabelAsKey",
arch: ImageClassificationEstimator.Architecture.ResnetV2101,
epoch: 100,
batchSize: 10,
testOnTrainSet: false,
metricsCallback: (metrics) => Logger.Debug(metrics),
validationSet: validationSet,
reuseTrainSetBottleneckCachedValues: true,
reuseValidationSetBottleneckCachedValues: true,
disableEarlyStopping: false
)
.Append(mlc.Transforms.Conversion.MapKeyToValue("PredictedLabel"));
ITransformer trainedModel = trainingPipeline.Fit(trainSet);
}
catch{
}
}
我收到例外:IndexOutOfRangeException
答案
可能是您的某些图像无法加载?也许在训练之前尝试将trainSet
保存到磁盘(作为文本或二进制文件),然后查看它是否继续抛出?
基本上,我怀疑数据本身已损坏,并且第一次扫描数据时,您立即发现了这一点。
以上是关于ImageClassification.Fit-IndexOutOfRangeException的主要内容,如果未能解决你的问题,请参考以下文章