-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathCLIPImage.cs
More file actions
47 lines (44 loc) · 1.94 KB
/
CLIPImage.cs
File metadata and controls
47 lines (44 loc) · 1.94 KB
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
// Copyright (c) TensorStack. All rights reserved.
// Licensed under the Apache 2.0 License.
using TensorStack.Common.Tensor;
namespace TensorStack.Common.Vision
{
/// <summary>
/// CLIPImage class for preprocessing images to prepare them for use with a CLIP model
/// </summary>
public class CLIPImage
{
/// <summary>
/// Resizes and normalizes an ImageTensor for CLIP input.
/// </summary>
/// <param name="input">The input.</param>
/// <param name="width">The width.</param>
/// <param name="height">The height.</param>
/// <returns>ImageTensor.</returns>
public static ImageTensor Process(ImageTensor input, int width = 224, int height = 224, ResizeMode ResizeMode = ResizeMode.Stretch)
{
return Process(input, new CLIPImageOptions(width, height, ResizeMode));
}
/// <summary>
/// Resizes and normalizes an ImageTensor for CLIP input.
/// </summary>
/// <param name="input">The input.</param>
/// <param name="options">The options.</param>
/// <returns>ImageTensor.</returns>
public static ImageTensor Process(ImageTensor input, CLIPImageOptions options)
{
options ??= new CLIPImageOptions();
var resultTensor = input.ResizeImage(options.Width, options.Height, options.ResizeMode, options.ResizeMethod);
for (int x = 0; x < resultTensor.Width; x++)
{
for (int y = 0; y < resultTensor.Height; y++)
{
resultTensor[0, 0, y, x] = (resultTensor[0, 0, y, x] - options.Mean[0]) / options.StdDev[0];
resultTensor[0, 1, y, x] = (resultTensor[0, 1, y, x] - options.Mean[1]) / options.StdDev[1];
resultTensor[0, 2, y, x] = (resultTensor[0, 2, y, x] - options.Mean[2]) / options.StdDev[2];
}
}
return resultTensor;
}
}
}