Skip to content

Commit

Permalink
Update Deep Learning Framework
Browse files Browse the repository at this point in the history
  • Loading branch information
daelsepara committed Oct 10, 2018
1 parent 5bb76a8 commit a51113a
Show file tree
Hide file tree
Showing 16 changed files with 1,485 additions and 58 deletions.
5 changes: 4 additions & 1 deletion DeepLearnUI/ConvolutionalNeuralNetworkOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ public class ConvolutionalNeuralNetworkOptions
public int BatchSize;
public int Epochs;
public int Items;
public bool Pool;
public bool Shuffle;

public ConvolutionalNeuralNetworkOptions(double alpha, int batchsize, int epochs, int items, bool shuffle = false)
public ConvolutionalNeuralNetworkOptions(double alpha, int batchsize, int epochs, int items, bool pool = false, bool shuffle = false)
{
Alpha = alpha;
BatchSize = batchsize;
Epochs = epochs;
Items = items;
Pool = pool;
Shuffle = shuffle;
}

Expand All @@ -23,6 +25,7 @@ public ConvolutionalNeuralNetworkOptions()
BatchSize = 50;
Epochs = 1;
Items = 50;
Pool = false;
Shuffle = false;
}
}
Expand Down
20 changes: 1 addition & 19 deletions DeepLearnUI/DeepLearnUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,25 +97,7 @@ void CleanUpUI()

void InitializeNetwork()
{
cnn = new ManagedCNN();

cnn.AddLayer(new ManagedLayer());
cnn.AddLayer(new ManagedLayer(6, 5));
cnn.AddLayer(new ManagedLayer(2));
cnn.AddLayer(new ManagedLayer(12, 5));
cnn.AddLayer(new ManagedLayer(2));

// Setup layer 1 - convolution layer
cnn.LoadFeatureMap(BaseDirectory, "Layer02FeatureMap", 1, 5, 5, 1, 6);
cnn.LoadFeatureMapBias(BaseDirectory, "Layer02Bias", 1, 6);

// Setup layer 3 - convolution layer
cnn.LoadFeatureMap(BaseDirectory, "Layer04FeatureMap", 3, 5, 5, 6, 12);
cnn.LoadFeatureMapBias(BaseDirectory, "Layer04Bias", 3, 12);

// Load Network Weights
cnn.LoadNetworkWeights(BaseDirectory, "NetworkWeights", 192, 10);
cnn.LoadNetworkBias(BaseDirectory, "NetworkBias", 10);
cnn = Utility.LoadCNN(BaseDirectory, "modelcnn");
}

void CleanUpNetwork()
Expand Down
8 changes: 8 additions & 0 deletions DeepLearnUI/DeepLearnUI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
<ApplicationIcon>DeepLearn.ico</ApplicationIcon>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.11.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
Expand Down Expand Up @@ -71,8 +74,12 @@
<Compile Include="ManagedFile.cs" />
<Compile Include="ManagedLayer.cs" />
<Compile Include="ManagedMatrix.cs" />
<Compile Include="ManagedNN.cs" />
<Compile Include="ManagedOps.cs" />
<Compile Include="MemOps.cs" />
<Compile Include="ModelJSON.cs" />
<Compile Include="NeuralNetworkOptions.cs" />
<Compile Include="Optimize.cs" />
<Compile Include="Profiler.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand All @@ -88,6 +95,7 @@
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<None Include="packages.config" />
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
Expand Down
2 changes: 1 addition & 1 deletion DeepLearnUI/LayerTypes.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace DeepLearnCS
{
public enum LayerTypes { Input, Convolution, Subsampling };
public enum LayerTypes { Input = 0, Convolution = 1, Subsampling = 2 };
}
5 changes: 5 additions & 0 deletions DeepLearnUI/ManagedArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ public ManagedIntList(int size)
}
}

public int Length()
{
return x;
}

public void Free()
{
MemOps.Free(Data);
Expand Down
22 changes: 15 additions & 7 deletions DeepLearnUI/ManagedCNN.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ public void Setup(ManagedArray input, int classes)

// 'onum' is the number of labels, that's why it is calculated using size(y, 1). If you have 20 labels so the output of the network will be 20 neurons.
// 'fvnum' is the number of output neurons at the last layer, the layer just before the output layer.
// 'Bias' is the biases of the output neurons.
// 'Weights' is the weights between the last layer and the output neurons. Note that the last layer is fully connected to the output layer, that's why the size of the weights is (onum * fvnum)
// 'ffb' is the biases of the output neurons.
// 'ffW' is the weights between the last layer and the output neurons. Note that the last layer is fully connected to the output layer, that's why the size of the weights is (onum * fvnum)

var fvnum = MapSizeX * MapSizeY * InputMaps;
var onum = classes;
Expand All @@ -118,7 +118,7 @@ public void Setup(ManagedArray input, int classes)
}

