Giter Club home page Giter Club logo

scisharp-stack-examples's Introduction

SciSharp STACK Examples

This repo contains many practical examples written in SciSharp's machine learning libraries. If you still don't know how to use .NET for deep learning, getting started from these examples is your best choice.

Join the chat at https://gitter.im/publiclab/publiclab

Requirements:

Run specific example in shell:

C#

:: run all examples from source code
dotnet run --project src/TensorFlowNET.Examples

:: run specific example
dotnet run --project src/TensorFlowNET.Examples -ex "Linear Regression (Graph)"

:: run in compiled library
dotnet TensorFlowNET.Examples.dll -ex "MNIST CNN (Eager)"

F#

:: run all examples from source code
dotnet run --project src/TensorFlowNET.Examples.FSharp

:: run specific example
dotnet run --project src/TensorFlowNET.Examples.FSharp -ex "Linear Regression (Eager)"

:: run in compiled library
dotnet TensorFlowNET.Examples.FSharp.dll -ex "MNIST CNN (Eager)"

Example runner will download all the required files like training data and model pb files.

Basic Model

  • Hello World C#, F#
  • Basic Operations C#, F#
  • Linear Regression in Graph mode C#, F#
  • Linear Regression in Eager mode C#, F#
  • Linear Regression in Keras C#
  • Logistic Regression in Graph mode C#, F#
  • Logistic Regression in Eager mode C#, F#
  • Nearest Neighbor C#, F#
  • Naive Bayes Classification C#, F#
  • K-means Clustering C#

Neural Network

  • Full Connected Neural Network in Eager mode C#, F#
  • Full Connected Neural Network (Keras) C#, F#
  • NN XOR C#
  • Object Detection in MobileNet C#
  • MNIST FNN in Keras Functional API C#, F#
  • MNIST CNN in Graph mode C#, F#
  • MNIST CNN in Eager mode C#, F#
  • MNIST CNN in Keras SubClass C#, F#
  • MNIST RNN C#
  • MNIST LSTM C#
  • Image Classification in Keras Sequential API C#, F#
  • Image Recognition Inception C#, F#
  • Toy ResNet in Keras Functional API C#, F#
  • Transfer Learning for Image Classification in InceptionV3 C#
  • CNN In Your Own Dataset C#, F#

Natural Language Processing

  • Binary Text Classification C#
  • CNN Text Classification C#
  • Named Entity Recognition C#

Time Series

  • Weather Prediction (CNN, RNN) C#

Welcome to PR your example to us.

Your contribution will make .NET community better than ever.

scisharp-stack-examples's People

Contributors

beacontownfc avatar dedale avatar dependabot[bot] avatar esther2013 avatar nhirschey avatar oceania2018 avatar pepure avatar wanglongzhi2001 avatar xuwaters avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

scisharp-stack-examples's Issues

Inputs was changed by accidently

When running LinearRegression Set int training_steps = 100 * 1000
Shape value is changed by accidently in math_grad._SumGrad:
image
It can't be reproduced always.

Something is wrong with the updated nuget packages

There seems to be something wrong with the nuget packages.

I upgraded to
SciSharp.Tensorflow.Redist-Windows-GPU 2.10.0
TensorFlow.Keras 0.10.0
TensorFlow.NET 0.100.0

and use the code from the example MnistGAN.cs
in https://github.com/SciSharp/SciSharp-Stack-Examples/tree/master/src/TensorFlowNET.Examples/GAN

Code compiles, but:

214292303-acf5d736-18f4-49d2-bd07-1311f318264a

There is only a few changes to the code in MnistGAN.cs to allow it to compile:
214294946-412d6708-6704-4001-b76a-1e8540074be0

What is strange is that the code works just fine if code is compiled using all the source from SciSharp repositories, but when using the NuGet packages, it fails to improve.

Let me know if I made an error. My code is available here:
https://github.com/Darlanio/TensorFlowNetUpdatedGAN

Mnist Model Loader stuck at loading training images

MnistModelLoader is stuck at loading images. I tried installing some nuget mnist packages but that didn't help. I tried deleting downloaded file but that also didn't make any diffrence. I tiried also running exe on VM, no change.
2020-12-22 03_25_16-Window

CnnInYourOwnData:after call Train() ,in Test() function will interrupt

