MyCaffe  1.12.0.60
Deep learning software for Windows C# programmers.
SimpleDatum.cs
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.Diagnostics;
5using System.Drawing;
6using System.IO;
7using System.Linq;
8using System.Runtime.InteropServices;
9using System.Text;
10using System.Threading;
11
12namespace MyCaffe.basecode
13{
17 [Serializable]
18 public class SimpleDatum
19 {
20 int m_nIndex = 0;
21 int m_nOriginalLabel = 0;
22 int m_nLabel = 0;
23 int m_nHeight = 0;
24 int m_nWidth = 0;
25 int m_nChannels = 0;
26 byte[] m_rgByteData = null;
27 float[] m_rgRealDataF = null;
28 double[] m_rgRealDataD = null;
29 bool m_bIsRealData = false;
30 DateTime m_dt = DateTime.MinValue;
31 int m_nOriginalBoost = 0;
32 int m_nBoost = 0;
33 bool m_bAutoLabeled = false;
34 int m_nImageID = 0;
35 int m_nVirtualID = 0;
36 int m_nGroupID = 0;
37 byte[] m_rgDataCriteria = null;
38 DATA_FORMAT m_dataCriteriaFormat = DATA_FORMAT.NONE;
39 byte[] m_rgDebugData = null;
40 DATA_FORMAT m_debugDataFormat = DATA_FORMAT.NONE;
41 string m_strDesc = null;
42 int m_nSourceID = 0;
43 int m_nOriginalSourceID = 0;
44 int m_nHitCount = 0;
45 ANNOTATION_TYPE m_nAnnotationType = ANNOTATION_TYPE.NONE;
46 AnnotationGroupCollection m_rgAnnotationGroup = null;
50 protected object m_tag = null;
54 protected string m_strTagName = null;
55
59 public enum ANNOTATION_TYPE
60 {
64 NONE = -1,
68 BBOX = 0
69 }
70
74 public enum DATA_FORMAT
75 {
79 NONE,
83 IMAGE_1CH,
87 IMAGE_3CH,
91 SEGMENTATION,
95 DICTIONARY,
99 CUSTOM,
104 BITMAP,
108 LIST_DOUBLE,
112 LIST_FLOAT,
116 ANNOTATION_DATA
117 }
118
119
123 public SimpleDatum()
124 {
125 }
126
143 public SimpleDatum(bool bIsReal, int nChannels, int nWidth, int nHeight, int nLabel = -1, DateTime? dtTime = null, int nBoost = 0, bool bAutoLabeled = false, int nIdx = -1, int nVirtualID = 0, int nImageID = 0, int nSourceID = 0, int nOriginalSourceID = 0)
144 {
145 m_nChannels = nChannels;
146 m_nWidth = nWidth;
147 m_nHeight = nHeight;
148 m_nOriginalLabel = nLabel;
149 m_nLabel = nLabel;
150 m_dt = dtTime.GetValueOrDefault(DateTime.MinValue);
151 m_nOriginalBoost = nBoost;
152 m_nBoost = nBoost;
153 m_bAutoLabeled = bAutoLabeled;
154 m_nVirtualID = nVirtualID;
155 m_bIsRealData = bIsReal;
156 m_nIndex = nIdx;
157 m_nImageID = nImageID;
158 m_nSourceID = nSourceID;
159 m_nOriginalSourceID = nOriginalSourceID;
160 }
161
179 public SimpleDatum(bool bIsReal, int nChannels, int nWidth, int nHeight, int nLabel, DateTime dtTime, List<byte> rgData, int nBoost, bool bAutoLabeled, int nIdx, int nVirtualID = 0, int nImageID = 0, int nSourceID = 0, int nOriginalSourceID = 0)
180 {
181 m_nChannels = nChannels;
182 m_nWidth = nWidth;
183 m_nHeight = nHeight;
184 m_nOriginalLabel = nLabel;
185 m_nLabel = nLabel;
186 m_dt = dtTime;
187 m_nOriginalBoost = nBoost;
188 m_nBoost = nBoost;
189 m_bAutoLabeled = bAutoLabeled;
190 m_nVirtualID = nVirtualID;
191 m_bIsRealData = bIsReal;
192 m_nIndex = nIdx;
193 m_nImageID = nImageID;
194 m_nSourceID = nSourceID;
195 m_nOriginalSourceID = nOriginalSourceID;
196
197 if (bIsReal)
198 throw new ArgumentException("The data sent is not real, but the bIsReal is set to true!");
199
200 m_rgByteData = rgData.ToArray();
201 }
202
220 public SimpleDatum(bool bIsReal, int nChannels, int nWidth, int nHeight, int nLabel, DateTime dtTime, List<double> rgfData, int nBoost, bool bAutoLabeled, int nIdx, int nVirtualID = 0, int nImageID = 0, int nSourceID = 0, int nOriginalSourceID = 0)
221 {
222 m_nChannels = nChannels;
223 m_nWidth = nWidth;
224 m_nHeight = nHeight;
225 m_nOriginalLabel = nLabel;
226 m_nLabel = nLabel;
227 m_dt = dtTime;
228 m_nOriginalBoost = nBoost;
229 m_nBoost = nBoost;
230 m_bAutoLabeled = bAutoLabeled;
231 m_nVirtualID = nVirtualID;
232 m_bIsRealData = bIsReal;
233 m_nIndex = nIdx;
234 m_nImageID = nImageID;
235 m_nSourceID = nSourceID;
236 m_nOriginalSourceID = nOriginalSourceID;
237
238 if (!bIsReal)
239 throw new ArgumentException("The data sent is real, but the bIsReal is set to false!");
240
241 m_rgRealDataD = rgfData.ToArray();
242 }
243
261 public SimpleDatum(bool bIsReal, int nChannels, int nWidth, int nHeight, int nLabel, DateTime dtTime, List<float> rgfData, int nBoost, bool bAutoLabeled, int nIdx, int nVirtualID = 0, int nImageID = 0, int nSourceID = 0, int nOriginalSourceID = 0)
262 {
263 m_nChannels = nChannels;
264 m_nWidth = nWidth;
265 m_nHeight = nHeight;
266 m_nOriginalLabel = nLabel;
267 m_nLabel = nLabel;
268 m_dt = dtTime;
269 m_nOriginalBoost = nBoost;
270 m_nBoost = nBoost;
271 m_bAutoLabeled = bAutoLabeled;
272 m_nVirtualID = nVirtualID;
273 m_bIsRealData = bIsReal;
274 m_nIndex = nIdx;
275 m_nImageID = nImageID;
276 m_nSourceID = nSourceID;
277 m_nOriginalSourceID = nOriginalSourceID;
278
279 if (!bIsReal)
280 throw new ArgumentException("The data sent is real, but the bIsReal is set to false!");
281
282 m_rgRealDataF = rgfData.ToArray();
283 }
284
302 public SimpleDatum(bool bIsReal, int nChannels, int nWidth, int nHeight, int nLabel, DateTime dtTime, byte[] rgData, int nBoost, bool bAutoLabeled, int nIdx, int nVirtualID = 0, int nImageID = 0, int nSourceID = 0, int nOriginalSourceID = 0)
303 {
304 m_nChannels = nChannels;
305 m_nWidth = nWidth;
306 m_nHeight = nHeight;
307 m_nOriginalLabel = nLabel;
308 m_nLabel = nLabel;
309 m_dt = dtTime;
310 m_nOriginalBoost = nBoost;
311 m_nBoost = nBoost;
312 m_bAutoLabeled = bAutoLabeled;
313 m_nVirtualID = nVirtualID;
314 m_bIsRealData = bIsReal;
315 m_nIndex = nIdx;
316 m_nImageID = nImageID;
317 m_nSourceID = nSourceID;
318 m_nOriginalSourceID = nOriginalSourceID;
319
320 if (bIsReal)
321 throw new ArgumentException("The data sent is not real, but the bIsReal is set to true!");
322
323 m_rgByteData = rgData;
324 }
325
343 public SimpleDatum(bool bIsReal, int nChannels, int nWidth, int nHeight, int nLabel, DateTime dtTime, double[] rgdfData, int nBoost, bool bAutoLabeled, int nIdx, int nVirtualID = 0, int nImageID = 0, int nSourceID = 0, int nOriginalSourceID = 0)
344 {
345 m_nChannels = nChannels;
346 m_nWidth = nWidth;
347 m_nHeight = nHeight;
348 m_nOriginalLabel = nLabel;
349 m_nLabel = nLabel;
350 m_dt = dtTime;
351 m_nOriginalBoost = nBoost;
352 m_nBoost = nBoost;
353 m_bAutoLabeled = bAutoLabeled;
354 m_nVirtualID = nVirtualID;
355 m_bIsRealData = bIsReal;
356 m_nIndex = nIdx;
357 m_nImageID = nImageID;
358 m_nSourceID = nSourceID;
359 m_nOriginalSourceID = nOriginalSourceID;
360
361 if (!bIsReal)
362 throw new ArgumentException("The data sent is real, but the bIsReal is set to false!");
363
364 m_rgRealDataD = rgdfData;
365 }
366
384 public SimpleDatum(bool bIsReal, int nChannels, int nWidth, int nHeight, int nLabel, DateTime dtTime, float[] rgfData, int nBoost, bool bAutoLabeled, int nIdx, int nVirtualID = 0, int nImageID = 0, int nSourceID = 0, int nOriginalSourceID = 0)
385 {
386 m_nChannels = nChannels;
387 m_nWidth = nWidth;
388 m_nHeight = nHeight;
389 m_nOriginalLabel = nLabel;
390 m_nLabel = nLabel;
391 m_dt = dtTime;
392 m_nOriginalBoost = nBoost;
393 m_nBoost = nBoost;
394 m_bAutoLabeled = bAutoLabeled;
395 m_nVirtualID = nVirtualID;
396 m_bIsRealData = bIsReal;
397 m_nIndex = nIdx;
398 m_nImageID = nImageID;
399 m_nSourceID = nSourceID;
400 m_nOriginalSourceID = nOriginalSourceID;
401
402 if (!bIsReal)
403 throw new ArgumentException("The data sent is real, but the bIsReal is set to false!");
404
405 m_rgRealDataF = rgfData;
406 }
407
424 public SimpleDatum(bool bIsReal, int nChannels, int nWidth, int nHeight, int nLabel, DateTime dtTime, int nBoost = 0, bool bAutoLabeled = false, int nIdx = -1, int nVirtualID = 0, int nImageID = 0, int nSourceID = 0, int nOriginalSourceID = 0)
425 {
426 m_nChannels = nChannels;
427 m_nWidth = nWidth;
428 m_nHeight = nHeight;
429 m_nOriginalLabel = nLabel;
430 m_nLabel = nLabel;
431 m_dt = dtTime;
432 m_nOriginalBoost = nBoost;
433 m_nBoost = nBoost;
434 m_bAutoLabeled = bAutoLabeled;
435 m_nVirtualID = nVirtualID;
436 m_bIsRealData = bIsReal;
437 m_nIndex = nIdx;
438 m_nImageID = nImageID;
439 m_nSourceID = nSourceID;
440 m_nOriginalSourceID = nOriginalSourceID;
441 m_rgByteData = null;
442 m_rgRealDataD = null;
443 m_rgRealDataF = null;
444 }
445
456 public SimpleDatum(int nChannels, int nWidth, int nHeight, float[] rgf, int nOffset, int nCount, bool bDataIsReal = true)
457 {
458 m_nChannels = nChannels;
459 m_nWidth = nWidth;
460 m_nHeight = nHeight;
461 m_nOriginalLabel = 0;
462 m_nOriginalBoost = 0;
463 m_dt = DateTime.MinValue;
464 m_bAutoLabeled = false;
465 m_nVirtualID = 0;
466 m_nImageID = 0;
467 m_nImageID = 0;
468 m_nSourceID = 0;
469 m_nOriginalSourceID = 0;
470 m_rgByteData = null;
471
472 int nLen = nChannels * nWidth * nHeight;
473 if (nLen != nCount)
474 throw new Exception("The channel x width x height should equal the count!");
475
476 if (bDataIsReal)
477 {
478 m_bIsRealData = true;
479 m_rgRealDataF = new float[nCount];
480
481 if (rgf != null)
482 Array.Copy(rgf, nOffset, m_rgRealDataF, 0, nCount);
483 }
484 else
485 {
486 m_bIsRealData = false;
487 m_rgByteData = new byte[nCount];
488
489 for (int i = 0; i < nCount; i++)
490 {
491 m_rgByteData[i] = Math.Min(Math.Max((byte)rgf[nOffset + i], (byte)0), (byte)255);
492 }
493 }
494 }
495
502 public SimpleDatum(int nChannels, int nWidth, int nHeight)
503 {
504 m_nChannels = nChannels;
505 m_nWidth = nWidth;
506 m_nHeight = nHeight;
507 m_nOriginalLabel = 0;
508 m_nOriginalBoost = 0;
509 m_dt = DateTime.MinValue;
510 m_bAutoLabeled = false;
511 m_nVirtualID = 0;
512 m_nImageID = 0;
513 m_nImageID = 0;
514 m_nSourceID = 0;
515 m_nOriginalSourceID = 0;
516 m_rgByteData = null;
517 }
518
523 public SimpleDatum(Bytemap data)
524 {
525 m_nChannels = data.Channels;
526 m_nWidth = data.Width;
527 m_nHeight = data.Height;
528 m_nOriginalLabel = -1;
529 m_nLabel = -1;
530 m_bIsRealData = false;
531
532 m_rgByteData = data.Bytes;
533 }
534
540 {
541 m_nChannels = data.Channels;
542 m_nWidth = data.Width;
543 m_nHeight = data.Height;
544 m_nOriginalLabel = -1;
545 m_nLabel = -1;
546 m_bIsRealData = true;
547
548 m_rgRealDataD = data.Values;
549 }
550
556 public SimpleDatum(SimpleDatum d, bool bCopyData = false)
557 {
558 Copy(d, bCopyData);
559 }
560
567 public SimpleDatum(SimpleDatum d, int nHeight, int nWidth)
568 {
569 Copy(d, false, nHeight, nWidth);
570 }
571
589 public SimpleDatum(List<SimpleDatum> rg, bool bAlignChannels)
590 {
591 if (rg.Count == 1)
592 {
593 Copy(rg[0], true);
594 return;
595 }
596
597 Copy(rg[0], false);
598
599 m_nChannels *= rg.Count;
600
601 if (bAlignChannels)
602 {
603 if (m_nChannels != rg.Count)
604 throw new Exception("Currently channel alignment is only allowed on single channel data.");
605
606 if (rg.Count >= 1)
607 {
608 if (rg[0].IsRealData)
609 {
610 if (rg[0].RealDataD != null)
611 {
612 double[] rgData = new double[m_nChannels * Height * Width];
613
614 for (int h = 0; h < Height; h++)
615 {
616 for (int w = 0; w < Width; w++)
617 {
618 int nIdxSrc = (h * Width) + w;
619 int nIdxDst = nIdxSrc * m_nChannels;
620
621 for (int c = 0; c < m_nChannels; c++)
622 {
623 rgData[nIdxDst + c] = rg[c].RealDataD[nIdxSrc];
624 }
625 }
626 }
627
628 m_rgRealDataD = rgData;
629 }
630 else if (rg[0].RealDataF != null)
631 {
632 float[] rgData = new float[m_nChannels * Height * Width];
633
634 for (int h = 0; h < Height; h++)
635 {
636 for (int w = 0; w < Width; w++)
637 {
638 int nIdxSrc = (h * Width) + w;
639 int nIdxDst = nIdxSrc * m_nChannels;
640
641 for (int c = 0; c < m_nChannels; c++)
642 {
643 rgData[nIdxDst + c] = rg[c].RealDataF[nIdxSrc];
644 }
645 }
646 }
647
648 m_rgRealDataF = rgData;
649 }
650 else
651 {
652 throw new Exception("SimpleDatum: Both the RealDataD and RealDataF are null!");
653 }
654 m_rgByteData = null;
655 }
656 else
657 {
658 byte[] rgData = new byte[m_nChannels * Height * Width];
659
660 for (int h = 0; h < Height; h++)
661 {
662 for (int w = 0; w < Width; w++)
663 {
664 int nIdxSrc = (h * Width) + w;
665 int nIdxDst = nIdxSrc * m_nChannels;
666
667 for (int c = 0; c < m_nChannels; c++)
668 {
669 rgData[nIdxDst + c] = rg[c].ByteData[nIdxSrc];
670 }
671 }
672 }
673
674 m_rgByteData = rgData;
675 m_rgRealDataF = null;
676 m_rgRealDataD = null;
677 }
678 }
679 }
680 else
681 {
682 bool bReal = rg[0].IsRealData;
683 int nDstIdx = 0;
684 int nItemCount = m_nChannels / rg.Count;
685
686 if (bReal)
687 {
688 if (rg[0].RealDataD != null)
689 {
690 m_rgRealDataD = new double[m_nChannels * Height * Width];
691 m_rgByteData = null;
692
693 for (int i = 0; i < rg.Count; i++)
694 {
695 Array.Copy(rg[i].RealDataD, 0, m_rgRealDataD, nDstIdx, nItemCount);
696 nDstIdx += nItemCount;
697 }
698 }
699 else if (rg[0].RealDataF != null)
700 {
701 m_rgRealDataF = new float[m_nChannels * Height * Width];
702 m_rgByteData = null;
703
704 for (int i = 0; i < rg.Count; i++)
705 {
706 Array.Copy(rg[i].RealDataF, 0, m_rgRealDataF, nDstIdx, nItemCount);
707 nDstIdx += nItemCount;
708 }
709 }
710 else
711 {
712 throw new Exception("SimpleDatum: Both the RealDataD and RealDataF are null!");
713 }
714 }
715 else
716 {
717 m_rgByteData = new byte[m_nChannels * Height * Width];
718 m_rgRealDataD = null;
719 m_rgRealDataF = null;
720
721 for (int i = 0; i < rg.Count; i++)
722 {
723 Array.Copy(rg[i].ByteData, 0, m_rgByteData, nDstIdx, nItemCount);
724 nDstIdx += nItemCount;
725 }
726 }
727 }
728 }
729
733 public int HitCount
734 {
735 get { return m_nHitCount; }
736 set { m_nHitCount = value; }
737 }
738
742 public object Tag
743 {
744 get { return m_tag; }
745 set { m_tag = value; }
746 }
747
751 public string TagName
752 {
753 get { return m_strTagName; }
754 set { m_strTagName = value; }
755 }
756
760 public double Min
761 {
762 get
763 {
764 if (m_bIsRealData)
765 {
766 if (m_rgRealDataD != null)
767 return m_rgRealDataD.Min(p => p);
768 else if (m_rgRealDataF != null)
769 return m_rgRealDataF.Min(p => p);
770 else
771 throw new Exception("SimpleDatum: Both the RealDataD and RealDataF are null!");
772 }
773 else
774 {
775 if (m_rgByteData != null)
776 return (double)m_rgByteData.Min(p => p);
777 }
778
779 return double.NaN;
780 }
781 }
782
786 public double Max
787 {
788 get
789 {
790 if (m_bIsRealData)
791 {
792 if (m_rgRealDataD != null)
793 return m_rgRealDataD.Max(p => p);
794 else if (m_rgRealDataF != null)
795 return m_rgRealDataF.Max(p => p);
796 else
797 throw new Exception("SimpleDatum: Both the RealDataD and RealDataF are null!");
798 }
799 else
800 {
801 if (m_rgByteData != null)
802 return (double)m_rgByteData.Max(p => p);
803 }
804
805 return double.NaN;
806 }
807 }
808
816 public void Clip(int nDataLen, int? nNewChannel, int? nNewHeight, int? nNewWidth)
817 {
818 if (m_rgByteData != null && m_rgByteData.Length > nDataLen)
819 {
820 byte[] rgData = new byte[nDataLen];
821 Array.Copy(m_rgByteData, rgData, nDataLen);
822 m_rgByteData = rgData;
823 }
824 if (m_rgRealDataD != null && m_rgRealDataD.Length > nDataLen)
825 {
826 double[] rgData = new double[nDataLen];
827 Array.Copy(m_rgRealDataD, rgData, nDataLen);
828 m_rgRealDataD = rgData;
829 }
830 if (m_rgRealDataF != null && m_rgRealDataF.Length > nDataLen)
831 {
832 float[] rgData = new float[nDataLen];
833 Array.Copy(m_rgRealDataF, rgData, nDataLen);
834 m_rgRealDataF = rgData;
835 }
836
837 m_nChannels = nNewChannel.GetValueOrDefault(m_nChannels);
838 m_nHeight = nNewHeight.GetValueOrDefault(m_nHeight);
839 m_nWidth = nNewWidth.GetValueOrDefault(m_nWidth);
840 }
841
846 public List<int> GetNonZeroIndexes()
847 {
848 List<int> rgIdx = new List<int>();
849
850 if (m_bIsRealData)
851 {
852 if (m_rgRealDataD != null)
853 {
854 for (int i = 0; i < m_rgRealDataD.Length; i++)
855 {
856 if (m_rgRealDataD[i] != 0)
857 rgIdx.Add(i);
858 }
859 }
860 else if (m_rgRealDataF != null)
861 {
862 for (int i = 0; i < m_rgRealDataF.Length; i++)
863 {
864 if (m_rgRealDataF[i] != 0)
865 rgIdx.Add(i);
866 }
867 }
868 else
869 {
870 throw new Exception("SimpleDatum: Both the RealDataD and RealDataF are null!");
871 }
872 }
873 else
874 {
875 for (int i = 0; i < m_rgByteData.Length; i++)
876 {
877 if (m_rgByteData[i] != 0)
878 rgIdx.Add(i);
879 }
880 }
881
882 return rgIdx;
883 }
884
888 public void Zero()
889 {
890 if (m_rgByteData != null)
891 Array.Clear(m_rgByteData, 0, m_rgByteData.Length);
892
893 if (m_rgRealDataD != null)
894 Array.Clear(m_rgRealDataD, 0, m_rgRealDataD.Length);
895
896 if (m_rgRealDataF != null)
897 Array.Clear(m_rgRealDataF, 0, m_rgRealDataF.Length);
898 }
899
906 public bool Sub(SimpleDatum sd, bool bSetNegativeToZero = false)
907 {
908 bool bDifferent = false;
909
910 if (sd.ItemCount != ItemCount)
911 throw new Exception("Both simple datums must have the same number of elements!");
912
913 if (m_rgByteData != null)
914 {
915 for (int i = 0; i < m_rgByteData.Length; i++)
916 {
917 int nVal = 0;
918
919 if (sd.m_rgByteData != null)
920 nVal = m_rgByteData[i] - sd.m_rgByteData[i];
921 else if (sd.m_rgRealDataD != null)
922 nVal = (int)m_rgByteData[i] - (int)sd.m_rgRealDataD[i];
923 else if (sd.m_rgRealDataF != null)
924 nVal = (int)m_rgByteData[i] - (int)sd.m_rgRealDataF[i];
925
926 if (nVal != 0)
927 {
928 bDifferent = true;
929
930 if (bSetNegativeToZero && nVal < 0)
931 nVal = 0;
932 }
933
934 m_rgByteData[i] = (byte)nVal;
935 }
936 }
937
938 if (m_rgRealDataD != null)
939 {
940 for (int i = 0; i < m_rgRealDataD.Length; i++)
941 {
942 if (sd.m_rgRealDataD != null)
943 m_rgRealDataD[i] -= sd.m_rgRealDataD[i];
944 else if (sd.m_rgRealDataF != null)
945 m_rgRealDataD[i] -= sd.m_rgRealDataF[i];
946 else if (sd.m_rgByteData != null)
947 m_rgRealDataD[i] -= sd.m_rgByteData[i];
948
949 if (m_rgRealDataD[i] != 0)
950 {
951 bDifferent = true;
952
953 if (bSetNegativeToZero && m_rgRealDataD[i] < 0)
954 m_rgRealDataD[i] = 0;
955 }
956 }
957 }
958
959 if (m_rgRealDataF != null)
960 {
961 for (int i = 0; i < m_rgRealDataF.Length; i++)
962 {
963 if (sd.m_rgRealDataD != null)
964 m_rgRealDataF[i] -= (float)sd.m_rgRealDataD[i];
965 else if (sd.m_rgRealDataF != null)
966 m_rgRealDataF[i] -= sd.m_rgRealDataF[i];
967 else if (sd.m_rgByteData != null)
968 m_rgRealDataF[i] -= sd.m_rgByteData[i];
969
970 if (m_rgRealDataF[i] != 0)
971 {
972 bDifferent = true;
973
974 if (bSetNegativeToZero && m_rgRealDataF[i] < 0)
975 m_rgRealDataF[i] = 0;
976 }
977 }
978 }
979
980 return bDifferent;
981 }
982
988 public bool SubAbs(SimpleDatum sd)
989 {
990 bool bDifferent = false;
991
992 if (sd.ItemCount != ItemCount)
993 throw new Exception("Both simple datums must have the same number of elements!");
994
995 if (m_rgByteData != null)
996 {
997 if (sd.m_rgByteData == null)
998 throw new Exception("Both simple datums must have the same type of data!");
999
1000 for (int i = 0; i < m_rgByteData.Length; i++)
1001 {
1002 m_rgByteData[i] = (byte)Math.Abs(m_rgByteData[i] - sd.m_rgByteData[i]);
1003 if (m_rgByteData[i] != 0)
1004 bDifferent = true;
1005 }
1006 }
1007
1008 if (m_rgRealDataD != null)
1009 {
1010 if (sd.m_rgRealDataD == null)
1011 throw new Exception("Both simple datums must have the same type of data!");
1012
1013 for (int i = 0; i < m_rgRealDataD.Length; i++)
1014 {
1015 m_rgRealDataD[i] = Math.Abs(m_rgRealDataD[i] - sd.m_rgRealDataD[i]);
1016 if (m_rgRealDataD[i] != 0)
1017 bDifferent = true;
1018 }
1019 }
1020
1021 if (m_rgRealDataF != null)
1022 {
1023 if (sd.m_rgRealDataF == null)
1024 throw new Exception("Both simple datums must have the same type of data!");
1025
1026 for (int i = 0; i < m_rgRealDataF.Length; i++)
1027 {
1028 m_rgRealDataF[i] = Math.Abs(m_rgRealDataF[i] - sd.m_rgRealDataF[i]);
1029 if (m_rgRealDataF[i] != 0)
1030 bDifferent = true;
1031 }
1032 }
1033
1034 return bDifferent;
1035 }
1036
1043 public void Scale(double dfScale, double dfMin = -double.MaxValue, double dfMax = double.MaxValue)
1044 {
1045 if (m_rgByteData != null)
1046 {
1047 for (int i = 0; i < m_rgByteData.Length; i++)
1048 {
1049 int nVal = (int)(m_rgByteData[i] * dfScale);
1050 if (nVal < (int)dfMin)
1051 nVal = (int)dfMin;
1052 else if (nVal > (int)dfMax)
1053 nVal = (int)dfMax;
1054 m_rgByteData[i] = (byte)nVal;
1055 }
1056 }
1057
1058 if (m_rgRealDataD != null)
1059 {
1060 for (int i = 0; i < m_rgRealDataD.Length; i++)
1061 {
1062 m_rgRealDataD[i] *= dfScale;
1063 if (m_rgRealDataD[i] < dfMin)
1064 m_rgRealDataD[i] = dfMin;
1065 else if (m_rgRealDataD[i] > dfMax)
1066 m_rgRealDataD[i] = dfMax;
1067 }
1068 }
1069
1070 if (m_rgRealDataF != null)
1071 {
1072 for (int i = 0; i < m_rgRealDataF.Length; i++)
1073 {
1074 m_rgRealDataF[i] *= (float)dfScale;
1075 if (m_rgRealDataF[i] < dfMin)
1076 m_rgRealDataF[i] = (float)dfMin;
1077 else if (m_rgRealDataF[i] > dfMax)
1078 m_rgRealDataF[i] = (float)dfMax;
1079 }
1080 }
1081 }
1082
1089 public void ScalePow(double dfScalePow, double dfMin = -double.MaxValue, double dfMax = double.MaxValue)
1090 {
1091 if (m_rgByteData != null)
1092 {
1093 for (int i = 0; i < m_rgByteData.Length; i++)
1094 {
1095 int nVal = (int)((double)Math.Pow(m_rgByteData[i], dfScalePow));
1096 if (nVal < (int)dfMin)
1097 nVal = (int)dfMin;
1098 else if (nVal > (int)dfMax)
1099 nVal = (int)dfMax;
1100 m_rgByteData[i] = (byte)nVal;
1101 }
1102 }
1103
1104 if (m_rgRealDataD != null)
1105 {
1106 for (int i = 0; i < m_rgRealDataD.Length; i++)
1107 {
1108 m_rgRealDataD[i] = Math.Pow(m_rgRealDataD[i], dfScalePow);
1109 if (m_rgRealDataD[i] < dfMin)
1110 m_rgRealDataD[i] = dfMin;
1111 else if (m_rgRealDataD[i] > dfMax)
1112 m_rgRealDataD[i] = dfMax;
1113 }
1114 }
1115
1116 if (m_rgRealDataF != null)
1117 {
1118 for (int i = 0; i < m_rgRealDataF.Length; i++)
1119 {
1120 m_rgRealDataF[i] = (float)(Math.Pow(m_rgRealDataF[i], dfScalePow));
1121 if (m_rgRealDataF[i] < dfMin)
1122 m_rgRealDataF[i] = (float)dfMin;
1123 else if (m_rgRealDataF[i] > dfMax)
1124 m_rgRealDataF[i] = (float)dfMax;
1125 }
1126 }
1127 }
1128
1136 public void Copy(SimpleDatum d, bool bCopyData, int? nHeight = null, int? nWidth = null)
1137 {
1138 m_bIsRealData = d.m_bIsRealData;
1139 m_nLabel = d.m_nLabel;
1140 m_nOriginalLabel = d.m_nOriginalLabel;
1141 m_nChannels = d.m_nChannels;
1142 m_nHeight = nHeight.GetValueOrDefault(d.m_nHeight);
1143 m_nWidth = nWidth.GetValueOrDefault(d.m_nWidth);
1144
1145 if (bCopyData)
1146 {
1147 m_rgRealDataD = Utility.Clone<double>(d.m_rgRealDataD);
1148 m_rgRealDataF = Utility.Clone<float>(d.m_rgRealDataF);
1149 m_rgByteData = Utility.Clone<byte>(d.m_rgByteData);
1150 }
1151 else
1152 {
1153 m_rgRealDataD = d.m_rgRealDataD;
1154 m_rgRealDataF = d.m_rgRealDataF;
1155 m_rgByteData = d.m_rgByteData;
1156 }
1157
1158 m_dt = d.m_dt;
1159 m_nOriginalBoost = d.m_nOriginalBoost;
1160 m_nBoost = d.m_nBoost;
1161 m_bAutoLabeled = d.m_bAutoLabeled;
1162 m_nImageID = d.m_nImageID;
1163 m_nVirtualID = d.m_nVirtualID;
1164 m_nGroupID = d.m_nGroupID;
1165 m_nIndex = d.m_nIndex;
1166 m_strDesc = d.m_strDesc;
1167 m_rgDataCriteria = d.m_rgDataCriteria;
1168 m_dataCriteriaFormat = d.m_dataCriteriaFormat;
1169 m_rgDebugData = d.m_rgDebugData;
1170 m_debugDataFormat = d.m_debugDataFormat;
1171 m_nSourceID = d.m_nSourceID;
1172 m_nOriginalSourceID = d.m_nOriginalSourceID;
1173 m_tag = d.m_tag;
1175
1176 m_nAnnotationType = d.m_nAnnotationType;
1177 m_rgAnnotationGroup = null;
1178
1179 if (d.m_rgAnnotationGroup != null)
1180 {
1181 m_rgAnnotationGroup = new AnnotationGroupCollection();
1182
1183 foreach (AnnotationGroup g in d.m_rgAnnotationGroup)
1184 {
1185 m_rgAnnotationGroup.Add(g.Clone());
1186 }
1187 }
1188 }
1189
1194 public void CopyData(SimpleDatum d)
1195 {
1196 m_nHeight = d.m_nHeight;
1197 m_nWidth = d.m_nWidth;
1198 m_nChannels = d.m_nChannels;
1199 m_rgByteData = d.m_rgByteData;
1200 m_bIsRealData = d.m_bIsRealData;
1201 m_rgRealDataD = d.m_rgRealDataD;
1202 m_rgRealDataF = d.m_rgRealDataF;
1203 }
1204
1209 public void SetData(SimpleDatum d)
1210 {
1211 if (d.IsRealData)
1212 {
1213 if (d.RealDataD != null)
1214 SetData(d.RealDataD.ToList(), d.Label);
1215 else if (d.RealDataF != null)
1216 SetData(d.RealDataF.ToList(), d.Label);
1217 else
1218 throw new Exception("SimpleDatum: Both the RealDataD and RealDataF are null!");
1219 }
1220 else
1221 SetData(d.ByteData.ToList(), d.Label);
1222 }
1223
1228 public bool GetDataValid(bool bByType = true)
1229 {
1230 if (bByType)
1231 {
1232 if (m_bIsRealData && m_rgRealDataD == null && m_rgRealDataF == null)
1233 return false;
1234 else if (!m_bIsRealData && m_rgByteData == null)
1235 return false;
1236
1237 return true;
1238 }
1239
1240 if (m_rgRealDataD == null && m_rgRealDataF == null && m_rgByteData == null)
1241 return false;
1242
1243 return true;
1244 }
1245
1252 public List<T> ClipToLastColumnsX<T>(int nLastColumns = 10)
1253 {
1254 int nC = m_nChannels;
1255 int nW = m_nWidth;
1256 int nH = m_nHeight;
1257 int nXStart = nW - nLastColumns;
1258 List<T> rg = new List<T>();
1259
1260 if (nXStart < 0)
1261 nXStart = 0;
1262
1263 for (int c = 0; c < nC; c++)
1264 {
1265 for (int y = 0; y < nH; y++)
1266 {
1267 for (int x = nXStart; x < nW; x++)
1268 {
1269 int nIdxImg = (c * (nH * nW)) + (y * nW) + x;
1270 rg.Add((T)Convert.ChangeType(ByteData[nIdxImg], typeof(T)));
1271 }
1272 }
1273 }
1274
1275 return rg;
1276 }
1277
1283 public void MaskOutAllButLastColumnsX(int nLastColumsToRetain, int nMaskingValue)
1284 {
1285 if (nLastColumsToRetain <= 0 || nLastColumsToRetain >= m_nWidth)
1286 return;
1287
1288 int nC = m_nChannels;
1289 int nW = m_nWidth;
1290 int nH = m_nHeight;
1291 int nMaskWid = nW - nLastColumsToRetain;
1292
1293 for (int c = 0; c < nC; c++)
1294 {
1295 for (int y = 0; y < nH; y++)
1296 {
1297 for (int x = 0; x < nMaskWid; x++)
1298 {
1299 int nIdxImg = (c * (nH * nW)) + (y * nW) + x;
1300 ByteData[nIdxImg] = (byte)nMaskingValue;
1301 }
1302 }
1303 }
1304 }
1305
1312 public void SetData(List<byte> rgByteData, int nLabel, bool bAllowVirtualOverride = false)
1313 {
1314 if (!bAllowVirtualOverride && m_nVirtualID != 0)
1315 throw new Exception("Cannot set the data of a virtual item!");
1316
1317 m_nVirtualID = 0;
1318 m_bIsRealData = false;
1319 m_rgByteData = rgByteData.ToArray();
1320 m_rgRealDataD = null;
1321 m_rgRealDataF = null;
1322 m_nLabel = nLabel;
1323 }
1324
1331 public void SetData(List<double> rgRealData, int nLabel, bool bAllowVirtualOverride = false)
1332 {
1333 if (!bAllowVirtualOverride && m_nVirtualID != 0)
1334 throw new Exception("Cannot set the data of a virtual item!");
1335
1336 m_nVirtualID = 0;
1337 m_bIsRealData = true;
1338 m_rgByteData = null;
1339 m_rgRealDataF = null;
1340 m_rgRealDataD = rgRealData.ToArray();
1341 m_nLabel = nLabel;
1342 }
1343
1350 public void SetData(List<float> rgRealData, int nLabel, bool bAllowVirtualOverride = false)
1351 {
1352 if (!bAllowVirtualOverride && m_nVirtualID != 0)
1353 throw new Exception("Cannot set the data of a virtual item!");
1354
1355 m_nVirtualID = 0;
1356 m_bIsRealData = true;
1357 m_rgByteData = null;
1358 m_rgRealDataD = null;
1359 m_rgRealDataF = rgRealData.ToArray();
1360 m_nLabel = nLabel;
1361 }
1362
1371 public void SetData(byte[] rgb, int nLabel)
1372 {
1373 m_nLabel = nLabel;
1374 m_rgByteData = new byte[rgb.Length];
1375 Array.Copy(rgb, m_rgByteData, rgb.Length);
1376 m_bIsRealData = false;
1377 m_rgRealDataD = null;
1378 m_rgRealDataF = null;
1379 }
1380
1389 public void SetData(double[] rgdf, int nLabel)
1390 {
1391 m_nLabel = nLabel;
1392 m_rgRealDataF = null;
1393 m_rgRealDataD = new double[rgdf.Length];
1394 Array.Copy(rgdf, m_rgRealDataD, rgdf.Length);
1395 m_rgByteData = null;
1396 m_bIsRealData = true;
1397 }
1398
1407 public void SetData(float[] rgf, int nLabel)
1408 {
1409 m_nLabel = nLabel;
1410 m_rgRealDataD = null;
1411 m_rgRealDataF = new float[rgf.Length];
1412 Array.Copy(rgf, m_rgRealDataF, rgf.Length);
1413 m_rgByteData = null;
1414 m_bIsRealData = true;
1415 }
1416
1421 public void SetLabel(int nLabel)
1422 {
1423 m_nLabel = nLabel;
1424 }
1425
1429 public void ResetLabel()
1430 {
1431 m_nLabel = m_nOriginalLabel;
1432 }
1433
1437 public int ItemCount
1438 {
1439 get
1440 {
1441 if (IsRealData)
1442 {
1443 if (m_rgRealDataD != null)
1444 return m_rgRealDataD.Length;
1445 else if (m_rgRealDataF != null)
1446 return m_rgRealDataF.Length;
1447 else
1448 throw new Exception("SimpleDatum: Both the RealDataD and RealDataF are null!");
1449 }
1450 else
1451 return m_rgByteData.Length;
1452 }
1453 }
1454
1458 public bool HasRealData
1459 {
1460 get
1461 {
1462 if (m_rgRealDataD != null && m_rgRealDataD.Length > 0)
1463 return true;
1464
1465 if (m_rgRealDataF != null && m_rgRealDataF.Length > 0)
1466 return true;
1467
1468 return false;
1469 }
1470 }
1471
1478 public T GetDataAt<T>(int nIdx)
1479 {
1480 if (IsRealData)
1481 {
1482 if (m_rgRealDataD != null)
1483 return (T)Convert.ChangeType(m_rgRealDataD[nIdx], typeof(T));
1484 else if (m_rgRealDataF != null)
1485 return (T)Convert.ChangeType(m_rgRealDataF[nIdx], typeof(T));
1486 else
1487 throw new Exception("SimpleDatum: Both the RealDataD and RealDataF are null!");
1488 }
1489 else
1490 {
1491 return (T)Convert.ChangeType(m_rgByteData[nIdx], typeof(T));
1492 }
1493 }
1494
1500 public double GetDataAtD(int nIdx)
1501 {
1502 if (IsRealData)
1503 {
1504 if (m_rgRealDataD != null)
1505 return m_rgRealDataD[nIdx];
1506 else if (m_rgRealDataF != null)
1507 return m_rgRealDataF[nIdx];
1508 else
1509 throw new Exception("SimpleDatum: Both the RealDataD and RealDataF are null!");
1510 }
1511 else
1512 {
1513 return m_rgByteData[nIdx];
1514 }
1515 }
1516
1522 public float GetDataAtF(int nIdx)
1523 {
1524 if (IsRealData)
1525 {
1526 if (m_rgRealDataD != null)
1527 return (float)m_rgRealDataD[nIdx];
1528 else if (m_rgRealDataF != null)
1529 return m_rgRealDataF[nIdx];
1530 else
1531 throw new Exception("SimpleDatum: Both the RealDataD and RealDataF are null!");
1532 }
1533 else
1534 {
1535 return m_rgByteData[nIdx];
1536 }
1537 }
1538
1544 public byte GetDataAtByte(int nIdx)
1545 {
1546 if (IsRealData)
1547 {
1548 double dfVal = 0;
1549
1550 if (m_rgRealDataD != null)
1551 dfVal = m_rgRealDataD[nIdx];
1552 else if (m_rgRealDataF != null)
1553 dfVal = m_rgRealDataF[nIdx];
1554 else
1555 throw new Exception("SimpleDatum: Both the RealDataD and RealDataF are null!");
1556
1557 return (byte)Math.Min(Math.Max(0, dfVal), 255);
1558 }
1559 else
1560 {
1561 return m_rgByteData[nIdx];
1562 }
1563 }
1564
1572 public T[] GetData<T>(int nImagePadX = 0, int nImagePadY = 0)
1573 {
1574 if (IsRealData)
1575 {
1576 if (m_rgRealDataD != null)
1577 {
1578 if (m_rgRealDataD.Length == 0)
1579 return null;
1580
1581 if (typeof(T) == typeof(double))
1582 return (T[])Convert.ChangeType(m_rgRealDataD.ToArray(), typeof(T[]));
1583
1584 T[] rg = new T[m_rgRealDataD.Length];
1585 for (int i = 0; i < rg.Length; i++)
1586 {
1587 rg[i] = (T)Convert.ChangeType(m_rgRealDataD[i], typeof(T));
1588 }
1589
1590 return rg;
1591 }
1592 else if (m_rgRealDataF != null)
1593 {
1594 if (m_rgRealDataF.Length == 0)
1595 return null;
1596
1597 if (typeof(T) == typeof(float))
1598 return (T[])Convert.ChangeType(m_rgRealDataF.ToArray(), typeof(T[]));
1599
1600 T[] rg = new T[m_rgRealDataF.Length];
1601 for (int i = 0; i < rg.Length; i++)
1602 {
1603 rg[i] = (T)Convert.ChangeType(m_rgRealDataF[i], typeof(T));
1604 }
1605
1606 return rg;
1607 }
1608 else
1609 {
1610 return null;
1611 }
1612 }
1613 else
1614 {
1615 if (m_rgByteData == null || m_rgByteData.Length == 0)
1616 return null;
1617
1618 T[] rg = new T[m_rgByteData.Length];
1619
1620 for (int i = 0; i < rg.Length; i++)
1621 {
1622 rg[i] = (T)Convert.ChangeType(m_rgByteData[i], typeof(T));
1623 }
1624
1625 return rg;
1626 }
1627 }
1628
1639 public static byte[] GetByteData(byte[] rgData, int nImagePadX, int nImagePadY, int nHeight, int nWidth, int nChannels)
1640 {
1641 if (nImagePadX == 0 && nImagePadY == 0)
1642 return rgData;
1643
1644 return PadData<byte>(new List<byte>(rgData), nImagePadX, nImagePadY, nHeight, nWidth, nChannels);
1645 }
1646
1657 public static Tuple<double[], float[]> GetRealData(byte[] rgData, int nImagePadX, int nImagePadY, int nHeight, int nWidth, int nChannels)
1658 {
1659 Tuple<double[], float[]> rgRealData = GetRealData(rgData);
1660
1661 double[] rgRealDataD = rgRealData.Item1;
1662 float[] rgRealDataF = rgRealData.Item2;
1663
1664 if (rgRealDataD != null)
1665 rgRealDataD = PadData<double>(rgRealDataD.ToList(), nImagePadX, nImagePadY, nHeight, nWidth, nChannels);
1666 else
1667 rgRealDataF = PadData<float>(rgRealDataF.ToList(), nImagePadX, nImagePadY, nHeight, nWidth, nChannels);
1668
1669 return new Tuple<double[], float[]>(rgRealDataD, rgRealDataF);
1670 }
1671
1683 public static T[] PadData<T>(List<T> rgData, int nImagePadX, int nImagePadY, int nHeight, int nWidth, int nChannels)
1684 {
1685 int nDstIdx = 0;
1686 int nCount = nChannels * (nHeight * (nImagePadX + nWidth) + (nImagePadY * nImagePadX));
1687 T[] rgDataNew = new T[nCount];
1688
1689 for (int c = 0; c < nChannels; c++)
1690 {
1691 for (int y = 0; y < nHeight; y++)
1692 {
1693 for (int i = 0; i < nImagePadX; i++)
1694 {
1695 rgDataNew[nDstIdx] = (T)Convert.ChangeType(0.0, typeof(T));
1696 nDstIdx++;
1697 }
1698
1699 for (int x = 0; x < nWidth; x++)
1700 {
1701 int nIdx = (c * nWidth * nHeight) + (y * nWidth) + x;
1702
1703 rgDataNew[nDstIdx] = rgData[nIdx];
1704 nDstIdx++;
1705 }
1706 }
1707
1708 for (int i = 0; i < nImagePadY; i++)
1709 {
1710 for (int x = 0; x < nWidth + nImagePadX; x++)
1711 {
1712 rgDataNew[nDstIdx] = (T)Convert.ChangeType(0.0, typeof(T));
1713 nDstIdx++;
1714 }
1715 }
1716 }
1717
1718 return rgDataNew;
1719 }
1720
1726 public byte[] GetByteData(out bool bEncoded)
1727 {
1728 if (!IsRealData)
1729 {
1730 bEncoded = false;
1731 return ByteData;
1732 }
1733
1734 bEncoded = true;
1735
1736 if (m_rgRealDataD != null)
1737 return GetByteData(new List<double>(m_rgRealDataD));
1738 else if (m_rgRealDataF != null)
1739 return GetByteData(new List<float>(m_rgRealDataF));
1740 else
1741 throw new Exception("SimpleDatum: Both the RealDataD and RealDataF are null!");
1742 }
1743
1752 public static byte[] GetByteData(List<double> rgData)
1753 {
1754 int nCount = rgData.Count;
1755 int nSize = sizeof(double);
1756
1757 int nOffset = 0;
1758 int nDataCount = sizeof(int) + sizeof(int) + (nSize * rgData.Count);
1759 byte[] rgByte = new byte[nDataCount];
1760
1761 byte[] rg = BitConverter.GetBytes(nCount);
1762 Array.Copy(rg, 0, rgByte, nOffset, rg.Length);
1763 nOffset += rg.Length;
1764
1765 rg = BitConverter.GetBytes(nSize);
1766 Array.Copy(rg, 0, rgByte, nOffset, rg.Length);
1767 nOffset += rg.Length;
1768
1769 foreach (double df in rgData)
1770 {
1771 rg = BitConverter.GetBytes(df);
1772 Array.Copy(rg, 0, rgByte, nOffset, rg.Length);
1773 nOffset += rg.Length;
1774 }
1775
1776 return rgByte;
1777 }
1778
1787 public static byte[] GetByteData(List<float> rgData)
1788 {
1789 int nCount = rgData.Count;
1790 int nSize = sizeof(float);
1791
1792 int nOffset = 0;
1793 int nDataCount = sizeof(int) + sizeof(int) + (nSize * rgData.Count);
1794 byte[] rgByte = new byte[nDataCount];
1795
1796 byte[] rg = BitConverter.GetBytes(nCount);
1797 Array.Copy(rg, 0, rgByte, nOffset, rg.Length);
1798 nOffset += rg.Length;
1799
1800 rg = BitConverter.GetBytes(nSize);
1801 Array.Copy(rg, 0, rgByte, nOffset, rg.Length);
1802 nOffset += rg.Length;
1803
1804 foreach (float df in rgData)
1805 {
1806 rg = BitConverter.GetBytes(df);
1807 Array.Copy(rg, 0, rgByte, nOffset, rg.Length);
1808 nOffset += rg.Length;
1809 }
1810
1811 return rgByte;
1812 }
1813
1819 public static Tuple<double[], float[]> GetRealData(byte[] rgData)
1820 {
1821 double[] rgDataD = null;
1822 float[] rgDataF = null;
1823 int nIdx = 0;
1824
1825 int nCount = BitConverter.ToInt32(rgData, nIdx);
1826 nIdx += 4;
1827 int nSize = BitConverter.ToInt32(rgData, nIdx);
1828 nIdx += 4;
1829
1830 // If the size is invalid, revert back to the legacy double only data.
1831 if (nSize != sizeof(float) && nSize != sizeof(double))
1832 {
1833 nIdx = 0;
1834 nSize = sizeof(double);
1835 }
1836
1837 if (nSize == sizeof(double))
1838 rgDataD = getRealDataD(rgData, nIdx);
1839 else
1840 rgDataF = getRealDataF(rgData, nIdx);
1841
1842 return new Tuple<double[], float[]>(rgDataD, rgDataF);
1843 }
1844
1851 protected static double[] getRealDataD(byte[] rgData, int nIdx)
1852 {
1853 List<double> rgData0 = new List<double>();
1854
1855 while (nIdx < rgData.Length)
1856 {
1857 rgData0.Add(BitConverter.ToDouble(rgData, nIdx));
1858 nIdx += 8;
1859 }
1860
1861 return rgData0.ToArray();
1862 }
1863
1870 protected static float[] getRealDataF(byte[] rgData, int nIdx)
1871 {
1872 List<float> rgData0 = new List<float>();
1873
1874 while (nIdx < rgData.Length)
1875 {
1876 rgData0.Add(BitConverter.ToSingle(rgData, nIdx));
1877 nIdx += 8;
1878 }
1879
1880 return rgData0.ToArray();
1881 }
1882
1889 {
1890 if (m_nChannels != d.Channels ||
1891 m_nHeight != d.Height ||
1892 m_nWidth != d.Width)
1893 throw new Exception("Datum dimmensions do not match!");
1894
1895 if (ItemCount != d.ItemCount)
1896 throw new Exception("Datum counts do not match!");
1897
1898 SimpleDatum d1 = new SimpleDatum(d, false);
1899
1900 d1.m_rgRealDataD = null;
1901 d1.m_rgRealDataF = null;
1902 d1.m_rgByteData = null;
1903
1904 if (m_bIsRealData)
1905 {
1906 if (m_rgRealDataD != null)
1907 {
1908 d1.m_rgRealDataD = new double[m_rgRealDataD.Length];
1909
1910 for (int i = 0; i < m_rgRealDataD.Length; i++)
1911 {
1912 d1.m_rgRealDataD[i] = m_rgRealDataD[i] + d.GetDataAtD(i);
1913 }
1914 }
1915 else if (m_rgRealDataF != null)
1916 {
1917 d1.m_rgRealDataF = new float[m_rgRealDataF.Length];
1918
1919 for (int i = 0; i < m_rgRealDataF.Length; i++)
1920 {
1921 d1.m_rgRealDataF[i] = m_rgRealDataF[i] + d.GetDataAtF(i);
1922 }
1923 }
1924 else
1925 {
1926 throw new Exception("SimpleDatum: Both the RealDataD and RealDataF are null!");
1927 }
1928
1929 d1.m_bIsRealData = true;
1930 }
1931 else
1932 {
1933 d1.m_rgByteData = new byte[m_rgByteData.Length];
1934 d1.m_bIsRealData = false;
1935
1936 for (int i = 0; i < m_rgByteData.Length; i++)
1937 {
1938 int nVal = m_rgByteData[i] + d.GetDataAtByte(i);
1939 d1.m_rgByteData[i] = (byte)Math.Min(Math.Max(0, nVal), 255);
1940 }
1941 }
1942
1943 return d1;
1944 }
1945
1952 public SimpleDatum Div(double dfVal, bool bConvertToByte)
1953 {
1954 if (dfVal == 0)
1955 throw new ArgumentOutOfRangeException("dfVal", 0, "Cannot divide the simple datums by zero!");
1956
1957 SimpleDatum d1 = new SimpleDatum(this, false);
1958
1959 d1.m_rgRealDataD = null;
1960 d1.m_rgRealDataF = null;
1961 d1.m_rgByteData = null;
1962
1963 int nCount = ItemCount;
1964
1965 if (m_bIsRealData && !bConvertToByte)
1966 {
1967 if (m_rgRealDataD != null)
1968 {
1969 d1.m_rgRealDataD = new double[nCount];
1970 d1.m_bIsRealData = true;
1971
1972 for (int i = 0; i < nCount; i++)
1973 {
1974 d1.m_rgRealDataD[i] = m_rgRealDataD[i] / dfVal;
1975 }
1976 }
1977 else if (m_rgRealDataF != null)
1978 {
1979 d1.m_rgRealDataF = new float[nCount];
1980 d1.m_bIsRealData = true;
1981
1982 for (int i = 0; i < nCount; i++)
1983 {
1984 d1.m_rgRealDataF[i] = (float)(m_rgRealDataF[i] / dfVal);
1985 }
1986 }
1987 else
1988 {
1989 throw new Exception("SimpleDatum: Both the RealDataD and RealDataF are null!");
1990 }
1991 }
1992 else if (m_bIsRealData && bConvertToByte)
1993 {
1994 d1.m_rgByteData = new byte[nCount];
1995 d1.m_bIsRealData = false;
1996
1997 if (m_rgRealDataD != null)
1998 {
1999 for (int i = 0; i < nCount; i++)
2000 {
2001 double dfVal1 = m_rgRealDataD[i] / dfVal;
2002 m_rgByteData[i] = (byte)Math.Min(Math.Max(dfVal1, 0), 255);
2003 }
2004 }
2005 else if (m_rgRealDataF != null)
2006 {
2007 for (int i = 0; i < nCount; i++)
2008 {
2009 double dfVal1 = m_rgRealDataF[i] / dfVal;
2010 m_rgByteData[i] = (byte)Math.Min(Math.Max(dfVal1, 0), 255);
2011 }
2012 }
2013 else
2014 {
2015 throw new Exception("SimpleDatum: Both the RealDataD and RealDataF are null!");
2016 }
2017 }
2018 else
2019 {
2020 d1.m_rgByteData = new byte[nCount];
2021 d1.m_bIsRealData = false;
2022
2023 for (int i = 0; i < nCount; i++)
2024 {
2025 double dfVal1 = (double)m_rgByteData[i] / dfVal;
2026 m_rgByteData[i] = (byte)Math.Min(Math.Max(dfVal1, 0), 255);
2027 }
2028 }
2029
2030 return d1;
2031 }
2032
2036 public int SourceID
2037 {
2038 get { return m_nSourceID; }
2039 }
2040
2045 {
2046 get { return m_nOriginalSourceID; }
2047 }
2048
2052 public int ImageID
2053 {
2054 get { return m_nImageID; }
2055 }
2056
2061 public void SetImageID(int nID)
2062 {
2063 m_nImageID = nID;
2064 }
2065
2069 public int VirtualID
2070 {
2071 get { return m_nVirtualID; }
2072 }
2073
2077 public int GroupID
2078 {
2079 get { return m_nGroupID; }
2080 set { m_nGroupID = value; }
2081 }
2082
2086 public int Index
2087 {
2088 get { return m_nIndex; }
2089 set { m_nIndex = value; }
2090 }
2091
2095 public DateTime TimeStamp
2096 {
2097 get { return m_dt; }
2098 set { m_dt = value; }
2099 }
2100
2104 public bool AutoLabeled
2105 {
2106 get { return m_bAutoLabeled; }
2107 set { m_bAutoLabeled = value; }
2108 }
2109
2113 public bool IsRealData
2114 {
2115 get { return m_bIsRealData; }
2116 }
2117
2121 public int Height
2122 {
2123 get { return m_nHeight; }
2124 }
2125
2129 public int Width
2130 {
2131 get { return m_nWidth; }
2132 }
2133
2137 public int Channels
2138 {
2139 get { return m_nChannels; }
2140 }
2141
2145 public int Label
2146 {
2147 get { return m_nLabel; }
2148 }
2149
2153 public int OriginalLabel
2154 {
2155 get { return m_nOriginalLabel; }
2156 set { m_nOriginalLabel = value; }
2157 }
2158
2162 public byte[] ByteData
2163 {
2164 get { return m_rgByteData; }
2165 }
2166
2170 public double[] RealDataD
2171 {
2172 get { return m_rgRealDataD; }
2173 }
2174
2178 public float[] RealDataF
2179 {
2180 get { return m_rgRealDataF; }
2181 }
2182
2186 public int Boost
2187 {
2188 get { return m_nBoost; }
2189 set { m_nBoost = value; }
2190 }
2191
2195 public void ResetBoost()
2196 {
2197 m_nBoost = m_nOriginalBoost;
2198 }
2199
2204 {
2205 get { return m_dataCriteriaFormat; }
2206 set { m_dataCriteriaFormat = value; }
2207 }
2208
2212 public byte[] DataCriteria
2213 {
2214 get { return m_rgDataCriteria; }
2215 set { m_rgDataCriteria = value; }
2216 }
2217
2222 {
2223 get { return m_debugDataFormat; }
2224 set { m_debugDataFormat = value; }
2225 }
2226
2230 public byte[] DebugData
2231 {
2232 get { return m_rgDebugData; }
2233 set { m_rgDebugData = value; }
2234 }
2235
2239 public string Description
2240 {
2241 get { return m_strDesc; }
2242 set { m_strDesc = value; }
2243 }
2244
2250 {
2251 get { return m_nAnnotationType; }
2252 set { m_nAnnotationType = value; }
2253 }
2254
2259 {
2260 get { return m_rgAnnotationGroup; }
2261 set { m_rgAnnotationGroup = value; }
2262 }
2263
2270 public SimpleDatum Resize(int nH, int nW)
2271 {
2272 Image bmp = ImageData.GetImage(new Datum(this));
2273 Bitmap bmpNew = ImageTools.ResizeImage(bmp, nH, nW);
2274 Datum d = ImageData.GetImageData(bmpNew, this);
2275
2276 bmp.Dispose();
2277 bmpNew.Dispose();
2278
2279 return d;
2280 }
2281
2286 public override string ToString()
2287 {
2288 string strOut = "Idx = " + m_nIndex.ToString("N0");
2289
2290 strOut += "; Label = " + m_nLabel.ToString();
2291 strOut += "; Boost = " + m_nBoost.ToString();
2292
2293 if (m_dt != DateTime.MinValue)
2294 strOut += "; Time = " + m_dt.ToString();
2295
2296 if (!string.IsNullOrEmpty(m_strDesc))
2297 strOut += "; Desc = " + m_strDesc;
2298
2299 return strOut;
2300 }
2301
2307 public string ToArrayAsString(int nMaxItems)
2308 {
2309 string str = "";
2310
2311 if (m_bIsRealData)
2312 {
2313 if (m_rgRealDataD != null)
2314 {
2315 for (int i = 0; i < m_rgRealDataD.Length && i < nMaxItems; i++)
2316 {
2317 str += m_rgRealDataD[i].ToString() + ",";
2318 }
2319 }
2320 else if (m_rgRealDataF != null)
2321 {
2322 for (int i = 0; i < m_rgRealDataF.Length && i < nMaxItems; i++)
2323 {
2324 str += m_rgRealDataF[i].ToString() + ",";
2325 }
2326 }
2327 else
2328 {
2329 str = "No Real Data Found!";
2330 }
2331 }
2332 else
2333 {
2334 for (int i = 0; i < m_rgByteData.Length && i < nMaxItems; i++)
2335 {
2336 str += m_rgByteData[i].ToString() + ",";
2337 }
2338 }
2339
2340 return str.TrimEnd(',');
2341 }
2342
2343
2352 {
2353 if (m_rgByteData == null)
2354 throw new Exception("Bytemaps are only supported with byte based data.");
2355
2356 return new Bytemap(m_nChannels, m_nHeight, m_nWidth, m_rgByteData);
2357 }
2358
2366 public static bool AccumulateMean(ref double[] rgdfMean, SimpleDatum sd, int nTotal)
2367 {
2368 if (rgdfMean == null)
2369 rgdfMean = new double[sd.ItemCount];
2370
2371 if (sd.IsRealData)
2372 {
2373 if (sd.RealDataD != null)
2374 {
2375 for (int i = 0; i < sd.ItemCount; i++)
2376 {
2377 rgdfMean[i] += sd.RealDataD[i] / nTotal;
2378 }
2379 }
2380 else if (sd.RealDataF != null)
2381 {
2382 for (int i = 0; i < sd.ItemCount; i++)
2383 {
2384 rgdfMean[i] += sd.RealDataF[i] / nTotal;
2385 }
2386 }
2387 else
2388 {
2389 throw new Exception("SimpleDatum: Both the RealDataD and RealDataF are null!");
2390 }
2391 }
2392 else
2393 {
2394 for (int i = 0; i < sd.ItemCount; i++)
2395 {
2396 rgdfMean[i] += (double)sd.ByteData[i] / (double)nTotal;
2397 }
2398 }
2399
2400 return true;
2401 }
2402
2410 public static SimpleDatum CalculateMean(Log log, SimpleDatum[] rgImg, WaitHandle[] rgAbort)
2411 {
2412 if (rgImg.Length < 2)
2413 throw new Exception("There must be at least 2 images in the simple datum array.");
2414
2415 float[] rgSums;
2416
2417 if (rgImg[0].ByteData != null)
2418 rgSums = new float[rgImg[0].ByteData.Length];
2419 else if (rgImg[0].RealDataD != null)
2420 rgSums = new float[rgImg[0].RealDataD.Length];
2421 else if (rgImg[0].RealDataF != null)
2422 rgSums = new float[rgImg[0].RealDataF.Length];
2423 else
2424 throw new Exception("No data in rgImg[0]!");
2425
2426 Stopwatch sw = new Stopwatch();
2427
2428 try
2429 {
2430 sw.Start();
2431
2432 int nCount = 0;
2433 int nHeight = 0;
2434 int nWidth = 0;
2435 for (int i = 0; i < rgImg.Length && i < 100; i++)
2436 {
2437 nHeight = Math.Max(nHeight, rgImg[i].Height);
2438 nWidth = Math.Max(nWidth, rgImg[i].Width);
2439 }
2440
2441 for (int i = 0; i < rgImg.Length; i++)
2442 {
2443 if (rgImg[i] != null)
2444 {
2445 if (rgImg[i].Height != nHeight || rgImg[i].Width != nWidth)
2446 continue;
2447
2448 if (rgImg[i].ByteData != null)
2449 {
2450 for (int n = 0; n < rgSums.Length; n++)
2451 {
2452 rgSums[n] += rgImg[i].ByteData[n];
2453 }
2454 }
2455 else if (rgImg[i].RealDataD != null)
2456 {
2457 for (int n = 0; n < rgSums.Length; n++)
2458 {
2459 rgSums[n] += (float)rgImg[i].RealDataD[n];
2460 }
2461 }
2462 else if (rgImg[i].RealDataF != null)
2463 {
2464 for (int n = 0; n < rgSums.Length; n++)
2465 {
2466 rgSums[n] += (float)rgImg[i].RealDataF[n];
2467 }
2468 }
2469 else
2470 {
2471 throw new Exception("No data in rgImg[" + i.ToString() + "]!");
2472 }
2473
2474 nCount++;
2475
2476 if (sw.Elapsed.TotalMilliseconds > 2000)
2477 {
2478 double dfPct = (double)i / (double)rgImg.Length;
2479 log.WriteLine("processing mean (" + dfPct.ToString("P") + ")");
2480 sw.Restart();
2481
2482 if (rgAbort != null)
2483 {
2484 if (EventWaitHandle.WaitAny(rgAbort, 0) != EventWaitHandle.WaitTimeout)
2485 return null;
2486 }
2487 }
2488 }
2489 }
2490
2491 if (rgImg[0].ByteData != null)
2492 {
2493 byte[] rgbMean = new byte[rgSums.Length];
2494
2495 for (int n = 0; n < rgSums.Length; n++)
2496 {
2497 rgbMean[n] = (byte)(rgSums[n] / (float)nCount);
2498 }
2499
2500 SimpleDatum d = new SimpleDatum(rgImg[0], false);
2501 d.SetData(new List<byte>(rgbMean), -1, true);
2502 return d;
2503 }
2504 else if (rgImg[0].RealDataD != null)
2505 {
2506 double[] rgdfMean = new double[rgSums.Length];
2507
2508 for (int n = 0; n < rgSums.Length; n++)
2509 {
2510 rgdfMean[n] = (double)rgSums[n] / nCount;
2511 }
2512
2513 SimpleDatum d = new SimpleDatum(rgImg[0], false);
2514 d.SetData(new List<double>(rgdfMean), -1, true);
2515 return d;
2516 }
2517 else if (rgImg[0].RealDataF != null)
2518 {
2519 float[] rgfMean = new float[rgSums.Length];
2520
2521 for (int n = 0; n < rgSums.Length; n++)
2522 {
2523 rgfMean[n] = (float)rgSums[n] / nCount;
2524 }
2525
2526 SimpleDatum d = new SimpleDatum(rgImg[0], false);
2527 d.SetData(new List<float>(rgfMean), -1, true);
2528 return d;
2529 }
2530 else
2531 {
2532 throw new Exception("SimpleDatum: Both the RealDataD and RealDataF are null!");
2533 }
2534 }
2535 finally
2536 {
2537 }
2538 }
2539
2547 {
2548 using (MemoryStream ms = new MemoryStream())
2549 using (BinaryWriter bw = new BinaryWriter(ms))
2550 {
2551 bw.Write((int)type);
2552
2553 int nCount = 0;
2554 if (annotations != null)
2555 nCount = annotations.Count;
2556
2557 bw.Write(nCount);
2558
2559 if (annotations != null)
2560 {
2561 for (int i = 0; i < annotations.Count; i++)
2562 {
2563 annotations[i].Save(bw);
2564 }
2565 }
2566
2567 bw.Flush();
2568 return ms.ToArray();
2569 }
2570 }
2571
2576 {
2577 DataCriteria = SaveAnnotationDataToDataCriteriaByteArray(m_nAnnotationType, m_rgAnnotationGroup);
2578 DataCriteriaFormat = DATA_FORMAT.ANNOTATION_DATA;
2579 }
2580
2589 {
2590 type = ANNOTATION_TYPE.NONE;
2591
2592 if (rg == null || fmt != DATA_FORMAT.ANNOTATION_DATA)
2593 return null;
2594
2595 using (MemoryStream ms = new MemoryStream(rg))
2596 using (BinaryReader br = new BinaryReader(ms))
2597 {
2598 type = (ANNOTATION_TYPE)br.ReadInt32();
2600
2601 int nCount = br.ReadInt32();
2602 if (nCount > 0)
2603 {
2604 for (int i = 0; i < nCount; i++)
2605 {
2606 rgGroups.Add(AnnotationGroup.Load(br));
2607 }
2608 }
2609
2610 return rgGroups;
2611 }
2612 }
2613
2619 {
2620 m_rgAnnotationGroup = LoadAnnotationDataFromDataCriteria(DataCriteria, DataCriteriaFormat, out m_nAnnotationType);
2621
2622 if (m_rgAnnotationGroup == null)
2623 return false;
2624
2625 return true;
2626 }
2627
2635 public void SaveInfo(string strFile)
2636 {
2637 using (StreamWriter sw = new StreamWriter(strFile + ".txt"))
2638 {
2639 sw.WriteLine("Index = " + Index.ToString());
2640 sw.WriteLine("ImageID = " + ImageID.ToString());
2641 sw.WriteLine("VirtualID = " + VirtualID.ToString());
2642 sw.WriteLine("Label = " + Label.ToString());
2643 sw.WriteLine("AutoLabeled = " + AutoLabeled.ToString());
2644 sw.WriteLine("Boost = " + Boost.ToString());
2645 sw.WriteLine("SourceID = " + SourceID.ToString());
2646 sw.WriteLine("OriginalSourceID = " + OriginalSourceID.ToString());
2647 sw.WriteLine("Description = " + Description);
2648 sw.WriteLine("Time = " + TimeStamp.ToString());
2649 sw.WriteLine("Size = {1," + Channels.ToString() + "," + Height.ToString() + "," + Width.ToString() + "}");
2650 }
2651 }
2652
2661 public static SimpleDatum LoadInfo(string strFile)
2662 {
2663 using (StreamReader sr = new StreamReader(strFile))
2664 {
2665 string strVal = parseValue(sr.ReadLine(), '=');
2666 int nIndex = int.Parse(strVal);
2667
2668 strVal = parseValue(sr.ReadLine(), '=');
2669 int nImageID = int.Parse(strVal);
2670
2671 strVal = parseValue(sr.ReadLine(), '=');
2672 int nVirtualID = int.Parse(strVal);
2673
2674 strVal = parseValue(sr.ReadLine(), '=');
2675 int nLabel = int.Parse(strVal);
2676
2677 strVal = parseValue(sr.ReadLine(), '=');
2678 bool bAutoLabeled = bool.Parse(strVal);
2679
2680 strVal = parseValue(sr.ReadLine(), '=');
2681 int nBoost = int.Parse(strVal);
2682
2683 strVal = parseValue(sr.ReadLine(), '=');
2684 int nSrcId = int.Parse(strVal);
2685
2686 strVal = parseValue(sr.ReadLine(), '=');
2687 int nOriginalSrcId = int.Parse(strVal);
2688
2689 strVal = parseValue(sr.ReadLine(), '=');
2690 string strDesc = strVal;
2691
2692 strVal = parseValue(sr.ReadLine(), '=');
2693 DateTime dt = DateTime.Parse(strVal);
2694
2695 strVal = parseValue(sr.ReadLine(), '=');
2696 strVal = strVal.Trim('{', '}');
2697 string[] rgstr = strVal.Split(',');
2698
2699 int nChannels = int.Parse(rgstr[1]);
2700 int nHeight = int.Parse(rgstr[2]);
2701 int nWidth = int.Parse(rgstr[3]);
2702
2703 return new SimpleDatum(false, nChannels, nWidth, nHeight, nLabel, dt, nBoost, bAutoLabeled, nIndex, nVirtualID, nImageID, nSrcId, nOriginalSrcId);
2704 }
2705 }
2706
2707 private static string parseValue(string str, char chDelim)
2708 {
2709 int nPos = str.LastIndexOf(chDelim);
2710 if (nPos < 0)
2711 return str;
2712
2713 return str.Substring(nPos + 1).Trim();
2714 }
2715
2721 public static List<SimpleDatum> LoadFromPath(string strPath)
2722 {
2723 string[] rgstrFiles = Directory.GetFiles(strPath, "*.txt");
2724 List<SimpleDatum> rgData = new List<SimpleDatum>();
2725
2726 foreach (string strFile in rgstrFiles)
2727 {
2728 rgData.Add(SimpleDatum.LoadInfo(strFile));
2729 }
2730
2731 return rgData;
2732 }
2733
2742 public static float[] Transpose(float[] rg, int nH, int nW, int nDim = 1)
2743 {
2744 float[] rgT = new float[rg.Length];
2745
2746 for (int i = 0; i < nH; i++)
2747 {
2748 for (int j = 0; j < nW; j++)
2749 {
2750 int nSrcIdx = (i * nW + j) * nDim;
2751 int nDstIdx = (j * nH + i) * nDim;
2752
2753 if (nDim == 1)
2754 rgT[nDstIdx] = rg[nSrcIdx];
2755 else
2756 Array.Copy(rg, nSrcIdx, rgT, nDstIdx, nDim);
2757 }
2758 }
2759
2760 return rgT;
2761 }
2762
2771 public static double[] Transpose(double[] rg, int nH, int nW, int nDim = 1)
2772 {
2773 double[] rgT = new double[rg.Length];
2774
2775 for (int i = 0; i < nH; i++)
2776 {
2777 for (int j = 0; j < nW; j++)
2778 {
2779 int nSrcIdx = (i * nW + j) * nDim;
2780 int nDstIdx = (j * nH + i) * nDim;
2781
2782 if (nDim == 1)
2783 rgT[nDstIdx] = rg[nSrcIdx];
2784 else
2785 Array.Copy(rg, nSrcIdx, rgT, nDstIdx, nDim);
2786 }
2787 }
2788
2789 return rgT;
2790 }
2791 }
2792
2796 public class SimpleDatumCollection : IEnumerable<SimpleDatum>
2797 {
2798 string m_strName;
2799 SimpleDatum[] m_rgItems = null;
2800 List<int> m_rgShape;
2801 object m_tag = null;
2802
2809 public SimpleDatumCollection(int nCount, string strName = "", List<int> rgShape = null)
2810 {
2811 m_strName = strName;
2812 m_rgItems = new SimpleDatum[nCount];
2813
2814 m_rgShape = new List<int>() { nCount };
2815
2816 if (rgShape != null)
2817 m_rgShape.AddRange(rgShape);
2818 }
2819
2823 public object Tag
2824 {
2825 get { return m_tag; }
2826 set { m_tag = value; }
2827 }
2828
2832 public List<int> Shape
2833 {
2834 get { return m_rgShape; }
2835 }
2836
2840 public string Name
2841 {
2842 get { return m_strName; }
2843 set { m_strName = value; }
2844 }
2845
2849 public int Count
2850 {
2851 get { return m_rgItems.Count(); }
2852 }
2853
2859 public SimpleDatum this[int nIdx]
2860 {
2861 get { return m_rgItems[nIdx]; }
2862 set { m_rgItems[nIdx] = value; }
2863 }
2864
2869 public IEnumerator<SimpleDatum> GetEnumerator()
2870 {
2871 return (IEnumerator<SimpleDatum>)m_rgItems.GetEnumerator();
2872 }
2873
2874 IEnumerator IEnumerable.GetEnumerator()
2875 {
2876 return m_rgItems.GetEnumerator();
2877 }
2878 }
2879}
Defines a collection of AnnotationGroups.
Definition: Annotation.cs:256
void Add(AnnotationGroupCollection col)
Add another AnnotationGroupCollection to this one.
Definition: Annotation.cs:369
int Count
Specifies the number of items in the collection.
Definition: Annotation.cs:350
The AnnoationGroup class manages a group of annotations.
Definition: Annotation.cs:124
AnnotationGroup Clone()
Create a copy of the annotation group.
Definition: Annotation.cs:145
static AnnotationGroup Load(BinaryReader br)
Load an annotation group using the binary reader.
Definition: Annotation.cs:236
The Bytemap operates similar to a bitmap but is actually just an array of bytes.
Definition: Bytemap.cs:15
int Channels
Specifies the channels of the data.
Definition: Bytemap.cs:141
int Height
Specifies the height of the data.
Definition: Bytemap.cs:149
byte[] Bytes
Specifies the data itself.
Definition: Bytemap.cs:133
int Width
Specifies the width of the data.
Definition: Bytemap.cs:157
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 Datum GetImageData(Bitmap bmp, SimpleDatum sd, bool? bIsDataRealOverride=null, int[] rgFocusMap=null)
The GetImageData function converts a Bitmap into a Datum.
Definition: ImageData.cs:23
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
The ImageTools class is a helper class used to manipulate image data.
Definition: ImageTools.cs:16
static Bitmap ResizeImage(Image image, int width, int height)
Resize the image to the specified width and height.
Definition: ImageTools.cs:39
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
The SimpleDatumCollection holds a named array of SimpleDatums
object Tag
Get/set a user defined value.
IEnumerator< SimpleDatum > GetEnumerator()
Get the enumerator for the collection.
SimpleDatumCollection(int nCount, string strName="", List< int > rgShape=null)
The constructor.
string Name
Get/set the name of the array.
List< int > Shape
Returns the shape of the items within the array (including the array count as the first element).
int Count
Get the number of items in the array.
The SimpleDatum class holds a data input within host memory.
Definition: SimpleDatum.cs:19
string TagName
Returns a user-defined name of the tag associated with the data.
Definition: SimpleDatum.cs:752
static Tuple< double[], float[]> GetRealData(byte[] rgData)
Decodes an array of byte values into a array of either double or float values depending on how the or...
void CopyData(SimpleDatum d)
Copy just the data from another SimpleDatum, making sure to update the C x H x W dimensions and IsRea...
byte[] GetByteData(out bool bEncoded)
Returns the data as a byte array regardless of how it is stored.
void SetImageID(int nID)
Set the image ID.
int OriginalLabel
Get/set the original known label of the data.
static byte[] GetByteData(List< float > rgData)
Encodes a list of float values to an encoded byte array.
void Copy(SimpleDatum d, bool bCopyData, int? nHeight=null, int? nWidth=null)
Copy another SimpleDatum into this one.
float GetDataAtF(int nIdx)
Returns the item at a specified index in the float type.
static T[] PadData< T >(List< T > rgData, int nImagePadX, int nImagePadY, int nHeight, int nWidth, int nChannels)
Padd the data.
bool Sub(SimpleDatum sd, bool bSetNegativeToZero=false)
Subtract the data of another SimpleDatum from this one, so this = this - sd.
Definition: SimpleDatum.cs:906
static byte[] SaveAnnotationDataToDataCriteriaByteArray(ANNOTATION_TYPE type, AnnotationGroupCollection annotations)
Save the annotation data to a byte array.
static double[] Transpose(double[] rg, int nH, int nW, int nDim=1)
Transpose the data within a double array.
static SimpleDatum CalculateMean(Log log, SimpleDatum[] rgImg, WaitHandle[] rgAbort)
Calculate the mean of an array of SimpleDatum and return the mean as a new SimpleDatum.
void Zero()
Zero out all data in the datum but keep the size and other settings.
Definition: SimpleDatum.cs:888
ANNOTATION_TYPE
Specifies the annotation type when using annotations.
Definition: SimpleDatum.cs:60
byte[] DebugData
Get/set debug data associated with the data.
void ResetBoost()
Reset the boost to the original boost.
SimpleDatum(bool bIsReal, int nChannels, int nWidth, int nHeight, int nLabel, DateTime dtTime, List< float > rgfData, int nBoost, bool bAutoLabeled, int nIdx, int nVirtualID=0, int nImageID=0, int nSourceID=0, int nOriginalSourceID=0)
The SimpleDatum constructor.
Definition: SimpleDatum.cs:261
SimpleDatum Div(double dfVal, bool bConvertToByte)
Divides all elements of the SimpleDatum by a value and returns the result as a new SimpleDatum.
SimpleDatum(bool bIsReal, int nChannels, int nWidth, int nHeight, int nLabel, DateTime dtTime, int nBoost=0, bool bAutoLabeled=false, int nIdx=-1, int nVirtualID=0, int nImageID=0, int nSourceID=0, int nOriginalSourceID=0)
The SimpleDatum constructor.
Definition: SimpleDatum.cs:424
SimpleDatum(SimpleDatum d, int nHeight, int nWidth)
The SimpleDatum constructor.
Definition: SimpleDatum.cs:567
List< int > GetNonZeroIndexes()
Returns all indexes with non-zero data.
Definition: SimpleDatum.cs:846
void MaskOutAllButLastColumnsX(int nLastColumsToRetain, int nMaskingValue)
DEPRECIATED: Masks out all data except for the last columns of data.
static byte[] GetByteData(List< double > rgData)
Encodes a list of double values to an encoded byte array.
string ToArrayAsString(int nMaxItems)
Returns a string containing the items of the SimpleDatum.
bool HasRealData
Returns true if either the RealDataD or RealDataF are non null and have length > 0.
bool LoadAnnotationDataFromDataCriteria()
Load the annotation data and type from the data criteria.
void SetLabel(int nLabel)
Sets the label.
int ItemCount
Returns the number of data items.
bool GetDataValid(bool bByType=true)
Returns true if the ByteData or RealDataD or RealDataF are not null, false otherwise.
List< T > ClipToLastColumnsX< T >(int nLastColumns=10)
DEPRECIATED: Clips the SimpleDatum to the last nLastColumns and returns the data.
SimpleDatum(Valuemap data)
The SimpleDatum constructor.
Definition: SimpleDatum.cs:539
T[] GetData< T >(int nImagePadX=0, int nImagePadY=0)
Returns the data as a generic array and optionally pads the data.
int VirtualID
Returns the virtual ID of the SimpleDatum.
SimpleDatum()
The SimpleDatum constructor.
Definition: SimpleDatum.cs:123
void SaveAnnotationDataToDataCriteria()
Save the annotation data and type to the data criteria.
DateTime TimeStamp
Get/set the Timestamp.
SimpleDatum Add(SimpleDatum d)
Creates a new SimpleDatum and adds another SimpleDatum to it.
SimpleDatum(bool bIsReal, int nChannels, int nWidth, int nHeight, int nLabel=-1, DateTime? dtTime=null, int nBoost=0, bool bAutoLabeled=false, int nIdx=-1, int nVirtualID=0, int nImageID=0, int nSourceID=0, int nOriginalSourceID=0)
The SimpleDatum constructor.
Definition: SimpleDatum.cs:143
SimpleDatum(bool bIsReal, int nChannels, int nWidth, int nHeight, int nLabel, DateTime dtTime, double[] rgdfData, int nBoost, bool bAutoLabeled, int nIdx, int nVirtualID=0, int nImageID=0, int nSourceID=0, int nOriginalSourceID=0)
The SimpleDatum constructor.
Definition: SimpleDatum.cs:343
SimpleDatum(Bytemap data)
The SimpleDatum constructor.
Definition: SimpleDatum.cs:523
object Tag
Specifies user data associated with the SimpleDatum.
Definition: SimpleDatum.cs:743
string m_strTagName
Specifies the name of the user value.
Definition: SimpleDatum.cs:54
SimpleDatum(SimpleDatum d, bool bCopyData=false)
The SimpleDatum constructor.
Definition: SimpleDatum.cs:556
void SetData(SimpleDatum d)
Set the data of the current SimpleDatum by copying the data of another.
string Description
Get/set a description of the data.
static byte[] GetByteData(byte[] rgData, int nImagePadX, int nImagePadY, int nHeight, int nWidth, int nChannels)
Return the non-real data as a byte array after padding the data.
SimpleDatum Resize(int nH, int nW)
Resize the data and return it as a new SimpleDatum.
SimpleDatum(int nChannels, int nWidth, int nHeight)
The SimpleDatum constructor.
Definition: SimpleDatum.cs:502
SimpleDatum(bool bIsReal, int nChannels, int nWidth, int nHeight, int nLabel, DateTime dtTime, float[] rgfData, int nBoost, bool bAutoLabeled, int nIdx, int nVirtualID=0, int nImageID=0, int nSourceID=0, int nOriginalSourceID=0)
The SimpleDatum constructor.
Definition: SimpleDatum.cs:384
void ScalePow(double dfScalePow, double dfMin=-double.MaxValue, double dfMax=double.MaxValue)
Scale the values by the scaling factor that raises each value by the specified power.
override string ToString()
Return a string representation of the SimpleDatum.
Bytemap ToBytemap()
Return the SimpleData data as a Bytemap.
static AnnotationGroupCollection LoadAnnotationDataFromDataCriteria(byte[] rg, DATA_FORMAT fmt, out ANNOTATION_TYPE type)
Load the AnnotationGroups from the byte array.
T GetDataAt< T >(int nIdx)
Returns the item at a specified index in the type specified.
int Channels
Return the number of channels of the data.
int OriginalSourceID
Returns the original source ID which is set when using a virtual ID.
static double[] getRealDataD(byte[] rgData, int nIdx)
Decodes an array of byte values into a array of double values.
SimpleDatum(bool bIsReal, int nChannels, int nWidth, int nHeight, int nLabel, DateTime dtTime, List< byte > rgData, int nBoost, bool bAutoLabeled, int nIdx, int nVirtualID=0, int nImageID=0, int nSourceID=0, int nOriginalSourceID=0)
The SimpleDatum constructor.
Definition: SimpleDatum.cs:179
bool IsRealData
Returns whether or not the data contains real numbers or byte data.
AnnotationGroupCollection annotation_group
When using annoations, each annotation group contains an annotation for a particular class used with ...
float[] RealDataF
Return the float data. This field is valid when IsRealData = true.
byte[] DataCriteria
Get/set data criteria associated with the data.
ANNOTATION_TYPE annotation_type
When using annotations, the annotation type specifies the type of annotation. Currently,...
void SetData(List< byte > rgByteData, int nLabel, bool bAllowVirtualOverride=false)
Sets the byte data of the SimpleDatum and its Label.
static bool AccumulateMean(ref double[] rgdfMean, SimpleDatum sd, int nTotal)
Accumulate a portion of a SimpleDatum to calculate the mean value.
DATA_FORMAT
Defines the data format of the DebugData and DataCriteria when specified.
Definition: SimpleDatum.cs:75
double Max
Returns the maximum value in the data or double.NaN if there is no data.
Definition: SimpleDatum.cs:787
int GroupID
Get/set the group ID of the SimpleDatum.
int Boost
Get/set the boost for this data.
object m_tag
Specifies a user value.
Definition: SimpleDatum.cs:50
void Clip(int nDataLen, int? nNewChannel, int? nNewHeight, int? nNewWidth)
Clip the data length down to a smaller size and copies the clipped data.
Definition: SimpleDatum.cs:816
int Width
Return the width of the data.
int HitCount
Get/set the hit count for the SimpleDatum.
Definition: SimpleDatum.cs:734
static List< SimpleDatum > LoadFromPath(string strPath)
Load all SimpleDatums from a directory of files previously stored with SaveInfo.
double GetDataAtD(int nIdx)
Returns the item at a specified index in the double type.
bool AutoLabeled
Get/set whether or not the label was auto generated.
SimpleDatum(bool bIsReal, int nChannels, int nWidth, int nHeight, int nLabel, DateTime dtTime, List< double > rgfData, int nBoost, bool bAutoLabeled, int nIdx, int nVirtualID=0, int nImageID=0, int nSourceID=0, int nOriginalSourceID=0)
The SimpleDatum constructor.
Definition: SimpleDatum.cs:220
void SetData(List< double > rgRealData, int nLabel, bool bAllowVirtualOverride=false)
Sets the double data of the SimpleDatum and its Label.
byte[] ByteData
Return the byte data. This field is valid when IsRealData = false.
int SourceID
Returns the ID of the data source that owns this image.
static float[] getRealDataF(byte[] rgData, int nIdx)
Decodes an array of byte values into a array of float values.
bool SubAbs(SimpleDatum sd)
Subtract the data of another SimpleDatum from this one, and take the absolute value,...
Definition: SimpleDatum.cs:988
static SimpleDatum LoadInfo(string strFile)
Load a SimpleData from text file information previously saved with SaveInfo.
int Index
Returns the index of the SimpleDatum.
DATA_FORMAT DebugDataFormat
Get/set the data format of the debug data.
void ResetLabel()
Resets the label to the original label used when creating the SimpleDatum.
byte GetDataAtByte(int nIdx)
Returns the item at a specified index in the byte type.
int ImageID
Returns the ID of the image in the database.
SimpleDatum(List< SimpleDatum > rg, bool bAlignChannels)
Constructor that copies an array into a single SimpleDatum by appending each to the other in order.
Definition: SimpleDatum.cs:589
int Height
Return the height of the data.
void SetData(float[] rgf, int nLabel)
Set the data to the float array specified.
void SetData(List< float > rgRealData, int nLabel, bool bAllowVirtualOverride=false)
Sets the float data of the SimpleDatum and its Label.
DATA_FORMAT DataCriteriaFormat
Get/set the data format of the data criteria.
void SetData(byte[] rgb, int nLabel)
Set the data to the byte array specified.
double[] RealDataD
Return the double data. This field is valid when IsRealData = true.
void SetData(double[] rgdf, int nLabel)
Set the data to the double array specified.
void Scale(double dfScale, double dfMin=-double.MaxValue, double dfMax=double.MaxValue)
Scale the values by the scaling factor.
static float[] Transpose(float[] rg, int nH, int nW, int nDim=1)
Transpose the data within a float array.
double Min
Returns the minimum value in the data or double.NaN if there is no data.
Definition: SimpleDatum.cs:761
static Tuple< double[], float[]> GetRealData(byte[] rgData, int nImagePadX, int nImagePadY, int nHeight, int nWidth, int nChannels)
Return the real data as a double or float array (depending on the original encoding data type) after ...
int Label
Return the known label of the data.
SimpleDatum(int nChannels, int nWidth, int nHeight, float[] rgf, int nOffset, int nCount, bool bDataIsReal=true)
The SimpleDatum constructor.
Definition: SimpleDatum.cs:456
void SaveInfo(string strFile)
Save the SimpleDatum information to a text file.
SimpleDatum(bool bIsReal, int nChannels, int nWidth, int nHeight, int nLabel, DateTime dtTime, byte[] rgData, int nBoost, bool bAutoLabeled, int nIdx, int nVirtualID=0, int nImageID=0, int nSourceID=0, int nOriginalSourceID=0)
The SimpleDatum constructor.
Definition: SimpleDatum.cs:302
The Utility class provides general utility funtions.
Definition: Utility.cs:35
The Realmap operates similar to a bitmap but is actually just an array of doubles.
Definition: Valuemap.cs:15
double[] Values
Specifies the data itself.
Definition: Valuemap.cs:155
int Channels
Specifies the channels of the data.
Definition: Valuemap.cs:163
int Height
Specifies the height of the data.
Definition: Valuemap.cs:171
int Width
Specifies the width of the data.
Definition: Valuemap.cs:179
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
Definition: Annotation.cs:12
@ CUSTOM
Defines a purely custom training method.
@ NONE
No training category specified.