Skip to content

Commit 79b17b9

Browse files
committed
More API touchups
1 parent 50aacfa commit 79b17b9

5 files changed

Lines changed: 114 additions & 2 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//
2+
// Image compression using neural networks
3+
//
4+
// From models/compression/encoder.py
5+
//
6+
open System
7+
open System.IO
8+
open TensorFlow
9+
10+
let input = "example.png"
11+
let iteration = 15
12+
let output_codes = None
13+
let model = "../../compression_residual_gru/residual_gru.pb"
14+
15+
let opt x =
16+
System.Nullable x
17+
18+
// Convenience functiosn to create tensor constants from an integer and a float
19+
let iconst (graph:TFGraph) (v:int) (label:string) =
20+
graph.Const (TFTensor.op_Implicit (v), label)
21+
22+
let input_tensor_names =
23+
[| for a in 0 .. 16 do yield sprintf "loop_%02d/add:0" a |]
24+
25+
let output_tensor_names =
26+
let first = [|"GruBinarizer/SignBinarizer/Sign:0" |]
27+
Seq.append first [| for a in 0 .. 16 do yield sprintf "GruBinarizer/SignBinarizer/Sign_%d:0" a |] |> Seq.toArray
28+
29+
[<EntryPoint>]
30+
let main argv =
31+
use graph = new TFGraph()
32+
graph.Import (new TFBuffer (File.ReadAllBytes (model)))
33+
let input_tensor = graph.["Placeholder:0"]
34+
let outputs = [| for name in output_tensor_names do yield graph.[name] |];
35+
let input_image = graph.Placeholder TFDataType.String
36+
37+
let decoded_image = if Path.GetExtension (input) = ".png" then graph.DecodePng (input_image, channels = opt 3L) else graph.DecodeJpeg (input_image, channels = opt 3L)
38+
let expanded_image = graph.ExpandDims (decoded_image, iconst graph 0 "zero")
39+
40+
use session = new TFSession (graph)
41+
42+
0 // return an integer exit code
43+
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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>{5A493E1F-407D-4A3B-AF9B-A0F2930C1C18}</ProjectGuid>
7+
<OutputType>Exe</OutputType>
8+
<RootNamespace>ImageCompression</RootNamespace>
9+
<AssemblyName>ImageCompression</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="ImageCompression.fs" />
46+
</ItemGroup>
47+
<ItemGroup>
48+
<Content Include="example.png">
49+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
50+
</Content>
51+
</ItemGroup>
52+
<ItemGroup>
53+
<ProjectReference Include="..\..\TensorFlowSharp\TensorFlowSharp.csproj">
54+
<Project>{0264C321-34F4-46AF-819E-168D1E597232}</Project>
55+
<Name>TensorFlowSharp</Name>
56+
</ProjectReference>
57+
</ItemGroup>
58+
<Import Project="$(FSharpTargetsPath)" />
59+
</Project>
3.01 MB
Loading

TensorFlowSharp.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ Project("{f2a71f9b-5d33-465a-a702-920d77279786}") = "FSharpExampleInceptionInfer
1515
EndProject
1616
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Examples", "Examples", "{674EC1D7-9649-462E-A7A8-93D0DE84FE64}"
1717
EndProject
18+
Project("{f2a71f9b-5d33-465a-a702-920d77279786}") = "ImageCompression", "Examples\ImageCompression\ImageCompression.fsproj", "{5A493E1F-407D-4A3B-AF9B-A0F2930C1C18}"
19+
EndProject
1820
Global
1921
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2022
Debug|Any CPU = Debug|Any CPU
@@ -45,9 +47,14 @@ Global
4547
{03FB7F3A-6D24-4033-9B04-69AD8A198CCF}.Debug|Any CPU.Build.0 = Debug|Any CPU
4648
{03FB7F3A-6D24-4033-9B04-69AD8A198CCF}.Release|Any CPU.ActiveCfg = Release|Any CPU
4749
{03FB7F3A-6D24-4033-9B04-69AD8A198CCF}.Release|Any CPU.Build.0 = Release|Any CPU
50+
{5A493E1F-407D-4A3B-AF9B-A0F2930C1C18}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
51+
{5A493E1F-407D-4A3B-AF9B-A0F2930C1C18}.Debug|Any CPU.Build.0 = Debug|Any CPU
52+
{5A493E1F-407D-4A3B-AF9B-A0F2930C1C18}.Release|Any CPU.ActiveCfg = Release|Any CPU
53+
{5A493E1F-407D-4A3B-AF9B-A0F2930C1C18}.Release|Any CPU.Build.0 = Release|Any CPU
4854
EndGlobalSection
4955
GlobalSection(NestedProjects) = preSolution
5056
{069A6736-7711-4805-8660-A267E713BC54} = {674EC1D7-9649-462E-A7A8-93D0DE84FE64}
5157
{03FB7F3A-6D24-4033-9B04-69AD8A198CCF} = {674EC1D7-9649-462E-A7A8-93D0DE84FE64}
58+
{5A493E1F-407D-4A3B-AF9B-A0F2930C1C18} = {674EC1D7-9649-462E-A7A8-93D0DE84FE64}
5259
EndGlobalSection
5360
EndGlobal

TensorFlowSharp/Tensorflow.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,9 @@ static TFBuffer ()
248248
FreeBufferFunc = Marshal.GetFunctionPointerForDelegate<Action<IntPtr,IntPtr>> (FreeBlock);
249249
}
250250

251+
// This constructor makes a copy of the data
252+
public TFBuffer (byte [] buffer) : this (buffer, 0, buffer.Length) { }
253+
251254
// This constructor makes a copy of the data
252255
public TFBuffer (byte [] buffer, int start, int count) : this ()
253256
{
@@ -1065,7 +1068,7 @@ public void ToGraphDef (TFBuffer outputGraphDef, TFStatus status = null)
10651068
[DllImport (NativeBinding.TensorFlowLibrary)]
10661069
static extern unsafe void TF_GraphImportGraphDef (TF_Graph graph, LLBuffer* graph_def, TF_ImportGraphDefOptions options, TF_Status status);
10671070

1068-
public void Import (TFBuffer graphDef, string prefix, TFStatus status = null)
1071+
public void Import (TFBuffer graphDef, string prefix = "", TFStatus status = null)
10691072
{
10701073
if (handle == IntPtr.Zero)
10711074
ObjectDisposedException ();
@@ -1097,7 +1100,7 @@ public void Import (TFBuffer graphDef, TFImportGraphDefOptions options, TFStatus
10971100
cstatus.CheckMaybeRaise (status);
10981101
}
10991102

1100-
public void Import (byte [] buffer, string prefix, TFStatus status = null)
1103+
public void Import (byte [] buffer, string prefix = "", TFStatus status = null)
11011104
{
11021105
if (handle == IntPtr.Zero)
11031106
ObjectDisposedException ();

0 commit comments

Comments
 (0)