Skip to content

Commit 2d67df1

Browse files
committed
Add nested structure decoder.
1 parent acae9b3 commit 2d67df1

2 files changed

Lines changed: 274 additions & 7 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Runtime.CompilerServices;
4+
using System.Text;
5+
6+
namespace Tensorflow.Common.Types
7+
{
8+
public class NamedTuple
9+
{
10+
public string Name { get; set; }
11+
public Dictionary<string, object> ValueDict { get; set; }
12+
}
13+
}
Lines changed: 261 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,268 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
34
using System.Text;
5+
using Tensorflow.Util;
6+
using static Tensorflow.Binding;
47

58
namespace Tensorflow.Training.Saving.SavedModel
69
{
7-
//public class nested_structure_coder
8-
//{
9-
// public static List<object> decode_proto(StructuredValue proto)
10-
// {
11-
// return proto s
12-
// }
13-
//}
10+
internal interface ICodec
11+
{
12+
//bool CanEncode(StructuredValue value);
13+
bool CanDecode(StructuredValue value);
14+
//StructuredValue DoEecode(object value, Func<object, StructuredValue> encode_fn);
15+
object DoDecode(StructuredValue value, Func<StructuredValue, object> decode_fn);
16+
}
17+
public class nested_structure_coder
18+
{
19+
private static Dictionary<StructuredValue.KindOneofCase, ICodec> _codecs = null;
20+
public static object decode_proto(StructuredValue proto)
21+
{
22+
if(_codecs is null)
23+
{
24+
_codecs = new Dictionary<StructuredValue.KindOneofCase, ICodec>();
25+
_codecs[StructuredValue.KindOneofCase.ListValue] = new ListCodec();
26+
_codecs[StructuredValue.KindOneofCase.TupleValue] = new TupleCodec();
27+
_codecs[StructuredValue.KindOneofCase.DictValue] = new DictCodec();
28+
_codecs[StructuredValue.KindOneofCase.NamedTupleValue] = new NamedTupleCodec();
29+
_codecs[StructuredValue.KindOneofCase.Float64Value] = new Float64Codec();
30+
_codecs[StructuredValue.KindOneofCase.Int64Value] = new Int64Codec();
31+
_codecs[StructuredValue.KindOneofCase.StringValue] = new StringCodec();
32+
_codecs[StructuredValue.KindOneofCase.NoneValue] = new NoneCodec();
33+
_codecs[StructuredValue.KindOneofCase.BoolValue] = new BoolCodec();
34+
_codecs[StructuredValue.KindOneofCase.TensorShapeValue] = new TensorShapeCodec();
35+
_codecs[StructuredValue.KindOneofCase.TensorDtypeValue] = new TensorTypeCodec();
36+
_codecs[StructuredValue.KindOneofCase.TensorSpecValue] = new TensorSpecCodec();
37+
_codecs[StructuredValue.KindOneofCase.BoundedTensorSpecValue] = new BoundedTensorSpecCodec();
38+
_codecs[StructuredValue.KindOneofCase.TypeSpecValue] = new TypeSpecCodec();
39+
}
40+
41+
return decode_proto_internal(proto, x => decode_proto(x));
42+
}
43+
44+
public static object decode_proto_internal(StructuredValue proto, Func<StructuredValue, object> encode_fn)
45+
{
46+
Debug.Assert(_codecs[proto.KindCase].CanDecode(proto));
47+
return _codecs[proto.KindCase].DoDecode(proto, encode_fn);
48+
}
49+
}
50+
51+
internal class ListCodec : ICodec
52+
{
53+
public bool CanDecode(StructuredValue value)
54+
{
55+
return value.ListValue is not null;
56+
}
57+
58+
public object DoDecode(StructuredValue value, Func<StructuredValue, object> decode_fn)
59+
{
60+
return value.ListValue.Values.Select(x => decode_fn(x)).ToList();
61+
}
62+
}
63+
64+
internal class TupleCodec: ICodec
65+
{
66+
public bool CanDecode(StructuredValue value)
67+
{
68+
return value.TupleValue is not null;
69+
}
70+
71+
public object DoDecode(StructuredValue value, Func<StructuredValue, object> decode_fn)
72+
{
73+
return value.TupleValue.Values.Select(x => decode_fn(x)).ToArray();
74+
}
75+
}
76+
77+
internal class DictCodec : ICodec
78+
{
79+
public bool CanDecode(StructuredValue value)
80+
{
81+
return value.DictValue is not null;
82+
}
83+
84+
public object DoDecode(StructuredValue value, Func<StructuredValue, object> decode_fn)
85+
{
86+
return value.DictValue.Fields.ToDictionary(x => x.Key, x => decode_fn(x.Value));
87+
}
88+
}
89+
90+
internal class NamedTupleCodec : ICodec
91+
{
92+
public bool CanDecode(StructuredValue value)
93+
{
94+
return value.NamedTupleValue is not null;
95+
}
96+
97+
public object DoDecode(StructuredValue value, Func<StructuredValue, object> decode_fn)
98+
{
99+
var key_value_pairs = value.NamedTupleValue.Values;
100+
var items = key_value_pairs.ToDictionary(x => x.Key, x => decode_fn(x.Value));
101+
return new Common.Types.NamedTuple()
102+
{
103+
Name = value.NamedTupleValue.Name,
104+
ValueDict = items
105+
};
106+
}
107+
}
108+
109+
internal class Float64Codec : ICodec
110+
{
111+
public bool CanDecode(StructuredValue value)
112+
{
113+
return value.KindCase == StructuredValue.KindOneofCase.Float64Value;
114+
}
115+
116+
public object DoDecode(StructuredValue value, Func<StructuredValue, object> decode_fn)
117+
{
118+
return value.Float64Value;
119+
}
120+
}
121+
122+
internal class Int64Codec : ICodec
123+
{
124+
public bool CanDecode(StructuredValue value)
125+
{
126+
return value.KindCase == StructuredValue.KindOneofCase.Int64Value;
127+
}
128+
129+
public object DoDecode(StructuredValue value, Func<StructuredValue, object> decode_fn)
130+
{
131+
return (int)value.Int64Value;
132+
}
133+
}
134+
135+
internal class StringCodec : ICodec
136+
{
137+
public bool CanDecode(StructuredValue value)
138+
{
139+
return value.StringValue is not null;
140+
}
141+
142+
public object DoDecode(StructuredValue value, Func<StructuredValue, object> decode_fn)
143+
{
144+
return tf.compat.as_str(value.StringValue);
145+
}
146+
}
147+
148+
internal class NoneCodec : ICodec
149+
{
150+
public bool CanDecode(StructuredValue value)
151+
{
152+
return value.NoneValue is not null;
153+
}
154+
155+
public object DoDecode(StructuredValue value, Func<StructuredValue, object> decode_fn)
156+
{
157+
return null;
158+
}
159+
}
160+
161+
internal class BoolCodec : ICodec
162+
{
163+
public bool CanDecode(StructuredValue value)
164+
{
165+
return value.KindCase == StructuredValue.KindOneofCase.BoolValue;
166+
}
167+
168+
public object DoDecode(StructuredValue value, Func<StructuredValue, object> decode_fn)
169+
{
170+
return value.BoolValue;
171+
}
172+
}
173+
174+
internal class TensorShapeCodec : ICodec
175+
{
176+
public bool CanDecode(StructuredValue value)
177+
{
178+
return value.TensorShapeValue is not null;
179+
}
180+
181+
public object DoDecode(StructuredValue value, Func<StructuredValue, object> decode_fn)
182+
{
183+
return new Shape(value.TensorShapeValue);
184+
}
185+
}
186+
187+
internal class TensorTypeCodec : ICodec
188+
{
189+
public bool CanDecode(StructuredValue value)
190+
{
191+
return value.KindCase == StructuredValue.KindOneofCase.TensorDtypeValue;
192+
}
193+
194+
public object DoDecode(StructuredValue value, Func<StructuredValue, object> decode_fn)
195+
{
196+
return value.TensorDtypeValue.as_tf_dtype();
197+
}
198+
}
199+
200+
internal class TensorSpecCodec : ICodec
201+
{
202+
public bool CanDecode(StructuredValue value)
203+
{
204+
return value.TensorSpecValue is not null;
205+
}
206+
207+
public object DoDecode(StructuredValue value, Func<StructuredValue, object> decode_fn)
208+
{
209+
var name = value.TensorSpecValue.Name;
210+
var shape = decode_fn(new StructuredValue()
211+
{
212+
TensorShapeValue = value.TensorSpecValue.Shape
213+
});
214+
Debug.Assert(shape is Shape);
215+
var dtype = decode_fn(new StructuredValue()
216+
{
217+
TensorDtypeValue = value.TensorSpecValue.Dtype
218+
});
219+
Debug.Assert(dtype is TF_DataType);
220+
return new Framework.Models.TensorSpec(shape as Shape, (TF_DataType)dtype,
221+
string.IsNullOrEmpty(name) ? null : name);
222+
}
223+
}
224+
225+
internal class BoundedTensorSpecCodec : ICodec
226+
{
227+
public bool CanDecode(StructuredValue value)
228+
{
229+
return value.BoundedTensorSpecValue is not null;
230+
}
231+
232+
public object DoDecode(StructuredValue value, Func<StructuredValue, object> decode_fn)
233+
{
234+
var btsv = value.BoundedTensorSpecValue;
235+
var name = btsv.Name;
236+
var shape = decode_fn(new StructuredValue()
237+
{
238+
TensorShapeValue = btsv.Shape
239+
});
240+
Debug.Assert(shape is Shape);
241+
var dtype = decode_fn(new StructuredValue()
242+
{
243+
TensorDtypeValue = btsv.Dtype
244+
});
245+
Debug.Assert(dtype is TF_DataType);
246+
throw new NotImplementedException("The `BoundedTensorSpec` has not been supported, " +
247+
"please submit an issue to https://github.com/SciSharp/TensorFlow.NET/issues");
248+
}
249+
}
250+
251+
internal class TypeSpecCodec : ICodec
252+
{
253+
public bool CanDecode(StructuredValue value)
254+
{
255+
return value.TypeSpecValue is not null;
256+
}
257+
258+
public object DoDecode(StructuredValue value, Func<StructuredValue, object> decode_fn)
259+
{
260+
var type_spec_proto = value.TypeSpecValue;
261+
var type_spec_class_enum = type_spec_proto.TypeSpecClass;
262+
var class_name = type_spec_proto.TypeSpecClassName;
263+
264+
throw new NotImplementedException("The `TypeSpec` analysis has not been supported, " +
265+
"please submit an issue to https://github.com/SciSharp/TensorFlow.NET/issues");
266+
}
267+
}
14268
}

0 commit comments

Comments
 (0)