MyCaffe  1.12.2.41
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.Runtime.Serialization;
10using System.Text;
11using System.Threading;
12
13namespace MyCaffe.basecode
14{
18 [Serializable]
20 {
21 int m_nChannels = 0;
22 int m_nWidth = 0;
23 int m_nHeight = 0;
24 float[] m_rgReadDataF = null;
25
33 public SimpleTemporalDatum(int nChannels, int nWidth, int nHeight, float[] rgData)
34 {
35 m_nChannels = nChannels;
36 m_nWidth = nWidth;
37 m_nHeight = nHeight;
38 m_rgReadDataF = rgData;
39 }
40
44 public float[] Data
45 {
46 get { return m_rgReadDataF; }
47 set { m_rgReadDataF = value; }
48 }
49
53 public int ItemCount
54 {
55 get
56 {
57 return m_rgReadDataF.Length;
58 }
59 }
60
64 public int Channels
65 {
66 get { return m_nChannels; }
67 }
68
72 public int Width
73 {
74 get { return m_nWidth; }
75 }
76
80 public int Height
81 {
82 get { return m_nHeight; }
83 }
84 }
85
89 [Serializable]
91 {
92 List<SimpleTemporalDatum> m_rgItems;
93
97 public SimpleTemporalDatumCollection(int nCapacity)
98 {
99 m_rgItems = new List<SimpleTemporalDatum>(nCapacity);
100 }
101
106 public void Add(SimpleDatum sd)
107 {
108 SimpleTemporalDatum sdt = null;
109
110 if (sd != null)
111 {
112 float[] rgData = sd.RealDataF;
113
114 if (rgData == null && sd.RealDataD != null)
115 {
116 rgData = new float[sd.RealDataD.Length];
117 for (int i = 0; i < rgData.Length; i++)
118 {
119 rgData[i] = (float)sd.RealDataD[i];
120 }
121 }
122
123 sdt = new SimpleTemporalDatum(sd.Channels, sd.Width, sd.Height, rgData);
124 }
125
126 m_rgItems.Add(sdt);
127 }
128
132 public int Count
133 {
134 get { return m_rgItems.Count; }
135 }
136
142 public SimpleTemporalDatum this[int nIdx]
143 {
144 get { return m_rgItems[nIdx]; }
145 }
146
150 public void Clear()
151 {
152 m_rgItems.Clear();
153 }
154 }
155
159 [Serializable]
160 public class SimpleDatum
161 {
162 int m_nIndex = 0;
163 int m_nOriginalLabel = 0;
164 int m_nLabel = 0;
165 int m_nHeight = 0;
166 int m_nWidth = 0;
167 int m_nChannels = 0;
168 byte[] m_rgByteData = null;
169 float[] m_rgRealDataF = null;
170 double[] m_rgRealDataD = null;
171 bool m_bIsRealData = false;
172 DateTime m_dt = DateTime.MinValue;
173 int m_nOriginalBoost = 0;
174 int m_nBoost = 0;
175 bool m_bAutoLabeled = false;
176 int m_nImageID = 0;
177 int m_nVirtualID = 0;
178 int m_nGroupID = 0;
179 byte[] m_rgDataCriteria = null;
180 DATA_FORMAT m_dataCriteriaFormat = DATA_FORMAT.NONE;
181 byte[] m_rgDebugData = null;
182 DATA_FORMAT m_debugDataFormat = DATA_FORMAT.NONE;
183 string m_strDesc = null;
184 int m_nSourceID = 0;
185 int m_nOriginalSourceID = 0;
186 int m_nHitCount = 0;
187 ANNOTATION_TYPE m_nAnnotationType = ANNOTATION_TYPE.NONE;
188 AnnotationGroupCollection m_rgAnnotationGroup = null;
192 protected object m_tag = null;
196 protected string m_strTagName = null;
197
201 [Serializable]
202 [DataContract]
203 public enum ANNOTATION_TYPE
204 {
208 [EnumMember]
209 NONE = -1,
213 [EnumMember]
214 BBOX = 0
215 }
216
220 [Serializable]
221 [DataContract]
222 public enum DATA_FORMAT
223 {
227 [EnumMember]
228 NONE,
232 [EnumMember]
233 IMAGE_1CH,
237 [EnumMember]
238 IMAGE_3CH,
242 [EnumMember]
243 SEGMENTATION,
247 [EnumMember]
248 DICTIONARY,
252 [EnumMember]
253 CUSTOM,
258 [EnumMember]
259 BITMAP,
263 [EnumMember]
264 LIST_DOUBLE,
268 [EnumMember]
269 LIST_FLOAT,
273 [EnumMember]
274 ANNOTATION_DATA
275 }
276
277
281 public SimpleDatum()
282 {
283 }
284
301 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)
302 {
303 m_nChannels = nChannels;
304 m_nWidth = nWidth;
305 m_nHeight = nHeight;
306 m_nOriginalLabel = nLabel;
307 m_nLabel = nLabel;
308 m_dt = dtTime.GetValueOrDefault(DateTime.MinValue);
309 m_nOriginalBoost = nBoost;
310 m_nBoost = nBoost;
311 m_bAutoLabeled = bAutoLabeled;
312 m_nVirtualID = nVirtualID;
313 m_bIsRealData = bIsReal;
314 m_nIndex = nIdx;
315 m_nImageID = nImageID;
316 m_nSourceID = nSourceID;
317 m_nOriginalSourceID = nOriginalSourceID;
318 }
319
337 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)
338 {
339 m_nChannels = nChannels;
340 m_nWidth = nWidth;
341 m_nHeight = nHeight;
342 m_nOriginalLabel = nLabel;
343 m_nLabel = nLabel;
344 m_dt = dtTime;
345 m_nOriginalBoost = nBoost;
346 m_nBoost = nBoost;
347 m_bAutoLabeled = bAutoLabeled;
348 m_nVirtualID = nVirtualID;
349 m_bIsRealData = bIsReal;
350 m_nIndex = nIdx;
351 m_nImageID = nImageID;
352 m_nSourceID = nSourceID;
353 m_nOriginalSourceID = nOriginalSourceID;
354
355 if (bIsReal)
356 throw new ArgumentException("The data sent is not real, but the bIsReal is set to true!");
357
358 m_rgByteData = rgData.ToArray();
359 }
360
378 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)
379 {
380 m_nChannels = nChannels;
381 m_nWidth = nWidth;
382 m_nHeight = nHeight;
383 m_nOriginalLabel = nLabel;
384 m_nLabel = nLabel;
385 m_dt = dtTime;
386 m_nOriginalBoost = nBoost;
387 m_nBoost = nBoost;
388 m_bAutoLabeled = bAutoLabeled;
389 m_nVirtualID = nVirtualID;
390 m_bIsRealData = bIsReal;
391 m_nIndex = nIdx;
392 m_nImageID = nImageID;
393 m_nSourceID = nSourceID;
394 m_nOriginalSourceID = nOriginalSourceID;
395
396 if (!bIsReal)
397 throw new ArgumentException("The data sent is real, but the bIsReal is set to false!");
398
399 m_rgRealDataD = rgfData.ToArray();
400 }
401
419 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)
420 {
421 m_nChannels = nChannels;
422 m_nWidth = nWidth;
423 m_nHeight = nHeight;
424 m_nOriginalLabel = nLabel;
425 m_nLabel = nLabel;
426 m_dt = dtTime;
427 m_nOriginalBoost = nBoost;
428 m_nBoost = nBoost;
429 m_bAutoLabeled = bAutoLabeled;
430 m_nVirtualID = nVirtualID;
431 m_bIsRealData = bIsReal;
432 m_nIndex = nIdx;
433 m_nImageID = nImageID;
434 m_nSourceID = nSourceID;
435 m_nOriginalSourceID = nOriginalSourceID;
436
437 if (!bIsReal)
438 throw new ArgumentException("The data sent is real, but the bIsReal is set to false!");
439
440 m_rgRealDataF = rgfData.ToArray();
441 }
442
460 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)
461 {
462 m_nChannels = nChannels;
463 m_nWidth = nWidth;
464 m_nHeight = nHeight;
465 m_nOriginalLabel = nLabel;
466 m_nLabel = nLabel;
467 m_dt = dtTime;
468 m_nOriginalBoost = nBoost;
469 m_nBoost = nBoost;
470 m_bAutoLabeled = bAutoLabeled;
471 m_nVirtualID = nVirtualID;
472 m_bIsRealData = bIsReal;
473 m_nIndex = nIdx;
474 m_nImageID = nImageID;
475 m_nSourceID = nSourceID;
476 m_nOriginalSourceID = nOriginalSourceID;
477
478 if (bIsReal)
479 throw new ArgumentException("The data sent is not real, but the bIsReal is set to true!");
480
481 m_rgByteData = rgData;
482 }
483
501 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)
502 {
503 m_nChannels = nChannels;
504 m_nWidth = nWidth;
505 m_nHeight = nHeight;
506 m_nOriginalLabel = nLabel;
507 m_nLabel = nLabel;
508 m_dt = dtTime;
509 m_nOriginalBoost = nBoost;
510 m_nBoost = nBoost;
511 m_bAutoLabeled = bAutoLabeled;
512 m_nVirtualID = nVirtualID;
513 m_bIsRealData = bIsReal;
514 m_nIndex = nIdx;
515 m_nImageID = nImageID;
516 m_nSourceID = nSourceID;
517 m_nOriginalSourceID = nOriginalSourceID;
518
519 if (!bIsReal)
520 throw new ArgumentException("The data sent is real, but the bIsReal is set to false!");
521
522 m_rgRealDataD = rgdfData;
523 }
524
542 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)
543 {
544 m_nChannels = nChannels;
545 m_nWidth = nWidth;
546 m_nHeight = nHeight;
547 m_nOriginalLabel = nLabel;
548 m_nLabel = nLabel;
549 m_dt = dtTime;
550 m_nOriginalBoost = nBoost;
551 m_nBoost = nBoost;
552 m_bAutoLabeled = bAutoLabeled;
553 m_nVirtualID = nVirtualID;
554 m_bIsRealData = bIsReal;
555 m_nIndex = nIdx;
556 m_nImageID = nImageID;
557 m_nSourceID = nSourceID;
558 m_nOriginalSourceID = nOriginalSourceID;
559
560 if (!bIsReal)
561 throw new ArgumentException("The data sent is real, but the bIsReal is set to false!");
562
563 m_rgRealDataF = rgfData;
564 }
565
582 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)
583 {
584 m_nChannels = nChannels;
585 m_nWidth = nWidth;
586 m_nHeight = nHeight;
587 m_nOriginalLabel = nLabel;
588 m_nLabel = nLabel;
589 m_dt = dtTime;
590 m_nOriginalBoost = nBoost;
591 m_nBoost = nBoost;
592 m_bAutoLabeled = bAutoLabeled;
593 m_nVirtualID = nVirtualID;
594 m_bIsRealData = bIsReal;
595 m_nIndex = nIdx;
596 m_nImageID = nImageID;
597 m_nSourceID = nSourceID;
598 m_nOriginalSourceID = nOriginalSourceID;
599 m_rgByteData = null;
600 m_rgRealDataD = null;
601 m_rgRealDataF = null;
602 }
603
614 public SimpleDatum(int nChannels, int nWidth, int nHeight, float[] rgf, int nOffset, int nCount, bool bDataIsReal = true)
615 {
616 m_nChannels = nChannels;
617 m_nWidth = nWidth;
618 m_nHeight = nHeight;
619 m_nOriginalLabel = 0;
620 m_nOriginalBoost = 0;
621 m_dt = DateTime.MinValue;
622 m_bAutoLabeled = false;
623 m_nVirtualID = 0;
624 m_nImageID = 0;
625 m_nImageID = 0;
626 m_nSourceID = 0;
627 m_nOriginalSourceID = 0;
628 m_rgByteData = null;
629
630 int nLen = nChannels * nWidth * nHeight;
631 if (nLen != nCount)
632 throw new Exception("The channel x width x height should equal the count!");
633
634 if (bDataIsReal)
635 {
636 m_bIsRealData = true;
637 m_rgRealDataF = new float[nCount];
638
639 if (rgf != null)
640 Array.Copy(rgf, nOffset, m_rgRealDataF, 0, nCount);
641 }
642 else
643 {
644 m_bIsRealData = false;
645 m_rgByteData = new byte[nCount];
646
647 for (int i = 0; i < nCount; i++)
648 {
649 m_rgByteData[i] = Math.Min(Math.Max((byte)rgf[nOffset + i], (byte)0), (byte)255);
650 }
651 }
652 }
653
660 public SimpleDatum(int nChannels, int nWidth, int nHeight)
661 {
662 m_nChannels = nChannels;
663 m_nWidth = nWidth;
664 m_nHeight = nHeight;
665 m_nOriginalLabel = 0;
666 m_nOriginalBoost = 0;
667 m_dt = DateTime.MinValue;
668 m_bAutoLabeled = false;
669 m_nVirtualID = 0;
670 m_nImageID = 0;
671 m_nImageID = 0;
672 m_nSourceID = 0;
673 m_nOriginalSourceID = 0;
674 m_rgByteData = null;
675 }
676
681 public SimpleDatum(Bytemap data)
682 {
683 m_nChannels = data.Channels;
684 m_nWidth = data.Width;
685 m_nHeight = data.Height;
686 m_nOriginalLabel = -1;
687 m_nLabel = -1;
688 m_bIsRealData = false;
689
690 m_rgByteData = data.Bytes;
691 }
692
698 {
699 m_nChannels = data.Channels;
700 m_nWidth = data.Width;
701 m_nHeight = data.Height;
702 m_nOriginalLabel = -1;
703 m_nLabel = -1;
704 m_bIsRealData = true;
705
706 m_rgRealDataD = data.Values;
707 }
708
714 public SimpleDatum(SimpleDatum d, bool bCopyData = false)
715 {
716 Copy(d, bCopyData);
717 }
718
725 public SimpleDatum(SimpleDatum d, int nHeight, int nWidth)
726 {
727 Copy(d, false, nHeight, nWidth);
728 }
729
747 public SimpleDatum(List<SimpleDatum> rg, bool bAlignChannels)
748 {
749 if (rg.Count == 1)
750 {
751 Copy(rg[0], true);
752 return;
753 }
754
755 Copy(rg[0], false);
756
757 m_nChannels *= rg.Count;
758
759 if (bAlignChannels)
760 {
761 if (m_nChannels != rg.Count)
762 throw new Exception("Currently channel alignment is only allowed on single channel data.");
763
764 if (rg.Count >= 1)
765 {
766 if (rg[0].IsRealData)
767 {
768 if (rg[0].RealDataD != null)
769 {
770 double[] rgData = new double[m_nChannels * Height * Width];
771
772 for (int h = 0; h < Height; h++)
773 {
774 for (int w = 0; w < Width; w++)
775 {
776 int nIdxSrc = (h * Width) + w;
777 int nIdxDst = nIdxSrc * m_nChannels;
778
779 for (int c = 0; c < m_nChannels; c++)
780 {
781 rgData[nIdxDst + c] = rg[c].RealDataD[nIdxSrc];
782 }
783 }
784 }
785
786 m_rgRealDataD = rgData;
787 }
788 else if (rg[0].RealDataF != null)
789 {
790 float[] rgData = new float[m_nChannels * Height * Width];
791
792 for (int h = 0; h < Height; h++)
793 {
794 for (int w = 0; w < Width; w++)
795 {
796 int nIdxSrc = (h * Width) + w;
797 int nIdxDst = nIdxSrc * m_nChannels;
798
799 for (int c = 0; c < m_nChannels; c++)
800 {
801 rgData[nIdxDst + c] = rg[c].RealDataF[nIdxSrc];
802 }
803 }
804 }
805
806 m_rgRealDataF = rgData;
807 }
808 else
809 {
810 throw new Exception("SimpleDatum: Both the RealDataD and RealDataF are null!");
811 }
812 m_rgByteData = null;
813 }
814 else
815 {
816 byte[] rgData = new byte[m_nChannels * Height * Width];
817
818 for (int h = 0; h < Height; h++)
819 {
820 for (int w = 0; w < Width; w++)
821 {
822 int nIdxSrc = (h * Width) + w;
823 int nIdxDst = nIdxSrc * m_nChannels;
824
825 for (int c = 0; c < m_nChannels; c++)
826 {
827 rgData[nIdxDst + c] = rg[c].ByteData[nIdxSrc];
828 }
829 }
830 }
831
832 m_rgByteData = rgData;
833 m_rgRealDataF = null;
834 m_rgRealDataD = null;
835 }
836 }
837 }
838 else
839 {
840 bool bReal = rg[0].IsRealData;
841 int nDstIdx = 0;
842 int nItemCount = m_nChannels / rg.Count;
843
844 if (bReal)
845 {
846 if (rg[0].RealDataD != null)
847 {
848 m_rgRealDataD = new double[m_nChannels * Height * Width];
849 m_rgByteData = null;
850
851 for (int i = 0; i < rg.Count; i++)
852 {
853 Array.Copy(rg[i].RealDataD, 0, m_rgRealDataD, nDstIdx, nItemCount);
854 nDstIdx += nItemCount;
855 }
856 }
857 else if (rg[0].RealDataF != null)
858 {
859 m_rgRealDataF = new float[m_nChannels * Height * Width];
860 m_rgByteData = null;
861
862 for (int i = 0; i < rg.Count; i++)
863 {
864 Array.Copy(rg[i].RealDataF, 0, m_rgRealDataF, nDstIdx, nItemCount);
865 nDstIdx += nItemCount;
866 }
867 }
868 else
869 {
870 throw new Exception("SimpleDatum: Both the RealDataD and RealDataF are null!");
871 }
872 }
873 else
874 {
875 m_rgByteData = new byte[m_nChannels * Height * Width];
876 m_rgRealDataD = null;
877 m_rgRealDataF = null;
878
879 for (int i = 0; i < rg.Count; i++)
880 {
881 Array.Copy(rg[i].ByteData, 0, m_rgByteData, nDstIdx, nItemCount);
882 nDstIdx += nItemCount;
883 }
884 }
885 }
886 }
887
891 public int HitCount
892 {
893 get { return m_nHitCount; }
894 set { m_nHitCount = value; }
895 }
896
900 public object Tag
901 {
902 get { return m_tag; }
903 set { m_tag = value; }
904 }
905
909 public string TagName
910 {
911 get { return m_strTagName; }
912 set { m_strTagName = value; }
913 }
914
918 public double Min
919 {
920 get
921 {
922 if (m_bIsRealData)
923 {
924 if (m_rgRealDataD != null)
925 return m_rgRealDataD.Min(p => p);
926 else if (m_rgRealDataF != null)
927 return m_rgRealDataF.Min(p => p);
928 else
929 throw new Exception("SimpleDatum: Both the RealDataD and RealDataF are null!");
930 }
931 else
932 {
933 if (m_rgByteData != null)
934 return (double)m_rgByteData.Min(p => p);
935 }
936
937 return double.NaN;
938 }
939 }
940
944 public double Max
945 {
946 get
947 {
948 if (m_bIsRealData)
949 {
950 if (m_rgRealDataD != null)
951 return m_rgRealDataD.Max(p => p);
952 else if (m_rgRealDataF != null)
953 return m_rgRealDataF.Max(p => p);
954 else
955 throw new Exception("SimpleDatum: Both the RealDataD and RealDataF are null!");
956 }
957 else
958 {
959 if (m_rgByteData != null)
960 return (double)m_rgByteData.Max(p => p);
961 }
962
963 return double.NaN;
964 }
965 }
966
974 public void Clip(int nDataLen, int? nNewChannel, int? nNewHeight, int? nNewWidth)
975 {
976 if (m_rgByteData != null && m_rgByteData.Length > nDataLen)
977 {
978 byte[] rgData = new byte[nDataLen];
979 Array.Copy(m_rgByteData, rgData, nDataLen);
980 m_rgByteData = rgData;
981 }
982 if (m_rgRealDataD != null && m_rgRealDataD.Length > nDataLen)
983 {
984 double[] rgData = new double[nDataLen];
985 Array.Copy(m_rgRealDataD, rgData, nDataLen);
986 m_rgRealDataD = rgData;
987 }
988 if (m_rgRealDataF != null && m_rgRealDataF.Length > nDataLen)
989 {
990 float[] rgData = new float[nDataLen];
991 Array.Copy(m_rgRealDataF, rgData, nDataLen);
992 m_rgRealDataF = rgData;
993 }
994
995 m_nChannels = nNewChannel.GetValueOrDefault(m_nChannels);
996 m_nHeight = nNewHeight.GetValueOrDefault(m_nHeight);
997 m_nWidth = nNewWidth.GetValueOrDefault(m_nWidth);
998 }
999
1004 public List<int> GetNonZeroIndexes()
1005 {
1006 List<int> rgIdx = new List<int>();
1007
1008 if (m_bIsRealData)
1009 {
1010 if (m_rgRealDataD != null)
1011 {
1012 for (int i = 0; i < m_rgRealDataD.Length; i++)
1013 {
1014 if (m_rgRealDataD[i] != 0)
1015 rgIdx.Add(i);
1016 }
1017 }
1018 else if (m_rgRealDataF != null)
1019 {
1020 for (int i = 0; i < m_rgRealDataF.Length; i++)
1021 {
1022 if (m_rgRealDataF[i] != 0)
1023 rgIdx.Add(i);
1024 }
1025 }
1026 else
1027 {
1028 throw new Exception("SimpleDatum: Both the RealDataD and RealDataF are null!");
1029 }
1030 }
1031 else
1032 {
1033 for (int i = 0; i < m_rgByteData.Length; i++)
1034 {
1035 if (m_rgByteData[i] != 0)
1036 rgIdx.Add(i);
1037 }
1038 }
1039
1040 return rgIdx;
1041 }
1042
1046 public void Zero()
1047 {
1048 if (m_rgByteData != null)
1049 Array.Clear(m_rgByteData, 0, m_rgByteData.Length);
1050
1051 if (m_rgRealDataD != null)
1052 Array.Clear(m_rgRealDataD, 0, m_rgRealDataD.Length);
1053
1054 if (m_rgRealDataF != null)
1055 Array.Clear(m_rgRealDataF, 0, m_rgRealDataF.Length);
1056 }
1057
1064 public bool Sub(SimpleDatum sd, bool bSetNegativeToZero = false)
1065 {
1066 bool bDifferent = false;
1067
1068 if (sd.ItemCount != ItemCount)
1069 throw new Exception("Both simple datums must have the same number of elements!");
1070
1071 if (m_rgByteData != null)
1072 {
1073 for (int i = 0; i < m_rgByteData.Length; i++)
1074 {
1075 int nVal = 0;
1076
1077 if (sd.m_rgByteData != null)
1078 nVal = m_rgByteData[i] - sd.m_rgByteData[i];
1079 else if (sd.m_rgRealDataD != null)
1080 nVal = (int)m_rgByteData[i] - (int)sd.m_rgRealDataD[i];
1081 else if (sd.m_rgRealDataF != null)
1082 nVal = (int)m_rgByteData[i] - (int)sd.m_rgRealDataF[i];
1083
1084 if (nVal != 0)
1085 {
1086 bDifferent = true;
1087
1088 if (bSetNegativeToZero && nVal < 0)
1089 nVal = 0;
1090 }
1091
1092 m_rgByteData[i] = (byte)nVal;
1093 }
1094 }
1095
1096 if (m_rgRealDataD != null)
1097 {
1098 for (int i = 0; i < m_rgRealDataD.Length; i++)
1099 {
1100 if (sd.m_rgRealDataD != null)
1101 m_rgRealDataD[i] -= sd.m_rgRealDataD[i];
1102 else if (sd.m_rgRealDataF != null)
1103 m_rgRealDataD[i] -= sd.m_rgRealDataF[i];
1104 else if (sd.m_rgByteData != null)
1105 m_rgRealDataD[i] -= sd.m_rgByteData[i];
1106
1107 if (m_rgRealDataD[i] != 0)
1108 {
1109 bDifferent = true;
1110
1111 if (bSetNegativeToZero && m_rgRealDataD[i] < 0)
1112 m_rgRealDataD[i] = 0;
1113 }
1114 }
1115 }
1116
1117 if (m_rgRealDataF != null)
1118 {
1119 for (int i = 0; i < m_rgRealDataF.Length; i++)
1120 {
1121 if (sd.m_rgRealDataD != null)
1122 m_rgRealDataF[i] -= (float)sd.m_rgRealDataD[i];
1123 else if (sd.m_rgRealDataF != null)
1124 m_rgRealDataF[i] -= sd.m_rgRealDataF[i];
1125 else if (sd.m_rgByteData != null)
1126 m_rgRealDataF[i] -= sd.m_rgByteData[i];
1127
1128 if (m_rgRealDataF[i] != 0)
1129 {
1130 bDifferent = true;
1131
1132 if (bSetNegativeToZero && m_rgRealDataF[i] < 0)
1133 m_rgRealDataF[i] = 0;
1134 }
1135 }
1136 }
1137
1138 return bDifferent;
1139 }
1140
1146 public bool SubAbs(SimpleDatum sd)
1147 {
1148 bool bDifferent = false;
1149
1150 if (sd.ItemCount != ItemCount)
1151 throw new Exception("Both simple datums must have the same number of elements!");
1152
1153 if (m_rgByteData != null)
1154 {
1155 if (sd.m_rgByteData == null)
1156 throw new Exception("Both simple datums must have the same type of data!");
1157
1158 for (int i = 0; i < m_rgByteData.Length; i++)
1159 {
1160 m_rgByteData[i] = (byte)Math.Abs(m_rgByteData[i] - sd.m_rgByteData[i]);
1161 if (m_rgByteData[i] != 0)
1162 bDifferent = true;
1163 }
1164 }
1165
1166 if (m_rgRealDataD != null)
1167 {
1168 if (sd.m_rgRealDataD == null)
1169 throw new Exception("Both simple datums must have the same type of data!");
1170
1171 for (int i = 0; i < m_rgRealDataD.Length; i++)
1172 {
1173 m_rgRealDataD[i] = Math.Abs(m_rgRealDataD[i] - sd.m_rgRealDataD[i]);
1174 if (m_rgRealDataD[i] != 0)
1175 bDifferent = true;
1176 }
1177 }
1178
1179 if (m_rgRealDataF != null)
1180 {
1181 if (sd.m_rgRealDataF == null)
1182 throw new Exception("Both simple datums must have the same type of data!");
1183
1184 for (int i = 0; i < m_rgRealDataF.Length; i++)
1185 {
1186 m_rgRealDataF[i] = Math.Abs(m_rgRealDataF[i] - sd.m_rgRealDataF[i]);
1187 if (m_rgRealDataF[i] != 0)
1188 bDifferent = true;
1189 }
1190 }
1191
1192 return bDifferent;
1193 }
1194
1201 public void Scale(double dfScale, double dfMin = -double.MaxValue, double dfMax = double.MaxValue)
1202 {
1203 if (m_rgByteData != null)
1204 {
1205 for (int i = 0; i < m_rgByteData.Length; i++)
1206 {
1207 int nVal = (int)(m_rgByteData[i] * dfScale);
1208 if (nVal < (int)dfMin)
1209 nVal = (int)dfMin;
1210 else if (nVal > (int)dfMax)
1211 nVal = (int)dfMax;
1212 m_rgByteData[i] = (byte)nVal;
1213 }
1214 }
1215
1216 if (m_rgRealDataD != null)
1217 {
1218 for (int i = 0; i < m_rgRealDataD.Length; i++)
1219 {
1220 m_rgRealDataD[i] *= dfScale;
1221 if (m_rgRealDataD[i] < dfMin)
1222 m_rgRealDataD[i] = dfMin;
1223 else if (m_rgRealDataD[i] > dfMax)
1224 m_rgRealDataD[i] = dfMax;
1225 }
1226 }
1227
1228 if (m_rgRealDataF != null)
1229 {
1230 for (int i = 0; i < m_rgRealDataF.Length; i++)
1231 {
1232 m_rgRealDataF[i] *= (float)dfScale;
1233 if (m_rgRealDataF[i] < dfMin)
1234 m_rgRealDataF[i] = (float)dfMin;
1235 else if (m_rgRealDataF[i] > dfMax)
1236 m_rgRealDataF[i] = (float)dfMax;
1237 }
1238 }
1239 }
1240
1247 public void ScalePow(double dfScalePow, double dfMin = -double.MaxValue, double dfMax = double.MaxValue)
1248 {
1249 if (m_rgByteData != null)
1250 {
1251 for (int i = 0; i < m_rgByteData.Length; i++)
1252 {
1253 int nVal = (int)((double)Math.Pow(m_rgByteData[i], dfScalePow));
1254 if (nVal < (int)dfMin)
1255 nVal = (int)dfMin;
1256 else if (nVal > (int)dfMax)
1257 nVal = (int)dfMax;
1258 m_rgByteData[i] = (byte)nVal;
1259 }
1260 }
1261
1262 if (m_rgRealDataD != null)
1263 {
1264 for (int i = 0; i < m_rgRealDataD.Length; i++)
1265 {
1266 m_rgRealDataD[i] = Math.Pow(m_rgRealDataD[i], dfScalePow);
1267 if (m_rgRealDataD[i] < dfMin)
1268 m_rgRealDataD[i] = dfMin;
1269 else if (m_rgRealDataD[i] > dfMax)
1270 m_rgRealDataD[i] = dfMax;
1271 }
1272 }
1273
1274 if (m_rgRealDataF != null)
1275 {
1276 for (int i = 0; i < m_rgRealDataF.Length; i++)
1277 {
1278 m_rgRealDataF[i] = (float)(Math.Pow(m_rgRealDataF[i], dfScalePow));
1279 if (m_rgRealDataF[i] < dfMin)
1280 m_rgRealDataF[i] = (float)dfMin;
1281 else if (m_rgRealDataF[i] > dfMax)
1282 m_rgRealDataF[i] = (float)dfMax;
1283 }
1284 }
1285 }
1286
1294 public void Copy(SimpleDatum d, bool bCopyData, int? nHeight = null, int? nWidth = null)
1295 {
1296 m_bIsRealData = d.m_bIsRealData;
1297 m_nLabel = d.m_nLabel;
1298 m_nOriginalLabel = d.m_nOriginalLabel;
1299 m_nChannels = d.m_nChannels;
1300 m_nHeight = nHeight.GetValueOrDefault(d.m_nHeight);
1301 m_nWidth = nWidth.GetValueOrDefault(d.m_nWidth);
1302
1303 if (bCopyData)
1304 {
1305 m_rgRealDataD = Utility.Clone<double>(d.m_rgRealDataD);
1306 m_rgRealDataF = Utility.Clone<float>(d.m_rgRealDataF);
1307 m_rgByteData = Utility.Clone<byte>(d.m_rgByteData);
1308 }
1309 else
1310 {
1311 m_rgRealDataD = d.m_rgRealDataD;
1312 m_rgRealDataF = d.m_rgRealDataF;
1313 m_rgByteData = d.m_rgByteData;
1314 }
1315
1316 m_dt = d.m_dt;
1317 m_nOriginalBoost = d.m_nOriginalBoost;
1318 m_nBoost = d.m_nBoost;
1319 m_bAutoLabeled = d.m_bAutoLabeled;
1320 m_nImageID = d.m_nImageID;
1321 m_nVirtualID = d.m_nVirtualID;
1322 m_nGroupID = d.m_nGroupID;
1323 m_nIndex = d.m_nIndex;
1324 m_strDesc = d.m_strDesc;
1325 m_rgDataCriteria = d.m_rgDataCriteria;
1326 m_dataCriteriaFormat = d.m_dataCriteriaFormat;
1327 m_rgDebugData = d.m_rgDebugData;
1328 m_debugDataFormat = d.m_debugDataFormat;
1329 m_nSourceID = d.m_nSourceID;
1330 m_nOriginalSourceID = d.m_nOriginalSourceID;
1331 m_tag = d.m_tag;
1333
1334 m_nAnnotationType = d.m_nAnnotationType;
1335 m_rgAnnotationGroup = null;
1336
1337 if (d.m_rgAnnotationGroup != null)
1338 {
1339 m_rgAnnotationGroup = new AnnotationGroupCollection();
1340
1341 foreach (AnnotationGroup g in d.m_rgAnnotationGroup)
1342 {
1343 m_rgAnnotationGroup.Add(g.Clone());
1344 }
1345 }
1346 }
1347
1352 public void CopyData(SimpleDatum d)
1353 {
1354 m_nHeight = d.m_nHeight;
1355 m_nWidth = d.m_nWidth;
1356 m_nChannels = d.m_nChannels;
1357 m_rgByteData = d.m_rgByteData;
1358 m_bIsRealData = d.m_bIsRealData;
1359 m_rgRealDataD = d.m_rgRealDataD;
1360 m_rgRealDataF = d.m_rgRealDataF;
1361 }
1362
1367 public void SetData(SimpleDatum d)
1368 {
1369 if (d.IsRealData)
1370 {
1371 if (d.RealDataD != null)
1372 SetData(d.RealDataD.ToList(), d.Label);
1373 else if (d.RealDataF != null)
1374 SetData(d.RealDataF.ToList(), d.Label);
1375 else
1376 throw new Exception("SimpleDatum: Both the RealDataD and RealDataF are null!");
1377 }
1378 else
1379 SetData(d.ByteData.ToList(), d.Label);
1380 }
1381
1386 public bool GetDataValid(bool bByType = true)
1387 {
1388 if (bByType)
1389 {
1390 if (m_bIsRealData && m_rgRealDataD == null && m_rgRealDataF == null)
1391 return false;
1392 else if (!m_bIsRealData && m_rgByteData == null)
1393 return false;
1394
1395 return true;
1396 }
1397
1398 if (m_rgRealDataD == null && m_rgRealDataF == null && m_rgByteData == null)
1399 return false;
1400
1401 return true;
1402 }
1403
1410 public List<T> ClipToLastColumnsX<T>(int nLastColumns = 10)
1411 {
1412 int nC = m_nChannels;
1413 int nW = m_nWidth;
1414 int nH = m_nHeight;
1415 int nXStart = nW - nLastColumns;
1416 List<T> rg = new List<T>();
1417
1418 if (nXStart < 0)
1419 nXStart = 0;
1420
1421 for (int c = 0; c < nC; c++)
1422 {
1423 for (int y = 0; y < nH; y++)
1424 {
1425 for (int x = nXStart; x < nW; x++)
1426 {
1427 int nIdxImg = (c * (nH * nW)) + (y * nW) + x;
1428 rg.Add((T)Convert.ChangeType(ByteData[nIdxImg], typeof(T)));
1429 }
1430 }
1431 }
1432
1433 return rg;
1434 }
1435
1441 public void MaskOutAllButLastColumnsX(int nLastColumsToRetain, int nMaskingValue)
1442 {
1443 if (nLastColumsToRetain <= 0 || nLastColumsToRetain >= m_nWidth)
1444 return;
1445
1446 int nC = m_nChannels;
1447 int nW = m_nWidth;
1448 int nH = m_nHeight;
1449 int nMaskWid = nW - nLastColumsToRetain;
1450
1451 for (int c = 0; c < nC; c++)
1452 {
1453 for (int y = 0; y < nH; y++)
1454 {
1455 for (int x = 0; x < nMaskWid; x++)
1456 {
1457 int nIdxImg = (c * (nH * nW)) + (y * nW) + x;
1458 ByteData[nIdxImg] = (byte)nMaskingValue;
1459 }
1460 }
1461 }
1462 }
1463
1470 public void SetData(List<byte> rgByteData, int nLabel, bool bAllowVirtualOverride = false)
1471 {
1472 if (!bAllowVirtualOverride && m_nVirtualID != 0)
1473 throw new Exception("Cannot set the data of a virtual item!");
1474
1475 m_nVirtualID = 0;
1476 m_bIsRealData = false;
1477 m_rgByteData = rgByteData.ToArray();
1478 m_rgRealDataD = null;
1479 m_rgRealDataF = null;
1480 m_nLabel = nLabel;
1481 }
1482
1489 public void SetData(List<double> rgRealData, int nLabel, bool bAllowVirtualOverride = false)
1490 {
1491 if (!bAllowVirtualOverride && m_nVirtualID != 0)
1492 throw new Exception("Cannot set the data of a virtual item!");
1493
1494 m_nVirtualID = 0;
1495 m_bIsRealData = true;
1496 m_rgByteData = null;
1497 m_rgRealDataF = null;
1498 m_rgRealDataD = rgRealData.ToArray();
1499 m_nLabel = nLabel;
1500 }
1501
1508 public void SetData(List<float> rgRealData, int nLabel, bool bAllowVirtualOverride = false)
1509 {
1510 if (!bAllowVirtualOverride && m_nVirtualID != 0)
1511 throw new Exception("Cannot set the data of a virtual item!");
1512
1513 m_nVirtualID = 0;
1514 m_bIsRealData = true;
1515 m_rgByteData = null;
1516 m_rgRealDataD = null;
1517 m_rgRealDataF = rgRealData.ToArray();
1518 m_nLabel = nLabel;
1519 }
1520
1529 public void SetData(byte[] rgb, int nLabel)
1530 {
1531 m_nLabel = nLabel;
1532 m_rgByteData = new byte[rgb.Length];
1533 Array.Copy(rgb, m_rgByteData, rgb.Length);
1534 m_bIsRealData = false;
1535 m_rgRealDataD = null;
1536 m_rgRealDataF = null;
1537 }
1538
1547 public void SetData(double[] rgdf, int nLabel)
1548 {
1549 m_nLabel = nLabel;
1550 m_rgRealDataF = null;
1551 m_rgRealDataD = new double[rgdf.Length];
1552 Array.Copy(rgdf, m_rgRealDataD, rgdf.Length);
1553 m_rgByteData = null;
1554 m_bIsRealData = true;
1555 }
1556
1565 public void SetData(float[] rgf, int nLabel)
1566 {
1567 m_nLabel = nLabel;
1568 m_rgRealDataD = null;
1569 m_rgRealDataF = new float[rgf.Length];
1570 Array.Copy(rgf, m_rgRealDataF, rgf.Length);
1571 m_rgByteData = null;
1572 m_bIsRealData = true;
1573 }
1574
1579 public void SetLabel(int nLabel)
1580 {
1581 m_nLabel = nLabel;
1582 }
1583
1587 public void ResetLabel()
1588 {
1589 m_nLabel = m_nOriginalLabel;
1590 }
1591
1595 public int ItemCount
1596 {
1597 get
1598 {
1599 if (IsRealData)
1600 {
1601 if (m_rgRealDataD != null)
1602 return m_rgRealDataD.Length;
1603 else if (m_rgRealDataF != null)
1604 return m_rgRealDataF.Length;
1605 else
1606 throw new Exception("SimpleDatum: Both the RealDataD and RealDataF are null!");
1607 }
1608 else
1609 return m_rgByteData.Length;
1610 }
1611 }
1612
1616 public bool HasRealData
1617 {
1618 get
1619 {
1620 if (m_rgRealDataD != null && m_rgRealDataD.Length > 0)
1621 return true;
1622
1623 if (m_rgRealDataF != null && m_rgRealDataF.Length > 0)
1624 return true;
1625
1626 return false;
1627 }
1628 }
1629
1636 public T GetDataAt<T>(int nIdx)
1637 {
1638 if (IsRealData)
1639 {
1640 if (m_rgRealDataD != null)
1641 return (T)Convert.ChangeType(m_rgRealDataD[nIdx], typeof(T));
1642 else if (m_rgRealDataF != null)
1643 return (T)Convert.ChangeType(m_rgRealDataF[nIdx], typeof(T));
1644 else
1645 throw new Exception("SimpleDatum: Both the RealDataD and RealDataF are null!");
1646 }
1647 else
1648 {
1649 return (T)Convert.ChangeType(m_rgByteData[nIdx], typeof(T));
1650 }
1651 }
1652
1658 public double GetDataAtD(int nIdx)
1659 {
1660 if (IsRealData)
1661 {
1662 if (m_rgRealDataD != null)
1663 return m_rgRealDataD[nIdx];
1664 else if (m_rgRealDataF != null)
1665 return m_rgRealDataF[nIdx];
1666 else
1667 throw new Exception("SimpleDatum: Both the RealDataD and RealDataF are null!");
1668 }
1669 else
1670 {
1671 return m_rgByteData[nIdx];
1672 }
1673 }
1674
1680 public float GetDataAtF(int nIdx)
1681 {
1682 if (IsRealData)
1683 {
1684 if (m_rgRealDataD != null)
1685 return (float)m_rgRealDataD[nIdx];
1686 else if (m_rgRealDataF != null)
1687 return m_rgRealDataF[nIdx];
1688 else
1689 throw new Exception("SimpleDatum: Both the RealDataD and RealDataF are null!");
1690 }
1691 else
1692 {
1693 return m_rgByteData[nIdx];
1694 }
1695 }
1696
1702 public byte GetDataAtByte(int nIdx)
1703 {
1704 if (IsRealData)
1705 {
1706 double dfVal = 0;
1707
1708 if (m_rgRealDataD != null)
1709 dfVal = m_rgRealDataD[nIdx];
1710 else if (m_rgRealDataF != null)
1711 dfVal = m_rgRealDataF[nIdx];
1712 else
1713 throw new Exception("SimpleDatum: Both the RealDataD and RealDataF are null!");
1714
1715 return (byte)Math.Min(Math.Max(0, dfVal), 255);
1716 }
1717 else
1718 {
1719 return m_rgByteData[nIdx];
1720 }
1721 }
1722
1730 public T[] GetData<T>(int nImagePadX = 0, int nImagePadY = 0)
1731 {
1732 if (IsRealData)
1733 {
1734 if (m_rgRealDataD != null)
1735 {
1736 if (m_rgRealDataD.Length == 0)
1737 return null;
1738
1739 if (typeof(T) == typeof(double))
1740 return (T[])Convert.ChangeType(m_rgRealDataD.ToArray(), typeof(T[]));
1741
1742 T[] rg = new T[m_rgRealDataD.Length];
1743 for (int i = 0; i < rg.Length; i++)
1744 {
1745 rg[i] = (T)Convert.ChangeType(m_rgRealDataD[i], typeof(T));
1746 }
1747
1748 return rg;
1749 }
1750 else if (m_rgRealDataF != null)
1751 {
1752 if (m_rgRealDataF.Length == 0)
1753 return null;
1754
1755 if (typeof(T) == typeof(float))
1756 return (T[])Convert.ChangeType(m_rgRealDataF.ToArray(), typeof(T[]));
1757
1758 T[] rg = new T[m_rgRealDataF.Length];
1759 for (int i = 0; i < rg.Length; i++)
1760 {
1761 rg[i] = (T)Convert.ChangeType(m_rgRealDataF[i], typeof(T));
1762 }
1763
1764 return rg;
1765 }
1766 else
1767 {
1768 return null;
1769 }
1770 }
1771 else
1772 {
1773 if (m_rgByteData == null || m_rgByteData.Length == 0)
1774 return null;
1775
1776 T[] rg = new T[m_rgByteData.Length];
1777
1778 for (int i = 0; i < rg.Length; i++)
1779 {
1780 rg[i] = (T)Convert.ChangeType(m_rgByteData[i], typeof(T));
1781 }
1782
1783 return rg;
1784 }
1785 }
1786
1797 public static byte[] GetByteData(byte[] rgData, int nImagePadX, int nImagePadY, int nHeight, int nWidth, int nChannels)
1798 {
1799 if (nImagePadX == 0 && nImagePadY == 0)
1800 return rgData;
1801
1802 return PadData<byte>(new List<byte>(rgData), nImagePadX, nImagePadY, nHeight, nWidth, nChannels);
1803 }
1804
1815 public static Tuple<double[], float[]> GetRealData(byte[] rgData, int nImagePadX, int nImagePadY, int nHeight, int nWidth, int nChannels)
1816 {
1817 Tuple<double[], float[]> rgRealData = GetRealData(rgData);
1818
1819 double[] rgRealDataD = rgRealData.Item1;
1820 float[] rgRealDataF = rgRealData.Item2;
1821
1822 if (rgRealDataD != null)
1823 rgRealDataD = PadData<double>(rgRealDataD.ToList(), nImagePadX, nImagePadY, nHeight, nWidth, nChannels);
1824 else
1825 rgRealDataF = PadData<float>(rgRealDataF.ToList(), nImagePadX, nImagePadY, nHeight, nWidth, nChannels);
1826
1827 return new Tuple<double[], float[]>(rgRealDataD, rgRealDataF);
1828 }
1829
1841 public static T[] PadData<T>(List<T> rgData, int nImagePadX, int nImagePadY, int nHeight, int nWidth, int nChannels)
1842 {
1843 int nDstIdx = 0;
1844 int nCount = nChannels * (nHeight * (nImagePadX + nWidth) + (nImagePadY * nImagePadX));
1845 T[] rgDataNew = new T[nCount];
1846
1847 for (int c = 0; c < nChannels; c++)
1848 {
1849 for (int y = 0; y < nHeight; y++)
1850 {
1851 for (int i = 0; i < nImagePadX; i++)
1852 {
1853 rgDataNew[nDstIdx] = (T)Convert.ChangeType(0.0, typeof(T));
1854 nDstIdx++;
1855 }
1856
1857 for (int x = 0; x < nWidth; x++)
1858 {
1859 int nIdx = (c * nWidth * nHeight) + (y * nWidth) + x;
1860
1861 rgDataNew[nDstIdx] = rgData[nIdx];
1862 nDstIdx++;
1863 }
1864 }
1865
1866 for (int i = 0; i < nImagePadY; i++)
1867 {
1868 for (int x = 0; x < nWidth + nImagePadX; x++)
1869 {
1870 rgDataNew[nDstIdx] = (T)Convert.ChangeType(0.0, typeof(T));
1871 nDstIdx++;
1872 }
1873 }
1874 }
1875
1876 return rgDataNew;
1877 }
1878
1884 public byte[] GetByteData(out bool bEncoded)
1885 {
1886 if (!IsRealData)
1887 {
1888 bEncoded = false;
1889 return ByteData;
1890 }
1891
1892 bEncoded = true;
1893
1894 if (m_rgRealDataD != null)
1895 return GetByteData(new List<double>(m_rgRealDataD));
1896 else if (m_rgRealDataF != null)
1897 return GetByteData(new List<float>(m_rgRealDataF));
1898 else
1899 throw new Exception("SimpleDatum: Both the RealDataD and RealDataF are null!");
1900 }
1901
1910 public static byte[] GetByteData(List<double> rgData)
1911 {
1912 int nCount = rgData.Count;
1913 int nSize = sizeof(double);
1914
1915 int nOffset = 0;
1916 int nDataCount = sizeof(int) + sizeof(int) + (nSize * rgData.Count);
1917 byte[] rgByte = new byte[nDataCount];
1918
1919 byte[] rg = BitConverter.GetBytes(nCount);
1920 Array.Copy(rg, 0, rgByte, nOffset, rg.Length);
1921 nOffset += rg.Length;
1922
1923 rg = BitConverter.GetBytes(nSize);
1924 Array.Copy(rg, 0, rgByte, nOffset, rg.Length);
1925 nOffset += rg.Length;
1926
1927 foreach (double df in rgData)
1928 {
1929 rg = BitConverter.GetBytes(df);
1930 Array.Copy(rg, 0, rgByte, nOffset, rg.Length);
1931 nOffset += rg.Length;
1932 }
1933
1934 return rgByte;
1935 }
1936
1945 public static byte[] GetByteData(List<float> rgData)
1946 {
1947 int nCount = rgData.Count;
1948 int nSize = sizeof(float);
1949
1950 int nOffset = 0;
1951 int nDataCount = sizeof(int) + sizeof(int) + (nSize * rgData.Count);
1952 byte[] rgByte = new byte[nDataCount];
1953
1954 byte[] rg = BitConverter.GetBytes(nCount);
1955 Array.Copy(rg, 0, rgByte, nOffset, rg.Length);
1956 nOffset += rg.Length;
1957
1958 rg = BitConverter.GetBytes(nSize);
1959 Array.Copy(rg, 0, rgByte, nOffset, rg.Length);
1960 nOffset += rg.Length;
1961
1962 foreach (float df in rgData)
1963 {
1964 rg = BitConverter.GetBytes(df);
1965 Array.Copy(rg, 0, rgByte, nOffset, rg.Length);
1966 nOffset += rg.Length;
1967 }
1968
1969 return rgByte;
1970 }
1971
1977 public static Tuple<double[], float[]> GetRealData(byte[] rgData)
1978 {
1979 double[] rgDataD = null;
1980 float[] rgDataF = null;
1981 int nIdx = 0;
1982
1983 int nCount = BitConverter.ToInt32(rgData, nIdx);
1984 nIdx += 4;
1985 int nSize = BitConverter.ToInt32(rgData, nIdx);
1986 nIdx += 4;
1987
1988 // If the size is invalid, revert back to the legacy double only data.
1989 if (nSize != sizeof(float) && nSize != sizeof(double))
1990 {
1991 nIdx = 0;
1992 nSize = sizeof(double);
1993 }
1994
1995 if (nSize == sizeof(double))
1996 rgDataD = getRealDataD(rgData, nIdx);
1997 else
1998 rgDataF = getRealDataF(rgData, nIdx);
1999
2000 return new Tuple<double[], float[]>(rgDataD, rgDataF);
2001 }
2002
2009 protected static double[] getRealDataD(byte[] rgData, int nIdx)
2010 {
2011 List<double> rgData0 = new List<double>();
2012
2013 while (nIdx < rgData.Length)
2014 {
2015 rgData0.Add(BitConverter.ToDouble(rgData, nIdx));
2016 nIdx += 8;
2017 }
2018
2019 return rgData0.ToArray();
2020 }
2021
2028 protected static float[] getRealDataF(byte[] rgData, int nIdx)
2029 {
2030 List<float> rgData0 = new List<float>();
2031
2032 while (nIdx < rgData.Length)
2033 {
2034 rgData0.Add(BitConverter.ToSingle(rgData, nIdx));
2035 nIdx += 8;
2036 }
2037
2038 return rgData0.ToArray();
2039 }
2040
2047 {
2048 if (m_nChannels != d.Channels ||
2049 m_nHeight != d.Height ||
2050 m_nWidth != d.Width)
2051 throw new Exception("Datum dimmensions do not match!");
2052
2053 if (ItemCount != d.ItemCount)
2054 throw new Exception("Datum counts do not match!");
2055
2056 SimpleDatum d1 = new SimpleDatum(d, false);
2057
2058 d1.m_rgRealDataD = null;
2059 d1.m_rgRealDataF = null;
2060 d1.m_rgByteData = null;
2061
2062 if (m_bIsRealData)
2063 {
2064 if (m_rgRealDataD != null)
2065 {
2066 d1.m_rgRealDataD = new double[m_rgRealDataD.Length];
2067
2068 for (int i = 0; i < m_rgRealDataD.Length; i++)
2069 {
2070 d1.m_rgRealDataD[i] = m_rgRealDataD[i] + d.GetDataAtD(i);
2071 }
2072 }
2073 else if (m_rgRealDataF != null)
2074 {
2075 d1.m_rgRealDataF = new float[m_rgRealDataF.Length];
2076
2077 for (int i = 0; i < m_rgRealDataF.Length; i++)
2078 {
2079 d1.m_rgRealDataF[i] = m_rgRealDataF[i] + d.GetDataAtF(i);
2080 }
2081 }
2082 else
2083 {
2084 throw new Exception("SimpleDatum: Both the RealDataD and RealDataF are null!");
2085 }
2086
2087 d1.m_bIsRealData = true;
2088 }
2089 else
2090 {
2091 d1.m_rgByteData = new byte[m_rgByteData.Length];
2092 d1.m_bIsRealData = false;
2093
2094 for (int i = 0; i < m_rgByteData.Length; i++)
2095 {
2096 int nVal = m_rgByteData[i] + d.GetDataAtByte(i);
2097 d1.m_rgByteData[i] = (byte)Math.Min(Math.Max(0, nVal), 255);
2098 }
2099 }
2100
2101 return d1;
2102 }
2103
2110 public SimpleDatum Div(double dfVal, bool bConvertToByte)
2111 {
2112 if (dfVal == 0)
2113 throw new ArgumentOutOfRangeException("dfVal", 0, "Cannot divide the simple datums by zero!");
2114
2115 SimpleDatum d1 = new SimpleDatum(this, false);
2116
2117 d1.m_rgRealDataD = null;
2118 d1.m_rgRealDataF = null;
2119 d1.m_rgByteData = null;
2120
2121 int nCount = ItemCount;
2122
2123 if (m_bIsRealData && !bConvertToByte)
2124 {
2125 if (m_rgRealDataD != null)
2126 {
2127 d1.m_rgRealDataD = new double[nCount];
2128 d1.m_bIsRealData = true;
2129
2130 for (int i = 0; i < nCount; i++)
2131 {
2132 d1.m_rgRealDataD[i] = m_rgRealDataD[i] / dfVal;
2133 }
2134 }
2135 else if (m_rgRealDataF != null)
2136 {
2137 d1.m_rgRealDataF = new float[nCount];
2138 d1.m_bIsRealData = true;
2139
2140 for (int i = 0; i < nCount; i++)
2141 {
2142 d1.m_rgRealDataF[i] = (float)(m_rgRealDataF[i] / dfVal);
2143 }
2144 }
2145 else
2146 {
2147 throw new Exception("SimpleDatum: Both the RealDataD and RealDataF are null!");
2148 }
2149 }
2150 else if (m_bIsRealData && bConvertToByte)
2151 {
2152 d1.m_rgByteData = new byte[nCount];
2153 d1.m_bIsRealData = false;
2154
2155 if (m_rgRealDataD != null)
2156 {
2157 for (int i = 0; i < nCount; i++)
2158 {
2159 double dfVal1 = m_rgRealDataD[i] / dfVal;
2160 m_rgByteData[i] = (byte)Math.Min(Math.Max(dfVal1, 0), 255);
2161 }
2162 }
2163 else if (m_rgRealDataF != null)
2164 {
2165 for (int i = 0; i < nCount; i++)
2166 {
2167 double dfVal1 = m_rgRealDataF[i] / dfVal;
2168 m_rgByteData[i] = (byte)Math.Min(Math.Max(dfVal1, 0), 255);
2169 }
2170 }
2171 else
2172 {
2173 throw new Exception("SimpleDatum: Both the RealDataD and RealDataF are null!");
2174 }
2175 }
2176 else
2177 {
2178 d1.m_rgByteData = new byte[nCount];
2179 d1.m_bIsRealData = false;
2180
2181 for (int i = 0; i < nCount; i++)
2182 {
2183 double dfVal1 = (double)m_rgByteData[i] / dfVal;
2184 m_rgByteData[i] = (byte)Math.Min(Math.Max(dfVal1, 0), 255);
2185 }
2186 }
2187
2188 return d1;
2189 }
2190
2194 public int SourceID
2195 {
2196 get { return m_nSourceID; }
2197 }
2198
2203 {
2204 get { return m_nOriginalSourceID; }
2205 }
2206
2210 public int ImageID
2211 {
2212 get { return m_nImageID; }
2213 }
2214
2219 public void SetImageID(int nID)
2220 {
2221 m_nImageID = nID;
2222 }
2223
2227 public int VirtualID
2228 {
2229 get { return m_nVirtualID; }
2230 }
2231
2235 public int GroupID
2236 {
2237 get { return m_nGroupID; }
2238 set { m_nGroupID = value; }
2239 }
2240
2244 public int Index
2245 {
2246 get { return m_nIndex; }
2247 set { m_nIndex = value; }
2248 }
2249
2253 public DateTime TimeStamp
2254 {
2255 get { return m_dt; }
2256 set { m_dt = value; }
2257 }
2258
2262 public bool AutoLabeled
2263 {
2264 get { return m_bAutoLabeled; }
2265 set { m_bAutoLabeled = value; }
2266 }
2267
2271 public bool IsRealData
2272 {
2273 get { return m_bIsRealData; }
2274 }
2275
2279 public int Height
2280 {
2281 get { return m_nHeight; }
2282 }
2283
2287 public int Width
2288 {
2289 get { return m_nWidth; }
2290 }
2291
2295 public int Channels
2296 {
2297 get { return m_nChannels; }
2298 }
2299
2303 public int Label
2304 {
2305 get { return m_nLabel; }
2306 }
2307
2311 public int OriginalLabel
2312 {
2313 get { return m_nOriginalLabel; }
2314 set { m_nOriginalLabel = value; }
2315 }
2316
2320 public byte[] ByteData
2321 {
2322 get { return m_rgByteData; }
2323 }
2324
2328 public double[] RealDataD
2329 {
2330 get { return m_rgRealDataD; }
2331 }
2332
2336 public float[] RealDataF
2337 {
2338 get { return m_rgRealDataF; }
2339 }
2340
2344 public int Boost
2345 {
2346 get { return m_nBoost; }
2347 set { m_nBoost = value; }
2348 }
2349
2353 public void ResetBoost()
2354 {
2355 m_nBoost = m_nOriginalBoost;
2356 }
2357
2362 {
2363 get { return m_dataCriteriaFormat; }
2364 set { m_dataCriteriaFormat = value; }
2365 }
2366
2370 public byte[] DataCriteria
2371 {
2372 get { return m_rgDataCriteria; }
2373 set { m_rgDataCriteria = value; }
2374 }
2375
2380 {
2381 get { return m_debugDataFormat; }
2382 set { m_debugDataFormat = value; }
2383 }
2384
2388 public byte[] DebugData
2389 {
2390 get { return m_rgDebugData; }
2391 set { m_rgDebugData = value; }
2392 }
2393
2397 public string Description
2398 {
2399 get { return m_strDesc; }
2400 set { m_strDesc = value; }
2401 }
2402
2408 {
2409 get { return m_nAnnotationType; }
2410 set { m_nAnnotationType = value; }
2411 }
2412
2417 {
2418 get { return m_rgAnnotationGroup; }
2419 set { m_rgAnnotationGroup = value; }
2420 }
2421
2428 public SimpleDatum Resize(int nH, int nW)
2429 {
2430 Image bmp = ImageData.GetImage(new Datum(this));
2431 Bitmap bmpNew = ImageTools.ResizeImage(bmp, nH, nW);
2432 Datum d = ImageData.GetImageData(bmpNew, this);
2433
2434 bmp.Dispose();
2435 bmpNew.Dispose();
2436
2437 return d;
2438 }
2439
2444 public override string ToString()
2445 {
2446 string strOut = "Idx = " + m_nIndex.ToString("N0");
2447
2448 strOut += "; Label = " + m_nLabel.ToString();
2449 strOut += "; Boost = " + m_nBoost.ToString();
2450
2451 if (m_dt != DateTime.MinValue)
2452 strOut += "; Time = " + m_dt.ToString();
2453
2454 if (!string.IsNullOrEmpty(m_strDesc))
2455 strOut += "; Desc = " + m_strDesc;
2456
2457 return strOut;
2458 }
2459
2465 public string ToArrayAsString(int nMaxItems)
2466 {
2467 string str = "";
2468
2469 if (m_bIsRealData)
2470 {
2471 if (m_rgRealDataD != null)
2472 {
2473 for (int i = 0; i < m_rgRealDataD.Length && i < nMaxItems; i++)
2474 {
2475 str += m_rgRealDataD[i].ToString() + ",";
2476 }
2477 }
2478 else if (m_rgRealDataF != null)
2479 {
2480 for (int i = 0; i < m_rgRealDataF.Length && i < nMaxItems; i++)
2481 {
2482 str += m_rgRealDataF[i].ToString() + ",";
2483 }
2484 }
2485 else
2486 {
2487 str = "No Real Data Found!";
2488 }
2489 }
2490 else
2491 {
2492 for (int i = 0; i < m_rgByteData.Length && i < nMaxItems; i++)
2493 {
2494 str += m_rgByteData[i].ToString() + ",";
2495 }
2496 }
2497
2498 return str.TrimEnd(',');
2499 }
2500
2501
2510 {
2511 if (m_rgByteData == null)
2512 throw new Exception("Bytemaps are only supported with byte based data.");
2513
2514 return new Bytemap(m_nChannels, m_nHeight, m_nWidth, m_rgByteData);
2515 }
2516
2524 public static bool AccumulateMean(ref double[] rgdfMean, SimpleDatum sd, int nTotal)
2525 {
2526 if (rgdfMean == null)
2527 rgdfMean = new double[sd.ItemCount];
2528
2529 if (sd.IsRealData)
2530 {
2531 if (sd.RealDataD != null)
2532 {
2533 for (int i = 0; i < sd.ItemCount; i++)
2534 {
2535 rgdfMean[i] += sd.RealDataD[i] / nTotal;
2536 }
2537 }
2538 else if (sd.RealDataF != null)
2539 {
2540 for (int i = 0; i < sd.ItemCount; i++)
2541 {
2542 rgdfMean[i] += sd.RealDataF[i] / nTotal;
2543 }
2544 }
2545 else
2546 {
2547 throw new Exception("SimpleDatum: Both the RealDataD and RealDataF are null!");
2548 }
2549 }
2550 else
2551 {
2552 for (int i = 0; i < sd.ItemCount; i++)
2553 {
2554 rgdfMean[i] += (double)sd.ByteData[i] / (double)nTotal;
2555 }
2556 }
2557
2558 return true;
2559 }
2560
2568 public static SimpleDatum CalculateMean(Log log, SimpleDatum[] rgImg, WaitHandle[] rgAbort)
2569 {
2570 if (rgImg.Length < 2)
2571 throw new Exception("There must be at least 2 images in the simple datum array.");
2572
2573 float[] rgSums;
2574
2575 if (rgImg[0].ByteData != null)
2576 rgSums = new float[rgImg[0].ByteData.Length];
2577 else if (rgImg[0].RealDataD != null)
2578 rgSums = new float[rgImg[0].RealDataD.Length];
2579 else if (rgImg[0].RealDataF != null)
2580 rgSums = new float[rgImg[0].RealDataF.Length];
2581 else
2582 throw new Exception("No data in rgImg[0]!");
2583
2584 Stopwatch sw = new Stopwatch();
2585
2586 try
2587 {
2588 sw.Start();
2589
2590 int nCount = 0;
2591 int nHeight = 0;
2592 int nWidth = 0;
2593 for (int i = 0; i < rgImg.Length && i < 100; i++)
2594 {
2595 nHeight = Math.Max(nHeight, rgImg[i].Height);
2596 nWidth = Math.Max(nWidth, rgImg[i].Width);
2597 }
2598
2599 for (int i = 0; i < rgImg.Length; i++)
2600 {
2601 if (rgImg[i] != null)
2602 {
2603 if (rgImg[i].Height != nHeight || rgImg[i].Width != nWidth)
2604 continue;
2605
2606 if (rgImg[i].ByteData != null)
2607 {
2608 for (int n = 0; n < rgSums.Length; n++)
2609 {
2610 rgSums[n] += rgImg[i].ByteData[n];
2611 }
2612 }
2613 else if (rgImg[i].RealDataD != null)
2614 {
2615 for (int n = 0; n < rgSums.Length; n++)
2616 {
2617 rgSums[n] += (float)rgImg[i].RealDataD[n];
2618 }
2619 }
2620 else if (rgImg[i].RealDataF != null)
2621 {
2622 for (int n = 0; n < rgSums.Length; n++)
2623 {
2624 rgSums[n] += (float)rgImg[i].RealDataF[n];
2625 }
2626 }
2627 else
2628 {
2629 throw new Exception("No data in rgImg[" + i.ToString() + "]!");
2630 }
2631
2632 nCount++;
2633
2634 if (sw.Elapsed.TotalMilliseconds > 2000)
2635 {
2636 double dfPct = (double)i / (double)rgImg.Length;
2637 log.WriteLine("processing mean (" + dfPct.ToString("P") + ")");
2638 sw.Restart();
2639
2640 if (rgAbort != null)
2641 {
2642 if (EventWaitHandle.WaitAny(rgAbort, 0) != EventWaitHandle.WaitTimeout)
2643 return null;
2644 }
2645 }
2646 }
2647 }
2648
2649 if (rgImg[0].ByteData != null)
2650 {
2651 byte[] rgbMean = new byte[rgSums.Length];
2652
2653 for (int n = 0; n < rgSums.Length; n++)
2654 {
2655 rgbMean[n] = (byte)(rgSums[n] / (float)nCount);
2656 }
2657
2658 SimpleDatum d = new SimpleDatum(rgImg[0], false);
2659 d.SetData(new List<byte>(rgbMean), -1, true);
2660 return d;
2661 }
2662 else if (rgImg[0].RealDataD != null)
2663 {
2664 double[] rgdfMean = new double[rgSums.Length];
2665
2666 for (int n = 0; n < rgSums.Length; n++)
2667 {
2668 rgdfMean[n] = (double)rgSums[n] / nCount;
2669 }
2670
2671 SimpleDatum d = new SimpleDatum(rgImg[0], false);
2672 d.SetData(new List<double>(rgdfMean), -1, true);
2673 return d;
2674 }
2675 else if (rgImg[0].RealDataF != null)
2676 {
2677 float[] rgfMean = new float[rgSums.Length];
2678
2679 for (int n = 0; n < rgSums.Length; n++)
2680 {
2681 rgfMean[n] = (float)rgSums[n] / nCount;
2682 }
2683
2684 SimpleDatum d = new SimpleDatum(rgImg[0], false);
2685 d.SetData(new List<float>(rgfMean), -1, true);
2686 return d;
2687 }
2688 else
2689 {
2690 throw new Exception("SimpleDatum: Both the RealDataD and RealDataF are null!");
2691 }
2692 }
2693 finally
2694 {
2695 }
2696 }
2697
2705 {
2706 using (MemoryStream ms = new MemoryStream())
2707 using (BinaryWriter bw = new BinaryWriter(ms))
2708 {
2709 bw.Write((int)type);
2710
2711 int nCount = 0;
2712 if (annotations != null)
2713 nCount = annotations.Count;
2714
2715 bw.Write(nCount);
2716
2717 if (annotations != null)
2718 {
2719 for (int i = 0; i < annotations.Count; i++)
2720 {
2721 annotations[i].Save(bw);
2722 }
2723 }
2724
2725 bw.Flush();
2726 return ms.ToArray();
2727 }
2728 }
2729
2734 {
2735 DataCriteria = SaveAnnotationDataToDataCriteriaByteArray(m_nAnnotationType, m_rgAnnotationGroup);
2736 DataCriteriaFormat = DATA_FORMAT.ANNOTATION_DATA;
2737 }
2738
2747 {
2748 type = ANNOTATION_TYPE.NONE;
2749
2750 if (rg == null || fmt != DATA_FORMAT.ANNOTATION_DATA)
2751 return null;
2752
2753 using (MemoryStream ms = new MemoryStream(rg))
2754 using (BinaryReader br = new BinaryReader(ms))
2755 {
2756 type = (ANNOTATION_TYPE)br.ReadInt32();
2758
2759 int nCount = br.ReadInt32();
2760 if (nCount > 0)
2761 {
2762 for (int i = 0; i < nCount; i++)
2763 {
2764 rgGroups.Add(AnnotationGroup.Load(br));
2765 }
2766 }
2767
2768 return rgGroups;
2769 }
2770 }
2771
2777 {
2778 m_rgAnnotationGroup = LoadAnnotationDataFromDataCriteria(DataCriteria, DataCriteriaFormat, out m_nAnnotationType);
2779
2780 if (m_rgAnnotationGroup == null)
2781 return false;
2782
2783 return true;
2784 }
2785
2793 public void SaveInfo(string strFile)
2794 {
2795 using (StreamWriter sw = new StreamWriter(strFile + ".txt"))
2796 {
2797 sw.WriteLine("Index = " + Index.ToString());
2798 sw.WriteLine("ImageID = " + ImageID.ToString());
2799 sw.WriteLine("VirtualID = " + VirtualID.ToString());
2800 sw.WriteLine("Label = " + Label.ToString());
2801 sw.WriteLine("AutoLabeled = " + AutoLabeled.ToString());
2802 sw.WriteLine("Boost = " + Boost.ToString());
2803 sw.WriteLine("SourceID = " + SourceID.ToString());
2804 sw.WriteLine("OriginalSourceID = " + OriginalSourceID.ToString());
2805 sw.WriteLine("Description = " + Description);
2806 sw.WriteLine("Time = " + TimeStamp.ToString());
2807 sw.WriteLine("Size = {1," + Channels.ToString() + "," + Height.ToString() + "," + Width.ToString() + "}");
2808 }
2809 }
2810
2819 public static SimpleDatum LoadInfo(string strFile)
2820 {
2821 using (StreamReader sr = new StreamReader(strFile))
2822 {
2823 string strVal = parseValue(sr.ReadLine(), '=');
2824 int nIndex = int.Parse(strVal);
2825
2826 strVal = parseValue(sr.ReadLine(), '=');
2827 int nImageID = int.Parse(strVal);
2828
2829 strVal = parseValue(sr.ReadLine(), '=');
2830 int nVirtualID = int.Parse(strVal);
2831
2832 strVal = parseValue(sr.ReadLine(), '=');
2833 int nLabel = int.Parse(strVal);
2834
2835 strVal = parseValue(sr.ReadLine(), '=');
2836 bool bAutoLabeled = bool.Parse(strVal);
2837
2838 strVal = parseValue(sr.ReadLine(), '=');
2839 int nBoost = int.Parse(strVal);
2840
2841 strVal = parseValue(sr.ReadLine(), '=');
2842 int nSrcId = int.Parse(strVal);
2843
2844 strVal = parseValue(sr.ReadLine(), '=');
2845 int nOriginalSrcId = int.Parse(strVal);
2846
2847 strVal = parseValue(sr.ReadLine(), '=');
2848 string strDesc = strVal;
2849
2850 strVal = parseValue(sr.ReadLine(), '=');
2851 DateTime dt = DateTime.Parse(strVal);
2852
2853 strVal = parseValue(sr.ReadLine(), '=');
2854 strVal = strVal.Trim('{', '}');
2855 string[] rgstr = strVal.Split(',');
2856
2857 int nChannels = int.Parse(rgstr[1]);
2858 int nHeight = int.Parse(rgstr[2]);
2859 int nWidth = int.Parse(rgstr[3]);
2860
2861 return new SimpleDatum(false, nChannels, nWidth, nHeight, nLabel, dt, nBoost, bAutoLabeled, nIndex, nVirtualID, nImageID, nSrcId, nOriginalSrcId);
2862 }
2863 }
2864
2865 private static string parseValue(string str, char chDelim)
2866 {
2867 int nPos = str.LastIndexOf(chDelim);
2868 if (nPos < 0)
2869 return str;
2870
2871 return str.Substring(nPos + 1).Trim();
2872 }
2873
2879 public static List<SimpleDatum> LoadFromPath(string strPath)
2880 {
2881 string[] rgstrFiles = Directory.GetFiles(strPath, "*.txt");
2882 List<SimpleDatum> rgData = new List<SimpleDatum>();
2883
2884 foreach (string strFile in rgstrFiles)
2885 {
2886 rgData.Add(SimpleDatum.LoadInfo(strFile));
2887 }
2888
2889 return rgData;
2890 }
2891
2900 public static float[] Transpose(float[] rg, int nH, int nW, int nDim = 1)
2901 {
2902 float[] rgT = new float[rg.Length];
2903
2904 for (int i = 0; i < nH; i++)
2905 {
2906 for (int j = 0; j < nW; j++)
2907 {
2908 int nSrcIdx = (i * nW + j) * nDim;
2909 int nDstIdx = (j * nH + i) * nDim;
2910
2911 if (nDim == 1)
2912 rgT[nDstIdx] = rg[nSrcIdx];
2913 else
2914 Array.Copy(rg, nSrcIdx, rgT, nDstIdx, nDim);
2915 }
2916 }
2917
2918 return rgT;
2919 }
2920
2929 public static double[] Transpose(double[] rg, int nH, int nW, int nDim = 1)
2930 {
2931 double[] rgT = new double[rg.Length];
2932
2933 for (int i = 0; i < nH; i++)
2934 {
2935 for (int j = 0; j < nW; j++)
2936 {
2937 int nSrcIdx = (i * nW + j) * nDim;
2938 int nDstIdx = (j * nH + i) * nDim;
2939
2940 if (nDim == 1)
2941 rgT[nDstIdx] = rg[nSrcIdx];
2942 else
2943 Array.Copy(rg, nSrcIdx, rgT, nDstIdx, nDim);
2944 }
2945 }
2946
2947 return rgT;
2948 }
2949 }
2950
2954 [Serializable]
2955 public class SimpleDatumCollection : IEnumerable<SimpleDatum>
2956 {
2957 string m_strName;
2958 SimpleDatum[] m_rgItems = null;
2959 List<int> m_rgShape;
2960 [NonSerialized]
2961 object m_tag = null;
2962
2969 public SimpleDatumCollection(int nCount, string strName = "", List<int> rgShape = null)
2970 {
2971 m_strName = strName;
2972 m_rgItems = new SimpleDatum[nCount];
2973
2974 m_rgShape = new List<int>() { nCount };
2975
2976 if (rgShape != null)
2977 m_rgShape.AddRange(rgShape);
2978 }
2979
2983 public object Tag
2984 {
2985 get { return m_tag; }
2986 set { m_tag = value; }
2987 }
2988
2992 public List<int> Shape
2993 {
2994 get { return m_rgShape; }
2995 }
2996
3000 public string Name
3001 {
3002 get { return m_strName; }
3003 set { m_strName = value; }
3004 }
3005
3009 public int Count
3010 {
3011 get { return m_rgItems.Count(); }
3012 }
3013
3019 public SimpleDatum this[int nIdx]
3020 {
3021 get { return m_rgItems[nIdx]; }
3022 set { m_rgItems[nIdx] = value; }
3023 }
3024
3029 public IEnumerator<SimpleDatum> GetEnumerator()
3030 {
3031 return (IEnumerator<SimpleDatum>)m_rgItems.GetEnumerator();
3032 }
3033
3034 IEnumerator IEnumerable.GetEnumerator()
3035 {
3036 return m_rgItems.GetEnumerator();
3037 }
3038 }
3039}
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:161
string TagName
Returns a user-defined name of the tag associated with the data.
Definition: SimpleDatum.cs:910
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.
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.
ANNOTATION_TYPE
Specifies the annotation type when using annotations.
Definition: SimpleDatum.cs:204
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:419
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:582
SimpleDatum(SimpleDatum d, int nHeight, int nWidth)
The SimpleDatum constructor.
Definition: SimpleDatum.cs:725
List< int > GetNonZeroIndexes()
Returns all indexes with non-zero data.
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:697
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:281
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:301
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:501
SimpleDatum(Bytemap data)
The SimpleDatum constructor.
Definition: SimpleDatum.cs:681
object Tag
Specifies user data associated with the SimpleDatum.
Definition: SimpleDatum.cs:901
string m_strTagName
Specifies the name of the user value.
Definition: SimpleDatum.cs:196
SimpleDatum(SimpleDatum d, bool bCopyData=false)
The SimpleDatum constructor.
Definition: SimpleDatum.cs:714
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:660
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:542
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:337
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:223
double Max
Returns the maximum value in the data or double.NaN if there is no data.
Definition: SimpleDatum.cs:945
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:192
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:974
int Width
Return the width of the data.
int HitCount
Get/set the hit count for the SimpleDatum.
Definition: SimpleDatum.cs:892
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:378
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,...
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:747
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:919
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:614
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:460
The SimpleTemporalDatumCollection manages a collection of SimpleTemporalDatum objects.
Definition: SimpleDatum.cs:91
SimpleTemporalDatumCollection(int nCapacity)
The constructor.
Definition: SimpleDatum.cs:97
int Count
Returns the number of items in the collection.
Definition: SimpleDatum.cs:133
void Add(SimpleDatum sd)
Add a new datum to the collection (must support the RealDataF data);
Definition: SimpleDatum.cs:106
void Clear()
Clear all items from the list.
Definition: SimpleDatum.cs:150
The SimpleTemporalDatum is used to return temporal data
Definition: SimpleDatum.cs:20
float[] Data
Get/set the data which is of length (Channels * Width * Height).
Definition: SimpleDatum.cs:45
int Height
Get the data height.
Definition: SimpleDatum.cs:81
SimpleTemporalDatum(int nChannels, int nWidth, int nHeight, float[] rgData)
The constructor.
Definition: SimpleDatum.cs:33
int ItemCount
Returns the number of items in the data.
Definition: SimpleDatum.cs:54
int Channels
Get the data channels.
Definition: SimpleDatum.cs:65
int Width
Get the data width.
Definition: SimpleDatum.cs:73
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.