In vs2019 debug mode,after CnnInYourOwnData call Train() and running finished,it can generated all ckpt files
but then call Test() function : (loss_test, accuracy_test) = sess.run((loss, accuracy), (x, x_test), (y, y_test)); will auto interrupted without any information after some time;

if deleted next two lines
Test();
TestDataOutput();

and direct call function
FreezeModel();

    public override string FreezeModel()
    {
        return tf.train.freeze_graph(Config.Name + @"\\MODEL", @".\\CNN_Best", new[]
        {
            "Input/X", 
            "Input/Y", 
            "Train/Prediction/predictions",
            "Train/Prediction/prob"
        });
    }

will generate pb file, but the pb file is more less ckpt files,ckpt file more than 786M, and pb file is only 262M,using netron open pb and ckpt it show very defferent

bbcb-1b55aad29374b9e1189c6f5003e79b8c

thanks

Training new models in various domains

Hello this is great for the C# community, can you please share some information /samples, i.e. do you have any samples for health drugs or prescriptons training models on doctors notes etc.

I saw there were some standars for each NER, but its hard to navigate your stack to figure out where this sits/belongs or even if there is a sample, we would appreciate the guidance or a sample

Often the use ase is that a doc's note could be a prescription or case history as a PDF or image. Using other engines we can get the text, but the NER is the challenge.

Unable to let ImageBackgroundRemoval to works

Hi all and you are amazing; this is a question.
I'm trying to create a simple function for my wpf project that remove background from a given image.
my relevant piece of code is:

        'private static NumSharp.NDArray ReadTensorFromImageFile(string file_name,
                            int input_height = 224,
                            int input_width = 224,
                            int input_mean = 117,
                            int input_std = 1)
        {
        var graph = tf.Graph().as_default();
        var file_reader = tf.read_file(file_name, "file_reader");
        var decodeJpeg = tf.image.decode_jpeg(file_reader, channels: 3, name: "DecodeJpeg");
        var cast = tf.cast(decodeJpeg, tf.float32);
        var dims_expander = tf.expand_dims(cast, 0);
        var resize = tf.constant(new int[] { input_height, input_width });
        var bilinear = tf.image.resize_bilinear(dims_expander, resize);
        var sub = tf.subtract(bilinear, new float[] { input_mean });
        var normalized = tf.divide(sub, new float[] { input_std });

        using (var sess = tf.Session(graph))
            return sess.run(normalized);
    }
    public bool Run(string fileName)
    {
        var nd = ReadTensorFromImageFile(fileName);
        var graph = new Graph();
        graph.Import("frozen_inference_graph.pb");
        Tensor output = graph.OperationByName("SemanticPredictions");
        Tensor imgTensor = graph.OperationByName("ImageTensor");
        using (var sess = tf.Session(graph))
        {
            var results = sess.run(output, new FeedItem(imgTensor, nd));
            return true;
        }

        return false;
    }

