Darkflow frozen pb - tensorflowsharp output - c#

I am having some problem with my object detection project. I have a working solution that uses tensorflowsharp nuget. I am trying to get faster detection and I wanted to experiment with YOLO model.
I am using darkflow for having YOLO working with Tensorflow. I trained my model on my custom dataset and then I froze it using the instruction on the darkflow page. I now have my PB file and a metafile, so far so good.
I then adjusted my code in the tensorflowsharp project, pointing to the just created protobuf and adapted the name of the input output variables, from:
String[] outputs = { "detection_boxes:0", "detection_scores:0", "detection_classes:0", "num_detections:0" };
runner.AddInput("image_tensor:0", tensor).Fetch(outputs);
try
{
output = runner.Run();
}
catch (TFException e)
{
Console.WriteLine(e.ToString());
}
to:
runner.AddInput("input:0", tensor).Fetch("output:0");
try
{
output = runner.Run();
}
catch (TFException e)
{
Console.WriteLine(e.ToString());
}
Following the name of the variables in the darkflow documentation. I am able to add input and output pointer to the session but when I get to run the detection (Runner.Run) I get an exception:
TensorFlow.TFException: Expects arg[0] to be float but uint8 is provided
Runner.Run() returns null.
I am not sure what the name of the output tensors are in darkflow, on the documentation I found:
The name of input tensor and output tensor are respectively 'input' and 'output'. For further usage of this protobuf file, please refer to the official documentation of Tensorflow on C++ API here.
but I would expect different collection (tensors) as return type as it is for SSD and other models, right?

Related

"Value does not exist" (WW CadLib)

