MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
DeepDraw.cs
1using MyCaffe.basecode;
2using MyCaffe.common;
3using MyCaffe.data;
4using System;
5using System.Collections;
6using System.Collections.Generic;
7using System.Drawing;
8using System.Drawing.Imaging;
9using System.Linq;
10using System.Text;
11using System.Threading.Tasks;
12
17{
29 public class DeepDraw<T> : IDisposable, IEnumerable<Octaves>
30 {
31 OctavesCollection m_rgOctaves = new OctavesCollection();
32 CancelEvent m_evtCancel;
33 Net<T> m_net;
34 DataTransformer<T> m_transformer;
35 Log m_log;
36 CudaDnn<T> m_cuda;
37 int m_nWid = 0;
38 int m_nHt = 0;
39 Blob<T> m_blobBase;
40 Blob<T> m_blobDetail;
41 Blob<T> m_blobBlur;
42
50 public DeepDraw(CancelEvent evtCancel, Net<T> net, DataTransformer<T> transformer, string strSrcBlobName = "data")
51 {
52 m_evtCancel = evtCancel;
53 m_net = net;
54 m_transformer = transformer;
55
56 Blob<T> blobSrc = m_net.blob_by_name(strSrcBlobName);
57 m_cuda = blobSrc.Cuda;
58 m_log = blobSrc.Log;
59
60 m_nWid = blobSrc.width;
61 m_nHt = blobSrc.height;
62
63 m_blobBase = new common.Blob<T>(m_cuda, m_log, false);
64 m_blobDetail = new common.Blob<T>(m_cuda, m_log, false);
65 m_blobBlur = new common.Blob<T>(m_cuda, m_log, false);
66
67 m_blobBase.ReshapeLike(blobSrc);
68 m_blobDetail.ReshapeLike(blobSrc);
69 m_blobBlur.ReshapeLike(blobSrc);
70 }
71
75 public void Dispose()
76 {
77 if (m_blobBase != null)
78 {
79 m_blobBase.Dispose();
80 m_blobBase = null;
81 }
82
83 if (m_blobDetail != null)
84 {
85 m_blobDetail.Dispose();
86 m_blobDetail = null;
87 }
88
89 if (m_blobBlur != null)
90 {
91 m_blobBlur.Dispose();
92 m_blobBlur = null;
93 }
94 }
95
99 public int Count
100 {
101 get { return m_rgOctaves.Count; }
102 }
103
115 public void Add(string strLayer, int nIterations, double dfStartSigma, double dfEndSigma, double dfStartStep, double dfEndStep, bool bSaveFile = false, double dfPctDetailsToApply = 0.25)
116 {
117 m_rgOctaves.Add(new Octaves(strLayer, nIterations, dfStartSigma, dfEndSigma, dfStartStep, dfEndStep, bSaveFile, dfPctDetailsToApply));
118 }
119
124 public void Add(Octaves octaves)
125 {
126 m_rgOctaves.Add(octaves);
127 }
128
133 public IEnumerator<Octaves> GetEnumerator()
134 {
135 return m_rgOctaves.GetEnumerator();
136 }
137
142 IEnumerator IEnumerable.GetEnumerator()
143 {
144 return m_rgOctaves.GetEnumerator();
145 }
146
155 public Bitmap CreateRandomImage(Color clrBack, int nW = -1, int nH = -1, double dfScale = 16.0)
156 {
157 if (nW <= 0)
158 nW = m_nWid;
159
160 if (nH <= 0)
161 nH = m_nWid;
162
163 return CreateRandomImageEx(clrBack, nW, nH, dfScale);
164 }
165
174 public static Bitmap CreateRandomImageEx(Color clrBack, int nW, int nH, double dfScale = 16.0)
175 {
176 Bitmap bmp = new Bitmap(nW, nH);
177 Random rand = new Random();
178 double dfVal;
179
180 for (int y = 0; y < nH; y++)
181 {
182 for (int x = 0; x < nW; x++)
183 {
184 dfVal = rand.NextDouble() * dfScale;
185 int nR = clrBack.R - (int)((dfScale / 2) + dfVal);
186
187 dfVal = rand.NextDouble() * dfScale;
188 int nG = clrBack.G - (int)((dfScale / 2) + dfVal);
189
190 dfVal = rand.NextDouble() * dfScale;
191 int nB = clrBack.B - (int)((dfScale / 2) + dfVal);
192
193 bmp.SetPixel(x, y, Color.FromArgb(nR, nG, nB));
194 }
195 }
196
197 return bmp;
198 }
199
213 public bool Render(Bitmap bmpInput, int nFocusLabel = -1, double dfDetailPercentageToOutput = 0.25, string strOutputDir = null, bool bVisualizeEachStep = false, float[] rgDirectInputs = null)
214 {
215 if (rgDirectInputs != null && nFocusLabel < 0)
216 throw new Exception("The focus label must be set to a unique value >= 0 that corresponds to this specific direct input set.");
217
218 // get the input dimensions from net
219 Blob<T> blobSrc = m_net.blob_by_name("data");
220
221 int nW = blobSrc.width;
222 int nH = blobSrc.height;
223
224 m_log.WriteLine("Starting drawing...");
225 blobSrc.Reshape(1, 3, nH, nW); // resize the networks input.
226
227 // Set the base data.
228 if (strOutputDir != null)
229 bmpInput.Save(strOutputDir + "\\input_image.png");
230
231 Datum d = ImageData.GetImageDataD(bmpInput, 3, false, -1);
232 m_blobBase.mutable_cpu_data = m_transformer.Transform(d);
233
234 m_blobDetail.SetData(0.0);
235 m_blobBlur.SetData(0);
236
237 for (int i=0; i<m_rgOctaves.Count; i++)
238 {
239 Octaves o = m_rgOctaves[i];
240 // Select layer.
241 string strLayer = o.LayerName;
242
243 // Add changed details to the image.
244 if (nFocusLabel < 0)
245 m_cuda.add(blobSrc.count(), m_blobBase.gpu_data, m_blobDetail.gpu_data, blobSrc.mutable_gpu_data, o.PercentageOfPreviousOctaveDetailsToApply);
246
247 for (int j = 0; j < o.IterationN; j++)
248 {
249 if (m_evtCancel.WaitOne(0))
250 return false;
251
252 if (nFocusLabel >= 0)
253 blobSrc.CopyFrom(m_blobBase);
254
255 double dfSigma = o.StartSigma + ((o.EndSigma - o.StartSigma) * j) / o.IterationN;
256 double dfStepSize = o.StartStepSize + ((o.EndStepSize - o.StartStepSize) * j) / o.IterationN;
257
258 make_step(strLayer, dfSigma, dfStepSize, nFocusLabel, rgDirectInputs);
259
260 if ((bVisualizeEachStep || (j == o.IterationN - 1 && o.Save)))
261 {
262 // Get the detail.
263 m_cuda.sub(m_blobDetail.count(), blobSrc.gpu_data, m_blobBase.gpu_data, m_blobDetail.mutable_gpu_data);
264
265 if (dfDetailPercentageToOutput < 1.0)
266 {
267 // reuse blob blur memory.
268 m_cuda.add(m_blobBlur.count(), m_blobBase.gpu_data, m_blobDetail.gpu_data, m_blobBlur.mutable_gpu_data, dfDetailPercentageToOutput);
269 }
270 else
271 {
272 m_blobBlur.CopyFrom(blobSrc);
273 }
274
275 Image bmp = getImage(m_blobBlur);
276
277 if (nFocusLabel < 0)
278 {
279 Bitmap bmp1 = AdjustContrast(bmp, 0.9f, 1.6f, 1.2f);
280 bmp.Dispose();
281 bmp = bmp1;
282 }
283
284 if (strOutputDir != null)
285 {
286 string strFile = strOutputDir + "\\" + o.UniqueName + "_" + j.ToString();
287 if (nFocusLabel >= 0)
288 {
289 if (rgDirectInputs != null)
290 strFile += "_idx_" + nFocusLabel.ToString();
291 else
292 strFile += "_class_" + nFocusLabel.ToString();
293 }
294
295 bmp.Save(strFile + ".png");
296 }
297
298 if (j == o.IterationN - 1)
299 o.Images.Add(nFocusLabel, bmp);
300 else
301 bmp.Dispose();
302 }
303
304 m_log.Progress = (double)j / (double)o.IterationN;
305 m_log.WriteLine("Focus Label: " + nFocusLabel.ToString() + " Octave: '" + o.LayerName + "' - " + j.ToString() + " of " + o.IterationN.ToString() + " " + m_log.Progress.ToString("P"));
306
307 if (nFocusLabel >= 0)
308 m_blobBase.CopyFrom(blobSrc);
309 }
310
311 // Extract details produced on the current octave.
312 if (nFocusLabel < 0)
313 m_cuda.sub(m_blobDetail.count(), blobSrc.gpu_data, m_blobBase.gpu_data, m_blobDetail.mutable_gpu_data);
314 }
315
316 m_log.WriteLine("Rendering completed!");
317 return true;
318 }
319
320 private void make_step(string strLayer, double dfSigma, double dfStepSize = 1.5, int nFocusLabel = -1, float[] rgDirectInputs = null)
321 {
322 Blob<T> blobSrc = m_net.blob_by_name("data"); // input image is stored in Net's 'data' blob
323 Blob<T> blobDst = m_net.blob_by_name(strLayer);
324 int nDstIdx = m_net.layer_index_by_name(strLayer);
325 double dfLoss;
326
327 m_net.Forward(out dfLoss);
328
329 if (nFocusLabel < 0 && rgDirectInputs == null)
330 {
331 m_cuda.copy(blobDst.count(), blobDst.gpu_data, blobDst.mutable_gpu_diff);
332 }
333 else if (rgDirectInputs != null)
334 {
335 blobDst.SetDiff(0);
336
337 for (int i = 0; i < rgDirectInputs.Length && i < blobDst.count(); i++)
338 {
339 if (rgDirectInputs[i] != 0)
340 blobDst.SetDiff(rgDirectInputs[i], i);
341 }
342 }
343 else
344 {
345 if (nFocusLabel >= blobDst.count())
346 throw new Exception("The focus label '" + nFocusLabel + "' is greater than the number of outputs for blob '" + blobDst.Name + "' -- it only has '" + blobDst.count().ToString() + "' outputs!");
347
348 blobDst.SetDiff(0);
349 blobDst.SetDiff(1.0, nFocusLabel);
350 }
351
352 m_net.Backward(nDstIdx);
353
354 // Apply normalized ascent step to the input image.
355 double dfAsum = Utility.ConvertVal<T>(blobSrc.asum_diff());
356 double dfMean = dfAsum / blobSrc.count();
357 double dfStep = dfStepSize / dfMean;
358 m_cuda.scal(blobSrc.count(), dfStep, blobSrc.mutable_gpu_diff);
359 m_cuda.add(blobSrc.count(), blobSrc.gpu_diff, blobSrc.gpu_data, blobSrc.mutable_gpu_data);
360
361 if (dfSigma != 0)
362 {
363 m_cuda.gaussian_blur(blobSrc.count(), blobSrc.channels, blobSrc.height, blobSrc.width, dfSigma, blobSrc.gpu_data, m_blobBlur.mutable_gpu_data);
364 blobSrc.CopyFrom(m_blobBlur);
365 }
366 }
367
368 private Image getImage(Blob<T> blobSrc)
369 {
370 scale(blobSrc, 0.0, 255.0, true);
371 return ImageData.GetImage(blobSrc.ToDatum());
372 }
373
374 private void scale(Blob<T> b, double dfMin1, double dfMax1, bool bByChannel)
375 {
376 int nDim = b.height * b.width;
377 double[] rgdf = Utility.ConvertVec<T>(b.mutable_cpu_data);
378 double dfMinR = double.MaxValue;
379 double dfMinG = double.MaxValue;
380 double dfMinB = double.MaxValue;
381 double dfMaxR = -double.MaxValue;
382 double dfMaxG = -double.MaxValue;
383 double dfMaxB = -double.MaxValue;
384
385 double dfMeanR = (m_transformer.param.mean_value.Count > 0) ? m_transformer.param.mean_value[0] : 0;
386 double dfMeanG = (m_transformer.param.mean_value.Count > 1) ? m_transformer.param.mean_value[1] : 0;
387 double dfMeanB = (m_transformer.param.mean_value.Count > 2) ? m_transformer.param.mean_value[2] : 0;
388
389 if (bByChannel)
390 {
391 for (int i = 0; i < nDim; i++)
392 {
393 double dfR = rgdf[i] + dfMeanR;
394 double dfG = rgdf[i + nDim] + dfMeanG;
395 double dfB = rgdf[i + nDim * 2] + dfMeanB;
396
397 dfMinR = Math.Min(dfR, dfMinR);
398 dfMaxR = Math.Max(dfR, dfMaxR);
399 dfMinG = Math.Min(dfG, dfMinG);
400 dfMaxG = Math.Max(dfG, dfMaxG);
401 dfMinB = Math.Min(dfB, dfMinB);
402 dfMaxB = Math.Max(dfB, dfMaxB);
403 }
404 }
405 else
406 {
407 dfMinR = b.min_data;
408 dfMaxR = b.max_data + Math.Max(dfMeanR, Math.Max(dfMeanG, dfMeanB));
409 dfMinG = dfMinR;
410 dfMaxG = dfMaxR;
411 dfMinB = dfMinR;
412 dfMaxB = dfMaxR;
413 }
414
415 double dfRs = (dfMax1 - dfMin1) / (dfMaxR - dfMinR);
416 double dfGs = (dfMax1 - dfMin1) / (dfMaxG - dfMinG);
417 double dfBs = (dfMax1 - dfMin1) / (dfMaxB - dfMinB);
418
419 for (int i = 0; i < nDim; i++)
420 {
421 double dfR = rgdf[i] + dfMeanR;
422 double dfG = rgdf[i + nDim] + dfMeanG;
423 double dfB = rgdf[i + nDim * 2] + dfMeanB;
424
425 dfR = (dfR - dfMinR) * dfRs + dfMin1;
426 dfG = (dfG - dfMinG) * dfGs + dfMin1;
427 dfB = (dfB - dfMinB) * dfBs + dfMin1;
428
429 rgdf[i] = dfR;
430 rgdf[i + nDim] = dfG;
431 rgdf[i + nDim * 2] = dfB;
432 }
433
435 }
436
445 public static Bitmap AdjustContrast(Image bmp, float fBrightness = 1.0f, float fContrast = 1.0f, float fGamma = 1.0f)
446 {
447 return ImageTools.AdjustContrast(bmp, fBrightness, fContrast, fGamma);
448 }
449
461 public static string CreateConfigurationString(int nWd, int nHt, double dfOutputDetailPct, OctavesCollection colOctaves, string strSrcBlobName, double dfRandomImageScale)
462 {
463 RawProtoCollection rgChildren = new RawProtoCollection();
464
465 rgChildren.Add("input_height", nHt.ToString(), RawProto.TYPE.STRING);
466 rgChildren.Add("input_width", nWd.ToString(), RawProto.TYPE.STRING);
467 rgChildren.Add("output_detail_pct", dfOutputDetailPct.ToString(), RawProto.TYPE.STRING);
468 rgChildren.Add("src_blob_name", strSrcBlobName, RawProto.TYPE.STRING);
469 rgChildren.Add("random_image_scale", dfRandomImageScale.ToString(), RawProto.TYPE.STRING);
470
471 foreach (Octaves octave in colOctaves)
472 {
473 rgChildren.Add(octave.ToProto("octave"));
474 }
475
476 RawProto proto = new RawProto("root", "", rgChildren);
477
478 return proto.ToString();
479 }
480
492 public static OctavesCollection ParseConfigurationString(string strConfig, out int nWd, out int nHt, out double dfOutputDetailPct, out string strSrcBlobName, out double dfRandomImageScale)
493 {
494 RawProto proto = RawProto.Parse(strConfig);
495 string strVal;
496
497 nHt = -1;
498 if ((strVal = proto.FindValue("input_height")) != null)
499 nHt = int.Parse(strVal);
500
501 nWd = -1;
502 if ((strVal = proto.FindValue("input_width")) != null)
503 nWd = int.Parse(strVal);
504
505 dfOutputDetailPct = 0.25;
506 if ((strVal = proto.FindValue("output_detail_pct")) != null)
507 dfOutputDetailPct = BaseParameter.ParseDouble(strVal);
508
509 strSrcBlobName = "data";
510 if ((strVal = proto.FindValue("src_blob_name")) != null)
511 strSrcBlobName = strVal;
512
513 dfRandomImageScale = 16;
514 if ((strVal = proto.FindValue("random_image_scale")) != null)
515 dfRandomImageScale = BaseParameter.ParseDouble(strVal);
516
518 RawProtoCollection rpcol = proto.FindChildren("octave");
519 foreach (RawProto protoChild in rpcol)
520 {
521 col.Add(Octaves.FromProto(protoChild));
522 }
523
524 return col;
525 }
526 }
527
531 public class Octaves : IDisposable
532 {
533 string m_strLayerName;
534 int m_nIterN;
535 double m_dfStartSigma;
536 double m_dfEndSigma;
537 double m_dfStartStepSize;
538 double m_dfEndStepSize;
539 bool m_bSave = false;
540 double m_dfDetailPctToApply = 0.25;
541 Dictionary<int, Image> m_rgImages = new Dictionary<int, Image>();
542
554 public Octaves(string strLayerName, int nIterN, double dfStartSigma, double dfEndSigma, double dfStartStepSize, double dfEndStepSize, bool bSaveFile = false, double dfPctDetailsToApply = 0.25)
555 {
556 m_strLayerName = strLayerName;
557 m_nIterN = nIterN;
558 m_dfStartSigma = dfStartSigma;
559 m_dfEndSigma = dfEndSigma;
560 m_dfStartStepSize = dfStartStepSize;
561 m_dfEndStepSize = dfEndStepSize;
562 m_dfDetailPctToApply = dfPctDetailsToApply;
563 m_bSave = bSaveFile;
564 }
565
569 public void Dispose()
570 {
571 foreach (KeyValuePair<int, Image> kv in m_rgImages)
572 {
573 if (kv.Value != null)
574 kv.Value.Dispose();
575 }
576
577 m_rgImages.Clear();
578 }
579
583 public string LayerName
584 {
585 get { return m_strLayerName; }
586 }
587
591 public int IterationN
592 {
593 get { return m_nIterN; }
594 }
595
599 public double StartSigma
600 {
601 get { return m_dfStartSigma; }
602 }
603
607 public double EndSigma
608 {
609 get { return m_dfEndSigma; }
610 }
611
615 public double StartStepSize
616 {
617 get { return m_dfStartStepSize; }
618 }
619
623 public double EndStepSize
624 {
625 get { return m_dfEndStepSize; }
626 }
627
631 public string UniqueName
632 {
633 get { return m_nIterN.ToString() + "_" + getCleanName(m_strLayerName); }
634 }
635
639 public Dictionary<int, Image> Images
640 {
641 get { return m_rgImages; }
642 set { m_rgImages = value; }
643 }
644
648 public bool Save
649 {
650 get { return m_bSave; }
651 }
652
657 {
658 get { return m_dfDetailPctToApply; }
659 set { m_dfDetailPctToApply = value; }
660 }
661
662 private string getCleanName(string str)
663 {
664 string strOut = "";
665
666 foreach (char ch in str)
667 {
668 if (!char.IsSymbol(ch) && ch != '\\' && ch != '/')
669 strOut += ch;
670 }
671
672 return strOut;
673 }
674
680 public RawProto ToProto(string strName)
681 {
682 RawProtoCollection rgChildren = new RawProtoCollection();
683
684 rgChildren.Add("layer", m_strLayerName, RawProto.TYPE.STRING);
685 rgChildren.Add("iterations", m_nIterN.ToString(), RawProto.TYPE.STRING);
686 rgChildren.Add("sigma_start", m_dfStartSigma.ToString(), RawProto.TYPE.STRING);
687 rgChildren.Add("sigma_end", m_dfEndSigma.ToString(), RawProto.TYPE.STRING);
688 rgChildren.Add("step_start", m_dfStartStepSize.ToString(), RawProto.TYPE.STRING);
689 rgChildren.Add("step_end", m_dfEndStepSize.ToString(), RawProto.TYPE.STRING);
690 rgChildren.Add("save", m_bSave.ToString(), RawProto.TYPE.STRING);
691 rgChildren.Add("pct_of_prev_detail", m_dfDetailPctToApply.ToString(), RawProto.TYPE.STRING);
692
693 return new RawProto(strName, "", rgChildren);
694 }
695
701 public static Octaves FromProto(RawProto rp)
702 {
703 string strVal;
704
705 string strLayer = "";
706 if ((strVal = rp.FindValue("layer")) != null)
707 strLayer = strVal;
708
709 int nIterations = 10;
710 if ((strVal = rp.FindValue("iterations")) != null)
711 nIterations = int.Parse(strVal);
712
713 double dfSigmaStart = 0;
714 if ((strVal = rp.FindValue("sigma_start")) != null)
715 dfSigmaStart = BaseParameter.ParseDouble(strVal);
716
717 double dfSigmaEnd = 0;
718 if ((strVal = rp.FindValue("sigma_end")) != null)
719 dfSigmaEnd = BaseParameter.ParseDouble(strVal);
720
721 double dfStepStart = 1.5;
722 if ((strVal = rp.FindValue("step_start")) != null)
723 dfStepStart = BaseParameter.ParseDouble(strVal);
724
725 double dfStepEnd = 1.5;
726 if ((strVal = rp.FindValue("step_end")) != null)
727 dfStepEnd = BaseParameter.ParseDouble(strVal);
728
729 bool bSave = false;
730 if ((strVal = rp.FindValue("save")) != null)
731 bSave = bool.Parse(strVal);
732
733 double dfPctOfDetail = .25;
734 if ((strVal = rp.FindValue("pct_of_prev_detail")) != null)
735 dfPctOfDetail = BaseParameter.ParseDouble(strVal);
736
737 return new Octaves(strLayer, nIterations, dfSigmaStart, dfSigmaEnd, dfStepStart, dfStepEnd, bSave, dfPctOfDetail);
738 }
739 }
740
744 public class OctavesCollection : IEnumerable<Octaves>
745 {
746 List<Octaves> m_rgOctaves = new List<Octaves>();
747
752 {
753 }
754
758 public int Count
759 {
760 get { return m_rgOctaves.Count; }
761 }
762
768 public Octaves this[int nIdx]
769 {
770 get { return m_rgOctaves[nIdx]; }
771 }
772
777 public void Add(Octaves o)
778 {
779 m_rgOctaves.Add(o);
780 }
781
787 public bool Remove(Octaves o)
788 {
789 return m_rgOctaves.Remove(o);
790 }
791
796 public void RemoveAt(int nIdx)
797 {
798 m_rgOctaves.RemoveAt(nIdx);
799 }
800
804 public void Clear()
805 {
806 m_rgOctaves.Clear();
807 }
808
813 public IEnumerator<Octaves> GetEnumerator()
814 {
815 return m_rgOctaves.GetEnumerator();
816 }
817
822 IEnumerator IEnumerable.GetEnumerator()
823 {
824 return m_rgOctaves.GetEnumerator();
825 }
826 }
827}
The BaseParameter class is the base class for all other parameter classes.
static double ParseDouble(string strVal)
Parse double values using the US culture if the decimal separator = '.', then using the native cultur...
The CancelEvent provides an extension to the manual cancel event that allows for overriding the manua...
Definition: CancelEvent.cs:17
bool WaitOne(int nMs=int.MaxValue)
Waits for the signal state to occur.
Definition: CancelEvent.cs:290
The Datum class is a simple wrapper to the SimpleDatum class to ensure compatibility with the origina...
Definition: Datum.cs:12
The ImageData class is a helper class used to convert between Datum, other raw data,...
Definition: ImageData.cs:14
static Bitmap GetImage(SimpleDatum d, ColorMapper clrMap=null, List< int > rgClrOrder=null)
Converts a SimplDatum (or Datum) into an image, optionally using a ColorMapper.
Definition: ImageData.cs:506
static Datum GetImageDataD(Bitmap bmp, int nChannels, bool bDataIsReal, int nLabel, bool bUseLockBitmap=true, int[] rgFocusMap=null)
The GetImageDataD function converts a Bitmap into a Datum using the double type for real data.
Definition: ImageData.cs:44
The ImageTools class is a helper class used to manipulate image data.
Definition: ImageTools.cs:16
static Bitmap AdjustContrast(Image bmp, float fBrightness=0.0f, float fContrast=1.0f, float fGamma=1.0f)
The AdjustContrast function adjusts the brightness, contrast and gamma of the image and returns the n...
Definition: ImageTools.cs:75
The Log class provides general output in text form.
Definition: Log.cs:13
void WriteLine(string str, bool bOverrideEnabled=false, bool bHeader=false, bool bError=false, bool bDisable=false)
Write a line of output.
Definition: Log.cs:80
double Progress
Get/set the progress associated with the Log.
Definition: Log.cs:147
The RawProtoCollection class is a list of RawProto objects.
void Add(RawProto p)
Adds a RawProto to the collection.
The RawProto class is used to parse and output Google prototxt file data.
Definition: RawProto.cs:17
TYPE
Defines the type of a RawProto node.
Definition: RawProto.cs:27
override string ToString()
Returns the RawProto as its full prototxt string.
Definition: RawProto.cs:681
static RawProto Parse(string str)
Parses a prototxt and places it in a new RawProto.
Definition: RawProto.cs:306
string FindValue(string strName)
Searches for a falue of a node within this nodes children.
Definition: RawProto.cs:105
RawProtoCollection FindChildren(params string[] rgstrName)
Searches for all children with a given name in this node's children.
Definition: RawProto.cs:263
The Utility class provides general utility funtions.
Definition: Utility.cs:35
static double[] ConvertVec(float[] rgf)
Convert an array of float to an array of generics.
Definition: Utility.cs:550
The Blob is the main holder of data that moves through the Layers of the Net.
Definition: Blob.cs:25
int channels
DEPRECIATED; legacy shape accessor channels: use shape(1) instead.
Definition: Blob.cs:800
void SetData(T[] rgData, int nCount=-1, bool bSetCount=true)
Sets a number of items within the Blob's data.
Definition: Blob.cs:1922
double min_data
Returns the minimum value in the data of the Blob.
Definition: Blob.cs:2499
double max_data
Returns the maximum value in the data of the Blob.
Definition: Blob.cs:2525
int height
DEPRECIATED; legacy shape accessor height: use shape(2) instead.
Definition: Blob.cs:808
Blob(CudaDnn< T > cuda, Log log, bool bIncludeDiff=true, bool bUseHalfSize=false)
The Blob constructor.
Definition: Blob.cs:64
long mutable_gpu_diff
Returns the diff GPU handle used by the CudaDnn connection.
Definition: Blob.cs:1555
long mutable_gpu_data
Returns the data GPU handle used by the CudaDnn connection.
Definition: Blob.cs:1487
T[] mutable_cpu_data
Get data from the GPU and bring it over to the host, or Set data from the Host and send it over to th...
Definition: Blob.cs:1461
T asum_diff()
Compute the sum of absolute values (L1 norm) of the diff.
Definition: Blob.cs:1718
Log Log
Returns the Log associated with the blob.
Definition: Blob.cs:394
void Reshape(int nNum, int nChannels, int nHeight, int nWidth, bool? bUseHalfSize=null)
DEPRECIATED; use
Definition: Blob.cs:442
Datum ToDatum()
Returns a new Datum that contains the shape and data of the Blob.
Definition: Blob.cs:2166
void CopyFrom(Blob< T > src, int nSrcOffset, int nDstOffset, int nCount, bool bCopyData, bool bCopyDiff)
Copy from a source Blob.
Definition: Blob.cs:903
int width
DEPRECIATED; legacy shape accessor width: use shape(3) instead.
Definition: Blob.cs:816
int count()
Returns the total number of items in the Blob.
Definition: Blob.cs:739
void ReshapeLike(Blob< T > b, bool? bUseHalfSize=null)
Reshape this Blob to have the same shape as another Blob.
Definition: Blob.cs:648
string Name
Get/set the name of the Blob.
Definition: Blob.cs:2184
CudaDnn< T > Cuda
Returns the CudaDnn object that manages the Blob's memory."/>
Definition: Blob.cs:386
long gpu_diff
Returns the diff GPU handle used by the CudaDnn connection.
Definition: Blob.cs:1541
virtual void Dispose(bool bDisposing)
Releases all resources used by the Blob (including both GPU and Host).
Definition: Blob.cs:402
void SetDiff(double dfVal, int nIdx=-1)
Either sets all of the diff items in the Blob to a given value, or alternatively only sets a single i...
Definition: Blob.cs:1981
long gpu_data
Returns the data GPU handle used by the CudaDnn connection.
Definition: Blob.cs:1479
The CudaDnn object is the main interface to the Low-Level Cuda C++ DLL.
Definition: CudaDnn.cs:969
void copy(int nCount, long hSrc, long hDst, int nSrcOffset=0, int nDstOffset=0, long hStream=-1, bool? bSrcHalfSizeOverride=null, bool? bDstHalfSizeOverride=null)
Copy data from one block of GPU memory to another.
Definition: CudaDnn.cs:6007
void scal(int n, double fAlpha, long hX, int nXOff=0)
Scales the data in X by a scaling factor.
Definition: CudaDnn.cs:6767
void sub(int n, long hA, long hB, long hY, int nAOff=0, int nBOff=0, int nYOff=0, int nB=0)
Subtracts B from A and places the result in Y.
Definition: CudaDnn.cs:7312
void add(int n, long hA, long hB, long hC, long hY)
Adds A, B and C and places the result in Y.
Definition: CudaDnn.cs:7209
void gaussian_blur(int n, int nChannels, int nHeight, int nWidth, double dfSigma, long hX, long hY)
The gaussian_blur runs a Gaussian blurring operation over each channel of the data using the sigma.
Definition: CudaDnn.cs:10980
Connects Layer's together into a direct acrylic graph (DAG) specified by a NetParameter
Definition: Net.cs:23
int layer_index_by_name(string strLayer)
Returns a Layer's index given its name.
Definition: Net.cs:2315
BlobCollection< T > Forward()
Run forward with the input Blob's already fed separately.
Definition: Net.cs:1445
void Backward(int nStart=int.MaxValue, int nEnd=0)
The network backward should take no input and output, since it solely computes the gradient w....
Definition: Net.cs:1499
Blob< T > blob_by_name(string strName, bool bThrowExceptionOnError=true)
Returns a blob given its name.
Definition: Net.cs:2245
Applies common transformations to the input data, such as scaling, mirroring, subtracting the image m...
void Transform(List< Datum > rgDatum, Blob< T > blobTransformed, CudaDnn< T > cuda, Log log)
Transforms a list of Datum and places the transformed data into a Blob.
TransformationParameter param
Returns the TransformationParameter used.
The DeepDraw class implements both deep drawing and deep dream as originally introduced by Google.
Definition: DeepDraw.cs:30
bool Render(Bitmap bmpInput, int nFocusLabel=-1, double dfDetailPercentageToOutput=0.25, string strOutputDir=null, bool bVisualizeEachStep=false, float[] rgDirectInputs=null)
Renders the deep draw image(s) depending on the Octave's installed.
Definition: DeepDraw.cs:213
Bitmap CreateRandomImage(Color clrBack, int nW=-1, int nH=-1, double dfScale=16.0)
Creates an image with random noise.
Definition: DeepDraw.cs:155
int Count
Returns the numberof Octaves installed.
Definition: DeepDraw.cs:100
void Add(string strLayer, int nIterations, double dfStartSigma, double dfEndSigma, double dfStartStep, double dfEndStep, bool bSaveFile=false, double dfPctDetailsToApply=0.25)
Adds a new Octave to run the deep drawing over.
Definition: DeepDraw.cs:115
void Add(Octaves octaves)
Add a new Octaves to the collection of Octaves to run.
Definition: DeepDraw.cs:124
static OctavesCollection ParseConfigurationString(string strConfig, out int nWd, out int nHt, out double dfOutputDetailPct, out string strSrcBlobName, out double dfRandomImageScale)
The ParseConfigurationString method parses a deep draw configuration string into the actual settings.
Definition: DeepDraw.cs:492
static string CreateConfigurationString(int nWd, int nHt, double dfOutputDetailPct, OctavesCollection colOctaves, string strSrcBlobName, double dfRandomImageScale)
The CreateConfigurationString function packs all deep draw settings into a configuration string.
Definition: DeepDraw.cs:461
IEnumerator< Octaves > GetEnumerator()
Returns the Octave enumerator.
Definition: DeepDraw.cs:133
static Bitmap CreateRandomImageEx(Color clrBack, int nW, int nH, double dfScale=16.0)
Creates an image with random noise.
Definition: DeepDraw.cs:174
static Bitmap AdjustContrast(Image bmp, float fBrightness=1.0f, float fContrast=1.0f, float fGamma=1.0f)
The AdjustContrast function adjusts the brightness, contrast and gamma of the image and returns the n...
Definition: DeepDraw.cs:445
void Dispose()
Releases all resources used by DeepDraw.
Definition: DeepDraw.cs:75
DeepDraw(CancelEvent evtCancel, Net< T > net, DataTransformer< T > transformer, string strSrcBlobName="data")
The constructor.
Definition: DeepDraw.cs:50
The OctavesCollection manages a list of Octaves.
Definition: DeepDraw.cs:745
bool Remove(Octaves o)
Removes an Octaves from the collection.
Definition: DeepDraw.cs:787
void Add(Octaves o)
Adds a new Octaves to the collection.
Definition: DeepDraw.cs:777
OctavesCollection()
The constructor.
Definition: DeepDraw.cs:751
IEnumerator< Octaves > GetEnumerator()
Returns the enumerator for the collection.
Definition: DeepDraw.cs:813
void Clear()
Removes all Octaves from the collection.
Definition: DeepDraw.cs:804
void RemoveAt(int nIdx)
Removes an Octaves at a given index in the collection.
Definition: DeepDraw.cs:796
int Count
The number of Octaves in the collection.
Definition: DeepDraw.cs:759
The Octave class defines the setting sused when images are generated.
Definition: DeepDraw.cs:532
static Octaves FromProto(RawProto rp)
The FromProto function parses a RawProto into a new Octaves.
Definition: DeepDraw.cs:701
int IterationN
Returns the number of iterations to run.
Definition: DeepDraw.cs:592
RawProto ToProto(string strName)
The ToProto function converts the Octaves settings into a RawProto.
Definition: DeepDraw.cs:680
double EndSigma
Returns the ending sigma used during gaussian blurring.
Definition: DeepDraw.cs:608
double EndStepSize
Returns the ending step.
Definition: DeepDraw.cs:624
string LayerName
Returns the 'end' target network layer.
Definition: DeepDraw.cs:584
double StartSigma
Returns the starting sigma used during gaussian blurring.
Definition: DeepDraw.cs:600
double StartStepSize
Returns the starting step.
Definition: DeepDraw.cs:616
Octaves(string strLayerName, int nIterN, double dfStartSigma, double dfEndSigma, double dfStartStepSize, double dfEndStepSize, bool bSaveFile=false, double dfPctDetailsToApply=0.25)
Specifies the constructor.
Definition: DeepDraw.cs:554
bool Save
Returns whether or not to save the Octave final image to disk.
Definition: DeepDraw.cs:649
double PercentageOfPreviousOctaveDetailsToApply
Get/set the percentage of detail from the previous Octave to apply to the source for this Octave.
Definition: DeepDraw.cs:657
Dictionary< int, Image > Images
Returns the images generated for the Octave, ordered by lable, where -1 = all.
Definition: DeepDraw.cs:640
string UniqueName
Returns a unique name of the Octave.
Definition: DeepDraw.cs:632
void Dispose()
Releases all resources used.
Definition: DeepDraw.cs:569
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
Definition: Annotation.cs:12
The MyCaffe.common namespace contains common MyCaffe classes.
Definition: BatchInput.cs:8
The MyCaffe.data namespace contains dataset creators used to create common testing datasets such as M...
Definition: BinaryFile.cs:16
The MyCaffe.extras namespace contains classes that use the MyCaffe and other namespaces to add enhanc...
Definition: DeepDraw.cs:17
The MyCaffe namespace contains the main body of MyCaffe code that closesly tracks the C++ Caffe open-...
Definition: Annotation.cs:12