SciSharp / TensorFlow.NET

.NET Standard bindings for Google's TensorFlow for developing, training and deploying Machine Learning models in C# and F#.
https://scisharp.github.io/tensorflow-net-docs
Apache License 2.0
3.25k stars 524 forks source link

Can't load saved_model with operation name is "encoded_image_string_tensor" #771

Open thong12889 opened 3 years ago

thong12889 commented 3 years ago

I exported model from Cloud AutoML and my input name is "encoded_image_string_tensor".

encoded_image_string_tensor: Accepts a 1-D string tensor of shape [None] containing encoded PNG or JPEG images. Image resolutions are expected to be the same if more than 1 image is provided.

How can I convert image to 1-D string ? Saved_model

P.S. I can use this model in python.

Oceania2018 commented 3 years ago

Can you provide the full reproduce environment?

thong12889 commented 3 years ago
public static string GetFormattedString(TF_DataType dtype, NDArray nd)
        {
            if (nd.size == 0)
                return "[]";

            switch (dtype)
            {
                case TF_DataType.TF_STRING:
                    return string.Join(string.Empty, nd.ToArray<byte>()
                        .Select(x => x < 32 || x > 127 ? "\\x" + x.ToString("x") : Convert.ToChar(x).ToString()));
                case TF_DataType.TF_BOOL:
                    return (nd.GetByte(0) > 0).ToString();
                case TF_DataType.TF_RESOURCE:
                    return "<unprintable>";
                default:
                    return nd.ToString();
            }
        }

private void predictBtn_Click(object sender, EventArgs e)
        {

            var imgByte = File.ReadAllBytes(imageDir);
            var imgNDarr = np.array(imgByte);
            var imgByteString = GetFormattedString(TF_DataType.TF_STRING, imgNDarr);

            using (var sess = Session.LoadFromSavedModel(pbFile))
            {
                var graph = sess.graph;

                Tensor tensorNum = graph.OperationByName("num_detections");
                Tensor tensorBoxes = graph.OperationByName("detection_boxes");
                Tensor tensorScores = graph.OperationByName("detection_scores");
                Tensor tensorClasses = graph.OperationByName("detection_classes");
                Tensor imgTensor = graph.OperationByName("encoded_image_string_tensor");                
                Tensor[] outTensorArr = new Tensor[] { tensorNum, tensorBoxes, tensorScores, tensorClasses };

                var results = sess.run(outTensorArr, new FeedItem(imgTensor, imgByteString));

                buildOutputImage(results);
            }

        }

Did you mean this ?

Oceania2018 commented 3 years ago

Correct, but where is the pb file?

thong12889 commented 3 years ago

Sorry, here is my pb file.

https://drive.google.com/file/d/1lFzFnGz8AeFtn9i8mN5ZxVZWcudcCvT8/view?usp=sharing