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

Commit fbc20b4

Browse files
author
Deepak Battini
committed
....
1 parent 17d878f commit fbc20b4

File tree

5 files changed

+87
-17
lines changed

5 files changed

+87
-17
lines changed

SiaNet/Application/FastRCNN.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ public List<PredResult> Predict(Bitmap bmp, double confidence = 0.5)
237237

238238
Logging.WriteTrace("Prediction Completed");
239239

240-
return null;
240+
return result;
241241
}
242242
catch (Exception ex)
243243
{

SiaNet/GlobalParameters.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ public static DeviceDescriptor Device
2121
{
2222
if (device == null)
2323
{
24-
var gpiList = DeviceDescriptor.AllDevices().Where(x => (x.Type == DeviceKind.GPU)).ToList();
25-
if (gpiList.Count > 0)
26-
device = gpiList[0];
24+
var gpuList = DeviceDescriptor.AllDevices().Where(x => (x.Type == DeviceKind.GPU)).ToList();
25+
if (gpuList.Count > 0)
26+
device = gpuList[0];
2727
else
2828
device = DeviceDescriptor.CPUDevice;
2929
}

SiaNet/Model/ImageDataFrame.cs

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using CNTK;
2+
using Emgu.CV.Structure;
23
using SiaNet.Processing;
34
using System;
45
using System.Collections.Generic;
@@ -11,13 +12,22 @@
1112

1213
namespace SiaNet.Model
1314
{
15+
internal class ImageMapInfo
16+
{
17+
internal string Filepath;
18+
internal int Label;
19+
internal int RotationAngle;
20+
internal Emgu.CV.CvEnum.FlipType Flip = Emgu.CV.CvEnum.FlipType.None;
21+
internal int Resize = 0;
22+
}
23+
1424
public class ImageDataFrame
1525
{
1626
private int[] features;
1727
private int labels;
1828
private string folder;
1929
private bool fromFolder;
20-
Dictionary<string, int> folderMapData;
30+
List<ImageMapInfo> folderMapData;
2131

2232
public ImageDataFrame(Variable feature, Variable label)
2333
{
@@ -27,11 +37,11 @@ public ImageDataFrame(Variable feature, Variable label)
2737
counter = 0;
2838
}
2939

30-
public ImageDataFrame(string folder, int randomRotation = 0, bool horizontalFlip = false, bool verticalFlip = false)
40+
public ImageDataFrame(string folder, int resize = 0, int numberOfRandomRotation = 0, bool horizontalFlip = false, bool verticalFlip = false)
3141
{
3242
this.folder = folder;
3343
fromFolder = true;
34-
folderMapData = new Dictionary<string, int>();
44+
folderMapData = new List<ImageMapInfo>();
3545
DirectoryInfo dir = new DirectoryInfo(folder);
3646
var subfolders = dir.GetDirectories();
3747
int counter = 1;
@@ -40,7 +50,24 @@ public ImageDataFrame(string folder, int randomRotation = 0, bool horizontalFlip
4050
var files = item.GetFiles().Select(x => (x.FullName)).ToList();
4151
foreach (var file in files)
4252
{
43-
folderMapData.Add(file, counter);
53+
folderMapData.Add(new ImageMapInfo() { Filepath = file, Label = counter, RotationAngle = 0, Resize = resize });
54+
if (numberOfRandomRotation > 0)
55+
{
56+
for (int i = 0; i < numberOfRandomRotation; i++)
57+
{
58+
folderMapData.Add(new ImageMapInfo() { Filepath = file, Label = counter, RotationAngle = new Random(30).Next(10, 360), Resize = resize });
59+
}
60+
}
61+
62+
if (horizontalFlip)
63+
{
64+
folderMapData.Add(new ImageMapInfo() { Filepath = file, Label = counter, RotationAngle = 0, Flip = Emgu.CV.CvEnum.FlipType.Horizontal, Resize = resize });
65+
}
66+
67+
if (verticalFlip)
68+
{
69+
folderMapData.Add(new ImageMapInfo() { Filepath = file, Label = counter, RotationAngle = 0, Flip = Emgu.CV.CvEnum.FlipType.Vertical, Resize = resize });
70+
}
4471
}
4572

4673
counter++;
@@ -121,12 +148,11 @@ public bool GetNextFromFolder(int batchSize)
121148

122149
foreach (var item in batchData)
123150
{
124-
Bitmap bmp = new Bitmap(item.Key);
125-
byteData.AddRange(bmp.ParallelExtractCHW());
151+
byteData.AddRange(processImageFile(item));
126152

127153
for (int i = 1; i <= labels; i++)
128154
{
129-
if (item.Value == i)
155+
if (item.Label == i)
130156
{
131157
labelData.Add(1);
132158
}
@@ -143,6 +169,28 @@ public bool GetNextFromFolder(int batchSize)
143169
return true;
144170
}
145171

172+
private List<float> processImageFile(ImageMapInfo mapInfo)
173+
{
174+
Bitmap bmp = new Bitmap(mapInfo.Filepath);
175+
Emgu.CV.Image<Bgr, byte> img = new Emgu.CV.Image<Bgr, byte>(bmp);
176+
if (mapInfo.Resize > 0)
177+
{
178+
img = img.Resize(mapInfo.Resize, mapInfo.Resize, Emgu.CV.CvEnum.Inter.Nearest);
179+
}
180+
181+
if (mapInfo.Flip != Emgu.CV.CvEnum.FlipType.None)
182+
{
183+
img = img.Flip(mapInfo.Flip);
184+
}
185+
186+
if (mapInfo.RotationAngle > 0)
187+
{
188+
img.Rotate(mapInfo.RotationAngle, new Bgr(Color.White));
189+
}
190+
191+
return img.Bitmap.ParallelExtractCHW();
192+
}
193+
146194
internal void Reset()
147195
{
148196
counter = 1;
@@ -152,7 +200,7 @@ internal void Reset()
152200

153201
private void Shuffle()
154202
{
155-
Dictionary<string, int> clone = folderMapData;
203+
List<ImageMapInfo> clone = folderMapData;
156204
if (folderMapData.Count > 0)
157205
{
158206
clone.Clear();
@@ -162,8 +210,8 @@ private void Shuffle()
162210
{
163211
int row = random.Next(0, folderMapData.Count);
164212
var element = folderMapData.ElementAt(row);
165-
clone.Add(element.Key, element.Value);
166-
folderMapData.Remove(element.Key);
213+
clone.Add(element);
214+
folderMapData.Remove(element);
167215
}
168216
}
169217

SiaNet/Sequential.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ public string Module
8181

8282
private ITrainPredict trainPredict;
8383

84+
private bool customBuilt = false;
85+
8486
/// <summary>
8587
/// Gets or sets the training result.
8688
/// </summary>
@@ -109,12 +111,27 @@ public Sequential()
109111
learners = new List<Learner>();
110112
}
111113

114+
/// <summary>
115+
/// Initializes a new instance of the <see cref="Sequential"/> class.
116+
/// </summary>
117+
/// <param name="model">Model build outside and pass to this instance for training</param>
118+
/// <param name="feature">Feature variable</param>
119+
/// <param name="label">Label variable</param>
120+
public Sequential(Function model, Variable feature, Variable label)
121+
: this()
122+
{
123+
modelOut = model;
124+
featureVariable = feature;
125+
labelVariable = label;
126+
customBuilt = true;
127+
}
128+
112129
/// <summary>
113130
/// Sequentials the on batch end.
114131
/// </summary>
115132
/// <param name="epoch">The epoch.</param>
116133
/// <param name="batchNumber">The batch number.</param>
117-
/// <param name="samplesSeen">The samples seen.</param>
134+
/// <param name="samplesSeen">The no. of samples seen.</param>
118135
/// <param name="loss">The loss.</param>
119136
/// <param name="metrics">The metrics.</param>
120137
private void Sequential_OnBatchEnd(int epoch, int batchNumber, uint samplesSeen, double loss, Dictionary<string, double> metrics)
@@ -217,6 +234,9 @@ public void LoadModel(string filepath)
217234
/// <param name="config">The configuration.</param>
218235
public void Add(LayerConfig config)
219236
{
237+
if (customBuilt)
238+
throw new Exception("Cannot add layers to this sequential instance.");
239+
220240
Layers.Add(config);
221241
}
222242

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

276296
private void CompileModel()
277297
{
298+
if (customBuilt)
299+
return;
300+
278301
bool first = true;
279302
foreach (var item in Layers)
280303
{

SieNet.Examples.CPUOnly/Program.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ static void Main(string[] args)
2020
{
2121
//Setting global device
2222
Logging.OnWriteLog += Logging_OnWriteLog;
23-
GlobalParameters.Device = DeviceDescriptor.CPUDevice;
24-
23+
2524
//Housing regression example
2625
HousingRegression.LoadData();
2726
HousingRegression.BuildModel();

0 commit comments

Comments
 (0)