question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Alternative to DecodeJPEG/DecodePNG

See original GitHub issue

Is there an alternative method to create input tensors other than using DecodeJPEG or DecodePNG? I’ve come across this post which kinda goes along the same path i’m trying to follow, but then I run into TFException: Expects arg[0] to be uint8 but float is provided errors.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:8

github_iconTop GitHub Comments

1reaction
MykolaKovalchukcommented, Jul 21, 2018

I have loaded image, accessed its pixels, and created tensor separately from a float array, e.g.:

		TFTensor ImageToTensor(IndexedImage image)
		{
			var matrix = new float[1, image.Size.Height, image.Size.Width, 3];
			
			using (image.LockPixels(out var pixels))
			{
				for (var iy = 0; iy < image.Size.Height; iy++)
				{
					for (int ix = 0, index = iy * image.Size.Width; ix < image.Size.Width; ix++, index++)
					{
						var pixel = pixels[index];
						matrix[0, iy, ix, 0] = pixel.Blue() / 255.0f;
						matrix[0, iy, ix, 1] = pixel.Green() / 255.0f;
						matrix[0, iy, ix, 2] = pixel.Red() / 255.0f;
					}
				}
			}

			TFTensor tensor = matrix;
			return tensor;
		}

Here IndexedImage is my own class, and to load images I use System.Graphics. Before this I did my own image cropping and scaling as needed. And for training in Python I used OpenCV to load and transform images, so colors order here is BGR.

0reactions
dhanarfmlxcommented, Jul 23, 2019

I will just leave it here: convert bitmap to tensor

Advantage: Self contained, no need additional dependency Disadvantage: Uses unsafe for performance reason

private TFTensor ConvertToTensor( Bitmap image )
{
  var depth = Image.GetPixelFormatSize( image.PixelFormat ) / 8;

  if( depth != 3 )
  {
	throw new NotSupportedException( "Only 24-bit images are supported" );
  }

  var matrix = new byte[1, image.Height, image.Width, 3];

  var data = image.LockBits( new Rectangle( 0, 0, image.Width, image.Height ), ImageLockMode.ReadOnly, image.PixelFormat );

  try
  {
	unsafe
	{
	  Parallel.For( 0, image.Height, y =>
	  {
		var line = (byte*)data.Scan0 + y * data.Stride;

		for( var x = 0; x < data.Width; x++ )
		{
		  var red = line[x * depth];
		  var green = line[x * depth + 1];
		  var blue = line[x * depth + 2];

		  matrix[0, y, x, 0] = blue;
		  matrix[0, y, x, 1] = green;
		  matrix[0, y, x, 2] = red;
		}
	  } );
	}
  }
  finally
  {
	image.UnlockBits( data );
  }

  return matrix;
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Announcing zune-jpeg: Rust's fastest JPEG decoder : r/rust
A safe replacement for one of the fastest JPEG image codec libraries, this is terrific! What could possibly explain the ±10 ms difference...
Read more >
Understanding and Decoding a JPEG Image using Python
Hi everyone! Today we are going to understand the JPEG compression algorithm. One thing a lot of people don't know is that JPEG...
Read more >
Progressive PNG Partial Decode? - Development
I've been interested in pushing the image formats I work with to allow for as close to random access as possible. PNG format...
Read more >
The Fastest, Safest PNG Decoder in the World
Summary: Wuffs' PNG image decoder is memory-safe but can also clock between 1.22x and 2.75x faster than libpng , the widely used open...
Read more >
python 3.x - Decode image bytes data stream to JPEG
I am struggling to successfully decode a JPEG image from bytes, back to JPEG again. I started from encoded frame from a MJPG...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found