partial void TappedDetectDigit(UIButton sender) { // get the digitView context so we can get the pixel values from it to intput to network var context = DigitView.GetViewContext(); // validate NeuralNetwork was initialized properly if (runningNet == null) { throw new InvalidProgramException(); } // putting input into MTLTexture in the MPSImage var region = new MTLRegion(new MTLOrigin(0, 0, 0), new MTLSize((nint)mnistInputWidth, mnistInputHeight, 1)); runningNet.SrcImage.Texture.ReplaceRegion(region, level: 0, slice: 0, pixelBytes: context.Data, bytesPerRow: mnistInputWidth, bytesPerImage: 0); // run the network forward pass var label = runningNet.Forward(); // show the prediction PredictionLabel.Text = $"{label}"; PredictionLabel.Hidden = false; }
private void PlatformConstruct( GraphicsDevice graphicsDevice, PixelFormat pixelFormat, int arraySize, int width, int height, TextureBindFlags bindFlags, int mipMapCount, TextureMipMapData[] mipMapData) { var descriptor = new MTLTextureDescriptor { ArrayLength = (nuint) arraySize, CpuCacheMode = MTLCpuCacheMode.DefaultCache, Depth = 1, Height = (nuint) height, MipmapLevelCount = (nuint) mipMapCount, PixelFormat = pixelFormat.ToMTLPixelFormat(), ResourceOptions = MTLResourceOptions.StorageModeManaged, SampleCount = 1, StorageMode = MTLStorageMode.Managed, TextureType = arraySize > 1 ? MTLTextureType.k2DArray : MTLTextureType.k2D, Width = (nuint) width, Usage = bindFlags.ToMTLTextureUsage() }; DeviceTexture = AddDisposable(graphicsDevice.Device.CreateTexture(descriptor)); if (mipMapData != null) { for (var i = 0; i < mipMapData.Length; i++) { var mipWidth = CalculateMipMapSize(i, width); var mipHeight = CalculateMipMapSize(i, height); var region = MTLRegion.Create2D(0, 0, mipWidth, mipHeight); var dataHandle = GCHandle.Alloc(mipMapData[i].Data, GCHandleType.Pinned); try { DeviceTexture.ReplaceRegion( region, (nuint) i, dataHandle.AddrOfPinnedObject(), (nuint) mipMapData[i].BytesPerRow); } finally { dataHandle.Free(); } } } }
private void SetData(int level, byte[] data, int bytesPerRow) { var processedData = data; var processedBytesPerRow = bytesPerRow; // TODO: Don't do this for Mac. switch (_originalPixelFormat) { case PixelFormat.Bc1: case PixelFormat.Bc2: case PixelFormat.Bc3: processedData = BlockCompressionUtility.Decompress( _originalPixelFormat, data, bytesPerRow, out processedBytesPerRow); break; } // TODO: This isn't correct for mip levels > 0. var region = MTLRegion.Create2D(0, 0, DeviceTexture.Width, DeviceTexture.Height); var pinnedArray = GCHandle.Alloc(processedData, GCHandleType.Pinned); try { var dataPointer = pinnedArray.AddrOfPinnedObject(); DeviceTexture.ReplaceRegion( region, (uint)level, dataPointer, (uint)processedBytesPerRow); } finally { pinnedArray.Free(); } }
public bool Finalize(IMTLDevice device) { if (MetalTexture != null) { return(true); } UIImage image = UIImage.FromFile(path); if (image == null) { return(false); } using (CGColorSpace colorSpace = CGColorSpace.CreateDeviceRGB()) { if (colorSpace == null) { return(false); } Width = image.CGImage.Width; Height = image.CGImage.Height; nuint width = (nuint)Width; nuint height = (nuint)Height; nuint rowBytes = width * 4; var context = new CGBitmapContext(IntPtr.Zero, (int)width, (int)height, 8, (int)rowBytes, colorSpace, CGImageAlphaInfo.PremultipliedLast); if (context == null) { return(false); } var bounds = new CGRect(0f, 0f, width, height); context.ClearRect(bounds); // Vertical Reflect if (flip) { context.TranslateCTM(width, height); context.ScaleCTM(-1f, -1f); } context.DrawImage(bounds, image.CGImage); MTLTextureDescriptor texDesc = MTLTextureDescriptor.CreateTexture2DDescriptor(MTLPixelFormat.RGBA8Unorm, width, height, false); if (texDesc == null) { return(false); } MetalTexture = device.CreateTexture(texDesc); if (MetalTexture == null) { context.Dispose(); return(false); } IntPtr pixels = context.Data; if (pixels != IntPtr.Zero) { var region = new MTLRegion(); region.Origin.X = 0; region.Origin.Y = 0; region.Size.Width = (nint)width; region.Size.Height = (nint)height; MetalTexture.ReplaceRegion(region, 0, pixels, rowBytes); } context.Dispose(); } return(true); }
partial void TappedDetectDigit (UIButton sender) { // get the digitView context so we can get the pixel values from it to intput to network var context = DigitView.GetViewContext (); // validate NeuralNetwork was initialized properly if (runningNet == null) throw new InvalidProgramException (); // putting input into MTLTexture in the MPSImage var region = new MTLRegion (new MTLOrigin (0, 0, 0), new MTLSize ((nint)mnistInputWidth, mnistInputHeight, 1)); runningNet.SrcImage.Texture.ReplaceRegion (region, level: 0, slice: 0, pixelBytes: context.Data, bytesPerRow: mnistInputWidth, bytesPerImage: 0); // run the network forward pass var label = runningNet.Forward (); // show the prediction PredictionLabel.Text = $"{label}"; PredictionLabel.Hidden = false; }
public bool Finalize (IMTLDevice device) { if (MetalTexture != null) return true; UIImage image = UIImage.FromFile (path); if (image == null) return false; using (CGColorSpace colorSpace = CGColorSpace.CreateDeviceRGB ()) { if (colorSpace == null) return false; Width = image.CGImage.Width; Height = image.CGImage.Height; nuint width = (nuint)Width; nuint height = (nuint)Height; nuint rowBytes = width * 4; var context = new CGBitmapContext (IntPtr.Zero, (int)width, (int)height, 8, (int)rowBytes, colorSpace, CGImageAlphaInfo.PremultipliedLast); if (context == null) return false; var bounds = new CGRect (0f, 0f, width, height); context.ClearRect (bounds); // Vertical Reflect if (flip) { context.TranslateCTM (width, height); context.ScaleCTM (-1f, -1f); } context.DrawImage (bounds, image.CGImage); MTLTextureDescriptor texDesc = MTLTextureDescriptor.CreateTexture2DDescriptor (MTLPixelFormat.RGBA8Unorm, width, height, false); if (texDesc == null) return false; MetalTexture = device.CreateTexture (texDesc); if (MetalTexture == null) { context.Dispose (); return false; } IntPtr pixels = context.Data; if (pixels != IntPtr.Zero) { var region = new MTLRegion (); region.Origin.X = 0; region.Origin.Y = 0; region.Size.Width = (nint)width; region.Size.Height = (nint)height; MetalTexture.ReplaceRegion (region, 0, pixels, rowBytes); } context.Dispose (); } return true; }