public override float Inference(CudaDeviceVariable <float> input) { _input = input; NPPImage_32fC1 tempConv = new NPPImage_32fC1(_tempConvolution.DevicePointer, InWidth, InHeight, InWidth * sizeof(float)); for (int outLayer = 0; outLayer < OutChannels; outLayer++) { SizeT offsetOut = outLayer * OutWidth * OutHeight * sizeof(float); CUdeviceptr ptrWithOffsetOut = _z.DevicePointer + offsetOut; NPPImage_32fC1 imgOut = new NPPImage_32fC1(ptrWithOffsetOut, OutWidth, OutHeight, OutWidth * sizeof(float)); imgOut.Set(0); for (int inLayer = 0; inLayer < InChannels; inLayer++) { SizeT offsetIn = inLayer * InWidth * InHeight * sizeof(float); CUdeviceptr ptrWithOffsetIn = _input.DevicePointer + offsetIn; NPPImage_32fC1 imgIn = new NPPImage_32fC1(ptrWithOffsetIn, InWidth, InHeight, InWidth * sizeof(float)); imgIn.SetRoi(_filterX / 2, _filterY / 2, InWidth - _filterX + 1, InHeight - _filterY + 1); SizeT offsetFilter = (outLayer * InChannels * _filterX * _filterY + inLayer * _filterX * _filterY) * sizeof(float); CudaDeviceVariable <float> filter = new CudaDeviceVariable <float>(_weights.DevicePointer + offsetFilter, false, _filterX * _filterY * sizeof(float)); imgIn.Filter(tempConv, filter, new NppiSize(_filterX, _filterY), new NppiPoint(_filterX / 2, _filterY / 2)); imgOut.Add(tempConv); } imgOut.Add(bHost[outLayer]); } switch (_activation) { case Activation.None: _y.CopyToDevice(_z); break; case Activation.Relu: //_aRelu is set to 0! _KernelPReluForward.RunSafe(_z, _aRelu, _y, _outWidth * _outHeight, _outChannels, _batch); break; case Activation.PRelu: _KernelPReluForward.RunSafe(_z, _aRelu, _y, _outWidth * _outHeight, _outChannels, _batch); break; case Activation.LeakyRelu: _KernelPReluForward.RunSafe(_z, _aRelu, _y, _outWidth * _outHeight, _outChannels, _batch); break; default: break; } return(_nextLayer.Inference(_y)); }
public void LucasKanade(NPPImage_32fC1 sourceImg, NPPImage_32fC1 targetImg, NPPImage_32fC2 tiledFlow, int tileSize, int tileCountX, int tileCountY, int iterations, float2 baseShift, float baseRotation, float minDet, int windowSize) { createFlowFieldFromTiles.RunSafe(tiledFlow, d_flow, baseShift, baseRotation, tileSize, tileCountX, tileCountY); for (int iter = 0; iter < iterations; iter++) { warpingKernel.RunSafe(sourceImg, d_tmp, d_flow); NppiPoint p = new NppiPoint(0, 0); d_Ix.Set(0); d_Iy.Set(0); d_Iz.Set(0); computeDerivativesKernel.RunSafe(d_tmp, targetImg, d_Ix, d_Iy, d_Iz); lukasKanade.RunSafe(d_flow, d_Ix, d_Iy, d_Iz, minDet, windowSize); } warpingKernel.RunSafe(sourceImg, d_tmp, d_flow); d_tmp.Copy(sourceImg); }
public double4 ScanAngles(NPPImage_32fC1 img, double incr, double range, double zero) { NppiRect saveRoi = new NppiRect(img.PointRoi, img.SizeRoi); NppiRect roi = new NppiRect(); roi.x = 0; roi.y = 0; roi.width = imgToTrackRotated.WidthRoi; roi.height = imgToTrackRotated.HeightRoi; img.SetRoi(roi); double maxVal = -double.MaxValue; double maxAng = 0; double maxX = 0; double maxY = 0; //first perform a coarse search for (double ang = zero - range; ang <= zero + range; ang += 5 * incr) { Matrix3x3 mat = Matrix3x3.RotAroundCenter(ang, imgToTrackRotated.Width, imgToTrackRotated.Height); imgToTrackRotated.Set(0); img.WarpAffine(imgToTrackRotated, mat.ToAffine(), InterpolationMode.Cubic); forward.Exec(imgToTrackRotated.DevicePointerRoi, imgToTrackCplx.DevicePointer); conjKernel.RunSafe(imgRefCplx, imgToTrackCplx); backward.Exec(imgToTrackCplx.DevicePointer, imgToTrackRotated.DevicePointerRoi); imgToTrackRotated.Div(imgToTrackRotated.WidthRoi * imgToTrackRotated.HeightRoi); imgToTrackRotated.MaxIndex(val, x, y); float v = val; int hx = x; int hy = y; //Console.WriteLine("Found Max at " + ang.ToString("0.000") + " deg (" + hx + ", " + hy + ") = " + v); if (v > maxVal) { maxVal = v; maxAng = ang; maxX = x; maxY = y; //Console.WriteLine("Max set!"); } } zero = maxAng; range = 10 * incr; //now perform a fine search but only around the previously found peak for (double ang = zero - range; ang <= zero + range; ang += incr) { Matrix3x3 mat = Matrix3x3.RotAroundCenter(ang, imgToTrackRotated.Width, imgToTrackRotated.Height); imgToTrackRotated.Set(0); img.WarpAffine(imgToTrackRotated, mat.ToAffine(), InterpolationMode.Cubic); int fftWidth = width / 2 + 1; forward.Exec(imgToTrackRotated.DevicePointerRoi, imgToTrackCplx.DevicePointer); conjKernel.RunSafe(imgRefCplx, imgToTrackCplx); backward.Exec(imgToTrackCplx.DevicePointer, imgToTrackRotated.DevicePointerRoi); imgToTrackRotated.Div(imgToTrackRotated.WidthRoi * imgToTrackRotated.HeightRoi); imgToTrackRotated.MaxIndex(val, x, y); float v = val; int hx = x; int hy = y; if (v > maxVal) { maxVal = v; maxAng = ang; maxX = x; maxY = y; //Console.WriteLine("Found Max at " + ang.ToString("0.000") + " deg (" + hx + ", " + hy + ") = " + v); //Console.WriteLine("Max set!"); } } if (maxX > imgToTrackRotated.WidthRoi / 2) { maxX -= imgToTrackRotated.WidthRoi; } if (maxY > imgToTrackRotated.HeightRoi / 2) { maxY -= imgToTrackRotated.HeightRoi; } img.SetRoi(saveRoi); return(new double4(-maxX, -maxY, maxAng, maxVal)); }
public TestRawFile(string dummyFile, int size, float preShiftX, float preShiftY, float preRotDeg, float shiftX, float shiftY) : base(dummyFile) { //close the file Close(); Random rand = new Random(0); mRawImage = new ushort[size, size]; float[] temp = new float[size * size]; NPPImage_32fC1 img1 = new NPPImage_32fC1(size, size); NPPImage_32fC1 img2 = new NPPImage_32fC1(size, size); NPPImage_16uC1 img16u = new NPPImage_16uC1(size, size); for (int i = 0; i < temp.Length; i++) { temp[i] = (float)rand.NextDouble(); } img1.CopyToDevice(temp); img1.FilterGaussBorder(img2, MaskSize.Size_5_X_5, NppiBorderType.Replicate); img1.Set(0); img2.WarpAffine(img1, Matrix3x3.ShiftAffine(-preShiftX, -preShiftY).ToAffine(), InterpolationMode.Cubic); img2.Set(0); img1.WarpAffine(img2, Matrix3x3.RotAroundCenter(-preRotDeg, size, size).ToAffine(), InterpolationMode.Cubic); img1.Set(0); img2.WarpAffine(img1, Matrix3x3.ShiftAffine(-shiftX, -shiftY).ToAffine(), InterpolationMode.Cubic); img1.Mul(65535); img1.Convert(img16u, NppRoundMode.Near); img16u.CopyToHost(mRawImage, size * sizeof(ushort)); double[] colorMatrix = new double[] { 1, 0, 0, 0, 1, 0, 0, 0, 1 }; mColorSpec = new PentaxPefFile.DNGColorSpec(colorMatrix, colorMatrix, PentaxPefFile.IFDDNGCalibrationIlluminant.Illuminant.D50, PentaxPefFile.IFDDNGCalibrationIlluminant.Illuminant.D65, new float[] { 1f, 1, 1f }); mOrientation = new PentaxPefFile.DNGOrientation(PentaxPefFile.DNGOrientation.Orientation.Normal); mWidth = size; mHeight = size; mCropLeft = 0; mCropTop = 0; mCroppedWidth = size; mCroppedHeight = size; mBitDepth = 16; mISO = 100; mBayerPattern = new BayerColor[] { BayerColor.Red, BayerColor.Cyan, BayerColor.Blue }; mWhiteLevel = new float[] { 65535, 65535, 65535 }; mBlackLevel = new float[] { 0, 0, 0 }; mWhiteBalance = new float[] { 1, 1, 1 };; mRollAngle = 0; mRollAnglePresent = false; mNoiseModelAlpha = float.Epsilon; mNoiseModelBeta = 0; mExposureTime = new PentaxPefFile.Rational(1, 1); mRecordingDate = DateTime.Now; mMake = "None"; mUniqueModelName = "Test file"; img1.Dispose(); img2.Dispose(); img16u.Dispose(); }