Skip to content

Commit 2cd8600

Browse files
authored
Merge pull request #56 from cbovar/Perf
Fix several errors in ConvNetSharp.Flow
2 parents e33e4c8 + 0287b02 commit 2cd8600

34 files changed

Lines changed: 730 additions & 185 deletions

Examples/FlowDemo/Classify2DDemo.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ public static void Classify2DDemo()
6767
var vm = new ViewModel<double>(net.Cost);
6868
var app = new Application();
6969
app.Run(new GraphControl { DataContext = vm });
70+
71+
net.Dispose();
72+
trainer.Dispose();
7073
}
7174

7275
private static void Classify2DUpdate(int n, List<double[]> data, TrainerBase<double> trainer, List<int> labels)

Examples/MnistDemo.Flow.GPU/Program.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.IO;
23
using System.Linq;
34
using System.Windows;
45
using ConvNetSharp.Flow;
@@ -27,7 +28,7 @@ private static void Main()
2728

2829
private void MnistDemo()
2930
{
30-
BuilderInstance.Volume = new VolumeBuilder();
31+
BuilderInstance.Volume = new VolumeBuilder();
3132

3233
var datasets = new DataSets();
3334
if (!datasets.Load(100))
@@ -38,10 +39,10 @@ private void MnistDemo()
3839
// Create network
3940
this._net = new Net<float>();
4041
this._net.AddLayer(new InputLayer<float>());
41-
this._net.AddLayer(new ConvLayer<float>(5, 5, 8) { Stride = 1, Pad = 2 });
42+
this._net.AddLayer(new ConvLayer<float>(5, 5, 8) { Stride = 1, Pad = 2, BiasPref = 0.1f });
4243
this._net.AddLayer(new ReluLayer<float>());
4344
this._net.AddLayer(new PoolLayer<float>(2, 2) { Stride = 2 });
44-
this._net.AddLayer(new ConvLayer<float>(5, 5, 16) { Stride = 1, Pad = 2 });
45+
this._net.AddLayer(new ConvLayer<float>(5, 5, 16) { Stride = 1, Pad = 2, BiasPref = 0.1f });
4546
this._net.AddLayer(new ReluLayer<float>());
4647
this._net.AddLayer(new PoolLayer<float>(3, 3) { Stride = 3 });
4748
this._net.AddLayer(new FullyConnLayer<float>(10));
@@ -66,6 +67,11 @@ private void MnistDemo()
6667
//Momentum = 0.9f
6768
};
6869

70+
if (File.Exists("loss.csv"))
71+
{
72+
File.Delete("loss.csv");
73+
}
74+
6975
Console.WriteLine("Convolutional neural network learning...[Press any key to stop]");
7076
do
7177
{
@@ -84,13 +90,18 @@ private void MnistDemo()
8490
Math.Round(this._trainer.BackwardTimeMs, 2),
8591
Math.Round(this._trainer.UpdateWeightsTimeMs, 2));
8692

93+
File.AppendAllLines("loss.csv", new[] { $"{this._stepCount}, {this._trainer.Loss}, { Math.Round(this._trainAccWindow.Items.Average() * 100.0, 2)}, {Math.Round(this._testAccWindow.Items.Average() * 100.0, 2)}" });
94+
8795
} while (!Console.KeyAvailable);
8896

8997

9098
// Display graph
9199
var vm = new ViewModel<float>(_net.Op);
92100
var app = new Application();
93101
app.Run(new GraphControl { DataContext = vm });
102+
103+
this._net.Dispose();
104+
this._trainer.Dispose();
94105
}
95106

96107
private void Test(Volume x, int[] labels, CircularBuffer<double> accuracy, bool forward = true)

Examples/MnistDemo.GPU/Program.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.IO;
23
using System.Linq;
34
using ConvNetSharp.Core;
45
using ConvNetSharp.Core.Layers.Single;
@@ -14,6 +15,7 @@ internal class Program
1415
private Net<float> _net;
1516
private int _stepCount;
1617
private SgdTrainer _trainer;
18+
private ConvLayer convLayer;
1719

1820
private static void Main()
1921
{
@@ -34,7 +36,8 @@ private void MnistDemo()
3436
// Create network
3537
this._net = new Net<float>();
3638
this._net.AddLayer(new InputLayer(28, 28, 1));
37-
this._net.AddLayer(new ConvLayer(5, 5, 8) { Stride = 1, Pad = 2 });
39+
this.convLayer = new ConvLayer(5, 5, 8) { Stride = 1, Pad = 2 };
40+
this._net.AddLayer(convLayer);
3841
this._net.AddLayer(new ReluLayer());
3942
this._net.AddLayer(new PoolLayer(2, 2) { Stride = 2 });
4043
this._net.AddLayer(new ConvLayer(5, 5, 16) { Stride = 1, Pad = 2 });
@@ -63,6 +66,11 @@ private void MnistDemo()
6366
//Momentum = 0.9f
6467
};
6568