// Compute Forward Transform on 3D Input
public void FeedForward(ManagedArray batch)
public void FeedForward(ManagedArray batch, bool pool = false)
{
var n = Layers.Count;

Expand Down Expand Up @@ -197,7 +197,15 @@ public void FeedForward(ManagedArray batch)

// Subsample
ManagedConvolution.Valid(Activation, FeatureMap, z);
ManagedOps.Copy3D4D(Layers[l].Activation, z, j, Layers[l].Scale);

if (pool)
{
ManagedOps.Pool3D4D(Layers[l].Activation, z, j, Layers[l].Scale);
}
else
{
ManagedOps.Copy3D4D(Layers[l].Activation, z, j, Layers[l].Scale);
}
}

ManagedOps.Free(Activation, FeatureMap, z);
Expand Down Expand Up @@ -484,7 +492,7 @@ public int Test(ManagedArray correct, ManagedArray classifcation)
}

// Classify data using trained network parameters and count classification errors
public int Classify(ManagedArray test_input, ManagedArray test_output, int classes, int items, int batchsize, ManagedArray classification)
public int Classify(ManagedArray test_input, ManagedArray test_output, int classes, int items, int batchsize, ManagedArray classification, bool pool = false)
{
var errors = 0;

Expand All @@ -503,7 +511,7 @@ public int Classify(ManagedArray test_input, ManagedArray test_output, int class
ManagedOps.Copy2D(tempy, test_output, i, 0);

// classify
FeedForward(tempx);
FeedForward(tempx, pool);

// count classifcation errors
errors += Test(tempy, tempclass);
Expand Down Expand Up @@ -535,7 +543,7 @@ public void Train(ManagedArray input, ManagedArray output, ConvolutionalNeuralNe
ManagedOps.Copy3D(temp_input, input, 0, 0, i);
ManagedOps.Copy2D(temp_output, output, i, 0);

FeedForward(temp_input);
FeedForward(temp_input, opts.Pool);
BackPropagation(temp_output);
ApplyGradients(opts);

Expand Down
54 changes: 27 additions & 27 deletions DeepLearnUI/ManagedFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ public static class ManagedFile
{
public static CultureInfo ci = new CultureInfo("en-us");

public static void Load1D(string filename, ManagedArray A)
public static void Load1D(string filename, ManagedArray A, char delimiter = ',')
{
if (File.Exists(filename))
{
var lines = File.ReadAllLines(filename);

if (lines.Length > 0)
{
var tokens = lines[0].Split(',');
var tokens = lines[0].Split(delimiter);

for (int x = 0; x < A.Length(); x++)
{
Expand All @@ -26,7 +26,7 @@ public static void Load1D(string filename, ManagedArray A)
}
}

public static void Save1D(string filename, ManagedArray A)
public static void Save1D(string filename, ManagedArray A, char delimiter = ',')
{
using (var file = new StreamWriter(filename, false))
{
Expand All @@ -36,7 +36,7 @@ public static void Save1D(string filename, ManagedArray A)

if (x < A.x - 1)
{
file.Write(",");
file.Write(delimiter);
}
}

Expand Down Expand Up @@ -68,20 +68,19 @@ public static void Save1DY(string filename, ManagedArray A)
}
}

public static void Load2D(string filename, ManagedArray A)
public static void Load2D(string filename, ManagedArray A, char delimiter = ',')
{
if (File.Exists(filename))
{
var temp = new ManagedArray(A.x, A.y);


var lines = File.ReadAllLines(filename);

for (int y = 0; y < A.y; y++)
{
if (y < lines.Length)
{
var tokens = lines[y].Split(',');
var tokens = lines[y].Split(delimiter);

for (int x = 0; x < A.x; x++)
{
Expand All @@ -96,7 +95,7 @@ public static void Load2D(string filename, ManagedArray A)
}
}

public static void Load2DV2(string filename, ManagedArray A)
public static void Load2DV2(string filename, ManagedArray A, char delimiter = ',')
{
if (File.Exists(filename))
{
Expand All @@ -110,7 +109,7 @@ public static void Load2DV2(string filename, ManagedArray A)

if (line != null)
{
var tokens = line.Split(',');
var tokens = line.Split(delimiter);

for (int x = 0; x < A.x; x++)
{
Expand All @@ -126,28 +125,29 @@ public static void Load2DV2(string filename, ManagedArray A)
}
}

public static void Save2D(string filename, ManagedArray A)
public static void Save2D(string filename, ManagedArray A, char delimiter = ',')
{
using (var file = new StreamWriter(filename, false))
{
for (int y = 0; y < A.y; y++)
{
if (y > 0)
file.WriteLine();

for (int x = 0; x < A.x; x++)
{
file.Write(A[x, y].ToString(ci));

if (x < A.x - 1)
{
file.Write(",");
file.Write(delimiter);
}
}

file.WriteLine();
}
}
}

public static void Load2D4D(string filename, ManagedArray A, int i, int j)
public static void Load2D4D(string filename, ManagedArray A, int i, int j, char delimiter = ',')
{
if (File.Exists(filename))
{
Expand All @@ -159,7 +159,7 @@ public static void Load2D4D(string filename, ManagedArray A, int i, int j)
{
if (y < lines.Length)
{
var tokens = lines[y].Split(',');
var tokens = lines[y].Split(delimiter);

for (int x = 0; x < A.x; x++)
{
Expand All @@ -174,7 +174,7 @@ public static void Load2D4D(string filename, ManagedArray A, int i, int j)
}
}

public static void Save2D4D(string filename, ManagedArray A, int i, int j)
public static void Save2D4D(string filename, ManagedArray A, int i, int j, char delimiter = ',')
{
using (var file = new StreamWriter(filename, false))
{
Expand All @@ -190,7 +190,7 @@ public static void Save2D4D(string filename, ManagedArray A, int i, int j)

if (x < A.x - 1)
{
file.Write(",");
file.Write(delimiter);
}
}

Expand All @@ -201,7 +201,7 @@ public static void Save2D4D(string filename, ManagedArray A, int i, int j)
}
}

public static void Load3D(string filename, ManagedArray A)
public static void Load3D(string filename, ManagedArray A, char delimiter = ',')
{
if (File.Exists(filename))
{
Expand All @@ -211,7 +211,7 @@ public static void Load3D(string filename, ManagedArray A)
{
if (y < lines.Length)
{
var tokens = lines[y].Split(',');
var tokens = lines[y].Split(delimiter);

for (int z = 0; z < A.z; z++)
{
Expand All @@ -225,7 +225,7 @@ public static void Load3D(string filename, ManagedArray A)
}
}

public static void Load3DV2(string filename, ManagedArray A)
public static void Load3DV2(string filename, ManagedArray A, char delimiter = ',')
{
if (File.Exists(filename))
{
Expand All @@ -237,7 +237,7 @@ public static void Load3DV2(string filename, ManagedArray A)

if (line != null)
{
var tokens = line.Split(',');
var tokens = line.Split(delimiter);

for (int z = 0; z < A.z; z++)
{
Expand All @@ -252,7 +252,7 @@ public static void Load3DV2(string filename, ManagedArray A)
}
}

public static void Save3D(string filename, ManagedArray A)
public static void Save3D(string filename, ManagedArray A, char delimiter = ',')
{
using (var file = new StreamWriter(filename, false))
{
Expand All @@ -266,7 +266,7 @@ public static void Save3D(string filename, ManagedArray A)

if (z < A.z - 1 || x < A.x - 1)
{
file.Write(",");
file.Write(delimiter);
}
}
}
Expand All @@ -276,7 +276,7 @@ public static void Save3D(string filename, ManagedArray A)
}
}

public static void Load3D2D(string filename, ManagedArray A)
public static void Load3D2D(string filename, ManagedArray A, char delimiter = ',')
{
if (File.Exists(filename))
{
Expand All @@ -295,7 +295,7 @@ public static void Load3D2D(string filename, ManagedArray A)

if (y < lines.Length)
{
var tokens = lines[y].Split(',');
var tokens = lines[y].Split(delimiter);

for (int z = 0; z < zz; z++)
{
Expand All @@ -311,7 +311,7 @@ public static void Load3D2D(string filename, ManagedArray A)
}
}

public static void Load3D2DV2(string filename, ManagedArray A)
public static void Load3D2DV2(string filename, ManagedArray A, char delimiter = ',')
{
if (File.Exists(filename))
{
Expand All @@ -332,7 +332,7 @@ public static void Load3D2DV2(string filename, ManagedArray A)
{
var xoffset = y * xx;

var tokens = line.Split(',');
var tokens = line.Split(delimiter);

for (int z = 0; z < zz; z++)
{
Expand Down

0 comments on commit a51113a

Please sign in to comment.