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