Skip to content

Commit

Permalink
Improve trainer logging informativity (less spam, use operations for …
Browse files Browse the repository at this point in the history
…elapsed time counting)
  • Loading branch information
Neakita committed Sep 30, 2023
1 parent 410a875 commit 42b9835
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
37 changes: 25 additions & 12 deletions SightKeeper.Application/Training/ImagesExporter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Globalization;
using Serilog;
using SerilogTimings.Extensions;
using SightKeeper.Application.Annotating;
using SightKeeper.Domain.Model.Common;
using SightKeeper.Domain.Model.Detector;
Expand Down Expand Up @@ -46,15 +47,19 @@ public Task Export(
private readonly ScreenshotImageLoader _imageLoader;
private readonly ILogger _logger;

private Task ExportImages(string directoryPath, IReadOnlyCollection<Asset> assets, CancellationToken cancellationToken) =>
Task.WhenAll(assets.Select((asset, assetIndex) => ExportImage(directoryPath, asset, assetIndex, cancellationToken)));
private async Task ExportImages(string directoryPath, IReadOnlyCollection<Asset> assets, CancellationToken cancellationToken)
{
var operation = _logger.BeginOperation("Exporting images for {AssetsCount} assets", assets.Count);
await Task.WhenAll(assets.Select((asset, assetIndex) =>
ExportImage(directoryPath, asset, assetIndex, cancellationToken)));
operation.Complete();
}

private async Task ExportImage(string directoryPath, Asset asset, int assetIndex, CancellationToken cancellationToken)
{
var imagePath = Path.Combine(directoryPath, $"{assetIndex}.png");
var image = await _imageLoader.LoadAsync(asset.Screenshot, cancellationToken);
await ExportImage(imagePath, image.Content, cancellationToken);
_logger.Information("Exported image #{AssetIndex} to {Path}", assetIndex, imagePath);
}

private static Task ExportImage(string path, byte[] content, CancellationToken cancellationToken)
Expand All @@ -67,31 +72,39 @@ private static Task ExportImage(string path, byte[] content, CancellationToken c
}, cancellationToken);
}

private Task ExportLabels(
private async Task ExportLabels(
string directoryPath,
IEnumerable<Asset> assets,
IReadOnlyCollection<Asset> assets,
IReadOnlyCollection<ItemClass> itemClasses,
CancellationToken cancellationToken)
{
await LoadItems(assets, cancellationToken);
var operation = _logger.BeginOperation("Exporting labels for {AssetsCount} assets ({AssetsWithoutItems} without items) with {ItemsCount} items",
assets.Count,
assets.Count(asset => !asset.Items.Any()),
assets.SelectMany(asset => asset.Items).Count());
var itemClassesWithIndexes = itemClasses
.Select((itemClass, itemClassIndex) => (itemClass, itemClassIndex))
.ToDictionary(tuple => tuple.itemClass, tuple => (byte)tuple.itemClassIndex);
_logger.Debug("Item classes by indexes: {ItemClasses}", itemClassesWithIndexes);
return Task.WhenAll(assets.Select((asset, assetIndex) => ExportLabels(directoryPath, asset, assetIndex, itemClassesWithIndexes, cancellationToken)));
await Task.WhenAll(assets.Select((asset, assetIndex) => ExportLabels(directoryPath, asset, assetIndex, itemClassesWithIndexes, cancellationToken)));
operation.Complete();
}

private async Task LoadItems(IReadOnlyCollection<Asset> assets, CancellationToken cancellationToken)
{
var operation = _logger.BeginOperation("Loading items for {AssetsCount} assets", assets.Count);
foreach (var asset in assets)
await _assetsDataAccess.LoadItems(asset, cancellationToken);
operation.Complete();
}

private async Task ExportLabels(string directoryPath, Asset asset, int assetIndex, Dictionary<ItemClass, byte> itemClasses, CancellationToken cancellationToken)
{
await _assetsDataAccess.LoadItems(asset, cancellationToken);
if (!asset.Items.Any())
return;
var labelPath = Path.Combine(directoryPath, $"{assetIndex}.txt");
await ExportLabels(labelPath, asset, itemClasses, cancellationToken);
_logger.Information("Exported labels #{AssetIndex} to {Path} with {ItemsCount} annotations ({AnnotationClasses})",
assetIndex,
labelPath,
asset.Items.Count,
asset.Items.Select(item => item.ItemClass).Distinct());
}

private async Task ExportLabels(
Expand Down
1 change: 0 additions & 1 deletion SightKeeper.Application/Training/Trainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ private async Task ExportDataSet(Domain.Model.DataSet dataSet, CancellationToken
{
DataSetConfigurationParameters dataSetParameters = new(Path.GetFullPath(DataDirectoryPath), dataSet.ItemClasses);
await _dataSetConfigurationExporter.Export(DataSetPath, dataSetParameters, cancellationToken);
_logger.Information("Exported dataset parameters: {Parameters}", dataSetParameters);
}

private async Task ExportWeights(Weights weights, CancellationToken cancellationToken)
Expand Down

0 comments on commit 42b9835

Please sign in to comment.