69+
if (File.Exists("loss.csv"))
70+
{
71+
File.Delete("loss.csv");
72+
}
73+
6674
Console.WriteLine("Convolutional neural network learning...[Press any key to stop]");
6775
do
6876
{
@@ -81,6 +89,8 @@ private void MnistDemo()
8189
Math.Round(this._trainer.BackwardTimeMs, 2),
8290
Math.Round(this._trainer.UpdateWeightsTimeMs, 2));
8391

92+
File.AppendAllLines("loss.csv", new[] { $"{this._stepCount}, {this._trainer.Loss}, { Math.Round(this._trainAccWindow.Items.Average() * 100.0, 2)}, {Math.Round(this._testAccWindow.Items.Average() * 100.0, 2)}" });
93+
8494
} while (!Console.KeyAvailable);
8595
}
8696

src/ConvNetSharp.Core/Layers/ConvLayer.cs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public ConvLayer(Dictionary<string, object> data) : base(data)
2323
this.Pad = Convert.ToInt32(data["Pad"]);
2424
this.Filters = BuilderInstance<T>.Volume.SameAs(data["Filters"].ToArrayOfT<T>(), new Shape(this.Width, this.Height, this.InputDepth, this.FilterCount));
2525
this.Bias = BuilderInstance<T>.Volume.SameAs(data["Bias"].ToArrayOfT<T>(), new Shape(1, 1, this.FilterCount));
26-
this.BiasPref = (T)Convert.ChangeType(data["BiasPref"], typeof(T));
26+
this.BiasPref = (T) Convert.ChangeType(data["BiasPref"], typeof(T));
2727
this.FiltersGradient = BuilderInstance<T>.Volume.SameAs(data["FiltersGradient"].ToArrayOfT<T>(), new Shape(this.Width, this.Height, this.InputDepth, this.FilterCount));
2828
this.BiasGradient = BuilderInstance<T>.Volume.SameAs(data["BiasGradient"].ToArrayOfT<T>(), new Shape(1, 1, this.FilterCount));
2929

@@ -172,19 +172,21 @@ internal void UpdateOutputSize()
172172
// volume exactly, the output volume will be trimmed and not contain the (incomplete) computed
173173
// final application.
174174
this.OutputWidth =
175-
(int)Math.Floor((this.InputWidth + this.Pad * 2 - this.Width) / (double)this.Stride + 1);
175+
(int) Math.Floor((this.InputWidth + this.Pad * 2 - this.Width) / (double) this.Stride + 1);
176176
this.OutputHeight =
177-
(int)Math.Floor((this.InputHeight + this.Pad * 2 - this.Height) / (double)this.Stride + 1);
177+
(int) Math.Floor((this.InputHeight + this.Pad * 2 - this.Height) / (double) this.Stride + 1);
178178

179179
// initializations
180180
var scale = Math.Sqrt(2.0 / (this.Width * this.Height * this.InputDepth));
181181

182-
this.Filters =
183-
BuilderInstance<T>.Volume.Random(
184-
new Shape(this.Width, this.Height, this.InputDepth, this.OutputDepth), 0.0, scale);
185-
this.FiltersGradient =
186-
BuilderInstance<T>.Volume.SameAs(
187-
new Shape(this.Width, this.Height, this.InputDepth, this.OutputDepth));
182+
var shape = new Shape(this.Width, this.Height, this.InputDepth, this.OutputDepth);
183+
if (this.Filters == null || !this.Filters.Shape.Equals(shape))
184+
{
185+
this.Filters?.Dispose();
186+
this.Filters = BuilderInstance<T>.Volume.Random(shape, 0.0, scale);
187+
this.FiltersGradient?.Dispose();
188+
this.FiltersGradient = BuilderInstance<T>.Volume.SameAs(shape);
189+
}
188190

189191
this.Bias = BuilderInstance<T>.Volume.SameAs(new T[this.OutputDepth].Populate(this.BiasPref),
190192
new Shape(1, 1, this.OutputDepth));

