-
Notifications
You must be signed in to change notification settings - Fork 35
/
StableDiffusionTest.cpp
55 lines (45 loc) · 1.61 KB
/
StableDiffusionTest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "pch.h"
#include "CppUnitTest.h"
#include "Storage/FileIO.h"
using namespace Axodox::Graphics;
using namespace Axodox::Storage;
using namespace Axodox::MachineLearning::Sessions;
using namespace Axodox::MachineLearning::Imaging::StableDiffusion;
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
using namespace std;
namespace Axodox::MachineLearning::Test
{
TEST_CLASS(StableDiffusionTest)
{
TEST_METHOD(TestStableDiffusion)
{
StableDiffusionDirectorySessionParameters sessionParameters{ lib_folder() / "../../../models/stable_diffusion" };
StableDiffusionOptions options{};
//Create text embedding
{
TextEmbedder textEmbedder{ sessionParameters };
auto positiveEmbedding = textEmbedder.ProcessPrompt("a clean bedroom");
auto negativeEmbedding = textEmbedder.ProcessPrompt("blurry, render");
options.TextEmbeddings.Tensor = negativeEmbedding.Concat(positiveEmbedding);
options.TextEmbeddings.Weights = { -1.f, 1.f };
}
//Run StableDiffusion
Tensor image;
{
StableDiffusionInferer uNet{ sessionParameters };
image = uNet.RunInference(options);
}
//Decode VAE
{
VaeDecoder vaeDecoder{ sessionParameters };
image = vaeDecoder.DecodeVae(image);
}
//Save result
{
auto imageTexture = image.ToTextureData(ColorNormalization::LinearPlusMinusOne);
auto imageBuffer = imageTexture[0].ToBuffer();
write_file(lib_folder() / "stablediffusion.png", imageBuffer);
}
}
};
}