6using System.Collections.Generic;
8using System.Drawing.Imaging;
11using System.Threading.Tasks;
29 public class DeepDraw<T> : IDisposable, IEnumerable<Octaves>
52 m_evtCancel = evtCancel;
54 m_transformer = transformer;
57 m_cuda = blobSrc.
Cuda;
60 m_nWid = blobSrc.
width;
63 m_blobBase =
new common.
Blob<T>(m_cuda, m_log,
false);
64 m_blobDetail =
new common.
Blob<T>(m_cuda, m_log,
false);
65 m_blobBlur =
new common.
Blob<T>(m_cuda, m_log,
false);
68 m_blobDetail.ReshapeLike(blobSrc);
69 m_blobBlur.ReshapeLike(blobSrc);
77 if (m_blobBase !=
null)
83 if (m_blobDetail !=
null)
89 if (m_blobBlur !=
null)
101 get {
return m_rgOctaves.Count; }
115 public void Add(
string strLayer,
int nIterations,
double dfStartSigma,
double dfEndSigma,
double dfStartStep,
double dfEndStep,
bool bSaveFile =
false,
double dfPctDetailsToApply = 0.25)
117 m_rgOctaves.Add(
new Octaves(strLayer, nIterations, dfStartSigma, dfEndSigma, dfStartStep, dfEndStep, bSaveFile, dfPctDetailsToApply));
126 m_rgOctaves.Add(octaves);
135 return m_rgOctaves.GetEnumerator();
142 IEnumerator IEnumerable.GetEnumerator()
144 return m_rgOctaves.GetEnumerator();
176 Bitmap bmp =
new Bitmap(nW, nH);
177 Random rand =
new Random();
180 for (
int y = 0; y < nH; y++)
182 for (
int x = 0; x < nW; x++)
184 dfVal = rand.NextDouble() * dfScale;
185 int nR = clrBack.R - (int)((dfScale / 2) + dfVal);
187 dfVal = rand.NextDouble() * dfScale;
188 int nG = clrBack.G - (int)((dfScale / 2) + dfVal);
190 dfVal = rand.NextDouble() * dfScale;
191 int nB = clrBack.B - (int)((dfScale / 2) + dfVal);
193 bmp.SetPixel(x, y, Color.FromArgb(nR, nG, nB));
213 public bool Render(Bitmap bmpInput,
int nFocusLabel = -1,
double dfDetailPercentageToOutput = 0.25,
string strOutputDir =
null,
bool bVisualizeEachStep =
false,
float[] rgDirectInputs =
null)
215 if (rgDirectInputs !=
null && nFocusLabel < 0)
216 throw new Exception(
"The focus label must be set to a unique value >= 0 that corresponds to this specific direct input set.");
221 int nW = blobSrc.
width;
228 if (strOutputDir !=
null)
229 bmpInput.Save(strOutputDir +
"\\input_image.png");
237 for (
int i=0; i<m_rgOctaves.Count; i++)
252 if (nFocusLabel >= 0)
258 make_step(strLayer, dfSigma, dfStepSize, nFocusLabel, rgDirectInputs);
265 if (dfDetailPercentageToOutput < 1.0)
275 Image bmp = getImage(m_blobBlur);
284 if (strOutputDir !=
null)
286 string strFile = strOutputDir +
"\\" + o.
UniqueName +
"_" + j.ToString();
287 if (nFocusLabel >= 0)
289 if (rgDirectInputs !=
null)
290 strFile +=
"_idx_" + nFocusLabel.ToString();
292 strFile +=
"_class_" + nFocusLabel.ToString();
295 bmp.Save(strFile +
".png");
299 o.
Images.Add(nFocusLabel, bmp);
305 m_log.
WriteLine(
"Focus Label: " + nFocusLabel.ToString() +
" Octave: '" + o.
LayerName +
"' - " + j.ToString() +
" of " + o.
IterationN.ToString() +
" " + m_log.
Progress.ToString(
"P"));
307 if (nFocusLabel >= 0)
320 private void make_step(
string strLayer,
double dfSigma,
double dfStepSize = 1.5,
int nFocusLabel = -1,
float[] rgDirectInputs =
null)
329 if (nFocusLabel < 0 && rgDirectInputs ==
null)
333 else if (rgDirectInputs !=
null)
337 for (
int i = 0; i < rgDirectInputs.Length && i < blobDst.
count(); i++)
339 if (rgDirectInputs[i] != 0)
340 blobDst.
SetDiff(rgDirectInputs[i], i);
345 if (nFocusLabel >= blobDst.
count())
346 throw new Exception(
"The focus label '" + nFocusLabel +
"' is greater than the number of outputs for blob '" + blobDst.
Name +
"' -- it only has '" + blobDst.
count().ToString() +
"' outputs!");
349 blobDst.
SetDiff(1.0, nFocusLabel);
356 double dfMean = dfAsum / blobSrc.
count();
357 double dfStep = dfStepSize / dfMean;
368 private Image getImage(
Blob<T> blobSrc)
370 scale(blobSrc, 0.0, 255.0,
true);
374 private void scale(
Blob<T> b,
double dfMin1,
double dfMax1,
bool bByChannel)
378 double dfMinR =
double.MaxValue;
379 double dfMinG =
double.MaxValue;
380 double dfMinB =
double.MaxValue;
381 double dfMaxR = -
double.MaxValue;
382 double dfMaxG = -
double.MaxValue;
383 double dfMaxB = -
double.MaxValue;
385 double dfMeanR = (m_transformer.
param.mean_value.Count > 0) ? m_transformer.
param.mean_value[0] : 0;
386 double dfMeanG = (m_transformer.
param.mean_value.Count > 1) ? m_transformer.
param.mean_value[1] : 0;
387 double dfMeanB = (m_transformer.
param.mean_value.Count > 2) ? m_transformer.
param.mean_value[2] : 0;
391 for (
int i = 0; i < nDim; i++)
393 double dfR = rgdf[i] + dfMeanR;
394 double dfG = rgdf[i + nDim] + dfMeanG;
395 double dfB = rgdf[i + nDim * 2] + dfMeanB;
397 dfMinR = Math.Min(dfR, dfMinR);
398 dfMaxR = Math.Max(dfR, dfMaxR);
399 dfMinG = Math.Min(dfG, dfMinG);
400 dfMaxG = Math.Max(dfG, dfMaxG);
401 dfMinB = Math.Min(dfB, dfMinB);
402 dfMaxB = Math.Max(dfB, dfMaxB);
408 dfMaxR = b.
max_data + Math.Max(dfMeanR, Math.Max(dfMeanG, dfMeanB));
415 double dfRs = (dfMax1 - dfMin1) / (dfMaxR - dfMinR);
416 double dfGs = (dfMax1 - dfMin1) / (dfMaxG - dfMinG);
417 double dfBs = (dfMax1 - dfMin1) / (dfMaxB - dfMinB);
419 for (
int i = 0; i < nDim; i++)
421 double dfR = rgdf[i] + dfMeanR;
422 double dfG = rgdf[i + nDim] + dfMeanG;
423 double dfB = rgdf[i + nDim * 2] + dfMeanB;
425 dfR = (dfR - dfMinR) * dfRs + dfMin1;
426 dfG = (dfG - dfMinG) * dfGs + dfMin1;
427 dfB = (dfB - dfMinB) * dfBs + dfMin1;
430 rgdf[i + nDim] = dfG;
431 rgdf[i + nDim * 2] = dfB;
445 public static Bitmap
AdjustContrast(Image bmp,
float fBrightness = 1.0f,
float fContrast = 1.0f,
float fGamma = 1.0f)
467 rgChildren.
Add(
"output_detail_pct", dfOutputDetailPct.ToString(),
RawProto.
TYPE.STRING);
469 rgChildren.
Add(
"random_image_scale", dfRandomImageScale.ToString(),
RawProto.
TYPE.STRING);
471 foreach (
Octaves octave
in colOctaves)
473 rgChildren.Add(octave.
ToProto(
"octave"));
498 if ((strVal = proto.
FindValue(
"input_height")) !=
null)
499 nHt =
int.
Parse(strVal);
502 if ((strVal = proto.
FindValue(
"input_width")) !=
null)
503 nWd =
int.
Parse(strVal);
505 dfOutputDetailPct = 0.25;
506 if ((strVal = proto.
FindValue(
"output_detail_pct")) !=
null)
509 strSrcBlobName =
"data";
510 if ((strVal = proto.
FindValue(
"src_blob_name")) !=
null)
511 strSrcBlobName = strVal;
513 dfRandomImageScale = 16;
514 if ((strVal = proto.
FindValue(
"random_image_scale")) !=
null)
519 foreach (
RawProto protoChild
in rpcol)
533 string m_strLayerName;
535 double m_dfStartSigma;
537 double m_dfStartStepSize;
538 double m_dfEndStepSize;
539 bool m_bSave =
false;
540 double m_dfDetailPctToApply = 0.25;
541 Dictionary<int, Image> m_rgImages =
new Dictionary<int, Image>();
554 public Octaves(
string strLayerName,
int nIterN,
double dfStartSigma,
double dfEndSigma,
double dfStartStepSize,
double dfEndStepSize,
bool bSaveFile =
false,
double dfPctDetailsToApply = 0.25)
556 m_strLayerName = strLayerName;
558 m_dfStartSigma = dfStartSigma;
559 m_dfEndSigma = dfEndSigma;
560 m_dfStartStepSize = dfStartStepSize;
561 m_dfEndStepSize = dfEndStepSize;
562 m_dfDetailPctToApply = dfPctDetailsToApply;
571 foreach (KeyValuePair<int, Image> kv
in m_rgImages)
573 if (kv.Value !=
null)
585 get {
return m_strLayerName; }
593 get {
return m_nIterN; }
601 get {
return m_dfStartSigma; }
609 get {
return m_dfEndSigma; }
617 get {
return m_dfStartStepSize; }
625 get {
return m_dfEndStepSize; }
633 get {
return m_nIterN.ToString() +
"_" + getCleanName(m_strLayerName); }
641 get {
return m_rgImages; }
642 set { m_rgImages = value; }
650 get {
return m_bSave; }
658 get {
return m_dfDetailPctToApply; }
659 set { m_dfDetailPctToApply = value; }
662 private string getCleanName(
string str)
666 foreach (
char ch
in str)
668 if (!
char.IsSymbol(ch) && ch !=
'\\' && ch !=
'/')
685 rgChildren.
Add(
"iterations", m_nIterN.ToString(),
RawProto.
TYPE.STRING);
686 rgChildren.
Add(
"sigma_start", m_dfStartSigma.ToString(),
RawProto.
TYPE.STRING);
687 rgChildren.
Add(
"sigma_end", m_dfEndSigma.ToString(),
RawProto.
TYPE.STRING);
688 rgChildren.
Add(
"step_start", m_dfStartStepSize.ToString(),
RawProto.
TYPE.STRING);
689 rgChildren.
Add(
"step_end", m_dfEndStepSize.ToString(),
RawProto.
TYPE.STRING);
691 rgChildren.
Add(
"pct_of_prev_detail", m_dfDetailPctToApply.ToString(),
RawProto.
TYPE.STRING);
693 return new RawProto(strName,
"", rgChildren);
705 string strLayer =
"";
706 if ((strVal = rp.
FindValue(
"layer")) !=
null)
709 int nIterations = 10;
710 if ((strVal = rp.
FindValue(
"iterations")) !=
null)
711 nIterations =
int.Parse(strVal);
713 double dfSigmaStart = 0;
714 if ((strVal = rp.
FindValue(
"sigma_start")) !=
null)
717 double dfSigmaEnd = 0;
718 if ((strVal = rp.
FindValue(
"sigma_end")) !=
null)
721 double dfStepStart = 1.5;
722 if ((strVal = rp.
FindValue(
"step_start")) !=
null)
725 double dfStepEnd = 1.5;
726 if ((strVal = rp.
FindValue(
"step_end")) !=
null)
730 if ((strVal = rp.
FindValue(
"save")) !=
null)
731 bSave =
bool.Parse(strVal);
733 double dfPctOfDetail = .25;
734 if ((strVal = rp.
FindValue(
"pct_of_prev_detail")) !=
null)
737 return new Octaves(strLayer, nIterations, dfSigmaStart, dfSigmaEnd, dfStepStart, dfStepEnd, bSave, dfPctOfDetail);
746 List<Octaves> m_rgOctaves =
new List<Octaves>();
760 get {
return m_rgOctaves.Count; }
770 get {
return m_rgOctaves[nIdx]; }
789 return m_rgOctaves.Remove(o);
798 m_rgOctaves.RemoveAt(nIdx);
815 return m_rgOctaves.GetEnumerator();
822 IEnumerator IEnumerable.GetEnumerator()
824 return m_rgOctaves.GetEnumerator();
The BaseParameter class is the base class for all other parameter classes.
static double ParseDouble(string strVal)
Parse double values using the US culture if the decimal separator = '.', then using the native cultur...
The CancelEvent provides an extension to the manual cancel event that allows for overriding the manua...
bool WaitOne(int nMs=int.MaxValue)
Waits for the signal state to occur.
The Datum class is a simple wrapper to the SimpleDatum class to ensure compatibility with the origina...
The ImageData class is a helper class used to convert between Datum, other raw data,...
static Bitmap GetImage(SimpleDatum d, ColorMapper clrMap=null, List< int > rgClrOrder=null)
Converts a SimplDatum (or Datum) into an image, optionally using a ColorMapper.
static Datum GetImageDataD(Bitmap bmp, int nChannels, bool bDataIsReal, int nLabel, bool bUseLockBitmap=true, int[] rgFocusMap=null)
The GetImageDataD function converts a Bitmap into a Datum using the double type for real data.
The Log class provides general output in text form.
void WriteLine(string str, bool bOverrideEnabled=false, bool bHeader=false, bool bError=false, bool bDisable=false)
Write a line of output.
double Progress
Get/set the progress associated with the Log.
The RawProtoCollection class is a list of RawProto objects.
void Add(RawProto p)
Adds a RawProto to the collection.
The RawProto class is used to parse and output Google prototxt file data.
TYPE
Defines the type of a RawProto node.
override string ToString()
Returns the RawProto as its full prototxt string.
static RawProto Parse(string str)
Parses a prototxt and places it in a new RawProto.
string FindValue(string strName)
Searches for a falue of a node within this nodes children.
RawProtoCollection FindChildren(params string[] rgstrName)
Searches for all children with a given name in this node's children.
The Utility class provides general utility funtions.
static double[] ConvertVec(float[] rgf)
Convert an array of float to an array of generics.
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.
Blob(CudaDnn< T > cuda, Log log, bool bIncludeDiff=true, bool bUseHalfSize=false)
The Blob constructor.
long mutable_gpu_diff
Returns the diff GPU handle used by the CudaDnn connection.
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...
T asum_diff()
Compute the sum of absolute values (L1 norm) of the diff.
Log Log
Returns the Log associated with the blob.
void Reshape(int nNum, int nChannels, int nHeight, int nWidth, bool? bUseHalfSize=null)
DEPRECIATED; use
Datum ToDatum()
Returns a new Datum that contains the shape and data of the Blob.
void CopyFrom(Blob< T > src, int nSrcOffset, int nDstOffset, int nCount, bool bCopyData, bool bCopyDiff)
Copy from a source Blob.
int width
DEPRECIATED; legacy shape accessor width: use shape(3) instead.
int count()
Returns the total number of items in the Blob.
void ReshapeLike(Blob< T > b, bool? bUseHalfSize=null)
Reshape this Blob to have the same shape as another Blob.
string Name
Get/set the name of the Blob.
CudaDnn< T > Cuda
Returns the CudaDnn object that manages the Blob's memory."/>
long gpu_diff
Returns the diff GPU handle used by the CudaDnn connection.
virtual void Dispose(bool bDisposing)
Releases all resources used by the Blob (including both GPU and Host).
void SetDiff(double dfVal, int nIdx=-1)
Either sets all of the diff items in the Blob to a given value, or alternatively only sets a single i...
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.
void scal(int n, double fAlpha, long hX, int nXOff=0)
Scales the data in X by a scaling factor.
void sub(int n, long hA, long hB, long hY, int nAOff=0, int nBOff=0, int nYOff=0, int nB=0)
Subtracts B from A and places the result in Y.
void add(int n, long hA, long hB, long hC, long hY)
Adds A, B and C and places the result in Y.
void gaussian_blur(int n, int nChannels, int nHeight, int nWidth, double dfSigma, long hX, long hY)
The gaussian_blur runs a Gaussian blurring operation over each channel of the data using the sigma.
Connects Layer's together into a direct acrylic graph (DAG) specified by a NetParameter
int layer_index_by_name(string strLayer)
Returns a Layer's index given its name.
BlobCollection< T > Forward()
Run forward with the input Blob's already fed separately.
void Backward(int nStart=int.MaxValue, int nEnd=0)
The network backward should take no input and output, since it solely computes the gradient w....
Blob< T > blob_by_name(string strName, bool bThrowExceptionOnError=true)
Returns a blob given its name.
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
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 namespace contains the main body of MyCaffe code that closesly tracks the C++ Caffe open-...