@@ -1,7 +1,10 @@ | |||
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 | |||
{ | |||
@@ -19,18 +22,42 @@ namespace TensorFlowNET.Examples.ImageProcess | |||
public string Name => "Image Background Removal"; | |||
string modelDir = "deeplabv3"; | |||
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 model file | |||
string url = "http://download.tensorflow.org/models/deeplabv3_mnv2_pascal_train_aug_2018_01_29.tar.gz"; | |||
Web.Download(url, modelDir, "deeplabv3_mnv2_pascal_train_aug_2018_01_29.tar.gz"); | |||
// 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);*/ | |||
} | |||
} | |||
} |
@@ -23,52 +23,45 @@ namespace TensorFlowNET.Examples.Utility | |||
string line; | |||
string newText = "{\"items\":["; | |||
try | |||
using (System.IO.StreamReader reader = new System.IO.StreamReader(filePath)) | |||
{ | |||
using (System.IO.StreamReader reader = new System.IO.StreamReader(filePath)) | |||
while ((line = reader.ReadLine()) != null) | |||
{ | |||
string newline = string.Empty; | |||
while ((line = reader.ReadLine()) != null) | |||
if (line.Contains("{")) | |||
{ | |||
string newline = string.Empty; | |||
if (line.Contains("{")) | |||
{ | |||
newline = line.Replace("item", "").Trim(); | |||
//newText += line.Insert(line.IndexOf("=") + 1, "\"") + "\","; | |||
newText += newline; | |||
} | |||
else if (line.Contains("}")) | |||
{ | |||
newText = newText.Remove(newText.Length - 1); | |||
newText += line; | |||
newText += ","; | |||
} | |||
else | |||
{ | |||
newline = line.Replace(":", "\":").Trim(); | |||
newline = "\"" + newline;// newline.Insert(0, "\""); | |||
newline += ","; | |||
newText += newline; | |||
} | |||
newline = line.Replace("item", "").Trim(); | |||
//newText += line.Insert(line.IndexOf("=") + 1, "\"") + "\","; | |||
newText += newline; | |||
} | |||
else if (line.Contains("}")) | |||
{ | |||
newText = newText.Remove(newText.Length - 1); | |||
newText += line; | |||
newText += ","; | |||
} | |||
else | |||
{ | |||
newline = line.Replace(":", "\":").Trim(); | |||
newline = "\"" + newline;// newline.Insert(0, "\""); | |||
newline += ","; | |||
newText = newText.Remove(newText.Length - 1); | |||
newText += "]}"; | |||
newText += newline; | |||
} | |||
reader.Close(); | |||
} | |||
PbtxtItems items = JsonConvert.DeserializeObject<PbtxtItems>(newText); | |||
newText = newText.Remove(newText.Length - 1); | |||
newText += "]}"; | |||
return items; | |||
} | |||
catch (Exception ex) | |||
{ | |||
return null; | |||
reader.Close(); | |||
} | |||
PbtxtItems items = JsonConvert.DeserializeObject<PbtxtItems>(newText); | |||
return items; | |||
} | |||
} | |||
} |