Skip to content

Commit a3082e4

Browse files
committed
Start of F# sample
1 parent c3b3e08 commit a3082e4

File tree

4 files changed

+125
-0
lines changed

4 files changed

+125
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace FExampleInceptionInference
2+
open System.Reflection
3+
open System.Runtime.CompilerServices
4+
5+
[<assembly: AssemblyTitle("FExampleInceptionInference")>]
6+
[<assembly: AssemblyDescription("")>]
7+
[<assembly: AssemblyConfiguration("")>]
8+
[<assembly: AssemblyCompany("")>]
9+
[<assembly: AssemblyProduct("")>]
10+
[<assembly: AssemblyCopyright("miguel")>]
11+
[<assembly: AssemblyTrademark("")>]
12+
13+
// The assembly version has the format {Major}.{Minor}.{Build}.{Revision}
14+
15+
[<assembly: AssemblyVersion("1.0.0.0")>]
16+
17+
//[<assembly: AssemblyDelaySign(false)>]
18+
//[<assembly: AssemblyKeyFile("")>]
19+
20+
()
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 DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
6+
<ProjectGuid>{03FB7F3A-6D24-4033-9B04-69AD8A198CCF}</ProjectGuid>
7+
<OutputType>Exe</OutputType>
8+
<RootNamespace>FExampleInceptionInference</RootNamespace>
9+
<AssemblyName>FExampleInceptionInference</AssemblyName>
10+
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
11+
</PropertyGroup>
12+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
13+
<DebugSymbols>true</DebugSymbols>
14+
<DebugType>full</DebugType>
15+
<Optimize>false</Optimize>
16+
<OutputPath>bin\Debug</OutputPath>
17+
<DefineConstants>DEBUG</DefineConstants>
18+
<ErrorReport>prompt</ErrorReport>
19+
<ExternalConsole>true</ExternalConsole>
20+
<PlatformTarget></PlatformTarget>
21+
</PropertyGroup>
22+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
23+
<Optimize>true</Optimize>
24+
<OutputPath>bin\Release</OutputPath>
25+
<DefineConstants></DefineConstants>
26+
<ErrorReport>prompt</ErrorReport>
27+
<ExternalConsole>true</ExternalConsole>
28+
<GenerateTailCalls>true</GenerateTailCalls>
29+
<PlatformTarget></PlatformTarget>
30+
</PropertyGroup>
31+
<PropertyGroup>
32+
<FSharpTargetsPath>$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\FSharp\Microsoft.FSharp.Targets</FSharpTargetsPath>
33+
</PropertyGroup>
34+
<PropertyGroup Condition="'$(VisualStudioVersion)' == '10.0' OR '$(VisualStudioVersion)' == '11.0'">
35+
<FSharpTargetsPath>$(MSBuildExtensionsPath32)\..\Microsoft SDKs\F#\3.0\Framework\v4.0\Microsoft.FSharp.Targets</FSharpTargetsPath>
36+
</PropertyGroup>
37+
<ItemGroup>
38+
<Reference Include="mscorlib" />
39+
<Reference Include="FSharp.Core" />
40+
<Reference Include="System" />
41+
<Reference Include="System.Core" />
42+
<Reference Include="System.Numerics" />
43+
</ItemGroup>
44+
<ItemGroup>
45+
<Compile Include="AssemblyInfo.fs" />
46+
<Compile Include="Program.fs" />
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="$(FSharpTargetsPath)" />
55+
</Project>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//
2+
// Things to improve in the API
3+
// Need TFTensor constructors for the sake of F# as it does not use the implicit constructors
4+
// The C# Nullables are not surfaced in a way that makes it nice for F#, must manually call System.Nullable on it
5+
//
6+
open TensorFlow
7+
open System.IO
8+
9+
let iconst (graph:TFGraph) (v:int) (label:string) =
10+
graph.Const (TFTensor.op_Implicit (v), label)
11+
12+
let fconst (graph:TFGraph) (v:float32) (label:string) =
13+
graph.Const (TFTensor.op_Implicit (v), label)
14+
15+
let constructGraphToNormalizeImage =
16+
use graph = new TFGraph ()
17+
let input = graph.Placeholder TFDataType.String
18+
19+
let image = graph.Cast (graph.DecodeJpeg (contents = input, channels = System.Nullable 3L), DstT = TFDataType.Float)
20+
let expand = graph.ExpandDims (input = image, dim = graph.Const (TFTensor.op_Implicit (0), "make_batch"))
21+
let size = graph.Const (TFTensor.op_Implicit [|224;224 |], "size")
22+
let resize = graph.ResizeBilinear (images = expand, size = size)
23+
let mean = fconst graph 117.f "Mean"
24+
let scale = fconst graph 1.f "Scale"
25+
let output = graph.Div (x = graph.Sub (x = resize, y = mean), y = scale)
26+
(graph, input, output)
27+
28+
let createTensorFromImageFile f =
29+
let tensor = File.ReadAllBytes f |> TFTensor.CreateString
30+
let (graph, input, output) = constructGraphToNormalizeImage
31+
use session = new TFSession (graph)
32+
let normalized = session.Run (runOptions = null, inputs = [| input |], inputValues = [| tensor |], outputs = [| output |])
33+
normalized.[0]
34+
35+
36+
[<EntryPoint>]
37+
let main argv =
38+
use graph = new TFGraph()
39+
40+
graph.Import (File.ReadAllBytes ("/tmp/tensorflow_inception_graph.pb"), "")
41+
use session = new TFSession (graph)
42+
43+
0 // return an integer exit code
44+

TensorFlowSharp.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Learn", "Learn\Learn.csproj
1111
EndProject
1212
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExampleInceptionInference", "ExampleInceptionInference\ExampleInceptionInference.csproj", "{069A6736-7711-4805-8660-A267E713BC54}"
1313
EndProject
14+
Project("{f2a71f9b-5d33-465a-a702-920d77279786}") = "FExampleInceptionInference", "FExampleInceptionInference\FExampleInceptionInference.fsproj", "{03FB7F3A-6D24-4033-9B04-69AD8A198CCF}"
15+
EndProject
1416
Global
1517
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1618
Debug|Any CPU = Debug|Any CPU
@@ -37,5 +39,9 @@ Global
3739
{069A6736-7711-4805-8660-A267E713BC54}.Debug|Any CPU.Build.0 = Debug|Any CPU
3840
{069A6736-7711-4805-8660-A267E713BC54}.Release|Any CPU.ActiveCfg = Release|Any CPU
3941
{069A6736-7711-4805-8660-A267E713BC54}.Release|Any CPU.Build.0 = Release|Any CPU
42+
{03FB7F3A-6D24-4033-9B04-69AD8A198CCF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
43+
{03FB7F3A-6D24-4033-9B04-69AD8A198CCF}.Debug|Any CPU.Build.0 = Debug|Any CPU
44+
{03FB7F3A-6D24-4033-9B04-69AD8A198CCF}.Release|Any CPU.ActiveCfg = Release|Any CPU
45+
{03FB7F3A-6D24-4033-9B04-69AD8A198CCF}.Release|Any CPU.Build.0 = Release|Any CPU
4046
EndGlobalSection
4147
EndGlobal

0 commit comments

Comments
 (0)