`

I got this error:
OverflowException: Overflow_Byte
NumSharp.Backends.Unmanaged.UnmanagedMemoryBlock.CastTo.AnonymousMethod__25(long) in UnmanagedMemoryBlock.Casting.cs
System.Threading.Tasks.Parallel.ForWorker64.AnonymousMethod__1()
System.Threading.Tasks.Task.InnerInvokeWithArg(System.Threading.Tasks.Task)
System.Threading.Tasks.Task.ExecuteSelfReplicating.AnonymousMethod__0(object)

what i'm doing wrong? (obviously lot of things...)

ImageBackgroundRemoval not complete

Hi
can anyone make ImageBackgroundRemoval example work?
I tried many things without success
can you please upload simple working code for Image Background Removal

Thanks

mnist.Train.GetNextBatch Exception

https://github.com/SciSharp/TensorFlow.NET-Examples/blob/24ed42b65bddc655b575532b7089d28aa5b07642/src/TensorFlowNET.Examples/BasicModels/LogisticRegression.cs#L92

When running LogisticRegression Example, I got an Exception:

System.InvalidOperationException: Can't construct NDCoordinatesIncrementor with an empty shape.
   at NumSharp.Utilities.NDCoordinatesIncrementor..ctor(Shape& shape)
   at NumSharp.NDArray._extract_indices(NDArray[] mindices, Boolean isCollapsed, NDArray out)
   at Tensorflow.Hub.MnistDataSet.GetNextBatch(Int32 batch_size, Boolean fake_data, Boolean shuffle)
   at TensorFlowNET.Examples.LogisticRegression.Run() in TensorFlow.NET-Examples/src/TensorFlowNET.Examples/BasicModels/LogisticRegression.cs:line 92
   at TensorFlowNET.Examples.Program.Main(String[] args) in TensorFlow.NET-Examples/src/TensorFlowNET.Examples/Program.cs:line 75

A fix to this example could be modifying this line to the following:

var start = i * batch_size;
var end = (i + 1) * batch_size;
var (batch_xs, batch_ys) = mnist.GetNextBatch(mnist.Train.Data, mnist.Train.Labels, start, end);

Keras subclass-based model CAN NOT save model/weights

Hi,

I am interested in Keras Api using the Subclass style for building the model. I clone example and run example MNIST CNN (Keras Subclass).
I use save_weights function but get errors as below.
Could you give any idea to save the model or weights? Thank you very much.

  • Snip of code:
            {
                x_test = x_test["::100"];
                y_test = y_test["::100"];
                var pred = conv_net.Apply(x_test);
                accuracy_test = (float)accuracy(pred, y_test);
                print($"Test Accuracy: {accuracy_test}");
            }

            conv_net.save_weights("subclass_mnist_model"); // I added this
           
            return accuracy_test > 0.90;
  • Error throw:
Choose one example to run, hit [Enter] to run all: 15
2021-09-08 오전 8:12:50 Starting MNIST CNN (Keras Subclass)
The file C:\Users\Quan\AppData\Local\Temp\mnist.npz already exists
2021-09-08 17:12:51.202274: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
step: 10, loss: 1.7204771, accuracy: 0.83
step: 20, loss: 1.5916963, accuracy: 0.898
step: 30, loss: 1.5561028, accuracy: 0.932
step: 40, loss: 1.5472074, accuracy: 0.944
step: 50, loss: 1.5292175, accuracy: 0.963
step: 60, loss: 1.507012, accuracy: 0.978
step: 70, loss: 1.5141329, accuracy: 0.968
step: 80, loss: 1.4908792, accuracy: 0.989
step: 90, loss: 1.4994849, accuracy: 0.978
Test Accuracy: 0.96
HDF5-DIAG: Error detected in HDF5 (1.10.5) thread 37204:
  #000: C:\projects\hdf-pinvoke-1-10\CMake-hdf5-1.10.5\hdf5-1.10.5\src\H5L.c line 790 in H5Lexists(): not a location
    major: Invalid arguments to routine
    minor: Inappropriate type
  #001: C:\projects\hdf-pinvoke-1-10\CMake-hdf5-1.10.5\hdf5-1.10.5\src\H5Gloc.c line 246 in H5G_loc(): invalid object ID    major: Invalid arguments to routine
    minor: Bad value
HDF5-DIAG: Error detected in HDF5 (1.10.5) thread 37204:
  #000: C:\projects\hdf-pinvoke-1-10\CMake-hdf5-1.10.5\hdf5-1.10.5\src\H5D.c line 119 in H5Dcreate2(): not a location ID    major: Invalid arguments to routine
    minor: Inappropriate type
  #001: C:\projects\hdf-pinvoke-1-10\CMake-hdf5-1.10.5\hdf5-1.10.5\src\H5Gloc.c line 246 in H5G_loc(): invalid object ID    major: Invalid arguments to routine
    minor: Bad value
HDF5CSharp.DataTypes.Hdf5Exception: Unable to create dataset for kernel:0
   at HDF5CSharp.Hdf5Utils.GetId(Int64 parentId, String name, Int64 dataType, Int64 spaceId, Hdf5ElementType type)
   at HDF5CSharp.Hdf5Utils.GetDatasetId(Int64 parentId, String name, Int64 dataType, Int64 spaceId)
   at HDF5CSharp.Hdf5.WriteDatasetFromArray[T](Int64 groupId, String name, Array dset)
   at Tensorflow.Keras.Saving.hdf5_format.WriteDataset(Int64 f, String name, Tensor data)
   at Tensorflow.Keras.Saving.hdf5_format.save_weights_to_hdf5_group(Int64 f, List`1 layers)
   at Tensorflow.Keras.Engine.Model.save_weights(String filepath, Boolean overwrite, String save_format, Object options)   at TensorFlowNET.Examples.DigitRecognitionCnnKeras.Run() in E:\Tensorflow.Net\Keras.NetExample\src\TensorFlowNET.Examples\ImageProcessing\MnistCnnKerasSubclass.cs:line 97
   at TensorFlowNET.Examples.Program.RunExamples(String key, IExample[] examples) in E:\Tensorflow.Net\Keras.NetExample\src\TensorFlowNET.Examples\Program.cs:line 89
