Skip to content

Commit 787c772

Browse files
committed
Merge branch 'master' into v0.11.x-numsharp
2 parents 4ff53c9 + 9dd61ab commit 787c772

13 files changed

Lines changed: 1016 additions & 13 deletions

File tree

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,14 @@ In comparison to other projects, like for instance TensorFlowSharp which only pr
2828

2929
Install TF.NET and TensorFlow binary through NuGet.
3030
```sh
31+
### install tensorflow C# binding
3132
PM> Install-Package TensorFlow.NET
33+
34+
### Install tensorflow binary
35+
### For CPU version
3236
PM> Install-Package SciSharp.TensorFlow.Redist
37+
### For GPU version (CUDA and cuDNN are required)
38+
PM> Install-Package SciSharp.TensorFlow.Redist-Windows-GPU
3339
```
3440

3541
Import TF.NET.

src/SciSharp.TensorFlow.Redist/README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
## SciSharp.TensorFlow.Redist ##
22

33

4-
`SciSharp.TensorFlow.Redist` is a migration from [Microsoft.ML.TensorFlow.Redist](https://github.com/dotnet/machinelearning/tree/release/1.2/src/Redist/Microsoft.ML.TensorFlow.Redist). [ML.NET](https://github.com/dotnet/machinelearning) team will not maintain the package since [ML.NET](https://www.nuget.org/packages/Microsoft.ML) v1.4.0 going forward.
4+
`SciSharp.TensorFlow.Redist` is a migration from [Microsoft.ML.TensorFlow.Redist](https://github.com/dotnet/machinelearning/tree/release/1.2/src/Redist/Microsoft.ML.TensorFlow.Redist). [ML.NET](https://github.com/dotnet/machinelearning) team will not maintain the package since [ML.NET](https://www.nuget.org/packages/Microsoft.ML) v1.3.0 going forward.
55

6+
* CPU version for all platforms (Windows, Linux, OSX)
7+
```powershell
8+
PM> Install-Package SciSharp.TensorFlow.Redist
9+
```
10+
11+
* GPU version for Windows
612
```powershell
713
PM> Install-Package SciSharp.TensorFlow.Redist
814
```
@@ -16,7 +22,7 @@ Related merged [commits](https://github.com/SciSharp/TensorFlow.NET/commit/854a5
1622
On Windows, the tar command does not support extracting archives with symlinks. So when `dotnet pack` runs on Windows it will only package the Windows binaries.
1723

1824
1. Run `dotnet pack` under `src/SciSharp.TensorFlow.Redist` directory in Linux.
19-
2. Run `nuget push SciSharp.TensorFlow.Redist.1.14.0.nupkg -k APIKEY -s https://api.nuget.org/v3/index.json`
25+
2. Run `dotnet nuget push SciSharp.TensorFlow.Redist.1.14.0.nupkg -k APIKEY -s https://api.nuget.org/v3/index.json`
2026

2127

2228

src/SciSharp.TensorFlow.Redist/Redist.nuspec renamed to src/SciSharp.TensorFlow.Redist/Redist-CPU.nuspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<license type="file">LICENSE.txt</license>
1010
<licenseUrl>https://aka.ms/deprecateLicenseUrl</licenseUrl>
1111
<projectUrl>https://www.tensorflow.org/</projectUrl>
12-
<description>$packageId$ contains the TensorFlow C library version $version$ redistributed as a NuGet package.</description>
12+
<description>$packageId$ contains the TensorFlow C library CPU version $version$ redistributed as a NuGet package.</description>
1313
<releaseNotes>https://github.com/tensorflow/tensorflow/releases/tag/v$version$</releaseNotes>
1414
<copyright>Copyright 2019 The TensorFlow Authors. All rights reserved.</copyright>
1515
<tags>TensorFlow</tags>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
3+
<metadata>
4+
<id>$packageId$</id>
5+
<version>$version$</version>
6+
<authors>The TensorFlow Authors</authors>
7+
<owners>The TensorFlow Authors</owners>
8+
<requireLicenseAcceptance>true</requireLicenseAcceptance>
9+
<license type="file">LICENSE.txt</license>
10+
<licenseUrl>https://aka.ms/deprecateLicenseUrl</licenseUrl>
11+
<projectUrl>https://www.tensorflow.org/</projectUrl>
12+
<description>$packageId$ contains the TensorFlow C library GPU version $version$ redistributed as a NuGet package.</description>
13+
<releaseNotes>https://github.com/tensorflow/tensorflow/releases/tag/v$version$</releaseNotes>
14+
<copyright>Copyright 2019 The TensorFlow Authors. All rights reserved.</copyright>
15+
<tags>TensorFlow</tags>
16+
<dependencies>
17+
<group targetFramework=".NETStandard2.0" />
18+
</dependencies>
19+
</metadata>
20+
<files>
21+
<file src="CommonPackage.props" target="build\netstandard2.0\$packageId$.props" />
22+
<file src="bin\packages\$packageId$\LICENSE.txt" target="LICENSE.txt" />
23+
<file src="bin\packages\$packageId$\THIRD_PARTY_NOTICES.txt" target="THIRD_PARTY_NOTICES.txt" />
24+
<file src="bin\packages\$packageId$\runtimes\**\*" target="runtimes" />
25+
</files>
26+
</package>

src/SciSharp.TensorFlow.Redist/SciSharp.TensorFlow.Redist.nupkgproj renamed to src/SciSharp.TensorFlow.Redist/SciSharp.TensorFlow.Redist-CPU.nupkgproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<NoBuild>true</NoBuild>
1818
<IncludeBuildOutput>false</IncludeBuildOutput>
1919

20-
<NuspecFile>Redist.nuspec</NuspecFile>
20+
<NuspecFile>Redist-CPU.nuspec</NuspecFile>
2121
<NuspecProperties>packageId=$(PackageId);version=$(PackageVersion)</NuspecProperties>
2222
<NuspecBasePath>$(ProjDir)</NuspecBasePath>
2323

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<ProjDir>$(MSBuildThisFileDirectory)</ProjDir>
5+
<BinDir>$(ProjDir)bin\</BinDir>
6+
<ObjDir>$(ProjDir)obj\</ObjDir>
7+
8+
<TargetArchitecture Condition="'$(TargetArchitecture)' == ''">x64</TargetArchitecture>
9+
<TargetFramework>netstandard2.0</TargetFramework>
10+
<TensorFlowVersion>1.14.0</TensorFlowVersion>
11+
<TensorFlowMajorVersion>1</TensorFlowMajorVersion>
12+
13+
<PackageAssetsPath>$(BinDir)packages\</PackageAssetsPath>
14+
<PackageId>$(MSBuildProjectName)</PackageId>
15+
<PackageVersion>$(TensorFlowVersion)</PackageVersion>
16+
17+
<NoBuild>true</NoBuild>
18+
<IncludeBuildOutput>false</IncludeBuildOutput>
19+
20+
<NuspecFile>Redist-Windows-GPU.nuspec</NuspecFile>
21+
<NuspecProperties>packageId=$(PackageId);version=$(PackageVersion)</NuspecProperties>
22+
<NuspecBasePath>$(ProjDir)</NuspecBasePath>
23+
24+
<GenerateNuspecDependsOn>CopyFilesFromArchive</GenerateNuspecDependsOn>
25+
26+
<PackageRid Condition="'$(OS)' == 'Windows_NT'">win</PackageRid>
27+
<PackageRid Condition="'$(OS)' != 'Windows_NT'">linux</PackageRid>
28+
<PackageRid Condition="$([MSBuild]::IsOSPlatform('osx'))">osx</PackageRid>
29+
<PackageRid>$(PackageRid)-$(TargetArchitecture)</PackageRid>
30+
31+
</PropertyGroup>
32+
33+
<PropertyGroup>
34+
<IncludeMLNetNotices>false</IncludeMLNetNotices>
35+
</PropertyGroup>
36+
37+
<ItemGroup>
38+
<TensorFlowConfig Include="windows"
39+
FileExtension=".zip"
40+
FilesFromArchive="lib\tensorflow.dll;
41+
include\tensorflow\c\LICENSE"
42+
Runtime="win-x64"/>
43+
44+
<TensorFlowConfig Condition="'$(OS)' != 'Windows_NT'"
45+
Include="linux"
46+
FileExtension=".tar.gz"
47+
FilesFromArchive="lib\libtensorflow.so;
48+
lib\libtensorflow_framework.so.$(TensorFlowMajorVersion);
49+
include\tensorflow\c\LICENSE"
50+
Runtime="linux-x64" />
51+
52+
<TensorFlowConfig Condition="'$(OS)' != 'Windows_NT'"
53+
Include="darwin" FileExtension=".tar.gz"
54+
FilesFromArchive="lib\libtensorflow.dylib;
55+
lib\libtensorflow_framework.$(TensorFlowMajorVersion).dylib;
56+
include\tensorflow\c\LICENSE"
57+
Runtime="osx-x64" />
58+
59+
<AdditionalDownloadFile Include="https://raw.githubusercontent.com/tensorflow/tensorflow/master/LICENSE"
60+
DownloadFile="$(BinDir)LICENSE" />
61+
</ItemGroup>
62+
63+
<Target Name="PrepareArchives">
64+
<ItemGroup>
65+
<!-- although we could extract all archives on all machines, mac requires a fixup which can only be run on mac
66+
so we split these per-rid and join during the official build packaging. -->
67+
<TensorFlowArchive
68+
Include="@(TensorFlowConfig->'https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-gpu-%(Identity)-x86_64-$(TensorFlowVersion)%(FileExtension)')" />
69+
<!-- set up metdata used by all targets -->
70+
<TensorFlowArchive DownloadFile="$(BinDir)%(FileName)%(Extension)"
71+
DownloadShaFile="$(BinDir)%(FileName)%(Extension).sha"
72+
ExtractDirectory="$(BinDir)%(FileName)"
73+
ExtractSemaphore="$(BinDir)%(FileName)\.extracted"
74+
LocalShaFile="$(MSBuildProjectDirectory)\%(FileName)%(Extension).sha"/>
75+
</ItemGroup>
76+
<Message Importance="high" Text="%(TensorFlowConfig.Runtime)"/>
77+
</Target>
78+
79+
<Target Name="DownloadArchives"
80+
DependsOnTargets="PrepareArchives"
81+
Inputs="$(MSBuildProjectFile)"
82+
Outputs="@(TensorFlowArchive->'%(DownloadFile)');@(AdditionalDownloadFile->'%(DownloadFile)')">
83+
<MakeDir Directories="$(BinDir)" />
84+
<ItemGroup>
85+
<_downloadFiles Include="@(TensorFlowArchive);@(AdditionalDownloadFile)" Url="%(Identity)" DestinationFile="%(DownloadFile)" />
86+
</ItemGroup>
87+
<Message Importance="High" Text="Downloading '%(_downloadFiles.Identity)' to '$(BinDir)'." />
88+
<DownloadFile SourceUrl="%(_downloadFiles.Identity)" DestinationFolder="$(BinDir)">
89+
<Output TaskParameter="DownloadedFile" ItemName="Content" />
90+
</DownloadFile>
91+
</Target>
92+
93+
94+
<Target Name="ValidateAndExtractArchives"
95+
DependsOnTargets="DownloadArchives"
96+
Inputs="@(TensorFlowArchive->'%(DownloadFile)')"
97+
Outputs="@(TensorFlowArchive->'%(ExtractSemaphore)')">
98+
99+
<GetFileHash Files="@(TensorFlowArchive->'%(DownloadFile)')" Algorithm="SHA512">
100+
<Output
101+
TaskParameter="Items"
102+
ItemName="FilesWithHashes" />
103+
</GetFileHash>
104+
105+
<WriteLinesToFile File="%(FilesWithHashes.Identity).sha" Lines="%(FilesWithHashes.FileHash)" Overwrite="true"/>
106+
107+
<!-- If specified we'll update the checked in SHAs with the downloaded ones. -->
108+
<Copy Condition="'$(UpdateSHA)' == 'true'"
109+
SourceFiles="@(TensorFlowArchive->'%(DownloadShaFile)')"
110+
DestinationFiles="@(TensorFlowArchive->'%(LocalShaFile)')" />
111+
112+
<ItemGroup>
113+
<TensorFlowArchive>
114+
<DownloadSha>@(FilesWithHashes->'%(FileHash)')</DownloadSha>
115+
<LocalSha>$([System.IO.File]::ReadAllText('%(LocalShaFile)').Replace("%0A", "").Replace("%0D", ""))</LocalSha>
116+
</TensorFlowArchive>
117+
</ItemGroup>
118+
119+
<Error Condition="!Exists('%(TensorFlowArchive.LocalShaFile)')" Text="SHA file '%(TensorFlowArchive.LocalShaFile)' does not exist. Build with /p:UpdateSHA=true to save it." />
120+
121+
<Message Importance="High" Text="@TensorFlowArchive->'%(TensorFlowArchive.DownloadFile) - %(TensorFlowArchive.LocalSha) - %(TensorFlowArchive.DownloadSha)"/>
122+
123+
<!-- Validate that the downloaded SHAs match the expected checked in SHAs -->
124+
<Error Condition="'%(TensorFlowArchive.LocalSha)' != '%(TensorFlowArchive.DownloadSha)'" Text="Downloaded file '%(TensorFlowArchive.DownloadFile)' has unexpected SHA.%0A expected: %(TensorFlowArchive.LocalSha)%0A --actual: %(TensorFlowArchive.DownloadSha)%0ABuild with /p:UpdateSHA=true if you intentionally changed the URL and wish to update the SHAs, otherwise this could indicate an incomplete download or intercerpted URL and should be examined." />
125+
126+
127+
<!-- The archives are valid, lets extract them, ensuring an empty directory -->
128+
<RemoveDir Directories="@(TensorFlowArchive->'%(ExtractDirectory)')" />
129+
<MakeDir Directories="@(TensorFlowArchive->'%(ExtractDirectory)')" />
130+
131+
<Message Importance="High" Text="Decompressing '%(TensorFlowArchive.DownloadFile)' to '%(TensorFlowArchive.ExtractDirectory)'." />
132+
133+
<Unzip Condition="'%(TensorFlowArchive.FileExtension)' == '.zip'"
134+
SourceFiles="%(TensorFlowArchive.DownloadFile)"
135+
DestinationFolder="%(TensorFlowArchive.ExtractDirectory)" />
136+
137+
<Exec Condition="'$(OS)' != 'Windows_NT' AND '%(TensorFlowArchive.FileExtension)' == '.tar.gz'"
138+
WorkingDirectory="$(MSBuildThisFileDirectory)"
139+
Command="tar -xzm --hard-dereference -f %(TensorFlowArchive.DownloadFile) -C %(TensorFlowArchive.ExtractDirectory)" />
140+
141+
<Exec Condition="'$(OS)' != 'Windows_NT'"
142+
Command="chmod -R +w %(TensorFlowArchive.ExtractDirectory)" />
143+
144+
<Touch Files="@(TensorFlowArchive->'%(ExtractSemaphore)')" AlwaysCreate="true" />
145+
</Target>
146+
147+
<!-- Select the files we want to copy out of each archive. -->
148+
<Target Name="GetFilesFromArchive"
149+
DependsOnTargets="ValidateAndExtractArchives" >
150+
<ItemGroup>
151+
<!-- batch rather than transform so that we can split FilesFromArchive metadata -->
152+
<_fileFromArchive Include="%(TensorFlowArchive.FilesFromArchive)" ExtractDirectory="%(TensorFlowArchive.ExtractDirectory)" Runtime="%(TensorFlowArchive.Runtime)" />
153+
<_fileFromArchive DestinationFile="%(FileName)%(Extension)"/>
154+
<_fileFromArchive PackagePath="runtimes\%(_fileFromArchive.Runtime)\native\%(_fileFromArchive.DestinationFile)" />
155+
156+
<!-- LICENSE from the package is actually THIRD_PARTY_NOTICES-->
157+
<_fileFromArchive Condition="'%(DestinationFile)' == 'LICENSE'" PackagePath="THIRD_PARTY_NOTICES.txt" Runtime="" />
158+
159+
<!-- copy to packaging location -->
160+
<FilesFromArchive Include="@(_fileFromArchive->'%(ExtractDirectory)\%(Identity)')"
161+
TargetPath="$(PackageAssetsPath)$(MSBuildProjectName)\%(PackagePath)" />
162+
<!-- include LICENSE that was downloaded from GitHub -->
163+
<FilesFromArchive Include="$(BinDir)\LICENSE"
164+
TargetPath="$(PackageAssetsPath)$(MSBuildProjectName)\LICENSE.txt" />
165+
166+
<!-- copy to NativeAssets location, only for current RID, so that they may be used by tests -->
167+
<!--<FilesFromArchive Condition="'$(PackageRID)' == '%(_fileFromArchive.Runtime)'"
168+
Include="@(_fileFromArchive->'%(ExtractDirectory)\%(Identity)')"
169+
TargetPath="$(NativeAssetsBuiltPath)\%(_fileFromArchive.DestinationFile)" />-->
170+
</ItemGroup>
171+
</Target>
172+
173+
<Target Name="CopyFilesFromArchive"
174+
DependsOnTargets="GetFilesFromArchive">
175+
176+
<Message Importance="High" Text="@(FilesFromArchive) -> %(FilesFromArchive.TargetPath)" />
177+
<Copy SourceFiles="@(FilesFromArchive)"
178+
DestinationFiles="@(FilesFromArchive->'%(TargetPath)')" />
179+
180+
</Target>
181+
182+
<Target Name="Clean">
183+
<Message Importance="High" Text="Deleting $(BinDir);$(ObjDir)" />
184+
<RemoveDir Directories="$(BinDir);$(ObjDir)" />
185+
</Target>
186+
187+
</Project>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
850A27858FA951DF77A78CD1BD78B54F6EE2532DD5A49F0579A7B02C795C62F0212F20177EAEA2BD77BD451A57FBBD1348362492F9E14BFE5CA5028C71711293

src/TensorFlowHub/MnistDataSet.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,54 @@ public MnistDataSet(NDArray images, NDArray labels, Type dataType, bool reshape)
2727
labels.astype(dataType);
2828
Labels = labels;
2929
}
30+
31+
public (NDArray, NDArray) GetNextBatch(int batch_size, bool fake_data = false, bool shuffle = true)
32+
{
33+
var start = IndexInEpoch;
34+
// Shuffle for the first epoch
35+
if(EpochsCompleted == 0 && start == 0 && shuffle)
36+
{
37+
var perm0 = np.arange(NumOfExamples);
38+
np.random.shuffle(perm0);
39+
Data = Data[perm0];
40+
Labels = Labels[perm0];
41+
}
42+
43+
// Go to the next epoch
44+
if (start + batch_size > NumOfExamples)
45+
{
46+
// Finished epoch
47+
EpochsCompleted += 1;
48+
49+
// Get the rest examples in this epoch
50+
var rest_num_examples = NumOfExamples - start;
51+
//var images_rest_part = _images[np.arange(start, _num_examples)];
52+
//var labels_rest_part = _labels[np.arange(start, _num_examples)];
53+
// Shuffle the data
54+
if (shuffle)
55+
{
56+
var perm = np.arange(NumOfExamples);
57+
np.random.shuffle(perm);
58+
Data = Data[perm];
59+
Labels = Labels[perm];
60+
}
61+
62+
start = 0;
63+
IndexInEpoch = batch_size - rest_num_examples;
64+
var end = IndexInEpoch;
65+
var images_new_part = Data[np.arange(start, end)];
66+
var labels_new_part = Labels[np.arange(start, end)];
67+
68+
/*return (np.concatenate(new float[][] { images_rest_part.Data<float>(), images_new_part.Data<float>() }, axis: 0),
69+
np.concatenate(new float[][] { labels_rest_part.Data<float>(), labels_new_part.Data<float>() }, axis: 0));*/
70+
return (images_new_part, labels_new_part);
71+
}
72+
else
73+
{
74+
IndexInEpoch += batch_size;
75+
var end = IndexInEpoch;
76+
return (Data[np.arange(start, end)], Labels[np.arange(start, end)]);
77+
}
78+
}
3079
}
3180
}

