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.5 kB

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