在 Enumerable 上调用 .ToArray 会损坏 Enumerable

Calling .ToArray on an Enumerable corrupts the Enumerable

我不确定这是否特定于 ML.NET,但它确实发生在它的上下文中。

我正在使用 ML.NET 对一些图像进行分类。我意识到无论我是否对结果 IEnumerable 调用 .ToArray() 都会造成严重的差异。前者导致所有数组元素与最后一个元素相同。

IEnumerable<ImageData> dataCollection = imagePaths.Select(path => new ImageData(path));
IDataView targetDataView = _mlContext.Data.LoadFromEnumerable(dataCollection);
IDataView predictionView = _transformerModel.Transform(targetDataView); 
return _mlContext.Data.CreateEnumerable<ImagePrediction>(predictionView, true).ToArray();

在上面显示的示例中,生成的预测都将其图像路径设置为 imagePaths 中的最后一个图像路径。

我不认为这是有意为之的行为。是什么原因造成的,我怎样才能安全地防止这种情况发生?目前我决定不打电话给 .ToArray(),但我想了解更多关于这个问题的信息。

问题似乎出在预测引擎中限制内存使用的位置,row 根据 reuseRowObject 重用。因此,当调用 ToList()ToArray() 方法时,只有最后一项用于投影 list/array.

public IEnumerable<TDst> RunPipe(bool reuseRowObject)
{
    var curCounter = _counter;
    using (var cursor = _cursorablePipe.GetCursor())
    {
        TDst row = null;
        while (cursor.MoveNext())
        {
            if (!reuseRowObject || row == null)
                row = new TDst();

            cursor.FillValues(row);
            yield return row;
            if (curCounter != _counter)
                throw Contracts.Except("An attempt was made to keep iterating after the pipe has been reset.");
        }
    }
}

调用方是 CreateEnumerable(),您明确将 reuseRowObject 设置为 true

public IEnumerable<TRow> CreateEnumerable<TRow>(IDataView data, bool reuseRowObject,
    bool ignoreMissingColumns = false, SchemaDefinition schemaDefinition = null)
    where TRow : class, new()
{
    _env.CheckValue(data, nameof(data));
    _env.CheckValueOrNull(schemaDefinition);

    var engine = new PipeEngine<TRow>(_env, data, ignoreMissingColumns, schemaDefinition);
    return engine.RunPipe(reuseRowObject);
}

reuseRowObject 设置为 false 应该可以解决您的问题。