SciSharp-Stack-Examples icon indicating copy to clipboard operation
SciSharp-Stack-Examples copied to clipboard

TF2 - Implement tf.saved_model.load

Open IamSierraCharlie opened this issue 3 years ago • 4 comments

Hello, as per the subject, are there plans to implement TF2 functions?

For example:

   tf.saved_model.load 

This is part of the code I would like to implement in C# from python

   image_np = load_image_into_numpy_array(image_path)
   input_tensor = tf.convert_to_tensor(image_np)
   input_tensor = input_tensor[tf.newaxis, ...]
   detections = detect_fn(input_tensor)
   num_detections = int(detections.pop('num_detections'))
   detections = {key: value[0, :num_detections].numpy() for key, value in detections.items()}
   detections['num_detections'] = num_detections
   detections['detection_classes'] = detections['detection_classes'].astype(np.int64)

Some of this I can do, but now with TF2 being the standard (no frozen inference graph), it is difficult to proceed with building models in the old standard.

Just want to know if there are any plans as I cant find anything in the documentation that matches this use case from python.

Many thanks for your work so far.

IamSierraCharlie avatar Jan 06 '22 21:01 IamSierraCharlie

I'm using below alternate solution:

var model = BuildModel();
model.load_weights("");

Esther2013 avatar Jan 07 '22 01:01 Esther2013

Thanks for your response. Can you elaborate on "BuildModel()"

Are you using this function? or something like it?

Functional BuildModel()
        {
            tf.Context.reset_context();
            var inputs = keras.Input(shape: 2);

            // 1st dense layer
            var DenseLayer = keras.layers.Dense(1, activation: keras.activations.Sigmoid);
            var outputs = DenseLayer.Apply(inputs);

            // build keras model
            Functional model = keras.Model(inputs, outputs, name: Guid.NewGuid().ToString());
            // show model summary
            model.summary();

            // compile keras model into tensorflow's static graph
            model.compile(loss: keras.losses.MeanSquaredError(name: Guid.NewGuid().ToString()),
                optimizer: keras.optimizers.Adam(name: Guid.NewGuid().ToString()),
                metrics: new[] { "accuracy" });
            return model;
        }

as found here? https://github.com/SciSharp/TensorFlow.NET/blob/425b258ccebf10e21f033e5c11da7a64bfc21a9f/test/TensorFlowNET.Keras.UnitTest/MultiThreadsTest.cs

IamSierraCharlie avatar Jan 07 '22 11:01 IamSierraCharlie

Correct.

Esther2013 avatar Jan 07 '22 14:01 Esther2013

Thankyou for your direction. I'm just not sure I understand how it works. Are there any other examples or further explanation on how to do this? Is it just a matter of setting keras.Input(shape: 2) to be the same shape as what I would input into my own model or is there more to it than that?

I note that when I export my tf built model, I see this warning:

WARNING:tensorflow:Skipping full serialization of Keras layer <keras.layers.core.lambda_layer.Lambda object at 0x7f5c9c357910>, because it is not built. W0124 21:12:34.170078 140039099339136 save_impl.py:71] Skipping full serialization of Keras layer <keras.layers.core.lambda_layer.Lambda object at 0x7f5c9c357910>, because it is not built.

Should I still be able to build / convert my model in this suggested way?

Also, I really would like to know the answer to my first question:

are there plans to implement TF2 functions?

IamSierraCharlie avatar Jan 24 '22 11:01 IamSierraCharlie