2using System.Collections.Generic;
26 List<double> m_rgMeanValues =
new List<double>();
29 float[] m_rgfMeanData =
null;
30 double[] m_rgdfMeanData =
null;
33 float[] m_rgfTransformedData =
null;
34 double[] m_rgdfTransformedData =
null;
35 double m_dfLastMin = 0;
36 double m_dfLastMax = 0;
66 int nDataSize = nC * nH * nW;
75 Update(nDataSize, imgMean);
98 nDataSize = imgMean.Channels * imgMean.Height * imgMean.Width;
101 m_protoMean = loadProtoMean(p.
mean_file);
105 if (m_protoMean ==
null)
109 if (m_imgMean !=
null)
111 if (typeof(T) == typeof(
double))
112 m_rgdfMeanData = m_imgMean.GetData<
double>();
114 m_rgfMeanData = m_imgMean.GetData<
float>();
119 if (m_protoMean.
data.Count > 0)
121 if (typeof(T) == typeof(
double))
123 m_rgdfMeanData =
new double[m_protoMean.
data.Count];
124 Array.
Copy(m_protoMean.
data.ToArray(), m_rgdfMeanData, m_rgdfMeanData.Length);
128 m_rgfMeanData =
new float[m_protoMean.
data.Count];
129 Array.Copy(m_protoMean.
data.ToArray(), m_rgfMeanData, m_rgfMeanData.Length);
134 if (typeof(T) == typeof(
double))
135 m_rgdfMeanData = m_protoMean.
double_data.ToArray();
137 m_rgfMeanData = m_protoMean.
double_data.Select(p1 => (
float)p1).ToArray();
144 m_log.CHECK(p.
use_imagedb_mean ==
false,
"Cannot specify use_image_mean and mean_value at the same time.");
175 get {
return m_param; }
183 get {
return m_imgMean; }
187 if (typeof(T) == typeof(
double))
188 m_rgdfMeanData = m_imgMean.GetData<
double>();
190 m_rgfMeanData = m_imgMean.GetData<
float>();
194 private BlobProto loadProtoMean(
string strFile)
198 if (!File.Exists(strFile))
199 throw new Exception(
"Cannot find the file '" + strFile +
"'!");
202 using (FileStream fs =
new FileStream(strFile, FileMode.Open, FileAccess.Read))
204 using (BinaryReader br =
new BinaryReader(fs))
206 rgBytes = br.ReadBytes((
int)fs.Length);
213 catch (Exception excpt)
215 m_log.FAIL(
"Loading Proto Image Mean: " + excpt.Message);
228 int[] rgShape =
null;
230 return new List<int>(rgShape);
244 int nDatumHeight = d.
Height;
245 int nDatumWidth = d.
Width;
248 m_log.CHECK_GT(nDatumChannels, 0,
"There must be 1 or more data channels in the datum.");
254 m_log.CHECK_GE(nDatumHeight, nCropSize,
"The datum height must be >= the crop size of " + nCropSize.ToString() +
". To fix this change the 'crop_size' DataLayer property.");
255 m_log.CHECK_GE(nDatumWidth, nCropSize,
"The datum width must be >= the crop size of " + nCropSize.ToString() +
". To fix this change the 'crop_size' DataLayer property.");
258 if (rgShape ==
null || rgShape.Length != 4)
259 rgShape =
new int[4];
262 rgShape[1] = nDatumChannels;
263 rgShape[2] = (nCropSize > 0) ? nCropSize : nDatumHeight;
264 rgShape[3] = (nCropSize > 0) ? nCropSize : nDatumWidth;
278 int nNum = rgD.Count();
279 m_log.CHECK_GT(nNum, 0,
"There are no datum in the input vector.");
301 m_log.CHECK_GT(nChannels, 0,
"There must be 1 or more data channels in the datum.");
307 m_log.CHECK_GE(nHeight, nCropSize,
"The height must be >= the crop size of " + nCropSize.ToString() +
". To fix this change the 'crop_size' DataLayer property.");
308 m_log.CHECK_GE(nWidth, nCropSize,
"The width must be >= the crop size of " + nCropSize.ToString() +
". To fix this change the 'crop_size' DataLayer property.");
311 List<int> rgShape =
new List<int>();
313 rgShape.Add(nChannels);
314 rgShape.Add((nCropSize > 0) ? nCropSize : nHeight);
315 rgShape.Add((nCropSize > 0) ? nCropSize : nWidth);
336 protected virtual int Rand(
int n)
338 return m_random.
Next(n);
346 get {
return new Tuple<double, double>(m_dfLastMin, m_dfLastMax); }
375 int nDatumNum = rgDatum.Count;
376 int nNum = blobTransformed.
num;
377 int nChannels = blobTransformed.
channels;
378 int nHeight = blobTransformed.
height;
379 int nWidth = blobTransformed.
width;
381 m_log.CHECK_GT(nDatumNum, 0,
"There are no datum to add.");
382 m_log.CHECK_LE(nDatumNum, nNum,
"The size of the rgDatum must be no greater than the transformed blob num.");
384 Blob<T> blobUni =
new Blob<T>(cuda, log, 1, nChannels, nHeight, nWidth,
false);
386 for (
int i = 0; i < nDatumNum; i++)
388 int nOffset = blobTransformed.
offset(i);
390 if (rgDatum[i] !=
null)
412 Transform(rgDatum.ToArray(), blobTransformed, cuda, log, bJustFill);
425 int nDatumNum = rgDatum.Length;
426 int nNum = blobTransformed.
num;
427 int nChannels = blobTransformed.
channels;
428 int nHeight = blobTransformed.
height;
429 int nWidth = blobTransformed.
width;
431 m_log.CHECK_GT(nDatumNum, 0,
"There are no datum to add.");
432 m_log.CHECK_LE(nDatumNum, nNum,
"The size of the rgDatum must be no greater than the transformed blob num.");
434 Blob<T> blobUni =
new Blob<T>(cuda, log, 1, nChannels, nHeight, nWidth,
false);
436 for (
int i = 0; i < nDatumNum; i++)
438 int nOffset = blobTransformed.
offset(i);
440 if (rgDatum[i] !=
null)
467 return Transform(d, blob, out bDoMirror);
481 int nDatumHeight = d.
Height;
482 int nDatumWidth = d.
Width;
486 int nHeight = blob.
height;
487 int nWidth = blob.
width;
490 m_log.CHECK_EQ(nChannels, nDatumChannels,
"The datum and blob must have equal channels.");
491 m_log.CHECK_LE(nHeight, nDatumHeight,
"The datum and blob must have equal height.");
492 m_log.CHECK_LE(nWidth, nDatumWidth,
"The datum and blob must have equal width.");
493 m_log.CHECK_GE(nNum, 1,
"The blob must have at least 1 item.");
497 m_log.CHECK_EQ(nDatumHeight, nHeight,
"The blob height must equal the datum height.");
498 m_log.CHECK_EQ(nDatumWidth, nWidth,
"The blob width must equal the datum width.");
520 if (typeof(T) == typeof(
double))
521 return (T[])Convert.ChangeType(transformD(d, out bMirror, crop_bbox), typeof(T[]));
523 return (T[])Convert.ChangeType(transformF(d, out bMirror, crop_bbox), typeof(T[]));
529 throw new Exception(
"There is no " + ((d.
IsRealData) ?
"REAL" :
"BYTE") +
" data in the SimpleDatum!");
531 m_dfLastMax = -
double.MaxValue;
532 m_dfLastMin =
double.MaxValue;
541 int nDatumHeight = d.
Height;
542 int nDatumWidth = d.
Width;
544 int nHeight = ((nCropSize != 0 && nCropSize < nDatumHeight) ? nCropSize : nDatumHeight);
545 int nWidth = ((nCropSize != 0 && nCropSize < nDatumWidth) ? nCropSize : nDatumWidth);
547 float fScale = (float)m_param.
scale;
553 bool bDoMirror = m_param.
mirror && (
Rand(2) == 1) ?
true :
false;
555 List<float> rgMeanValues =
null;
556 float[] rgMean =
null;
561 m_log.CHECK_GT(nDatumChannels, 0,
"The datum must have at least 1 channel.");
562 m_log.CHECK_GE(nDatumHeight, nCropSize,
"The datum height must be at least as great as the crop size " + nCropSize.ToString());
563 m_log.CHECK_GE(nDatumWidth, nCropSize,
"The datum width must be at least as great as the crop size " + nCropSize.ToString());
567 if (m_rgfMeanData ==
null)
568 m_log.FAIL(
"You must specify an imgMean parameter when using IMAGE mean subtraction.");
570 rgMean = m_rgfMeanData;
572 int nExpected = nDatumChannels * nDatumHeight * nDatumWidth;
573 if (nExpected != rgMean.Length)
575 if (nExpected > rgMean.Length)
576 m_log.WriteLine(
"The size of the 'mean' image is incorrect! Expected '" + nExpected.ToString() +
"' elements, yet loaded '" + rgMean.Length +
"' elements.");
578 m_log.WriteLine(
"WARNING: The size of the 'mean' image is larger than expected! Expected '" + nExpected.ToString() +
"' elements, yet loaded '" + rgMean.Length +
"' elements.");
582 if (m_rgMeanValues.Count > 0)
584 m_log.CHECK(m_rgMeanValues.Count == 1 || m_rgMeanValues.Count == nDatumChannels,
"Specify either 1 mean value or as many as channels: " + nDatumChannels.ToString());
585 rgMeanValues =
new List<float>();
587 for (
int c = 0; c < nDatumChannels; c++)
590 if (c == 0 || m_rgMeanValues.Count == 1)
591 rgMeanValues.Add((
float)m_rgMeanValues[0]);
593 rgMeanValues.Add((
float)m_rgMeanValues[c]);
603 if (m_phase ==
Phase.TRAIN)
605 h_off =
Rand(nDatumHeight - nHeight + 1);
606 w_off =
Rand(nDatumWidth - nWidth + 1);
610 h_off = (nDatumHeight - nHeight) / 2;
611 w_off = (nDatumWidth - nWidth) / 2;
616 if (crop_bbox !=
null)
617 crop_bbox.Set((
float)w_off / nDatumWidth, (float)h_off / nDatumHeight, (
float)(w_off + nWidth) / nDatumWidth, (
float)(h_off + nHeight) / nDatumHeight);
624 float fTransformedElement;
627 int nItemCount = nDatumChannels * nHeight * nWidth;
628 int[] rgChannelSwap =
null;
630 if (m_rgfTransformedData ==
null || m_rgfTransformedData.Length < nItemCount)
631 m_rgfTransformedData =
new float[nItemCount];
634 rgChannelSwap =
new int[] { 2, 1, 0 };
636 for (
int c1 = 0; c1 < nDatumChannels; c1++)
638 int c = (rgChannelSwap ==
null) ? c1 : rgChannelSwap[c1];
640 for (
int h = 0; h < nHeight; h++)
642 for (
int w = 0; w < nWidth; w++)
644 nDataIdx = (c * nDatumHeight + h_off + h) * nDatumWidth + w_off + w;
647 nTopIdx = (c * nHeight + h) * nWidth + (nWidth - 1 - w);
649 nTopIdx = (c * nHeight + h) * nWidth + w;
651 fDataElement = (bIsRealData) ? ((rgfData !=
null) ? rgfData[nDataIdx] : (float)rgdfData[nDataIdx]) : rgbData[nDataIdx];
655 float fVal = fDataElement - rgMean[nDataIdx];
658 fTransformedElement = (fVal < 0) ? fVal : (
float)Math.Pow(fVal, fScale);
660 fTransformedElement = fVal * fScale;
662 else if (rgMeanValues !=
null && rgMeanValues.Count > 0)
664 float fVal = fDataElement - rgMeanValues[c];
667 fTransformedElement = (fVal < 0) ? fVal : (
float)Math.Pow(fVal, fScale);
669 fTransformedElement = fVal * fScale;
674 fTransformedElement = (fDataElement < 0) ? fDataElement : (
float)Math.Pow(fDataElement, fScale);
676 fTransformedElement = fDataElement * fScale;
683 fTransformedElement = 0;
686 if (m_dfLastMax < fTransformedElement)
687 m_dfLastMax = fTransformedElement;
689 if (m_dfLastMin > fTransformedElement)
690 m_dfLastMin = fTransformedElement;
692 m_rgfTransformedData[nTopIdx] = fTransformedElement;
697 return m_rgfTransformedData;
703 throw new Exception(
"There is no " + ((d.
IsRealData) ?
"REAL" :
"BYTE") +
" data in the SimpleDatum!");
705 m_dfLastMax = -
double.MaxValue;
706 m_dfLastMin =
double.MaxValue;
715 int nDatumHeight = d.
Height;
716 int nDatumWidth = d.
Width;
718 int nHeight = ((nCropSize != 0 && nCropSize < nDatumHeight) ? nCropSize : nDatumHeight);
719 int nWidth = ((nCropSize != 0 && nCropSize < nDatumWidth) ? nCropSize : nDatumWidth);
721 double dfScale = m_param.
scale;
727 bool bDoMirror = m_param.
mirror && (
Rand(2) == 1) ?
true :
false;
729 List<double> rgMeanValues =
null;
730 double[] rgMean =
null;
735 m_log.CHECK_GT(nDatumChannels, 0,
"The datum must have at least 1 channel.");
736 m_log.CHECK_GE(nDatumHeight, nCropSize,
"The datum height must be at least as great as the crop size " + nCropSize.ToString());
737 m_log.CHECK_GE(nDatumWidth, nCropSize,
"The datum width must be at least as great as the crop size " + nCropSize.ToString());
741 if (m_rgdfMeanData ==
null)
742 m_log.FAIL(
"You must specify an imgMean parameter when using IMAGE mean subtraction.");
744 rgMean = m_rgdfMeanData;
746 int nExpected = nDatumChannels * nDatumHeight * nDatumWidth;
747 m_log.CHECK_EQ(rgMean.Length, nExpected,
"The size of the 'mean' image is incorrect! Expected '" + nExpected.ToString() +
"' elements, yet loaded '" + rgMean.Length +
"' elements.");
750 if (m_rgMeanValues.Count > 0)
752 m_log.CHECK(m_rgMeanValues.Count == 1 || m_rgMeanValues.Count == nDatumChannels,
"Specify either 1 mean value or as many as channels: " + nDatumChannels.ToString());
753 rgMeanValues =
new List<double>();
755 for (
int c = 0; c < nDatumChannels; c++)
758 if (c == 0 || m_rgMeanValues.Count == 1)
759 rgMeanValues.Add(m_rgMeanValues[0]);
761 rgMeanValues.Add(m_rgMeanValues[c]);
771 if (m_phase ==
Phase.TRAIN)
773 h_off =
Rand(nDatumHeight - nHeight + 1);
774 w_off =
Rand(nDatumWidth - nWidth + 1);
778 h_off = (nDatumHeight - nHeight) / 2;
779 w_off = (nDatumWidth - nWidth) / 2;
784 if (crop_bbox !=
null)
785 crop_bbox.Set((
float)w_off / nDatumWidth, (float)h_off / nDatumHeight, (
float)(w_off + nWidth) / nDatumWidth, (
float)(h_off + nHeight) / nDatumHeight);
791 double dfDataElement;
792 double dfTransformedElement;
795 int nItemCount = nDatumChannels * nHeight * nWidth;
796 int[] rgChannelSwap =
null;
798 if (m_rgdfTransformedData ==
null || m_rgdfTransformedData.Length < nItemCount)
799 m_rgdfTransformedData =
new double[nItemCount];
802 rgChannelSwap =
new int[] { 2, 1, 0 };
804 for (
int c1 = 0; c1 < nDatumChannels; c1++)
806 int c = (rgChannelSwap ==
null) ? c1 : rgChannelSwap[c1];
808 for (
int h = 0; h < nHeight; h++)
810 for (
int w = 0; w < nWidth; w++)
812 nDataIdx = (c * nDatumHeight + h_off + h) * nDatumWidth + w_off + w;
815 nTopIdx = (c * nHeight + h) * nWidth + (nWidth - 1 - w);
817 nTopIdx = (c * nHeight + h) * nWidth + w;
819 dfDataElement = (bIsRealData) ? ((rgdfData !=
null) ? rgdfData[nDataIdx] : rgfData[nDataIdx]) : rgbData[nDataIdx];
823 double dfVal = dfDataElement - rgMean[nDataIdx];
826 dfTransformedElement = (dfVal < 0) ? dfVal : Math.Pow(dfVal, dfScale);
828 dfTransformedElement = (dfDataElement - rgMean[nDataIdx]) * dfScale;
830 else if (rgMeanValues !=
null && rgMeanValues.Count > 0)
832 double dfVal = dfDataElement - rgMeanValues[c];
835 dfTransformedElement = (dfVal < 0) ? dfVal : Math.Pow(dfVal, dfScale);
837 dfTransformedElement = dfVal * dfScale;
842 dfTransformedElement = (dfDataElement < 0) ? dfDataElement : Math.Pow(dfDataElement, dfScale);
844 dfTransformedElement = dfDataElement * dfScale;
851 dfTransformedElement = 0;
854 if (m_dfLastMax < dfTransformedElement)
855 m_dfLastMax = dfTransformedElement;
857 if (m_dfLastMin > dfTransformedElement)
858 m_dfLastMin = dfTransformedElement;
860 m_rgdfTransformedData[nTopIdx] = dfTransformedElement;
865 return m_rgdfTransformedData;
891 T[] rgTrans =
Transform(d, out bMirror, crop_bbox);
910 int nImgWd = d.
Width;
921 bool bHasValidAnnotation =
false;
925 for (
int a = 0; a < anno_group.
annotations.Count; a++)
934 m_log.CHECK_GT(nImgHt, 0,
"The image height must be > 0!");
935 m_log.CHECK_GT(nImgWd, 0,
"The image width must be > 0!");
943 if (m_bbox.
Project(crop_bbox, resize_bbox, out proj_bbox))
945 bHasValidAnnotation =
true;
951 float fTemp = transformed_bbox.
xmin;
952 transformed_bbox.
xmin = 1 - transformed_bbox.
xmax;
953 transformed_bbox.
xmax = 1 - fTemp;
960 transformed_anno_group.
annotations.Add(transformed_anno);
965 if (bHasValidAnnotation)
968 rgTransformedAnnotationGroup.
Add(transformed_anno_group);
975 m_log.FAIL(
"Unknown annotation type.");
978 return rgTransformedAnnotationGroup;
994 double dfScale = (dfNewMax - dfNewMin) / (dfMax - dfMin);
1009 int nDatumHeight = d.
Height;
1010 int nDatumWidth = d.
Width;
1015 int w_off = (int)scaled_bbox.
xmin;
1016 int h_off = (
int)scaled_bbox.
ymin;
1017 int width = (int)(scaled_bbox.
xmax - scaled_bbox.
xmin);
1018 int height = (int)(scaled_bbox.
ymax - scaled_bbox.
ymin);
1022 int nCropDatumSize = nDatumChannels * height * width;
1028 double[] rgData =
new double[nCropDatumSize];
1030 for (
int h = h_off; h < h_off + height; h++)
1032 for (
int w = w_off; w < w_off + width; w++)
1034 for (
int c = 0; c < nDatumChannels; c++)
1036 int nDatumIdx = (c * nDatumHeight + h) * nDatumWidth + w;
1037 int nCropDatumIdx = (c * height + h - h_off) * width + w - w_off;
1038 rgData[nCropDatumIdx] = d.
RealDataD[nDatumIdx];
1047 float[] rgData =
new float[nCropDatumSize];
1049 for (
int h = h_off; h < h_off + height; h++)
1051 for (
int w = w_off; w < w_off + width; w++)
1053 for (
int c = 0; c < nDatumChannels; c++)
1055 int nDatumIdx = (c * nDatumHeight + h) * nDatumWidth + w;
1056 int nCropDatumIdx = (c * height + h - h_off) * width + w - w_off;
1057 rgData[nCropDatumIdx] = d.
RealDataF[nDatumIdx];
1066 throw new Exception(
"SimpleDatum: Both the RealDataD and RealDataF are null!");
1071 byte[] rgData =
new byte[nCropDatumSize];
1073 for (
int h = h_off; h < h_off + height; h++)
1075 for (
int w = w_off; w < w_off + width; w++)
1077 for (
int c = 0; c < nDatumChannels; c++)
1079 int nDatumIdx = (c * nDatumHeight + h) * nDatumWidth + w;
1080 int nCropDatumIdx = (c * height + h - h_off) * width + w - w_off;
1081 rgData[nCropDatumIdx] = d.
ByteData[nDatumIdx];
1102 int nDatumHeight = d.
Height;
1103 int nDatumWidth = d.
Width;
1106 int width = (int)(nDatumWidth * fExpandRatio);
1107 int height = (int)(nDatumHeight * fExpandRatio);
1111 h_off = (float)Math.Floor(h_off);
1112 w_off = (float)Math.Floor(w_off);
1114 expand_bbox.
xmin = -w_off / nDatumWidth;
1115 expand_bbox.
ymin = -h_off / nDatumHeight;
1116 expand_bbox.
xmax = (width - w_off) / nDatumWidth;
1117 expand_bbox.
ymax = (height - h_off) / nDatumHeight;
1121 int nExpandDatumSize = nDatumChannels * height * width;
1127 double[] rgData =
new double[nExpandDatumSize];
1129 for (
int h = (
int)h_off; h < (int)h_off + nDatumHeight; h++)
1131 for (
int w = (
int)w_off; w < (int)w_off + nDatumWidth; w++)
1133 for (
int c = 0; c < nDatumChannels; c++)
1135 int nDatumIdx = (int)((c * nDatumHeight + h - h_off) * nDatumWidth + w - w_off);
1136 int nExpandIdx = (c * height + h) * width + w;
1137 rgData[nExpandIdx] = d.
RealDataD[nDatumIdx];
1146 float[] rgData =
new float[nExpandDatumSize];
1148 for (
int h = (
int)h_off; h < (int)h_off + nDatumHeight; h++)
1150 for (
int w = (
int)w_off; w < (int)w_off + nDatumWidth; w++)
1152 for (
int c = 0; c < nDatumChannels; c++)
1154 int nDatumIdx = (int)((c * nDatumHeight + h - h_off) * nDatumWidth + w - w_off);
1155 int nExpandIdx = (c * height + h) * width + w;
1156 rgData[nExpandIdx] = d.
RealDataF[nDatumIdx];
1165 throw new Exception(
"SimpleDatum: Both the RealDataD and RealDataF are null!");
1170 byte[] rgData =
new byte[nExpandDatumSize];
1172 for (
int h = (
int)h_off; h < (int)h_off + nDatumHeight; h++)
1174 for (
int w = (
int)w_off; w < (int)w_off + nDatumWidth; w++)
1176 for (
int c = 0; c < nDatumChannels; c++)
1178 int nDatumIdx = (int)((c * nDatumHeight + h - h_off) * nDatumWidth + w - w_off);
1179 int nExpandIdx = (c * height + h) * width + w;
1180 rgData[nExpandIdx] = d.
ByteData[nDatumIdx];
1188 return expand_datum;
1191 private float randomValue(
float fMin,
float fMax)
1194 return (fVal * (fMax - fMin)) + fMin;
1210 if (fProb > fExpandProb)
1214 if (Math.Abs(fMaxExpandRatio - 1.0f) < 1e-2)
1217 float fExpandRatio = randomValue(1.0f, fMaxExpandRatio);
1225 bool bMirror =
false;
1226 bool bResize =
false;
1229 return expanded_datum;
1272 if (m_hImageOp == 0)
1305 for (
int c = 0; c < d.
Channels; c++)
1307 for (
int y = 0; y < d.
Height; y++)
1309 for (
int x = 0; x < d.
Width; x++)
1311 int nIdx = c * nDim + y * d.
Width + x;
1313 if (y >= nT && y <= nB && x >= nL && x <= nR)
1349 int nC = rgShape[1];
1350 int nH = rgShape[2];
1351 int nW = rgShape[3];
1354 for (
int c = 0; c < nC; c++)
1356 for (
int y = 0; y < nH; y++)
1358 for (
int x = 0; x < nW; x++)
1360 int nIdx = c * nDim + y * nW + x;
1362 if (y >= nT && y <= nB && x >= nL && x <= nR)
1381 if (typeof(T) == typeof(
double))
1382 return unTransformD(blob, bIncludeMean);
1384 return unTransformF(blob, bIncludeMean);
1387 private Datum unTransformF(
Blob<T> blob,
bool bIncludeMean =
true)
1390 byte[] rgOutput =
new byte[rgData.Length];
1393 int nW = blob.
width;
1394 int[] rgChannelSwap =
null;
1396 List<float> rgMeanValues =
null;
1397 float[] rgMean =
null;
1398 float dfScale = (float)m_param.
scale;
1402 if (m_rgfMeanData ==
null)
1403 m_log.FAIL(
"You must specify an imgMean parameter when using IMAGE mean subtraction.");
1405 rgMean = m_rgfMeanData;
1407 int nExpected = nC * nH * nW;
1408 m_log.CHECK_EQ(rgMean.Length, nExpected,
"The size of the 'mean' image is incorrect! Expected '" + nExpected.ToString() +
"' elements, yet loaded '" + rgMean.Length +
"' elements.");
1411 if (m_rgMeanValues.Count > 0)
1413 m_log.CHECK(m_rgMeanValues.Count == 1 || m_rgMeanValues.Count == nC,
"Specify either 1 mean value or as many as channels: " + nC.ToString());
1414 rgMeanValues =
new List<float>();
1416 for (
int c = 0; c < nC; c++)
1419 if (c == 0 || m_rgMeanValues.Count == 1)
1420 rgMeanValues.Add((
float)m_rgMeanValues[0]);
1422 rgMeanValues.Add((
float)m_rgMeanValues[c]);
1425 rgMean = rgMeanValues.ToArray();
1429 rgChannelSwap =
new int[] { 2, 1, 0 };
1431 for (
int c1 = 0; c1 < nC; c1++)
1433 int c = (rgChannelSwap ==
null) ? c1 : rgChannelSwap[c1];
1435 for (
int h = 0; h < nH; h++)
1437 for (
int w = 0; w < nW; w++)
1439 int nDataIdx = (c * nH + h) * nW + w;
1440 float fVal = (rgData[nDataIdx] / dfScale);
1445 fVal += rgMean[nDataIdx];
1446 else if (rgMean !=
null && rgMean.Length == nC)
1455 int nOutIdx = (c1 * nH + h) * nW + w;
1456 rgOutput[nOutIdx] = (byte)fVal;
1461 return new Datum(
false, nC, nW, nH, -1, DateTime.MinValue, rgOutput.ToList(), 0,
false, -1);
1464 private Datum unTransformD(
Blob<T> blob,
bool bIncludeMean =
true)
1467 byte[] rgOutput =
new byte[rgData.Length];
1470 int nW = blob.
width;
1471 int[] rgChannelSwap =
null;
1473 List<double> rgMeanValues =
null;
1474 double[] rgMean =
null;
1475 double dfScale = m_param.
scale;
1479 if (m_rgdfMeanData ==
null)
1480 m_log.FAIL(
"You must specify an imgMean parameter when using IMAGE mean subtraction.");
1482 rgMean = m_rgdfMeanData;
1484 int nExpected = nC * nH * nW;
1485 m_log.CHECK_EQ(rgMean.Length, nExpected,
"The size of the 'mean' image is incorrect! Expected '" + nExpected.ToString() +
"' elements, yet loaded '" + rgMean.Length +
"' elements.");
1488 if (m_rgMeanValues.Count > 0)
1490 m_log.CHECK(m_rgMeanValues.Count == 1 || m_rgMeanValues.Count == nC,
"Specify either 1 mean value or as many as channels: " + nC.ToString());
1491 rgMeanValues =
new List<double>();
1493 for (
int c = 0; c < nC; c++)
1496 if (c == 0 || m_rgMeanValues.Count == 1)
1497 rgMeanValues.Add(m_rgMeanValues[0]);
1499 rgMeanValues.Add(m_rgMeanValues[c]);
1502 rgMean = rgMeanValues.ToArray();
1506 rgChannelSwap =
new int[] { 2, 1, 0 };
1508 for (
int c1 = 0; c1 < nC; c1++)
1510 int c = (rgChannelSwap ==
null) ? c1 : rgChannelSwap[c1];
1512 for (
int h = 0; h < nH; h++)
1514 for (
int w = 0; w < nW; w++)
1516 int nDataIdx = (c * nH + h) * nW + w;
1517 double dfVal = (rgData[nDataIdx] / dfScale);
1522 dfVal += rgMean[nDataIdx];
1523 else if (rgMean !=
null && rgMean.Length == nC)
1532 int nOutIdx = (c1 * nH + h) * nW + w;
1533 rgOutput[nOutIdx] = (byte)dfVal;
1538 return new Datum(
false, nC, nW, nH, -1, DateTime.MinValue, rgOutput.ToList(), 0,
false, -1);
Defines a collection of AnnotationGroups.
void Add(AnnotationGroupCollection col)
Add another AnnotationGroupCollection to this one.
int Count
Specifies the number of items in the collection.
The AnnoationGroup class manages a group of annotations.
int group_label
Get/set the group label.
List< Annotation > annotations
Get/set the group annoations.
The Annotation class is used by annotations attached to SimpleDatum's and used in SSD.
int instance_id
Get/set the instance ID.
NormalizedBBox bbox
Get/set the bounding box.
The CryptoRandom is a random number generator that can use either the standard .Net Random objec or t...
METHOD
Defines the random number generation method to use.
int Next(int nMinVal, int nMaxVal, bool bMaxInclusive=true)
Returns a random int within the range
double NextDouble()
Returns a random double within the range .
The Datum class is a simple wrapper to the SimpleDatum class to ensure compatibility with the origina...
The Log class provides general output in text form.
The NormalizedBBox manages a bounding box used in SSD.
float ymax
Get/set the y maximum.
float xmax
Get/set the x maximum.
NormalizedBBox Clone()
Return a copy of the object.
float xmin
Get/set the x minimum.
float ymin
Get/set the y minimum.
The SimpleDatum class holds a data input within host memory.
void Copy(SimpleDatum d, bool bCopyData, int? nHeight=null, int? nWidth=null)
Copy another SimpleDatum into this one.
ANNOTATION_TYPE
Specifies the annotation type when using annotations.
void SetLabel(int nLabel)
Sets the label.
bool GetDataValid(bool bByType=true)
Returns true if the ByteData or RealDataD or RealDataF are not null, false otherwise.
void SetData(SimpleDatum d)
Set the data of the current SimpleDatum by copying the data of another.
int Channels
Return the number of channels of the data.
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.
ANNOTATION_TYPE annotation_type
When using annotations, the annotation type specifies the type of annotation. Currently,...
int Boost
Get/set the boost for this data.
int Width
Return the width of the data.
byte[] ByteData
Return the byte data. This field is valid when IsRealData = false.
int Height
Return the height of the data.
double[] RealDataD
Return the double data. This field is valid when IsRealData = true.
int Label
Return the known label of the data.
The Utility class provides general utility funtions.
static double[] ConvertVec(float[] rgf)
Convert an array of float to an array of generics.
The BBox class processes the NormalizedBBox data used with SSD.
NormalizedBBox Clip(NormalizedBBox bbox, float fHeight=1.0f, float fWidth=1.0f)
Clip the BBox to a set range.
bool MeetEmitConstraint(NormalizedBBox src_bbox, NormalizedBBox bbox, EmitConstraint emit_constraint)
Check if a bbox meets the emit constraint w.r.t the src_bbox.
bool Project(NormalizedBBox src, NormalizedBBox bbox, out NormalizedBBox proj_bbox)
Project one bbox onto another.
NormalizedBBox Scale(NormalizedBBox bbox, int nHeight, int nWidth)
Scale the BBox to a set range.
void Extrapolate(ResizeParameter param, int nHeight, int nWidth, NormalizedBBox crop_bbox, NormalizedBBox bbox)
Extrapolate the transformed bbox if height_scale and width_scale are explicitly provied,...
The Blob is the main holder of data that moves through the Layers of the Net.
int channels
DEPRECIATED; legacy shape accessor channels: use shape(1) instead.
void SetData(T[] rgData, int nCount=-1, bool bSetCount=true)
Sets a number of items within the Blob's data.
double min_data
Returns the minimum value in the data of the Blob.
double max_data
Returns the maximum value in the data of the Blob.
int height
DEPRECIATED; legacy shape accessor height: use shape(2) instead.
long mutable_gpu_data
Returns the data GPU handle used by the CudaDnn connection.
T[] mutable_cpu_data
Get data from the GPU and bring it over to the host, or Set data from the Host and send it over to th...
void scale_data(double df)
Scale the data by a scaling factor.
int width
DEPRECIATED; legacy shape accessor width: use shape(3) instead.
void add_scalar(double dfVal)
Adds a scalar value to the Blob.
T[] update_cpu_data()
Update the CPU data by transferring the GPU data over to the Host.
int count()
Returns the total number of items in the Blob.
int offset(int n, int c=0, int h=0, int w=0)
Returns the flat offset given the number, channel, height and width.
virtual void Dispose(bool bDisposing)
Releases all resources used by the Blob (including both GPU and Host).
int num
DEPRECIATED; legacy shape accessor num: use shape(0) instead.
long gpu_data
Returns the data GPU handle used by the CudaDnn connection.
The CudaDnn object is the main interface to the Low-Level Cuda C++ DLL.
void copy(int nCount, long hSrc, long hDst, int nSrcOffset=0, int nDstOffset=0, long hStream=-1, bool? bSrcHalfSizeOverride=null, bool? bDstHalfSizeOverride=null)
Copy data from one block of GPU memory to another.
long CreateImageOp(int nNum, double dfBrightnessProb, double dfBrightnessDelta, double dfContrastProb, double dfContrastLower, double dfContrastUpper, double dfSaturationProb, double dfSaturationLower, double dfSaturationUpper, long lRandomSeed=0)
Create a new ImageOp used to perform image operations on the GPU.
void DistortImage(long h, int nCount, int nNum, int nDim, long hX, long hY)
Distort an image using the ImageOp handle provided.
void FreeImageOp(long h)
Free an image op, freeing up all GPU memory used.
The PersistCaffe class is used to load and save weight files in the .caffemodel format.
BlobProto LoadBlobProto(byte[] rg, int nFieldId)
The LoadBlobProto function loads a BlobProto from a proto buffer.
The BlobProto contains the descripion of a blob.
List< float > data
Get/set the data as a List of float.
List< double > double_data
Get/set the data as a List of double.
int MapLabel(int nLabel, int nBoost)
Queries the mapped label for a given label.
int boundary_bottom
Get/set the mask boundary bottom.
int boundary_left
Get/set the mask boundary left.
int boundary_right
Get/set the mask boundary left.
int boundary_top
Get/set the mask boundary top.
bool Active
When active, the parameter is used, otherwise it is ignored.
float contrast_lower
Get/set lower bound for random contrast factor (default = 0.5).
float brightness_delta
Get/set amount to add to the pixel values within [-delta,delta] (default = 0)
float saturation_prob
Get/set probability of adjusting the saturation (default = 0).
float saturation_lower
Get/set lower bound for random saturation factor (default = 0.5).
bool use_gpu
Get/set whether or not to use the GPU for the distortion operations (default = true).
long random_seed
Get/set the random seed (default = 0, only used when testing).
float saturation_upper
Get/set upper bound for random saturation factor (default = 1.5).
float brightness_prob
Get/set probability of adjusting the brightness (default = 0).
float contrast_prob
Get/set probability of adjusting the contrast (default = 0).
float contrast_upper
Get/set upper bound for random contrast factor (default = 1.5).
float max_expand_ratio
Get/set the ratio to expand the image.
float prob
Get/set probability of using this expansion policy.
uint height
Get/set the resizing height.
uint width
Get/set the resizing width.
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
Phase
Defines the Phase under which to run a Net.
The MyCaffe.common namespace contains common MyCaffe classes.
The MyCaffe.data namespace contains dataset creators used to create common testing datasets such as M...
The MyCaffe.param.ssd namespace contains all SSD related parameter objects that correspond to the nat...
The MyCaffe.param namespace contains parameters used to create models.
The MyCaffe namespace contains the main body of MyCaffe code that closesly tracks the C++ Caffe open-...