2021-09-08 오전 8:13:20 Completed MNIST CNN (Keras Subclass) ++
Example: MNIST CNN (Keras Subclass) is Failed!
TensorFlow Binary v2.6.0
TensorFlow.NET v0.60.2.0
TensorFlow.Keras v0.6.2.0
1 of 27 example(s) are completed.
Press [Enter] to continue...

Unhandled exception. Tensorflow.InvalidArgumentError: Unknown image file format. One of JPEG, PNG, GIF, BMP required. [[{{node DecodeJpeg}}]]

Hi, I'm currently trying to use ImageRecognitionInception example using my own trained model. (which is a frozen inception .pb file) But here comes a problem.

** Unhandled exception. Tensorflow.InvalidArgumentError: Unknown image file format. One of JPEG, PNG, GIF, BMP required. [[{{node DecodeJpeg}}]]**

image

This happens even I used a right file format. I tried JPEG, PNG, JPG
(It worked well when I used a provided sample images and trained models)
I brought my own files from google drive. Is this could be a problem?
And also, can I fix this problem by changing my Exception Settings as well?

It would be so thankful if you give me some comments with this.

Support .NET 6

The FullyConnectedKeras example in the TensorFlowNET.Examples project runs properly with a target framework of net5.0, with an accuracy around 95%. But if you change the target framework to net6.0, the accuracy plummets to <10%. It isn't clear what the root cause is.

I discovered this when I tried to transplant this code to my .NET 6 project. It appears there is some sort of regression or bug that is tanking the accuracy in .NET 6 only.

Steps to Reproduce:

  • Change the TargetFramework of the TensorFlowNET.Examples project from net5.0 to net6.0.
  • Clean, rebuild, and run the Fully Connected Neural Network (Keras) example.
  • Observe accuracy has dropped to ~0.098.

TF2 - Implement tf.saved_model.load

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.

DQN

Anyone creating an example of an DQN/DDQN implementation?

Thanks!

Word2Vec: System.AccessViolationException

