Skip to content
This repository was archived by the owner on Nov 27, 2024. It is now read-only.

Commit f6a2bba

Browse files
author
James Tayler
committed
output logs and images in integration tests
1 parent 049bb31 commit f6a2bba

File tree

7 files changed

+58
-14
lines changed

7 files changed

+58
-14
lines changed

.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@
2121
**/obj
2222
**/secrets.dev.yaml
2323
**/values.dev.yaml
24+
docker-test-output/
2425
LICENSE
2526
README.md

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,3 +344,4 @@ test/TensorFlowNET.Examples/mnist
344344
site/
345345

346346
/OnnxStack.WebUI/wwwroot/images/Results/*
347+
docker-test-output/*

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ RUN curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.d
99
RUN git clone https://huggingface.co/runwayml/stable-diffusion-v1-5 -b onnx
1010

1111
COPY . .
12-
RUN ls -ltr
1312
RUN dotnet build OnnxStackCore.sln
14-
RUN dotnet test OnnxStackCore.sln
13+
14+
ENTRYPOINT ["dotnet", "test", "OnnxStackCore.sln"]

OnnxStack.IntegrationTests/OnnxStack.IntegrationTests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
1515
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
1616
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.1" />
17+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="7.0.0" />
1718
<PackageReference Include="Microsoft.ML.OnnxRuntime" Version="1.16.2" />
1819
<PackageReference Include="Microsoft.ML.OnnxRuntime.Extensions" Version="0.9.0" />
1920
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2"/>
2021
<PackageReference Include="xunit" Version="2.4.2"/>
22+
<PackageReference Include="Xunit.Extensions.Logging" Version="1.1.0" />
2123
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
2224
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2325
<PrivateAssets>all</PrivateAssets>

OnnxStack.IntegrationTests/StableDiffusionTests.cs

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
1+
using System.Security.Cryptography;
12
using FluentAssertions;
3+
using FluentAssertions.Execution;
24
using Microsoft.Extensions.DependencyInjection;
5+
using Microsoft.Extensions.Logging;
36
using OnnxStack.Core;
47
using OnnxStack.StableDiffusion.Common;
58
using OnnxStack.StableDiffusion.Config;
69
using OnnxStack.StableDiffusion.Enums;
10+
using SixLabors.ImageSharp;
711
using Xunit.Abstractions;
812

913
namespace OnnxStack.IntegrationTests;
1014

1115
public class StableDiffusionTests
1216
{
13-
private readonly ITestOutputHelper _testOutputHelper;
1417
private readonly IStableDiffusionService _stableDiffusion;
18+
private readonly ILogger<StableDiffusionTests> _logger;
1519

1620
public StableDiffusionTests(ITestOutputHelper testOutputHelper)
1721
{
18-
_testOutputHelper = testOutputHelper;
19-
2022
var services = new ServiceCollection();
21-
services.AddLogging();
23+
services.AddLogging(builder => builder.AddConsole());
24+
services.AddLogging(builder => builder.AddXunit(testOutputHelper));
2225
services.AddOnnxStack();
2326
services.AddOnnxStackStableDiffusion();
2427
var provider = services.BuildServiceProvider();
2528
_stableDiffusion = provider.GetRequiredService<IStableDiffusionService>();
29+
_logger = provider.GetRequiredService<ILogger<StableDiffusionTests>>();
2630
}
2731

2832
[Fact]
@@ -32,6 +36,7 @@ public async Task GivenStableDiffusion15_WhenLoadModel_ThenModelIsLoaded()
3236
var model = _stableDiffusion.Models.Single(m => m.Name == "StableDiffusion 1.5");
3337

3438
//act
39+
_logger.LogInformation("Attempting to load model {0}", model.Name);
3540
var isModelLoaded = await _stableDiffusion.LoadModel(model);
3641

3742
//assert
@@ -43,6 +48,7 @@ public async Task GivenTextToImage_WhenInference_ThenImageGenerated()
4348
{
4449
//arrange
4550
var model = _stableDiffusion.Models.Single(m => m.Name == "StableDiffusion 1.5");
51+
_logger.LogInformation("Attempting to load model {0}", model.Name);
4652
await _stableDiffusion.LoadModel(model);
4753

4854
var prompt = new PromptOptions
@@ -60,23 +66,49 @@ public async Task GivenTextToImage_WhenInference_ThenImageGenerated()
6066
Height = 512,
6167
InferenceSteps = 10,
6268
GuidanceScale = 7.0f,
63-
Seed = -1
69+
Seed = 1
6470
};
6571

6672
var steps = 0;
6773

6874
//act
6975
var image = await _stableDiffusion.GenerateAsImageAsync(model, prompt, scheduler, (currentStep, totalSteps) =>
7076
{
71-
_testOutputHelper.WriteLine($"Step {currentStep}/{totalSteps}");
77+
_logger.LogInformation($"Step {currentStep}/{totalSteps}");
7278
steps++;
7379
});
7480

81+
var imagesDirectory = Path.Combine(Directory.GetCurrentDirectory(), "images");
82+
if (!Directory.Exists(imagesDirectory))
83+
{
84+
_logger.LogInformation($"Creating directory {imagesDirectory}");
85+
Directory.CreateDirectory(imagesDirectory);
86+
}
87+
else
88+
{
89+
_logger.LogInformation($"Directory {imagesDirectory} already exists");
90+
}
91+
92+
var fileName = $"{imagesDirectory}/{nameof(GivenTextToImage_WhenInference_ThenImageGenerated)}-{DateTime.Now:yyyyMMddHHmmss}.png";
93+
_logger.LogInformation($"Saving generated image to {fileName}");
94+
await image.SaveAsPngAsync(fileName);
95+
7596
//assert
76-
steps.Should().Be(10);
77-
image.Should().NotBeNull();
78-
image.Size.IsEmpty.Should().BeFalse();
79-
image.Width.Should().Be(512);
80-
image.Height.Should().Be(512);
97+
using (new AssertionScope())
98+
{
99+
steps.Should().Be(10);
100+
image.Should().NotBeNull();
101+
image.Size.IsEmpty.Should().BeFalse();
102+
image.Width.Should().Be(512);
103+
image.Height.Should().Be(512);
104+
105+
File.Exists(fileName).Should().BeTrue();
106+
var md5 = MD5.Create();
107+
var hash = md5.ComputeHash(File.ReadAllBytes(fileName));
108+
var hashString = string.Join("", hash.Select(b => b.ToString("X2")));
109+
_logger.LogInformation($"MD5 Hash of generated image: {hashString}");
110+
111+
hashString.Should().Be("E518D0E4F67CBD5E93513574D30F3FD7");
112+
}
81113
}
82114
}

OnnxStackCore.sln

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OnnxStack.StableDiffusion",
66
EndProject
77
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OnnxStack.IntegrationTests", "OnnxStack.IntegrationTests\OnnxStack.IntegrationTests.csproj", "{9336EF88-5290-451F-972C-CF9550EF545D}"
88
EndProject
9+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SolutionItems", "SolutionItems", "{556184C8-7C99-4F06-86EF-EAAF52165252}"
10+
ProjectSection(SolutionItems) = preProject
11+
Dockerfile = Dockerfile
12+
.dockerignore = .dockerignore
13+
.gitignore = .gitignore
14+
docker-compose.yml = docker-compose.yml
15+
EndProjectSection
16+
EndProject
917
Global
1018
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1119
Debug|Any CPU = Debug|Any CPU

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ services:
44
app:
55
build: .
66
volumes:
7-
- ./output:/app/images
7+
- "./docker-test-output:/app/OnnxStack.IntegrationTests/bin/Debug/net7.0/images"

0 commit comments

Comments
 (0)