Preface
TensorFlow is an artificial intelligence learning system open source by Google. Why call it this name? Tensor means tensor, representing N-dimensional arrays; Flow means flow, representing calculations based on data flow graphs. The process of flowing N-dimensional numbers from one end of the flow diagram to the other end is the process of analysis and processing by artificial intelligence neural networks.
After training the Tf model for a long time, I finally went to the production environment to take the postgraduate entrance examination. Today I spent some time studying how the TF model is used in a production environment. Probably sorted out these methods.
Continue to use the ckpt file saved in steps
This seems to be inseparable from the tensorflow framework, and the generated ckpt file is relatively large. When publishing it to the production environment, you have to install the Python algorithm files together. How to interact with other programs, you may have to write services yourself. It is estimated that few people do this, and the performance seems to be very average.
Using tensorflow Serving
tf Serving seems to be a method that everyone is more respected. You need to compile tfServing and then export the model. By directly executing the tf Serving process, you can provide services to the outside world. When calling it in detail, you have to write the client yourself, use human gRPC to call Serving, and then provide services to the outside world, which sounds quite troublesome. Moreover, I don’t have much time to study gRPC today. Many of the clients on the Internet are written in python. I feel that my python level is relatively good and I don’t have the confidence to write well. So this method has not been studied for now.
Produce .pb files, and then write the program to call .pb files
After the .pb file is generated, it can be called directly by the program, and parameters can be passed in, and the generated .pb file is very small. And I have rich experience in .net development. I was wondering whether I could use C# to parse .pb files and then make an external service API of .net core, which seems to be more efficient. The key is to be familiar with the development of this model and do not need to spend too much time exploring. ,
Specific ideas
Use the TensorFlow framework tensorflowSharp (it seems to be still not out of the framework). Call the pb file, and then make the .net core web API to provide services to the outside world.
Specific implementation
It's very simple to directly upload the code. There are very few places to design it to tensorflowsharp.
var graph = new TFGraph(); //The key point is the following sentence, read out the bytes of the trained pb file, and then import itvar model = (model_file); (model); ("Please enter the address of a picture"); var src = (); var tensor = (src); using (var sess = new TFSession(graph)) { var runner = (); (graph["Cast_1"][0], tensor); var r = ((graph["softmax_linear/softmax_linear"][0])); var v = (float[,])(); (v[0,0]); (v[0, 1]); }
ImageUtil is a class library that converts images into tensorflowSharp's official example. I copied it directly and modified several parameters according to my network.
public static class ImageUtil { public static TFTensor CreateTensorFromImageFile(byte[] contents, TFDataType destinationDataType = ) { var tensor = (contents); TFOutput input, output; // Construct a graph to normalize the image using (var graph = ConstructGraphToNormalizeImage(out input, out output, destinationDataType)) { // Execute that graph to normalize this one image using (var session = new TFSession(graph)) { var normalized = ( inputs: new[] { input }, inputValues: new[] { tensor }, outputs: new[] { output }); return normalized[0]; } } } // Convert the image in filename to a Tensor suitable as input to the Inception model. public static TFTensor CreateTensorFromImageFile(string file, TFDataType destinationDataType = ) { var contents = (file); // DecodeJpeg uses a scalar String-valued tensor as input. var tensor = (contents); TFOutput input, output; // Construct a graph to normalize the image using (var graph = ConstructGraphToNormalizeImage(out input, out output, destinationDataType)) { // Execute that graph to normalize this one image using (var session = new TFSession(graph)) { var normalized = ( inputs: new[] { input }, inputValues: new[] { tensor }, outputs: new[] { output }); return normalized[0]; } } } // The inception model takes as input the image described by a Tensor in a very // specific normalized format (a particular image size, shape of the input tensor, // normalized pixel values etc.). // // This function constructs a graph of TensorFlow operations which takes as // input a JPEG-encoded string and returns a tensor suitable as input to the // inception model. private static TFGraph ConstructGraphToNormalizeImage(out TFOutput input, out TFOutput output, TFDataType destinationDataType = ) { // Some constants specific to the pre-trained model at: // //models/ // // - The model was trained after with images scaled to 224x224 pixels. // - The colors, represented as R, G, B in 1-byte each were converted to // float using (value - Mean)/Scale. const int W = 128; const int H = 128; const float Mean = 0; const float Scale = 1f; var graph = new TFGraph(); input = (); output = ( (x: (x: (images: (input: ((contents: input, channels: 3), DstT: ), dim: (0, "make_batch")), size: (new int[] { W, H }, "size")), y: (Mean, "mean")), y: (Scale, "scale")), destinationDataType); return graph; } }
Get it done
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.