The unhandled System.AccessViolationException occurs , and I my try/catch cannot catch it.
What is the reason? How to fix it?

        public bool Run()
        {
            PrepareData();

            var graph = tf.Graph().as_default();
            tf.train.import_meta_graph($"graph{Path.DirectorySeparatorChar}word2vec.meta"); //<---------Exception

System.AccessViolationException
  HResult=0x80004003
  Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

This is from the app console:

graph\word2vec.meta already exists.
word2vec\text8.zip already exists.
Words count: 17005207
Unique words: 253854
Vocabulary size: 47135
Most common words: UNK 0 444176, the 1 1061396, of 2 593677, and 3 416629, one 4 411764, in 5 372201, a 6 325873, to 7 316376, zero 8 264975, nine 9 250430
Fatal error. System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
   at Tensorflow.c_api.TF_ImportGraphDefOptionsSetPrefix(Tensorflow.SafeImportGraphDefOptionsHandle, System.String)
   at Tensorflow.c_api.TF_ImportGraphDefOptionsSetPrefix(Tensorflow.SafeImportGraphDefOptionsHandle, System.String)
   at Tensorflow.importer._PopulateTFImportGraphDefOptions(Tensorflow.ImportGraphDefOptions, System.String, System.Collections.Generic.Dictionary`2<System.String,Tensorflow.Tensor>, System.String[])
   at Tensorflow.importer.import_graph_def(Tensorflow.GraphDef, System.Collections.Generic.Dictionary`2<System.String,Tensorflow.Tensor>, System.String[], System.String, Tensorflow.OpList)
   at Tensorflow.meta_graph.import_scoped_meta_graph_with_return_elements(Tensorflow.MetaGraphDef, Boolean, System.String, System.Collections.Generic.Dictionary`2<System.String,Tensorflow.Tensor>, System.String, System.String[])
   at Tensorflow.saver._import_meta_graph_with_return_elements(System.String, Boolean, System.String, System.String[])
   at Tensorflow.tensorflow+train_internal.import_meta_graph(System.String, Boolean, System.String)
   at TensorFlowNET.Examples.Word2Vec.Run()
   at TensorFlowNET.Examples.Program.RunExamples(System.String, TensorFlowNET.Examples.IExample[])
   at TensorFlowNET.Examples.Program.Main(System.String[])

Lots of exceptions

Unfortunately, none of the examples work without throwing exceptions. The documentation in VS Intellisense is also not nice.
image
This is just of the annoying exceptions. Do I have to first install TensorFLow to get it working??? And which assemblies are we supposed to install? Thanks

Can I use pre-trained models in the repo?

Hello:
I want to know if I can use the repo to convert some pre-trained models to the models which can be used by the repo? For example, Nvidia StyleGAN3, they have a number of pre-trained model in .PKL format.
Can I use their pre-trained model (.pkl format) in the repo to make image prediction?
https://github.com/NVlabs/stylegan3
If yes, please give some general instructions on how to start.
By the way, I am using Visual Studio 2022 in Windows 10 (version 21H2).
Thanks,

creation of pbtxt

Hello Masters!
Were you able to include the creation of pbtxt file from transfer Learning for image?
I tried the sample for transfer learning but the output label is text file and not same format from the pretrained models.
Thanks for anyone who would reply to my concern.

Word2Vec Sequence contains no matching element

Hi folks,

I am using word2vec and I have added textfile instead of URL Web Download (text8.zip). The rest of the code is the same as the example written in word2vec.

...............
...............
// code omitted
...............
...............

int wordId = 0;
//text_words = File.ReadAllText($"word2vec{Path.DirectorySeparatorChar}text8").Trim().ToLower().Split();

string textFile = @"c:\...........\MyData.txt";
text_words = File.ReadAllText(textFile).Trim().ToLower().Split();

...............
...............
// code omitted
...............
...............

Here is my sample data text file (MyData.txt) as below:

This is Singapore and one two three four five and Test123 Kings are brave and Queens are beautiful Singapore is a really expensive country but really beautiful country and modern city
This is Malaysia and one two three four five and Test123 Kings are brave and Queens are beautiful Malaysia is a very old country but really beautiful country and old fashion city
This is Singapore and one two three four five and Test123 Kings are brave and Queens are beautiful Singapore is a really expensive country but really beautiful country and modern city
This is Malaysia and one two three four five and Test123 Kings are brave and Queens are beautiful Malaysia is a very old country but really beautiful country and old fashion city

The reading path file is working but I am getting an error message and it says:

graph\word2vec.meta already exists.
Words count: 129
Unique words: 27
Vocabulary size: 2
Most common words: UNK 0 113, and 1 16
2021-05-04 10:32:28.137672: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
Step 1, Average Loss= 610.5444
Evaluation...
System.InvalidOperationException: Sequence contains no matching element
   at System.Linq.ThrowHelper.ThrowNoMatchException()
   at System.Linq.Enumerable.First[TSource](IEnumerable`1 source, Func`2 predicate)
   at TensorFlowNET.Examples.Word2Vec.Run() in C:\********************\SciSharp-Stack-Examples\src\TensorFlowNET.Examples\TextProcessing\Word2Vec.cs:line 114
   at TensorFlowNET.Examples.Program.RunExamples(String key, IExample[] examples) in C:\Users\Adnan Salahuddin\Desktop\TensorFlow\SciSharp-Stack-Examples\src\TensorFlowNET.Examples\Program.cs:line 89
5/4/2021 5:32:29 AM Completed Word2Vec
Example: Word2Vec is Failed!
TensorFlow Binary v2.4.1
TensorFlow.NET v0.40.1.0
TensorFlow.Keras v0.5.1.0

I tried many times to fix this issue but still no clue. Could you please help me to solve this issue?

Thanking in anticipation

Deep Q Learning

Hi,
Is Deep Q Learning (Reinforcement Learning) supported by SciSharp? Because I can't find any example.

Thanks!

DigitRecognitionRNN.cs error

System.ArgumentNullException: Value cannot be null. (Parameter 'first')
at System.Linq.ThrowHelper.ThrowArgumentNullException(ExceptionArgument argument)
at System.Linq.Enumerable.Concat[TSource](IEnumerable1 first, IEnumerable1 second)
at Tensorflow.Optimizer.compute_gradients(Tensor loss, List1 var_list, Nullable1 aggregation_method, GateGradientType gate_gradients, Boolean colocate_gradients_with_ops, Tensor grad_loss)
at Tensorflow.Optimizer.minimize(Tensor loss, IVariableV1 global_step, List1 var_list, GateGradientType gate_gradients, Nullable1 aggregation_method, Boolean colocate_gradients_with_ops, String name, Tensor grad_loss)
at TensorFlowNET.Examples.DigitRecognitionRNN.BuildGraph() in D:\workspace\CS\SciSharp-Stack-Examples-master\src\TensorFlowNET.Examples\ImageProcessing\DigitRecognitionRNN.cs:line 100
at TensorFlowNET.Examples.DigitRecognitionRNN.Run() in D:\workspace\CS\SciSharp-Stack-Examples-master\src\TensorFlowNET.Examples\ImageProcessing\DigitRecognitionRNN.cs:line 75
at TensorFlowNET.Examples.Program.RunExamples(String key, IExample[] examples) in D:\workspace\CS\SciSharp-Stack-Examples-master\src\TensorFlowNET.Examples\Program.cs:line 91
2020-10-28 05:29:20 Completed MNIST RNN (Graph)

No operations detected in saved model when running Session.LoadFromSavedModel

Hello ,

I have trained a SSD MobileNet V2 FPNLite 640x640 pretrained model in Tensorflow 2.4 with my own data and exported as saved model .The inference and image object detection prediction is working perfectly fine in python .
Im trying to do the same in Tensorflow.Net using the below code :
Session session = Session.LoadFromSavedModel(@"........\saved_model");
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("image_tensor");
Tensor[] outTensorArr = new Tensor[] { tensorNum, tensorBoxes, tensorScores, tensorClasses };

Im getting below weird error:

Tensorflow.ValueError: 'Could not find operation "num_detections" inside graph "grap-key-1/".'

I tried to loop the graph operations of the session but there is nothing in there .

Why this is happening ,it is the same model Im using in python and giving correct results .

Appreciate any help

Regards
Ayad

The majority of the examples crash

The first four examples work, the rest crash on two different computers - both running with fresh VS2019 installs. Is that to be expected, or did I miss something? Are we in between two working releases?

Example: Hello World in 0,0848558s is OK!
Example: Basic Operations in 0,1338481s is OK!
Example: NN XOR in 0,000847s is OK!
Example: Linear Regression in 1,1675918s is OK!
Example: Logistic Regression is Failed!
Example: Naive Bayes Classifier is Failed!
Example: Nearest Neighbor is Failed!
Example: Digits Recognition Neural Network is Failed!
Example: MNIST CNN is Failed!
Example: MNIST RNN is Failed!
Example: MNIST LSTM is Failed!
Example: CnnInYourOwnData is Failed!
Example: Fully Connected Neural Network is Failed!
Example: Convert TensorFlow Model to OpenCv is Failed!
Example: Image Recognition Inception is Failed!
Example: Retrain Classifier With InceptionV3 is Failed!
Example: Object Detection is Failed!
Example: CNN Text Classification is Failed!
Example: Word2Vec is Failed!

I'm including the log file.

output.txt

TrainableVariables missing in NuGet-packages - not able to run example using NuGet-packages, only by compiling full source

See public github repository for example:
https://github.com/Darlanio/SciSharp_TensorFlowNet_Missing_TrainableVariables

I was able to compile the SciSharp-Stack-Examples and run the GAN-example without too much trouble, by incorporating all necessary repositories from SciSharp into the same solution.

However, using the NuGet packages, the TrainableVariables referred to on lines 163-167 in the file
https://github.com/SciSharp/SciSharp-Stack-Examples/blob/master/src/TensorFlowNET.Examples/GAN/MnistGAN.cs
will not be found.

One other thing that is missing is the ToBitmap() in NDArray, line 227. I wrote another SaveImage() to bypass this problem.

Since the TrainableVariables are available in the SciSharp-repositories, I guess this will be fixed with the next version of the NuGet-packages?

Dictionary with members count or item count?

Hi!

I was just stepping through the example - one question regarding

Isn't it a typo that just the current members count of the dictionary is put into it?
My proposal would be:
word_dict[word.Word] = word.Count;

If it's just a wrong interpretation of me - ignore this and nevermind.

Regards
Sascha

PS: I think @Oceania2018 should be mentioned here :-)

Tensorflow.InvalidArgumentError: 'Can not squeeze dim[1], expected a dimension of 1, got 3 [[{{node InceptionV1/Logits/SpatialSqueeze}}]]'

Hi, I'm currently trying to run a 'ImageRecognitionInception' example with my pre-trained model (which is .pb file)

and here comes a problem :

Tensorflow.InvalidArgumentError: 'Can not squeeze dim[1], expected a dimension of 1, got 3
[[{{node InceptionV1/Logits/SpatialSqueeze}}]]'

and here is the code

using NumSharp;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using Tensorflow;
using Tensorflow.Keras.Utils;
using static Tensorflow.Binding;
using Console = Colorful.Console;

namespace TensorFlowNET.Examples
{
     /// Inception v3 is a widely-used image recognition model 
    /// that has been shown to attain greater than 78.1% accuracy on the ImageNet dataset. 
    /// The model is the culmination of many ideas developed by multiple researchers over the years.

    public class ImageRecognitionInception : SciSharpExample, IExample
    {
        string dir = "ImageRecognitionInception";
        string pbFile = "tensorflow_inception_graph.pb";
        string labelFile = "imagenet_comp_graph_label_strings.txt";
        List<NDArray> file_ndarrays = new List<NDArray>();
        public ExampleConfig InitConfig()
            => Config = new ExampleConfig
            {
                Name = "Image Recognition Inception",
                Enabled = true,
                IsImportingGraph = false
            };

        public bool Run()
        {
            tf.compat.v1.disable_eager_execution();

            PrepareData(); 

            var graph = new Graph();
            //import GraphDef from pb file
            graph.Import(Path.Join(dir, pbFile));

            var input_name = "input";
            var output_name = "InceptionV1/Logits/Predictions/Softmax";

            var input_operation = graph.OperationByName(input_name);
            var output_operation = graph.OperationByName(output_name);

            var labels = File.ReadAllLines(Path.Join(dir, labelFile)); 
            var result_labels = new List<string>(); 
            var sw = new Stopwatch(); 

            using (var sess = tf.Session(graph)) 
            {
                foreach (var nd in file_ndarrays)
                {
                    sw.Restart();

                    var results = sess.run(output_operation.outputs[0], (input_operation.outputs[0], nd));  // the error happens here
                    results = np.squeeze(results); 
                    int idx = np.argmax(results); 

                    Console.WriteLine($"{labels[idx]} {results[idx]} in {sw.ElapsedMilliseconds}ms", Color.Tan); 
                    result_labels.Add(labels[idx]); 
                }
            }

            return result_labels.Contains("Blues"); 
        }

        private NDArray ReadTensorFromImageFile(string file_name, 
                                int input_height = 288,
                                int input_width = 432,
                                int input_mean = 0,
                                int input_std = 255)
        {
            var graph = tf.Graph().as_default();

            var file_reader = tf.io.read_file(file_name, "file_reader");
            var decodeJpeg = tf.image.decode_jpeg(file_reader, channels: 3, name: "DecodeJpeg");
            var cast = tf.cast(decodeJpeg, tf.float32);
            var dims_expander = tf.expand_dims(cast, 0);
            var resize = tf.constant(new int[] { input_height, input_width });
            var bilinear = tf.image.resize_bilinear(dims_expander, resize);
            var sub = tf.subtract(bilinear, new float[] { input_mean });
            var normalized = tf.divide(sub, new float[] { input_std });

            using (var sess = tf.Session(graph))
                return sess.run(normalized);
        }

        public override void PrepareData()
        {
            //DirectoryInfo di = new DirectoryInfo(dir);
            //di.Delete(true);
            Directory.CreateDirectory(dir);

            // get model file
            string url = "https://github.com/dorodorodororo/trainedmodel/raw/main/trainedmodel.zip"; //my own trained model

            Web.Download(url, dir, "trainedmodel.zip");

            Compress.UnZip(Path.Join(dir, "trainedmodel.zip"), dir);

            // download sample picture
            Directory.CreateDirectory(Path.Join(dir, "img"));


            url = $"https://raw.githubusercontent.com/dorodorodororo/Csharp/main/blues00081.jpg";
            Web.Download(url, Path.Join(dir, "img"), "blues00081.jpg");


            // load image file
            var files = Directory.GetFiles(Path.Join(dir, "img"));
            for (int i = 0; i < files.Length; i++)
            {
                var nd = ReadTensorFromImageFile(files[i]);
                file_ndarrays.Add(nd);
            }
        }
    }
}

It says it expected a dimension of 1, but mine is 3.

So I brought it on Netron and got this flowchart. (It's too large so I just got the output node.)

image

I don't know how to fix this problem.

Can fix this problem by using a V3 model in training instead of V1? (I made this .pb file using V1 model and I think you used model using V3)

I'd appreciate it if you could give me any suggestions or comments.

Thank you
Have a nice day!

Example not compiling

Just downloaded the full Example project and ran the command:

dotnet run --project src/TensorFlowNET.Examples -ex "Linear Regression".

I got the following errors:

C:\Users\User\Documents\Projects\Internal\Vision\SciSharp-Stack-Examples-master>dotnet run --project src/TensorFlowNET.Examples -ex "Linear Regression"
ImageProcessing\DigitRecognitionRnnKeras.cs(130,41): error CS0246: The type or namespace name 'Tensors' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\User\Documents\Projects\Internal\Vision\SciSharp-Stack-Examples-master\src\TensorFlowNET.Examples\TensorFlowNET.Examples.csproj]
ImageProcessing\DigitRecognitionRnnKeras.cs(130,28): error CS0246: The type or namespace name 'Tensors' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\User\Documents\Projects\Internal\Vision\SciSharp-Stack-Examples-master\src\TensorFlowNET.Examples\TensorFlowNET.Examples.csproj]
NeuralNetworks\FullyConnectedKeras.cs(178,45): error CS0246: The type or namespace name 'Tensors' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\User\Documents\Projects\Internal\Vision\SciSharp-Stack-Examples-master\src\TensorFlowNET.Examples\TensorFlowNET.Examples.csproj]
NeuralNetworks\FullyConnectedKeras.cs(178,32): error CS0246: The type or namespace name 'Tensors' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\User\Documents\Projects\Internal\Vision\SciSharp-Stack-Examples-master\src\TensorFlowNET.Examples\TensorFlowNET.Examples.csproj]
ImageProcessing\DigitRecognitionCnnKeras.cs(207,45): error CS0246: The type or namespace name 'Tensors' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\User\Documents\Projects\Internal\Vision\SciSharp-Stack-Examples-master\src\TensorFlowNET.Examples\TensorFlowNET.Examples.csproj]
ImageProcessing\DigitRecognitionCnnKeras.cs(207,32): error CS0246: The type or namespace name 'Tensors' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\User\Documents\Projects\Internal\Vision\SciSharp-Stack-Examples-master\src\TensorFlowNET.Examples\TensorFlowNET.Examples.csproj]
ImageProcessing\TransferLearningWithInceptionV3.cs(512,21): warning CS0114: 'TransferLearningWithInceptionV3.PrepareData()' hides inherited member 'SciSharpExample.PrepareData()'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword. [C:\Users\User\Documents\Projects\Internal\Vision\SciSharp-Stack-Examples-master\src\TensorFlowNET.Examples\TensorFlowNET.Examples.csproj]
ImageProcessing\TransferLearningWithInceptionV3.cs(591,22): warning CS0114: 'TransferLearningWithInceptionV3.ImportGraph()' hides inherited member 'SciSharpExample.ImportGraph()'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword. [C:\Users\User\Documents\Projects\Internal\Vision\SciSharp-Stack-Examples-master\src\TensorFlowNET.Examples\TensorFlowNET.Examples.csproj]
TextProcessing\CnnTextClassification.cs(188,22): warning CS0114: 'CnnTextClassification.BuildGraph()' hides inherited member 'SciSharpExample.BuildGraph()'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword. [C:\Users\User\Documents\Projects\Internal\Vision\SciSharp-Stack-Examples-master\src\TensorFlowNET.Examples\TensorFlowNET.Examples.csproj]

The build failed. Fix the build errors and run again.

I get the same errors compiling in VisualStudio.

Using VS Version 16.7.5

I updated the NuGet packages too, no results.

Am I missing something?

Axl

Download the project that cannot run C #.

很多错误,这是其中之一。
严重性 代码 说明 项目 文件 行 禁止显示状态
错误 CS0111 类型“CIFAR10_CNN”已定义了一个名为“Run”的具有相同参数类型的成员 TensorFlowNET.Examples C:\Users\47013\Desktop\临时\SciSharp-Stack-Examples-master\src\TensorFlowNET.Examples\ImageProcessing\ToyResNet.cs 43 活动

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.