forked from TensorStack-AI/TensorStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageTensor.cs
More file actions
165 lines (142 loc) · 5.63 KB
/
ImageTensor.cs
File metadata and controls
165 lines (142 loc) · 5.63 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright (c) TensorStack. All rights reserved.
// Licensed under the Apache 2.0 License.
using System;
namespace TensorStack.Common.Tensor
{
/// <summary>
/// ImageTensor to handle Tensor data as an image.
/// Implements the <see cref="Tensor{float}" />
/// </summary>
/// <seealso cref="Tensor{float}" />
public class ImageTensor : Tensor<float>
{
/// <summary>
/// Initializes a new instance of the <see cref="ImageTensor"/> class.
/// </summary>
/// <param name="tensor">The tensor.</param>
public ImageTensor(Tensor<float> tensor)
: base(tensor.Memory, tensor.Dimensions)
{
ThrowIfInvalid();
}
/// <summary>
/// Initializes a new instance of the <see cref="ImageTensor"/> class.
/// </summary>
/// <param name="dimensions">The dimensions.</param>
public ImageTensor(ReadOnlySpan<int> dimensions)
: base(dimensions)
{
ThrowIfInvalid();
}
/// <summary>
/// Gets the channel count (RGB, RGBA etc).
/// </summary>
public int Channels => Dimensions[1];
/// <summary>
/// Gets the image height.
/// </summary>
public int Height => Dimensions[2];
/// <summary>
/// Gets the image width.
/// </summary>
public int Width => Dimensions[3];
/// <summary>
/// Normalizes the tensor values from range -1 to 1 to 0 to 1.
/// </summary>
/// <param name="imageTensor">The image tensor.</param>
public void NormalizeZeroToOne()
{
this.NormalizeOneOneToZeroOne();
}
/// <summary>
/// Normalizes the tensor values from range 0 to 1 to -1 to 1.
/// </summary>
/// <param name="imageTensor">The image tensor.</param>
public void NormalizeOneToOne()
{
this.NormalizeZeroOneToOneOne();
}
/// <summary>
/// Gets a TensorSpan with the specified channels. (1 = Greyscale, 3 = RGB, 4 = RGBA)
/// </summary>
/// <param name="count">The channels count.</param>
/// <returns>TensorSpan<System.Single>.</returns>
public TensorSpan<float> GetChannels(int channels)
{
ArgumentOutOfRangeException.ThrowIfGreaterThan(channels, Channels);
ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(channels, 0);
if (Channels == channels)
return this.AsTensorSpan();
var channelSize = Height * Width;
var channelDimensions = new int[] { 1, channels, Height, Width };
return new TensorSpan<float>(Memory.Span.Slice(0, channelSize * channels), channelDimensions);
}
/// <summary>
/// Gets the specified channel. (1=R, 2=G, 3=B, 4=A)
/// </summary>
/// <param name="channel">The channel.</param>
/// <returns>Span<System.Single>.</returns>
public Span<float> GetChannel(int channel)
{
ArgumentOutOfRangeException.ThrowIfGreaterThan(channel, Channels);
ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(channel, 0);
var channelSize = Height * Width;
var startIndex = channelSize * (channel - 1);
return Memory.Span.Slice(startIndex, channelSize);
}
/// <summary>
/// Updates the channel. (1=R, 2=G, 3=B, 4=A)
/// </summary>
/// <param name="channel">The channel.</param>
/// <param name="channelData">The channel data.</param>
public void UpdateChannel(int channel, ReadOnlySpan<float> channelData)
{
var channelSpan = GetChannel(channel);
for (int i = 0; i < channelSpan.Length; i++)
{
channelSpan[i] = channelData[i];
}
OnTensorDataChanged();
}
/// <summary>
/// Updates the alpha channel with the one from the specified tensor.
/// </summary>
/// <param name="tensor">The tensor.</param>
public void UpdateAlphaChannel(ImageTensor tensor)
{
var source = tensor.GetChannel(tensor.Channels);
UpdateChannel(Channels, source);
}
/// <summary>
/// Resizes the ImageTensor
/// </summary>
/// <param name="width">The target width in pixels.</param>
/// <param name="height">The target height in pixels..</param>
/// <param name="resizeMode">The resize mode.</param>
public void Resize(int width, int height, ResizeMode resizeMode)
{
UpdateTensor(this.ResizeImage(width, height, resizeMode));
}
/// <summary>
/// Clones as ImageTensor.
/// </summary>
/// <returns>ImageTensor.</returns>
public ImageTensor CloneAs()
{
return Clone().AsImageTensor();
}
/// <summary>
/// Throws if Dimensions are invalid.
/// </summary>
/// <param name="dimensions">The dimensions.</param>
protected void ThrowIfInvalid()
{
ArgumentOutOfRangeException.ThrowIfGreaterThan(Dimensions[0], 1, "Batch");
ArgumentOutOfRangeException.ThrowIfEqual(Channels, 2, nameof(Channels));
ArgumentOutOfRangeException.ThrowIfLessThan(Channels, 1, nameof(Channels));
ArgumentOutOfRangeException.ThrowIfGreaterThan(Channels, 4, nameof(Channels));
ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(Height, 0, nameof(Height));
ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(Width, 0, nameof(Width));
}
}
}