src/TensorFlowHub/MnistModelLoader.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,27 @@ public class MnistModelLoader : IModelLoader<MnistDataSet>
1515
private const string TEST_IMAGES = "t10k-images-idx3-ubyte.gz";
1616
private const string TEST_LABELS = "t10k-labels-idx1-ubyte.gz";
1717

18-
public static async Task<Datasets<MnistDataSet>> LoadAsync(string trainDir, bool oneHot = false)
18+
public static async Task<Datasets<MnistDataSet>> LoadAsync(string trainDir, bool oneHot = false, int? trainSize = null, int? validationSize = null, int? testSize = null)
1919
{
2020
var loader = new MnistModelLoader();
21-
return await loader.LoadAsync(new ModelLoadSetting
21+
22+
var setting = new ModelLoadSetting
2223
{
2324
TrainDir = trainDir,
24-
OneHot = oneHot
25-
});
25+
OneHot = oneHot,
26+
TrainSize = trainSize
27+
};
28+
29+
if (trainSize.HasValue)
30+
setting.TrainSize = trainSize.Value;
31+
32+
if (validationSize.HasValue)
33+
setting.ValidationSize = validationSize.Value;
34+
35+
if (testSize.HasValue)
36+
setting.TestSize = testSize.Value;
37+
38+
return await loader.LoadAsync(setting);
2639
}
2740

2841
public async Task<Datasets<MnistDataSet>> LoadAsync(ModelLoadSetting setting)

0 commit comments

Comments
 (0)