Skip to content
This repository has been archived by the owner on Mar 12, 2020. It is now read-only.

Commit

Permalink
....
Browse files Browse the repository at this point in the history
  • Loading branch information
Deepak Battini committed Nov 14, 2017
1 parent 17d878f commit fbc20b4
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 17 deletions.
2 changes: 1 addition & 1 deletion SiaNet/Application/FastRCNN.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ public List<PredResult> Predict(Bitmap bmp, double confidence = 0.5)

Logging.WriteTrace("Prediction Completed");

return null;
return result;
}
catch (Exception ex)
{
Expand Down
6 changes: 3 additions & 3 deletions SiaNet/GlobalParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public static DeviceDescriptor Device
{
if (device == null)
{
var gpiList = DeviceDescriptor.AllDevices().Where(x => (x.Type == DeviceKind.GPU)).ToList();
if (gpiList.Count > 0)
device = gpiList[0];
var gpuList = DeviceDescriptor.AllDevices().Where(x => (x.Type == DeviceKind.GPU)).ToList();
if (gpuList.Count > 0)
device = gpuList[0];
else
device = DeviceDescriptor.CPUDevice;
}
Expand Down
68 changes: 58 additions & 10 deletions SiaNet/Model/ImageDataFrame.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CNTK;
using Emgu.CV.Structure;
using SiaNet.Processing;
using System;
using System.Collections.Generic;
Expand All @@ -11,13 +12,22 @@

namespace SiaNet.Model
{
internal class ImageMapInfo
{
internal string Filepath;
internal int Label;
internal int RotationAngle;
internal Emgu.CV.CvEnum.FlipType Flip = Emgu.CV.CvEnum.FlipType.None;
internal int Resize = 0;
}

public class ImageDataFrame
{
private int[] features;
private int labels;
private string folder;
private bool fromFolder;
Dictionary<string, int> folderMapData;
List<ImageMapInfo> folderMapData;

public ImageDataFrame(Variable feature, Variable label)
{
Expand All @@ -27,11 +37,11 @@ public ImageDataFrame(Variable feature, Variable label)
counter = 0;
}

public ImageDataFrame(string folder, int randomRotation = 0, bool horizontalFlip = false, bool verticalFlip = false)
public ImageDataFrame(string folder, int resize = 0, int numberOfRandomRotation = 0, bool horizontalFlip = false, bool verticalFlip = false)
{
this.folder = folder;
fromFolder = true;
folderMapData = new Dictionary<string, int>();
folderMapData = new List<ImageMapInfo>();
DirectoryInfo dir = new DirectoryInfo(folder);
var subfolders = dir.GetDirectories();
int counter = 1;
Expand All @@ -40,7 +50,24 @@ public ImageDataFrame(string folder, int randomRotation = 0, bool horizontalFlip
var files = item.GetFiles().Select(x => (x.FullName)).ToList();
foreach (var file in files)
{
folderMapData.Add(file, counter);
folderMapData.Add(new ImageMapInfo() { Filepath = file, Label = counter, RotationAngle = 0, Resize = resize });
if (numberOfRandomRotation > 0)
{
for (int i = 0; i < numberOfRandomRotation; i++)
{
folderMapData.Add(new ImageMapInfo() { Filepath = file, Label = counter, RotationAngle = new Random(30).Next(10, 360), Resize = resize });
}
}

if (horizontalFlip)
{
folderMapData.Add(new ImageMapInfo() { Filepath = file, Label = counter, RotationAngle = 0, Flip = Emgu.CV.CvEnum.FlipType.Horizontal, Resize = resize });
}

if (verticalFlip)
{
folderMapData.Add(new ImageMapInfo() { Filepath = file, Label = counter, RotationAngle = 0, Flip = Emgu.CV.CvEnum.FlipType.Vertical, Resize = resize });
}
}

counter++;
Expand Down Expand Up @@ -121,12 +148,11 @@ public bool GetNextFromFolder(int batchSize)

foreach (var item in batchData)
{
Bitmap bmp = new Bitmap(item.Key);
byteData.AddRange(bmp.ParallelExtractCHW());
byteData.AddRange(processImageFile(item));

for (int i = 1; i <= labels; i++)
{
if (item.Value == i)
if (item.Label == i)
{
labelData.Add(1);
}
Expand All @@ -143,6 +169,28 @@ public bool GetNextFromFolder(int batchSize)
return true;
}

private List<float> processImageFile(ImageMapInfo mapInfo)
{
Bitmap bmp = new Bitmap(mapInfo.Filepath);
Emgu.CV.Image<Bgr, byte> img = new Emgu.CV.Image<Bgr, byte>(bmp);
if (mapInfo.Resize > 0)
{
img = img.Resize(mapInfo.Resize, mapInfo.Resize, Emgu.CV.CvEnum.Inter.Nearest);
}

if (mapInfo.Flip != Emgu.CV.CvEnum.FlipType.None)
{
img = img.Flip(mapInfo.Flip);
}

if (mapInfo.RotationAngle > 0)
{
img.Rotate(mapInfo.RotationAngle, new Bgr(Color.White));
}

return img.Bitmap.ParallelExtractCHW();
}

internal void Reset()
{
counter = 1;
Expand All @@ -152,7 +200,7 @@ internal void Reset()

private void Shuffle()
{
Dictionary<string, int> clone = folderMapData;
List<ImageMapInfo> clone = folderMapData;
if (folderMapData.Count > 0)
{
clone.Clear();
Expand All @@ -162,8 +210,8 @@ private void Shuffle()
{
int row = random.Next(0, folderMapData.Count);
var element = folderMapData.ElementAt(row);
clone.Add(element.Key, element.Value);
folderMapData.Remove(element.Key);
clone.Add(element);
folderMapData.Remove(element);
}
}

Expand Down
25 changes: 24 additions & 1 deletion SiaNet/Sequential.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ public string Module

private ITrainPredict trainPredict;

private bool customBuilt = false;

/// <summary>
/// Gets or sets the training result.
/// </summary>
Expand Down Expand Up @@ -109,12 +111,27 @@ public Sequential()
learners = new List<Learner>();
}

/// <summary>
/// Initializes a new instance of the <see cref="Sequential"/> class.
/// </summary>
/// <param name="model">Model build outside and pass to this instance for training</param>
/// <param name="feature">Feature variable</param>
/// <param name="label">Label variable</param>
public Sequential(Function model, Variable feature, Variable label)
: this()
{
modelOut = model;
featureVariable = feature;
labelVariable = label;
customBuilt = true;
}

/// <summary>
/// Sequentials the on batch end.
/// </summary>
/// <param name="epoch">The epoch.</param>
/// <param name="batchNumber">The batch number.</param>
/// <param name="samplesSeen">The samples seen.</param>
/// <param name="samplesSeen">The no. of samples seen.</param>
/// <param name="loss">The loss.</param>
/// <param name="metrics">The metrics.</param>
private void Sequential_OnBatchEnd(int epoch, int batchNumber, uint samplesSeen, double loss, Dictionary<string, double> metrics)
Expand Down Expand Up @@ -217,6 +234,9 @@ public void LoadModel(string filepath)
/// <param name="config">The configuration.</param>
public void Add(LayerConfig config)
{
if (customBuilt)
throw new Exception("Cannot add layers to this sequential instance.");

Layers.Add(config);
}

Expand Down Expand Up @@ -275,6 +295,9 @@ public void Compile(BaseOptimizer optimizer, string loss, string metric = "", Re

private void CompileModel()
{
if (customBuilt)
return;

bool first = true;
foreach (var item in Layers)
{
Expand Down
3 changes: 1 addition & 2 deletions SieNet.Examples.CPUOnly/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ static void Main(string[] args)
{
//Setting global device
Logging.OnWriteLog += Logging_OnWriteLog;
GlobalParameters.Device = DeviceDescriptor.CPUDevice;


//Housing regression example
HousingRegression.LoadData();
HousingRegression.BuildModel();
Expand Down

0 comments on commit fbc20b4

Please sign in to comment.