Developing your own TensorFlow application

Now that we've shown you some preliminary code samples, let's move on to our example project—how to use TensorFlowSharp from a console application to detect objects within an image. This code is easy enough for you to be able to add into your solution if you so desire. Just tweak the input and output names, perhaps allow for user adjusted hyperparameters, and you're off!

To run this solution, you should have the source code for this chapter downloaded from the website and open in Microsoft Visual Studio. Please follow the instructions for downloading code for this book:

Before we dive into the code, let's talk about one very important variable:

private static double MIN_SCORE_FOR_OBJECT_HIGHLIGHTING = 0.5;

This variable is our threshold for identifying and highlighting objects in our base image. At 0.5, there is a reasonable synchronicity between detection reliability and accuracy. As we lower this number, we will find that more objects are identified, however, the identification accuracy begins to suffer. The lower we go, the greater the chance we have of identifying objects incorrectly. We will identify them, but they may not be what we intended, as you will see in a bit.

Now, let's have a quick look at the main function of this sample and walk through what it's doing:

static void Main(string[] args)
{

Load the default model and data:

_catalogPath = DownloadDefaultTexts(_currentDir);
_modelPath = DownloadDefaultModel(_currentDir);
_catalog = CatalogUtil.ReadCatalogItems(_catalogPath);
var fileTuples = new List<(string input, string output)>() { (_input, _output) };
string modelFile = _modelPath;

Let's create our TensorFlowSharp graph object here:

using (var graph = new TFGraph())
{

Read all of the data into our graph object:

graph.Import(new TFBuffer(File.ReadAllBytes(modelFile)));

Create a new TensorFlowSharp session to work with:

using (var session = new TFSession(graph))
{
Console.WriteLine("Detecting objects", Color.Yellow);
foreach (var tuple in fileTuples)
{

Create our tensor from our image file:

var tensor = ImageUtil.CreateTensorFromImageFile(tuple.input, TFDataType.UInt8);
var runner = session.GetRunner();
runner.AddInput(graph["image_tensor"][0], tensor).Fetch(graph["detection_boxes"][0],graph["detection_scores"][0],graph["detection_classes"][0],graph["num_detections"][0]);var output = runner.Run();
var boxes = (float[,,])output[0].GetValue();
var scores = (float[,])output[1].GetValue();
var classes = (float[,])output[2].GetValue();
Console.WriteLine("Highlighting object...", Color.Green);

After all variables are processed, let's identify and draw the boxes of the objects we have detected on our sample image:

DrawBoxesOnImage(boxes, scores, classes, tuple.input, tuple.output, MIN_SCORE_FOR_OBJECT_HIGHLIGHTING);
Console.WriteLine($"Done. See {_output_relative}. Press any key", Color.Yellow);
Console.ReadKey();
}
}
}

Well, that is all well and good for a simple operation, but what if what we really need to do is a more complicated operation, let's say multiplying a matrix? We can do that as follows:

void BasicMultidimensionalArray ()
{

Create our TFGraph object:

using (var g = new TFGraph ())
{

Create our TFSession object:

var s = new TFSession (g);

Create a placeholder for our variable for multiplication:

var var_a = g.Placeholder (TFDataType.Int32);
var mul = g.Mul (var_a, g.Const (2));

Do the multiplication:

var a = new int[,,] { { { 0, 1 } , { 2, 3 } } , { { 4, 5 }, { 6, 7 } } };
var result = s.GetRunner ().AddInput (var_a, a).Fetch (mul).Run () [0];

Test the results:

var actual = (int[,,])result.GetValue ();
var expected = new int[,,] { { {0, 2} , {4, 6} } , { {8, 10}, {12, 14} } };
Console.WriteLine ("Actual: " + RowOrderJoin (actual));
Console.WriteLine ("Expected: " + RowOrderJoin (expected));
Assert(expected.Cast<int> ().SequenceEqual (actual.Cast<int> ()));
};
}
private static string RowOrderJoin(int[,,] array) => string.Join (", ", array.Cast<int> ());
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset