TensorFlow.NET icon indicating copy to clipboard operation
TensorFlow.NET copied to clipboard

Winform Support

Open veeru612 opened this issue 5 years ago • 4 comments

Hi, Thanks for the great package and easy to follow examples. When I ran a mock model in console application - I get nicely callback output (verbose 1, epoch runs) in the console window. However, when I tried the same in a winform application, there is no output. It still compiles and runs but there is no output. For eg:

public void PrepareData()
      {
          int batch_size = 32;
          TensorShape img_dim = (180, 180);
          //var data_dir = @"C:/Users/haipi/.keras/datasets/flower_photos";
          var data_dir = @"C:\flowers";
          var train_ds = keras.preprocessing.image_dataset_from_directory(data_dir,  validation_split: 0.2f,
              subset: "training",
              seed: 123,
              image_size: img_dim,
              batch_size: batch_size);                       

          var val_ds = keras.preprocessing.image_dataset_from_directory(data_dir,
              validation_split: 0.2f,
              subset: "validation",
              seed: 123,
              image_size: img_dim,
              batch_size: batch_size);           

          train_ds = train_ds.cache().shuffle(100).prefetch(buffer_size: -1);
          val_ds = val_ds.cache().prefetch(buffer_size: -1);

          foreach (var (img, label) in train_ds)
          {
              print("batch images: " + img.TensorShape);
              print("labels: " + label);
              tbConsole.AppendText("batch images: " + img.TensorShape);
              tbConsole.AppendText(Environment.NewLine);
              tbConsole.AppendText("labels: " + label);
              tbConsole.AppendText(Environment.NewLine);
          }

          int num_classes = 5;
          // var normalization_layer = tf.keras.layers.Rescaling(1.0f / 255);
          var layers = keras.layers;
          var model = keras.Sequential(new List<ILayer>
          {
              layers.Rescaling(1.0f / 255, input_shape: (img_dim.dims[0], img_dim.dims[1], 3)),
              layers.Conv2D(16, 3, padding: "same", activation: keras.activations.Relu),
              layers.MaxPooling2D(),
              /*layers.Conv2D(32, 3, padding: "same", activation: "relu"),
              layers.MaxPooling2D(),
              layers.Conv2D(64, 3, padding: "same", activation: "relu"),
              layers.MaxPooling2D(),*/
              layers.Flatten(),
              layers.Dense(128, activation: keras.activations.Relu),
              layers.Dense(num_classes)
          });
          model.compile(loss: keras.losses.SparseCategoricalCrossentropy(from_logits: true),
          optimizer: keras.optimizers.Adam(), metrics: new[] { "accuracy" });

         model.summary();           
         model.fit(train_ds, validation_data: val_ds, epochs: 2, verbose: 1);                       

      }

image

I would like to know if there is a way to get the console outputs in a winform application.

Thanks, Veeru

veeru612 avatar Feb 23 '21 19:02 veeru612

I've created a PR #764 which could solve your problem.

lsylusiyao avatar Feb 25 '21 03:02 lsylusiyao

Hi lsylusiyao, Thank you for creating pull request for solving the problem. I have a very stupid (newbie) question, how can I use your code change in my project - should I download the repository, compile and then import the dll into my project ?

veeru612 avatar Feb 25 '21 18:02 veeru612

I downloaded the repository and tried packing tensorflow.keras to generate TensorFlow.Keras.0.5.0.nupkg in the debug folder of the project but I was not able to install it on my project. I am sure I am missing something, new to this so it will take time. I appreciate your help. Thanks,

veeru612 avatar Feb 25 '21 19:02 veeru612

It seems that you've already finished compiling and created the useful nupkg. Then you can just search for how to install local nuget package in visual studio and you'll find the answer. :)

lsylusiyao avatar Feb 26 '21 00:02 lsylusiyao