在cntk c#中使用Faster-rcnn?

时间:2018-04-23 22:19:07

标签: c# visual-studio machine-learning cntk

我试图让更快的rcnn模型在C#代码中运行。

我有一个更快的rcnn训练模型,在CNTK python代码中构建和测试以下是我尝试从c#

开始工作 到目前为止,我有:

string testImage = @"C:\data\images\mytestimage.jpg";
Bitmap bitmap = new Bitmap(Bitmap.FromFile(testImage ));
DeviceDescriptor device = DeviceDescriptor.CPUDevice;

Function modelFunc = Function.Load(modelPath, device);
var inDoims = modelFunc.Arguments;
Variable inputVar = modelFunc.Arguments.FirstOrDefault();
Variable inputVar2 = modelFunc.Arguments[1];


NDShape inputShape = inputVar.Shape;
int imageWidth = inputShape[0];

int imageHeight = inputShape[1];
int imageChannels = inputShape[2];
int imageSize = inputShape.TotalSize;

bitmap = ImageProcessing.Resize(bitmap, imageWidth, imageHeight, true);
var pixels = ImageProcessing.ParallelExtractCHW(bitmap);

Variable input = modelFunc.Arguments[0];
var inputDataMap = new Dictionary<Variable, Value>();


List<float> input2Vals = new List<float>();

input2Vals.Add(imageWidth);
input2Vals.Add(imageHeight);
input2Vals.Add(imageWidth);
input2Vals.Add(imageHeight);
input2Vals.Add(imageWidth);
input2Vals.Add(imageHeight);

Value inputVal = Value.CreateBatch(inputVar.Shape, pixels, device);
Value inputValue2 = Value.CreateBatch(inputVar2.Shape, input2Vals, device);

inputDataMap[input] = inputVal;
inputDataMap[inputVar2] = inputValue2;

NDShape outputShape1 = modelFunc.Outputs[0].Shape;
NDShape outputShape2 = modelFunc.Outputs[1].Shape;
NDShape outputShape3 = modelFunc.Outputs[2].Shape;
Value outputValue1 = null;
Value outputValue2 = null; ;
Value outputValue3 = null; ;

var outputDataMap = new Dictionary<Variable, Value>() 
{ 
   { modelFunc.Outputs[0], outputValue1 }, 
   { modelFunc.Outputs[1], outputValue2 }, 
   { modelFunc.Outputs[2], outputValue3 }
};

//run the model
modelFunc.Evaluate(inputDataMap, outputDataMap, device);

var out0 = outputDataMap[modelFunc.Outputs[0]];
var out1 = outputDataMap[modelFunc.Outputs[1]];
var out2 = outputDataMap[modelFunc.Outputs[2]];



var clsPred = out0.GetDenseData<float>(modelFunc.Outputs[0])[0];
var rois = out1.GetDenseData<float>(modelFunc.Outputs[1])[0];
var vbboxR = out2.GetDenseData<float>(modelFunc.Outputs[2])[0];

  var labels = new[] { "__background__", "firstobject", "secondobject", "thirdsobject"};

....

string testImage = @"C:\data\images\mytestimage.jpg"; Bitmap bitmap = new Bitmap(Bitmap.FromFile(testImage )); DeviceDescriptor device = DeviceDescriptor.CPUDevice; Function modelFunc = Function.Load(modelPath, device); var inDoims = modelFunc.Arguments; Variable inputVar = modelFunc.Arguments.FirstOrDefault(); Variable inputVar2 = modelFunc.Arguments[1]; NDShape inputShape = inputVar.Shape; int imageWidth = inputShape[0]; int imageHeight = inputShape[1]; int imageChannels = inputShape[2]; int imageSize = inputShape.TotalSize; bitmap = ImageProcessing.Resize(bitmap, imageWidth, imageHeight, true); var pixels = ImageProcessing.ParallelExtractCHW(bitmap); Variable input = modelFunc.Arguments[0]; var inputDataMap = new Dictionary<Variable, Value>(); List<float> input2Vals = new List<float>(); input2Vals.Add(imageWidth); input2Vals.Add(imageHeight); input2Vals.Add(imageWidth); input2Vals.Add(imageHeight); input2Vals.Add(imageWidth); input2Vals.Add(imageHeight); Value inputVal = Value.CreateBatch(inputVar.Shape, pixels, device); Value inputValue2 = Value.CreateBatch(inputVar2.Shape, input2Vals, device); inputDataMap[input] = inputVal; inputDataMap[inputVar2] = inputValue2; NDShape outputShape1 = modelFunc.Outputs[0].Shape; NDShape outputShape2 = modelFunc.Outputs[1].Shape; NDShape outputShape3 = modelFunc.Outputs[2].Shape; Value outputValue1 = null; Value outputValue2 = null; ; Value outputValue3 = null; ; var outputDataMap = new Dictionary<Variable, Value>() { { modelFunc.Outputs[0], outputValue1 }, { modelFunc.Outputs[1], outputValue2 }, { modelFunc.Outputs[2], outputValue3 } }; //run the model modelFunc.Evaluate(inputDataMap, outputDataMap, device); var out0 = outputDataMap[modelFunc.Outputs[0]]; var out1 = outputDataMap[modelFunc.Outputs[1]]; var out2 = outputDataMap[modelFunc.Outputs[2]]; var clsPred = out0.GetDenseData<float>(modelFunc.Outputs[0])[0]; var rois = out1.GetDenseData<float>(modelFunc.Outputs[1])[0]; var vbboxR = out2.GetDenseData<float>(modelFunc.Outputs[2])[0]; var labels = new[] { "__background__", "firstobject", "secondobject", "thirdsobject"}; ....

但是我在clsPred中找回的结果没有任何意义......例如,当我的测试图像完全空白时......模型似乎认为其中有物体...... 有人有什么建议吗?

巴兹

0 个答案:

没有答案