forked from SciSharp/TensorFlow.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNDArrayConverter.cs
More file actions
119 lines (105 loc) · 4.48 KB
/
Copy pathNDArrayConverter.cs
File metadata and controls
119 lines (105 loc) · 4.48 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Tensorflow.NumPy
{
public class NDArrayConverter
{
public unsafe static T Scalar<T>(NDArray nd) where T : unmanaged
=> nd.dtype switch
{
TF_DataType.TF_BOOL => Scalar<T>(*(bool*)nd.data),
TF_DataType.TF_UINT8 => Scalar<T>(*(byte*)nd.data),
TF_DataType.TF_FLOAT => Scalar<T>(*(float*)nd.data),
TF_DataType.TF_INT32 => Scalar<T>(*(int*)nd.data),
TF_DataType.TF_INT64 => Scalar<T>(*(long*)nd.data),
TF_DataType.TF_DOUBLE => Scalar<T>(*(double*)nd.data),
_ => throw new NotImplementedException(nameof(NDArrayConverter))
};
static T Scalar<T>(byte input)
=> Type.GetTypeCode(typeof(T)) switch
{
TypeCode.Byte => (T)Convert.ChangeType(input, TypeCode.Byte),
TypeCode.Int32 => (T)Convert.ChangeType(input, TypeCode.Int32),
TypeCode.Single => (T)Convert.ChangeType(input, TypeCode.Single),
TypeCode.Double => (T)Convert.ChangeType(input, TypeCode.Double),
_ => throw new NotImplementedException(nameof(NDArrayConverter))
};
static T Scalar<T>(float input)
=> Type.GetTypeCode(typeof(T)) switch
{
TypeCode.Byte => (T)Convert.ChangeType(input, TypeCode.Byte),
TypeCode.Int32 => (T)Convert.ChangeType(input, TypeCode.Int32),
TypeCode.Single => (T)Convert.ChangeType(input, TypeCode.Single),
TypeCode.Double => (T)Convert.ChangeType(input, TypeCode.Double),
_ => throw new NotImplementedException(nameof(NDArrayConverter))
};
static T Scalar<T>(int input)
=> Type.GetTypeCode(typeof(T)) switch
{
TypeCode.Byte => (T)Convert.ChangeType(input, TypeCode.Byte),
TypeCode.Int64 => (T)Convert.ChangeType(input, TypeCode.Int64),
TypeCode.Single => (T)Convert.ChangeType(input, TypeCode.Single),
TypeCode.Double => (T)Convert.ChangeType(input, TypeCode.Double),
_ => throw new NotImplementedException(nameof(NDArrayConverter))
};
static T Scalar<T>(long input)
=> Type.GetTypeCode(typeof(T)) switch
{
TypeCode.Byte => (T)Convert.ChangeType(input, TypeCode.Byte),
TypeCode.Int32 => (T)Convert.ChangeType(input, TypeCode.Int32),
TypeCode.Single => (T)Convert.ChangeType(input, TypeCode.Single),
TypeCode.Double => (T)Convert.ChangeType(input, TypeCode.Double),
_ => throw new NotImplementedException(nameof(NDArrayConverter))
};
public static unsafe Array ToMultiDimArray<T>(NDArray nd) where T : unmanaged
{
var ret = Array.CreateInstance(typeof(T), nd.shape.as_int_list());
var addr = ret switch
{
T[] array => Addr(array),
T[,] array => Addr(array),
T[,,] array => Addr(array),
T[,,,] array => Addr(array),
T[,,,,] array => Addr(array),
T[,,,,,] array => Addr(array),
_ => throw new NotImplementedException(nameof(NDArrayConverter))
};
System.Buffer.MemoryCopy(nd.data.ToPointer(), addr, nd.bytesize, nd.bytesize);
return ret;
}
#region multiple array
static unsafe T* Addr<T>(T[] array) where T : unmanaged
{
fixed (T* a = &array[0])
return a;
}
static unsafe T* Addr<T>(T[,] array) where T : unmanaged
{
fixed (T* a = &array[0, 0])
return a;
}
static unsafe T* Addr<T>(T[,,] array) where T : unmanaged
{
fixed (T* a = &array[0, 0, 0])
return a;
}
static unsafe T* Addr<T>(T[,,,] array) where T : unmanaged
{
fixed (T* a = &array[0, 0, 0, 0])
return a;
}
static unsafe T* Addr<T>(T[,,,,] array) where T : unmanaged
{
fixed (T* a = &array[0, 0, 0, 0, 0])
return a;
}
static unsafe T* Addr<T>(T[,,,,,] array) where T : unmanaged
{
fixed (T* a = &array[0, 0, 0, 0, 0, 0])
return a;
}
#endregion
}
}