Skip to content

Commit

Permalink
fix csharp examples and references
Browse files Browse the repository at this point in the history
  • Loading branch information
TJ committed Oct 1, 2018
1 parent fcdeef6 commit 7c838d3
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
<ProjectReference Include="..\..\..\bindings\csharp\CNTKLibraryManagedDll\CNTKLibraryManagedDll.csproj" />
<Reference Include="netstandard" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\..\Evaluation\ImageExtension\CNTKImageProcessing.cs">
Expand Down Expand Up @@ -71,20 +71,24 @@
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="Cntk.Core.Managed-2.6, processorArchitecture=AMD64">
<HintPath>..\packages\CNTK.CPUOnly.2.6.0\lib\netstandard2.0\Cntk.Core.Managed-2.6.dll</HintPath>
<Private>True</Private>
</Reference>
<None Include="App.config" />
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\packages\CNTK.Deps.MKL.2.6.0\build\net45\CNTK.Deps.MKL.targets" Condition="Exists('..\packages\CNTK.Deps.MKL.2.6.0\build\net45\CNTK.Deps.MKL.targets')" />
<Import Project="..\packages\CNTK.Deps.OpenCV.Zip.2.6.0\build\net45\CNTK.Deps.OpenCV.Zip.targets" Condition="Exists('..\packages\CNTK.Deps.OpenCV.Zip.2.6.0\build\net45\CNTK.Deps.OpenCV.Zip.targets')" />
<Import Project="..\packages\CNTK.CPUOnly.2.6.0\build\net45\CNTK.CPUOnly.targets" Condition="Exists('..\packages\CNTK.CPUOnly.2.6.0\build\net45\CNTK.CPUOnly.targets')" />
<Import Project="..\packages\CNTK.Deps.MKL.2.6.0\build\netstandard2.0\CNTK.Deps.MKL.targets" Condition="Exists('..\packages\CNTK.Deps.MKL.2.6.0\build\netstandard2.0\CNTK.Deps.MKL.targets')" />
<Import Project="..\packages\CNTK.Deps.OpenCV.Zip.2.6.0\build\netstandard2.0\CNTK.Deps.OpenCV.Zip.targets" Condition="Exists('..\packages\CNTK.Deps.OpenCV.Zip.2.6.0\build\netstandard2.0\CNTK.Deps.OpenCV.Zip.targets')" />
<Import Project="..\packages\CNTK.CPUOnly.2.6.0\build\netstandard2.0\CNTK.CPUOnly.targets" Condition="Exists('..\packages\CNTK.CPUOnly.2.6.0\build\netstandard2.0\CNTK.CPUOnly.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\CNTK.Deps.MKL.2.6.0\build\net45\CNTK.Deps.MKL.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\CNTK.Deps.MKL.2.6.0\build\net45\CNTK.Deps.MKL.targets'))" />
<Error Condition="!Exists('..\packages\CNTK.Deps.OpenCV.Zip.2.6.0\build\net45\CNTK.Deps.OpenCV.Zip.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\CNTK.Deps.OpenCV.Zip.2.6.0\build\net45\CNTK.Deps.OpenCV.Zip.targets'))" />
<Error Condition="!Exists('..\packages\CNTK.CPUOnly.2.6.0\build\net45\CNTK.CPUOnly.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\CNTK.CPUOnly.2.6.0\build\net45\CNTK.CPUOnly.targets'))" />
<Error Condition="!Exists('..\packages\CNTK.Deps.MKL.2.6.0\build\netstandard2.0\CNTK.Deps.MKL.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\CNTK.Deps.MKL.2.6.0\build\netstandard2.0\CNTK.Deps.MKL.targets'))" />
<Error Condition="!Exists('..\packages\CNTK.Deps.OpenCV.Zip.2.6.0\build\netstandard2.0\CNTK.Deps.OpenCV.Zip.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\CNTK.Deps.OpenCV.Zip.2.6.0\build\netstandard2.0\CNTK.Deps.OpenCV.Zip.targets'))" />
<Error Condition="!Exists('..\packages\CNTK.CPUOnly.2.6.0\build\netstandard2.0\CNTK.CPUOnly.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\CNTK.CPUOnly.2.6.0\build\netstandard2.0\CNTK.CPUOnly.targets'))" />
</Target>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Program
{
static void Main(string[] args)
{
TestCommon.TestDataDirPrefix = "../../";
var device = DeviceDescriptor.CPUDevice;
Console.WriteLine($"======== running LogisticRegression.TrainAndEvaluate using {device.Type} ========");
LogisticRegression.TrainAndEvaluate(device);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="CNTK.CPUOnly" version="2.6.0" targetFramework="net45" />
<package id="CNTK.Deps.MKL" version="2.6.0" targetFramework="net45" />
<package id="CNTK.Deps.OpenCV.Zip" version="2.6.0" targetFramework="net45" />
<package id="CNTK.CPUOnly" version="2.6.0" targetFramework="netstandard2.0" />
<package id="CNTK.Deps.MKL" version="2.6.0" targetFramework="netstandard2.0" />
<package id="CNTK.Deps.OpenCV.Zip" version="2.6.0" targetFramework="netstandard2.0" />
</packages>
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
<ProjectReference Include="..\..\..\bindings\csharp\CNTKLibraryManagedDll\CNTKLibraryManagedDll.csproj" />
<Reference Include="netstandard" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\..\Evaluation\ImageExtension\CNTKImageProcessing.cs">
Expand Down Expand Up @@ -72,22 +72,26 @@
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="Cntk.Core.Managed-2.6, processorArchitecture=AMD64">
<HintPath>..\packages\CNTK.GPU.2.6.0\lib\netstandard2.0\Cntk.Core.Managed-2.6.dll</HintPath>
<Private>True</Private>
</Reference>
<None Include="App.config" />
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\packages\CNTK.Deps.Cuda.2.6.0\build\net45\CNTK.Deps.Cuda.targets" Condition="Exists('..\packages\CNTK.Deps.Cuda.2.6.0\build\net45\CNTK.Deps.Cuda.targets')" />
<Import Project="..\packages\CNTK.Deps.MKL.2.6.0\build\net45\CNTK.Deps.MKL.targets" Condition="Exists('..\packages\CNTK.Deps.MKL.2.6.0\build\net45\CNTK.Deps.MKL.targets')" />
<Import Project="..\packages\CNTK.Deps.OpenCV.Zip.2.6.0\build\net45\CNTK.Deps.OpenCV.Zip.targets" Condition="Exists('..\packages\CNTK.Deps.OpenCV.Zip.2.6.0\build\net45\CNTK.Deps.OpenCV.Zip.targets')" />
<Import Project="..\packages\CNTK.GPU.2.6.0\build\net45\CNTK.GPU.targets" Condition="Exists('..\packages\CNTK.GPU.2.6.0\build\net45\CNTK.GPU.targets')" />
<Import Project="..\packages\CNTK.Deps.Cuda.2.6.0\build\netstandard2.0\CNTK.Deps.Cuda.targets" Condition="Exists('..\packages\CNTK.Deps.Cuda.2.6.0\build\netstandard2.0\CNTK.Deps.Cuda.targets')" />
<Import Project="..\packages\CNTK.Deps.MKL.2.6.0\build\netstandard2.0\CNTK.Deps.MKL.targets" Condition="Exists('..\packages\CNTK.Deps.MKL.2.6.0\build\netstandard2.0\CNTK.Deps.MKL.targets')" />
<Import Project="..\packages\CNTK.Deps.OpenCV.Zip.2.6.0\build\netstandard2.0\CNTK.Deps.OpenCV.Zip.targets" Condition="Exists('..\packages\CNTK.Deps.OpenCV.Zip.2.6.0\build\netstandard2.0\CNTK.Deps.OpenCV.Zip.targets')" />
<Import Project="..\packages\CNTK.GPU.2.6.0\build\netstandard2.0\CNTK.GPU.targets" Condition="Exists('..\packages\CNTK.GPU.2.6.0\build\netstandard2.0\CNTK.GPU.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\CNTK.Deps.Cuda.2.6.0\build\net45\CNTK.Deps.Cuda.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\CNTK.Deps.Cuda.2.6.0\build\net45\CNTK.Deps.Cuda.targets'))" />
<Error Condition="!Exists('..\packages\CNTK.Deps.MKL.2.6.0\build\net45\CNTK.Deps.MKL.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\CNTK.Deps.MKL.2.6.0\build\net45\CNTK.Deps.MKL.targets'))" />
<Error Condition="!Exists('..\packages\CNTK.Deps.OpenCV.Zip.2.6.0\build\net45\CNTK.Deps.OpenCV.Zip.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\CNTK.Deps.OpenCV.Zip.2.6.0\build\net45\CNTK.Deps.OpenCV.Zip.targets'))" />
<Error Condition="!Exists('..\packages\CNTK.GPU.2.6.0\build\net45\CNTK.GPU.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\CNTK.GPU.2.6.0\build\net45\CNTK.GPU.targets'))" />
<Error Condition="!Exists('..\packages\CNTK.Deps.Cuda.2.6.0\build\netstandard2.0\CNTK.Deps.Cuda.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\CNTK.Deps.Cuda.2.6.0\build\netstandard2.0\CNTK.Deps.Cuda.targets'))" />
<Error Condition="!Exists('..\packages\CNTK.Deps.MKL.2.6.0\build\netstandard2.0\CNTK.Deps.MKL.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\CNTK.Deps.MKL.2.6.0\build\netstandard2.0\CNTK.Deps.MKL.targets'))" />
<Error Condition="!Exists('..\packages\CNTK.Deps.OpenCV.Zip.2.6.0\build\netstandard2.0\CNTK.Deps.OpenCV.Zip.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\CNTK.Deps.OpenCV.Zip.2.6.0\build\netstandard2.0\CNTK.Deps.OpenCV.Zip.targets'))" />
<Error Condition="!Exists('..\packages\CNTK.GPU.2.6.0\build\netstandard2.0\CNTK.GPU.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\CNTK.GPU.2.6.0\build\netstandard2.0\CNTK.GPU.targets'))" />
</Target>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
17 changes: 14 additions & 3 deletions Examples/TrainingCSharp/CSTrainingGPUExamples/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
Expand All @@ -10,6 +11,7 @@ class Program
{
static void Main(string[] args)
{
TestCommon.TestDataDirPrefix = "../../";
var device = DeviceDescriptor.GPUDevice(0);
Console.WriteLine($"======== running LogisticRegression.TrainAndEvaluate using {device.Type} ========");
LogisticRegression.TrainAndEvaluate(device);
Expand All @@ -21,14 +23,23 @@ static void Main(string[] args)
MNISTClassifier.TrainAndEvaluate(device, true, true);

Console.WriteLine($"======== running CifarResNet.TrainAndEvaluate using {device.Type} ========");
CifarResNetClassifier.CifarDataFolder = "../../Examples/Image/DataSets/CIFAR-10";
CifarResNetClassifier.TrainAndEvaluate(device, true);
TestCommon.TestDataDirPrefix = "../../Examples/Image/DataSets/";
string modelFileSourceDir = "../../PretrainedModels/ResNet_18.model";
if (!File.Exists(modelFileSourceDir))
{
Console.WriteLine("Model file doesn't exist. Please run download_model.py in CNTK/CNTK/PretrainedModels");
Console.ReadKey();
return;
}

Console.WriteLine($"======== running TransferLearning.TrainAndEvaluateWithFlowerData using {device.Type} ========");
TransferLearning.TrainAndEvaluateWithFlowerData(device, true);

TransferLearning.BaseResnetModelFile = "ResNet_18.model";
File.Copy(modelFileSourceDir, TransferLearning.ExampleImageFolder + TransferLearning.BaseResnetModelFile,/*overwrite*/true);
Console.WriteLine($"======== running TransferLearning.TrainAndEvaluateWithAnimalData using {device.Type} ========");
TransferLearning.TrainAndEvaluateWithAnimalData(device, true);

TestCommon.TestDataDirPrefix = "../../";
Console.WriteLine($"======== running LSTMSequenceClassifier.Train using {device.Type} ========");
LSTMSequenceClassifier.Train(device);
}
Expand Down
8 changes: 4 additions & 4 deletions Examples/TrainingCSharp/CSTrainingGPUExamples/packages.config
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="CNTK.Deps.Cuda" version="2.6.0" targetFramework="net45" />
<package id="CNTK.Deps.MKL" version="2.6.0" targetFramework="net45" />
<package id="CNTK.Deps.OpenCV.Zip" version="2.6.0" targetFramework="net45" />
<package id="CNTK.GPU" version="2.6.0" targetFramework="net45" />
<package id="CNTK.Deps.Cuda" version="2.6.0" targetFramework="netstandard2.0" />
<package id="CNTK.Deps.MKL" version="2.6.0" targetFramework="netstandard2.0" />
<package id="CNTK.Deps.OpenCV.Zip" version="2.6.0" targetFramework="netstandard2.0" />
<package id="CNTK.GPU" version="2.6.0" targetFramework="netstandard2.0" />
</packages>
1 change: 0 additions & 1 deletion Examples/TrainingCSharp/Common/LSTMSequenceClassifier.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using CNTK.CNTKLibraryCSTrainingTest;

namespace CNTK.CSTrainingExamples
{
Expand Down
1 change: 0 additions & 1 deletion Examples/TrainingCSharp/Common/MNISTClassifier.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using CNTK.CNTKLibraryCSTrainingTest;

namespace CNTK.CSTrainingExamples
{
Expand Down
4 changes: 4 additions & 0 deletions Examples/TrainingCSharp/Common/TestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ public enum Activation
Sigmoid,
Tanh
}
public static class TestCommon
{
public static string TestDataDirPrefix;
}
public class TestHelper
{
public static Function Dense(Variable input, int outputDim, DeviceDescriptor device,
Expand Down
18 changes: 11 additions & 7 deletions Examples/TrainingCSharp/Common/TransferLearning.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Drawing;
using System.IO;
using System.Linq;
using CNTK.CNTKLibraryCSTrainingTest;
using System.Runtime.CompilerServices;

namespace CNTK.CSTrainingExamples
{
Expand All @@ -22,13 +22,17 @@ public class TransferLearning
/// data folder is: CNTK/Examples/Image
/// model folder is: CNTK/PretrainedModels
/// </summary>
public static string ExampleImageFoler = TestCommon.TestDataDirPrefix;
public static string ExampleImageFolder
{
get { return TestCommon.TestDataDirPrefix; }
set { TestCommon.TestDataDirPrefix = value; }
}

public static string BaseResnetModelFile = TestCommon.TestDataDirPrefix + "/ResNet18_ImageNet_CNTK.model";

private static string featureNodeName = "features";
private static string lastHiddenNodeName = "z.x";
private static int[] imageDims = new int[] { 224, 224, 3 };

/// <summary>
/// TrainAndEvaluateWithFlowerData shows how to do transfer learning with a MinibatchSource. MinibatchSource is constructed with
/// a map file that contains image file paths and labels. Data loading, image preprocessing, and batch randomization are handled
Expand All @@ -39,7 +43,7 @@ public class TransferLearning
/// it only evaluates the model is it exists. </param>
public static void TrainAndEvaluateWithFlowerData(DeviceDescriptor device, bool forceReTrain = false)
{
string flowerFolder = Path.Combine(ExampleImageFoler, "Flowers");
string flowerFolder = Path.Combine(ExampleImageFolder, "Flowers");
string flowersTrainingMap = Path.Combine(flowerFolder, "1k_img_map.txt");
string flowersValidationMap = Path.Combine(flowerFolder, "val_map.txt");
int flowerModelNumClasses = 102;
Expand All @@ -65,7 +69,7 @@ public static void TrainAndEvaluateWithFlowerData(DeviceDescriptor device, bool
Function trainingLoss, predictionError;

// create a transfer model
Function transferLearningModel = CreateTransferLearningModel(BaseResnetModelFile, featureNodeName,
Function transferLearningModel = CreateTransferLearningModel(Path.Combine(ExampleImageFolder, BaseResnetModelFile), featureNodeName,
predictionNodeName, lastHiddenNodeName, flowerModelNumClasses, device,
out imageInput, out labelInput, out trainingLoss, out predictionError);

Expand Down Expand Up @@ -120,7 +124,7 @@ public static void TrainAndEvaluateWithFlowerData(DeviceDescriptor device, bool
/// it only evaluates the model is it exists. </param>
public static void TrainAndEvaluateWithAnimalData(DeviceDescriptor device, bool forceRetrain = false)
{
string animalDataFolder = Path.Combine(ExampleImageFoler, "Animals");
string animalDataFolder = Path.Combine(ExampleImageFolder, "Animals");
string[] animals = new string[] { "Sheep", "Wolf" };
int animalModelNumClasses = 2;
string animalsModelFile = Path.Combine(CurrentFolder, "AnimalsTransferLearning.model");
Expand All @@ -140,7 +144,7 @@ public static void TrainAndEvaluateWithAnimalData(DeviceDescriptor device, bool
string predictionNodeName = "prediction";
Variable imageInput, labelInput;
Function trainingLoss, predictionError;
Function transferLearningModel = CreateTransferLearningModel(Path.Combine(ExampleImageFoler, BaseResnetModelFile), featureNodeName, predictionNodeName,
Function transferLearningModel = CreateTransferLearningModel(Path.Combine(ExampleImageFolder, BaseResnetModelFile), featureNodeName, predictionNodeName,
lastHiddenNodeName, animalModelNumClasses, device,
out imageInput, out labelInput, out trainingLoss, out predictionError);

Expand Down
1 change: 1 addition & 0 deletions PretrainedModels/download_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
('Image Classification', 'InceptionV3_ImageNet_CNTK', 'https://www.cntk.ai/Models/CNTK_Pretrained/InceptionV3_ImageNet_CNTK.model'),
('Image Classification', 'BNInception_ImageNet_Caffe', 'https://www.cntk.ai/Models/Caffe_Converted/BNInception_ImageNet_Caffe.model'),
('Image Classification', 'ResNet18_ImageNet_CNTK', 'https://www.cntk.ai/Models/CNTK_Pretrained/ResNet18_ImageNet_CNTK.model'),
('Image Classification', 'ResNet_18', 'https://cntkbuildstorage.blob.core.windows.net/cntk-pretrained-model/ResNet_18.model'),
('Image Classification', 'ResNet34_ImageNet_CNTK', 'https://www.cntk.ai/Models/CNTK_Pretrained/ResNet34_ImageNet_CNTK.model'),
('Image Classification', 'ResNet50_ImageNet_CNTK', 'https://www.cntk.ai/Models/CNTK_Pretrained/ResNet50_ImageNet_CNTK.model'),
('Image Classification', 'ResNet101_ImageNet_CNTK', 'https://www.cntk.ai/Models/CNTK_Pretrained/ResNet101_ImageNet_CNTK.model'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,4 @@ static void RunAllExamples(DeviceDescriptor device)
LSTMSequenceClassifier.Train(device);
}
}

public static class TestCommon
{
public static string TestDataDirPrefix;
}
}

0 comments on commit 7c838d3

Please sign in to comment.