-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathImageBackgroundRemoval.cs
More file actions
58 lines (48 loc) · 1.79 KB
/
Copy pathImageBackgroundRemoval.cs
File metadata and controls
58 lines (48 loc) · 1.79 KB
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
using System.IO;
using Tensorflow;
using Tensorflow.Keras.Utils;
using static Tensorflow.Binding;
namespace TensorFlowNET.Examples;
/// <summary>
/// This example removes the background from an input image.
///
/// https://github.com/susheelsk/image-background-removal
/// </summary>
public class ImageBackgroundRemoval : SciSharpExample, IExample
{
string dataDir = "deeplabv3";
string modelDir = "deeplabv3_mnv2_pascal_train_aug";
string modelName = "frozen_inference_graph.pb";
public ExampleConfig InitConfig()
=> Config = new ExampleConfig
{
Name = "Image Background Removal",
Enabled = false,
IsImportingGraph = true
};
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");
var sess = tf.Session(graph);
// Runs inference on a single image.
sess.run(output, new FeedItem(output, "[np.asarray(resized_image)]"));
return false;
}
public override 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);*/
}
}