Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix the error when loading VGG19. #1027

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Tensorflow.Keras.ArgsDefinition
/// This class has nothing but the attributes different from `LayerArgs`.
/// It's used to serialize the model to `tf` format.
/// If the `get_config` of a `Layer` in python code of tensorflow contains `super().get_config`,
/// then the Arg definition should inherit `utoSerializeLayerArgs` instead of `LayerArgs`.
/// then the Arg definition should inherit `AutoSerializeLayerArgs` instead of `LayerArgs`.
/// </summary>
public class AutoSerializeLayerArgs: LayerArgs
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@

namespace Tensorflow.Keras.Common
{
class ShapeInfoFromPython
{
public string class_name { get; set; }
public long?[] items { get; set; }
}
public class CustomizedShapeJsonConverter: JsonConverter
{
public override bool CanConvert(Type objectType)
Expand Down Expand Up @@ -44,36 +49,23 @@ public override void WriteJson(JsonWriter writer, object? value, JsonSerializer
dims[i] = shape.dims[i];
}
}
var token = JToken.FromObject(dims);
var token = JToken.FromObject(new ShapeInfoFromPython()
{
class_name = "__tuple__",
items = dims
});
token.WriteTo(writer);
}
}

public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
{
long?[] dims;
try
{
dims = serializer.Deserialize(reader, typeof(long?[])) as long?[];
}
catch (JsonSerializationException ex)
{
if (reader.Value.Equals("class_name"))
{
reader.Read();
reader.Read();
reader.Read();
dims = serializer.Deserialize(reader, typeof(long?[])) as long?[];
}
else
{
throw ex;
}
}
if (dims is null)
var shape_info_from_python = serializer.Deserialize<ShapeInfoFromPython>(reader);
if (shape_info_from_python is null)
{
return null;
}
long ?[]dims = shape_info_from_python.items;
long[] convertedDims = new long[dims.Length];
for(int i = 0; i < dims.Length; i++)
{
Expand Down
2 changes: 1 addition & 1 deletion src/TensorFlowNET.Core/Tensorflow.Binding.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ https://tensorflownet.readthedocs.io</Description>

<ItemGroup>
<PackageReference Include="MethodBoundaryAspect.Fody" Version="2.0.148" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="OneOf" Version="3.0.223" />
<PackageReference Include="Protobuf.Text" Version="0.7.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
Expand Down
11 changes: 9 additions & 2 deletions src/TensorFlowNET.Core/Training/Saving/SavedModel/loader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ private void _add_object_graph_edges(SavedObject proto, int node_id)
return proto.KindCase switch
{
SavedObject.KindOneofCase.UserObject => _recreate_user_object(proto.UserObject, node_id),
SavedObject.KindOneofCase.Function => _recreate_function(proto.Function, null),
SavedObject.KindOneofCase.Function => _recreate_function(proto.Function, dependencies),
SavedObject.KindOneofCase.BareConcreteFunction => _recreate_bare_concrete_function(proto.BareConcreteFunction, dependencies),
SavedObject.KindOneofCase.Variable => _recreate_variable(proto.Variable),
SavedObject.KindOneofCase.CapturedTensor => throw new NotImplementedException(),
Expand Down Expand Up @@ -626,7 +626,7 @@ private void _add_object_graph_edges(SavedObject proto, int node_id)
}

private (Function, Action<object, object, object>) _recreate_function(SavedFunction proto,
Dictionary<OneOf<string, int>, Trackable> dependencies)
IDictionary<OneOf<string, int>, Trackable> dependencies)
{
var fn = function_deserialization.recreate_function(proto, _concrete_functions);
foreach (var name in proto.ConcreteFunctions)
Expand All @@ -644,6 +644,13 @@ private void _add_object_graph_edges(SavedObject proto, int node_id)
return (fn, setattr);
}

private (Tensor, Action<object, object, object>) _get_tensor_from_fn(CapturedTensor proto)
{
var outer_graph = _concrete_functions[proto.ConcreteFunction].func_graph;
var captured_tensor = outer_graph.get_tensor_by_name(proto.Name);
return (captured_tensor, setattr);
}

// TODO: remove this to a common class.
public static Action<object, object, object> setattr = (x, y, z) =>
{
Expand Down
6 changes: 6 additions & 0 deletions src/TensorFlowNET.Keras/Utils/generic_utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ public static Layer deserialize_keras_object(string class_name, JToken config)
var args = deserializationGenericMethod.Invoke(config, null);
var layer = Assembly.Load("Tensorflow.Keras").CreateInstance($"Tensorflow.Keras.Layers.{class_name}", true, BindingFlags.Default, null, new object[] { args }, null, null);
Debug.Assert(layer is Layer);

// TODO(Rinne): _shared_object_loading_scope().set(shared_object_id, deserialized_obj)

return layer as Layer;
}

Expand All @@ -82,6 +85,9 @@ public static Layer deserialize_keras_object(string class_name, LayerArgs args)
return null;
}
Debug.Assert(layer is Layer);

// TODO(Rinne): _shared_object_loading_scope().set(shared_object_id, deserialized_obj)

return layer as Layer;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
using Tensorflow.Keras.UnitTest.Helpers;
using Tensorflow.NumPy;
using static Tensorflow.Binding;
using static Tensorflow.KerasApi;

namespace TensorFlowNET.Keras.UnitTest.SaveModel;

[TestClass]
public class SequentialModelLoad
{
[Ignore]
[TestMethod]
public void SimpleModelFromAutoCompile()
{
Expand Down Expand Up @@ -80,4 +80,27 @@ public void ModelWithSelfDefinedModule()

model.fit(dataset.Train.Data, dataset.Train.Labels, batch_size, num_epochs);
}

[Ignore]
[TestMethod]
public void VGG19()
{
var model = tf.keras.models.load_model(@"D:\development\tf.net\models\VGG19");
model.summary();

var classify_model = keras.Sequential(new System.Collections.Generic.List<Tensorflow.Keras.ILayer>()
{
model,
keras.layers.Flatten(),
keras.layers.Dense(10),
});
classify_model.summary();

classify_model.compile(tf.keras.optimizers.Adam(), tf.keras.losses.SparseCategoricalCrossentropy(), new string[] { "accuracy" });

var x = np.random.uniform(0, 1, (8, 512, 512, 3));
var y = np.ones((8));

classify_model.fit(x, y, batch_size: 4);
}
}