I think this is a rather straightforward issue, but I don't have much information to go on. I have this code to try and read a DWG file into memory, so that I can read and manipulate the data. I am getting an error that the parameter "Value" cannot be null.
While that does let me know something is wrong, how do I proceed in figuring out what exactly this value is so that I can fix it?
string basePath = Path.Combine($"{Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location)}", "temp");
string fileName = Path.Combine(basePath, "ATemplateV0.2.dwg");
if(File.Exists(fileName))
{
DwgReader dwgReader = new(fileName);
try
{
DxfModel dxfModel = dwgReader.Read();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
WW CadLib needs a license to run, if you don't have one that may be causing the error.
The code example in the answer from #b166er is referring to an Open Source library called ACadSharp to read dwg/dxf files from one of my repositories.
Right now you can use a pre-release version of it in Nuget
You can pass a onNotification event into the DwgReader constructor that might give you more information. Examples here: https://github.com/DomCR/ACadSharp/blob/master/ACadSharp.Examples/Program.cs

I cannot get Xamarin to play small media files

Using an android scanner device, running KitKat.
Using Xamarin in Visual Studio Enterprise 2017, 15.9.9.
I need to generate a "Success" or "Error" sound, based on the content of the scanned barcode.
I have two files: "Success.mp3" and "Error.wav", neither of which will play.
Since this should be a very simple process, I am trying to use Android's MediaPlayer class, rather than add some NuGet package.
I am using Dependency Injection to properly run the Android code, since Xamarin does not have any sort or media API.
I instantiate Android's MediaPlayer as variable "player", and it does successfully instantiate, but as soon as I attempt to do anything with it, it throws a Null Exception error and the value of "player" displays as Null.
I have been experimenting with different ways to do this and have copies of the sound files stored both in the Assets folder, and the Resources/Raw folder (see below).
Here is my method:
public void PlaySound(string soundType) {
var filename =
global::Android.App.Application.Context.Assets.OpenFd(soundType);
if (player == null) {
MediaPlayer player = new MediaPlayer();
}
//This is where the error happens
player.SetDataSource(filename);
player.Prepare();
player.Start();
}
I have also tried the last three lines as the following, with the same result:
player.Prepared += (s, e) => {
player.Start();
};
player.SetDataSource(filename.FileDescriptor, filename.StartOffset,
filename.Length);
player.Prepare();
I have also attempted to utilize what so many people demonstrate as the way to do this, but it does not work for me. This is where the file must be stored in Resources/Raw:
player = MediaPlayer.Create(global::Android.App.Application.Context,
Resource.Raw.someFileName);
Whatever value that you use for "someFileName", all Visual Studio gives you is "'Resource.Raw' does not contain a definition for 'someFileName'".
Resource.designer.CS does contain entries for both files:
public const int Error = 2131230720;
public const int Success = 2131230721;
Expected results: sound, or some meaningful error message that puts me on the right path.
I am still relatively new to Xamarin and am probably missing something that would be obvious to veteran eyes. I have tried so many other things, most of which are not mentioned here, grasping for some straw. This should be simple, but is proving otherwise. Thank you for any help that you can provide.

Trying to call third party dll from Azure Function V2 I get an exception

I strictly follow this sample: https://odetocode.com/blogs/scott/archive/2018/02/14/pdf-generation-in-azure-functions-v2.aspx
I download the three files from the DinkToPdf repository
Problem is when I add either 32bit OR 64bit DLLs, I get the following error:
One or more errors occurred. (An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B))
on this code in particular:
private static byte[] BuildPdf(string html)
{
byte[] arr = null;
try
{
arr = pdfConverter.Convert(new HtmlToPdfDocument() { Objects = { new ObjectSettings { HtmlContent = html } } });
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex);
}
return arr;
}
My Azure Function V2 .net Core project is set to Any CPU with no option for other. I saw other posts on Stack Overflow about this error and I feel that this is different because other people are talking about two projects that have control over and there is some differences in the architecture, but here I just point a few binaries and no matter if they are 32 or 64 the error stays. Also I saw that some people change settings of the IIS and I don't have such. Any ideas? Thanks!
One or more errors occurred. (An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B))
This error is caused by using 64 bit dll. By default, VS works with 32 bit Function CLI hence the error is expected. The bit is not the problem, we can use x64 CLI with several steps.
Problem is one tricky error Qt: Could not initialize OLE (error 80070005).
Function hangs when our html content has some javascript tags or references. See the test done by Travis. When js is removed Function returns PDF successfully but the error message persists.(I assume it doesn't matter as we get a complete PDF as expected.)
If this restriction is not acceptable, install package OpenHtmlToPdf, change your code as below. Also you can try other packages.
private static byte[] BuildPdf(string html)
{
return Pdf.From(html).Content();
}
Note that this method doesn't work in Function on Consumption plan or Free App service plan.

TensorflowSharp - TFException: Attempting to use uninitialized value

I'm attempting to import a model created in Keras/Tensorflow and use it for inference in a Unity project.
I have successfully imported the model and validated by printing names of input and output nodes in the graph. Though, when I try to get the output value from the runner, I get this exception.
TFException: Attempting to use uninitialized value action_W
[[Node: action_W/read = IdentityT=DT_FLOAT, _class=["loc:#action_W"], _device="/job:localhost/replica:0/task:0/cpu:0"]]
TensorFlow.TFStatus.CheckMaybeRaise (TensorFlow.TFStatus incomingStatus, System.Boolean last) (at <6ed6db22f8874deba74ffe3e566039be>:0)
TensorFlow.TFSession.Run (TensorFlow.TFOutput[] inputs, TensorFlow.TFTensor[] inputValues, TensorFlow.TFOutput[] outputs, TensorFlow.TFOperation[] targetOpers, TensorFlow.TFBuffer runMetadata, TensorFlow.TFBuffer runOptions, TensorFlow.TFStatus status) (at <6ed6db22f8874deba74ffe3e566039be>:0)
TensorFlow.TFSession+Runner.Run (TensorFlow.TFStatus status) (at <6ed6db22f8874deba74ffe3e566039be>:0)
RecordArbitraryData.ModelPredict (System.Single[,] input) (at Assets/Scripts/Spells/RecordArbitraryData.cs:230)
RecordArbitraryData.FixedUpdate () (at Assets/Scripts/Spells/RecordArbitraryData.cs:95)
Here are the two functions I use. InstantiateModel is called OnStart() in my unity script. And ModelPredict is called when the user passes an input to the script.
void InstantiateModel(){
string model_name = "simple_as_binary";
//Instantiate Graph
graphModel = Resources.Load (model_name) as TextAsset;
graph = new TFGraph ();
graph.Import (graphModel.bytes);
session = new TFSession (graph);
}
void ModelPredict(float[,] input){
using (graph) {
using (session) {
//Assign input tensors
var runner = session.GetRunner ();
runner.AddInput (graph [input_node_name] [0], input);
//Calculate and access output of graph
runner.Fetch (graph[output_node_name][0]);
Debug.Log ("Output node name: " + graph [output_node_name].Name);
float[,] recurrent_tensor = runner.Run () [0].GetValue () as float[,];
//var results = runner.Run();
//Debug.Log("Prediciton: " + results);
}
}
}
Any help appreciated - TensorflowSharp is very new to me.
I was able to figure out most of my problems. I'm currently at a point where my model is predicting in unity, but only predicting the first of four classes. My guess is it has something to do with the weights not getting initialized correctly from the checkpoint files? Edit: My values weren't being normalized before being passed to neural network.
Preface: Mozilla Firefox works best for displaying tensorboard; it took me a long time to realize that google chrome was causing my graph to be invisible (tensorboard is how I was able to figure out the nodes that needed to be used for input and output).
First Issue: I was renaming a .pb file into a .bytes file. This is incorrect because the model’s weights come from the checkpoint file, and are given to the nodes held in the .pb file. This was causing the uninitialized variables. These variables were used for training, which were removed after using the freeze_graph function.
Second Issue: I was using the file created called ‘checkpoint’, which was throwing an error. I then changed the name of the checkpoint to ‘test’ and used this in the freeze_graph function. When calling the checkpoints file, I was required to use ‘test.ckpt’. I assume this function knows to grab the three files automatically based on the .ckpt? ‘Test’ without ‘.ckpt’ did not work.
Third Issue: when using the freeze_graph function, I needed to export the .pb file in keras/tf with text=False. I tested True and False; True threw an error about “bad wiring”.
Fourth Issue: Tensorboard was very difficult to use without any organization. Using tf.name_scope helped a lot with not only visualization, but making sure I was using/referencing the correct nodes in TensorFlowSharp. In keras I found it helpful to separate the final Dense layer and Activation into their own scopes so I could find the correct output node. The rest of my network was put into a ‘body’ scope, and the sole input layer in ‘input’ scope. The name_scope function prepends ‘scopename/’ to the node name. I don't think it’s necessary, but it helped me.
Fifth Issue: The version of tensorflowsharp released as a unity package is not up to date. This caused an issue with a keras placeholder for ‘keras_training_phase’. In keras, you pass this along with an input like [0] + input. I tried to do the same by creating a new TFTensor(bool), but I was getting an error ‘inaccessible due to its protection level. This was an error with the implicit conversion between bool and TFTensor in my unity TensorFlowSharp version. To fix this I had to use a function found in this stackoverflow solution where the .bytes file is read in, the placeholder for keras_training_phase is found, and is swapped out for a bool constant set to false. This worked for me because my model was pretrained in python, so it may not be a great fix for someone that’s trying to train and test the model. A condition for removing this node with the freeze_graph function would really be useful.
Hope someone finds this useful!

Xamarin Android - Exception when calling Environment.GetExternalStorageState(File path)

I’m unable to use the Android.OS.Environment.GetExternalStorageState method as outlined here.
The Android.OS.Environment.GetExternalStorageState property works. However in order to query the state the secondary external SD card’s state I need to use the method (not the property) and pass in the path to the secondary external SD card. When calling this method I receive a .NET exception as follows:
No static method with name=’getExternalStorageState’
signature=’(Ljava/io/File;)Ljava/lang/String;’ in class
Landroid/os/Environment;
A sample app that reliably reproduces the problem can be found here.
Note that this is on a device that has 2 SD cards. One is internal (not removable) and is the default SD card path. The other SD card is an external SD card which is removable. This test app assumes the external secondary removable SD card is at a root path of ‘sdcard1’ and the default internal is ‘sdcard0’. You may need to change this if your device is different.
Here is a trimmed down version of the CheckState from the sample app.
private bool CheckState(string pathStr)
{
try
{
var path = new File(pathStr);
var otherState = Environment.ExternalStorageState;
var state = Environment.GetExternalStorageState(path);
// Good state - set values and be done
return state.Equals(Environment.MediaMounted);
}
catch (Java.Lang.Exception ex)
{
Debug(Tag, $"Java Ex: {ex.Message}");
}
catch (Exception ex)
{
Debug(Tag, $".Net Ex: {ex.Message}");
}
return false;
}
In the example code above, the otherState variable is set with the state of the internal SD card. However when the next line with the state variable executes an exception is thrown.
Also, as an FYI - it is the .Net System.Exception that is thrown, no the Java.Lang.Exception.
If this code is correct and there is in fact a bug in the framework code somewhere (Xamarin and/or Android) then is there a recommended work around to determine if an SD card is available for reading & writing?
No static method with name=’getExternalStorageState’ signature=’(Ljava/io/File;)Ljava/lang/String;’ in class Landroid/os/Environment;
String getExternalStorageState (File path) was added in API 21.
re: https://developer.android.com/reference/android/os/Environment.html#getExternalStorageState(java.io.File)

Categories

Resources