Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathImageRecognitionInception.cs
More file actions
Latest commit
117 lines (94 loc) · 4.08 KB
/
Copy pathImageRecognitionInception.cs
File metadata and controls
117 lines (94 loc) · 4.08 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
usingSystem.Collections.Generic;
usingSystem.Diagnostics;
usingSystem.Drawing;
usingSystem.IO;
usingTensorflow;
usingTensorflow.Keras.Utils;
usingTensorflow.NumPy;
usingstaticTensorflow.Binding;
usingConsole=Colorful.Console;
namespaceTensorFlowNET.Examples;
/// <summary>
/// Inception v3 is a widely-used image recognition model
/// that has been shown to attain greater than 78.1% accuracy on the ImageNet dataset.
/// The model is the culmination of many ideas developed by multiple researchers over the years.
/// </summary>
publicclassImageRecognitionInception:SciSharpExample,IExample
{
stringdir="ImageRecognitionInception";
stringpbFile="tensorflow_inception_graph.pb";
stringlabelFile="imagenet_comp_graph_label_strings.txt";
List<NDArray>file_ndarrays=newList<NDArray>();
publicExampleConfigInitConfig()
=>Config=newExampleConfig
{
Name="Image Recognition Inception",
Enabled=true,
IsImportingGraph=false
};
publicboolRun()
{
tf.compat.v1.disable_eager_execution();
PrepareData();
vargraph=tf.Graph().as_default();
//import GraphDef from pb file
graph.Import(Path.Join(dir,pbFile));
varinput_name="input";
varoutput_name="output";
varinput_operation=graph.OperationByName(input_name);
varoutput_operation=graph.OperationByName(output_name);
varlabels=File.ReadAllLines(Path.Join(dir,labelFile));
varresult_labels=newList<string>();
varsw=newStopwatch();
varsess=tf.Session(graph);
foreach(varndinfile_ndarrays)
{
sw.Restart();
varresults=sess.run(output_operation.outputs[0],(input_operation.outputs[0],nd));
results=np.squeeze(results);
intidx=np.argmax(results);
Console.WriteLine($"{labels[idx]}{results[idx]} in {sw.ElapsedMilliseconds}ms",Color.Tan);
result_labels.Add(labels[idx]);
}
returnresult_labels.Contains("military uniform");
}
privateNDArrayReadTensorFromImageFile(stringfile_name,
intinput_height=224,
intinput_width=224,
intinput_mean=117,
intinput_std=1)
{
vargraph=tf.Graph().as_default();
varfile_reader=tf.io.read_file(file_name,"file_reader");
vardecodeJpeg=tf.image.decode_jpeg(file_reader,channels:3,name:"DecodeJpeg");
varcast=tf.cast(decodeJpeg,tf.float32);
vardims_expander=tf.expand_dims(cast,0);
varresize=tf.constant(newint[]{input_height,input_width});
varbilinear=tf.image.resize_bilinear(dims_expander,resize);
varsub=tf.subtract(bilinear,newfloat[]{input_mean});
varnormalized=tf.divide(sub,newfloat[]{input_std});
varsess=tf.Session(graph);
returnsess.run(normalized);
}
publicoverridevoidPrepareData()
{
Directory.CreateDirectory(dir);
// get model file
stringurl="https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip";
Web.Download(url,dir,"inception5h.zip");
Compress.UnZip(Path.Join(dir,"inception5h.zip"),dir);
// download sample picture
Directory.CreateDirectory(Path.Join(dir,"img"));
url=$"https://raw.githubusercontent.com/tensorflow/tensorflow/master/tensorflow/examples/label_image/data/grace_hopper.jpg";
Web.Download(url,Path.Join(dir,"img"),"grace_hopper.jpg");
url=$"https://raw.githubusercontent.com/SciSharp/TensorFlow.NET/master/data/shasta-daisy.jpg";
Web.Download(url,Path.Join(dir,"img"),"shasta-daisy.jpg");
// load image file
varfiles=Directory.GetFiles(Path.Join(dir,"img"));
for(inti=0;i<files.Length;i++)
{
varnd=ReadTensorFromImageFile(files[i]);
file_ndarrays.Add(nd);
}
}
}