You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

ImageBackgroundRemoval.cs 2.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. using Tensorflow;
  6. using TensorFlowNET.Examples.Utility;
  7. using static Tensorflow.Python;
  8. namespace TensorFlowNET.Examples.ImageProcess
  9. {
  10. /// <summary>
  11. /// This example removes the background from an input image.
  12. ///
  13. /// https://github.com/susheelsk/image-background-removal
  14. /// </summary>
  15. public class ImageBackgroundRemoval : IExample
  16. {
  17. public bool Enabled { get; set; } = true;
  18. public bool IsImportingGraph { get; set; } = true;
  19. public string Name => "Image Background Removal";
  20. string dataDir = "deeplabv3";
  21. string modelDir = "deeplabv3_mnv2_pascal_train_aug";
  22. string modelName = "frozen_inference_graph.pb";
  23. public bool Run()
  24. {
  25. PrepareData();
  26. // import GraphDef from pb file
  27. var graph = new Graph().as_default();
  28. graph.Import(Path.Join(dataDir, modelDir, modelName));
  29. Tensor output = graph.OperationByName("SemanticPredictions");
  30. with(tf.Session(graph), sess =>
  31. {
  32. // Runs inference on a single image.
  33. sess.run(output, new FeedItem(output, "[np.asarray(resized_image)]"));
  34. });
  35. return false;
  36. }
  37. public void PrepareData()
  38. {
  39. // get mobile_net_model file
  40. string fileName = "deeplabv3_mnv2_pascal_train_aug_2018_01_29.tar.gz";
  41. string url = $"http://download.tensorflow.org/models/{fileName}";
  42. Web.Download(url, dataDir, fileName);
  43. Compress.ExtractTGZ(Path.Join(dataDir, fileName), dataDir);
  44. // xception_model, better accuracy
  45. /*fileName = "deeplabv3_pascal_train_aug_2018_01_04.tar.gz";
  46. url = $"http://download.tensorflow.org/models/{fileName}";
  47. Web.Download(url, modelDir, fileName);
  48. Compress.ExtractTGZ(Path.Join(modelDir, fileName), modelDir);*/
  49. }
  50. public Graph ImportGraph()
  51. {
  52. throw new NotImplementedException();
  53. }
  54. public Graph BuildGraph()
  55. {
  56. throw new NotImplementedException();
  57. }
  58. public bool Train()
  59. {
  60. throw new NotImplementedException();
  61. }
  62. public bool Predict()
  63. {
  64. throw new NotImplementedException();
  65. }
  66. }
  67. }