src/ConvNetSharp.Flow.Tests/ConvNetSharp.Flow.Tests.2015.csproj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<DefineConstants>DEBUG;TRACE</DefineConstants>
2222
<ErrorReport>prompt</ErrorReport>
2323
<WarningLevel>4</WarningLevel>
24+
<PlatformTarget>x64</PlatformTarget>
2425
</PropertyGroup>
2526
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
2627
<DebugType>pdbonly</DebugType>
@@ -69,10 +70,18 @@
6970
<None Include="packages.config" />
7071
</ItemGroup>
7172
<ItemGroup>
73+
<ProjectReference Include="..\ConvNetSharp.Core\ConvNetSharp.Core.2015.csproj">
74+
<Project>{3A6864AF-30B4-4C0E-B621-3571E3D88B43}</Project>
75+
<Name>ConvNetSharp.Core.2015</Name>
76+
</ProjectReference>
7277
<ProjectReference Include="..\ConvNetSharp.Flow\ConvNetSharp.Flow.2015.csproj">
7378
<Project>{9854f171-8272-49e4-a0bb-c9bfa70c30d3}</Project>
7479
<Name>ConvNetSharp.Flow.2015</Name>
7580
</ProjectReference>
81+
<ProjectReference Include="..\ConvNetSharp.Volume.GPU\ConvNetSharp.Volume.GPU.2015.csproj">
82+
<Project>{bdcc5296-bfd4-411a-8f92-bf30ac85e0ba}</Project>
83+
<Name>ConvNetSharp.Volume.GPU.2015</Name>
84+
</ProjectReference>
7685
<ProjectReference Include="..\ConvNetSharp.Volume\ConvNetSharp.Volume.2015.csproj">
7786
<Project>{1a285120-f2f0-4a94-ab31-b4e870679c0e}</Project>
7887
<Name>ConvNetSharp.Volume.2015</Name>

src/ConvNetSharp.Flow.Tests/ConvNetSharp.Flow.Tests.csproj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<DefineConstants>DEBUG;TRACE</DefineConstants>
2222
<ErrorReport>prompt</ErrorReport>
2323
<WarningLevel>4</WarningLevel>
24+
<PlatformTarget>x64</PlatformTarget>
2425
</PropertyGroup>
2526
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
2627
<DebugType>pdbonly</DebugType>
@@ -61,10 +62,18 @@
6162
<Compile Include="VolumeMock.cs" />
6263
</ItemGroup>
6364
<ItemGroup>
65+
<ProjectReference Include="..\ConvNetSharp.Core\ConvNetSharp.Core.csproj">
66+
<Project>{3a6864af-30b4-4c0e-b621-3571e3d88b43}</Project>
67+
<Name>ConvNetSharp.Core</Name>
68+
</ProjectReference>
6469
<ProjectReference Include="..\ConvNetSharp.Flow\ConvNetSharp.Flow.csproj">
6570
<Project>{bf60e630-00e6-40cd-8b6d-f92c37da9959}</Project>
6671
<Name>ConvNetSharp.Flow</Name>
6772
</ProjectReference>
73+
<ProjectReference Include="..\ConvNetSharp.Volume.GPU\ConvNetSharp.Volume.GPU.csproj">
74+
<Project>{608c97db-721f-4b34-be2d-01a85b762be9}</Project>
75+
<Name>ConvNetSharp.Volume.GPU</Name>
76+
</ProjectReference>
6877
<ProjectReference Include="..\ConvNetSharp.Volume\ConvNetSharp.Volume.csproj">
6978
<Project>{72f5d1b1-e47b-4d37-8ed4-160aa653b763}</Project>
7079
<Name>ConvNetSharp.Volume</Name>

src/ConvNetSharp.Flow.Tests/OpsTests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ namespace ConvNetSharp.Flow.Tests
88
[TestClass]
99
public class OpsTests
1010
{
11+
public OpsTests()
12+
{
13+
BuilderInstance<double>.Volume = new Volume.GPU.Double.VolumeBuilder();
14+
}
15+
1116
class MyOp : Op<float>
1217
{
1318
public override void Differentiate()
@@ -74,6 +79,29 @@ public void AddOpForward()
7479
}
7580
}
7681

82+
[TestMethod]
83+
public void AddOpBackward()
84+
{
85+
var volA = new Const<double>(BuilderInstance<double>.Volume.SameAs(new Shape(1, 1, 3, 5)), "A");
86+
var volB = new Const<double>(BuilderInstance<double>.Volume.SameAs(new[] { 1.0, 2.0, 3.0 }, new Shape(1, 1, 3, 1)), "bias");
87+
var op = new Add<double>(volA, volB);
88+
89+
using (var session = new Session<double>())
90+
{
91+
var eval = op.Evaluate(session);
92+
Assert.IsNotNull(eval);
93+
94+
op.Derivate = new Const<double>(BuilderInstance<double>.Volume.SameAs(new double[15].Populate(1.0), new Shape(1, 1, 3, 5)), "error");
95+
96+
op.Differentiate();
97+
98+
var volADiff = volA.Derivate.Evaluate(session);
99+
Assert.AreEqual(volA.Result.Shape, volADiff.Shape);
100+
var volBDiff = volB.Derivate.Evaluate(session);
101+
Assert.AreEqual(volB.Result.Shape, volBDiff.Shape);
102+
}
103+
}
104+
77105
[TestMethod]
78106
public void MultOpForward()
79107
{

0 commit comments

Comments
 (0)