Skip to content

Commit 81a059e

Browse files
Dorokhovmigueldeicaza
authored andcommitted
Object detection sample (migueldeicaza#138)
* Added F# unit test for tensorflow arithmetic operation * Added Travis CI status * Added Travis CI status * fixed Travis config * added libtensorflow.dylib * fixed Travis config * downgraded xunit to v1.9.2 * broken test to check CI report about failed tests * xunit 2.2.0 * xunit runner 2.0.0 * xunit runner 2.0.0 * xunit runner 2.1.0 * xunit runner 2.1.0 * libtensorflow.so * added dllmap * dllmap * dllmap * dllmap * dllmap * dllmap * shared library * fixed config * xslt * log level * config * config * travis * travis * travis * travis * travis * travis * travis * travis * travis * travis * x64 * travis * travis * travis * travis * travis * travis * travis * travis * travis * travis * travis * travis * travis * travis * travis * travis * travis * travis * removed libs * travis * travis * travis * travis travis * travis * removed ref to so * fixed test * travis switched to migueldeicaza * removed libtensorflow.dll * fixed test runner path * travis * travis * travis * removed Script.fsx * removed .runsettings * removed App.config * removed .nuspec * removed App.config * travis * specified lang version * latest mono * mono 5.0.0 * returned build status back * removed compiler version * make payload * travis * fixed regression * added padding FIFO queue * added PaddingFIFOQueue * WIP: optimizer * object detection * reuse ImageUtil * removed unused items and added more comments * reverted unnecessary changes * removed queue related changes
1 parent 10ed768 commit 81a059e

File tree

18 files changed

+591
-77
lines changed

18 files changed

+591
-77
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System.Collections.Generic;
2+
using System.IO;
3+
using System.Text.RegularExpressions;
4+
5+
namespace ExampleCommon
6+
{
7+
public static class CatalogUtil
8+
{
9+
private static string CATALOG_ITEM_PATTERN = @"item {\r\n name: ""(?<name>.*)""\r\n id: (?<id>\d+)\r\n display_name: ""(?<displayName>.*)""\r\n}";
10+
11+
/// <summary>
12+
/// Reads catalog of well-known objects from text file.
13+
/// </summary>
14+
/// <param name="file">path to the text file</param>
15+
/// <returns>collection of items</returns>
16+
public static IEnumerable<CatalogItem> ReadCatalogItems (string file)
17+
{
18+
using (FileStream stream = File.OpenRead (file))
19+
using (StreamReader reader = new StreamReader (stream)) {
20+
string text = reader.ReadToEnd ();
21+
if (string.IsNullOrWhiteSpace (text)) {
22+
yield break;
23+
}
24+
25+
Regex regex = new Regex (CATALOG_ITEM_PATTERN);
26+
var matches = regex.Matches (text);
27+
foreach (Match match in matches) {
28+
var name = match.Groups [1].Value;
29+
var id = int.Parse (match.Groups [2].Value);
30+
var displayName = match.Groups [3].Value;
31+
32+
yield return new CatalogItem () {
33+
Id = id,
34+
Name = name,
35+
DisplayName = displayName
36+
};
37+
}
38+
}
39+
}
40+
}
41+
42+
public class CatalogItem
43+
{
44+
public int Id { get; set; }
45+
public string Name { get; set; }
46+
public string DisplayName { get; set; }
47+
}
48+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{116BA176-F67C-4066-8685-C080705BAA16}</ProjectGuid>
8+
<OutputType>Library</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>ExampleCommon</RootNamespace>
11+
<AssemblyName>ExampleCommon</AssemblyName>
12+
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
<TargetFrameworkProfile />
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<DebugSymbols>true</DebugSymbols>
18+
<DebugType>full</DebugType>
19+
<Optimize>false</Optimize>
20+
<OutputPath>bin\Debug\</OutputPath>
21+
<DefineConstants>DEBUG;TRACE</DefineConstants>
22+
<ErrorReport>prompt</ErrorReport>
23+
<WarningLevel>4</WarningLevel>
24+
</PropertyGroup>
25+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
26+
<DebugType>pdbonly</DebugType>
27+
<Optimize>true</Optimize>
28+
<OutputPath>bin\Release\</OutputPath>
29+
<DefineConstants>TRACE</DefineConstants>
30+
<ErrorReport>prompt</ErrorReport>
31+
<WarningLevel>4</WarningLevel>
32+
</PropertyGroup>
33+
<ItemGroup>
34+
<Reference Include="System" />
35+
<Reference Include="System.Core" />
36+
<Reference Include="System.Xml.Linq" />
37+
<Reference Include="System.Data.DataSetExtensions" />
38+
<Reference Include="Microsoft.CSharp" />
39+
<Reference Include="System.Data" />
40+
<Reference Include="System.Net.Http" />
41+
<Reference Include="System.Xml" />
42+
</ItemGroup>
43+
<ItemGroup>
44+
<Compile Include="CatalogUtil.cs" />
45+
<Compile Include="ImageUtil.cs" />
46+
<Compile Include="Properties\AssemblyInfo.cs" />
47+
</ItemGroup>
48+
<ItemGroup>
49+
<ProjectReference Include="..\..\TensorFlowSharp\TensorFlowSharp.csproj">
50+
<Project>{0264c321-34f4-46af-819e-168d1e597232}</Project>
51+
<Name>TensorFlowSharp</Name>
52+
</ProjectReference>
53+
</ItemGroup>
54+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
55+
</Project>
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using System.IO;
2+
using TensorFlow;
3+
4+
namespace ExampleCommon
5+
{
6+
public static class ImageUtil
7+
{
8+
// Convert the image in filename to a Tensor suitable as input to the Inception model.
9+
public static TFTensor CreateTensorFromImageFile (string file, TFDataType destinationDataType = TFDataType.Float)
10+
{
11+
var contents = File.ReadAllBytes (file);
12+
13+
// DecodeJpeg uses a scalar String-valued tensor as input.
14+
var tensor = TFTensor.CreateString (contents);
15+
16+
TFGraph graph;
17+
TFOutput input, output;
18+
19+
// Construct a graph to normalize the image
20+
ConstructGraphToNormalizeImage (out graph, out input, out output, destinationDataType);
21+
22+
// Execute that graph to normalize this one image
23+
using (var session = new TFSession (graph)) {
24+
var normalized = session.Run (
25+
inputs: new [] { input },
26+
inputValues: new [] { tensor },
27+
outputs: new [] { output });
28+
29+
return normalized [0];
30+
}
31+
}
32+
33+
// The inception model takes as input the image described by a Tensor in a very
34+
// specific normalized format (a particular image size, shape of the input tensor,
35+
// normalized pixel values etc.).
36+
//
37+
// This function constructs a graph of TensorFlow operations which takes as
38+
// input a JPEG-encoded string and returns a tensor suitable as input to the
39+
// inception model.
40+
private static void ConstructGraphToNormalizeImage (out TFGraph graph, out TFOutput input, out TFOutput output, TFDataType destinationDataType = TFDataType.Float)
41+
{
42+
// Some constants specific to the pre-trained model at:
43+
// https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip
44+
//
45+
// - The model was trained after with images scaled to 224x224 pixels.
46+
// - The colors, represented as R, G, B in 1-byte each were converted to
47+
// float using (value - Mean)/Scale.
48+
49+
const int W = 224;
50+
const int H = 224;
51+
const float Mean = 117;
52+
const float Scale = 1;
53+
54+
graph = new TFGraph ();
55+
input = graph.Placeholder (TFDataType.String);
56+
57+
output = graph.Cast (graph.Div (
58+
x: graph.Sub (
59+
x: graph.ResizeBilinear (
60+
images: graph.ExpandDims (
61+
input: graph.Cast (
62+
graph.DecodeJpeg (contents: input, channels: 3), DstT: TFDataType.Float),
63+
dim: graph.Const (0, "make_batch")),
64+
size: graph.Const (new int [] { W, H }, "size")),
65+
y: graph.Const (Mean, "mean")),
66+
y: graph.Const (Scale, "scale")), destinationDataType);
67+
}
68+
}
69+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("ExampleCommon")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("ExampleCommon")]
13+
[assembly: AssemblyCopyright("Copyright © 2017")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("116ba176-f67c-4066-8685-c080705baa16")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

Examples/ExampleInceptionInference/ExampleInceptionInference.csproj

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<PropertyGroup>
44
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@@ -47,9 +47,13 @@
4747
<Project>{0264C321-34F4-46AF-819E-168D1E597232}</Project>
4848
<Name>TensorFlowSharp</Name>
4949
</ProjectReference>
50+
<ProjectReference Include="..\ExampleCommon\ExampleCommon.csproj">
51+
<Project>{116BA176-F67C-4066-8685-C080705BAA16}</Project>
52+
<Name>ExampleCommon</Name>
53+
</ProjectReference>
5054
</ItemGroup>
5155
<ItemGroup>
5256
<None Include="packages.config" />
5357
</ItemGroup>
5458
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
55-
</Project>
59+
</Project>

Examples/ExampleInceptionInference/Program.cs

Lines changed: 3 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
using System.IO.Compression;
3737
using System.Net;
3838
using System.Collections.Generic;
39+
using ExampleCommon;
3940

4041
namespace ExampleInceptionInference
4142
{
@@ -94,7 +95,7 @@ public static void Main (string [] args)
9495
// For multiple images, session.Run() can be called in a loop (and
9596
// concurrently). Alternatively, images can be batched since the model
9697
// accepts batches of image data as input.
97-
var tensor = CreateTensorFromImageFile (file);
98+
var tensor = ImageUtil.CreateTensorFromImageFile (file);
9899

99100
var runner = session.GetRunner ();
100101
runner.AddInput (graph ["input"] [0], tensor).Fetch (graph ["output"] [0]);
@@ -148,68 +149,7 @@ public static void Main (string [] args)
148149
}
149150
}
150151
}
151-
152-
// Convert the image in filename to a Tensor suitable as input to the Inception model.
153-
static TFTensor CreateTensorFromImageFile (string file)
154-
{
155-
var contents = File.ReadAllBytes (file);
156-
157-
// DecodeJpeg uses a scalar String-valued tensor as input.
158-
var tensor = TFTensor.CreateString (contents);
159-
160-
TFGraph graph;
161-
TFOutput input, output;
162-
163-
// Construct a graph to normalize the image
164-
ConstructGraphToNormalizeImage (out graph, out input, out output);
165-
166-
// Execute that graph to normalize this one image
167-
using (var session = new TFSession (graph)) {
168-
var normalized = session.Run (
169-
inputs: new [] { input },
170-
inputValues: new [] { tensor },
171-
outputs: new [] { output });
172-
173-
return normalized [0];
174-
}
175-
}
176-
177-
// The inception model takes as input the image described by a Tensor in a very
178-
// specific normalized format (a particular image size, shape of the input tensor,
179-
// normalized pixel values etc.).
180-
//
181-
// This function constructs a graph of TensorFlow operations which takes as
182-
// input a JPEG-encoded string and returns a tensor suitable as input to the
183-
// inception model.
184-
static void ConstructGraphToNormalizeImage (out TFGraph graph, out TFOutput input, out TFOutput output)
185-
{
186-
// Some constants specific to the pre-trained model at:
187-
// https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip
188-
//
189-
// - The model was trained after with images scaled to 224x224 pixels.
190-
// - The colors, represented as R, G, B in 1-byte each were converted to
191-
// float using (value - Mean)/Scale.
192-
193-
const int W = 224;
194-
const int H = 224;
195-
const float Mean = 117;
196-
const float Scale = 1;
197-
198-
graph = new TFGraph ();
199-
input = graph.Placeholder (TFDataType.String);
200-
201-
output = graph.Div (
202-
x: graph.Sub (
203-
x: graph.ResizeBilinear (
204-
images: graph.ExpandDims (
205-
input: graph.Cast (
206-
graph.DecodeJpeg (contents: input, channels: 3), DstT: TFDataType.Float),
207-
dim: graph.Const (0, "make_batch")),
208-
size: graph.Const (new int [] { W, H }, "size")),
209-
y: graph.Const (Mean, "mean")),
210-
y: graph.Const (Scale, "scale"));
211-
}
212-
152+
213153
//
214154
// Downloads the inception graph and labels
215155
//
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/>
5+
</startup>
6+
</configuration>

0 commit comments

Comments
 (0)